test/tools/javac/annotations/typeAnnotations/classfile/ClassfileTestHelper.java

Tue, 12 Mar 2013 17:39:34 +0100

author
jfranck
date
Tue, 12 Mar 2013 17:39:34 +0100
changeset 1629
f427043f8c65
parent 1534
bec996065c45
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

7196531: Duplicate error messages on repeating annotations
Reviewed-by: jjg

jjg@1521 1 /*
darcy@1534 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1521 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1521 4 *
jjg@1521 5 * This code is free software; you can redistribute it and/or modify it
jjg@1521 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1521 7 * published by the Free Software Foundation.
jjg@1521 8 *
jjg@1521 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1521 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1521 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1521 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1521 13 * accompanied this code).
jjg@1521 14 *
jjg@1521 15 * You should have received a copy of the GNU General Public License version
jjg@1521 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1521 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1521 18 *
jjg@1521 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1521 20 * or visit www.oracle.com if you need additional information or have any
jjg@1521 21 * questions.
jjg@1521 22 */
jjg@1521 23
jjg@1521 24 import java.lang.annotation.*;
jjg@1521 25 import java.io.*;
jjg@1521 26 import java.net.URL;
jjg@1521 27 import java.util.List;
jjg@1521 28
jjg@1521 29 import com.sun.tools.classfile.*;
jjg@1521 30
jjg@1521 31 public class ClassfileTestHelper {
jjg@1521 32 int expected_tinvisibles = 0;
jjg@1521 33 int expected_tvisibles = 0;
jjg@1521 34 int expected_invisibles = 0;
jjg@1521 35 int expected_visibles = 0;
jjg@1521 36
jjg@1521 37 //Makes debugging much easier. Set to 'false' for less output.
jjg@1521 38 public Boolean verbose = true;
jjg@1521 39 void println(String msg) { if(verbose) System.out.println(msg); }
jjg@1521 40
jjg@1521 41 File writeTestFile(String fname, String source) throws IOException {
jjg@1521 42 File f = new File(fname);
jjg@1521 43 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
jjg@1521 44 out.println(source);
jjg@1521 45 out.close();
jjg@1521 46 return f;
jjg@1521 47 }
jjg@1521 48
jjg@1521 49 File compile(File f) {
jjg@1521 50 int rc = com.sun.tools.javac.Main.compile(new String[] {
jjg@1521 51 "-source", "1.8", "-g", f.getPath() });
jjg@1521 52 if (rc != 0)
jjg@1521 53 throw new Error("compilation failed. rc=" + rc);
jjg@1521 54 String path = f.getPath();
jjg@1521 55 return new File(path.substring(0, path.length() - 5) + ".class");
jjg@1521 56 }
jjg@1521 57
jjg@1521 58 ClassFile getClassFile(String name) throws IOException, ConstantPoolException {
jjg@1521 59 URL url = getClass().getResource(name);
jjg@1521 60 InputStream in = url.openStream();
jjg@1521 61 try {
jjg@1521 62 return ClassFile.read(in);
jjg@1521 63 } finally {
jjg@1521 64 in.close();
jjg@1521 65 }
jjg@1521 66 }
jjg@1521 67
jjg@1521 68 ClassFile getClassFile(URL url) throws IOException, ConstantPoolException {
jjg@1521 69 InputStream in = url.openStream();
jjg@1521 70 try {
jjg@1521 71 return ClassFile.read(in);
jjg@1521 72 } finally {
jjg@1521 73 in.close();
jjg@1521 74 }
jjg@1521 75 }
jjg@1521 76
jjg@1521 77 /************ Helper annotations counting methods ******************/
jjg@1521 78 void test(ClassFile cf) {
jjg@1521 79 test("CLASS",cf, null, null, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@1521 80 test("CLASS",cf, null, null, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@1521 81 //RuntimeAnnotations since one annotation can result in two attributes.
jjg@1521 82 test("CLASS",cf, null, null, Attribute.RuntimeVisibleAnnotations, true);
jjg@1521 83 test("CLASS",cf, null, null, Attribute.RuntimeInvisibleAnnotations, false);
jjg@1521 84 }
jjg@1521 85
jjg@1521 86 void test(ClassFile cf, Method m) {
jjg@1521 87 test("METHOD",cf, null, m, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@1521 88 test("METHOD",cf, null, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@1521 89 test("METHOD",cf, null, m, Attribute.RuntimeVisibleAnnotations, true);
jjg@1521 90 test("METHOD",cf, null, m, Attribute.RuntimeInvisibleAnnotations, false);
jjg@1521 91 }
jjg@1521 92
jjg@1521 93 void test(ClassFile cf, Field f) {
jjg@1521 94 test("FIELD",cf, f, null, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@1521 95 test("FIELD",cf, f, null, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@1521 96 test("FIELD",cf, f, null, Attribute.RuntimeVisibleAnnotations, true);
jjg@1521 97 test("FIELD",cf, f, null, Attribute.RuntimeInvisibleAnnotations, false);
jjg@1521 98 }
jjg@1521 99
jjg@1521 100
jjg@1521 101 // Test the result of Attributes.getIndex according to expectations
jjg@1521 102 // encoded in the class/field/method name; increment annotations counts.
jjg@1521 103 void test(String ttype, ClassFile cf, Field f, Method m, String annName, boolean visible) {
jjg@1521 104 String testtype = ttype;
jjg@1521 105 String name = null;
jjg@1521 106 int index = -1;
jjg@1521 107 Attribute attr = null;
jjg@1521 108 boolean isTAattr = annName.contains("TypeAnnotations");
jjg@1521 109 try {
jjg@1521 110 switch(testtype) {
jjg@1521 111 case "FIELD":
jjg@1521 112 name = f.getName(cf.constant_pool);
jjg@1521 113 index = f.attributes.getIndex(cf.constant_pool, annName);
jjg@1521 114 if(index!= -1) attr = f.attributes.get(index);
jjg@1521 115 break;
jjg@1521 116 case "METHOD":
jjg@1521 117 name = m.getName(cf.constant_pool);
jjg@1521 118 index = m.attributes.getIndex(cf.constant_pool, annName);
jjg@1521 119 if(index!= -1) attr = m.attributes.get(index);
jjg@1521 120 break;
jjg@1521 121 default:
jjg@1521 122 name = cf.getName();
jjg@1521 123 index = cf.attributes.getIndex(cf.constant_pool, annName);
jjg@1521 124 if(index!= -1) attr = cf.attributes.get(index);
jjg@1521 125 }
jjg@1521 126 } catch(ConstantPoolException cpe) { cpe.printStackTrace(); }
jjg@1521 127
jjg@1521 128 if (index != -1) {
jjg@1521 129 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@1521 130 if(isTAattr) { //count RuntimeTypeAnnotations
jjg@1521 131 RuntimeTypeAnnotations_attribute tAttr =
jjg@1521 132 (RuntimeTypeAnnotations_attribute)attr;
jjg@1521 133 println(testtype + ": " + name + ", " + annName + ": " +
jjg@1521 134 tAttr.annotations.length );
jjg@1521 135 allt += tAttr.annotations.length;
jjg@1521 136 if (visible)
jjg@1521 137 tvisibles += tAttr.annotations.length;
jjg@1521 138 else
jjg@1521 139 tinvisibles += tAttr.annotations.length;
jjg@1521 140 } else {
jjg@1521 141 RuntimeAnnotations_attribute tAttr =
jjg@1521 142 (RuntimeAnnotations_attribute)attr;
jjg@1521 143 println(testtype + ": " + name + ", " + annName + ": " +
jjg@1521 144 tAttr.annotations.length );
jjg@1521 145 all += tAttr.annotations.length;
jjg@1521 146 if (visible)
jjg@1521 147 visibles += tAttr.annotations.length;
jjg@1521 148 else
jjg@1521 149 invisibles += tAttr.annotations.length;
jjg@1521 150 }
jjg@1521 151 }
jjg@1521 152 }
jjg@1521 153
jjg@1521 154 void countAnnotations() {
jjg@1521 155 errors=0;
jjg@1521 156 int expected_allt = expected_tvisibles + expected_tinvisibles;
jjg@1521 157 int expected_all = expected_visibles + expected_invisibles;
jjg@1521 158
jjg@1521 159 if (expected_allt != allt) {
jjg@1521 160 errors++;
jjg@1521 161 System.err.println("Failure: expected " + expected_allt +
jjg@1521 162 " type annotations but found " + allt);
jjg@1521 163 }
jjg@1521 164 if (expected_all != all) {
jjg@1521 165 errors++;
jjg@1521 166 System.err.println("Failure: expected " + expected_all +
jjg@1521 167 " annotations but found " + all);
jjg@1521 168 }
jjg@1521 169 if (expected_tvisibles != tvisibles) {
jjg@1521 170 errors++;
jjg@1521 171 System.err.println("Failure: expected " + expected_tvisibles +
jjg@1521 172 " typevisible annotations but found " + tvisibles);
jjg@1521 173 }
jjg@1521 174
jjg@1521 175 if (expected_tinvisibles != tinvisibles) {
jjg@1521 176 errors++;
jjg@1521 177 System.err.println("Failure: expected " + expected_tinvisibles +
jjg@1521 178 " typeinvisible annotations but found " + tinvisibles);
jjg@1521 179 }
jjg@1521 180 allt=0;
jjg@1521 181 tvisibles=0;
jjg@1521 182 tinvisibles=0;
jjg@1521 183 all=0;
jjg@1521 184 visibles=0;
jjg@1521 185 invisibles=0;
jjg@1521 186 }
jjg@1521 187
jjg@1521 188 int errors;
jjg@1521 189 int allt;
jjg@1521 190 int tvisibles;
jjg@1521 191 int tinvisibles;
jjg@1521 192 int all;
jjg@1521 193 int visibles;
jjg@1521 194 int invisibles;
jjg@1521 195 }

mercurial