diff -r b0fcf59be391 src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java Fri Jul 20 14:48:41 2018 -0700 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java Mon Jul 30 22:22:50 2018 +0200 @@ -419,12 +419,20 @@ final List onlyTypeAnnotations, final TypeAnnotationPosition pos) { + return typeWithAnnotations(typetree, type, annotations, onlyTypeAnnotations, pos, false); + } + private Type typeWithAnnotations(final JCTree typetree, final Type type, + final List annotations, + final List onlyTypeAnnotations, + final TypeAnnotationPosition pos, + boolean arrayElem) + { if (annotations.isEmpty()) { return type; } 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); @@ -523,8 +531,29 @@ p.location = p.location.appendList(depth.toList()); } - Type ret = typeWithAnnotations(type, enclTy, annotations); - typetree.type = ret; + Type ret; + + if (arrayElem) { + if (type.getMetadata() != null) { + List tcs; + if (type.getAnnotationMirrors().isEmpty()) { + tcs = annotations; + } else { + // Special case, lets prepend + tcs = annotations.appendList(type.getAnnotationMirrors()); + } + ret = type.cloneWithMetadata(type + .getMetadata() + .without(Kind.ANNOTATIONS) + .combine(new TypeMetadata.Annotations(tcs))); + } else { + ret = type.cloneWithMetadata(new TypeMetadata(new TypeMetadata.Annotations(annotations))); + } + } + else { + ret = typeWithAnnotations(type, enclTy, annotations); + typetree.type = ret; + } return ret; } } @@ -536,57 +565,34 @@ * * SIDE EFFECT: Update position for the annotations to be {@code pos}. */ - private Type rewriteArrayType(ArrayType type, List annotations, TypeAnnotationPosition pos) { - ArrayType tomodify = new ArrayType(type); - ArrayType res = tomodify; - - List 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; + private Type rewriteArrayType(JCTree typetree, ArrayType type, List annotations, + List onlyTypeAnnotations, TypeAnnotationPosition pos) { + ArrayType res = new ArrayType(type); - tmpType = arr.elemtype; - loc = loc.prepend(TypePathEntry.ARRAY); - } - - // Fix innermost element type - Type elemType; - if (tmpType.getMetadata() != null) { - List 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; - - // Update positions + // Update first position for (TypeCompound tc : annotations) { if (tc.position == null) tc.position = pos; - tc.position.location = loc; + tc.position.location = tc.position.location.append(TypePathEntry.ARRAY); + break; } + res.elemtype = typeWithAnnotations(arrayTypeTree(typetree).elemtype, type.elemtype, annotations, + onlyTypeAnnotations, pos, !type.elemtype.hasTag(TypeTag.ARRAY)); return res; } + private JCArrayTypeTree arrayTypeTree(JCTree typetree) { + if (typetree.getKind() == JCTree.Kind.ARRAY_TYPE) { + return (JCArrayTypeTree) typetree; + } else if (typetree.getKind() == JCTree.Kind.ANNOTATED_TYPE) { + return (JCArrayTypeTree) ((JCAnnotatedType) typetree).underlyingType; + } else { + Assert.error("Could not determine array type from type tree: " + typetree); + return null; + } + } + /** 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. @@ -1399,16 +1405,6 @@ scan(tree.elems); } - - private void findTypeCompoundPosition(JCTree tree, JCTree frame, List annotations) { - if (!annotations.isEmpty()) { - final TypeAnnotationPosition p = - resolveFrame(tree, frame, frames, currentLambda, 0, new ListBuffer<>()); - for (TypeCompound tc : annotations) - tc.position = p; - } - } - private void findPosition(JCTree tree, JCTree frame, List annotations) { if (!annotations.isEmpty()) { diff -r b0fcf59be391 test/langtools/tools/javac/annotations/typeAnnotations/referenceinfos/Driver.java --- a/test/langtools/tools/javac/annotations/typeAnnotations/referenceinfos/Driver.java Fri Jul 20 14:48:41 2018 -0700 +++ b/test/langtools/tools/javac/annotations/typeAnnotations/referenceinfos/Driver.java Mon Jul 30 22:22:50 2018 +0200 @@ -235,8 +235,10 @@ || 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"); diff -r b0fcf59be391 test/langtools/tools/javac/annotations/typeAnnotations/referenceinfos/Fields.java --- a/test/langtools/tools/javac/annotations/typeAnnotations/referenceinfos/Fields.java Fri Jul 20 14:48:41 2018 -0700 +++ b/test/langtools/tools/javac/annotations/typeAnnotations/referenceinfos/Fields.java Mon Jul 30 22:22:50 2018 +0200 @@ -67,6 +67,26 @@ @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 }) + @TADescription(annotation = "TC", type = FIELD, genericLocation = { 0, 0, 0, 0 }) public String fieldAsArrayOld() { return "@TC String test @TA [] @TB [];";