langtools/test/tools/javac/diags/examples/InvalidStaticInterface/processors/CreateBadClassFile.java
changeset 21006 534673718919
child 36526 3b41f1c69604
equal deleted inserted replaced
21005:4db633bd7696 21006:534673718919
       
     1 /*
       
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import com.sun.tools.classfile.*;
       
    25 import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;
       
    26 import com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info;
       
    27 import com.sun.tools.classfile.ConstantPool.CPInfo;
       
    28 import java.io.*;
       
    29 import java.util.*;
       
    30 import javax.annotation.processing.*;
       
    31 import javax.lang.model.*;
       
    32 import javax.lang.model.element.*;
       
    33 import javax.tools.*;
       
    34 
       
    35 /* Create an invalid classfile with version 51.0 and a static method in an interface.*/
       
    36 @SupportedAnnotationTypes("*")
       
    37 public class CreateBadClassFile extends AbstractProcessor {
       
    38     public boolean process(Set<? extends TypeElement> elems, RoundEnvironment renv) {
       
    39         if (++round == 1) {
       
    40             ConstantPool cp = new ConstantPool(new CPInfo[] {
       
    41                 new CONSTANT_Utf8_info(""),                     //0
       
    42                 new CONSTANT_Utf8_info("Test"),                 //1
       
    43                 new CONSTANT_Class_info(null, 1),               //2
       
    44                 new CONSTANT_Utf8_info("java/lang/Object"),     //3
       
    45                 new CONSTANT_Class_info(null, 3),               //4
       
    46                 new CONSTANT_Utf8_info("test"),                 //5
       
    47                 new CONSTANT_Utf8_info("()V"),                  //6
       
    48             });
       
    49             ClassFile cf = new ClassFile(0xCAFEBABE,
       
    50                           0,
       
    51                           51,
       
    52                           cp,
       
    53                           new AccessFlags(AccessFlags.ACC_ABSTRACT |
       
    54                                           AccessFlags.ACC_INTERFACE |
       
    55                                           AccessFlags.ACC_PUBLIC),
       
    56                           2,
       
    57                           4,
       
    58                           new int[0],
       
    59                           new Field[0],
       
    60                           new Method[] {
       
    61                               //creating static method in 51.0 classfile:
       
    62                               new Method(new AccessFlags(AccessFlags.ACC_PUBLIC |
       
    63                                                          AccessFlags.ACC_STATIC),
       
    64                                          5,
       
    65                                          new Descriptor(6),
       
    66                                          new Attributes(cp, new Attribute[0]))
       
    67                           },
       
    68                           new Attributes(cp, new Attribute[0]));
       
    69             try {
       
    70                 JavaFileObject clazz = processingEnv.getFiler().createClassFile("Test");
       
    71                 try (OutputStream out = clazz.openOutputStream()) {
       
    72                     new ClassWriter().write(cf, out);
       
    73                 }
       
    74             } catch (IOException ex) {
       
    75                 ex.printStackTrace();
       
    76             }
       
    77         }
       
    78         return false;
       
    79     }
       
    80 
       
    81     public SourceVersion getSupportedSourceVersion() {
       
    82         return SourceVersion.latest();
       
    83     }
       
    84 
       
    85     int round = 0;
       
    86 }