Skip to content

Commit 4d9a1cd

Browse files
author
Srikanth Adayapalam
committed
8292159: TYPE_USE annotations on generic type arguments of record components discarded
Reviewed-by: vromero
1 parent 210fe49 commit 4d9a1cd

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;
@@ -84,6 +85,7 @@
8485
import com.sun.tools.javac.util.Log;
8586
import com.sun.tools.javac.util.Names;
8687

88+
import static com.sun.tools.javac.code.Flags.RECORD;
8789
import static com.sun.tools.javac.code.Kinds.Kind.*;
8890

8991
/**
@@ -1302,6 +1304,16 @@ public void visitVarDef(final JCVariableDecl tree) {
13021304
if (!sigOnly) {
13031305
scan(tree.init);
13041306
}
1307+
1308+
// Now that type and declaration annotations have been segregated into their own buckets ...
1309+
if (sigOnly) {
1310+
if (tree.sym != null && tree.sym.getKind() == ElementKind.FIELD && (tree.sym.flags_field & RECORD) != 0) {
1311+
RecordComponent rc = ((ClassSymbol)tree.sym.owner).getRecordComponent(tree.sym);
1312+
rc.setTypeAttributes(tree.sym.getRawTypeAttributes());
1313+
// to get all the type annotations applied to the type
1314+
rc.type = tree.sym.type;
1315+
}
1316+
}
13051317
}
13061318

13071319
@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
@@ -3005,9 +3005,15 @@ private void validateAnnotation(JCAnnotation a, JCTree declarationTree, Symbol s
30053005
rc.appendAttributes(s.getRawAttributes().stream().filter(anno ->
30063006
Arrays.stream(getTargetNames(anno.type.tsym)).anyMatch(name -> name == names.RECORD_COMPONENT)
30073007
).collect(List.collector()));
3008-
rc.setTypeAttributes(s.getRawTypeAttributes());
3009-
// to get all the type annotations applied to the type
3010-
rc.type = s.type;
3008+
3009+
/* At this point, we used to carry over any type annotations from the VARDEF to the record component, but
3010+
* that is problematic, since we get here only when *some* annotation is applied to the SE5 (declaration)
3011+
* annotation location, inadvertently failing to carry over the type annotations when the VarDef has no
3012+
* annotations in the SE5 annotation location.
3013+
*
3014+
* Now type annotations are assigned to record components in a method that would execute irrespective of
3015+
* whether there are SE5 annotations on a VarDef viz com.sun.tools.javac.code.TypeAnnotations.TypeAnnotationPositions.visitVarDef
3016+
*/
30113017
}
30123018
}
30133019
}

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,57 @@ record R(@Anno String s) {}
15661566
}
15671567
}
15681568

1569+
// JDK-8292159: TYPE_USE annotations on generic type arguments
1570+
// of record components discarded
1571+
public void testOnlyTypeAnnotationsOnComponentField() throws Exception {
1572+
String code =
1573+
"""
1574+
import java.lang.annotation.*;
1575+
import java.util.List;
1576+
@Target({ElementType.TYPE_USE})
1577+
@Retention(RetentionPolicy.RUNTIME)
1578+
@interface Anno { }
1579+
record R(List<@Anno String> s) {}
1580+
""";
1581+
1582+
File dir = assertOK(true, code);
1583+
1584+
ClassFile classFile = ClassFile.read(findClassFileOrFail(dir, "R.class"));
1585+
1586+
// field first
1587+
Assert.check(classFile.fields.length == 1);
1588+
Field field = classFile.fields[0];
1589+
checkTypeAnno(
1590+
classFile,
1591+
(RuntimeVisibleTypeAnnotations_attribute) findAttributeOrFail(field.attributes, RuntimeVisibleTypeAnnotations_attribute.class),
1592+
"FIELD",
1593+
"Anno");
1594+
1595+
// checking for the annotation on the corresponding parameter of the canonical constructor
1596+
Method init = findMethodOrFail(classFile, "<init>");
1597+
checkTypeAnno(
1598+
classFile,
1599+
(RuntimeVisibleTypeAnnotations_attribute) findAttributeOrFail(init.attributes, RuntimeVisibleTypeAnnotations_attribute.class),
1600+
"METHOD_FORMAL_PARAMETER", "Anno");
1601+
1602+
// checking for the annotation in the accessor
1603+
Method accessor = findMethodOrFail(classFile, "s");
1604+
checkTypeAnno(
1605+
classFile,
1606+
(RuntimeVisibleTypeAnnotations_attribute) findAttributeOrFail(accessor.attributes, RuntimeVisibleTypeAnnotations_attribute.class),
1607+
"METHOD_RETURN", "Anno");
1608+
1609+
// checking for the annotation in the Record attribute
1610+
Record_attribute record = (Record_attribute) findAttributeOrFail(classFile.attributes, Record_attribute.class);
1611+
Assert.check(record.component_count == 1);
1612+
checkTypeAnno(
1613+
classFile,
1614+
(RuntimeVisibleTypeAnnotations_attribute) findAttributeOrFail(
1615+
record.component_info_arr[0].attributes,
1616+
RuntimeVisibleTypeAnnotations_attribute.class),
1617+
"FIELD", "Anno");
1618+
}
1619+
15691620
private void checkTypeAnno(ClassFile classFile,
15701621
RuntimeTypeAnnotations_attribute rtAnnos,
15711622
String positionType,

0 commit comments

Comments
 (0)