1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8256266 8281238
* @summary Verify annotations work correctly on binding variables
* @library /tools/javac/lib
* @modules java.compiler
* jdk.compiler
* @build JavacTestingAbstractProcessor
* @compile Annotations.java
* @compile -processor Annotations -proc:only Annotations.java
* @run main Annotations
*/
import com.sun.source.tree.InstanceOfTree;
import com.sun.source.util.TreePath;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.util.Set;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import com.sun.source.tree.BindingPatternTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.Trees;
import java.lang.classfile.*;
import java.lang.classfile.attribute.CodeAttribute;
import java.lang.classfile.attribute.RuntimeInvisibleTypeAnnotationsAttribute;
import java.io.InputStream;
public class Annotations extends JavacTestingAbstractProcessor {
public static void main(String... args) throws Exception {
new Annotations().run();
}
void run() throws Exception {
InputStream annotationsClass =
Annotations.class.getResourceAsStream("Annotations.class");
assert annotationsClass != null;
ClassModel cf = ClassFile.of().parse(annotationsClass.readAllBytes());
for (MethodModel m : cf.methods()) {
if (m.methodName().equalsString("test")) {
CodeAttribute codeAttr = m.findAttribute(Attributes.code()).orElseThrow();
RuntimeInvisibleTypeAnnotationsAttribute annotations = codeAttr.findAttribute(Attributes.runtimeInvisibleTypeAnnotations()).orElseThrow();
String expected = "LAnnotations$DTA; pos: [LOCAL_VARIABLE, {start_pc=31, end_pc=38, index=1}], " +
"LAnnotations$TA; pos: [LOCAL_VARIABLE, {start_pc=50, end_pc=57, index=1}], ";
StringBuilder actual = new StringBuilder();
for (TypeAnnotation ta: annotations.annotations()) {
TypeAnnotation.LocalVarTargetInfo info = ((TypeAnnotation.LocalVarTarget) ta.targetInfo()).table().getFirst();
actual.append(ta.annotation().className().stringValue() + " pos: [" + ta.targetInfo().targetType());
actual.append(", {start_pc=" + codeAttr.labelToBci(info.startLabel()) + ", end_pc=" + codeAttr.labelToBci(info.endLabel()));
actual.append(", index=" + info.index()+ "}], ");
}
if (!expected.contentEquals(actual)) {
throw new AssertionError("Unexpected type annotations: " +
actual);
}
}
}
}
private static void test(Object o) {
if (o instanceof @DA String da) {
System.err.println(da);
}
if (o instanceof @DTA String dta) {
System.err.println(dta);
}
if (o instanceof @TA String ta) {
System.err.println(ta);
}
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Trees trees = Trees.instance(processingEnv);
for (Element root : roundEnv.getRootElements()) {
TreePath tp = trees.getPath(root);
new TreePathScanner<Void, Void>() {
@Override
public Void visitInstanceOf(InstanceOfTree node, Void p) {
BindingPatternTree bpt = (BindingPatternTree) node.getPattern();
VariableTree var = bpt.getVariable();
Element varEl = trees.getElement(new TreePath(getCurrentPath(), var));
String expectedDeclAnnos;
String expectedType;
switch (var.getName().toString()) {
case "da" -> {
expectedDeclAnnos = "@Annotations.DA";
expectedType = "java.lang.String";
}
case "dta" -> {
expectedDeclAnnos = "@Annotations.DTA";
expectedType = "java.lang.@Annotations.DTA String";
}
case "ta" -> {
expectedDeclAnnos = "";
expectedType = "java.lang.@Annotations.TA String";
}
default -> {
throw new AssertionError("Unexpected variable: " + var);
}
}
String declAnnos = varEl.getAnnotationMirrors().toString();
if (!expectedDeclAnnos.equals(declAnnos)) {
throw new AssertionError("Unexpected modifiers: " + declAnnos +
" for: " + var.getName());
}
TypeMirror varType = varEl.asType();
String type = varType.toString();
if (!expectedType.equals(type)) {
throw new AssertionError("Unexpected type: " + type +
" for: " + var.getName() + " expected " + expectedType);
}
return super.visitInstanceOf(node, p);
}
}.scan(tp.getCompilationUnit(), null);
}
return false;
}
@Target(ElementType.LOCAL_VARIABLE)
@interface DA {}
@Target({ElementType.TYPE_USE, ElementType.LOCAL_VARIABLE})
@interface DTA {}
@Target(ElementType.TYPE_USE)
@interface TA {}
}
|