Fri, 31 Aug 2018 18:43:01 +0100
8160928: javac incorrectly copies over interior type annotations to bridge method
Reviewed-by: vromero, mcimadamore
Contributed-by: Evgeny Mandrikov <mandrikov@gmail.com>
1.1 --- a/src/share/classes/com/sun/tools/javac/code/SymbolMetadata.java Tue Jul 24 09:52:12 2018 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/SymbolMetadata.java Fri Aug 31 18:43:01 2018 +0100 1.3 @@ -31,10 +31,13 @@ 1.4 1.5 import com.sun.tools.javac.comp.Annotate; 1.6 import com.sun.tools.javac.comp.AttrContext; 1.7 +import com.sun.tools.javac.code.Attribute.TypeCompound; 1.8 +import com.sun.tools.javac.code.Kinds; 1.9 import com.sun.tools.javac.comp.Env; 1.10 import com.sun.tools.javac.util.*; 1.11 import com.sun.tools.javac.util.Assert; 1.12 import com.sun.tools.javac.util.List; 1.13 +import com.sun.tools.javac.util.ListBuffer; 1.14 import com.sun.tools.javac.util.Log; 1.15 import com.sun.tools.javac.util.Pair; 1.16 import static com.sun.tools.javac.code.Kinds.PCK; 1.17 @@ -152,9 +155,22 @@ 1.18 throw new NullPointerException(); 1.19 } 1.20 setDeclarationAttributes(other.getDeclarationAttributes()); 1.21 - setTypeAttributes(other.getTypeAttributes()); 1.22 - setInitTypeAttributes(other.getInitTypeAttributes()); 1.23 - setClassInitTypeAttributes(other.getClassInitTypeAttributes()); 1.24 + if ((sym.flags() & Flags.BRIDGE) != 0) { 1.25 + Assert.check(other.sym.kind == Kinds.MTH); 1.26 + ListBuffer<TypeCompound> typeAttributes = new ListBuffer<>(); 1.27 + for (TypeCompound tc : other.getTypeAttributes()) { 1.28 + // Carry over only contractual type annotations: i.e nothing interior to method body. 1.29 + if (!tc.position.type.isLocal()) 1.30 + typeAttributes.append(tc); 1.31 + } 1.32 + setTypeAttributes(typeAttributes.toList()); 1.33 + } else { 1.34 + setTypeAttributes(other.getTypeAttributes()); 1.35 + } 1.36 + if (sym.kind == Kinds.TYP) { 1.37 + setInitTypeAttributes(other.getInitTypeAttributes()); 1.38 + setClassInitTypeAttributes(other.getClassInitTypeAttributes()); 1.39 + } 1.40 } 1.41 1.42 public void setDeclarationAttributesWithCompletion(final Annotate.AnnotateRepeatedContext<Attribute.Compound> ctx) {
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/annotations/typeAnnotations/classfile/BridgeShouldHaveNoInteriorAnnotationsTest.java Fri Aug 31 18:43:01 2018 +0100 2.3 @@ -0,0 +1,97 @@ 2.4 +/* 2.5 + * Copyright (c) 2018, 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 +/* 2.28 + * @test 2.29 + * @bug 8160928 2.30 + * @summary javac incorrectly copies over interior type annotations to bridge method 2.31 + * @library /tools/javac/lib 2.32 + * @build ToolBox 2.33 + * @run compile -g BridgeShouldHaveNoInteriorAnnotationsTest.java 2.34 + * @run main BridgeShouldHaveNoInteriorAnnotationsTest 2.35 + */ 2.36 + 2.37 +import java.nio.file.Path; 2.38 +import java.nio.file.Paths; 2.39 +import java.lang.annotation.ElementType; 2.40 +import java.lang.annotation.Target; 2.41 +import java.io.*; 2.42 + 2.43 +class Pair_8160928<T1, T2> { 2.44 +} 2.45 + 2.46 +public class BridgeShouldHaveNoInteriorAnnotationsTest implements java.util.Iterator<Pair_8160928<Object, Object>> { 2.47 + 2.48 + @Override 2.49 + public boolean hasNext() { 2.50 + throw new RuntimeException(); 2.51 + } 2.52 + 2.53 + @Override 2.54 + public Pair_8160928<@NonNull Object, Object> next() { 2.55 + Comparable<@NonNull Object> cble1 = (Comparable<@NonNull Object>) null; 2.56 + return null; 2.57 + } 2.58 + 2.59 + @Override 2.60 + public void remove() { 2.61 + throw new RuntimeException(); 2.62 + } 2.63 + 2.64 + @Target(ElementType.TYPE_USE) 2.65 + public @interface NonNull { 2.66 + } 2.67 + 2.68 + static class OutputExpectedOnceHolder { 2.69 + public String[] outputs = { 2.70 + "0: #55(): CAST, offset=0, type_index=0, location=[TYPE_ARGUMENT(0)]", 2.71 + "1: #55(): LOCAL_VARIABLE, {start_pc=5, length=2, index=1}, location=[TYPE_ARGUMENT(0)]", 2.72 + }; 2.73 + } 2.74 + 2.75 + static class OutputExpectedTwiceHolder { 2.76 + public String[] outputs = { 2.77 + "0: #55(): METHOD_RETURN, location=[TYPE_ARGUMENT(0)]", 2.78 + }; 2.79 + } 2.80 + 2.81 + public static void main(String[] args) throws Exception { 2.82 + Path classPath = Paths.get(System.getProperty("test.classes"), "BridgeShouldHaveNoInteriorAnnotationsTest.class"); 2.83 + String javapOut = ToolBox.javap(new ToolBox.JavaToolArgs().setAllArgs("-v", "-p", classPath.toString())); 2.84 + 2.85 + OutputExpectedOnceHolder holder = new OutputExpectedOnceHolder(); 2.86 + for (String s : holder.outputs) { 2.87 + String newOutput = javapOut.replace(s, ""); 2.88 + if (((javapOut.length() - newOutput.length()) / s.length()) != 1) 2.89 + throw new AssertionError("Interior annotations carried over to bridge ?"); 2.90 + } 2.91 + 2.92 + OutputExpectedTwiceHolder holder2 = new OutputExpectedTwiceHolder(); 2.93 + for (String s : holder2.outputs) { 2.94 + String newOutput = javapOut.replace(s, ""); 2.95 + if (((javapOut.length() - newOutput.length()) / s.length()) != 2) 2.96 + throw new AssertionError("Exterior annotations not properly carried over to bridge"); 2.97 + } 2.98 + } 2.99 + 2.100 +}