|
| 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 | + """ |
| 59 | + import java.util.function.*; |
| 60 | + import java.lang.annotation.ElementType; |
| 61 | + import java.lang.annotation.Target; |
| 62 | +
|
| 63 | + @Target({ElementType.TYPE_USE, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE}) |
| 64 | + @interface A {} |
| 65 | +
|
| 66 | + class Test { |
| 67 | + void kaa() { |
| 68 | + @A var c = g(1, 1L); |
| 69 | + } |
| 70 | +
|
| 71 | + <X> X g(X a, X b) { |
| 72 | + return a; |
| 73 | + } |
| 74 | +
|
| 75 | + void foo() { |
| 76 | + bar((@A var s) -> s); |
| 77 | + } |
| 78 | +
|
| 79 | + void bar(Function<String, String> f) {} |
| 80 | + } |
| 81 | + """; |
| 82 | + |
| 83 | + public static void main(String... args) throws Exception { |
| 84 | + new VariablesDeclaredWithVarTest().run(); |
| 85 | + } |
| 86 | + |
| 87 | + void run() throws Exception { |
| 88 | + compileTestClass(); |
| 89 | + checkClassFile(new File(Paths.get(System.getProperty("user.dir"), |
| 90 | + "Test.class").toUri()), 0); |
| 91 | + } |
| 92 | + |
| 93 | + void compileTestClass() throws Exception { |
| 94 | + new JavacTask(tb) |
| 95 | + .sources(src) |
| 96 | + .run(); |
| 97 | + } |
| 98 | + |
| 99 | + void checkClassFile(final File cfile, int... taPositions) throws Exception { |
| 100 | + ClassFile classFile = ClassFile.read(cfile); |
| 101 | + List<TypeAnnotation> annos = new ArrayList<>(); |
| 102 | + for (Method method : classFile.methods) { |
| 103 | + findAnnotations(classFile, method, annos); |
| 104 | + String methodName = method.getName(classFile.constant_pool); |
| 105 | + Assert.check(annos.size() == 0, "there shouldn't be any type annotations in any method, found " + annos.size() + |
| 106 | + " type annotations at method " + methodName); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + void findAnnotations(ClassFile cf, Method m, List<TypeAnnotation> annos) { |
| 111 | + findAnnotations(cf, m, Attribute.RuntimeVisibleTypeAnnotations, annos); |
| 112 | + findAnnotations(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, annos); |
| 113 | + } |
| 114 | + |
| 115 | + void findAnnotations(ClassFile cf, Method m, String name, List<TypeAnnotation> annos) { |
| 116 | + int index = m.attributes.getIndex(cf.constant_pool, name); |
| 117 | + if (index != -1) { |
| 118 | + Attribute attr = m.attributes.get(index); |
| 119 | + assert attr instanceof RuntimeTypeAnnotations_attribute; |
| 120 | + RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; |
| 121 | + annos.addAll(Arrays.asList(tAttr.annotations)); |
| 122 | + } |
| 123 | + |
| 124 | + int cindex = m.attributes.getIndex(cf.constant_pool, Attribute.Code); |
| 125 | + if (cindex != -1) { |
| 126 | + Attribute cattr = m.attributes.get(cindex); |
| 127 | + assert cattr instanceof Code_attribute; |
| 128 | + Code_attribute cAttr = (Code_attribute)cattr; |
| 129 | + index = cAttr.attributes.getIndex(cf.constant_pool, name); |
| 130 | + if (index != -1) { |
| 131 | + Attribute attr = cAttr.attributes.get(index); |
| 132 | + assert attr instanceof RuntimeTypeAnnotations_attribute; |
| 133 | + RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; |
| 134 | + annos.addAll(Arrays.asList(tAttr.annotations)); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments