|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 8261205 |
| 27 | + * @summary check that potentially applicable type annotations are skip if the variable or parameter was declared with var |
| 28 | + * @library /tools/lib |
| 29 | + * @modules |
| 30 | + * jdk.jdeps/com.sun.tools.classfile |
| 31 | + * jdk.compiler/com.sun.tools.javac.api |
| 32 | + * jdk.compiler/com.sun.tools.javac.main |
| 33 | + * jdk.compiler/com.sun.tools.javac.code |
| 34 | + * jdk.compiler/com.sun.tools.javac.util |
| 35 | + * @build toolbox.ToolBox toolbox.JavacTask |
| 36 | + * @run main VariablesDeclaredWithVarTest |
| 37 | + */ |
| 38 | + |
| 39 | +import java.util.List; |
| 40 | +import java.util.ArrayList; |
| 41 | + |
| 42 | +import java.io.File; |
| 43 | +import java.nio.file.Paths; |
| 44 | + |
| 45 | +import java.lang.annotation.*; |
| 46 | +import java.util.Arrays; |
| 47 | + |
| 48 | +import com.sun.tools.classfile.*; |
| 49 | +import com.sun.tools.javac.util.Assert; |
| 50 | + |
| 51 | +import toolbox.JavacTask; |
| 52 | +import toolbox.ToolBox; |
| 53 | + |
| 54 | +public class VariablesDeclaredWithVarTest { |
| 55 | + ToolBox tb = new ToolBox(); |
| 56 | + |
| 57 | + final String src = |
| 58 | + "import java.util.function.*;\n" + |
| 59 | + "import java.lang.annotation.ElementType;\n" + |
| 60 | + "import java.lang.annotation.Target;\n" + |
| 61 | + "\n" + |
| 62 | + "@Target({ElementType.TYPE_USE, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})\n" + |
| 63 | + "@interface A {}\n" + |
| 64 | + "\n" + |
| 65 | + "class Test {\n" + |
| 66 | + " void kaa() {\n" + |
| 67 | + " @A var c = g(1, 1L);\n" + |
| 68 | + " }\n" + |
| 69 | + "\n" + |
| 70 | + " <X> X g(X a, X b) {\n" + |
| 71 | + " return a;\n" + |
| 72 | + " }\n" + |
| 73 | + "\n" + |
| 74 | + " void foo() {\n" + |
| 75 | + " bar((@A var s) -> s);\n" + |
| 76 | + " }\n" + |
| 77 | + "\n" + |
| 78 | + " void bar(Function<String, String> f) {}\n" + |
| 79 | + "}\n"; |
| 80 | + |
| 81 | + public static void main(String... args) throws Exception { |
| 82 | + new VariablesDeclaredWithVarTest().run(); |
| 83 | + } |
| 84 | + |
| 85 | + void run() throws Exception { |
| 86 | + compileTestClass(); |
| 87 | + checkClassFile(new File(Paths.get(System.getProperty("user.dir"), |
| 88 | + "Test.class").toUri()), 0); |
| 89 | + } |
| 90 | + |
| 91 | + void compileTestClass() throws Exception { |
| 92 | + new JavacTask(tb) |
| 93 | + .sources(src) |
| 94 | + .run(); |
| 95 | + } |
| 96 | + |
| 97 | + void checkClassFile(final File cfile, int... taPositions) throws Exception { |
| 98 | + ClassFile classFile = ClassFile.read(cfile); |
| 99 | + List<TypeAnnotation> annos = new ArrayList<>(); |
| 100 | + for (Method method : classFile.methods) { |
| 101 | + findAnnotations(classFile, method, annos); |
| 102 | + String methodName = method.getName(classFile.constant_pool); |
| 103 | + Assert.check(annos.size() == 0, "there shouldn't be any type annotations in any method, found " + annos.size() + |
| 104 | + " type annotations at method " + methodName); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + void findAnnotations(ClassFile cf, Method m, List<TypeAnnotation> annos) { |
| 109 | + findAnnotations(cf, m, Attribute.RuntimeVisibleTypeAnnotations, annos); |
| 110 | + findAnnotations(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, annos); |
| 111 | + } |
| 112 | + |
| 113 | + void findAnnotations(ClassFile cf, Method m, String name, List<TypeAnnotation> annos) { |
| 114 | + int index = m.attributes.getIndex(cf.constant_pool, name); |
| 115 | + if (index != -1) { |
| 116 | + Attribute attr = m.attributes.get(index); |
| 117 | + assert attr instanceof RuntimeTypeAnnotations_attribute; |
| 118 | + RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; |
| 119 | + annos.addAll(Arrays.asList(tAttr.annotations)); |
| 120 | + } |
| 121 | + |
| 122 | + int cindex = m.attributes.getIndex(cf.constant_pool, Attribute.Code); |
| 123 | + if (cindex != -1) { |
| 124 | + Attribute cattr = m.attributes.get(cindex); |
| 125 | + assert cattr instanceof Code_attribute; |
| 126 | + Code_attribute cAttr = (Code_attribute)cattr; |
| 127 | + index = cAttr.attributes.getIndex(cf.constant_pool, name); |
| 128 | + if (index != -1) { |
| 129 | + Attribute attr = cAttr.attributes.get(index); |
| 130 | + assert attr instanceof RuntimeTypeAnnotations_attribute; |
| 131 | + RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; |
| 132 | + annos.addAll(Arrays.asList(tAttr.annotations)); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments