langtools/src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java
changeset 24396 3c36c6afcbca
parent 22702 1297fbaf34fa
equal deleted inserted replaced
24395:5c541aef1350 24396:3c36c6afcbca
    30 import javax.lang.model.type.TypeKind;
    30 import javax.lang.model.type.TypeKind;
    31 
    31 
    32 import javax.tools.JavaFileObject;
    32 import javax.tools.JavaFileObject;
    33 
    33 
    34 import com.sun.tools.javac.code.Attribute.TypeCompound;
    34 import com.sun.tools.javac.code.Attribute.TypeCompound;
    35 import com.sun.tools.javac.code.Type.AnnotatedType;
       
    36 import com.sun.tools.javac.code.Type.ArrayType;
    35 import com.sun.tools.javac.code.Type.ArrayType;
    37 import com.sun.tools.javac.code.Type.CapturedType;
    36 import com.sun.tools.javac.code.Type.CapturedType;
    38 import com.sun.tools.javac.code.Type.ClassType;
    37 import com.sun.tools.javac.code.Type.ClassType;
    39 import com.sun.tools.javac.code.Type.ErrorType;
    38 import com.sun.tools.javac.code.Type.ErrorType;
    40 import com.sun.tools.javac.code.Type.ForAll;
    39 import com.sun.tools.javac.code.Type.ForAll;
   329                 // Therefore, use typeAnnotations instead of onlyTypeAnnos.
   328                 // Therefore, use typeAnnotations instead of onlyTypeAnnos.
   330                 type = typeWithAnnotations(typetree, type, typeAnnotations, typeAnnotations);
   329                 type = typeWithAnnotations(typetree, type, typeAnnotations, typeAnnotations);
   331                 // Note that we don't use the result, the call to
   330                 // Note that we don't use the result, the call to
   332                 // typeWithAnnotations side-effects the type annotation positions.
   331                 // typeWithAnnotations side-effects the type annotation positions.
   333                 // This is important for constructors of nested classes.
   332                 // This is important for constructors of nested classes.
   334 
       
   335                 sym.appendUniqueTypeAttributes(typeAnnotations);
   333                 sym.appendUniqueTypeAttributes(typeAnnotations);
   336                 return;
   334                 return;
   337             }
   335             }
   338 
   336 
   339             // type is non-null and annotations are added to that type
   337             // type is non-null and annotations are added to that type
   389         // As a side effect the method sets the type annotation position of "annotations".
   387         // As a side effect the method sets the type annotation position of "annotations".
   390         // Note that it is assumed that all annotations share the same position.
   388         // Note that it is assumed that all annotations share the same position.
   391         private Type typeWithAnnotations(final JCTree typetree, final Type type,
   389         private Type typeWithAnnotations(final JCTree typetree, final Type type,
   392                 final List<Attribute.TypeCompound> annotations,
   390                 final List<Attribute.TypeCompound> annotations,
   393                 final List<Attribute.TypeCompound> onlyTypeAnnotations) {
   391                 final List<Attribute.TypeCompound> onlyTypeAnnotations) {
   394             // System.out.printf("typeWithAnnotations(typetree: %s, type: %s, annotations: %s, onlyTypeAnnotations: %s)%n",
   392             //System.err.printf("typeWithAnnotations(typetree: %s, type: %s, annotations: %s, onlyTypeAnnotations: %s)%n",
   395             //         typetree, type, annotations, onlyTypeAnnotations);
   393             //         typetree, type, annotations, onlyTypeAnnotations);
   396             if (annotations.isEmpty()) {
   394             if (annotations.isEmpty()) {
   397                 return type;
   395                 return type;
   398             }
   396             }
   399             if (type.hasTag(TypeTag.ARRAY)) {
   397             if (type.hasTag(TypeTag.ARRAY)) {
   400                 Type.ArrayType arType = (Type.ArrayType) type.unannotatedType();
   398                 Type.ArrayType arType = (Type.ArrayType) type;
   401                 Type.ArrayType tomodify = new Type.ArrayType(null, arType.tsym);
   399                 Type.ArrayType tomodify = new Type.ArrayType(null, arType.tsym,
       
   400                                                              Type.noAnnotations);
   402                 Type toreturn;
   401                 Type toreturn;
   403                 if (type.isAnnotated()) {
   402                 if (type.isAnnotated()) {
   404                     toreturn = tomodify.annotatedType(type.getAnnotationMirrors());
   403                     toreturn = tomodify.annotatedType(type.getAnnotationMirrors());
   405                 } else {
   404                 } else {
   406                     toreturn = tomodify;
   405                     toreturn = tomodify;
   411                 ListBuffer<TypePathEntry> depth = new ListBuffer<>();
   410                 ListBuffer<TypePathEntry> depth = new ListBuffer<>();
   412                 depth = depth.append(TypePathEntry.ARRAY);
   411                 depth = depth.append(TypePathEntry.ARRAY);
   413                 while (arType.elemtype.hasTag(TypeTag.ARRAY)) {
   412                 while (arType.elemtype.hasTag(TypeTag.ARRAY)) {
   414                     if (arType.elemtype.isAnnotated()) {
   413                     if (arType.elemtype.isAnnotated()) {
   415                         Type aelemtype = arType.elemtype;
   414                         Type aelemtype = arType.elemtype;
   416                         arType = (Type.ArrayType) aelemtype.unannotatedType();
   415                         arType = (Type.ArrayType) aelemtype;
   417                         ArrayType prevToMod = tomodify;
   416                         ArrayType prevToMod = tomodify;
   418                         tomodify = new Type.ArrayType(null, arType.tsym);
   417                         tomodify = new Type.ArrayType(null, arType.tsym,
       
   418                                                       Type.noAnnotations);
   419                         prevToMod.elemtype = tomodify.annotatedType(arType.elemtype.getAnnotationMirrors());
   419                         prevToMod.elemtype = tomodify.annotatedType(arType.elemtype.getAnnotationMirrors());
   420                     } else {
   420                     } else {
   421                         arType = (Type.ArrayType) arType.elemtype;
   421                         arType = (Type.ArrayType) arType.elemtype;
   422                         tomodify.elemtype = new Type.ArrayType(null, arType.tsym);
   422                         tomodify.elemtype = new Type.ArrayType(null, arType.tsym,
       
   423                                                                Type.noAnnotations);
   423                         tomodify = (Type.ArrayType) tomodify.elemtype;
   424                         tomodify = (Type.ArrayType) tomodify.elemtype;
   424                     }
   425                     }
   425                     arTree = arrayTypeTree(arTree.elemtype);
   426                     arTree = arrayTypeTree(arTree.elemtype);
   426                     depth = depth.append(TypePathEntry.ARRAY);
   427                     depth = depth.append(TypePathEntry.ARRAY);
   427                 }
   428                 }
   567          * @return A copy of type that contains the annotations.
   568          * @return A copy of type that contains the annotations.
   568          */
   569          */
   569         private Type typeWithAnnotations(final Type type,
   570         private Type typeWithAnnotations(final Type type,
   570                 final Type stopAt,
   571                 final Type stopAt,
   571                 final List<Attribute.TypeCompound> annotations) {
   572                 final List<Attribute.TypeCompound> annotations) {
       
   573             //System.err.println("typeWithAnnotations " + type + " " + annotations + " stopAt " + stopAt);
   572             Visitor<Type, List<TypeCompound>> visitor =
   574             Visitor<Type, List<TypeCompound>> visitor =
   573                     new Type.Visitor<Type, List<Attribute.TypeCompound>>() {
   575                     new Type.Visitor<Type, List<Attribute.TypeCompound>>() {
   574                 @Override
   576                 @Override
   575                 public Type visitClassType(ClassType t, List<TypeCompound> s) {
   577                 public Type visitClassType(ClassType t, List<TypeCompound> s) {
   576                     // assert that t.constValue() == null?
   578                     // assert that t.constValue() == null?
   577                     if (t == stopAt ||
   579                     if (t == stopAt ||
   578                         t.getEnclosingType() == Type.noType) {
   580                         t.getEnclosingType() == Type.noType) {
   579                         return t.annotatedType(s);
   581                         return t.annotatedType(s);
   580                     } else {
   582                     } else {
   581                         ClassType ret = new ClassType(t.getEnclosingType().accept(this, s),
   583                         ClassType ret = new ClassType(t.getEnclosingType().accept(this, s),
   582                                 t.typarams_field, t.tsym);
   584                                                       t.typarams_field, t.tsym,
       
   585                                                       t.getAnnotationMirrors());
   583                         ret.all_interfaces_field = t.all_interfaces_field;
   586                         ret.all_interfaces_field = t.all_interfaces_field;
   584                         ret.allparams_field = t.allparams_field;
   587                         ret.allparams_field = t.allparams_field;
   585                         ret.interfaces_field = t.interfaces_field;
   588                         ret.interfaces_field = t.interfaces_field;
   586                         ret.rank_field = t.rank_field;
   589                         ret.rank_field = t.rank_field;
   587                         ret.supertype_field = t.supertype_field;
   590                         ret.supertype_field = t.supertype_field;
   588                         return ret;
   591                         return ret;
   589                     }
   592                     }
   590                 }
   593                 }
   591 
   594 
   592                 @Override
   595                 @Override
   593                 public Type visitAnnotatedType(AnnotatedType t, List<TypeCompound> s) {
       
   594                     return t.unannotatedType().accept(this, s).annotatedType(t.getAnnotationMirrors());
       
   595                 }
       
   596 
       
   597                 @Override
       
   598                 public Type visitWildcardType(WildcardType t, List<TypeCompound> s) {
   596                 public Type visitWildcardType(WildcardType t, List<TypeCompound> s) {
   599                     return t.annotatedType(s);
   597                     return t.annotatedType(s);
   600                 }
   598                 }
   601 
   599 
   602                 @Override
   600                 @Override
   603                 public Type visitArrayType(ArrayType t, List<TypeCompound> s) {
   601                 public Type visitArrayType(ArrayType t, List<TypeCompound> s) {
   604                     ArrayType ret = new ArrayType(t.elemtype.accept(this, s), t.tsym);
   602                     ArrayType ret = new ArrayType(t.elemtype.accept(this, s), t.tsym,
       
   603                                                   t.getAnnotationMirrors());
   605                     return ret;
   604                     return ret;
   606                 }
   605                 }
   607 
   606 
   608                 @Override
   607                 @Override
   609                 public Type visitMethodType(MethodType t, List<TypeCompound> s) {
   608                 public Type visitMethodType(MethodType t, List<TypeCompound> s) {