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