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

Print this page

        

*** 661,911 **** /* This is the beginning of the second part of organizing * type annotations: determine the type annotation positions. */ ! private void resolveFrame(JCTree tree, JCTree frame, ! List<JCTree> path, TypeAnnotationPosition p) { /* System.out.println("Resolving tree: " + tree + " kind: " + tree.getKind()); System.out.println(" Framing tree: " + frame + " kind: " + frame.getKind()); */ // Note that p.offset is set in // com.sun.tools.javac.jvm.Gen.setTypeAnnotationPositions(int) switch (frame.getKind()) { case TYPE_CAST: ! JCTypeCast frameTC = (JCTypeCast) frame; ! p.type = TargetType.CAST; ! if (frameTC.clazz.hasTag(Tag.TYPEINTERSECTION)) { ! // This case was already handled by INTERSECTION_TYPE ! } else { ! p.type_index = 0; ! } ! p.pos = frame.pos; ! return; case INSTANCE_OF: ! p.type = TargetType.INSTANCEOF; ! p.pos = frame.pos; ! return; case NEW_CLASS: ! JCNewClass frameNewClass = (JCNewClass) frame; if (frameNewClass.def != null) { // Special handling for anonymous class instantiations ! JCClassDecl frameClassDecl = frameNewClass.def; if (frameClassDecl.extending == tree) { ! p.type = TargetType.CLASS_EXTENDS; ! p.type_index = -1; } else if (frameClassDecl.implementing.contains(tree)) { ! p.type = TargetType.CLASS_EXTENDS; ! p.type_index = frameClassDecl.implementing.indexOf(tree); } else { // In contrast to CLASS below, typarams cannot occur here. ! Assert.error("Could not determine position of tree " + tree + " within frame " + frame); } } else if (frameNewClass.typeargs.contains(tree)) { ! p.type = TargetType.CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT; ! p.type_index = frameNewClass.typeargs.indexOf(tree); ! } else { ! p.type = TargetType.NEW; } - p.pos = frame.pos; - return; case NEW_ARRAY: ! p.type = TargetType.NEW; ! p.pos = frame.pos; ! return; case ANNOTATION_TYPE: case CLASS: case ENUM: case INTERFACE: - p.pos = frame.pos; if (((JCClassDecl)frame).extending == tree) { ! p.type = TargetType.CLASS_EXTENDS; ! p.type_index = -1; } else if (((JCClassDecl)frame).implementing.contains(tree)) { ! p.type = TargetType.CLASS_EXTENDS; ! p.type_index = ((JCClassDecl)frame).implementing.indexOf(tree); } else if (((JCClassDecl)frame).typarams.contains(tree)) { ! p.type = TargetType.CLASS_TYPE_PARAMETER; ! p.parameter_index = ((JCClassDecl)frame).typarams.indexOf(tree); } else { ! Assert.error("Could not determine position of tree " + tree + ! " within frame " + frame); } - return; case METHOD: { ! JCMethodDecl frameMethod = (JCMethodDecl) frame; ! p.pos = frame.pos; if (frameMethod.thrown.contains(tree)) { ! p.type = TargetType.THROWS; ! p.type_index = frameMethod.thrown.indexOf(tree); } else if (frameMethod.restype == tree) { ! p.type = TargetType.METHOD_RETURN; } else if (frameMethod.typarams.contains(tree)) { ! p.type = TargetType.METHOD_TYPE_PARAMETER; ! p.parameter_index = frameMethod.typarams.indexOf(tree); } else { ! Assert.error("Could not determine position of tree " + tree + " within frame " + frame); } - return; } case PARAMETERIZED_TYPE: { List<JCTree> newPath = path.tail; if (((JCTypeApply)frame).clazz == tree) { // generic: RAW; noop } else if (((JCTypeApply)frame).arguments.contains(tree)) { JCTypeApply taframe = (JCTypeApply) frame; int arg = taframe.arguments.indexOf(tree); ! p.location = p.location.prepend(new TypePathEntry(TypePathEntryKind.TYPE_ARGUMENT, arg)); Type typeToUse; ! if (newPath.tail != null && newPath.tail.head.hasTag(Tag.NEWCLASS)) { ! // If we are within an anonymous class instantiation, use its type, ! // because it contains a correctly nested type. typeToUse = newPath.tail.head.type; } else { typeToUse = taframe.type; } ! locateNestedTypes(typeToUse, p); } else { ! Assert.error("Could not determine type argument position of tree " + tree + " within frame " + frame); } ! resolveFrame(newPath.head, newPath.tail.head, newPath, p); ! return; } case MEMBER_REFERENCE: { JCMemberReference mrframe = (JCMemberReference) frame; if (mrframe.expr == tree) { switch (mrframe.mode) { case INVOKE: ! p.type = TargetType.METHOD_REFERENCE; ! break; case NEW: ! p.type = TargetType.CONSTRUCTOR_REFERENCE; ! break; default: ! Assert.error("Unknown method reference mode " + mrframe.mode + " for tree " + tree + " within frame " + frame); } - p.pos = frame.pos; } else if (mrframe.typeargs != null && mrframe.typeargs.contains(tree)) { ! int arg = mrframe.typeargs.indexOf(tree); ! p.type_index = arg; switch (mrframe.mode) { case INVOKE: ! p.type = TargetType.METHOD_REFERENCE_TYPE_ARGUMENT; ! break; case NEW: ! p.type = TargetType.CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT; ! break; default: ! Assert.error("Unknown method reference mode " + mrframe.mode + " for tree " + tree + " within frame " + frame); } - p.pos = frame.pos; } else { ! Assert.error("Could not determine type argument position of tree " + tree + " within frame " + frame); } - return; } case ARRAY_TYPE: { ! ListBuffer<TypePathEntry> index = new ListBuffer<>(); ! index = index.append(TypePathEntry.ARRAY); List<JCTree> newPath = path.tail; while (true) { JCTree npHead = newPath.tail.head; if (npHead.hasTag(JCTree.Tag.TYPEARRAY)) { newPath = newPath.tail; ! index = index.append(TypePathEntry.ARRAY); } else if (npHead.hasTag(JCTree.Tag.ANNOTATED_TYPE)) { newPath = newPath.tail; } else { break; } } ! p.location = p.location.prependList(index.toList()); ! resolveFrame(newPath.head, newPath.tail.head, newPath, p); ! return; } case TYPE_PARAMETER: if (path.tail.tail.head.hasTag(JCTree.Tag.CLASSDEF)) { ! JCClassDecl clazz = (JCClassDecl)path.tail.tail.head; ! p.type = TargetType.CLASS_TYPE_PARAMETER_BOUND; ! p.parameter_index = clazz.typarams.indexOf(path.tail.head); ! p.bound_index = ((JCTypeParameter)frame).bounds.indexOf(tree); ! if (((JCTypeParameter)frame).bounds.get(0).type.isInterface()) { ! // Account for an implicit Object as bound 0 ! p.bound_index += 1; ! } } else if (path.tail.tail.head.hasTag(JCTree.Tag.METHODDEF)) { ! JCMethodDecl method = (JCMethodDecl)path.tail.tail.head; ! p.type = TargetType.METHOD_TYPE_PARAMETER_BOUND; ! p.parameter_index = method.typarams.indexOf(path.tail.head); ! p.bound_index = ((JCTypeParameter)frame).bounds.indexOf(tree); ! if (((JCTypeParameter)frame).bounds.get(0).type.isInterface()) { ! // Account for an implicit Object as bound 0 ! p.bound_index += 1; ! } } else { ! Assert.error("Could not determine position of tree " + tree + " within frame " + frame); } - p.pos = frame.pos; - return; case VARIABLE: VarSymbol v = ((JCVariableDecl)frame).sym; ! p.pos = frame.pos; switch (v.getKind()) { case LOCAL_VARIABLE: ! p.type = TargetType.LOCAL_VARIABLE; ! break; case FIELD: ! p.type = TargetType.FIELD; ! break; case PARAMETER: if (v.getQualifiedName().equals(names._this)) { ! // TODO: Intro a separate ElementKind? ! p.type = TargetType.METHOD_RECEIVER; ! } else { ! p.type = TargetType.METHOD_FORMAL_PARAMETER; ! p.parameter_index = methodParamIndex(path, frame); } - break; case EXCEPTION_PARAMETER: ! p.type = TargetType.EXCEPTION_PARAMETER; ! break; case RESOURCE_VARIABLE: ! p.type = TargetType.RESOURCE_VARIABLE; ! break; default: ! Assert.error("Found unexpected type annotation for variable: " + v + " with kind: " + v.getKind()); ! } ! if (v.getKind() != ElementKind.FIELD) { ! v.owner.appendUniqueTypeAttributes(v.getRawTypeAttributes()); } - return; case ANNOTATED_TYPE: { if (frame == tree) { // This is only true for the first annotated type we see. // For any other annotated types along the path, we do --- 661,962 ---- /* This is the beginning of the second part of organizing * type annotations: determine the type annotation positions. */ ! // This method is considered deprecated, and will be removed ! // in the near future. Don't use it for anything new. ! private TypeAnnotationPosition ! resolveFrame(JCTree tree, ! JCTree frame, ! List<JCTree> path, ! JCLambda currentLambda, ! int outer_type_index, ! ListBuffer<TypePathEntry> location) { /* System.out.println("Resolving tree: " + tree + " kind: " + tree.getKind()); System.out.println(" Framing tree: " + frame + " kind: " + frame.getKind()); */ // Note that p.offset is set in // com.sun.tools.javac.jvm.Gen.setTypeAnnotationPositions(int) switch (frame.getKind()) { case TYPE_CAST: ! final JCTypeCast frameTC = (JCTypeCast) frame; ! return TypeAnnotationPosition.typeCast(location.toList(), ! currentLambda, ! outer_type_index, ! frame.pos); case INSTANCE_OF: ! return TypeAnnotationPosition.instanceOf(location.toList(), ! currentLambda, ! frame.pos); case NEW_CLASS: ! final JCNewClass frameNewClass = (JCNewClass) frame; if (frameNewClass.def != null) { // Special handling for anonymous class instantiations ! final JCClassDecl frameClassDecl = frameNewClass.def; if (frameClassDecl.extending == tree) { ! return TypeAnnotationPosition ! .classExtends(location.toList(), currentLambda, ! frame.pos); } else if (frameClassDecl.implementing.contains(tree)) { ! final int type_index = ! frameClassDecl.implementing.indexOf(tree); ! return TypeAnnotationPosition ! .classExtends(location.toList(), currentLambda, ! type_index, frame.pos); } else { // In contrast to CLASS below, typarams cannot occur here. ! throw new AssertionError("Could not determine position of tree " + tree + " within frame " + frame); } } else if (frameNewClass.typeargs.contains(tree)) { ! final int type_index = ! frameNewClass.typeargs.indexOf(tree); ! return TypeAnnotationPosition ! .constructorInvocationTypeArg(location.toList(), ! currentLambda, ! type_index, ! frame.pos); ! } else { ! return TypeAnnotationPosition ! .newObj(location.toList(), currentLambda, ! frame.pos); } case NEW_ARRAY: ! return TypeAnnotationPosition ! .newObj(location.toList(), currentLambda, frame.pos); case ANNOTATION_TYPE: case CLASS: case ENUM: case INTERFACE: if (((JCClassDecl)frame).extending == tree) { ! return TypeAnnotationPosition ! .classExtends(location.toList(), currentLambda, ! frame.pos); } else if (((JCClassDecl)frame).implementing.contains(tree)) { ! final int type_index = ! ((JCClassDecl)frame).implementing.indexOf(tree); ! return TypeAnnotationPosition ! .classExtends(location.toList(), currentLambda, ! type_index, frame.pos); } else if (((JCClassDecl)frame).typarams.contains(tree)) { ! ! final int parameter_index = ! ((JCClassDecl)frame).typarams.indexOf(tree); ! return TypeAnnotationPosition ! .typeParameter(location.toList(), currentLambda, ! parameter_index, frame.pos); } else { ! throw new AssertionError("Could not determine position of tree " + ! tree + " within frame " + frame); } case METHOD: { ! final JCMethodDecl frameMethod = (JCMethodDecl) frame; if (frameMethod.thrown.contains(tree)) { ! final int type_index = frameMethod.thrown.indexOf(tree); ! return TypeAnnotationPosition ! .methodThrows(location.toList(), currentLambda, ! type_index, frame.pos); } else if (frameMethod.restype == tree) { ! return TypeAnnotationPosition ! .methodReturn(location.toList(), currentLambda, ! frame.pos); } else if (frameMethod.typarams.contains(tree)) { ! final int parameter_index = ! frameMethod.typarams.indexOf(tree); ! return TypeAnnotationPosition ! .methodTypeParameter(location.toList(), ! currentLambda, ! parameter_index, frame.pos); } else { ! throw new AssertionError("Could not determine position of tree " + tree + " within frame " + frame); } } case PARAMETERIZED_TYPE: { List<JCTree> newPath = path.tail; if (((JCTypeApply)frame).clazz == tree) { // generic: RAW; noop } else if (((JCTypeApply)frame).arguments.contains(tree)) { JCTypeApply taframe = (JCTypeApply) frame; int arg = taframe.arguments.indexOf(tree); ! location = location.prepend( ! new TypePathEntry(TypePathEntryKind.TYPE_ARGUMENT, ! arg)); Type typeToUse; ! if (newPath.tail != null && ! newPath.tail.head.hasTag(Tag.NEWCLASS)) { ! // If we are within an anonymous class ! // instantiation, use its type, because it ! // contains a correctly nested type. typeToUse = newPath.tail.head.type; } else { typeToUse = taframe.type; } ! location = locateNestedTypes(typeToUse, location); } else { ! throw new AssertionError("Could not determine type argument position of tree " + tree + " within frame " + frame); } ! return resolveFrame(newPath.head, newPath.tail.head, ! newPath, currentLambda, ! outer_type_index, location); } case MEMBER_REFERENCE: { JCMemberReference mrframe = (JCMemberReference) frame; if (mrframe.expr == tree) { switch (mrframe.mode) { case INVOKE: ! return TypeAnnotationPosition ! .methodRef(location.toList(), currentLambda, ! frame.pos); case NEW: ! return TypeAnnotationPosition ! .constructorRef(location.toList(), ! currentLambda, ! frame.pos); default: ! throw new AssertionError("Unknown method reference mode " + mrframe.mode + " for tree " + tree + " within frame " + frame); } } else if (mrframe.typeargs != null && mrframe.typeargs.contains(tree)) { ! final int type_index = mrframe.typeargs.indexOf(tree); switch (mrframe.mode) { case INVOKE: ! return TypeAnnotationPosition ! .methodRefTypeArg(location.toList(), ! currentLambda, ! type_index, frame.pos); case NEW: ! return TypeAnnotationPosition ! .constructorRefTypeArg(location.toList(), ! currentLambda, ! type_index, frame.pos); default: ! throw new AssertionError("Unknown method reference mode " + mrframe.mode + " for tree " + tree + " within frame " + frame); } } else { ! throw new AssertionError("Could not determine type argument position of tree " + tree + " within frame " + frame); } } case ARRAY_TYPE: { ! location = location.prepend(TypePathEntry.ARRAY); List<JCTree> newPath = path.tail; while (true) { JCTree npHead = newPath.tail.head; if (npHead.hasTag(JCTree.Tag.TYPEARRAY)) { newPath = newPath.tail; ! location = location.prepend(TypePathEntry.ARRAY); } else if (npHead.hasTag(JCTree.Tag.ANNOTATED_TYPE)) { newPath = newPath.tail; } else { break; } } ! return resolveFrame(newPath.head, newPath.tail.head, ! newPath, currentLambda, ! outer_type_index, location); } case TYPE_PARAMETER: if (path.tail.tail.head.hasTag(JCTree.Tag.CLASSDEF)) { ! final JCClassDecl clazz = ! (JCClassDecl)path.tail.tail.head; ! final int parameter_index = ! clazz.typarams.indexOf(path.tail.head); ! final int bound_index = ! ((JCTypeParameter)frame).bounds.get(0) ! .type.isInterface() ? ! ((JCTypeParameter)frame).bounds.indexOf(tree) + 1: ! ((JCTypeParameter)frame).bounds.indexOf(tree); ! return TypeAnnotationPosition ! .typeParameterBound(location.toList(), ! currentLambda, ! parameter_index, bound_index, ! frame.pos); } else if (path.tail.tail.head.hasTag(JCTree.Tag.METHODDEF)) { ! final JCMethodDecl method = ! (JCMethodDecl)path.tail.tail.head; ! final int parameter_index = ! method.typarams.indexOf(path.tail.head); ! final int bound_index = ! ((JCTypeParameter)frame).bounds.get(0) ! .type.isInterface() ? ! ((JCTypeParameter)frame).bounds.indexOf(tree) + 1: ! ((JCTypeParameter)frame).bounds.indexOf(tree); ! return TypeAnnotationPosition ! .methodTypeParameterBound(location.toList(), ! currentLambda, ! parameter_index, ! bound_index, ! frame.pos); } else { ! throw new AssertionError("Could not determine position of tree " + tree + " within frame " + frame); } case VARIABLE: VarSymbol v = ((JCVariableDecl)frame).sym; ! if (v.getKind() != ElementKind.FIELD) { ! v.owner.appendUniqueTypeAttributes(v.getRawTypeAttributes()); ! } switch (v.getKind()) { case LOCAL_VARIABLE: ! return TypeAnnotationPosition ! .localVariable(location.toList(), currentLambda, ! frame.pos); case FIELD: ! return TypeAnnotationPosition.field(location.toList(), ! currentLambda, ! frame.pos); case PARAMETER: if (v.getQualifiedName().equals(names._this)) { ! return TypeAnnotationPosition ! .methodReceiver(location.toList(), ! currentLambda, ! frame.pos); ! } else { ! final int parameter_index = ! methodParamIndex(path, frame); ! return TypeAnnotationPosition ! .methodParameter(location.toList(), ! currentLambda, ! parameter_index, ! frame.pos); } case EXCEPTION_PARAMETER: ! return TypeAnnotationPosition ! .exceptionParameter(location.toList(), ! currentLambda, ! frame.pos); case RESOURCE_VARIABLE: ! return TypeAnnotationPosition ! .resourceVariable(location.toList(), ! currentLambda, ! frame.pos); default: ! throw new AssertionError("Found unexpected type annotation for variable: " + v + " with kind: " + v.getKind()); } case ANNOTATED_TYPE: { if (frame == tree) { // This is only true for the first annotated type we see. // For any other annotated types along the path, we do
*** 919,1005 **** utype.getKind().equals(TypeKind.ARRAY)) { // Type parameters, wildcards, and arrays have the declaring // class/method as enclosing elements. // There is actually nothing to do for them. } else { ! locateNestedTypes(utype, p); } } List<JCTree> newPath = path.tail; ! resolveFrame(newPath.head, newPath.tail.head, newPath, p); ! return; } case UNION_TYPE: { List<JCTree> newPath = path.tail; ! resolveFrame(newPath.head, newPath.tail.head, newPath, p); ! return; } case INTERSECTION_TYPE: { JCTypeIntersection isect = (JCTypeIntersection)frame; ! p.type_index = isect.bounds.indexOf(tree); ! List<JCTree> newPath = path.tail; ! resolveFrame(newPath.head, newPath.tail.head, newPath, p); ! return; } case METHOD_INVOCATION: { JCMethodInvocation invocation = (JCMethodInvocation)frame; if (!invocation.typeargs.contains(tree)) { ! Assert.error("{" + tree + "} is not an argument in the invocation: " + invocation); } MethodSymbol exsym = (MethodSymbol) TreeInfo.symbol(invocation.getMethodSelect()); if (exsym == null) { ! Assert.error("could not determine symbol for {" + invocation + "}"); } else if (exsym.isConstructor()) { ! p.type = TargetType.CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT; ! } else { ! p.type = TargetType.METHOD_INVOCATION_TYPE_ARGUMENT; } - p.pos = invocation.pos; - p.type_index = invocation.typeargs.indexOf(tree); - return; } case EXTENDS_WILDCARD: case SUPER_WILDCARD: { // Annotations in wildcard bounds ! p.location = p.location.prepend(TypePathEntry.WILDCARD); ! List<JCTree> newPath = path.tail; ! resolveFrame(newPath.head, newPath.tail.head, newPath, p); ! return; } case MEMBER_SELECT: { ! List<JCTree> newPath = path.tail; ! resolveFrame(newPath.head, newPath.tail.head, newPath, p); ! return; } default: ! Assert.error("Unresolved frame: " + frame + " of kind: " + frame.getKind() + "\n Looking for tree: " + tree); - return; } } ! private void locateNestedTypes(Type type, TypeAnnotationPosition p) { ! // The number of "steps" to get from the full type to the ! // left-most outer type. ! ListBuffer<TypePathEntry> depth = new ListBuffer<>(); ! Type encl = type.getEnclosingType(); while (encl != null && encl.getKind() != TypeKind.NONE && encl.getKind() != TypeKind.ERROR) { ! depth = depth.append(TypePathEntry.INNER_TYPE); encl = encl.getEnclosingType(); } ! if (depth.nonEmpty()) { ! p.location = p.location.prependList(depth.toList()); ! } } private int methodParamIndex(List<JCTree> path, JCTree param) { List<JCTree> curr = path; while (curr.head.getTag() != Tag.METHODDEF && --- 970,1062 ---- utype.getKind().equals(TypeKind.ARRAY)) { // Type parameters, wildcards, and arrays have the declaring // class/method as enclosing elements. // There is actually nothing to do for them. } else { ! location = locateNestedTypes(utype, location); } } List<JCTree> newPath = path.tail; ! return resolveFrame(newPath.head, newPath.tail.head, ! newPath, currentLambda, ! outer_type_index, location); } case UNION_TYPE: { List<JCTree> newPath = path.tail; ! return resolveFrame(newPath.head, newPath.tail.head, ! newPath, currentLambda, ! outer_type_index, location); } case INTERSECTION_TYPE: { JCTypeIntersection isect = (JCTypeIntersection)frame; ! final List<JCTree> newPath = path.tail; ! return resolveFrame(newPath.head, newPath.tail.head, ! newPath, currentLambda, ! isect.bounds.indexOf(tree), location); } case METHOD_INVOCATION: { JCMethodInvocation invocation = (JCMethodInvocation)frame; if (!invocation.typeargs.contains(tree)) { ! throw new AssertionError("{" + tree + "} is not an argument in the invocation: " + invocation); } MethodSymbol exsym = (MethodSymbol) TreeInfo.symbol(invocation.getMethodSelect()); + final int type_index = invocation.typeargs.indexOf(tree); if (exsym == null) { ! throw new AssertionError("could not determine symbol for {" + invocation + "}"); } else if (exsym.isConstructor()) { ! return TypeAnnotationPosition ! .constructorInvocationTypeArg(location.toList(), ! currentLambda, ! type_index, ! invocation.pos); ! } else { ! return TypeAnnotationPosition ! .methodInvocationTypeArg(location.toList(), ! currentLambda, ! type_index, ! invocation.pos); } } case EXTENDS_WILDCARD: case SUPER_WILDCARD: { // Annotations in wildcard bounds ! final List<JCTree> newPath = path.tail; ! return resolveFrame(newPath.head, newPath.tail.head, ! newPath, currentLambda, ! outer_type_index, ! location.prepend(TypePathEntry.WILDCARD)); } case MEMBER_SELECT: { ! final List<JCTree> newPath = path.tail; ! return resolveFrame(newPath.head, newPath.tail.head, ! newPath, currentLambda, ! outer_type_index, location); } default: ! throw new AssertionError("Unresolved frame: " + frame + ! " of kind: " + frame.getKind() + "\n Looking for tree: " + tree); } } ! private ListBuffer<TypePathEntry> ! locateNestedTypes(Type type, ! ListBuffer<TypePathEntry> depth) { Type encl = type.getEnclosingType(); while (encl != null && encl.getKind() != TypeKind.NONE && encl.getKind() != TypeKind.ERROR) { ! depth = depth.prepend(TypePathEntry.INNER_TYPE); encl = encl.getEnclosingType(); } ! return depth; } private int methodParamIndex(List<JCTree> path, JCTree param) { List<JCTree> curr = path; while (curr.head.getTag() != Tag.METHODDEF &&
*** 1046,1090 **** if (tree.sym == null) { Assert.error("Visiting tree node before memberEnter"); } if (sigOnly) { if (!tree.mods.annotations.isEmpty()) { - // Nothing to do for separateAnnotationsKinds if - // there are no annotations of either kind. - TypeAnnotationPosition pos = new TypeAnnotationPosition(); - pos.type = TargetType.METHOD_RETURN; if (tree.sym.isConstructor()) { ! pos.pos = tree.pos; ! // Use null to mark that the annotations go with the symbol. separateAnnotationsKinds(tree, null, tree.sym, pos); } else { ! pos.pos = tree.restype.pos; ! separateAnnotationsKinds(tree.restype, tree.sym.type.getReturnType(), tree.sym, pos); } } if (tree.recvparam != null && tree.recvparam.sym != null && !tree.recvparam.mods.annotations.isEmpty()) { // Nothing to do for separateAnnotationsKinds if // there are no annotations of either kind. // TODO: make sure there are no declaration annotations. ! TypeAnnotationPosition pos = new TypeAnnotationPosition(); ! pos.type = TargetType.METHOD_RECEIVER; ! pos.pos = tree.recvparam.vartype.pos; ! separateAnnotationsKinds(tree.recvparam.vartype, tree.recvparam.sym.type, tree.recvparam.sym, pos); } int i = 0; for (JCVariableDecl param : tree.params) { if (!param.mods.annotations.isEmpty()) { // Nothing to do for separateAnnotationsKinds if // there are no annotations of either kind. ! TypeAnnotationPosition pos = new TypeAnnotationPosition(); ! pos.type = TargetType.METHOD_FORMAL_PARAMETER; ! pos.parameter_index = i; ! pos.pos = param.vartype.pos; ! separateAnnotationsKinds(param.vartype, param.sym.type, param.sym, pos); } ++i; } } --- 1103,1147 ---- if (tree.sym == null) { Assert.error("Visiting tree node before memberEnter"); } if (sigOnly) { if (!tree.mods.annotations.isEmpty()) { if (tree.sym.isConstructor()) { ! final TypeAnnotationPosition pos = ! TypeAnnotationPosition.methodReturn(tree.pos); ! // Use null to mark that the annotations go ! // with the symbol. separateAnnotationsKinds(tree, null, tree.sym, pos); } else { ! final TypeAnnotationPosition pos = ! TypeAnnotationPosition.methodReturn(tree.restype.pos); ! separateAnnotationsKinds(tree.restype, ! tree.sym.type.getReturnType(), tree.sym, pos); } } if (tree.recvparam != null && tree.recvparam.sym != null && !tree.recvparam.mods.annotations.isEmpty()) { // Nothing to do for separateAnnotationsKinds if // there are no annotations of either kind. // TODO: make sure there are no declaration annotations. ! final TypeAnnotationPosition pos = ! TypeAnnotationPosition.methodReceiver(tree.recvparam.vartype.pos); ! separateAnnotationsKinds(tree.recvparam.vartype, ! tree.recvparam.sym.type, tree.recvparam.sym, pos); } int i = 0; for (JCVariableDecl param : tree.params) { if (!param.mods.annotations.isEmpty()) { // Nothing to do for separateAnnotationsKinds if // there are no annotations of either kind. ! final TypeAnnotationPosition pos = ! TypeAnnotationPosition.methodParameter(i, param.vartype.pos); ! separateAnnotationsKinds(param.vartype, ! param.sym.type, ! param.sym, pos); } ++i; } }
*** 1117,1131 **** int i = 0; for (JCVariableDecl param : tree.params) { if (!param.mods.annotations.isEmpty()) { // Nothing to do for separateAnnotationsKinds if // there are no annotations of either kind. ! TypeAnnotationPosition pos = new TypeAnnotationPosition(); ! pos.type = TargetType.METHOD_FORMAL_PARAMETER; ! pos.parameter_index = i; ! pos.pos = param.vartype.pos; ! pos.onLambda = tree; separateAnnotationsKinds(param.vartype, param.sym.type, param.sym, pos); } ++i; } --- 1174,1186 ---- int i = 0; for (JCVariableDecl param : tree.params) { if (!param.mods.annotations.isEmpty()) { // Nothing to do for separateAnnotationsKinds if // there are no annotations of either kind. ! final TypeAnnotationPosition pos = ! TypeAnnotationPosition.methodParameter(tree, i, ! param.vartype.pos); separateAnnotationsKinds(param.vartype, param.sym.type, param.sym, pos); } ++i; }
*** 1151,1182 **** Assert.error("Visiting tree node before memberEnter"); } else if (tree.sym.getKind() == ElementKind.PARAMETER) { // Parameters are handled in visitMethodDef or visitLambda. } else if (tree.sym.getKind() == ElementKind.FIELD) { if (sigOnly) { ! TypeAnnotationPosition pos = new TypeAnnotationPosition(); ! pos.type = TargetType.FIELD; ! pos.pos = tree.pos; separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); } } else if (tree.sym.getKind() == ElementKind.LOCAL_VARIABLE) { ! TypeAnnotationPosition pos = new TypeAnnotationPosition(); ! pos.type = TargetType.LOCAL_VARIABLE; ! pos.pos = tree.pos; ! pos.onLambda = currentLambda; separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); } else if (tree.sym.getKind() == ElementKind.EXCEPTION_PARAMETER) { ! TypeAnnotationPosition pos = new TypeAnnotationPosition(); ! pos.type = TargetType.EXCEPTION_PARAMETER; ! pos.pos = tree.pos; ! pos.onLambda = currentLambda; separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); } else if (tree.sym.getKind() == ElementKind.RESOURCE_VARIABLE) { ! TypeAnnotationPosition pos = new TypeAnnotationPosition(); ! pos.type = TargetType.RESOURCE_VARIABLE; ! pos.pos = tree.pos; ! pos.onLambda = currentLambda; separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); } else if (tree.sym.getKind() == ElementKind.ENUM_CONSTANT) { // No type annotations can occur here. } else { // There is nothing else in a variable declaration that needs separation. --- 1206,1233 ---- Assert.error("Visiting tree node before memberEnter"); } else if (tree.sym.getKind() == ElementKind.PARAMETER) { // Parameters are handled in visitMethodDef or visitLambda. } else if (tree.sym.getKind() == ElementKind.FIELD) { if (sigOnly) { ! TypeAnnotationPosition pos = ! TypeAnnotationPosition.field(tree.pos); separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); } } else if (tree.sym.getKind() == ElementKind.LOCAL_VARIABLE) { ! final TypeAnnotationPosition pos = ! TypeAnnotationPosition.localVariable(currentLambda, ! tree.pos); separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); } else if (tree.sym.getKind() == ElementKind.EXCEPTION_PARAMETER) { ! final TypeAnnotationPosition pos = ! TypeAnnotationPosition.exceptionParameter(currentLambda, ! tree.pos); separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); } else if (tree.sym.getKind() == ElementKind.RESOURCE_VARIABLE) { ! final TypeAnnotationPosition pos = ! TypeAnnotationPosition.resourceVariable(currentLambda, ! tree.pos); separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); } else if (tree.sym.getKind() == ElementKind.ENUM_CONSTANT) { // No type annotations can occur here. } else { // There is nothing else in a variable declaration that needs separation.
*** 1216,1253 **** super.visitTypeParameter(tree); } private void copyNewClassAnnotationsToOwner(JCNewClass tree) { Symbol sym = tree.def.sym; ! TypeAnnotationPosition pos = new TypeAnnotationPosition(); ListBuffer<Attribute.TypeCompound> newattrs = new ListBuffer<>(); for (Attribute.TypeCompound old : sym.getRawTypeAttributes()) { newattrs.append(new Attribute.TypeCompound(old.type, old.values, pos)); } - pos.type = TargetType.NEW; - pos.pos = tree.pos; sym.owner.appendUniqueTypeAttributes(newattrs.toList()); } @Override public void visitNewClass(JCNewClass tree) { if (tree.def != null && !tree.def.mods.annotations.isEmpty()) { JCClassDecl classdecl = tree.def; ! TypeAnnotationPosition pos = new TypeAnnotationPosition(); ! pos.type = TargetType.CLASS_EXTENDS; ! pos.pos = tree.pos; if (classdecl.extending == tree.clazz) { ! pos.type_index = -1; } else if (classdecl.implementing.contains(tree.clazz)) { ! pos.type_index = classdecl.implementing.indexOf(tree.clazz); } else { // In contrast to CLASS elsewhere, typarams cannot occur here. ! Assert.error("Could not determine position of tree " + tree); } Type before = classdecl.sym.type; separateAnnotationsKinds(classdecl, tree.clazz.type, classdecl.sym, pos); copyNewClassAnnotationsToOwner(tree); // classdecl.sym.type now contains an annotated type, which --- 1267,1303 ---- super.visitTypeParameter(tree); } private void copyNewClassAnnotationsToOwner(JCNewClass tree) { Symbol sym = tree.def.sym; ! final TypeAnnotationPosition pos = ! TypeAnnotationPosition.newObj(tree.pos); ListBuffer<Attribute.TypeCompound> newattrs = new ListBuffer<>(); for (Attribute.TypeCompound old : sym.getRawTypeAttributes()) { newattrs.append(new Attribute.TypeCompound(old.type, old.values, pos)); } sym.owner.appendUniqueTypeAttributes(newattrs.toList()); } @Override public void visitNewClass(JCNewClass tree) { if (tree.def != null && !tree.def.mods.annotations.isEmpty()) { JCClassDecl classdecl = tree.def; ! TypeAnnotationPosition pos; ! if (classdecl.extending == tree.clazz) { ! pos = TypeAnnotationPosition.classExtends(tree.pos); } else if (classdecl.implementing.contains(tree.clazz)) { ! final int index = classdecl.implementing.indexOf(tree.clazz); ! pos = TypeAnnotationPosition.classExtends(index, tree.pos); } else { // In contrast to CLASS elsewhere, typarams cannot occur here. ! throw new AssertionError("Could not determine position of tree " + tree); } Type before = classdecl.sym.type; separateAnnotationsKinds(classdecl, tree.clazz.type, classdecl.sym, pos); copyNewClassAnnotationsToOwner(tree); // classdecl.sym.type now contains an annotated type, which
*** 1271,1288 **** int dimAnnosCount = tree.dimAnnotations.size(); ListBuffer<TypePathEntry> depth = new ListBuffer<>(); // handle annotations associated with dimensions for (int i = 0; i < dimAnnosCount; ++i) { ! TypeAnnotationPosition p = new TypeAnnotationPosition(); ! p.pos = tree.pos; ! p.onLambda = currentLambda; ! p.type = TargetType.NEW; if (i != 0) { depth = depth.append(TypePathEntry.ARRAY); ! p.location = p.location.appendList(depth.toList()); } setTypeAnnotationPos(tree.dimAnnotations.get(i), p); } // handle "free" annotations --- 1321,1340 ---- int dimAnnosCount = tree.dimAnnotations.size(); ListBuffer<TypePathEntry> depth = new ListBuffer<>(); // handle annotations associated with dimensions for (int i = 0; i < dimAnnosCount; ++i) { ! ListBuffer<TypePathEntry> location = ! new ListBuffer<TypePathEntry>(); if (i != 0) { depth = depth.append(TypePathEntry.ARRAY); ! location = location.appendList(depth.toList()); } + final TypeAnnotationPosition p = + TypeAnnotationPosition.newObj(location.toList(), + currentLambda, + tree.pos); setTypeAnnotationPos(tree.dimAnnotations.get(i), p); } // handle "free" annotations
*** 1291,1306 **** JCExpression elemType = tree.elemtype; depth = depth.append(TypePathEntry.ARRAY); while (elemType != null) { if (elemType.hasTag(JCTree.Tag.ANNOTATED_TYPE)) { JCAnnotatedType at = (JCAnnotatedType)elemType; ! TypeAnnotationPosition p = new TypeAnnotationPosition(); ! p.type = TargetType.NEW; ! p.pos = tree.pos; ! p.onLambda = currentLambda; ! locateNestedTypes(elemType.type, p); ! p.location = p.location.prependList(depth.toList()); setTypeAnnotationPos(at.annotations, p); elemType = at.underlyingType; } else if (elemType.hasTag(JCTree.Tag.TYPEARRAY)) { depth = depth.append(TypePathEntry.ARRAY); elemType = ((JCArrayTypeTree)elemType).elemtype; --- 1343,1360 ---- JCExpression elemType = tree.elemtype; depth = depth.append(TypePathEntry.ARRAY); while (elemType != null) { if (elemType.hasTag(JCTree.Tag.ANNOTATED_TYPE)) { JCAnnotatedType at = (JCAnnotatedType)elemType; ! final ListBuffer<TypePathEntry> locationbuf = ! locateNestedTypes(elemType.type, ! new ListBuffer<TypePathEntry>()); ! final List<TypePathEntry> location = ! locationbuf.toList().prependList(depth.toList()); ! final TypeAnnotationPosition p = ! TypeAnnotationPosition.newObj(location, currentLambda, ! tree.pos); setTypeAnnotationPos(at.annotations, p); elemType = at.underlyingType; } else if (elemType.hasTag(JCTree.Tag.TYPEARRAY)) { depth = depth.append(TypePathEntry.ARRAY); elemType = ((JCArrayTypeTree)elemType).elemtype;
*** 1318,1330 **** /* System.err.println("Finding pos for: " + annotations); System.err.println(" tree: " + tree + " kind: " + tree.getKind()); System.err.println(" frame: " + frame + " kind: " + frame.getKind()); */ ! TypeAnnotationPosition p = new TypeAnnotationPosition(); ! p.onLambda = currentLambda; ! resolveFrame(tree, frame, frames.toList(), p); setTypeAnnotationPos(annotations, p); } } private void setTypeAnnotationPos(List<JCAnnotation> annotations, --- 1372,1384 ---- /* System.err.println("Finding pos for: " + annotations); System.err.println(" tree: " + tree + " kind: " + tree.getKind()); System.err.println(" frame: " + frame + " kind: " + frame.getKind()); */ ! final TypeAnnotationPosition p = ! resolveFrame(tree, frame, frames.toList(), currentLambda, 0, ! new ListBuffer<TypePathEntry>()); setTypeAnnotationPos(annotations, p); } } private void setTypeAnnotationPos(List<JCAnnotation> annotations,