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

changeset 795
7b99f98b3035
parent 789
878c8f760ded
child 798
4868a36f6fd8
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java	Mon Dec 13 14:08:01 2010 -0800
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Mon Dec 13 15:11:00 2010 -0800
     1.3 @@ -33,6 +33,7 @@
     1.4  
     1.5  import com.sun.tools.javac.jvm.ClassReader;
     1.6  import com.sun.tools.javac.code.Attribute.RetentionPolicy;
     1.7 +import com.sun.tools.javac.code.Lint.LintCategory;
     1.8  import com.sun.tools.javac.comp.Check;
     1.9  
    1.10  import static com.sun.tools.javac.code.Type.*;
    1.11 @@ -272,13 +273,36 @@
    1.12      public boolean isConvertible(Type t, Type s, Warner warn) {
    1.13          boolean tPrimitive = t.isPrimitive();
    1.14          boolean sPrimitive = s.isPrimitive();
    1.15 -        if (tPrimitive == sPrimitive)
    1.16 +        if (tPrimitive == sPrimitive) {
    1.17 +            checkUnsafeVarargsConversion(t, s, warn);
    1.18              return isSubtypeUnchecked(t, s, warn);
    1.19 +        }
    1.20          if (!allowBoxing) return false;
    1.21          return tPrimitive
    1.22              ? isSubtype(boxedClass(t).type, s)
    1.23              : isSubtype(unboxedType(t), s);
    1.24      }
    1.25 +    //where
    1.26 +    private void checkUnsafeVarargsConversion(Type t, Type s, Warner warn) {
    1.27 +        if (t.tag != ARRAY || isReifiable(t)) return;
    1.28 +        ArrayType from = (ArrayType)t;
    1.29 +        boolean shouldWarn = false;
    1.30 +        switch (s.tag) {
    1.31 +            case ARRAY:
    1.32 +                ArrayType to = (ArrayType)s;
    1.33 +                shouldWarn = from.isVarargs() &&
    1.34 +                        !to.isVarargs() &&
    1.35 +                        !isReifiable(from);
    1.36 +                break;
    1.37 +            case CLASS:
    1.38 +                shouldWarn = from.isVarargs() &&
    1.39 +                        isSubtype(from, s);
    1.40 +                break;
    1.41 +        }
    1.42 +        if (shouldWarn) {
    1.43 +            warn.warn(LintCategory.VARARGS);
    1.44 +        }
    1.45 +    }
    1.46  
    1.47      /**
    1.48       * Is t a subtype of or convertiable via boxing/unboxing
    1.49 @@ -301,9 +325,18 @@
    1.50       */
    1.51      public boolean isSubtypeUnchecked(Type t, Type s, Warner warn) {
    1.52          if (t.tag == ARRAY && s.tag == ARRAY) {
    1.53 -            return (((ArrayType)t).elemtype.tag <= lastBaseTag)
    1.54 -                ? isSameType(elemtype(t), elemtype(s))
    1.55 -                : isSubtypeUnchecked(elemtype(t), elemtype(s), warn);
    1.56 +            if (((ArrayType)t).elemtype.tag <= lastBaseTag) {
    1.57 +                return isSameType(elemtype(t), elemtype(s));
    1.58 +            } else {
    1.59 +                ArrayType from = (ArrayType)t;
    1.60 +                ArrayType to = (ArrayType)s;
    1.61 +                if (from.isVarargs() &&
    1.62 +                        !to.isVarargs() &&
    1.63 +                        !isReifiable(from)) {
    1.64 +                    warn.warn(LintCategory.VARARGS);
    1.65 +                }
    1.66 +                return isSubtypeUnchecked(elemtype(t), elemtype(s), warn);
    1.67 +            }
    1.68          } else if (isSubtype(t, s)) {
    1.69              return true;
    1.70          }
    1.71 @@ -319,9 +352,9 @@
    1.72              Type t2 = asSuper(t, s.tsym);
    1.73              if (t2 != null && t2.isRaw()) {
    1.74                  if (isReifiable(s))
    1.75 -                    warn.silentUnchecked();
    1.76 +                    warn.silentWarn(LintCategory.UNCHECKED);
    1.77                  else
    1.78 -                    warn.warnUnchecked();
    1.79 +                    warn.warn(LintCategory.UNCHECKED);
    1.80                  return true;
    1.81              }
    1.82          }
    1.83 @@ -922,6 +955,7 @@
    1.84          if (warn != warnStack.head) {
    1.85              try {
    1.86                  warnStack = warnStack.prepend(warn);
    1.87 +                checkUnsafeVarargsConversion(t, s, warn);
    1.88                  return isCastable.visit(t,s);
    1.89              } finally {
    1.90                  warnStack = warnStack.tail;
    1.91 @@ -964,7 +998,7 @@
    1.92  
    1.93                  if (s.tag == TYPEVAR) {
    1.94                      if (isCastable(t, s.getUpperBound(), Warner.noWarnings)) {
    1.95 -                        warnStack.head.warnUnchecked();
    1.96 +                        warnStack.head.warn(LintCategory.UNCHECKED);
    1.97                          return true;
    1.98                      } else {
    1.99                          return false;
   1.100 @@ -980,8 +1014,8 @@
   1.101                          if (!visit(intf, s))
   1.102                              return false;
   1.103                      }
   1.104 -                    if (warnStack.head.unchecked == true)
   1.105 -                        oldWarner.warnUnchecked();
   1.106 +                    if (warnStack.head.hasLint(LintCategory.UNCHECKED))
   1.107 +                        oldWarner.warn(LintCategory.UNCHECKED);
   1.108                      return true;
   1.109                  }
   1.110  
   1.111 @@ -996,13 +1030,13 @@
   1.112                          || isSubtype(erasure(s), erasure(t))) {
   1.113                          if (!upcast && s.tag == ARRAY) {
   1.114                              if (!isReifiable(s))
   1.115 -                                warnStack.head.warnUnchecked();
   1.116 +                                warnStack.head.warn(LintCategory.UNCHECKED);
   1.117                              return true;
   1.118                          } else if (s.isRaw()) {
   1.119                              return true;
   1.120                          } else if (t.isRaw()) {
   1.121                              if (!isUnbounded(s))
   1.122 -                                warnStack.head.warnUnchecked();
   1.123 +                                warnStack.head.warn(LintCategory.UNCHECKED);
   1.124                              return true;
   1.125                          }
   1.126                          // Assume |a| <: |b|
   1.127 @@ -1035,7 +1069,7 @@
   1.128                                  && !disjointTypes(aLow.allparams(), lowSub.allparams())) {
   1.129                                  if (upcast ? giveWarning(a, b) :
   1.130                                      giveWarning(b, a))
   1.131 -                                    warnStack.head.warnUnchecked();
   1.132 +                                    warnStack.head.warn(LintCategory.UNCHECKED);
   1.133                                  return true;
   1.134                              }
   1.135                          }
   1.136 @@ -1072,7 +1106,7 @@
   1.137                      return true;
   1.138                  case TYPEVAR:
   1.139                      if (isCastable(s, t, Warner.noWarnings)) {
   1.140 -                        warnStack.head.warnUnchecked();
   1.141 +                        warnStack.head.warn(LintCategory.UNCHECKED);
   1.142                          return true;
   1.143                      } else {
   1.144                          return false;
   1.145 @@ -1101,7 +1135,7 @@
   1.146                      if (isSubtype(t, s)) {
   1.147                          return true;
   1.148                      } else if (isCastable(t.bound, s, Warner.noWarnings)) {
   1.149 -                        warnStack.head.warnUnchecked();
   1.150 +                        warnStack.head.warn(LintCategory.UNCHECKED);
   1.151                          return true;
   1.152                      } else {
   1.153                          return false;
   1.154 @@ -2906,7 +2940,7 @@
   1.155              return true;
   1.156          if (!isSubtype(r1.getReturnType(), erasure(r2res)))
   1.157              return false;
   1.158 -        warner.warnUnchecked();
   1.159 +        warner.warn(LintCategory.UNCHECKED);
   1.160          return true;
   1.161      }
   1.162  
   1.163 @@ -3122,7 +3156,7 @@
   1.164              commonSupers = commonSupers.tail;
   1.165          }
   1.166          if (giveWarning && !isReifiable(reverse ? from : to))
   1.167 -            warn.warnUnchecked();
   1.168 +            warn.warn(LintCategory.UNCHECKED);
   1.169          if (!source.allowCovariantReturns())
   1.170              // reject if there is a common method signature with
   1.171              // incompatible return types.
   1.172 @@ -3156,7 +3190,7 @@
   1.173              chk.checkCompatibleAbstracts(warn.pos(), from, to);
   1.174          if (!isReifiable(target) &&
   1.175              (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2)))
   1.176 -            warn.warnUnchecked();
   1.177 +            warn.warn(LintCategory.UNCHECKED);
   1.178          return true;
   1.179      }
   1.180  

mercurial