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

changeset 1570
f91144b7da75
parent 1521
71f35e4b93a5
child 1550
1df20330f6bd
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java	Mon Jan 21 01:27:42 2013 -0500
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Mon Feb 04 18:08:53 2013 -0500
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 2003, 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 @@ -34,13 +34,14 @@
    1.11  import java.util.Set;
    1.12  import java.util.WeakHashMap;
    1.13  
    1.14 +import javax.lang.model.type.TypeKind;
    1.15 +
    1.16  import com.sun.tools.javac.code.Attribute.RetentionPolicy;
    1.17  import com.sun.tools.javac.code.Lint.LintCategory;
    1.18  import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
    1.19  import com.sun.tools.javac.comp.Check;
    1.20  import com.sun.tools.javac.jvm.ClassReader;
    1.21  import com.sun.tools.javac.util.*;
    1.22 -import com.sun.tools.javac.util.List;
    1.23  import static com.sun.tools.javac.code.BoundKind.*;
    1.24  import static com.sun.tools.javac.code.Flags.*;
    1.25  import static com.sun.tools.javac.code.Scope.*;
    1.26 @@ -354,8 +355,29 @@
    1.27                  return descSym;
    1.28              }
    1.29  
    1.30 -            public Type getType(Type origin) {
    1.31 -                return memberType(origin, descSym);
    1.32 +            public Type getType(Type site) {
    1.33 +                if (capture(site) != site) {
    1.34 +                    Type formalInterface = site.tsym.type;
    1.35 +                    ListBuffer<Type> typeargs = ListBuffer.lb();
    1.36 +                    List<Type> actualTypeargs = site.getTypeArguments();
    1.37 +                    //simply replace the wildcards with its bound
    1.38 +                    for (Type t : formalInterface.getTypeArguments()) {
    1.39 +                        if (actualTypeargs.head.hasTag(WILDCARD)) {
    1.40 +                            WildcardType wt = (WildcardType)actualTypeargs.head;
    1.41 +                            typeargs.append(wt.type);
    1.42 +                        } else {
    1.43 +                            typeargs.append(actualTypeargs.head);
    1.44 +                        }
    1.45 +                        actualTypeargs = actualTypeargs.tail;
    1.46 +                    }
    1.47 +                    site = subst(formalInterface, formalInterface.getTypeArguments(), typeargs.toList());
    1.48 +                    if (!chk.checkValidGenericType(site)) {
    1.49 +                        //if the inferred functional interface type is not well-formed,
    1.50 +                        //or if it's not a subtype of the original target, issue an error
    1.51 +                        throw failure(diags.fragment("no.suitable.functional.intf.inst", site));
    1.52 +                    }
    1.53 +                }
    1.54 +                return memberType(site, descSym);
    1.55              }
    1.56          }
    1.57  
    1.58 @@ -392,9 +414,9 @@
    1.59           * Compute the function descriptor associated with a given functional interface
    1.60           */
    1.61          public FunctionDescriptor findDescriptorInternal(TypeSymbol origin, CompoundScope membersCache) throws FunctionDescriptorLookupError {
    1.62 -            if (!origin.isInterface()) {
    1.63 +            if (!origin.isInterface() || (origin.flags() & ANNOTATION) != 0) {
    1.64                  //t must be an interface
    1.65 -                throw failure("not.a.functional.intf");
    1.66 +                throw failure("not.a.functional.intf", origin);
    1.67              }
    1.68  
    1.69              final ListBuffer<Symbol> abstracts = ListBuffer.lb();
    1.70 @@ -406,13 +428,13 @@
    1.71                      abstracts.append(sym);
    1.72                  } else {
    1.73                      //the target method(s) should be the only abstract members of t
    1.74 -                    throw failure("not.a.functional.intf.1",
    1.75 +                    throw failure("not.a.functional.intf.1",  origin,
    1.76                              diags.fragment("incompatible.abstracts", Kinds.kindName(origin), origin));
    1.77                  }
    1.78              }
    1.79              if (abstracts.isEmpty()) {
    1.80                  //t must define a suitable non-generic method
    1.81 -                throw failure("not.a.functional.intf.1",
    1.82 +                throw failure("not.a.functional.intf.1", origin,
    1.83                              diags.fragment("no.abstracts", Kinds.kindName(origin), origin));
    1.84              } else if (abstracts.size() == 1) {
    1.85                  return new FunctionDescriptor(abstracts.first());
    1.86 @@ -553,6 +575,15 @@
    1.87              return false;
    1.88          }
    1.89      }
    1.90 +
    1.91 +    public boolean isFunctionalInterface(Type site) {
    1.92 +        try {
    1.93 +            findDescriptorType(site);
    1.94 +            return true;
    1.95 +        } catch (FunctionDescriptorLookupError ex) {
    1.96 +            return false;
    1.97 +        }
    1.98 +    }
    1.99      // </editor-fold>
   1.100  
   1.101     /**
   1.102 @@ -654,6 +685,8 @@
   1.103      //where
   1.104          private boolean isSubtypeUncheckedInternal(Type t, Type s, Warner warn) {
   1.105              if (t.hasTag(ARRAY) && s.hasTag(ARRAY)) {
   1.106 +                t = t.unannotatedType();
   1.107 +                s = s.unannotatedType();
   1.108                  if (((ArrayType)t).elemtype.isPrimitive()) {
   1.109                      return isSameType(elemtype(t), elemtype(s));
   1.110                  } else {
   1.111 @@ -679,7 +712,10 @@
   1.112          }
   1.113  
   1.114          private void checkUnsafeVarargsConversion(Type t, Type s, Warner warn) {
   1.115 -            if (t.tag != ARRAY || isReifiable(t)) return;
   1.116 +            if (t.tag != ARRAY || isReifiable(t))
   1.117 +                return;
   1.118 +            t = t.unannotatedType();
   1.119 +            s = s.unannotatedType();
   1.120              ArrayType from = (ArrayType)t;
   1.121              boolean shouldWarn = false;
   1.122              switch (s.tag) {
   1.123 @@ -712,6 +748,12 @@
   1.124          if (t == s)
   1.125              return true;
   1.126  
   1.127 +        t = t.unannotatedType();
   1.128 +        s = s.unannotatedType();
   1.129 +
   1.130 +        if (t == s)
   1.131 +            return true;
   1.132 +
   1.133          if (s.isPartial())
   1.134              return isSuperType(s, t);
   1.135  
   1.136 @@ -1653,6 +1695,7 @@
   1.137          case WILDCARD:
   1.138              return elemtype(upperBound(t));
   1.139          case ARRAY:
   1.140 +            t = t.unannotatedType();
   1.141              return ((ArrayType)t).elemtype;
   1.142          case FORALL:
   1.143              return elemtype(((ForAll)t).qtype);
   1.144 @@ -1981,6 +2024,11 @@
   1.145              public Type visitErrorType(ErrorType t, Boolean recurse) {
   1.146                  return t;
   1.147              }
   1.148 +
   1.149 +            @Override
   1.150 +            public Type visitAnnotatedType(AnnotatedType t, Boolean recurse) {
   1.151 +                return new AnnotatedType(t.typeAnnotations, erasure(t.underlyingType, recurse));
   1.152 +            }
   1.153          };
   1.154  
   1.155      private Mapping erasureFun = new Mapping ("erasure") {
   1.156 @@ -2923,6 +2971,7 @@
   1.157       * graph. Undefined for all but reference types.
   1.158       */
   1.159      public int rank(Type t) {
   1.160 +        t = t.unannotatedType();
   1.161          switch(t.tag) {
   1.162          case CLASS: {
   1.163              ClassType cls = (ClassType)t;
   1.164 @@ -3624,6 +3673,7 @@
   1.165                  t = subst(type1, t.tsym.type.getTypeArguments(), t.getTypeArguments());
   1.166              }
   1.167          }
   1.168 +        t = t.unannotatedType();
   1.169          ClassType cls = (ClassType)t;
   1.170          if (cls.isRaw() || !cls.isParameterized())
   1.171              return cls;
   1.172 @@ -4142,6 +4192,8 @@
   1.173          public R visitForAll(ForAll t, S s)             { return visitType(t, s); }
   1.174          public R visitUndetVar(UndetVar t, S s)         { return visitType(t, s); }
   1.175          public R visitErrorType(ErrorType t, S s)       { return visitType(t, s); }
   1.176 +        // Pretend annotations don't exist
   1.177 +        public R visitAnnotatedType(AnnotatedType t, S s) { return visit(t.underlyingType, s); }
   1.178      }
   1.179  
   1.180      /**

mercurial