< prev index next >

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

Print this page

 199         } else if (at2 == AnnotationType.NONE) {
 200             return at1;
 201         } else if (at1 != at2) {
 202             return AnnotationType.BOTH;
 203         } else {
 204             return at1;
 205         }
 206     }
 207 
 208     private AnnotationType targetToAnnotationType(JCTree pos, Attribute.Compound anno, Attribute a, Symbol s) {
 209         Attribute.Enum e = (Attribute.Enum)a;
 210         if (e.value.name == names.TYPE) {
 211             if (s.kind == TYP)
 212                 return AnnotationType.DECLARATION;
 213         } else if (e.value.name == names.FIELD || e.value.name == names.RECORD_COMPONENT) {
 214             if (s.kind == VAR &&
 215                     s.owner.kind != MTH)
 216                 return AnnotationType.DECLARATION;
 217         } else if (e.value.name == names.METHOD) {
 218             if (s.kind == MTH &&
 219                     !s.isConstructor())
 220                 return AnnotationType.DECLARATION;
 221         } else if (e.value.name == names.PARAMETER) {
 222             if (s.kind == VAR &&
 223                     s.owner.kind == MTH &&
 224                     (s.flags() & Flags.PARAMETER) != 0)
 225                 return AnnotationType.DECLARATION;
 226         } else if (e.value.name == names.CONSTRUCTOR) {
 227             if (s.kind == MTH &&
 228                     s.isConstructor())
 229                 return AnnotationType.DECLARATION;
 230         } else if (e.value.name == names.LOCAL_VARIABLE) {
 231             if (s.kind == VAR &&
 232                     s.owner.kind == MTH &&
 233                     (s.flags() & Flags.PARAMETER) == 0)
 234                 return AnnotationType.DECLARATION;
 235         } else if (e.value.name == names.ANNOTATION_TYPE) {
 236             if (s.kind == TYP &&
 237                     (s.flags() & Flags.ANNOTATION) != 0)
 238                 return AnnotationType.DECLARATION;
 239         } else if (e.value.name == names.PACKAGE) {
 240             if (s.kind == PCK)
 241                 return AnnotationType.DECLARATION;
 242         } else if (e.value.name == names.TYPE_USE) {
 243             if (s.kind == TYP ||
 244                     s.kind == VAR ||
 245                     (s.kind == MTH && !s.isConstructor() &&
 246                     !s.type.getReturnType().hasTag(TypeTag.VOID)) ||
 247                     (s.kind == MTH && s.isConstructor()))
 248                 return AnnotationType.TYPE;
 249         } else if (e.value.name == names.TYPE_PARAMETER) {
 250             /* Irrelevant in this case */
 251             // TYPE_PARAMETER doesn't aid in distinguishing between
 252             // Type annotations and declaration annotations on an
 253             // Element
 254         } else if (e.value.name == names.MODULE) {
 255             if (s.kind == MDL)
 256                 return AnnotationType.DECLARATION;
 257         } else {
 258             // there is an erroneous target, an error should have been reported already
 259             return AnnotationType.DECLARATION;
 260         }
 261         return AnnotationType.NONE;
 262     }
 263 
 264     private class TypeAnnotationPositions extends TreeScanner {
 265 
 266         private final boolean sigOnly;
 267 

 587          *
 588          * @param type The type to copy.
 589          * @param stopAt The type to stop at.
 590          * @param annotations The annotations to insert.
 591          * @return A copy of type that contains the annotations.
 592          */
 593         private Type typeWithAnnotations(final Type type,
 594                 final Type stopAt,
 595                 final List<Attribute.TypeCompound> annotations) {
 596             Visitor<Type, List<TypeCompound>> visitor =
 597                     new Type.Visitor<Type, List<Attribute.TypeCompound>>() {
 598                 @Override
 599                 public Type visitClassType(ClassType t, List<TypeCompound> s) {
 600                     // assert that t.constValue() == null?
 601                     if (t == stopAt ||
 602                         t.getEnclosingType() == Type.noType) {
 603                         return t.annotatedType(s);
 604                     } else {
 605                         ClassType ret = new ClassType(t.getEnclosingType().accept(this, s),
 606                                                       t.typarams_field, t.tsym,
 607                                                       t.getMetadata());
 608                         ret.all_interfaces_field = t.all_interfaces_field;
 609                         ret.allparams_field = t.allparams_field;
 610                         ret.interfaces_field = t.interfaces_field;
 611                         ret.rank_field = t.rank_field;
 612                         ret.supertype_field = t.supertype_field;
 613                         return ret;
 614                     }
 615                 }
 616 
 617                 @Override
 618                 public Type visitWildcardType(WildcardType t, List<TypeCompound> s) {
 619                     return t.annotatedType(s);
 620                 }
 621 
 622                 @Override
 623                 public Type visitArrayType(ArrayType t, List<TypeCompound> s) {
 624                     ArrayType ret = new ArrayType(t.elemtype.accept(this, s), t.tsym,
 625                                                   t.getMetadata());
 626                     return ret;
 627                 }

1005                                         outer_type_index, location);
1006                 }
1007 
1008                 case INTERSECTION_TYPE: {
1009                     JCTypeIntersection isect = (JCTypeIntersection)frame;
1010                     final List<JCTree> newPath = path.tail;
1011                     return resolveFrame(newPath.head, newPath.tail.head,
1012                                         newPath, currentLambda,
1013                                         isect.bounds.indexOf(tree), location);
1014                 }
1015 
1016                 case METHOD_INVOCATION: {
1017                     JCMethodInvocation invocation = (JCMethodInvocation)frame;
1018                     if (!invocation.typeargs.contains(tree)) {
1019                         return TypeAnnotationPosition.unknown;
1020                     }
1021                     MethodSymbol exsym = (MethodSymbol) TreeInfo.symbol(invocation.getMethodSelect());
1022                     final int type_index = invocation.typeargs.indexOf(tree);
1023                     if (exsym == null) {
1024                         throw new AssertionError("could not determine symbol for {" + invocation + "}");
1025                     } else if (exsym.isConstructor()) {
1026                         return TypeAnnotationPosition
1027                             .constructorInvocationTypeArg(location.toList(),
1028                                                           currentLambda,
1029                                                           type_index,
1030                                                           invocation.pos);
1031                     } else {
1032                         return TypeAnnotationPosition
1033                             .methodInvocationTypeArg(location.toList(),
1034                                                      currentLambda,
1035                                                      type_index,
1036                                                      invocation.pos);
1037                     }
1038                 }
1039 
1040                 case EXTENDS_WILDCARD:
1041                 case SUPER_WILDCARD: {
1042                     // Annotations in wildcard bounds
1043                     final List<JCTree> newPath = path.tail;
1044                     return resolveFrame(newPath.head, newPath.tail.head,
1045                                         newPath, currentLambda,

1112                 scan(tree.extending);
1113                 scan(tree.implementing);
1114             }
1115             scan(tree.defs);
1116             if (tree.sym.isRecord()) {
1117                 tree.sym.getRecordComponents().forEach(rc -> scan(rc.accessorMeth));
1118             }
1119         }
1120 
1121         /**
1122          * Resolve declaration vs. type annotations in methods and
1123          * then determine the positions.
1124          */
1125         @Override
1126         public void visitMethodDef(final JCMethodDecl tree) {
1127             if (tree.sym == null) {
1128                 Assert.error("Visiting tree node before memberEnter");
1129             }
1130             if (sigOnly) {
1131                 if (!tree.mods.annotations.isEmpty()) {
1132                     if (tree.sym.isConstructor()) {
1133                         final TypeAnnotationPosition pos =
1134                             TypeAnnotationPosition.methodReturn(tree.pos);
1135                         // Use null to mark that the annotations go
1136                         // with the symbol.
1137                         separateAnnotationsKinds(tree, tree, null, tree.sym, pos);
1138                     } else {
1139                         final TypeAnnotationPosition pos =
1140                             TypeAnnotationPosition.methodReturn(tree.restype.pos);
1141                         separateAnnotationsKinds(tree, tree.restype,
1142                                                  tree.sym.type.getReturnType(),
1143                                                  tree.sym, pos);
1144                     }
1145                 }
1146                 if (tree.recvparam != null && tree.recvparam.sym != null &&
1147                         !tree.recvparam.mods.annotations.isEmpty()) {
1148                     // Nothing to do for separateAnnotationsKinds if
1149                     // there are no annotations of either kind.
1150                     // TODO: make sure there are no declaration annotations.
1151                     final TypeAnnotationPosition pos = TypeAnnotationPosition.methodReceiver(tree.recvparam.vartype.pos);
1152                     push(tree.recvparam);

 199         } else if (at2 == AnnotationType.NONE) {
 200             return at1;
 201         } else if (at1 != at2) {
 202             return AnnotationType.BOTH;
 203         } else {
 204             return at1;
 205         }
 206     }
 207 
 208     private AnnotationType targetToAnnotationType(JCTree pos, Attribute.Compound anno, Attribute a, Symbol s) {
 209         Attribute.Enum e = (Attribute.Enum)a;
 210         if (e.value.name == names.TYPE) {
 211             if (s.kind == TYP)
 212                 return AnnotationType.DECLARATION;
 213         } else if (e.value.name == names.FIELD || e.value.name == names.RECORD_COMPONENT) {
 214             if (s.kind == VAR &&
 215                     s.owner.kind != MTH)
 216                 return AnnotationType.DECLARATION;
 217         } else if (e.value.name == names.METHOD) {
 218             if (s.kind == MTH &&
 219                     !s.isInitOrVNew())
 220                 return AnnotationType.DECLARATION;
 221         } else if (e.value.name == names.PARAMETER) {
 222             if (s.kind == VAR &&
 223                     s.owner.kind == MTH &&
 224                     (s.flags() & Flags.PARAMETER) != 0)
 225                 return AnnotationType.DECLARATION;
 226         } else if (e.value.name == names.CONSTRUCTOR) {
 227             if (s.kind == MTH &&
 228                     s.isConstructor())
 229                 return AnnotationType.DECLARATION;
 230         } else if (e.value.name == names.LOCAL_VARIABLE) {
 231             if (s.kind == VAR &&
 232                     s.owner.kind == MTH &&
 233                     (s.flags() & Flags.PARAMETER) == 0)
 234                 return AnnotationType.DECLARATION;
 235         } else if (e.value.name == names.ANNOTATION_TYPE) {
 236             if (s.kind == TYP &&
 237                     (s.flags() & Flags.ANNOTATION) != 0)
 238                 return AnnotationType.DECLARATION;
 239         } else if (e.value.name == names.PACKAGE) {
 240             if (s.kind == PCK)
 241                 return AnnotationType.DECLARATION;
 242         } else if (e.value.name == names.TYPE_USE) {
 243             if (s.kind == TYP ||
 244                     s.kind == VAR ||
 245                     (s.kind == MTH && !s.isInitOrVNew() &&
 246                     !s.type.getReturnType().hasTag(TypeTag.VOID)) ||
 247                     (s.kind == MTH && s.isInitOrVNew()))
 248                 return AnnotationType.TYPE;
 249         } else if (e.value.name == names.TYPE_PARAMETER) {
 250             /* Irrelevant in this case */
 251             // TYPE_PARAMETER doesn't aid in distinguishing between
 252             // Type annotations and declaration annotations on an
 253             // Element
 254         } else if (e.value.name == names.MODULE) {
 255             if (s.kind == MDL)
 256                 return AnnotationType.DECLARATION;
 257         } else {
 258             // there is an erroneous target, an error should have been reported already
 259             return AnnotationType.DECLARATION;
 260         }
 261         return AnnotationType.NONE;
 262     }
 263 
 264     private class TypeAnnotationPositions extends TreeScanner {
 265 
 266         private final boolean sigOnly;
 267 

 587          *
 588          * @param type The type to copy.
 589          * @param stopAt The type to stop at.
 590          * @param annotations The annotations to insert.
 591          * @return A copy of type that contains the annotations.
 592          */
 593         private Type typeWithAnnotations(final Type type,
 594                 final Type stopAt,
 595                 final List<Attribute.TypeCompound> annotations) {
 596             Visitor<Type, List<TypeCompound>> visitor =
 597                     new Type.Visitor<Type, List<Attribute.TypeCompound>>() {
 598                 @Override
 599                 public Type visitClassType(ClassType t, List<TypeCompound> s) {
 600                     // assert that t.constValue() == null?
 601                     if (t == stopAt ||
 602                         t.getEnclosingType() == Type.noType) {
 603                         return t.annotatedType(s);
 604                     } else {
 605                         ClassType ret = new ClassType(t.getEnclosingType().accept(this, s),
 606                                                       t.typarams_field, t.tsym,
 607                                                       t.getMetadata(), t.getFlavor());
 608                         ret.all_interfaces_field = t.all_interfaces_field;
 609                         ret.allparams_field = t.allparams_field;
 610                         ret.interfaces_field = t.interfaces_field;
 611                         ret.rank_field = t.rank_field;
 612                         ret.supertype_field = t.supertype_field;
 613                         return ret;
 614                     }
 615                 }
 616 
 617                 @Override
 618                 public Type visitWildcardType(WildcardType t, List<TypeCompound> s) {
 619                     return t.annotatedType(s);
 620                 }
 621 
 622                 @Override
 623                 public Type visitArrayType(ArrayType t, List<TypeCompound> s) {
 624                     ArrayType ret = new ArrayType(t.elemtype.accept(this, s), t.tsym,
 625                                                   t.getMetadata());
 626                     return ret;
 627                 }

1005                                         outer_type_index, location);
1006                 }
1007 
1008                 case INTERSECTION_TYPE: {
1009                     JCTypeIntersection isect = (JCTypeIntersection)frame;
1010                     final List<JCTree> newPath = path.tail;
1011                     return resolveFrame(newPath.head, newPath.tail.head,
1012                                         newPath, currentLambda,
1013                                         isect.bounds.indexOf(tree), location);
1014                 }
1015 
1016                 case METHOD_INVOCATION: {
1017                     JCMethodInvocation invocation = (JCMethodInvocation)frame;
1018                     if (!invocation.typeargs.contains(tree)) {
1019                         return TypeAnnotationPosition.unknown;
1020                     }
1021                     MethodSymbol exsym = (MethodSymbol) TreeInfo.symbol(invocation.getMethodSelect());
1022                     final int type_index = invocation.typeargs.indexOf(tree);
1023                     if (exsym == null) {
1024                         throw new AssertionError("could not determine symbol for {" + invocation + "}");
1025                     } else if (exsym.isInitOrVNew()) {
1026                         return TypeAnnotationPosition
1027                             .constructorInvocationTypeArg(location.toList(),
1028                                                           currentLambda,
1029                                                           type_index,
1030                                                           invocation.pos);
1031                     } else {
1032                         return TypeAnnotationPosition
1033                             .methodInvocationTypeArg(location.toList(),
1034                                                      currentLambda,
1035                                                      type_index,
1036                                                      invocation.pos);
1037                     }
1038                 }
1039 
1040                 case EXTENDS_WILDCARD:
1041                 case SUPER_WILDCARD: {
1042                     // Annotations in wildcard bounds
1043                     final List<JCTree> newPath = path.tail;
1044                     return resolveFrame(newPath.head, newPath.tail.head,
1045                                         newPath, currentLambda,

1112                 scan(tree.extending);
1113                 scan(tree.implementing);
1114             }
1115             scan(tree.defs);
1116             if (tree.sym.isRecord()) {
1117                 tree.sym.getRecordComponents().forEach(rc -> scan(rc.accessorMeth));
1118             }
1119         }
1120 
1121         /**
1122          * Resolve declaration vs. type annotations in methods and
1123          * then determine the positions.
1124          */
1125         @Override
1126         public void visitMethodDef(final JCMethodDecl tree) {
1127             if (tree.sym == null) {
1128                 Assert.error("Visiting tree node before memberEnter");
1129             }
1130             if (sigOnly) {
1131                 if (!tree.mods.annotations.isEmpty()) {
1132                     if (tree.sym.isInitOrVNew()) {
1133                         final TypeAnnotationPosition pos =
1134                             TypeAnnotationPosition.methodReturn(tree.pos);
1135                         // Use null to mark that the annotations go
1136                         // with the symbol.
1137                         separateAnnotationsKinds(tree, tree, null, tree.sym, pos);
1138                     } else {
1139                         final TypeAnnotationPosition pos =
1140                             TypeAnnotationPosition.methodReturn(tree.restype.pos);
1141                         separateAnnotationsKinds(tree, tree.restype,
1142                                                  tree.sym.type.getReturnType(),
1143                                                  tree.sym, pos);
1144                     }
1145                 }
1146                 if (tree.recvparam != null && tree.recvparam.sym != null &&
1147                         !tree.recvparam.mods.annotations.isEmpty()) {
1148                     // Nothing to do for separateAnnotationsKinds if
1149                     // there are no annotations of either kind.
1150                     // TODO: make sure there are no declaration annotations.
1151                     final TypeAnnotationPosition pos = TypeAnnotationPosition.methodReceiver(tree.recvparam.vartype.pos);
1152                     push(tree.recvparam);
< prev index next >