Skip to content

Commit c9bbd55

Browse files
committed
8292159: TYPE_USE annotations on generic type arguments of record components discarded
Backport-of: 4d9a1cd26fa0cda902aafcccd6e02bd7bc60bbb3
1 parent 013709f commit c9bbd55

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import com.sun.tools.javac.code.Attribute.TypeCompound;
3434
import com.sun.tools.javac.code.Symbol.ClassSymbol;
35+
import com.sun.tools.javac.code.Symbol.RecordComponent;
3536
import com.sun.tools.javac.code.Symbol.TypeSymbol;
3637
import com.sun.tools.javac.code.Type.ArrayType;
3738
import com.sun.tools.javac.code.Type.CapturedType;
@@ -75,6 +76,7 @@
7576
import com.sun.tools.javac.util.Log;
7677
import com.sun.tools.javac.util.Names;
7778

79+
import static com.sun.tools.javac.code.Flags.RECORD;
7880
import static com.sun.tools.javac.code.Kinds.Kind.*;
7981

8082
/**
@@ -1293,6 +1295,16 @@ public void visitVarDef(final JCVariableDecl tree) {
12931295
if (!sigOnly) {
12941296
scan(tree.init);
12951297
}
1298+
1299+
// Now that type and declaration annotations have been segregated into their own buckets ...
1300+
if (sigOnly) {
1301+
if (tree.sym != null && tree.sym.getKind() == ElementKind.FIELD && (tree.sym.flags_field & RECORD) != 0) {
1302+
RecordComponent rc = ((ClassSymbol)tree.sym.owner).getRecordComponent(tree.sym);
1303+
rc.setTypeAttributes(tree.sym.getRawTypeAttributes());
1304+
// to get all the type annotations applied to the type
1305+
rc.type = tree.sym.type;
1306+
}
1307+
}
12961308
}
12971309

12981310
@Override

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2972,9 +2972,15 @@ private void validateAnnotation(JCAnnotation a, JCTree declarationTree, Symbol s
29722972
rc.appendAttributes(s.getRawAttributes().stream().filter(anno ->
29732973
Arrays.stream(getTargetNames(anno.type.tsym)).anyMatch(name -> name == names.RECORD_COMPONENT)
29742974
).collect(List.collector()));
2975-
rc.setTypeAttributes(s.getRawTypeAttributes());
2976-
// to get all the type annotations applied to the type
2977-
rc.type = s.type;
2975+
2976+
/* At this point, we used to carry over any type annotations from the VARDEF to the record component, but
2977+
* that is problematic, since we get here only when *some* annotation is applied to the SE5 (declaration)
2978+
* annotation location, inadvertently failing to carry over the type annotations when the VarDef has no
2979+
* annotations in the SE5 annotation location.
2980+
*
2981+
* Now type annotations are assigned to record components in a method that would execute irrespective of
2982+
* whether there are SE5 annotations on a VarDef viz com.sun.tools.javac.code.TypeAnnotations.TypeAnnotationPositions.visitVarDef
2983+
*/
29782984
}
29792985
}
29802986
}

test/langtools/tools/javac/records/RecordCompilationTests.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,57 @@ record R(@Anno String s) {}
14961496
}
14971497
}
14981498

1499+
// JDK-8292159: TYPE_USE annotations on generic type arguments
1500+
// of record components discarded
1501+
public void testOnlyTypeAnnotationsOnComponentField() throws Exception {
1502+
String code =
1503+
"""
1504+
import java.lang.annotation.*;
1505+
import java.util.List;
1506+
@Target({ElementType.TYPE_USE})
1507+
@Retention(RetentionPolicy.RUNTIME)
1508+
@interface Anno { }
1509+
record R(List<@Anno String> s) {}
1510+
""";
1511+
1512+
File dir = assertOK(true, code);
1513+
1514+
ClassFile classFile = ClassFile.read(findClassFileOrFail(dir, "R.class"));
1515+
1516+
// field first
1517+
Assert.check(classFile.fields.length == 1);
1518+
Field field = classFile.fields[0];
1519+
checkTypeAnno(
1520+
classFile,
1521+
(RuntimeVisibleTypeAnnotations_attribute) findAttributeOrFail(field.attributes, RuntimeVisibleTypeAnnotations_attribute.class),
1522+
"FIELD",
1523+
"Anno");
1524+
1525+
// checking for the annotation on the corresponding parameter of the canonical constructor
1526+
Method init = findMethodOrFail(classFile, "<init>");
1527+
checkTypeAnno(
1528+
classFile,
1529+
(RuntimeVisibleTypeAnnotations_attribute) findAttributeOrFail(init.attributes, RuntimeVisibleTypeAnnotations_attribute.class),
1530+
"METHOD_FORMAL_PARAMETER", "Anno");
1531+
1532+
// checking for the annotation in the accessor
1533+
Method accessor = findMethodOrFail(classFile, "s");
1534+
checkTypeAnno(
1535+
classFile,
1536+
(RuntimeVisibleTypeAnnotations_attribute) findAttributeOrFail(accessor.attributes, RuntimeVisibleTypeAnnotations_attribute.class),
1537+
"METHOD_RETURN", "Anno");
1538+
1539+
// checking for the annotation in the Record attribute
1540+
Record_attribute record = (Record_attribute) findAttributeOrFail(classFile.attributes, Record_attribute.class);
1541+
Assert.check(record.component_count == 1);
1542+
checkTypeAnno(
1543+
classFile,
1544+
(RuntimeVisibleTypeAnnotations_attribute) findAttributeOrFail(
1545+
record.component_info_arr[0].attributes,
1546+
RuntimeVisibleTypeAnnotations_attribute.class),
1547+
"FIELD", "Anno");
1548+
}
1549+
14991550
private void checkTypeAnno(ClassFile classFile,
15001551
RuntimeTypeAnnotations_attribute rtAnnos,
15011552
String positionType,

0 commit comments

Comments
 (0)