提交 2ac88bb5 编写于 作者: V vromero

6695379: Copy method annotations and parameter annotations to synthetic bridge methods

Reviewed-by: mcimadamore
上级 50b0b601
...@@ -258,6 +258,14 @@ public class TransTypes extends TreeTranslator { ...@@ -258,6 +258,14 @@ public class TransTypes extends TreeTranslator {
meth.name, meth.name,
bridgeType, bridgeType,
origin); origin);
/* once JDK-6996415 is solved it should be checked if this approach can
* be applied to method addOverrideBridgesIfNeeded
*/
bridge.params = createBridgeParams(impl, bridge, bridgeType);
if (impl.annotations != null) {
bridge.annotations.setAttributes(impl.annotations);
}
if (!hypothetical) { if (!hypothetical) {
JCMethodDecl md = make.MethodDef(bridge, null); JCMethodDecl md = make.MethodDef(bridge, null);
...@@ -292,6 +300,28 @@ public class TransTypes extends TreeTranslator { ...@@ -292,6 +300,28 @@ public class TransTypes extends TreeTranslator {
overridden.put(bridge, meth); overridden.put(bridge, meth);
} }
private List<VarSymbol> createBridgeParams(MethodSymbol impl, MethodSymbol bridge,
Type bridgeType) {
List<VarSymbol> bridgeParams = null;
if (impl.params != null) {
bridgeParams = List.nil();
List<VarSymbol> implParams = impl.params;
Type.MethodType mType = (Type.MethodType)bridgeType;
List<Type> argTypes = mType.argtypes;
while (implParams.nonEmpty() && argTypes.nonEmpty()) {
VarSymbol param = new VarSymbol(implParams.head.flags() | SYNTHETIC,
implParams.head.name, argTypes.head, bridge);
if (implParams.head.annotations != null) {
param.annotations.setAttributes(implParams.head.annotations);
}
bridgeParams = bridgeParams.append(param);
implParams = implParams.tail;
argTypes = argTypes.tail;
}
}
return bridgeParams;
}
/** Add bridge if given symbol is a non-private, non-static member /** Add bridge if given symbol is a non-private, non-static member
* of the given class, which is either defined in the class or non-final * of the given class, which is either defined in the class or non-final
* inherited, and one of the two following conditions holds: * inherited, and one of the two following conditions holds:
......
...@@ -31,7 +31,6 @@ import java.util.Map; ...@@ -31,7 +31,6 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.HashSet; import java.util.HashSet;
import javax.lang.model.type.TypeKind;
import javax.tools.JavaFileManager; import javax.tools.JavaFileManager;
import javax.tools.FileObject; import javax.tools.FileObject;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
...@@ -39,9 +38,6 @@ import javax.tools.JavaFileObject; ...@@ -39,9 +38,6 @@ import javax.tools.JavaFileObject;
import com.sun.tools.javac.code.*; import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Attribute.RetentionPolicy; import com.sun.tools.javac.code.Attribute.RetentionPolicy;
import com.sun.tools.javac.code.Attribute.TypeCompound; import com.sun.tools.javac.code.Attribute.TypeCompound;
import static com.sun.tools.javac.code.BoundKind.EXTENDS;
import static com.sun.tools.javac.code.BoundKind.SUPER;
import static com.sun.tools.javac.code.BoundKind.UNBOUND;
import com.sun.tools.javac.code.Symbol.*; import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Type.*; import com.sun.tools.javac.code.Type.*;
import com.sun.tools.javac.code.Types.UniqueType; import com.sun.tools.javac.code.Types.UniqueType;
...@@ -674,7 +670,8 @@ public class ClassWriter extends ClassFile { ...@@ -674,7 +670,8 @@ public class ClassWriter extends ClassFile {
int writeParameterAttrs(MethodSymbol m) { int writeParameterAttrs(MethodSymbol m) {
boolean hasVisible = false; boolean hasVisible = false;
boolean hasInvisible = false; boolean hasInvisible = false;
if (m.params != null) for (VarSymbol s : m.params) { if (m.params != null) {
for (VarSymbol s : m.params) {
for (Attribute.Compound a : s.getRawAttributes()) { for (Attribute.Compound a : s.getRawAttributes()) {
switch (types.getRetention(a)) { switch (types.getRetention(a)) {
case SOURCE: break; case SOURCE: break;
...@@ -684,6 +681,7 @@ public class ClassWriter extends ClassFile { ...@@ -684,6 +681,7 @@ public class ClassWriter extends ClassFile {
} }
} }
} }
}
int attrCount = 0; int attrCount = 0;
if (hasVisible) { if (hasVisible) {
......
/* /*
* Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -429,9 +429,9 @@ public class T6889255 { ...@@ -429,9 +429,9 @@ public class T6889255 {
// -- no Code attribute for the LocalVariableTable attribute // -- no Code attribute for the LocalVariableTable attribute
if ((v.owner.flags() & Flags.ABSTRACT) != 0) if ((v.owner.flags() & Flags.ABSTRACT) != 0)
return "arg" + (i - 1); return "arg" + (i - 1);
// bridge methods use xN // bridge methods use argN. No LVT for them anymore
if ((v.owner.flags() & Flags.BRIDGE) != 0) if ((v.owner.flags() & Flags.BRIDGE) != 0)
return "x" + (i - 1); return "arg" + (i - 1);
// The rest of this method assumes the local conventions in the test program // The rest of this method assumes the local conventions in the test program
Type t = v.type; Type t = v.type;
......
...@@ -21,10 +21,8 @@ ...@@ -21,10 +21,8 @@
* questions. * questions.
*/ */
import com.sun.tools.classfile.*;
import java.io.*; import java.io.*;
import javax.lang.model.element.*; import com.sun.tools.classfile.*;
import java.util.*;
/** /**
* The {@code ClassFileVisitor} reads a class file using the * The {@code ClassFileVisitor} reads a class file using the
...@@ -150,6 +148,7 @@ class ClassFileVisitor extends Tester.Visitor { ...@@ -150,6 +148,7 @@ class ClassFileVisitor extends Tester.Visitor {
public int mNumParams; public int mNumParams;
public boolean mSynthetic; public boolean mSynthetic;
public boolean mIsConstructor; public boolean mIsConstructor;
public boolean mIsBridge;
public String prefix; public String prefix;
void visitMethod(Method method, StringBuilder sb) throws Exception { void visitMethod(Method method, StringBuilder sb) throws Exception {
...@@ -162,6 +161,7 @@ class ClassFileVisitor extends Tester.Visitor { ...@@ -162,6 +161,7 @@ class ClassFileVisitor extends Tester.Visitor {
mSynthetic = method.access_flags.is(AccessFlags.ACC_SYNTHETIC); mSynthetic = method.access_flags.is(AccessFlags.ACC_SYNTHETIC);
mIsConstructor = mName.equals("<init>"); mIsConstructor = mName.equals("<init>");
prefix = cname + "." + mName + "() - "; prefix = cname + "." + mName + "() - ";
mIsBridge = method.access_flags.is(AccessFlags.ACC_BRIDGE);
sb.append(cname).append(".").append(mName).append("("); sb.append(cname).append(".").append(mName).append("(");
...@@ -320,6 +320,12 @@ class ClassFileVisitor extends Tester.Visitor { ...@@ -320,6 +320,12 @@ class ClassFileVisitor extends Tester.Visitor {
} else if (isEnum && mNumParams == 1 && index == 0 && mName.equals("valueOf")) { } else if (isEnum && mNumParams == 1 && index == 0 && mName.equals("valueOf")) {
expect = "name"; expect = "name";
allowMandated = true; allowMandated = true;
} else if (mIsBridge) {
allowSynthetic = true;
/* you can't expect an special name for bridges' parameters.
* The name of the original parameters are now copied.
*/
expect = null;
} }
if (mandated) sb.append("!"); if (mandated) sb.append("!");
if (synthetic) sb.append("!!"); if (synthetic) sb.append("!!");
......
...@@ -22,8 +22,6 @@ ...@@ -22,8 +22,6 @@
*/ */
import java.io.*; import java.io.*;
import java.util.*;
import java.net.*;
import java.lang.reflect.*; import java.lang.reflect.*;
/** /**
...@@ -250,7 +248,7 @@ public class ReflectionVisitor extends Tester.Visitor { ...@@ -250,7 +248,7 @@ public class ReflectionVisitor extends Tester.Visitor {
String expect = m.isSynthetic() ? ("arg" + i) : ((++c) + param); String expect = m.isSynthetic() ? ("arg" + i) : ((++c) + param);
param = p.getName(); param = p.getName();
sb.append(sep).append(param); sb.append(sep).append(param);
if (!expect.equals(param)) { if (!m.isBridge() && !expect.equals(param)) {
error(prefix + "param[" + i + "]='" error(prefix + "param[" + i + "]='"
+ param + "' expected '" + expect + "'"); + param + "' expected '" + expect + "'");
break; break;
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
*/ */
import java.io.*; import java.io.*;
import java.util.*;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
/** /**
......
/*
* Copyright (c) 2013, 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 6695379
* @summary Copy method annotations and parameter annotations to synthetic
* bridge methods
* @run main AnnotationsAreNotCopiedToBridgeMethodsTest
*/
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.io.BufferedInputStream;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.sun.tools.classfile.AccessFlags;
import com.sun.tools.classfile.Attribute;
import com.sun.tools.classfile.Attributes;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.Method;
import com.sun.tools.javac.util.Assert;
public class AnnotationsAreNotCopiedToBridgeMethodsTest {
public static void main(String[] args) throws Exception {
new AnnotationsAreNotCopiedToBridgeMethodsTest().run();
}
void run() throws Exception {
checkClassFile(Paths.get(System.getProperty("test.classes"),
this.getClass().getSimpleName() + "$CovariantReturnType.class"));
checkClassFile(Paths.get(System.getProperty("test.classes"),
this.getClass().getSimpleName() +
"$CovariantReturnType$VisibilityChange.class"));
}
void checkClassFile(final Path cfilePath) throws Exception {
ClassFile classFile = ClassFile.read(
new BufferedInputStream(Files.newInputStream(cfilePath)));
for (Method method : classFile.methods) {
if (method.access_flags.is(AccessFlags.ACC_BRIDGE)) {
checkForAttr(method.attributes,
"Annotations hasn't been copied to bridge method",
Attribute.RuntimeVisibleAnnotations,
Attribute.RuntimeVisibleParameterAnnotations);
}
}
}
void checkForAttr(Attributes attrs, String errorMsg, String... attrNames) {
for (String attrName : attrNames) {
Assert.checkNonNull(attrs.get(attrName), errorMsg);
}
}
@Target(value = {ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@interface ParamAnnotation {}
@Target(value = {ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MethodAnnotation {}
abstract class T<A,B> {
B m(A a){return null;}
}
class CovariantReturnType extends T<Integer, Integer> {
@MethodAnnotation
Integer m(@ParamAnnotation Integer i) {
return i;
}
public class VisibilityChange extends CovariantReturnType {}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册