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 Tue Jul 31 11:30:55 2018 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2018, 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 @@ -50,7 +50,6 @@ 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; @@ -419,12 +418,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); @@ -524,7 +531,10 @@ } Type ret = typeWithAnnotations(type, enclTy, annotations); - typetree.type = ret; + + if (!arrayElem) + typetree.type = ret; + return ret; } } @@ -536,57 +546,35 @@ * * 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); + private Type rewriteArrayType(JCTree typetree, ArrayType type, List annotations, + List onlyTypeAnnotations, TypeAnnotationPosition pos) { + ArrayType res = new ArrayType(type); - 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); - } + // Update first position + TypeCompound tc = annotations.get(0); - // 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; + if (tc.position == null) + tc.position = pos; - // Update positions - for (TypeCompound tc : annotations) { - if (tc.position == null) - tc.position = pos; - tc.position.location = loc; - } + tc.position.location = tc.position.location.append(TypePathEntry.ARRAY); + + 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 +1387,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 Tue Jul 31 11:30:55 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 Tue Jul 31 11:30:55 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 [];";