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

changeset 1521
71f35e4b93a5
parent 1452
de1ec6fc93fe
child 1550
1df20330f6bd
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Type.java	Wed Jan 23 20:57:40 2013 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Type.java	Wed Jan 23 13:27:24 2013 -0800
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -31,6 +31,7 @@
    1.11  import java.util.Map;
    1.12  import java.util.Set;
    1.13  
    1.14 +import javax.lang.model.element.AnnotationMirror;
    1.15  import javax.lang.model.type.*;
    1.16  
    1.17  import com.sun.tools.javac.code.Symbol.*;
    1.18 @@ -87,7 +88,7 @@
    1.19       */
    1.20      protected TypeTag tag;
    1.21  
    1.22 -    /** The defining class / interface / package / type variable
    1.23 +    /** The defining class / interface / package / type variable.
    1.24       */
    1.25      public TypeSymbol tsym;
    1.26  
    1.27 @@ -166,7 +167,7 @@
    1.28      /**
    1.29       * Get the representation of this type used for modelling purposes.
    1.30       * By default, this is itself. For ErrorType, a different value
    1.31 -     * may be provided,
    1.32 +     * may be provided.
    1.33       */
    1.34      public Type getModelType() {
    1.35          return this;
    1.36 @@ -245,6 +246,14 @@
    1.37          return this;
    1.38      }
    1.39  
    1.40 +    /**
    1.41 +     * If this is an annotated type, return the underlying type.
    1.42 +     * Otherwise, return the type itself.
    1.43 +     */
    1.44 +    public Type unannotatedType() {
    1.45 +        return this;
    1.46 +    }
    1.47 +
    1.48      /** Return the base types of a list of types.
    1.49       */
    1.50      public static List<Type> baseTypes(List<Type> ts) {
    1.51 @@ -339,8 +348,11 @@
    1.52              args = args.tail;
    1.53              buf.append(',');
    1.54          }
    1.55 -        if (args.head.tag == ARRAY) {
    1.56 -            buf.append(((ArrayType)args.head).elemtype);
    1.57 +        if (args.head.unannotatedType().tag == ARRAY) {
    1.58 +            buf.append(((ArrayType)args.head.unannotatedType()).elemtype);
    1.59 +            if (args.head.getAnnotations().nonEmpty()) {
    1.60 +                buf.append(args.head.getAnnotations());
    1.61 +            }
    1.62              buf.append("...");
    1.63          } else {
    1.64              buf.append(args.head);
    1.65 @@ -350,10 +362,12 @@
    1.66  
    1.67      /** Access methods.
    1.68       */
    1.69 +    public List<? extends AnnotationMirror> getAnnotations() { return List.nil(); }
    1.70      public List<Type>        getTypeArguments()  { return List.nil(); }
    1.71 -    public Type              getEnclosingType() { return null; }
    1.72 +    public Type              getEnclosingType()  { return null; }
    1.73      public List<Type>        getParameterTypes() { return List.nil(); }
    1.74      public Type              getReturnType()     { return null; }
    1.75 +    public Type              getReceiverType()   { return null; }
    1.76      public List<Type>        getThrownTypes()    { return List.nil(); }
    1.77      public Type              getUpperBound()     { return null; }
    1.78      public Type              getLowerBound()     { return null; }
    1.79 @@ -600,7 +614,7 @@
    1.80  
    1.81          /** The enclosing type of this type. If this is the type of an inner
    1.82           *  class, outer_field refers to the type of its enclosing
    1.83 -         *  instance class, in all other cases it referes to noType.
    1.84 +         *  instance class, in all other cases it refers to noType.
    1.85           */
    1.86          private Type outer_field;
    1.87  
    1.88 @@ -974,6 +988,10 @@
    1.89          public Type restype;
    1.90          public List<Type> thrown;
    1.91  
    1.92 +        /** The type annotations on the method receiver.
    1.93 +         */
    1.94 +        public Type recvtype;
    1.95 +
    1.96          public MethodType(List<Type> argtypes,
    1.97                            Type restype,
    1.98                            List<Type> thrown,
    1.99 @@ -1000,6 +1018,7 @@
   1.100  
   1.101          public List<Type>        getParameterTypes() { return argtypes; }
   1.102          public Type              getReturnType()     { return restype; }
   1.103 +        public Type              getReceiverType()   { return recvtype; }
   1.104          public List<Type>        getThrownTypes()    { return thrown; }
   1.105  
   1.106          public boolean isErroneous() {
   1.107 @@ -1028,6 +1047,7 @@
   1.108              for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
   1.109                  l.head.complete();
   1.110              restype.complete();
   1.111 +            recvtype.complete();
   1.112              for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
   1.113                  l.head.complete();
   1.114          }
   1.115 @@ -1112,7 +1132,11 @@
   1.116          }
   1.117  
   1.118          @Override
   1.119 -        public Type getUpperBound() { return bound; }
   1.120 +        public Type getUpperBound() {
   1.121 +            if ((bound == null || bound.tag == NONE) && this != tsym.type)
   1.122 +                bound = tsym.type.getUpperBound();
   1.123 +            return bound;
   1.124 +        }
   1.125  
   1.126          int rank_field = -1;
   1.127  
   1.128 @@ -1183,6 +1207,7 @@
   1.129          public Type getEnclosingType() { return qtype.getEnclosingType(); }
   1.130          public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
   1.131          public Type getReturnType() { return qtype.getReturnType(); }
   1.132 +        public Type getReceiverType() { return qtype.getReceiverType(); }
   1.133          public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
   1.134          public List<Type> allparams() { return qtype.allparams(); }
   1.135          public Type getUpperBound() { return qtype.getUpperBound(); }
   1.136 @@ -1435,7 +1460,7 @@
   1.137          }
   1.138  
   1.139          public Type constType(Object constValue) { return this; }
   1.140 -        public Type getEnclosingType()          { return this; }
   1.141 +        public Type getEnclosingType()           { return this; }
   1.142          public Type getReturnType()              { return this; }
   1.143          public Type asSub(Symbol sym)            { return this; }
   1.144          public Type map(Mapping f)               { return this; }
   1.145 @@ -1461,11 +1486,165 @@
   1.146          }
   1.147      }
   1.148  
   1.149 +    public static class AnnotatedType extends Type
   1.150 +            implements javax.lang.model.type.AnnotatedType {
   1.151 +        /** The type annotations on this type.
   1.152 +         */
   1.153 +        public List<Attribute.TypeCompound> typeAnnotations;
   1.154 +
   1.155 +        /** The underlying type that is annotated.
   1.156 +         */
   1.157 +        public Type underlyingType;
   1.158 +
   1.159 +        public AnnotatedType(Type underlyingType) {
   1.160 +            super(underlyingType.tag, underlyingType.tsym);
   1.161 +            this.typeAnnotations = List.nil();
   1.162 +            this.underlyingType = underlyingType;
   1.163 +            Assert.check(underlyingType.getKind() != TypeKind.ANNOTATED,
   1.164 +                    "Can't annotate already annotated type: " + underlyingType);
   1.165 +        }
   1.166 +
   1.167 +        public AnnotatedType(List<Attribute.TypeCompound> typeAnnotations,
   1.168 +                Type underlyingType) {
   1.169 +            super(underlyingType.tag, underlyingType.tsym);
   1.170 +            this.typeAnnotations = typeAnnotations;
   1.171 +            this.underlyingType = underlyingType;
   1.172 +            Assert.check(underlyingType.getKind() != TypeKind.ANNOTATED,
   1.173 +                    "Can't annotate already annotated type: " + underlyingType +
   1.174 +                    "; adding: " + typeAnnotations);
   1.175 +        }
   1.176 +
   1.177 +        @Override
   1.178 +        public TypeKind getKind() {
   1.179 +            return TypeKind.ANNOTATED;
   1.180 +        }
   1.181 +
   1.182 +        @Override
   1.183 +        public List<? extends AnnotationMirror> getAnnotations() {
   1.184 +            return typeAnnotations;
   1.185 +        }
   1.186 +
   1.187 +        @Override
   1.188 +        public TypeMirror getUnderlyingType() {
   1.189 +            return underlyingType;
   1.190 +        }
   1.191 +
   1.192 +        @Override
   1.193 +        public Type unannotatedType() {
   1.194 +            return underlyingType;
   1.195 +        }
   1.196 +
   1.197 +        @Override
   1.198 +        public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   1.199 +            return v.visitAnnotatedType(this, s);
   1.200 +        }
   1.201 +
   1.202 +        @Override
   1.203 +        public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   1.204 +            return v.visitAnnotated(this, p);
   1.205 +        }
   1.206 +
   1.207 +        @Override
   1.208 +        public Type map(Mapping f) {
   1.209 +            underlyingType.map(f);
   1.210 +            return this;
   1.211 +        }
   1.212 +
   1.213 +        @Override
   1.214 +        public Type constType(Object constValue) { return underlyingType.constType(constValue); }
   1.215 +        @Override
   1.216 +        public Type getEnclosingType()           { return underlyingType.getEnclosingType(); }
   1.217 +
   1.218 +        @Override
   1.219 +        public Type getReturnType()              { return underlyingType.getReturnType(); }
   1.220 +        @Override
   1.221 +        public List<Type> getTypeArguments()     { return underlyingType.getTypeArguments(); }
   1.222 +        @Override
   1.223 +        public List<Type> getParameterTypes()    { return underlyingType.getParameterTypes(); }
   1.224 +        @Override
   1.225 +        public Type getReceiverType()            { return underlyingType.getReceiverType(); }
   1.226 +        @Override
   1.227 +        public List<Type> getThrownTypes()       { return underlyingType.getThrownTypes(); }
   1.228 +        @Override
   1.229 +        public Type getUpperBound()              { return underlyingType.getUpperBound(); }
   1.230 +        @Override
   1.231 +        public Type getLowerBound()              { return underlyingType.getLowerBound(); }
   1.232 +
   1.233 +        @Override
   1.234 +        public boolean isErroneous()             { return underlyingType.isErroneous(); }
   1.235 +        @Override
   1.236 +        public boolean isCompound()              { return underlyingType.isCompound(); }
   1.237 +        @Override
   1.238 +        public boolean isInterface()             { return underlyingType.isInterface(); }
   1.239 +        @Override
   1.240 +        public List<Type> allparams()            { return underlyingType.allparams(); }
   1.241 +        @Override
   1.242 +        public boolean isNumeric()               { return underlyingType.isNumeric(); }
   1.243 +        @Override
   1.244 +        public boolean isReference()             { return underlyingType.isReference(); }
   1.245 +        @Override
   1.246 +        public boolean isParameterized()         { return underlyingType.isParameterized(); }
   1.247 +        @Override
   1.248 +        public boolean isRaw()                   { return underlyingType.isRaw(); }
   1.249 +        @Override
   1.250 +        public boolean isFinal()                 { return underlyingType.isFinal(); }
   1.251 +        @Override
   1.252 +        public boolean isSuperBound()            { return underlyingType.isSuperBound(); }
   1.253 +        @Override
   1.254 +        public boolean isExtendsBound()          { return underlyingType.isExtendsBound(); }
   1.255 +        @Override
   1.256 +        public boolean isUnbound()               { return underlyingType.isUnbound(); }
   1.257 +
   1.258 +        @Override
   1.259 +        public String toString() {
   1.260 +            // TODO more logic for arrays, etc.
   1.261 +            if (typeAnnotations != null &&
   1.262 +                    !typeAnnotations.isEmpty()) {
   1.263 +                return "(" + typeAnnotations.toString() + " :: " + underlyingType.toString() + ")";
   1.264 +            } else {
   1.265 +                return "({} :: " + underlyingType.toString() +")";
   1.266 +            }
   1.267 +        }
   1.268 +
   1.269 +        @Override
   1.270 +        public boolean contains(Type t)          { return underlyingType.contains(t); }
   1.271 +
   1.272 +        // TODO: attach annotations?
   1.273 +        @Override
   1.274 +        public Type withTypeVar(Type t)          { return underlyingType.withTypeVar(t); }
   1.275 +
   1.276 +        // TODO: attach annotations?
   1.277 +        @Override
   1.278 +        public TypeSymbol asElement()            { return underlyingType.asElement(); }
   1.279 +
   1.280 +        // TODO: attach annotations?
   1.281 +        @Override
   1.282 +        public MethodType asMethodType()         { return underlyingType.asMethodType(); }
   1.283 +
   1.284 +        @Override
   1.285 +        public void complete()                   { underlyingType.complete(); }
   1.286 +
   1.287 +        @Override
   1.288 +        public TypeMirror getComponentType()     { return ((ArrayType)underlyingType).getComponentType(); }
   1.289 +
   1.290 +        // The result is an ArrayType, but only in the model sense, not the Type sense.
   1.291 +        public AnnotatedType makeVarargs() {
   1.292 +            AnnotatedType atype = new AnnotatedType(((ArrayType)underlyingType).makeVarargs());
   1.293 +            atype.typeAnnotations = this.typeAnnotations;
   1.294 +            return atype;
   1.295 +        }
   1.296 +
   1.297 +        @Override
   1.298 +        public TypeMirror getExtendsBound()      { return ((WildcardType)underlyingType).getExtendsBound(); }
   1.299 +        @Override
   1.300 +        public TypeMirror getSuperBound()        { return ((WildcardType)underlyingType).getSuperBound(); }
   1.301 +    }
   1.302 +
   1.303      /**
   1.304       * A visitor for types.  A visitor is used to implement operations
   1.305       * (or relations) on types.  Most common operations on types are
   1.306       * binary relations and this interface is designed for binary
   1.307 -     * relations, that is, operations on the form
   1.308 +     * relations, that is, operations of the form
   1.309       * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
   1.310       * <!-- In plain text: Type x S -> R -->
   1.311       *
   1.312 @@ -1486,6 +1665,7 @@
   1.313          R visitForAll(ForAll t, S s);
   1.314          R visitUndetVar(UndetVar t, S s);
   1.315          R visitErrorType(ErrorType t, S s);
   1.316 +        R visitAnnotatedType(AnnotatedType t, S s);
   1.317          R visitType(Type t, S s);
   1.318      }
   1.319  }

mercurial