Tue, 25 Nov 2014 08:30:52 -0500
8065132: Parameter annotations not updated when synthetic parameters are prepended
Summary: Cause javac to add synthetic parameters to Runtime[In]VisibleParameterAnnotations attributes
Reviewed-by: jjg, jfranck
1.1 --- a/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java Mon Nov 24 12:49:30 2014 -0500 1.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java Tue Nov 25 08:30:52 2014 -0500 1.3 @@ -670,6 +670,27 @@ 1.4 } 1.5 1.6 1.7 + private void writeParamAnnotations(List<VarSymbol> params, 1.8 + RetentionPolicy retention) { 1.9 + for (VarSymbol s : params) { 1.10 + ListBuffer<Attribute.Compound> buf = new ListBuffer<>(); 1.11 + for (Attribute.Compound a : s.getRawAttributes()) 1.12 + if (types.getRetention(a) == retention) 1.13 + buf.append(a); 1.14 + databuf.appendChar(buf.length()); 1.15 + for (Attribute.Compound a : buf) 1.16 + writeCompoundAttribute(a); 1.17 + } 1.18 + 1.19 + } 1.20 + 1.21 + private void writeParamAnnotations(MethodSymbol m, 1.22 + RetentionPolicy retention) { 1.23 + databuf.appendByte(m.params.length() + m.extraParams.length()); 1.24 + writeParamAnnotations(m.extraParams, retention); 1.25 + writeParamAnnotations(m.params, retention); 1.26 + } 1.27 + 1.28 /** Write method parameter annotations; 1.29 * return number of attributes written. 1.30 */ 1.31 @@ -692,31 +713,13 @@ 1.32 int attrCount = 0; 1.33 if (hasVisible) { 1.34 int attrIndex = writeAttr(names.RuntimeVisibleParameterAnnotations); 1.35 - databuf.appendByte(m.params.length()); 1.36 - for (VarSymbol s : m.params) { 1.37 - ListBuffer<Attribute.Compound> buf = new ListBuffer<Attribute.Compound>(); 1.38 - for (Attribute.Compound a : s.getRawAttributes()) 1.39 - if (types.getRetention(a) == RetentionPolicy.RUNTIME) 1.40 - buf.append(a); 1.41 - databuf.appendChar(buf.length()); 1.42 - for (Attribute.Compound a : buf) 1.43 - writeCompoundAttribute(a); 1.44 - } 1.45 + writeParamAnnotations(m, RetentionPolicy.RUNTIME); 1.46 endAttr(attrIndex); 1.47 attrCount++; 1.48 } 1.49 if (hasInvisible) { 1.50 int attrIndex = writeAttr(names.RuntimeInvisibleParameterAnnotations); 1.51 - databuf.appendByte(m.params.length()); 1.52 - for (VarSymbol s : m.params) { 1.53 - ListBuffer<Attribute.Compound> buf = new ListBuffer<Attribute.Compound>(); 1.54 - for (Attribute.Compound a : s.getRawAttributes()) 1.55 - if (types.getRetention(a) == RetentionPolicy.CLASS) 1.56 - buf.append(a); 1.57 - databuf.appendChar(buf.length()); 1.58 - for (Attribute.Compound a : buf) 1.59 - writeCompoundAttribute(a); 1.60 - } 1.61 + writeParamAnnotations(m, RetentionPolicy.CLASS); 1.62 endAttr(attrIndex); 1.63 attrCount++; 1.64 }
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/lib/annotations/annotations/classfile/ClassfileInspector.java Tue Nov 25 08:30:52 2014 -0500 2.3 @@ -0,0 +1,1733 @@ 2.4 +/* 2.5 + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +package annotations.classfile; 2.28 + 2.29 +import java.io.*; 2.30 +import java.net.URL; 2.31 +import java.util.List; 2.32 + 2.33 +import com.sun.tools.classfile.*; 2.34 + 2.35 +/** 2.36 + * A class providing utilities for writing tests that inspect class 2.37 + * files directly, looking for specific type annotations. 2.38 + * 2.39 + * Note: this framework does not currently handle repeating 2.40 + * annotations. 2.41 + */ 2.42 +public class ClassfileInspector { 2.43 + 2.44 + /** 2.45 + * A group of expected annotations to be found in a given class. 2.46 + * If the class name is null, then the template will be applied to 2.47 + * every class. 2.48 + */ 2.49 + public static class Expected { 2.50 + /** 2.51 + * The name of the class. If {@code null} this template will 2.52 + * apply to every class; otherwise, it will only be applied to 2.53 + * the named class. 2.54 + */ 2.55 + public final String classname; 2.56 + 2.57 + /** 2.58 + * The expected class annotations. These will be checked 2.59 + * against the class' attributes. 2.60 + */ 2.61 + public final ExpectedAnnotation[] classAnnos; 2.62 + 2.63 + /** 2.64 + * The expected method annotations. These will be checked 2.65 + * against all methods in the class. 2.66 + */ 2.67 + public final ExpectedMethodAnnotation[] methodAnnos; 2.68 + 2.69 + /** 2.70 + * The expected method parameter annotations. These will be checked 2.71 + * against all methods in the class. 2.72 + */ 2.73 + public final ExpectedParameterAnnotation[] methodParamAnnos; 2.74 + 2.75 + /** 2.76 + * The expected field type annotations. These will be checked 2.77 + * against all fields in the class. 2.78 + */ 2.79 + public final ExpectedFieldAnnotation[] fieldAnnos; 2.80 + 2.81 + /** 2.82 + * The expected class type annotations. These will be checked 2.83 + * against the class' attributes. 2.84 + */ 2.85 + public final ExpectedTypeAnnotation[] classTypeAnnos; 2.86 + 2.87 + /** 2.88 + * The expected method type annotations. These will be checked 2.89 + * against all methods in the class. 2.90 + */ 2.91 + public final ExpectedMethodTypeAnnotation[] methodTypeAnnos; 2.92 + 2.93 + /** 2.94 + * The expected field type annotations. These will be checked 2.95 + * against all fields in the class. 2.96 + */ 2.97 + public final ExpectedFieldTypeAnnotation[] fieldTypeAnnos; 2.98 + 2.99 + /** 2.100 + * Create an {@code Expected} from all components. 2.101 + * 2.102 + * @param classname The name of the class to match, or {@code 2.103 + * null} for all classes. 2.104 + * @param classAnnos The expected class annotations. 2.105 + * @param methodAnnos The expected method annotations. 2.106 + * @param methodParamAnnos The expected method parameter annotations. 2.107 + * @param fieldAnnos The expected field annotations. 2.108 + * @param classTypeAnnos The expected class type annotations. 2.109 + * @param methodTypeAnnos The expected method type annotations. 2.110 + * @param fieldTypeAnnos The expected field type annotations. 2.111 + */ 2.112 + public Expected(String classname, 2.113 + ExpectedAnnotation[] classAnnos, 2.114 + ExpectedMethodAnnotation[] methodAnnos, 2.115 + ExpectedParameterAnnotation[] methodParamAnnos, 2.116 + ExpectedFieldAnnotation[] fieldAnnos, 2.117 + ExpectedTypeAnnotation[] classTypeAnnos, 2.118 + ExpectedMethodTypeAnnotation[] methodTypeAnnos, 2.119 + ExpectedFieldTypeAnnotation[] fieldTypeAnnos) { 2.120 + this.classname = classname; 2.121 + this.classAnnos = classAnnos; 2.122 + this.methodAnnos = methodAnnos; 2.123 + this.methodParamAnnos = methodParamAnnos; 2.124 + this.fieldAnnos = fieldAnnos; 2.125 + this.classTypeAnnos = classTypeAnnos; 2.126 + this.methodTypeAnnos = methodTypeAnnos; 2.127 + this.fieldTypeAnnos = fieldTypeAnnos; 2.128 + } 2.129 + 2.130 + /** 2.131 + * Create an {@code Expected} from regular annotation components. 2.132 + * 2.133 + * @param classname The name of the class to match, or {@code 2.134 + * null} for all classes. 2.135 + * @param classAnnos The expected class annotations. 2.136 + * @param methodAnnos The expected method annotations. 2.137 + * @param methodParamAnnos The expected method parameter annotations. 2.138 + * @param fieldAnnos The expected field annotations. 2.139 + */ 2.140 + public Expected(String classname, 2.141 + ExpectedAnnotation[] classAnnos, 2.142 + ExpectedMethodAnnotation[] methodAnnos, 2.143 + ExpectedParameterAnnotation[] methodParamAnnos, 2.144 + ExpectedFieldAnnotation[] fieldAnnos) { 2.145 + this(classname, classAnnos, methodAnnos, methodParamAnnos, 2.146 + fieldAnnos, null, null, null); 2.147 + } 2.148 + 2.149 + /** 2.150 + * Create an {@code Expected} from type annotation components. 2.151 + * 2.152 + * @param classname The name of the class to match, or {@code 2.153 + * null} for all classes. 2.154 + * @param classTypeAnnos The expected class type annotations. 2.155 + * @param methodTypeAnnos The expected method type annotations. 2.156 + * @param fieldTypeAnnos The expected field type annotations. 2.157 + */ 2.158 + public Expected(String classname, 2.159 + ExpectedTypeAnnotation[] classTypeAnnos, 2.160 + ExpectedMethodTypeAnnotation[] methodTypeAnnos, 2.161 + ExpectedFieldTypeAnnotation[] fieldTypeAnnos) { 2.162 + this(classname, null, null, null, null, 2.163 + classTypeAnnos, methodTypeAnnos, fieldTypeAnnos); 2.164 + } 2.165 + 2.166 + public String toString() { 2.167 + final StringBuilder sb = new StringBuilder(); 2.168 + final String newline = System.lineSeparator(); 2.169 + sb.append("Expected on class ").append(classname); 2.170 + if (null != classAnnos) { 2.171 + sb.append(newline).append("Class annotations:").append(newline); 2.172 + for(ExpectedAnnotation anno : classAnnos) { 2.173 + sb.append(anno).append(newline); 2.174 + } 2.175 + } 2.176 + if (null != methodAnnos) { 2.177 + sb.append(newline).append("Method annotations:").append(newline); 2.178 + for(ExpectedAnnotation anno : methodAnnos) { 2.179 + sb.append(anno).append(newline); 2.180 + } 2.181 + } 2.182 + if (null != methodParamAnnos) { 2.183 + sb.append(newline).append("Method param annotations:").append(newline); 2.184 + for(ExpectedAnnotation anno : methodParamAnnos) { 2.185 + sb.append(anno).append(newline); 2.186 + } 2.187 + } 2.188 + if (null != fieldAnnos) { 2.189 + sb.append(newline).append("Field annotations:").append(newline); 2.190 + for(ExpectedAnnotation anno : fieldAnnos) { 2.191 + sb.append(anno).append(newline); 2.192 + } 2.193 + } 2.194 + if (null != classTypeAnnos) { 2.195 + sb.append(newline).append("Class type annotations:").append(newline); 2.196 + for(ExpectedAnnotation anno : classTypeAnnos) { 2.197 + sb.append(anno).append(newline); 2.198 + } 2.199 + } 2.200 + if (null != methodTypeAnnos) { 2.201 + sb.append(newline).append("Method type annotations:").append(newline); 2.202 + for(ExpectedAnnotation anno : methodTypeAnnos) { 2.203 + sb.append(anno).append(newline); 2.204 + } 2.205 + } 2.206 + if (null != fieldTypeAnnos) { 2.207 + sb.append(newline).append("Field type annotations:").append(newline); 2.208 + for(ExpectedAnnotation anno : fieldTypeAnnos) { 2.209 + sb.append(anno).append(newline); 2.210 + } 2.211 + } 2.212 + return sb.toString(); 2.213 + } 2.214 + 2.215 + /** 2.216 + * See if this template applies to a class. 2.217 + * 2.218 + * @param classname The classname to check. 2.219 + * @return Whether or not this template should apply. 2.220 + */ 2.221 + public boolean matchClassName(String classname) { 2.222 + return this.classname == null || this.classname.equals(classname); 2.223 + } 2.224 + 2.225 + /** 2.226 + * After applying the template to all classes, check to see if 2.227 + * any of the expected annotations weren't matched. 2.228 + * 2.229 + * @return The number of missed matches. 2.230 + */ 2.231 + public int check() { 2.232 + int count = 0; 2.233 + if (classAnnos != null) { 2.234 + for(ExpectedAnnotation expected : classAnnos) { 2.235 + if (!expected.check()) { 2.236 + count++; 2.237 + } 2.238 + } 2.239 + } 2.240 + if (methodAnnos != null) { 2.241 + for(ExpectedAnnotation expected : methodAnnos) { 2.242 + if (!expected.check()) { 2.243 + count++; 2.244 + } 2.245 + } 2.246 + } 2.247 + if (methodParamAnnos != null) { 2.248 + for(ExpectedAnnotation expected : methodParamAnnos) { 2.249 + if (!expected.check()) { 2.250 + count++; 2.251 + } 2.252 + } 2.253 + } 2.254 + if (fieldAnnos != null) { 2.255 + for(ExpectedAnnotation expected : fieldAnnos) { 2.256 + if (!expected.check()) { 2.257 + count++; 2.258 + } 2.259 + } 2.260 + } 2.261 + if (classTypeAnnos != null) { 2.262 + for(ExpectedAnnotation expected : classTypeAnnos) { 2.263 + if (!expected.check()) { 2.264 + count++; 2.265 + } 2.266 + } 2.267 + } 2.268 + if (methodTypeAnnos != null) { 2.269 + for(ExpectedAnnotation expected : methodTypeAnnos) { 2.270 + if (!expected.check()) { 2.271 + count++; 2.272 + } 2.273 + } 2.274 + } 2.275 + if (fieldTypeAnnos != null) { 2.276 + for(ExpectedAnnotation expected : fieldTypeAnnos) { 2.277 + if (!expected.check()) { 2.278 + count++; 2.279 + } 2.280 + } 2.281 + } 2.282 + return count; 2.283 + } 2.284 + } 2.285 + 2.286 + /** 2.287 + * An expected annotation. This is both a superclass for 2.288 + * method, field, and type annotations, as well as a class for 2.289 + * annotations on a class. 2.290 + */ 2.291 + public static class ExpectedAnnotation { 2.292 + protected int count = 0; 2.293 + protected final String expectedName; 2.294 + protected final int expectedCount; 2.295 + protected final boolean visibility; 2.296 + 2.297 + /** 2.298 + * Create an {@code ExpectedAnnotation} from its 2.299 + * components. It is usually a better idea to use a {@code 2.300 + * Builder} to do this. 2.301 + * 2.302 + * @param expectedName The expected annotation name. 2.303 + * @param visibility Whether this annotation should be runtime-visible. 2.304 + * @param expectedCount The number of annotations that should 2.305 + * be seen. If 0, this asserts that the 2.306 + * described annotation is not present. 2.307 + */ 2.308 + public ExpectedAnnotation(String expectedName, 2.309 + boolean visibility, 2.310 + int expectedCount) { 2.311 + this.expectedName = expectedName; 2.312 + this.visibility = visibility; 2.313 + this.expectedCount = expectedCount; 2.314 + } 2.315 + 2.316 + public String toString() { 2.317 + final StringBuilder sb = new StringBuilder(); 2.318 + sb.append("Expected "); 2.319 + sb.append(expectedCount); 2.320 + sb.append(" annotation "); 2.321 + sb.append(expectedName); 2.322 + sb.append(visibility ? ", runtime visibile " : ", runtime invisibile "); 2.323 + return sb.toString(); 2.324 + } 2.325 + 2.326 + /** 2.327 + * See if this template matches the given visibility. 2.328 + * 2.329 + * @param Whether or not the annotation is visible at runtime. 2.330 + * @return Whether or not this template matches the visibility. 2.331 + */ 2.332 + public boolean matchVisibility(boolean visibility) { 2.333 + return this.visibility == visibility; 2.334 + } 2.335 + 2.336 + /** 2.337 + * Attempty to match this template against an annotation. If 2.338 + * it does match, then the match count for the template will 2.339 + * be incremented. Otherwise, nothing will be done. 2.340 + * 2.341 + * @param anno The annotation to attempt to match. 2.342 + */ 2.343 + public void matchAnnotation(ConstantPool cpool, 2.344 + Annotation anno) { 2.345 + if (checkMatch(cpool, anno)) { 2.346 + count++; 2.347 + } 2.348 + } 2.349 + 2.350 + /** 2.351 + * Indicate whether an annotation matches this expected 2.352 + * annotation. 2.353 + * 2.354 + * @param ConstantPool The constant pool to use. 2.355 + * @param anno The annotation to check. 2.356 + * @return Whether the annotation matches. 2.357 + */ 2.358 + protected boolean checkMatch(ConstantPool cpool, 2.359 + Annotation anno) { 2.360 + try { 2.361 + return cpool.getUTF8Info(anno.type_index).value.equals("L" + expectedName + ";"); 2.362 + } catch(Exception e) { 2.363 + return false; 2.364 + } 2.365 + } 2.366 + 2.367 + /** 2.368 + * After all matching, check to see if the expected number of 2.369 + * matches equals the actual number. If not, then print a 2.370 + * failure message and return {@code false}. 2.371 + * 2.372 + * @return Whether or not the expected number of matched 2.373 + * equals the actual number. 2.374 + */ 2.375 + public boolean check() { 2.376 + if (count != expectedCount) { 2.377 + System.err.println(this + ", but saw " + count); 2.378 + return false; 2.379 + } else { 2.380 + return true; 2.381 + } 2.382 + } 2.383 + } 2.384 + 2.385 + /** 2.386 + * An annotation found on a method. 2.387 + */ 2.388 + public static class ExpectedMethodAnnotation extends ExpectedAnnotation { 2.389 + protected final String methodname; 2.390 + 2.391 + /** 2.392 + * Create an {@code ExpectedMethodAnnotation} from its 2.393 + * components. It is usually a better idea to use a {@code 2.394 + * Builder} to do this. 2.395 + * 2.396 + * @param methodname The expected method name. 2.397 + * @param expectedName The expected annotation name. 2.398 + * @param visibility Whether this annotation should be runtime-visible. 2.399 + * @param expectedCount The number of annotations that should be seen. 2.400 + */ 2.401 + public ExpectedMethodAnnotation(String methodname, 2.402 + String expectedName, 2.403 + boolean visibility, 2.404 + int expectedCount) { 2.405 + super(expectedName, visibility, expectedCount); 2.406 + this.methodname = methodname; 2.407 + } 2.408 + 2.409 + public String toString() { 2.410 + final StringBuilder sb = new StringBuilder(); 2.411 + sb.append("Expected "); 2.412 + sb.append(expectedCount); 2.413 + sb.append(" annotation "); 2.414 + sb.append(expectedName); 2.415 + sb.append(visibility ? ", runtime visibile " : ", runtime invisibile "); 2.416 + sb.append(" on method "); 2.417 + sb.append(methodname); 2.418 + return sb.toString(); 2.419 + } 2.420 + 2.421 + /** 2.422 + * See if this template applies to a method. 2.423 + * 2.424 + * @param methodname The method name to check. 2.425 + * @return Whether or not this template should apply. 2.426 + */ 2.427 + public boolean matchMethodName(String methodname) { 2.428 + return this.methodname.equals(methodname); 2.429 + } 2.430 + 2.431 + } 2.432 + 2.433 + /** 2.434 + * An annotation found on a method parameter. 2.435 + */ 2.436 + public static class ExpectedParameterAnnotation 2.437 + extends ExpectedMethodAnnotation { 2.438 + protected final int index; 2.439 + 2.440 + /** 2.441 + * Create an {@code ExpectedParameterAnnotation} from its 2.442 + * components. It is usually a better idea to use a {@code 2.443 + * Builder} to do this. 2.444 + * 2.445 + * @param methodname The expected method name. 2.446 + * @param index The parameter index. 2.447 + * @param expectedName The expected annotation name. 2.448 + * @param visibility Whether this annotation should be runtime-visible. 2.449 + * @param expectedCount The number of annotations that should be seen. 2.450 + */ 2.451 + public ExpectedParameterAnnotation(String methodname, 2.452 + int index, 2.453 + String expectedName, 2.454 + boolean visibility, 2.455 + int expectedCount) { 2.456 + super(methodname, expectedName, visibility, expectedCount); 2.457 + this.index = index; 2.458 + } 2.459 + 2.460 + public String toString() { 2.461 + final StringBuilder sb = new StringBuilder(); 2.462 + sb.append("Expected "); 2.463 + sb.append(expectedCount); 2.464 + sb.append(" annotation "); 2.465 + sb.append(expectedName); 2.466 + sb.append(visibility ? ", runtime visibile " : ", runtime invisibile "); 2.467 + sb.append(" on method "); 2.468 + sb.append(methodname); 2.469 + sb.append(" parameter " + index); 2.470 + return sb.toString(); 2.471 + } 2.472 + 2.473 + } 2.474 + 2.475 + /** 2.476 + * An annotation found on a field. 2.477 + */ 2.478 + public static class ExpectedFieldAnnotation extends ExpectedAnnotation { 2.479 + private final String fieldname; 2.480 + 2.481 + /** 2.482 + * Create an {@code ExpectedFieldAnnotation} from its 2.483 + * components. It is usually a better idea to use a {@code 2.484 + * Builder} to do this. 2.485 + * 2.486 + * @param fieldname The expected field name. 2.487 + * @param expectedName The expected annotation name. 2.488 + * @param visibility Whether this annotation should be runtime-visible. 2.489 + * @param expectedCount The number of annotations that should be seen. 2.490 + */ 2.491 + public ExpectedFieldAnnotation(String fieldname, 2.492 + String expectedName, 2.493 + boolean visibility, 2.494 + int expectedCount) { 2.495 + super(expectedName, visibility, expectedCount); 2.496 + this.fieldname = fieldname; 2.497 + } 2.498 + 2.499 + public String toString() { 2.500 + final StringBuilder sb = new StringBuilder(); 2.501 + sb.append("Expected ").append(expectedCount) 2.502 + .append(" annotation ").append(expectedName) 2.503 + .append(visibility ? ", runtime visibile " : ", runtime invisibile ") 2.504 + .append(" on field ").append(fieldname); 2.505 + return sb.toString(); 2.506 + } 2.507 + 2.508 + /** 2.509 + * See if this template applies to a field. 2.510 + * 2.511 + * @param fieldname The field name to check. 2.512 + * @return Whether or not this template should apply. 2.513 + */ 2.514 + public boolean matchFieldName(String fieldname) { 2.515 + return this.fieldname.equals(fieldname); 2.516 + } 2.517 + 2.518 + } 2.519 + 2.520 + /** 2.521 + * An expected type annotation. This is both a superclass for 2.522 + * method and field type annotations, as well as a class for type 2.523 + * annotations on a class. 2.524 + */ 2.525 + public static class ExpectedTypeAnnotation extends ExpectedAnnotation { 2.526 + protected final TypeAnnotation.TargetType targetType; 2.527 + protected final int bound_index; 2.528 + protected final int parameter_index; 2.529 + protected final int type_index; 2.530 + protected final int exception_index; 2.531 + protected final TypeAnnotation.Position.TypePathEntry[] typePath; 2.532 + 2.533 + /** 2.534 + * Create an {@code ExpectedTypeAnnotation} from its 2.535 + * components. It is usually a better idea to use a {@code 2.536 + * Builder} to do this. 2.537 + * 2.538 + * @param expectedName The expected annotation name. 2.539 + * @param visibility Whether this annotation should be runtime-visible. 2.540 + * @param expectedCount The number of annotations that should 2.541 + * be seen. If 0, this asserts that the 2.542 + * described annotation is not present. 2.543 + * @param targetType The expected target type. 2.544 + * @param bound_index The expected bound index, or {@code Integer.MIN_VALUE}. 2.545 + * @param parameter_index The expected parameter index, or 2.546 + * {@code Integer.MIN_VALUE}. 2.547 + * @param type_index The expected type index, or {@code Integer.MIN_VALUE}. 2.548 + * @param exception_index The expected exception index, or 2.549 + * {@code Integer.MIN_VALUE}. 2.550 + * @param typePath The expected type path. 2.551 + */ 2.552 + public ExpectedTypeAnnotation(String expectedName, 2.553 + boolean visibility, 2.554 + int expectedCount, 2.555 + TypeAnnotation.TargetType targetType, 2.556 + int bound_index, 2.557 + int parameter_index, 2.558 + int type_index, 2.559 + int exception_index, 2.560 + TypeAnnotation.Position.TypePathEntry... typePath) { 2.561 + super(expectedName, visibility, expectedCount); 2.562 + this.targetType = targetType; 2.563 + this.bound_index = bound_index; 2.564 + this.parameter_index = parameter_index; 2.565 + this.type_index = type_index; 2.566 + this.exception_index = exception_index; 2.567 + this.typePath = typePath; 2.568 + } 2.569 + 2.570 + public String toString() { 2.571 + final StringBuilder sb = new StringBuilder(); 2.572 + sb.append("Expected "); 2.573 + sb.append(expectedCount); 2.574 + sb.append(" annotation "); 2.575 + sb.append(expectedName); 2.576 + sb.append(visibility ? ", runtime visibile " : ", runtime invisibile "); 2.577 + sb.append(targetType); 2.578 + sb.append(", bound_index = "); 2.579 + sb.append(bound_index); 2.580 + sb.append(", parameter_index = "); 2.581 + sb.append(parameter_index); 2.582 + sb.append(", type_index = "); 2.583 + sb.append(type_index); 2.584 + sb.append(", exception_index = "); 2.585 + sb.append(exception_index); 2.586 + sb.append(", type_path = ["); 2.587 + for(int i = 0; i < typePath.length; i++) { 2.588 + if (i != 0) { 2.589 + sb.append(", "); 2.590 + } 2.591 + sb.append(typePath[i]); 2.592 + } 2.593 + sb.append("]"); 2.594 + return sb.toString(); 2.595 + } 2.596 + 2.597 + @Override 2.598 + public void matchAnnotation(ConstantPool cpool, 2.599 + Annotation anno) {} 2.600 + 2.601 + public void matchAnnotation(TypeAnnotation anno) { 2.602 + if (checkMatch(anno)) { 2.603 + count++; 2.604 + } 2.605 + } 2.606 + 2.607 + public boolean checkMatch(TypeAnnotation anno) { 2.608 + boolean matches = checkMatch(anno.constant_pool, anno.annotation); 2.609 + 2.610 + matches = matches && anno.position.type == targetType; 2.611 + matches = matches && anno.position.bound_index == bound_index; 2.612 + matches = matches && anno.position.parameter_index == parameter_index; 2.613 + matches = matches && anno.position.type_index == type_index; 2.614 + matches = matches && anno.position.exception_index == exception_index; 2.615 + matches = matches && anno.position.location.size() == typePath.length; 2.616 + 2.617 + if (matches) { 2.618 + int i = 0; 2.619 + for(TypeAnnotation.Position.TypePathEntry entry : 2.620 + anno.position.location) { 2.621 + matches = matches && typePath[i++].equals(entry); 2.622 + } 2.623 + } 2.624 + 2.625 + return matches; 2.626 + } 2.627 + 2.628 + /** 2.629 + * A builder class for creating {@code 2.630 + * ExpectedTypeAnnotation}s in a more convenient fashion. The 2.631 + * constructor for {@code ExpectedTypeAnnotation} takes a 2.632 + * large number of parameters (by necessity). This class 2.633 + * allows users to construct a {@code ExpectedTypeAnnotation}s 2.634 + * using only the ones they need. 2.635 + */ 2.636 + public static class Builder { 2.637 + protected final String expectedName; 2.638 + protected final boolean visibility; 2.639 + protected final int expectedCount; 2.640 + protected final TypeAnnotation.TargetType targetType; 2.641 + protected int bound_index = Integer.MIN_VALUE; 2.642 + protected int parameter_index = Integer.MIN_VALUE; 2.643 + protected int type_index = Integer.MIN_VALUE; 2.644 + protected int exception_index = Integer.MIN_VALUE; 2.645 + protected TypeAnnotation.Position.TypePathEntry[] typePath = 2.646 + new TypeAnnotation.Position.TypePathEntry[0]; 2.647 + 2.648 + /** 2.649 + * Create a {@code Builder} from the mandatory parameters. 2.650 + * 2.651 + * @param expectedName The expected annotation name. 2.652 + * @param targetType The expected target type. 2.653 + * @param visibility Whether this annotation should be runtime-visible. 2.654 + * @param expectedCount The number of annotations that should be seen. 2.655 + */ 2.656 + public Builder(String expectedName, 2.657 + TypeAnnotation.TargetType targetType, 2.658 + boolean visibility, 2.659 + int expectedCount) { 2.660 + this.expectedName = expectedName; 2.661 + this.visibility = visibility; 2.662 + this.expectedCount = expectedCount; 2.663 + this.targetType = targetType; 2.664 + } 2.665 + 2.666 + /** 2.667 + * Create an {@code ExpectedTypeAnnotation} from all 2.668 + * parameters that have been provided. The default values 2.669 + * will be used for those that have not. 2.670 + * 2.671 + * @return The cretaed {@code ExpectedTypeAnnotation}. 2.672 + */ 2.673 + public ExpectedTypeAnnotation build() { 2.674 + return new ExpectedTypeAnnotation(expectedName, visibility, 2.675 + expectedCount, targetType, 2.676 + bound_index, parameter_index, 2.677 + type_index, exception_index, 2.678 + typePath); 2.679 + } 2.680 + 2.681 + /** 2.682 + * Provide a bound index parameter. 2.683 + * 2.684 + * @param bound_index The bound_index value. 2.685 + */ 2.686 + public Builder setBoundIndex(int bound_index) { 2.687 + this.bound_index = bound_index; 2.688 + return this; 2.689 + } 2.690 + 2.691 + /** 2.692 + * Provide a parameter index parameter. 2.693 + * 2.694 + * @param bound_index The parameter_index value. 2.695 + */ 2.696 + public Builder setParameterIndex(int parameter_index) { 2.697 + this.parameter_index = parameter_index; 2.698 + return this; 2.699 + } 2.700 + 2.701 + /** 2.702 + * Provide a type index parameter. 2.703 + * 2.704 + * @param type_index The type_index value. 2.705 + */ 2.706 + public Builder setTypeIndex(int type_index) { 2.707 + this.type_index = type_index; 2.708 + return this; 2.709 + } 2.710 + 2.711 + /** 2.712 + * Provide an exception index parameter. 2.713 + * 2.714 + * @param exception_index The exception_index value. 2.715 + */ 2.716 + public Builder setExceptionIndex(int exception_index) { 2.717 + this.exception_index = exception_index; 2.718 + return this; 2.719 + } 2.720 + 2.721 + /** 2.722 + * Provide a type path parameter. 2.723 + * 2.724 + * @param typePath The type path value. 2.725 + */ 2.726 + public Builder setTypePath(TypeAnnotation.Position.TypePathEntry[] typePath) { 2.727 + this.typePath = typePath; 2.728 + return this; 2.729 + } 2.730 + } 2.731 + } 2.732 + 2.733 + /** 2.734 + * A type annotation found on a method. 2.735 + */ 2.736 + public static class ExpectedMethodTypeAnnotation extends ExpectedTypeAnnotation { 2.737 + private final String methodname; 2.738 + 2.739 + /** 2.740 + * Create an {@code ExpectedMethodTypeAnnotation} from its 2.741 + * components. It is usually a better idea to use a {@code 2.742 + * Builder} to do this. 2.743 + * 2.744 + * @param methodname The expected method name. 2.745 + * @param expectedName The expected annotation name. 2.746 + * @param visibility Whether this annotation should be runtime-visible. 2.747 + * @param expectedCount The number of annotations that should be seen. 2.748 + * @param targetType The expected target type. 2.749 + * @param bound_index The expected bound index, or {@code Integer.MIN_VALUE}. 2.750 + * @param parameter_index The expected parameter index, or 2.751 + * {@code Integer.MIN_VALUE}. 2.752 + * @param type_index The expected type index, or {@code Integer.MIN_VALUE}. 2.753 + * @param exception_index The expected exception index, or 2.754 + * {@code Integer.MIN_VALUE}. 2.755 + * @param typePath The expected type path. 2.756 + */ 2.757 + public ExpectedMethodTypeAnnotation(String methodname, 2.758 + String expectedName, 2.759 + boolean visibility, 2.760 + int expectedCount, 2.761 + TypeAnnotation.TargetType targetType, 2.762 + int bound_index, 2.763 + int parameter_index, 2.764 + int type_index, 2.765 + int exception_index, 2.766 + TypeAnnotation.Position.TypePathEntry... typePath) { 2.767 + super(expectedName, visibility, expectedCount, targetType, bound_index, 2.768 + parameter_index, type_index, exception_index, typePath); 2.769 + this.methodname = methodname; 2.770 + } 2.771 + 2.772 + public String toString() { 2.773 + final StringBuilder sb = new StringBuilder(); 2.774 + sb.append("Expected "); 2.775 + sb.append(expectedCount); 2.776 + sb.append(" annotation "); 2.777 + sb.append(expectedName); 2.778 + sb.append(visibility ? ", runtime visibile " : ", runtime invisibile "); 2.779 + sb.append(targetType); 2.780 + sb.append(", bound_index = "); 2.781 + sb.append(bound_index); 2.782 + sb.append(", parameter_index = "); 2.783 + sb.append(parameter_index); 2.784 + sb.append(", type_index = "); 2.785 + sb.append(type_index); 2.786 + sb.append(", exception_index = "); 2.787 + sb.append(exception_index); 2.788 + sb.append(", type_path = ["); 2.789 + for(int i = 0; i < typePath.length; i++) { 2.790 + if (i != 0) { 2.791 + sb.append(", "); 2.792 + } 2.793 + sb.append(typePath[i]); 2.794 + } 2.795 + sb.append("]"); 2.796 + sb.append(" on method "); 2.797 + sb.append(methodname); 2.798 + return sb.toString(); 2.799 + } 2.800 + 2.801 + /** 2.802 + * See if this template applies to a method. 2.803 + * 2.804 + * @param methodname The method name to check. 2.805 + * @return Whether or not this template should apply. 2.806 + */ 2.807 + public boolean matchMethodName(String methodname) { 2.808 + return this.methodname.equals(methodname); 2.809 + } 2.810 + 2.811 + /** 2.812 + * A builder class for creating {@code 2.813 + * ExpectedMethodTypeAnnotation}s in a more convenient fashion. The 2.814 + * constructor for {@code ExpectedMethodTypeAnnotation} takes a 2.815 + * large number of parameters (by necessity). This class 2.816 + * allows users to construct a {@code ExpectedMethodTypeAnnotation}s 2.817 + * using only the ones they need. 2.818 + */ 2.819 + public static class Builder extends ExpectedTypeAnnotation.Builder { 2.820 + protected final String methodname; 2.821 + 2.822 + /** 2.823 + * Create a {@code Builder} from the mandatory parameters. 2.824 + * 2.825 + * @param methodname The expected method name. 2.826 + * @param expectedName The expected annotation name. 2.827 + * @param targetType The expected target type. 2.828 + * @param visibility Whether this annotation should be runtime-visible. 2.829 + * @param expectedCount The number of annotations that should be seen. 2.830 + */ 2.831 + public Builder(String methodname, 2.832 + String expectedName, 2.833 + TypeAnnotation.TargetType targetType, 2.834 + boolean visibility, 2.835 + int expectedCount) { 2.836 + super(expectedName, targetType, visibility, expectedCount); 2.837 + this.methodname = methodname; 2.838 + } 2.839 + 2.840 + /** 2.841 + * Create an {@code ExpectedMethodTypeAnnotation} from all 2.842 + * parameters that have been provided. The default values 2.843 + * will be used for those that have not. 2.844 + * 2.845 + * @return The cretaed {@code ExpectedMethodTypeAnnotation}. 2.846 + */ 2.847 + public ExpectedMethodTypeAnnotation build() { 2.848 + return new ExpectedMethodTypeAnnotation(methodname, expectedName, 2.849 + visibility, expectedCount, 2.850 + targetType, bound_index, 2.851 + parameter_index, type_index, 2.852 + exception_index, typePath); 2.853 + } 2.854 + } 2.855 + } 2.856 + 2.857 + /** 2.858 + * A type annotation found on a field. 2.859 + */ 2.860 + public static class ExpectedFieldTypeAnnotation extends ExpectedTypeAnnotation { 2.861 + private final String fieldname; 2.862 + 2.863 + /** 2.864 + * Create an {@code ExpectedFieldTypeAnnotation} from its 2.865 + * components. It is usually a better idea to use a {@code 2.866 + * Builder} to do this. 2.867 + * 2.868 + * @param fieldname The expected field name. 2.869 + * @param expectedName The expected annotation name. 2.870 + * @param visibility Whether this annotation should be runtime-visible. 2.871 + * @param expectedCount The number of annotations that should be seen. 2.872 + * @param targetType The expected target type. 2.873 + * @param bound_index The expected bound index, or {@code Integer.MIN_VALUE}. 2.874 + * @param parameter_index The expected parameter index, or 2.875 + * {@code Integer.MIN_VALUE}. 2.876 + * @param type_index The expected type index, or {@code Integer.MIN_VALUE}. 2.877 + * @param exception_index The expected exception index, or 2.878 + * {@code Integer.MIN_VALUE}. 2.879 + * @param typePath The expected type path. 2.880 + */ 2.881 + public ExpectedFieldTypeAnnotation(String fieldname, 2.882 + String expectedName, 2.883 + boolean visibility, 2.884 + int expectedCount, 2.885 + TypeAnnotation.TargetType targetType, 2.886 + int bound_index, 2.887 + int parameter_index, 2.888 + int type_index, 2.889 + int exception_index, 2.890 + TypeAnnotation.Position.TypePathEntry... typePath) { 2.891 + super(expectedName, visibility, expectedCount, targetType, bound_index, 2.892 + parameter_index, type_index, exception_index, typePath); 2.893 + this.fieldname = fieldname; 2.894 + } 2.895 + 2.896 + public String toString() { 2.897 + final StringBuilder sb = new StringBuilder(); 2.898 + sb.append("Expected ").append(expectedCount) 2.899 + .append(" annotation ").append(expectedName) 2.900 + .append(visibility ? ", runtime visibile " : ", runtime invisibile ") 2.901 + .append(targetType) 2.902 + .append(", bound_index = ").append(bound_index) 2.903 + .append(", parameter_index = ").append(parameter_index) 2.904 + .append(", type_index = ").append(type_index) 2.905 + .append(", exception_index = ").append(exception_index) 2.906 + .append(", type_path = ["); 2.907 + 2.908 + for(int i = 0; i < typePath.length; i++) { 2.909 + if (i != 0) { 2.910 + sb.append(", "); 2.911 + } 2.912 + sb.append(typePath[i]); 2.913 + } 2.914 + sb.append("]") 2.915 + .append(" on field ").append(fieldname); 2.916 + return sb.toString(); 2.917 + } 2.918 + 2.919 + /** 2.920 + * See if this template applies to a field. 2.921 + * 2.922 + * @param fieldname The field name to check. 2.923 + * @return Whether or not this template should apply. 2.924 + */ 2.925 + public boolean matchFieldName(String fieldname) { 2.926 + return this.fieldname.equals(fieldname); 2.927 + } 2.928 + 2.929 + /** 2.930 + * A builder class for creating {@code 2.931 + * ExpectedFieldTypeAnnotation}s in a more convenient fashion. The 2.932 + * constructor for {@code ExpectedFieldTypeAnnotation} takes a 2.933 + * large number of parameters (by necessity). This class 2.934 + * allows users to construct a {@code ExpectedFieldTypeAnnotation}s 2.935 + * using only the ones they need. 2.936 + */ 2.937 + public static class Builder extends ExpectedTypeAnnotation.Builder { 2.938 + protected final String fieldname; 2.939 + 2.940 + /** 2.941 + * Create a {@code Builder} from the mandatory parameters. 2.942 + * 2.943 + * @param fieldname The expected field name. 2.944 + * @param expectedName The expected annotation name. 2.945 + * @param targetType The expected target type. 2.946 + * @param visibility Whether this annotation should be runtime-visible. 2.947 + * @param expectedCount The number of annotations that should be seen. 2.948 + */ 2.949 + public Builder(String fieldname, 2.950 + String expectedName, 2.951 + TypeAnnotation.TargetType targetType, 2.952 + boolean visibility, 2.953 + int expectedCount) { 2.954 + super(expectedName, targetType, visibility, expectedCount); 2.955 + this.fieldname = fieldname; 2.956 + } 2.957 + 2.958 + /** 2.959 + * Create an {@code ExpectedFieldTypeAnnotation} from all 2.960 + * parameters that have been provided. The default values 2.961 + * will be used for those that have not. 2.962 + * 2.963 + * @return The cretaed {@code ExpectedFieldTypeAnnotation}. 2.964 + */ 2.965 + public ExpectedFieldTypeAnnotation build() { 2.966 + return new ExpectedFieldTypeAnnotation(fieldname, expectedName, 2.967 + visibility, expectedCount, 2.968 + targetType, bound_index, 2.969 + parameter_index, type_index, 2.970 + exception_index, typePath); 2.971 + } 2.972 + } 2.973 + } 2.974 + 2.975 + private void matchClassAnnotation(ClassFile classfile, 2.976 + ExpectedAnnotation expected) 2.977 + throws ConstantPoolException { 2.978 + for(Attribute attr : classfile.attributes) { 2.979 + attr.accept(annoMatcher(classfile.constant_pool), expected); 2.980 + } 2.981 + } 2.982 + 2.983 + private void matchMethodAnnotation(ClassFile classfile, 2.984 + ExpectedMethodAnnotation expected) 2.985 + throws ConstantPoolException { 2.986 + for(Method meth : classfile.methods) { 2.987 + if (expected.matchMethodName(meth.getName(classfile.constant_pool))) { 2.988 + for(Attribute attr : meth.attributes) { 2.989 + attr.accept(annoMatcher(classfile.constant_pool), expected); 2.990 + } 2.991 + } 2.992 + } 2.993 + } 2.994 + 2.995 + private void matchParameterAnnotation(ClassFile classfile, 2.996 + ExpectedParameterAnnotation expected) 2.997 + throws ConstantPoolException { 2.998 + for(Method meth : classfile.methods) { 2.999 + if (expected.matchMethodName(meth.getName(classfile.constant_pool))) { 2.1000 + for(Attribute attr : meth.attributes) { 2.1001 + attr.accept(paramMatcher(classfile.constant_pool), expected); 2.1002 + } 2.1003 + } 2.1004 + } 2.1005 + } 2.1006 + 2.1007 + private void matchFieldAnnotation(ClassFile classfile, 2.1008 + ExpectedFieldAnnotation expected) 2.1009 + throws ConstantPoolException { 2.1010 + for(Field field : classfile.fields) { 2.1011 + if (expected.matchFieldName(field.getName(classfile.constant_pool))) { 2.1012 + for(Attribute attr : field.attributes) { 2.1013 + attr.accept(annoMatcher(classfile.constant_pool), expected); 2.1014 + } 2.1015 + } 2.1016 + } 2.1017 + } 2.1018 + 2.1019 + private void matchClassTypeAnnotation(ClassFile classfile, 2.1020 + ExpectedTypeAnnotation expected) 2.1021 + throws ConstantPoolException { 2.1022 + for(Attribute attr : classfile.attributes) { 2.1023 + attr.accept(typeAnnoMatcher, expected); 2.1024 + } 2.1025 + } 2.1026 + 2.1027 + private void matchMethodTypeAnnotation(ClassFile classfile, 2.1028 + ExpectedMethodTypeAnnotation expected) 2.1029 + throws ConstantPoolException { 2.1030 + for(Method meth : classfile.methods) { 2.1031 + if (expected.matchMethodName(meth.getName(classfile.constant_pool))) { 2.1032 + for(Attribute attr : meth.attributes) { 2.1033 + attr.accept(typeAnnoMatcher, expected); 2.1034 + } 2.1035 + } 2.1036 + } 2.1037 + } 2.1038 + 2.1039 + private void matchFieldTypeAnnotation(ClassFile classfile, 2.1040 + ExpectedFieldTypeAnnotation expected) 2.1041 + throws ConstantPoolException { 2.1042 + for(Field field : classfile.fields) { 2.1043 + if (expected.matchFieldName(field.getName(classfile.constant_pool))) { 2.1044 + for(Attribute attr : field.attributes) { 2.1045 + attr.accept(typeAnnoMatcher, expected); 2.1046 + } 2.1047 + } 2.1048 + } 2.1049 + } 2.1050 + 2.1051 + private void matchClassAnnotations(ClassFile classfile, 2.1052 + ExpectedAnnotation[] expected) 2.1053 + throws ConstantPoolException { 2.1054 + for(ExpectedAnnotation one : expected) { 2.1055 + matchClassAnnotation(classfile, one); 2.1056 + } 2.1057 + } 2.1058 + 2.1059 + private void matchMethodAnnotations(ClassFile classfile, 2.1060 + ExpectedMethodAnnotation[] expected) 2.1061 + throws ConstantPoolException { 2.1062 + for(ExpectedMethodAnnotation one : expected) { 2.1063 + matchMethodAnnotation(classfile, one); 2.1064 + } 2.1065 + } 2.1066 + 2.1067 + private void matchParameterAnnotations(ClassFile classfile, 2.1068 + ExpectedParameterAnnotation[] expected) 2.1069 + throws ConstantPoolException { 2.1070 + for(ExpectedParameterAnnotation one : expected) { 2.1071 + matchParameterAnnotation(classfile, one); 2.1072 + } 2.1073 + } 2.1074 + 2.1075 + private void matchFieldAnnotations(ClassFile classfile, 2.1076 + ExpectedFieldAnnotation[] expected) 2.1077 + throws ConstantPoolException { 2.1078 + for(ExpectedFieldAnnotation one : expected) { 2.1079 + matchFieldAnnotation(classfile, one); 2.1080 + } 2.1081 + } 2.1082 + 2.1083 + private void matchClassTypeAnnotations(ClassFile classfile, 2.1084 + ExpectedTypeAnnotation[] expected) 2.1085 + throws ConstantPoolException { 2.1086 + for(ExpectedTypeAnnotation one : expected) { 2.1087 + matchClassTypeAnnotation(classfile, one); 2.1088 + } 2.1089 + } 2.1090 + 2.1091 + private void matchMethodTypeAnnotations(ClassFile classfile, 2.1092 + ExpectedMethodTypeAnnotation[] expected) 2.1093 + throws ConstantPoolException { 2.1094 + for(ExpectedMethodTypeAnnotation one : expected) { 2.1095 + matchMethodTypeAnnotation(classfile, one); 2.1096 + } 2.1097 + } 2.1098 + 2.1099 + private void matchFieldTypeAnnotations(ClassFile classfile, 2.1100 + ExpectedFieldTypeAnnotation[] expected) 2.1101 + throws ConstantPoolException { 2.1102 + for(ExpectedFieldTypeAnnotation one : expected) { 2.1103 + matchFieldTypeAnnotation(classfile, one); 2.1104 + } 2.1105 + } 2.1106 + 2.1107 + /** 2.1108 + * Run a template on a single {@code ClassFile}. 2.1109 + * 2.1110 + * @param classfile The {@code ClassFile} on which to run tests. 2.1111 + * @param expected The expected annotation template. 2.1112 + */ 2.1113 + public void run(ClassFile classfile, 2.1114 + Expected... expected) 2.1115 + throws ConstantPoolException { 2.1116 + run(new ClassFile[] { classfile }, expected); 2.1117 + } 2.1118 + 2.1119 + /** 2.1120 + * Run a template on multiple {@code ClassFile}s. 2.1121 + * 2.1122 + * @param classfile The {@code ClassFile}s on which to run tests. 2.1123 + * @param expected The expected annotation template. 2.1124 + */ 2.1125 + public void run(ClassFile[] classfiles, 2.1126 + Expected... expected) 2.1127 + throws ConstantPoolException { 2.1128 + for(ClassFile classfile : classfiles) { 2.1129 + for(Expected one : expected) { 2.1130 + if (one.matchClassName(classfile.getName())) { 2.1131 + if (one.classAnnos != null) 2.1132 + matchClassAnnotations(classfile, one.classAnnos); 2.1133 + if (one.methodAnnos != null) 2.1134 + matchMethodAnnotations(classfile, one.methodAnnos); 2.1135 + if (one.methodParamAnnos != null) 2.1136 + matchParameterAnnotations(classfile, one.methodParamAnnos); 2.1137 + if (one.fieldAnnos != null) 2.1138 + matchFieldAnnotations(classfile, one.fieldAnnos); 2.1139 + if (one.classTypeAnnos != null) 2.1140 + matchClassTypeAnnotations(classfile, one.classTypeAnnos); 2.1141 + if (one.methodTypeAnnos != null) 2.1142 + matchMethodTypeAnnotations(classfile, one.methodTypeAnnos); 2.1143 + if (one.fieldTypeAnnos != null) 2.1144 + matchFieldTypeAnnotations(classfile, one.fieldTypeAnnos); 2.1145 + } 2.1146 + } 2.1147 + } 2.1148 + int count = 0; 2.1149 + for (Expected one : expected) { 2.1150 + count += one.check(); 2.1151 + } 2.1152 + 2.1153 + if (count != 0) { 2.1154 + throw new RuntimeException(count + " errors occurred in test"); 2.1155 + } 2.1156 + } 2.1157 + 2.1158 + /** 2.1159 + * Get a {@code ClassFile} from its file name. 2.1160 + * 2.1161 + * @param name The class' file name. 2.1162 + * @param host A class in the same package. 2.1163 + * @return The {@code ClassFile} 2.1164 + */ 2.1165 + public static ClassFile getClassFile(String name, 2.1166 + Class<?> host) 2.1167 + throws IOException, ConstantPoolException { 2.1168 + final URL url = host.getResource(name); 2.1169 + final InputStream in = url.openStream(); 2.1170 + try { 2.1171 + return ClassFile.read(in); 2.1172 + } finally { 2.1173 + in.close(); 2.1174 + } 2.1175 + } 2.1176 + 2.1177 + private static final Attribute.Visitor<Void, ExpectedTypeAnnotation> typeAnnoMatcher = 2.1178 + new Attribute.Visitor<Void, ExpectedTypeAnnotation>() { 2.1179 + 2.1180 + @Override 2.1181 + public Void visitBootstrapMethods(BootstrapMethods_attribute attr, 2.1182 + ExpectedTypeAnnotation expected) { 2.1183 + return null; 2.1184 + } 2.1185 + 2.1186 + @Override 2.1187 + public Void visitDefault(DefaultAttribute attr, 2.1188 + ExpectedTypeAnnotation expected) { 2.1189 + return null; 2.1190 + } 2.1191 + 2.1192 + @Override 2.1193 + public Void visitAnnotationDefault(AnnotationDefault_attribute attr, 2.1194 + ExpectedTypeAnnotation expected) { 2.1195 + return null; 2.1196 + } 2.1197 + 2.1198 + @Override 2.1199 + public Void visitCharacterRangeTable(CharacterRangeTable_attribute attr, 2.1200 + ExpectedTypeAnnotation expected) { 2.1201 + return null; 2.1202 + } 2.1203 + 2.1204 + @Override 2.1205 + public Void visitCode(Code_attribute attr, 2.1206 + ExpectedTypeAnnotation expected) { 2.1207 + return null; 2.1208 + } 2.1209 + 2.1210 + @Override 2.1211 + public Void visitCompilationID(CompilationID_attribute attr, 2.1212 + ExpectedTypeAnnotation expected) { 2.1213 + return null; 2.1214 + } 2.1215 + 2.1216 + @Override 2.1217 + public Void visitConstantValue(ConstantValue_attribute attr, 2.1218 + ExpectedTypeAnnotation expected) { 2.1219 + return null; 2.1220 + } 2.1221 + 2.1222 + @Override 2.1223 + public Void visitDeprecated(Deprecated_attribute attr, 2.1224 + ExpectedTypeAnnotation expected) { 2.1225 + return null; 2.1226 + } 2.1227 + 2.1228 + @Override 2.1229 + public Void visitEnclosingMethod(EnclosingMethod_attribute attr, 2.1230 + ExpectedTypeAnnotation expected) { 2.1231 + return null; 2.1232 + } 2.1233 + 2.1234 + @Override 2.1235 + public Void visitExceptions(Exceptions_attribute attr, 2.1236 + ExpectedTypeAnnotation expected) { 2.1237 + return null; 2.1238 + } 2.1239 + 2.1240 + @Override 2.1241 + public Void visitInnerClasses(InnerClasses_attribute attr, 2.1242 + ExpectedTypeAnnotation expected) { 2.1243 + return null; 2.1244 + } 2.1245 + 2.1246 + @Override 2.1247 + public Void visitLineNumberTable(LineNumberTable_attribute attr, 2.1248 + ExpectedTypeAnnotation expected) { 2.1249 + return null; 2.1250 + } 2.1251 + 2.1252 + @Override 2.1253 + public Void visitLocalVariableTable(LocalVariableTable_attribute attr, 2.1254 + ExpectedTypeAnnotation expected) { 2.1255 + return null; 2.1256 + } 2.1257 + 2.1258 + @Override 2.1259 + public Void visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, 2.1260 + ExpectedTypeAnnotation expected) { 2.1261 + return null; 2.1262 + } 2.1263 + 2.1264 + @Override 2.1265 + public Void visitMethodParameters(MethodParameters_attribute attr, 2.1266 + ExpectedTypeAnnotation expected) { 2.1267 + return null; 2.1268 + } 2.1269 + 2.1270 + @Override 2.1271 + public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, 2.1272 + ExpectedTypeAnnotation expected) { 2.1273 + return null; 2.1274 + } 2.1275 + 2.1276 + @Override 2.1277 + public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, 2.1278 + ExpectedTypeAnnotation expected) { 2.1279 + return null; 2.1280 + } 2.1281 + 2.1282 + @Override 2.1283 + public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, 2.1284 + ExpectedTypeAnnotation expected) { 2.1285 + return null; 2.1286 + } 2.1287 + 2.1288 + @Override 2.1289 + public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, 2.1290 + ExpectedTypeAnnotation expected) { 2.1291 + return null; 2.1292 + } 2.1293 + 2.1294 + @Override 2.1295 + public Void visitSignature(Signature_attribute attr, 2.1296 + ExpectedTypeAnnotation expected) { 2.1297 + return null; 2.1298 + } 2.1299 + 2.1300 + @Override 2.1301 + public Void visitSourceDebugExtension(SourceDebugExtension_attribute attr, 2.1302 + ExpectedTypeAnnotation expected) { 2.1303 + return null; 2.1304 + } 2.1305 + 2.1306 + @Override 2.1307 + public Void visitSourceFile(SourceFile_attribute attr, 2.1308 + ExpectedTypeAnnotation expected) { 2.1309 + return null; 2.1310 + } 2.1311 + 2.1312 + @Override 2.1313 + public Void visitSourceID(SourceID_attribute attr, 2.1314 + ExpectedTypeAnnotation expected) { 2.1315 + return null; 2.1316 + } 2.1317 + 2.1318 + @Override 2.1319 + public Void visitStackMap(StackMap_attribute attr, 2.1320 + ExpectedTypeAnnotation expected) { 2.1321 + return null; 2.1322 + } 2.1323 + 2.1324 + @Override 2.1325 + public Void visitStackMapTable(StackMapTable_attribute attr, 2.1326 + ExpectedTypeAnnotation expected) { 2.1327 + return null; 2.1328 + } 2.1329 + 2.1330 + @Override 2.1331 + public Void visitSynthetic(Synthetic_attribute attr, 2.1332 + ExpectedTypeAnnotation expected) { 2.1333 + return null; 2.1334 + } 2.1335 + 2.1336 + @Override 2.1337 + public Void visitRuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotations_attribute attr, 2.1338 + ExpectedTypeAnnotation expected) { 2.1339 + if (expected.matchVisibility(true)) { 2.1340 + for(TypeAnnotation anno : attr.annotations) { 2.1341 + expected.matchAnnotation(anno); 2.1342 + } 2.1343 + } 2.1344 + 2.1345 + return null; 2.1346 + } 2.1347 + 2.1348 + @Override 2.1349 + public Void visitRuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotations_attribute attr, 2.1350 + ExpectedTypeAnnotation expected) { 2.1351 + if (expected.matchVisibility(false)) { 2.1352 + for(TypeAnnotation anno : attr.annotations) { 2.1353 + expected.matchAnnotation(anno); 2.1354 + } 2.1355 + } 2.1356 + 2.1357 + return null; 2.1358 + } 2.1359 + }; 2.1360 + 2.1361 + private static Attribute.Visitor<Void, ExpectedAnnotation> annoMatcher(ConstantPool cpool) { 2.1362 + return new Attribute.Visitor<Void, ExpectedAnnotation>() { 2.1363 + 2.1364 + @Override 2.1365 + public Void visitBootstrapMethods(BootstrapMethods_attribute attr, 2.1366 + ExpectedAnnotation expected) { 2.1367 + return null; 2.1368 + } 2.1369 + 2.1370 + @Override 2.1371 + public Void visitDefault(DefaultAttribute attr, 2.1372 + ExpectedAnnotation expected) { 2.1373 + return null; 2.1374 + } 2.1375 + 2.1376 + @Override 2.1377 + public Void visitAnnotationDefault(AnnotationDefault_attribute attr, 2.1378 + ExpectedAnnotation expected) { 2.1379 + return null; 2.1380 + } 2.1381 + 2.1382 + @Override 2.1383 + public Void visitCharacterRangeTable(CharacterRangeTable_attribute attr, 2.1384 + ExpectedAnnotation expected) { 2.1385 + return null; 2.1386 + } 2.1387 + 2.1388 + @Override 2.1389 + public Void visitCode(Code_attribute attr, 2.1390 + ExpectedAnnotation expected) { 2.1391 + return null; 2.1392 + } 2.1393 + 2.1394 + @Override 2.1395 + public Void visitCompilationID(CompilationID_attribute attr, 2.1396 + ExpectedAnnotation expected) { 2.1397 + return null; 2.1398 + } 2.1399 + 2.1400 + @Override 2.1401 + public Void visitConstantValue(ConstantValue_attribute attr, 2.1402 + ExpectedAnnotation expected) { 2.1403 + return null; 2.1404 + } 2.1405 + 2.1406 + @Override 2.1407 + public Void visitDeprecated(Deprecated_attribute attr, 2.1408 + ExpectedAnnotation expected) { 2.1409 + return null; 2.1410 + } 2.1411 + 2.1412 + @Override 2.1413 + public Void visitEnclosingMethod(EnclosingMethod_attribute attr, 2.1414 + ExpectedAnnotation expected) { 2.1415 + return null; 2.1416 + } 2.1417 + 2.1418 + @Override 2.1419 + public Void visitExceptions(Exceptions_attribute attr, 2.1420 + ExpectedAnnotation expected) { 2.1421 + return null; 2.1422 + } 2.1423 + 2.1424 + @Override 2.1425 + public Void visitInnerClasses(InnerClasses_attribute attr, 2.1426 + ExpectedAnnotation expected) { 2.1427 + return null; 2.1428 + } 2.1429 + 2.1430 + @Override 2.1431 + public Void visitLineNumberTable(LineNumberTable_attribute attr, 2.1432 + ExpectedAnnotation expected) { 2.1433 + return null; 2.1434 + } 2.1435 + 2.1436 + @Override 2.1437 + public Void visitLocalVariableTable(LocalVariableTable_attribute attr, 2.1438 + ExpectedAnnotation expected) { 2.1439 + return null; 2.1440 + } 2.1441 + 2.1442 + @Override 2.1443 + public Void visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, 2.1444 + ExpectedAnnotation expected) { 2.1445 + return null; 2.1446 + } 2.1447 + 2.1448 + @Override 2.1449 + public Void visitMethodParameters(MethodParameters_attribute attr, 2.1450 + ExpectedAnnotation expected) { 2.1451 + return null; 2.1452 + } 2.1453 + 2.1454 + @Override 2.1455 + public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, 2.1456 + ExpectedAnnotation expected) { 2.1457 + return null; 2.1458 + } 2.1459 + 2.1460 + @Override 2.1461 + public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, 2.1462 + ExpectedAnnotation expected) { 2.1463 + return null; 2.1464 + } 2.1465 + 2.1466 + @Override 2.1467 + public Void visitRuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotations_attribute attr, 2.1468 + ExpectedAnnotation expected) { 2.1469 + return null; 2.1470 + } 2.1471 + 2.1472 + @Override 2.1473 + public Void visitRuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotations_attribute attr, 2.1474 + ExpectedAnnotation expected) { 2.1475 + return null; 2.1476 + } 2.1477 + 2.1478 + @Override 2.1479 + public Void visitSignature(Signature_attribute attr, 2.1480 + ExpectedAnnotation expected) { 2.1481 + return null; 2.1482 + } 2.1483 + 2.1484 + @Override 2.1485 + public Void visitSourceDebugExtension(SourceDebugExtension_attribute attr, 2.1486 + ExpectedAnnotation expected) { 2.1487 + return null; 2.1488 + } 2.1489 + 2.1490 + @Override 2.1491 + public Void visitSourceFile(SourceFile_attribute attr, 2.1492 + ExpectedAnnotation expected) { 2.1493 + return null; 2.1494 + } 2.1495 + 2.1496 + @Override 2.1497 + public Void visitSourceID(SourceID_attribute attr, 2.1498 + ExpectedAnnotation expected) { 2.1499 + return null; 2.1500 + } 2.1501 + 2.1502 + @Override 2.1503 + public Void visitStackMap(StackMap_attribute attr, 2.1504 + ExpectedAnnotation expected) { 2.1505 + return null; 2.1506 + } 2.1507 + 2.1508 + @Override 2.1509 + public Void visitStackMapTable(StackMapTable_attribute attr, 2.1510 + ExpectedAnnotation expected) { 2.1511 + return null; 2.1512 + } 2.1513 + 2.1514 + @Override 2.1515 + public Void visitSynthetic(Synthetic_attribute attr, 2.1516 + ExpectedAnnotation expected) { 2.1517 + return null; 2.1518 + } 2.1519 + 2.1520 + @Override 2.1521 + public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, 2.1522 + ExpectedAnnotation expected) { 2.1523 + if (expected.matchVisibility(true)) { 2.1524 + for(Annotation anno : attr.annotations) { 2.1525 + expected.matchAnnotation(cpool, anno); 2.1526 + } 2.1527 + } 2.1528 + 2.1529 + return null; 2.1530 + } 2.1531 + 2.1532 + @Override 2.1533 + public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, 2.1534 + ExpectedAnnotation expected) { 2.1535 + if (expected.matchVisibility(false)) { 2.1536 + for(Annotation anno : attr.annotations) { 2.1537 + expected.matchAnnotation(cpool, anno); 2.1538 + } 2.1539 + } 2.1540 + 2.1541 + return null; 2.1542 + } 2.1543 + }; 2.1544 + } 2.1545 + 2.1546 + private static Attribute.Visitor<Void, ExpectedParameterAnnotation> paramMatcher(ConstantPool cpool) { 2.1547 + return new Attribute.Visitor<Void, ExpectedParameterAnnotation>() { 2.1548 + 2.1549 + @Override 2.1550 + public Void visitBootstrapMethods(BootstrapMethods_attribute attr, 2.1551 + ExpectedParameterAnnotation expected) { 2.1552 + return null; 2.1553 + } 2.1554 + 2.1555 + @Override 2.1556 + public Void visitDefault(DefaultAttribute attr, 2.1557 + ExpectedParameterAnnotation expected) { 2.1558 + return null; 2.1559 + } 2.1560 + 2.1561 + @Override 2.1562 + public Void visitAnnotationDefault(AnnotationDefault_attribute attr, 2.1563 + ExpectedParameterAnnotation expected) { 2.1564 + return null; 2.1565 + } 2.1566 + 2.1567 + @Override 2.1568 + public Void visitCharacterRangeTable(CharacterRangeTable_attribute attr, 2.1569 + ExpectedParameterAnnotation expected) { 2.1570 + return null; 2.1571 + } 2.1572 + 2.1573 + @Override 2.1574 + public Void visitCode(Code_attribute attr, 2.1575 + ExpectedParameterAnnotation expected) { 2.1576 + return null; 2.1577 + } 2.1578 + 2.1579 + @Override 2.1580 + public Void visitCompilationID(CompilationID_attribute attr, 2.1581 + ExpectedParameterAnnotation expected) { 2.1582 + return null; 2.1583 + } 2.1584 + 2.1585 + @Override 2.1586 + public Void visitConstantValue(ConstantValue_attribute attr, 2.1587 + ExpectedParameterAnnotation expected) { 2.1588 + return null; 2.1589 + } 2.1590 + 2.1591 + @Override 2.1592 + public Void visitDeprecated(Deprecated_attribute attr, 2.1593 + ExpectedParameterAnnotation expected) { 2.1594 + return null; 2.1595 + } 2.1596 + 2.1597 + @Override 2.1598 + public Void visitEnclosingMethod(EnclosingMethod_attribute attr, 2.1599 + ExpectedParameterAnnotation expected) { 2.1600 + return null; 2.1601 + } 2.1602 + 2.1603 + @Override 2.1604 + public Void visitExceptions(Exceptions_attribute attr, 2.1605 + ExpectedParameterAnnotation expected) { 2.1606 + return null; 2.1607 + } 2.1608 + 2.1609 + @Override 2.1610 + public Void visitInnerClasses(InnerClasses_attribute attr, 2.1611 + ExpectedParameterAnnotation expected) { 2.1612 + return null; 2.1613 + } 2.1614 + 2.1615 + @Override 2.1616 + public Void visitLineNumberTable(LineNumberTable_attribute attr, 2.1617 + ExpectedParameterAnnotation expected) { 2.1618 + return null; 2.1619 + } 2.1620 + 2.1621 + @Override 2.1622 + public Void visitLocalVariableTable(LocalVariableTable_attribute attr, 2.1623 + ExpectedParameterAnnotation expected) { 2.1624 + return null; 2.1625 + } 2.1626 + 2.1627 + @Override 2.1628 + public Void visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, 2.1629 + ExpectedParameterAnnotation expected) { 2.1630 + return null; 2.1631 + } 2.1632 + 2.1633 + @Override 2.1634 + public Void visitMethodParameters(MethodParameters_attribute attr, 2.1635 + ExpectedParameterAnnotation expected) { 2.1636 + return null; 2.1637 + } 2.1638 + 2.1639 + @Override 2.1640 + public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, 2.1641 + ExpectedParameterAnnotation expected) { 2.1642 + return null; 2.1643 + } 2.1644 + 2.1645 + @Override 2.1646 + public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, 2.1647 + ExpectedParameterAnnotation expected) { 2.1648 + return null; 2.1649 + } 2.1650 + 2.1651 + @Override 2.1652 + public Void visitRuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotations_attribute attr, 2.1653 + ExpectedParameterAnnotation expected) { 2.1654 + return null; 2.1655 + } 2.1656 + 2.1657 + @Override 2.1658 + public Void visitRuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotations_attribute attr, 2.1659 + ExpectedParameterAnnotation expected) { 2.1660 + return null; 2.1661 + } 2.1662 + 2.1663 + @Override 2.1664 + public Void visitSignature(Signature_attribute attr, 2.1665 + ExpectedParameterAnnotation expected) { 2.1666 + return null; 2.1667 + } 2.1668 + 2.1669 + @Override 2.1670 + public Void visitSourceDebugExtension(SourceDebugExtension_attribute attr, 2.1671 + ExpectedParameterAnnotation expected) { 2.1672 + return null; 2.1673 + } 2.1674 + 2.1675 + @Override 2.1676 + public Void visitSourceFile(SourceFile_attribute attr, 2.1677 + ExpectedParameterAnnotation expected) { 2.1678 + return null; 2.1679 + } 2.1680 + 2.1681 + @Override 2.1682 + public Void visitSourceID(SourceID_attribute attr, 2.1683 + ExpectedParameterAnnotation expected) { 2.1684 + return null; 2.1685 + } 2.1686 + 2.1687 + @Override 2.1688 + public Void visitStackMap(StackMap_attribute attr, 2.1689 + ExpectedParameterAnnotation expected) { 2.1690 + return null; 2.1691 + } 2.1692 + 2.1693 + @Override 2.1694 + public Void visitStackMapTable(StackMapTable_attribute attr, 2.1695 + ExpectedParameterAnnotation expected) { 2.1696 + return null; 2.1697 + } 2.1698 + 2.1699 + @Override 2.1700 + public Void visitSynthetic(Synthetic_attribute attr, 2.1701 + ExpectedParameterAnnotation expected) { 2.1702 + return null; 2.1703 + } 2.1704 + 2.1705 + @Override 2.1706 + public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, 2.1707 + ExpectedParameterAnnotation expected) { 2.1708 + if (expected.matchVisibility(true)) { 2.1709 + if (expected.index < attr.parameter_annotations.length) { 2.1710 + for(Annotation anno : 2.1711 + attr.parameter_annotations[expected.index]) { 2.1712 + expected.matchAnnotation(cpool, anno); 2.1713 + } 2.1714 + } 2.1715 + } 2.1716 + 2.1717 + return null; 2.1718 + } 2.1719 + 2.1720 + @Override 2.1721 + public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, 2.1722 + ExpectedParameterAnnotation expected) { 2.1723 + if (expected.matchVisibility(false)) { 2.1724 + if (expected.index < attr.parameter_annotations.length) { 2.1725 + for(Annotation anno : 2.1726 + attr.parameter_annotations[expected.index]) { 2.1727 + expected.matchAnnotation(cpool, anno); 2.1728 + } 2.1729 + } 2.1730 + } 2.1731 + 2.1732 + return null; 2.1733 + } 2.1734 + }; 2.1735 + } 2.1736 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/annotations/SyntheticParameters.java Tue Nov 25 08:30:52 2014 -0500 3.3 @@ -0,0 +1,198 @@ 3.4 +/* 3.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +/* 3.28 + * @test SyntheticParameters 3.29 + * @bug 8065132 3.30 + * @summary Test generation of annotations on inner class parameters. 3.31 + * @library /lib/annotations/ 3.32 + * @run main SyntheticParameters 3.33 + */ 3.34 + 3.35 +import annotations.classfile.ClassfileInspector; 3.36 + 3.37 +import java.io.*; 3.38 +import java.lang.annotation.*; 3.39 + 3.40 +import com.sun.tools.classfile.*; 3.41 + 3.42 +public class SyntheticParameters extends ClassfileInspector { 3.43 + 3.44 + private static final String Inner_class = "SyntheticParameters$Inner.class"; 3.45 + private static final String Foo_class = "SyntheticParameters$Foo.class"; 3.46 + private static final Expected Inner_expected = 3.47 + new Expected("SyntheticParameters$Inner", 3.48 + null, 3.49 + null, 3.50 + new ExpectedParameterAnnotation[] { 3.51 + (ExpectedParameterAnnotation) 3.52 + // Assert there is no annotation on the 3.53 + // this$0 parameter. 3.54 + new ExpectedParameterAnnotation( 3.55 + "<init>", 3.56 + 0, 3.57 + "A", 3.58 + true, 3.59 + 0), 3.60 + (ExpectedParameterAnnotation) 3.61 + // Assert there is an annotation on the 3.62 + // first parameter. 3.63 + new ExpectedParameterAnnotation( 3.64 + "<init>", 3.65 + 1, 3.66 + "A", 3.67 + true, 3.68 + 1), 3.69 + (ExpectedParameterAnnotation) 3.70 + new ExpectedParameterAnnotation( 3.71 + "foo", 3.72 + 0, 3.73 + "A", 3.74 + true, 3.75 + 1), 3.76 + (ExpectedParameterAnnotation) 3.77 + new ExpectedParameterAnnotation( 3.78 + "foo", 3.79 + 1, 3.80 + "A", 3.81 + true, 3.82 + 0), 3.83 + (ExpectedParameterAnnotation) 3.84 + // Assert there is no annotation on the 3.85 + // this$0 parameter. 3.86 + new ExpectedParameterAnnotation( 3.87 + "<init>", 3.88 + 0, 3.89 + "B", 3.90 + false, 3.91 + 0), 3.92 + (ExpectedParameterAnnotation) 3.93 + // Assert there is an annotation on the 3.94 + // first parameter. 3.95 + new ExpectedParameterAnnotation( 3.96 + "<init>", 3.97 + 1, 3.98 + "B", 3.99 + false, 3.100 + 1), 3.101 + (ExpectedParameterAnnotation) 3.102 + new ExpectedParameterAnnotation( 3.103 + "foo", 3.104 + 0, 3.105 + "B", 3.106 + false, 3.107 + 1), 3.108 + (ExpectedParameterAnnotation) 3.109 + new ExpectedParameterAnnotation( 3.110 + "foo", 3.111 + 1, 3.112 + "B", 3.113 + false, 3.114 + 0) 3.115 + }, 3.116 + null); 3.117 + private static final Expected Foo_expected = 3.118 + new Expected("SyntheticParameters$Foo", 3.119 + null, 3.120 + null, 3.121 + new ExpectedParameterAnnotation[] { 3.122 + (ExpectedParameterAnnotation) 3.123 + // Assert there is no annotation on the 3.124 + // $enum$name parameter. 3.125 + new ExpectedParameterAnnotation( 3.126 + "<init>", 3.127 + 0, 3.128 + "A", 3.129 + true, 3.130 + 0), 3.131 + (ExpectedParameterAnnotation) 3.132 + // Assert there is no annotation on the 3.133 + // $enum$ordinal parameter. 3.134 + new ExpectedParameterAnnotation( 3.135 + "<init>", 3.136 + 1, 3.137 + "A", 3.138 + true, 3.139 + 0), 3.140 + (ExpectedParameterAnnotation) 3.141 + // Assert there is an annotation on the 3.142 + // first parameter. 3.143 + new ExpectedParameterAnnotation( 3.144 + "<init>", 3.145 + 2, 3.146 + "A", 3.147 + true, 3.148 + 1), 3.149 + (ExpectedParameterAnnotation) 3.150 + // Assert there is no annotation on the 3.151 + // $enum$name parameter. 3.152 + new ExpectedParameterAnnotation( 3.153 + "<init>", 3.154 + 0, 3.155 + "B", 3.156 + false, 3.157 + 0), 3.158 + (ExpectedParameterAnnotation) 3.159 + // Assert there is no annotation on the 3.160 + // $enum$ordinal parameter. 3.161 + new ExpectedParameterAnnotation( 3.162 + "<init>", 3.163 + 1, 3.164 + "B", 3.165 + false, 3.166 + 0), 3.167 + (ExpectedParameterAnnotation) 3.168 + // Assert there is an annotation on the 3.169 + // first parameter. 3.170 + new ExpectedParameterAnnotation( 3.171 + "<init>", 3.172 + 2, 3.173 + "B", 3.174 + false, 3.175 + 1) 3.176 + }, 3.177 + null); 3.178 + 3.179 + public static void main(String... args) throws Exception { 3.180 + new SyntheticParameters().run( 3.181 + new ClassFile[] { getClassFile(Inner_class, Inner.class), 3.182 + getClassFile(Foo_class, Foo.class) }, 3.183 + new Expected[] { Inner_expected, Foo_expected }); 3.184 + } 3.185 + 3.186 + public class Inner { 3.187 + public Inner(@A @B int a) {} 3.188 + public void foo(@A @B int a, int b) {} 3.189 + } 3.190 + 3.191 + public static enum Foo { 3.192 + ONE(null); 3.193 + Foo(@A @B Object a) {} 3.194 + } 3.195 +} 3.196 + 3.197 +@Retention(RetentionPolicy.RUNTIME) 3.198 +@interface A {} 3.199 + 3.200 +@Retention(RetentionPolicy.CLASS) 3.201 +@interface B {}
4.1 --- a/test/tools/javac/annotations/typeAnnotations/classfile/ClassfileInspector.java Mon Nov 24 12:49:30 2014 -0500 4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 4.3 @@ -1,950 +0,0 @@ 4.4 -/* 4.5 - * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 4.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 - * 4.8 - * This code is free software; you can redistribute it and/or modify it 4.9 - * under the terms of the GNU General Public License version 2 only, as 4.10 - * published by the Free Software Foundation. 4.11 - * 4.12 - * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 - * version 2 for more details (a copy is included in the LICENSE file that 4.16 - * accompanied this code). 4.17 - * 4.18 - * You should have received a copy of the GNU General Public License version 4.19 - * 2 along with this work; if not, write to the Free Software Foundation, 4.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 - * 4.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 - * or visit www.oracle.com if you need additional information or have any 4.24 - * questions. 4.25 - */ 4.26 - 4.27 -import java.lang.annotation.*; 4.28 -import java.io.*; 4.29 -import java.net.URL; 4.30 -import java.util.List; 4.31 - 4.32 -import com.sun.tools.classfile.*; 4.33 - 4.34 -/** 4.35 - * A class providing utilities for writing tests that inspect class 4.36 - * files directly, looking for specific type annotations. 4.37 - * 4.38 - * Note: this framework does not currently handle repeating 4.39 - * annotations. 4.40 - */ 4.41 -public class ClassfileInspector { 4.42 - 4.43 - /** 4.44 - * A group of expected annotations to be found in a given class. 4.45 - * If the class name is null, then the template will be applied to 4.46 - * every class. 4.47 - */ 4.48 - public static class Expected { 4.49 - /** 4.50 - * The name of the class. If {@code null} this template will 4.51 - * apply to every class; otherwise, it will only be applied to 4.52 - * the named class. 4.53 - */ 4.54 - public final String classname; 4.55 - 4.56 - /** 4.57 - * The expected class annotations. These will be checked 4.58 - * against the class' attributes. 4.59 - */ 4.60 - public final ExpectedTypeAnnotation[] classAnnos; 4.61 - 4.62 - /** 4.63 - * The expected method annotations. These will be checked 4.64 - * against all methods in the class. 4.65 - */ 4.66 - public final ExpectedMethodTypeAnnotation[] methodAnnos; 4.67 - 4.68 - /** 4.69 - * The expected field annotations. These will be checked 4.70 - * against all fields in the class. 4.71 - */ 4.72 - public final ExpectedFieldTypeAnnotation[] fieldAnnos; 4.73 - 4.74 - /** 4.75 - * Create an {@code Expected} from its components. 4.76 - * 4.77 - * @param classname The name of the class to match, or {@code 4.78 - * null} for all classes. 4.79 - * @param classAnnos The expected class annotations. 4.80 - * @param methodAnnos The expected method annotations. 4.81 - * @param fieldAnnos The expected field annotations. 4.82 - */ 4.83 - public Expected(String classname, 4.84 - ExpectedTypeAnnotation[] classAnnos, 4.85 - ExpectedMethodTypeAnnotation[] methodAnnos, 4.86 - ExpectedFieldTypeAnnotation[] fieldAnnos) { 4.87 - this.classname = classname; 4.88 - this.classAnnos = classAnnos; 4.89 - this.methodAnnos = methodAnnos; 4.90 - this.fieldAnnos = fieldAnnos; 4.91 - } 4.92 - 4.93 - public String toString() { 4.94 - final StringBuilder sb = new StringBuilder(); 4.95 - final String newline = System.lineSeparator(); 4.96 - sb.append("Expected on class ").append(classname); 4.97 - if (null != classAnnos) { 4.98 - sb.append(newline).append("Class annotations:").append(newline); 4.99 - for(ExpectedTypeAnnotation anno : classAnnos) { 4.100 - sb.append(anno).append(newline); 4.101 - } 4.102 - } 4.103 - if (null != methodAnnos) { 4.104 - sb.append(newline).append("Method annotations:").append(newline); 4.105 - for(ExpectedTypeAnnotation anno : methodAnnos) { 4.106 - sb.append(anno).append(newline); 4.107 - } 4.108 - } 4.109 - if (null != fieldAnnos) { 4.110 - sb.append(newline).append("Field annotations:").append(newline); 4.111 - for(ExpectedTypeAnnotation anno : fieldAnnos) { 4.112 - sb.append(anno).append(newline); 4.113 - } 4.114 - } 4.115 - return sb.toString(); 4.116 - } 4.117 - 4.118 - /** 4.119 - * See if this template applies to a class. 4.120 - * 4.121 - * @param classname The classname to check. 4.122 - * @return Whether or not this template should apply. 4.123 - */ 4.124 - public boolean matchClassName(String classname) { 4.125 - return this.classname == null || this.classname.equals(classname); 4.126 - } 4.127 - 4.128 - /** 4.129 - * After applying the template to all classes, check to see if 4.130 - * any of the expected annotations weren't matched. 4.131 - * 4.132 - * @return The number of missed matches. 4.133 - */ 4.134 - public int check() { 4.135 - int count = 0; 4.136 - if (classAnnos != null) { 4.137 - for(ExpectedTypeAnnotation expected : classAnnos) { 4.138 - if (!expected.check()) { 4.139 - count++; 4.140 - } 4.141 - } 4.142 - } 4.143 - if (methodAnnos != null) { 4.144 - for(ExpectedMethodTypeAnnotation expected : methodAnnos) { 4.145 - if (!expected.check()) { 4.146 - count++; 4.147 - } 4.148 - } 4.149 - } 4.150 - if (fieldAnnos != null) { 4.151 - for(ExpectedFieldTypeAnnotation expected : fieldAnnos) { 4.152 - if (!expected.check()) { 4.153 - count++; 4.154 - } 4.155 - } 4.156 - } 4.157 - return count; 4.158 - } 4.159 - } 4.160 - 4.161 - /** 4.162 - * An expected type annotation. This is both a superclass for 4.163 - * method and field type annotations, as well as a class for type 4.164 - * annotations on a class. 4.165 - */ 4.166 - public static class ExpectedTypeAnnotation { 4.167 - private int count = 0; 4.168 - protected final String expectedName; 4.169 - protected final int expectedCount; 4.170 - protected final TypeAnnotation.TargetType targetType; 4.171 - protected final int bound_index; 4.172 - protected final int parameter_index; 4.173 - protected final int type_index; 4.174 - protected final int exception_index; 4.175 - protected final TypeAnnotation.Position.TypePathEntry[] typePath; 4.176 - protected final boolean visibility; 4.177 - 4.178 - /** 4.179 - * Create an {@code ExpectedTypeAnnotation} from its 4.180 - * components. It is usually a better idea to use a {@code 4.181 - * Builder} to do this. 4.182 - * 4.183 - * @param expectedName The expected annotation name. 4.184 - * @param visibility Whether this annotation should be runtime-visible. 4.185 - * @param expectedCount The number of annotations that should 4.186 - * be seen. If 0, this asserts that the 4.187 - * described annotation is not present. 4.188 - * @param targetType The expected target type. 4.189 - * @param bound_index The expected bound index, or {@code Integer.MIN_VALUE}. 4.190 - * @param parameter_index The expected parameter index, or 4.191 - * {@code Integer.MIN_VALUE}. 4.192 - * @param type_index The expected type index, or {@code Integer.MIN_VALUE}. 4.193 - * @param exception_index The expected exception index, or 4.194 - * {@code Integer.MIN_VALUE}. 4.195 - * @param typePath The expected type path. 4.196 - */ 4.197 - public ExpectedTypeAnnotation(String expectedName, 4.198 - boolean visibility, 4.199 - int expectedCount, 4.200 - TypeAnnotation.TargetType targetType, 4.201 - int bound_index, 4.202 - int parameter_index, 4.203 - int type_index, 4.204 - int exception_index, 4.205 - TypeAnnotation.Position.TypePathEntry... typePath) { 4.206 - this.expectedName = expectedName; 4.207 - this.visibility = visibility; 4.208 - this.expectedCount = expectedCount; 4.209 - this.targetType = targetType; 4.210 - this.bound_index = bound_index; 4.211 - this.parameter_index = parameter_index; 4.212 - this.type_index = type_index; 4.213 - this.exception_index = exception_index; 4.214 - this.typePath = typePath; 4.215 - } 4.216 - 4.217 - public String toString() { 4.218 - final StringBuilder sb = new StringBuilder(); 4.219 - sb.append("Expected "); 4.220 - sb.append(expectedCount); 4.221 - sb.append(" annotation "); 4.222 - sb.append(expectedName); 4.223 - sb.append(visibility ? ", runtime visibile " : ", runtime invisibile "); 4.224 - sb.append(targetType); 4.225 - sb.append(", bound_index = "); 4.226 - sb.append(bound_index); 4.227 - sb.append(", parameter_index = "); 4.228 - sb.append(parameter_index); 4.229 - sb.append(", type_index = "); 4.230 - sb.append(type_index); 4.231 - sb.append(", exception_index = "); 4.232 - sb.append(exception_index); 4.233 - sb.append(", type_path = ["); 4.234 - for(int i = 0; i < typePath.length; i++) { 4.235 - if (i != 0) { 4.236 - sb.append(", "); 4.237 - } 4.238 - sb.append(typePath[i]); 4.239 - } 4.240 - sb.append("]"); 4.241 - return sb.toString(); 4.242 - } 4.243 - 4.244 - /** 4.245 - * See if this template matches the given visibility. 4.246 - * 4.247 - * @param Whether or not the annotation is visible at runtime. 4.248 - * @return Whether or not this template matches the visibility. 4.249 - */ 4.250 - public boolean matchVisibility(boolean visibility) { 4.251 - return this.visibility == visibility; 4.252 - } 4.253 - 4.254 - /** 4.255 - * Attempty to match this template against an annotation. If 4.256 - * it does match, then the match count for the template will 4.257 - * be incremented. Otherwise, nothing will be done. 4.258 - * 4.259 - * @param anno The annotation to attempt to match. 4.260 - */ 4.261 - public void matchAnnotation(TypeAnnotation anno) { 4.262 - boolean matches = true; 4.263 - 4.264 - try { 4.265 - matches = anno.constant_pool.getUTF8Info(anno.annotation.type_index).value.equals("L" + expectedName + ";"); 4.266 - } catch(Exception e) { 4.267 - matches = false; 4.268 - } 4.269 - 4.270 - matches = matches && anno.position.type == targetType; 4.271 - matches = matches && anno.position.bound_index == bound_index; 4.272 - matches = matches && anno.position.parameter_index == parameter_index; 4.273 - matches = matches && anno.position.type_index == type_index; 4.274 - matches = matches && anno.position.exception_index == exception_index; 4.275 - matches = matches && anno.position.location.size() == typePath.length; 4.276 - 4.277 - if (matches) { 4.278 - int i = 0; 4.279 - for(TypeAnnotation.Position.TypePathEntry entry : 4.280 - anno.position.location) { 4.281 - matches = matches && typePath[i++].equals(entry); 4.282 - } 4.283 - } 4.284 - 4.285 - if (matches) { 4.286 - count++; 4.287 - } 4.288 - } 4.289 - 4.290 - /** 4.291 - * After all matching, check to see if the expected number of 4.292 - * matches equals the actual number. If not, then print a 4.293 - * failure message and return {@code false}. 4.294 - * 4.295 - * @return Whether or not the expected number of matched 4.296 - * equals the actual number. 4.297 - */ 4.298 - public boolean check() { 4.299 - if (count != expectedCount) { 4.300 - System.err.println(this + ", but saw " + count); 4.301 - return false; 4.302 - } else { 4.303 - return true; 4.304 - } 4.305 - } 4.306 - 4.307 - /** 4.308 - * A builder class for creating {@code 4.309 - * ExpectedTypeAnnotation}s in a more convenient fashion. The 4.310 - * constructor for {@code ExpectedTypeAnnotation} takes a 4.311 - * large number of parameters (by necessity). This class 4.312 - * allows users to construct a {@code ExpectedTypeAnnotation}s 4.313 - * using only the ones they need. 4.314 - */ 4.315 - public static class Builder { 4.316 - protected final String expectedName; 4.317 - protected final int expectedCount; 4.318 - protected final TypeAnnotation.TargetType targetType; 4.319 - protected final boolean visibility; 4.320 - protected int bound_index = Integer.MIN_VALUE; 4.321 - protected int parameter_index = Integer.MIN_VALUE; 4.322 - protected int type_index = Integer.MIN_VALUE; 4.323 - protected int exception_index = Integer.MIN_VALUE; 4.324 - protected TypeAnnotation.Position.TypePathEntry[] typePath = 4.325 - new TypeAnnotation.Position.TypePathEntry[0]; 4.326 - 4.327 - /** 4.328 - * Create a {@code Builder} from the mandatory parameters. 4.329 - * 4.330 - * @param expectedName The expected annotation name. 4.331 - * @param targetType The expected target type. 4.332 - * @param visibility Whether this annotation should be runtime-visible. 4.333 - * @param expectedCount The number of annotations that should be seen. 4.334 - */ 4.335 - public Builder(String expectedName, 4.336 - TypeAnnotation.TargetType targetType, 4.337 - boolean visibility, 4.338 - int expectedCount) { 4.339 - this.expectedName = expectedName; 4.340 - this.visibility = visibility; 4.341 - this.expectedCount = expectedCount; 4.342 - this.targetType = targetType; 4.343 - } 4.344 - 4.345 - /** 4.346 - * Create an {@code ExpectedTypeAnnotation} from all 4.347 - * parameters that have been provided. The default values 4.348 - * will be used for those that have not. 4.349 - * 4.350 - * @return The cretaed {@code ExpectedTypeAnnotation}. 4.351 - */ 4.352 - public ExpectedTypeAnnotation build() { 4.353 - return new ExpectedTypeAnnotation(expectedName, visibility, 4.354 - expectedCount, targetType, 4.355 - bound_index, parameter_index, 4.356 - type_index, exception_index, 4.357 - typePath); 4.358 - } 4.359 - 4.360 - /** 4.361 - * Provide a bound index parameter. 4.362 - * 4.363 - * @param bound_index The bound_index value. 4.364 - */ 4.365 - public Builder setBoundIndex(int bound_index) { 4.366 - this.bound_index = bound_index; 4.367 - return this; 4.368 - } 4.369 - 4.370 - /** 4.371 - * Provide a parameter index parameter. 4.372 - * 4.373 - * @param bound_index The parameter_index value. 4.374 - */ 4.375 - public Builder setParameterIndex(int parameter_index) { 4.376 - this.parameter_index = parameter_index; 4.377 - return this; 4.378 - } 4.379 - 4.380 - /** 4.381 - * Provide a type index parameter. 4.382 - * 4.383 - * @param type_index The type_index value. 4.384 - */ 4.385 - public Builder setTypeIndex(int type_index) { 4.386 - this.type_index = type_index; 4.387 - return this; 4.388 - } 4.389 - 4.390 - /** 4.391 - * Provide an exception index parameter. 4.392 - * 4.393 - * @param exception_index The exception_index value. 4.394 - */ 4.395 - public Builder setExceptionIndex(int exception_index) { 4.396 - this.exception_index = exception_index; 4.397 - return this; 4.398 - } 4.399 - 4.400 - /** 4.401 - * Provide a type path parameter. 4.402 - * 4.403 - * @param typePath The type path value. 4.404 - */ 4.405 - public Builder setTypePath(TypeAnnotation.Position.TypePathEntry[] typePath) { 4.406 - this.typePath = typePath; 4.407 - return this; 4.408 - } 4.409 - } 4.410 - } 4.411 - 4.412 - /** 4.413 - * A type annotation found on a method. 4.414 - */ 4.415 - public static class ExpectedMethodTypeAnnotation extends ExpectedTypeAnnotation { 4.416 - private final String methodname; 4.417 - 4.418 - /** 4.419 - * Create an {@code ExpectedMethodTypeAnnotation} from its 4.420 - * components. It is usually a better idea to use a {@code 4.421 - * Builder} to do this. 4.422 - * 4.423 - * @param methodname The expected method name. 4.424 - * @param expectedName The expected annotation name. 4.425 - * @param visibility Whether this annotation should be runtime-visible. 4.426 - * @param expectedCount The number of annotations that should be seen. 4.427 - * @param targetType The expected target type. 4.428 - * @param bound_index The expected bound index, or {@code Integer.MIN_VALUE}. 4.429 - * @param parameter_index The expected parameter index, or 4.430 - * {@code Integer.MIN_VALUE}. 4.431 - * @param type_index The expected type index, or {@code Integer.MIN_VALUE}. 4.432 - * @param exception_index The expected exception index, or 4.433 - * {@code Integer.MIN_VALUE}. 4.434 - * @param typePath The expected type path. 4.435 - */ 4.436 - public ExpectedMethodTypeAnnotation(String methodname, 4.437 - String expectedName, 4.438 - boolean visibility, 4.439 - int expectedCount, 4.440 - TypeAnnotation.TargetType targetType, 4.441 - int bound_index, 4.442 - int parameter_index, 4.443 - int type_index, 4.444 - int exception_index, 4.445 - TypeAnnotation.Position.TypePathEntry... typePath) { 4.446 - super(expectedName, visibility, expectedCount, targetType, bound_index, 4.447 - parameter_index, type_index, exception_index, typePath); 4.448 - this.methodname = methodname; 4.449 - } 4.450 - 4.451 - public String toString() { 4.452 - final StringBuilder sb = new StringBuilder(); 4.453 - sb.append("Expected "); 4.454 - sb.append(expectedCount); 4.455 - sb.append(" annotation "); 4.456 - sb.append(expectedName); 4.457 - sb.append(visibility ? ", runtime visibile " : ", runtime invisibile "); 4.458 - sb.append(targetType); 4.459 - sb.append(", bound_index = "); 4.460 - sb.append(bound_index); 4.461 - sb.append(", parameter_index = "); 4.462 - sb.append(parameter_index); 4.463 - sb.append(", type_index = "); 4.464 - sb.append(type_index); 4.465 - sb.append(", exception_index = "); 4.466 - sb.append(exception_index); 4.467 - sb.append(", type_path = ["); 4.468 - for(int i = 0; i < typePath.length; i++) { 4.469 - if (i != 0) { 4.470 - sb.append(", "); 4.471 - } 4.472 - sb.append(typePath[i]); 4.473 - } 4.474 - sb.append("]"); 4.475 - sb.append(" on method "); 4.476 - sb.append(methodname); 4.477 - return sb.toString(); 4.478 - } 4.479 - 4.480 - /** 4.481 - * See if this template applies to a method. 4.482 - * 4.483 - * @param methodname The method name to check. 4.484 - * @return Whether or not this template should apply. 4.485 - */ 4.486 - public boolean matchMethodName(String methodname) { 4.487 - return this.methodname.equals(methodname); 4.488 - } 4.489 - 4.490 - /** 4.491 - * A builder class for creating {@code 4.492 - * ExpectedMethodTypeAnnotation}s in a more convenient fashion. The 4.493 - * constructor for {@code ExpectedMethodTypeAnnotation} takes a 4.494 - * large number of parameters (by necessity). This class 4.495 - * allows users to construct a {@code ExpectedMethodTypeAnnotation}s 4.496 - * using only the ones they need. 4.497 - */ 4.498 - public static class Builder extends ExpectedTypeAnnotation.Builder { 4.499 - protected final String methodname; 4.500 - 4.501 - /** 4.502 - * Create a {@code Builder} from the mandatory parameters. 4.503 - * 4.504 - * @param methodname The expected method name. 4.505 - * @param expectedName The expected annotation name. 4.506 - * @param targetType The expected target type. 4.507 - * @param visibility Whether this annotation should be runtime-visible. 4.508 - * @param expectedCount The number of annotations that should be seen. 4.509 - */ 4.510 - public Builder(String methodname, 4.511 - String expectedName, 4.512 - TypeAnnotation.TargetType targetType, 4.513 - boolean visibility, 4.514 - int expectedCount) { 4.515 - super(expectedName, targetType, visibility, expectedCount); 4.516 - this.methodname = methodname; 4.517 - } 4.518 - 4.519 - /** 4.520 - * Create an {@code ExpectedMethodTypeAnnotation} from all 4.521 - * parameters that have been provided. The default values 4.522 - * will be used for those that have not. 4.523 - * 4.524 - * @return The cretaed {@code ExpectedMethodTypeAnnotation}. 4.525 - */ 4.526 - public ExpectedMethodTypeAnnotation build() { 4.527 - return new ExpectedMethodTypeAnnotation(methodname, expectedName, 4.528 - visibility, expectedCount, 4.529 - targetType, bound_index, 4.530 - parameter_index, type_index, 4.531 - exception_index, typePath); 4.532 - } 4.533 - } 4.534 - } 4.535 - 4.536 - /** 4.537 - * A type annotation found on a field. 4.538 - */ 4.539 - public static class ExpectedFieldTypeAnnotation extends ExpectedTypeAnnotation { 4.540 - private final String fieldname; 4.541 - 4.542 - /** 4.543 - * Create an {@code ExpectedFieldTypeAnnotation} from its 4.544 - * components. It is usually a better idea to use a {@code 4.545 - * Builder} to do this. 4.546 - * 4.547 - * @param fieldname The expected field name. 4.548 - * @param expectedName The expected annotation name. 4.549 - * @param visibility Whether this annotation should be runtime-visible. 4.550 - * @param expectedCount The number of annotations that should be seen. 4.551 - * @param targetType The expected target type. 4.552 - * @param bound_index The expected bound index, or {@code Integer.MIN_VALUE}. 4.553 - * @param parameter_index The expected parameter index, or 4.554 - * {@code Integer.MIN_VALUE}. 4.555 - * @param type_index The expected type index, or {@code Integer.MIN_VALUE}. 4.556 - * @param exception_index The expected exception index, or 4.557 - * {@code Integer.MIN_VALUE}. 4.558 - * @param typePath The expected type path. 4.559 - */ 4.560 - public ExpectedFieldTypeAnnotation(String fieldname, 4.561 - String expectedName, 4.562 - boolean visibility, 4.563 - int expectedCount, 4.564 - TypeAnnotation.TargetType targetType, 4.565 - int bound_index, 4.566 - int parameter_index, 4.567 - int type_index, 4.568 - int exception_index, 4.569 - TypeAnnotation.Position.TypePathEntry... typePath) { 4.570 - super(expectedName, visibility, expectedCount, targetType, bound_index, 4.571 - parameter_index, type_index, exception_index, typePath); 4.572 - this.fieldname = fieldname; 4.573 - } 4.574 - 4.575 - public String toString() { 4.576 - final StringBuilder sb = new StringBuilder(); 4.577 - sb.append("Expected ").append(expectedCount) 4.578 - .append(" annotation ").append(expectedName) 4.579 - .append(visibility ? ", runtime visibile " : ", runtime invisibile ") 4.580 - .append(targetType) 4.581 - .append(", bound_index = ").append(bound_index) 4.582 - .append(", parameter_index = ").append(parameter_index) 4.583 - .append(", type_index = ").append(type_index) 4.584 - .append(", exception_index = ").append(exception_index) 4.585 - .append(", type_path = ["); 4.586 - 4.587 - for(int i = 0; i < typePath.length; i++) { 4.588 - if (i != 0) { 4.589 - sb.append(", "); 4.590 - } 4.591 - sb.append(typePath[i]); 4.592 - } 4.593 - sb.append("]") 4.594 - .append(" on field ").append(fieldname); 4.595 - return sb.toString(); 4.596 - } 4.597 - 4.598 - /** 4.599 - * See if this template applies to a field. 4.600 - * 4.601 - * @param fieldname The field name to check. 4.602 - * @return Whether or not this template should apply. 4.603 - */ 4.604 - public boolean matchFieldName(String fieldname) { 4.605 - return this.fieldname.equals(fieldname); 4.606 - } 4.607 - 4.608 - /** 4.609 - * A builder class for creating {@code 4.610 - * ExpectedFieldTypeAnnotation}s in a more convenient fashion. The 4.611 - * constructor for {@code ExpectedFieldTypeAnnotation} takes a 4.612 - * large number of parameters (by necessity). This class 4.613 - * allows users to construct a {@code ExpectedFieldTypeAnnotation}s 4.614 - * using only the ones they need. 4.615 - */ 4.616 - public static class Builder extends ExpectedTypeAnnotation.Builder { 4.617 - protected final String fieldname; 4.618 - 4.619 - /** 4.620 - * Create a {@code Builder} from the mandatory parameters. 4.621 - * 4.622 - * @param fieldname The expected field name. 4.623 - * @param expectedName The expected annotation name. 4.624 - * @param targetType The expected target type. 4.625 - * @param visibility Whether this annotation should be runtime-visible. 4.626 - * @param expectedCount The number of annotations that should be seen. 4.627 - */ 4.628 - public Builder(String fieldname, 4.629 - String expectedName, 4.630 - TypeAnnotation.TargetType targetType, 4.631 - boolean visibility, 4.632 - int expectedCount) { 4.633 - super(expectedName, targetType, visibility, expectedCount); 4.634 - this.fieldname = fieldname; 4.635 - } 4.636 - 4.637 - /** 4.638 - * Create an {@code ExpectedFieldTypeAnnotation} from all 4.639 - * parameters that have been provided. The default values 4.640 - * will be used for those that have not. 4.641 - * 4.642 - * @return The cretaed {@code ExpectedFieldTypeAnnotation}. 4.643 - */ 4.644 - public ExpectedFieldTypeAnnotation build() { 4.645 - return new ExpectedFieldTypeAnnotation(fieldname, expectedName, 4.646 - visibility, expectedCount, 4.647 - targetType, bound_index, 4.648 - parameter_index, type_index, 4.649 - exception_index, typePath); 4.650 - } 4.651 - } 4.652 - } 4.653 - 4.654 - private void matchClassTypeAnnotation(ClassFile classfile, 4.655 - ExpectedTypeAnnotation expected) 4.656 - throws ConstantPoolException { 4.657 - for(Attribute attr : classfile.attributes) { 4.658 - attr.accept(typeAnnoMatcher, expected); 4.659 - } 4.660 - } 4.661 - 4.662 - private void matchMethodTypeAnnotation(ClassFile classfile, 4.663 - ExpectedMethodTypeAnnotation expected) 4.664 - throws ConstantPoolException { 4.665 - for(Method meth : classfile.methods) { 4.666 - if (expected.matchMethodName(meth.getName(classfile.constant_pool))) { 4.667 - for(Attribute attr : meth.attributes) { 4.668 - attr.accept(typeAnnoMatcher, expected); 4.669 - } 4.670 - } 4.671 - } 4.672 - } 4.673 - 4.674 - private void matchFieldTypeAnnotation(ClassFile classfile, 4.675 - ExpectedFieldTypeAnnotation expected) 4.676 - throws ConstantPoolException { 4.677 - for(Field field : classfile.fields) { 4.678 - if (expected.matchFieldName(field.getName(classfile.constant_pool))) { 4.679 - for(Attribute attr : field.attributes) { 4.680 - attr.accept(typeAnnoMatcher, expected); 4.681 - } 4.682 - } 4.683 - } 4.684 - } 4.685 - 4.686 - private void matchClassTypeAnnotations(ClassFile classfile, 4.687 - ExpectedTypeAnnotation[] expected) 4.688 - throws ConstantPoolException { 4.689 - for(ExpectedTypeAnnotation one : expected) { 4.690 - matchClassTypeAnnotation(classfile, one); 4.691 - } 4.692 - } 4.693 - 4.694 - private void matchMethodTypeAnnotations(ClassFile classfile, 4.695 - ExpectedMethodTypeAnnotation[] expected) 4.696 - throws ConstantPoolException { 4.697 - for(ExpectedMethodTypeAnnotation one : expected) { 4.698 - matchMethodTypeAnnotation(classfile, one); 4.699 - } 4.700 - } 4.701 - 4.702 - private void matchFieldTypeAnnotations(ClassFile classfile, 4.703 - ExpectedFieldTypeAnnotation[] expected) 4.704 - throws ConstantPoolException { 4.705 - for(ExpectedFieldTypeAnnotation one : expected) { 4.706 - matchFieldTypeAnnotation(classfile, one); 4.707 - } 4.708 - } 4.709 - 4.710 - /** 4.711 - * Run a template on a single {@code ClassFile}. 4.712 - * 4.713 - * @param classfile The {@code ClassFile} on which to run tests. 4.714 - * @param expected The expected annotation template. 4.715 - */ 4.716 - public void run(ClassFile classfile, 4.717 - Expected... expected) 4.718 - throws ConstantPoolException { 4.719 - run(new ClassFile[] { classfile }, expected); 4.720 - } 4.721 - 4.722 - /** 4.723 - * Run a template on multiple {@code ClassFile}s. 4.724 - * 4.725 - * @param classfile The {@code ClassFile}s on which to run tests. 4.726 - * @param expected The expected annotation template. 4.727 - */ 4.728 - public void run(ClassFile[] classfiles, 4.729 - Expected... expected) 4.730 - throws ConstantPoolException { 4.731 - for(ClassFile classfile : classfiles) { 4.732 - for(Expected one : expected) { 4.733 - if (one.matchClassName(classfile.getName())) { 4.734 - if (one.classAnnos != null) 4.735 - matchClassTypeAnnotations(classfile, one.classAnnos); 4.736 - if (one.methodAnnos != null) 4.737 - matchMethodTypeAnnotations(classfile, one.methodAnnos); 4.738 - if (one.fieldAnnos != null) 4.739 - matchFieldTypeAnnotations(classfile, one.fieldAnnos); 4.740 - } 4.741 - } 4.742 - } 4.743 - int count = 0; 4.744 - for (Expected one : expected) { 4.745 - count += one.check(); 4.746 - } 4.747 - 4.748 - if (count != 0) { 4.749 - throw new RuntimeException(count + " errors occurred in test"); 4.750 - } 4.751 - } 4.752 - 4.753 - /** 4.754 - * Get a {@code ClassFile} from its file name. 4.755 - * 4.756 - * @param name The class' file name. 4.757 - * @return The {@code ClassFile} 4.758 - */ 4.759 - public static ClassFile getClassFile(String name) 4.760 - throws IOException, ConstantPoolException { 4.761 - final URL url = ClassfileInspector.class.getResource(name); 4.762 - final InputStream in = url.openStream(); 4.763 - try { 4.764 - return ClassFile.read(in); 4.765 - } finally { 4.766 - in.close(); 4.767 - } 4.768 - } 4.769 - 4.770 - private static final Attribute.Visitor<Void, ExpectedTypeAnnotation> typeAnnoMatcher = 4.771 - new Attribute.Visitor<Void, ExpectedTypeAnnotation>() { 4.772 - 4.773 - @Override 4.774 - public Void visitBootstrapMethods(BootstrapMethods_attribute attr, 4.775 - ExpectedTypeAnnotation expected) { 4.776 - return null; 4.777 - } 4.778 - 4.779 - @Override 4.780 - public Void visitDefault(DefaultAttribute attr, 4.781 - ExpectedTypeAnnotation expected) { 4.782 - return null; 4.783 - } 4.784 - 4.785 - @Override 4.786 - public Void visitAnnotationDefault(AnnotationDefault_attribute attr, 4.787 - ExpectedTypeAnnotation expected) { 4.788 - return null; 4.789 - } 4.790 - 4.791 - @Override 4.792 - public Void visitCharacterRangeTable(CharacterRangeTable_attribute attr, 4.793 - ExpectedTypeAnnotation expected) { 4.794 - return null; 4.795 - } 4.796 - 4.797 - @Override 4.798 - public Void visitCode(Code_attribute attr, 4.799 - ExpectedTypeAnnotation expected) { 4.800 - return null; 4.801 - } 4.802 - 4.803 - @Override 4.804 - public Void visitCompilationID(CompilationID_attribute attr, 4.805 - ExpectedTypeAnnotation expected) { 4.806 - return null; 4.807 - } 4.808 - 4.809 - @Override 4.810 - public Void visitConstantValue(ConstantValue_attribute attr, 4.811 - ExpectedTypeAnnotation expected) { 4.812 - return null; 4.813 - } 4.814 - 4.815 - @Override 4.816 - public Void visitDeprecated(Deprecated_attribute attr, 4.817 - ExpectedTypeAnnotation expected) { 4.818 - return null; 4.819 - } 4.820 - 4.821 - @Override 4.822 - public Void visitEnclosingMethod(EnclosingMethod_attribute attr, 4.823 - ExpectedTypeAnnotation expected) { 4.824 - return null; 4.825 - } 4.826 - 4.827 - @Override 4.828 - public Void visitExceptions(Exceptions_attribute attr, 4.829 - ExpectedTypeAnnotation expected) { 4.830 - return null; 4.831 - } 4.832 - 4.833 - @Override 4.834 - public Void visitInnerClasses(InnerClasses_attribute attr, 4.835 - ExpectedTypeAnnotation expected) { 4.836 - return null; 4.837 - } 4.838 - 4.839 - @Override 4.840 - public Void visitLineNumberTable(LineNumberTable_attribute attr, 4.841 - ExpectedTypeAnnotation expected) { 4.842 - return null; 4.843 - } 4.844 - 4.845 - @Override 4.846 - public Void visitLocalVariableTable(LocalVariableTable_attribute attr, 4.847 - ExpectedTypeAnnotation expected) { 4.848 - return null; 4.849 - } 4.850 - 4.851 - @Override 4.852 - public Void visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, 4.853 - ExpectedTypeAnnotation expected) { 4.854 - return null; 4.855 - } 4.856 - 4.857 - @Override 4.858 - public Void visitMethodParameters(MethodParameters_attribute attr, 4.859 - ExpectedTypeAnnotation expected) { 4.860 - return null; 4.861 - } 4.862 - 4.863 - @Override 4.864 - public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, 4.865 - ExpectedTypeAnnotation expected) { 4.866 - return null; 4.867 - } 4.868 - 4.869 - @Override 4.870 - public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, 4.871 - ExpectedTypeAnnotation expected) { 4.872 - return null; 4.873 - } 4.874 - 4.875 - @Override 4.876 - public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, 4.877 - ExpectedTypeAnnotation expected) { 4.878 - return null; 4.879 - } 4.880 - 4.881 - @Override 4.882 - public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, 4.883 - ExpectedTypeAnnotation expected) { 4.884 - return null; 4.885 - } 4.886 - 4.887 - @Override 4.888 - public Void visitSignature(Signature_attribute attr, 4.889 - ExpectedTypeAnnotation expected) { 4.890 - return null; 4.891 - } 4.892 - 4.893 - @Override 4.894 - public Void visitSourceDebugExtension(SourceDebugExtension_attribute attr, 4.895 - ExpectedTypeAnnotation expected) { 4.896 - return null; 4.897 - } 4.898 - 4.899 - @Override 4.900 - public Void visitSourceFile(SourceFile_attribute attr, 4.901 - ExpectedTypeAnnotation expected) { 4.902 - return null; 4.903 - } 4.904 - 4.905 - @Override 4.906 - public Void visitSourceID(SourceID_attribute attr, 4.907 - ExpectedTypeAnnotation expected) { 4.908 - return null; 4.909 - } 4.910 - 4.911 - @Override 4.912 - public Void visitStackMap(StackMap_attribute attr, 4.913 - ExpectedTypeAnnotation expected) { 4.914 - return null; 4.915 - } 4.916 - 4.917 - @Override 4.918 - public Void visitStackMapTable(StackMapTable_attribute attr, 4.919 - ExpectedTypeAnnotation expected) { 4.920 - return null; 4.921 - } 4.922 - 4.923 - @Override 4.924 - public Void visitSynthetic(Synthetic_attribute attr, 4.925 - ExpectedTypeAnnotation expected) { 4.926 - return null; 4.927 - } 4.928 - 4.929 - @Override 4.930 - public Void visitRuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotations_attribute attr, 4.931 - ExpectedTypeAnnotation expected) { 4.932 - if (expected.matchVisibility(true)) { 4.933 - for(TypeAnnotation anno : attr.annotations) { 4.934 - expected.matchAnnotation(anno); 4.935 - } 4.936 - } 4.937 - 4.938 - return null; 4.939 - } 4.940 - 4.941 - @Override 4.942 - public Void visitRuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotations_attribute attr, 4.943 - ExpectedTypeAnnotation expected) { 4.944 - if (expected.matchVisibility(false)) { 4.945 - for(TypeAnnotation anno : attr.annotations) { 4.946 - expected.matchAnnotation(anno); 4.947 - } 4.948 - } 4.949 - 4.950 - return null; 4.951 - } 4.952 - }; 4.953 -}
5.1 --- a/test/tools/javac/annotations/typeAnnotations/classfile/SyntheticParameters.java Mon Nov 24 12:49:30 2014 -0500 5.2 +++ b/test/tools/javac/annotations/typeAnnotations/classfile/SyntheticParameters.java Tue Nov 25 08:30:52 2014 -0500 5.3 @@ -24,10 +24,12 @@ 5.4 /* 5.5 * @test SyntheticParameters 5.6 * @summary Test generation of annotations on inner class parameters. 5.7 - * @build ClassfileInspector 5.8 + * @library /lib/annotations/ 5.9 * @run main SyntheticParameters 5.10 */ 5.11 5.12 +import annotations.classfile.ClassfileInspector; 5.13 + 5.14 import java.io.*; 5.15 import java.lang.annotation.*; 5.16 5.17 @@ -111,7 +113,8 @@ 5.18 5.19 public static void main(String... args) throws Exception { 5.20 new SyntheticParameters().run( 5.21 - new ClassFile[] { getClassFile(Inner_class), getClassFile(Foo_class) }, 5.22 + new ClassFile[] { getClassFile(Inner_class, Inner.class), 5.23 + getClassFile(Foo_class, Foo.class) }, 5.24 new Expected[] { Inner_expected, Foo_expected }); 5.25 } 5.26