8208470: Type annotations on inner type that is an array component

Co-authored-by: Bernard Blaser <bsrbnd@openjdk.org>
Reviewed-by: jlahoda
This commit is contained in:
Vicente Romero 2023-02-16 04:01:12 +00:00
parent 573c316c57
commit 1480d418e3
4 changed files with 43 additions and 53 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/code
test/langtools/tools/javac
annotations/typeAnnotations/referenceinfos
warnings/6747671

@ -50,7 +50,6 @@ import com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntryKind;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Type.ModuleType;
import com.sun.tools.javac.code.TypeMetadata.Entry.Kind;
import com.sun.tools.javac.comp.Annotate;
import com.sun.tools.javac.comp.Attr;
import com.sun.tools.javac.comp.AttrContext;
@ -448,7 +447,7 @@ public class TypeAnnotations {
}
if (type.hasTag(TypeTag.ARRAY))
return rewriteArrayType((ArrayType)type, annotations, pos);
return rewriteArrayType(typetree, (ArrayType)type, annotations, onlyTypeAnnotations, pos);
if (type.hasTag(TypeTag.TYPEVAR)) {
return type.annotatedType(onlyTypeAnnotations);
@ -557,56 +556,24 @@ public class TypeAnnotations {
*
* SIDE EFFECT: Update position for the annotations to be {@code pos}.
*/
private Type rewriteArrayType(ArrayType type, List<TypeCompound> annotations, TypeAnnotationPosition pos) {
ArrayType tomodify = new ArrayType(type);
if (type.isVarargs()) {
tomodify = tomodify.makeVarargs();
}
ArrayType res = tomodify;
List<TypePathEntry> loc = List.nil();
// peel one and update loc
Type tmpType = type.elemtype;
loc = loc.prepend(TypePathEntry.ARRAY);
while (tmpType.hasTag(TypeTag.ARRAY)) {
ArrayType arr = (ArrayType)tmpType;
// Update last type with new element type
ArrayType tmp = new ArrayType(arr);
tomodify.elemtype = tmp;
tomodify = tmp;
tmpType = arr.elemtype;
loc = loc.prepend(TypePathEntry.ARRAY);
}
// Fix innermost element type
Type elemType;
if (tmpType.getMetadata() != null) {
List<TypeCompound> tcs;
if (tmpType.getAnnotationMirrors().isEmpty()) {
tcs = annotations;
} else {
// Special case, lets prepend
tcs = annotations.appendList(tmpType.getAnnotationMirrors());
}
elemType = tmpType.cloneWithMetadata(tmpType
.getMetadata()
.without(Kind.ANNOTATIONS)
.combine(new TypeMetadata.Annotations(tcs)));
} else {
elemType = tmpType.cloneWithMetadata(new TypeMetadata(new TypeMetadata.Annotations(annotations)));
}
tomodify.elemtype = elemType;
private Type rewriteArrayType(JCTree typetree, ArrayType type, List<TypeCompound> annotations,
List<Attribute.TypeCompound> onlyTypeAnnotations, TypeAnnotationPosition pos) {
ArrayType res = new ArrayType(type);
// Update positions
pos.location = loc;
pos.location = pos.location.append(TypePathEntry.ARRAY);
res.elemtype = typeWithAnnotations(arrayElemTypeTree(typetree), type.elemtype, annotations, onlyTypeAnnotations, pos);
return res;
}
private JCTree arrayElemTypeTree(JCTree typetree) {
if (typetree.getKind() == JCTree.Kind.ANNOTATED_TYPE) {
typetree = ((JCAnnotatedType) typetree).underlyingType;
}
return ((JCArrayTypeTree) typetree).elemtype;
}
/** Return a copy of the first type that only differs by
* inserting the annotations to the left-most/inner-most type
* or the type given by stopAt.

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2023, 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
@ -235,8 +235,10 @@ public class Driver {
|| compact.contains(" class"))
&& !compact.contains("interface")
&& !compact.contains("enum");
if (isSnippet)
if (isSnippet) {
sb.append("class %TEST_CLASS_NAME% {\n");
sb.append("class Nested {}\n");
}
sb.append(compact);
sb.append("\n");

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2023, 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
@ -21,16 +21,17 @@
* questions.
*/
import static com.sun.tools.classfile.TypeAnnotation.TargetType.*;
/*
* @test
* @bug 8042451
* @bug 8042451 8208470
* @summary Test population of reference info for field
* @modules jdk.jdeps/com.sun.tools.classfile
* @compile -g Driver.java ReferenceInfoUtil.java Fields.java
* @run main Driver Fields
*/
import static com.sun.tools.classfile.TypeAnnotation.TargetType.*;
public class Fields {
// field types
@TADescription(annotation = "TA", type = FIELD)
@ -63,6 +64,26 @@ public class Fields {
return "@TC String @TA [] @TB [] test;";
}
@TADescription(annotation = "TA", type = FIELD)
@TADescription(annotation = "TB", type = FIELD,
genericLocation = { 0, 0 })
@TADescription(annotation = "TC", type = FIELD,
genericLocation = { 0, 0, 0, 0, 1, 0})
public String fieldAsNestedArray1() {
return "@TC Nested @TA [] @TB [] test;";
}
@TADescription(annotation = "TA", type = FIELD)
@TADescription(annotation = "TB", type = FIELD,
genericLocation = { 0, 0 })
@TADescription(annotation = "TC", type = FIELD,
genericLocation = { 0, 0, 0, 0, 1, 0})
@TADescription(annotation = "TD", type = FIELD,
genericLocation = { 0, 0, 0, 0 })
public String fieldAsNestedArray2() {
return "@TD %TEST_CLASS_NAME%. @TC Nested @TA [] @TB [] test;";
}
@TADescription(annotation = "TA", type = FIELD)
@TADescription(annotation = "TB", type = FIELD,
genericLocation = { 0, 0 })

@ -7,6 +7,6 @@ T6747671.java:29:28: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
T6747671.java:32:9: compiler.warn.raw.class.use: T6747671.A, T6747671<E>.A<X>
T6747671.java:32:20: compiler.warn.raw.class.use: T6747671.A, T6747671<E>.A<X>
T6747671.java:33:16: compiler.warn.raw.class.use: T6747671.A.Z, T6747671<E>.A<X>.Z<Y>
T6747671.java:36:9: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
T6747671.java:36:9: compiler.warn.raw.class.use: @T6747671.TA T6747671.B, T6747671.B<X>
T6747671.java:36:27: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
11 warnings