CreateBadClassFile.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2013, 2016, 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
24import java.io.*;
25import java.util.*;
26import javax.annotation.processing.*;
27import javax.lang.model.*;
28import javax.lang.model.element.*;
29import javax.tools.*;
30
31import com.sun.tools.classfile.*;
32import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;
33import com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info;
34import com.sun.tools.classfile.ConstantPool.CPInfo;
35
36/* Create an invalid classfile with version 51.0 and a static method in an interface.*/
37@SupportedAnnotationTypes("*")
38public class CreateBadClassFile extends AbstractProcessor {
39    public boolean process(Set<? extends TypeElement> elems, RoundEnvironment renv) {
40        if (++round == 1) {
41            ConstantPool cp = new ConstantPool(new CPInfo[] {
42                new CONSTANT_Utf8_info(""),                     //0
43                new CONSTANT_Utf8_info("Test"),                 //1
44                new CONSTANT_Class_info(null, 1),               //2
45                new CONSTANT_Utf8_info("java/lang/Object"),     //3
46                new CONSTANT_Class_info(null, 3),               //4
47                new CONSTANT_Utf8_info("test"),                 //5
48                new CONSTANT_Utf8_info("()V"),                  //6
49            });
50            ClassFile cf = new ClassFile(0xCAFEBABE,
51                          0,
52                          51,
53                          cp,
54                          new AccessFlags(AccessFlags.ACC_ABSTRACT |
55                                          AccessFlags.ACC_INTERFACE |
56                                          AccessFlags.ACC_PUBLIC),
57                          2,
58                          4,
59                          new int[0],
60                          new Field[0],
61                          new Method[] {
62                              //creating static method in 51.0 classfile:
63                              new Method(new AccessFlags(AccessFlags.ACC_PUBLIC |
64                                                         AccessFlags.ACC_STATIC),
65                                         5,
66                                         new Descriptor(6),
67                                         new Attributes(cp, new Attribute[0]))
68                          },
69                          new Attributes(cp, new Attribute[0]));
70            try {
71                JavaFileObject clazz = processingEnv.getFiler().createClassFile("Test");
72                try (OutputStream out = clazz.openOutputStream()) {
73                    new ClassWriter().write(cf, out);
74                }
75            } catch (IOException ex) {
76                ex.printStackTrace();
77            }
78        }
79        return false;
80    }
81
82    public SourceVersion getSupportedSourceVersion() {
83        return SourceVersion.latest();
84    }
85
86    int round = 0;
87}
88