Skip to content

Commit fd96d57

Browse files
authored
Bridge methods do not inherit annotations with RetentionPolicy.RUNTIME (#3936)
1 parent a51c7f1 commit fd96d57

File tree

2 files changed

+60
-2
lines changed
  • org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler
  • org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression

2 files changed

+60
-2
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ClassFile.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4337,8 +4337,11 @@ public int generateMethodInfoAttributes(MethodBinding methodBinding) {
43374337
AbstractMethodDeclaration methodDeclaration = methodBinding.sourceMethod();
43384338
if (methodBinding instanceof SyntheticMethodBinding) {
43394339
SyntheticMethodBinding syntheticMethod = (SyntheticMethodBinding) methodBinding;
4340-
if (syntheticMethod.purpose == SyntheticMethodBinding.SuperMethodAccess && CharOperation.equals(syntheticMethod.selector, syntheticMethod.targetMethod.selector))
4341-
methodDeclaration = ((SyntheticMethodBinding)methodBinding).targetMethod.sourceMethod();
4340+
if (syntheticMethod.purpose == SyntheticMethodBinding.BridgeMethod
4341+
|| (syntheticMethod.purpose == SyntheticMethodBinding.SuperMethodAccess
4342+
&& CharOperation.equals(syntheticMethod.selector, syntheticMethod.targetMethod.selector))) {
4343+
methodDeclaration = ((SyntheticMethodBinding) methodBinding).targetMethod.sourceMethod();
4344+
}
43424345
if (syntheticMethod.recordComponentBinding != null) {
43434346
assert methodDeclaration == null;
43444347
long rcMask = TagBits.AnnotationForMethod | TagBits.AnnotationForTypeUse;

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TypeAnnotationTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7116,4 +7116,59 @@ public void testBug566803() throws Exception {
71167116
},
71177117
"0 @X.TestAnnFirst()");
71187118
}
7119+
public void testGH3932_1() throws Exception {
7120+
this.runConformTest(
7121+
new String[] {
7122+
"A.java",
7123+
"public interface A<T> {\n"
7124+
+ " T getContent();\n"
7125+
+ "}\n",
7126+
"SourceAnnotation.java",
7127+
"import java.lang.annotation.Retention;\n"
7128+
+ "import java.lang.annotation.RetentionPolicy;\n"
7129+
+ "import java.lang.annotation.Target;\n"
7130+
+ "import java.lang.annotation.ElementType;\n"
7131+
+ "@Retention(RetentionPolicy.SOURCE)\n"
7132+
+ "@Target(ElementType.METHOD)\n"
7133+
+ "public @interface SourceAnnotation {}",
7134+
"ClassAnnotation.java",
7135+
"import java.lang.annotation.Retention;\n"
7136+
+ "import java.lang.annotation.RetentionPolicy;\n"
7137+
+ "import java.lang.annotation.Target;\n"
7138+
+ "import java.lang.annotation.ElementType;\n"
7139+
+ "@Retention(RetentionPolicy.CLASS)\n"
7140+
+ "@Target(ElementType.METHOD)\n"
7141+
+ "public @interface ClassAnnotation {}",
7142+
"RuntimeAnnotation.java",
7143+
"import java.lang.annotation.Retention;\n"
7144+
+ "import java.lang.annotation.RetentionPolicy;\n"
7145+
+ "import java.lang.annotation.Target;\n"
7146+
+ "import java.lang.annotation.ElementType;\n"
7147+
+ "@Retention(RetentionPolicy.RUNTIME)\n"
7148+
+ "@Target(ElementType.METHOD)\n"
7149+
+ "public @interface RuntimeAnnotation {}",
7150+
"B.java",
7151+
"public interface B extends A<String> {\n"
7152+
+ " @RuntimeAnnotation @ClassAnnotation @SourceAnnotation\n"
7153+
+ " default String getContent() {\n"
7154+
+ " return \"the string\";\n"
7155+
+ " }\n"
7156+
+ "}\n"
7157+
},
7158+
"");
7159+
String expectedOutput =
7160+
" public bridge synthetic java.lang.Object getContent();\n"
7161+
+ " 0 aload_0 [this]\n"
7162+
+ " 1 invokeinterface B.getContent() : java.lang.String [21] [nargs: 1]\n"
7163+
+ " 6 areturn\n"
7164+
+ " Line numbers:\n"
7165+
+ " [pc: 0, line: 1]\n"
7166+
+ " RuntimeVisibleAnnotations: \n"
7167+
+ " #12 @RuntimeAnnotation(\n"
7168+
+ " )\n"
7169+
+ " RuntimeInvisibleAnnotations: \n"
7170+
+ " #10 @ClassAnnotation(\n"
7171+
+ " )\n";
7172+
checkDisassembledClassFile(OUTPUT_DIR + File.separator + "B.class", "B", expectedOutput, ClassFileBytesDisassembler.SYSTEM);
7173+
}
71197174
}

0 commit comments

Comments
 (0)