ClassfileTestHelper.java revision 1754:ddb4a2bfcd82
1/*
2 * Copyright (c) 2012, 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
24import java.lang.annotation.*;
25import java.io.*;
26import java.net.URL;
27import java.util.List;
28
29import com.sun.tools.classfile.*;
30
31public class ClassfileTestHelper {
32    int expected_tinvisibles = 0;
33    int expected_tvisibles = 0;
34    int expected_invisibles = 0;
35    int expected_visibles = 0;
36
37    //Makes debugging much easier. Set to 'false' for less output.
38    public Boolean verbose = true;
39    void println(String msg) { if(verbose) System.out.println(msg); }
40
41    File writeTestFile(String fname, String source) throws IOException {
42      File f = new File(fname);
43        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
44        out.println(source);
45        out.close();
46        return f;
47    }
48
49    File compile(File f) {
50        int rc = com.sun.tools.javac.Main.compile(new String[] {
51                "-source", "1.8", "-g", f.getPath() });
52        if (rc != 0)
53            throw new Error("compilation failed. rc=" + rc);
54        String path = f.getPath();
55        return new File(path.substring(0, path.length() - 5) + ".class");
56    }
57
58    ClassFile getClassFile(String name) throws IOException, ConstantPoolException {
59        URL url = getClass().getResource(name);
60        InputStream in = url.openStream();
61        try {
62            return ClassFile.read(in);
63        } finally {
64            in.close();
65        }
66    }
67
68    ClassFile getClassFile(URL url) throws IOException, ConstantPoolException {
69        InputStream in = url.openStream();
70        try {
71            return ClassFile.read(in);
72        } finally {
73            in.close();
74        }
75    }
76
77    /************ Helper annotations counting methods ******************/
78    void test(ClassFile cf) {
79        test("CLASS",cf, null, null, Attribute.RuntimeVisibleTypeAnnotations, true);
80        test("CLASS",cf, null, null, Attribute.RuntimeInvisibleTypeAnnotations, false);
81        //RuntimeAnnotations since one annotation can result in two attributes.
82        test("CLASS",cf, null, null, Attribute.RuntimeVisibleAnnotations, true);
83        test("CLASS",cf, null, null, Attribute.RuntimeInvisibleAnnotations, false);
84    }
85
86    void test(ClassFile cf, Field f, Boolean local) {
87        if (!local) {
88            test("FIELD",cf, f, null, Attribute.RuntimeVisibleTypeAnnotations, true);
89            test("FIELD",cf, f, null, Attribute.RuntimeInvisibleTypeAnnotations, false);
90            test("FIELD",cf, f, null, Attribute.RuntimeVisibleAnnotations, true);
91            test("FIELD",cf, f, null, Attribute.RuntimeInvisibleAnnotations, false);
92        } else {
93            test("CODE",cf, f, null, Attribute.RuntimeVisibleTypeAnnotations, true);
94            test("CODE",cf, f, null, Attribute.RuntimeInvisibleTypeAnnotations, false);
95            test("CODE",cf, f, null, Attribute.RuntimeVisibleAnnotations, true);
96            test("CODE",cf, f, null, Attribute.RuntimeInvisibleAnnotations, false);
97        }
98    }
99
100    void test(ClassFile cf, Field f) {
101        test(cf, f, false);
102    }
103
104    // 'local' determines whether to look for annotations in code attribute or not.
105    void test(ClassFile cf, Method m, Boolean local) {
106        if (!local) {
107            test("METHOD",cf, null, m, Attribute.RuntimeVisibleTypeAnnotations, true);
108            test("METHOD",cf, null, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
109            test("METHOD",cf, null, m, Attribute.RuntimeVisibleAnnotations, true);
110            test("METHOD",cf, null, m, Attribute.RuntimeInvisibleAnnotations, false);
111        } else  {
112            test("MCODE",cf, null, m, Attribute.RuntimeVisibleTypeAnnotations, true);
113            test("MCODE",cf, null, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
114            test("MCODE",cf, null, m, Attribute.RuntimeVisibleAnnotations, true);
115            test("MCODE",cf, null, m, Attribute.RuntimeInvisibleAnnotations, false);
116        }
117    }
118
119    // default to not looking in code attribute
120    void test(ClassFile cf, Method m ) {
121        test(cf, m, false);
122    }
123
124    // Test the result of Attributes.getIndex according to expectations
125    // encoded in the class/field/method name; increment annotations counts.
126    void test(String ttype, ClassFile cf, Field f, Method m, String annName, boolean visible) {
127        String testtype = ttype;
128        String name = null;
129        int index = -1;
130        Attribute attr = null;
131        Code_attribute cAttr = null;
132        boolean isTAattr = annName.contains("TypeAnnotations");
133        try {
134            switch(testtype) {
135                case "FIELD":
136                    name = f.getName(cf.constant_pool);
137                    index = f.attributes.getIndex(cf.constant_pool, annName);
138                    if(index!= -1)
139                        attr = f.attributes.get(index);
140                    break;
141                case "CODE":
142                    name = f.getName(cf.constant_pool);
143                    //fetch index of and code attribute and annotations from code attribute.
144                    index = cf.attributes.getIndex(cf.constant_pool, Attribute.Code);
145                    if(index!= -1) {
146                        attr = cf.attributes.get(index);
147                        assert attr instanceof Code_attribute;
148                        cAttr = (Code_attribute)attr;
149                        index = cAttr.attributes.getIndex(cf.constant_pool, annName);
150                        if(index!= -1)
151                            attr = cAttr.attributes.get(index);
152                    }
153                    break;
154                case "METHOD":
155                    name = m.getName(cf.constant_pool);
156                    index = m.attributes.getIndex(cf.constant_pool, annName);
157                    if(index!= -1)
158                        attr = m.attributes.get(index);
159                    break;
160                case "MCODE":
161                    name = m.getName(cf.constant_pool);
162                    //fetch index of and code attribute and annotations from code attribute.
163                    index = m.attributes.getIndex(cf.constant_pool, Attribute.Code);
164                    if(index!= -1) {
165                        attr = m.attributes.get(index);
166                        assert attr instanceof Code_attribute;
167                        cAttr = (Code_attribute)attr;
168                        index = cAttr.attributes.getIndex(cf.constant_pool, annName);
169                        if(index!= -1)
170                            attr = cAttr.attributes.get(index);
171                    }
172                    break;
173                default:
174                    name = cf.getName();
175                    index = cf.attributes.getIndex(cf.constant_pool, annName);
176                    if(index!= -1) attr = cf.attributes.get(index);
177            }
178        } catch(ConstantPoolException cpe) { cpe.printStackTrace(); }
179
180        if (index != -1) {
181            if(isTAattr) { //count RuntimeTypeAnnotations
182                RuntimeTypeAnnotations_attribute tAttr =
183                        (RuntimeTypeAnnotations_attribute)attr;
184                println(testtype + ": " + name + ", " + annName + ": " +
185                        tAttr.annotations.length );
186                allt += tAttr.annotations.length;
187                if (visible)
188                    tvisibles += tAttr.annotations.length;
189                else
190                    tinvisibles += tAttr.annotations.length;
191            } else {
192                RuntimeAnnotations_attribute tAttr =
193                        (RuntimeAnnotations_attribute)attr;
194                println(testtype + ": " + name + ", " + annName + ": " +
195                        tAttr.annotations.length );
196                all += tAttr.annotations.length;
197                if (visible)
198                    visibles += tAttr.annotations.length;
199                else
200                    invisibles += tAttr.annotations.length;
201            }
202        }
203    }
204
205    void countAnnotations() {
206        errors=0;
207        int expected_allt = expected_tvisibles + expected_tinvisibles;
208        int expected_all = expected_visibles + expected_invisibles;
209
210        if (expected_allt != allt) {
211            errors++;
212            System.err.println("Failure: expected " + expected_allt +
213                    " type annotations but found " + allt);
214        }
215        if (expected_all != all) {
216            errors++;
217            System.err.println("Failure: expected " + expected_all +
218                    " annotations but found " + all);
219        }
220        if (expected_tvisibles != tvisibles) {
221            errors++;
222            System.err.println("Failure: expected " + expected_tvisibles +
223                    " typevisible annotations but found " + tvisibles);
224        }
225
226        if (expected_tinvisibles != tinvisibles) {
227            errors++;
228            System.err.println("Failure: expected " + expected_tinvisibles +
229                    " typeinvisible annotations but found " + tinvisibles);
230        }
231        allt=0;
232        tvisibles=0;
233        tinvisibles=0;
234        all=0;
235        visibles=0;
236        invisibles=0;
237    }
238
239    int errors;
240    int allt;
241    int tvisibles;
242    int tinvisibles;
243    int all;
244    int visibles;
245    int invisibles;
246}
247