com.sun.tools.javac.util.Warner Java Examples

The following examples show how to use com.sun.tools.javac.util.Warner. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Types.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Is t is castable to s?<br>
 * s is assumed to be an erased type.<br>
 * (not defined for Method and ForAll types).
 */
public boolean isCastable(Type t, Type s, Warner warn) {
    if (t == s)
        return true;

    if (t.isPrimitive() != s.isPrimitive())
        return allowBoxing && (
                isConvertible(t, s, warn)
                || (allowObjectToPrimitiveCast &&
                    s.isPrimitive() &&
                    isSubtype(boxedClass(s).type, t)));
    if (warn != warnStack.head) {
        try {
            warnStack = warnStack.prepend(warn);
            checkUnsafeVarargsConversion(t, s, warn);
            return isCastable.visit(t,s);
        } finally {
            warnStack = warnStack.tail;
        }
    } else {
        return isCastable.visit(t,s);
    }
}
 
Example #2
Source File: Types.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public boolean returnTypeSubstitutable(Type r1,
                                       Type r2, Type r2res,
                                       Warner warner) {
    if (isSameType(r1.getReturnType(), r2res))
        return true;
    if (r1.getReturnType().isPrimitive() || r2res.isPrimitive())
        return false;

    if (hasSameArgs(r1, r2))
        return covariantReturnType(r1.getReturnType(), r2res, warner);
    if (!allowCovariantReturns)
        return false;
    if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner))
        return true;
    if (!isSubtype(r1.getReturnType(), erasure(r2res)))
        return false;
    warner.warn(LintCategory.UNCHECKED);
    return true;
}
 
Example #3
Source File: Types.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Is t is castable to s?<br>
 * s is assumed to be an erased type.<br>
 * (not defined for Method and ForAll types).
 */
public boolean isCastable(Type t, Type s, Warner warn) {
    if (t == s)
        return true;

    if (t.isPrimitive() != s.isPrimitive())
        return allowBoxing && (
                isConvertible(t, s, warn)
                || (allowObjectToPrimitiveCast &&
                    s.isPrimitive() &&
                    isSubtype(boxedClass(s).type, t)));
    if (warn != warnStack.head) {
        try {
            warnStack = warnStack.prepend(warn);
            checkUnsafeVarargsConversion(t, s, warn);
            return isCastable.visit(t,s);
        } finally {
            warnStack = warnStack.tail;
        }
    } else {
        return isCastable.visit(t,s);
    }
}
 
Example #4
Source File: Types.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Boolean visitArrayType(ArrayType t, Type s) {
    switch (s.tag) {
    case ERROR:
    case BOT:
        return true;
    case TYPEVAR:
        if (isCastable(s, t, Warner.noWarnings)) {
            warnStack.head.warn(LintCategory.UNCHECKED);
            return true;
        } else {
            return false;
        }
    case CLASS:
        return isSubtype(t, s);
    case ARRAY:
        if (elemtype(t).tag <= lastBaseTag ||
                elemtype(s).tag <= lastBaseTag) {
            return elemtype(t).tag == elemtype(s).tag;
        } else {
            return visit(elemtype(t), elemtype(s));
        }
    default:
        return false;
    }
}
 
Example #5
Source File: Types.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Boolean visitTypeVar(TypeVar t, Type s) {
    switch (s.tag) {
    case ERROR:
    case BOT:
        return true;
    case TYPEVAR:
        if (isSubtype(t, s)) {
            return true;
        } else if (isCastable(t.bound, s, Warner.noWarnings)) {
            warnStack.head.warn(LintCategory.UNCHECKED);
            return true;
        } else {
            return false;
        }
    default:
        return isCastable(t.bound, s, warnStack.head);
    }
}
 
Example #6
Source File: ExpressionTemplate.java    From Refaster with Apache License 2.0 6 votes vote down vote up
@Override
@Nullable
public Unifier unify(JCExpression target, Unifier unifier) {
  unifier = expression().unify(target, unifier);
  if (unifier != null) {
    Inliner inliner = unifier.createInliner();
    try {
      List<Type> expectedTypes = expectedTypes(inliner);
      List<Type> actualTypes = actualTypes(inliner);
      /*
       * The Java compiler's type inference doesn't directly take into account the expected
       * return type, so we test the return type by treating the expected return type as an
       * extra method argument, and the actual type of the return expression as its actual
       * value.
       */
      expectedTypes = expectedTypes.prepend(returnType().inline(inliner));
      actualTypes = actualTypes.prepend(target.type);

      return typecheck(unifier, inliner, new Warner(target), expectedTypes, actualTypes);
    } catch (CouldNotResolveImportException e) {
      logger.log(FINE, "Failure to resolve import", e);
    }
  }
  return null;
}
 
Example #7
Source File: InferenceContext.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Instantiate inference variables in legacy mode (JLS 15.12.2.7, 15.12.2.8).
 * During overload resolution, instantiation is done by doing a partial
 * inference process using eq/lower bound instantiation. During check,
 * we also instantiate any remaining vars by repeatedly using eq/upper
 * instantiation, until all variables are solved.
 */
public void solveLegacy(boolean partial, Warner warn, EnumSet<InferenceStep> steps) {
    while (true) {
        List<Type> solvedVars = solveBasic(steps);
        if (restvars().isEmpty() || partial) {
            //all variables have been instantiated - exit
            break;
        } else if (solvedVars.isEmpty()) {
            //some variables could not be instantiated because of cycles in
            //upper bounds - provide a (possibly recursive) default instantiation
            infer.instantiateAsUninferredVars(restvars(), this);
            break;
        } else {
            //some variables have been instantiated - replace newly instantiated
            //variables in remaining upper bounds and continue
            for (Type t : undetvars) {
                UndetVar uv = (UndetVar)t;
                uv.substBounds(solvedVars, asInstTypes(solvedVars), types);
            }
        }
    }
    infer.doIncorporation(this, warn);
}
 
Example #8
Source File: Types.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean visitTypeVar(TypeVar t, Type s) {
    switch (s.tag) {
    case ERROR:
    case BOT:
        return true;
    case TYPEVAR:
        if (isSubtype(t, s)) {
            return true;
        } else if (isCastable(t.bound, s, Warner.noWarnings)) {
            warnStack.head.warn(LintCategory.UNCHECKED);
            return true;
        } else {
            return false;
        }
    default:
        return isCastable(t.bound, s, warnStack.head);
    }
}
 
Example #9
Source File: Types.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean visitArrayType(ArrayType t, Type s) {
    switch (s.tag) {
    case ERROR:
    case BOT:
        return true;
    case TYPEVAR:
        if (isCastable(s, t, Warner.noWarnings)) {
            warnStack.head.warn(LintCategory.UNCHECKED);
            return true;
        } else {
            return false;
        }
    case CLASS:
        return isSubtype(t, s);
    case ARRAY:
        if (elemtype(t).tag <= lastBaseTag ||
                elemtype(s).tag <= lastBaseTag) {
            return elemtype(t).tag == elemtype(s).tag;
        } else {
            return visit(elemtype(t), elemtype(s));
        }
    default:
        return false;
    }
}
 
Example #10
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Check bounds and perform incorporation.
 */
void doIncorporation(InferenceContext inferenceContext, Warner warn) throws InferenceException {
    try {
        boolean progress = true;
        int round = 0;
        while (progress && round < MAX_INCORPORATION_STEPS) {
            progress = false;
            for (Type t : inferenceContext.undetvars) {
                UndetVar uv = (UndetVar)t;
                if (!uv.incorporationActions.isEmpty()) {
                    progress = true;
                    uv.incorporationActions.removeFirst().apply(inferenceContext, warn);
                }
            }
            round++;
        }
    } finally {
        incorporationCache.clear();
    }
}
 
Example #11
Source File: Types.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
public boolean returnTypeSubstitutable(Type r1,
                                       Type r2, Type r2res,
                                       Warner warner) {
    if (isSameType(r1.getReturnType(), r2res))
        return true;
    if (r1.getReturnType().isPrimitive() || r2res.isPrimitive())
        return false;

    if (hasSameArgs(r1, r2))
        return covariantReturnType(r1.getReturnType(), r2res, warner);
    if (!allowCovariantReturns)
        return false;
    if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner))
        return true;
    if (!isSubtype(r1.getReturnType(), erasure(r2res)))
        return false;
    warner.warn(LintCategory.UNCHECKED);
    return true;
}
 
Example #12
Source File: Types.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private void checkUnsafeVarargsConversion(Type t, Type s, Warner warn) {
    if (t.tag != ARRAY || isReifiable(t)) return;
    ArrayType from = (ArrayType)t;
    boolean shouldWarn = false;
    switch (s.tag) {
        case ARRAY:
            ArrayType to = (ArrayType)s;
            shouldWarn = from.isVarargs() &&
                    !to.isVarargs() &&
                    !isReifiable(from);
            break;
        case CLASS:
            shouldWarn = from.isVarargs();
            break;
    }
    if (shouldWarn) {
        warn.warn(LintCategory.VARARGS);
    }
}
 
Example #13
Source File: Types.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
private void checkUnsafeVarargsConversion(Type t, Type s, Warner warn) {
    if (t.tag != ARRAY || isReifiable(t)) return;
    ArrayType from = (ArrayType)t;
    boolean shouldWarn = false;
    switch (s.tag) {
        case ARRAY:
            ArrayType to = (ArrayType)s;
            shouldWarn = from.isVarargs() &&
                    !to.isVarargs() &&
                    !isReifiable(from);
            break;
        case CLASS:
            shouldWarn = from.isVarargs();
            break;
    }
    if (shouldWarn) {
        warn.warn(LintCategory.VARARGS);
    }
}
 
Example #14
Source File: BlockTemplate.java    From Refaster with Apache License 2.0 6 votes vote down vote up
@Nullable
private Unifier match(List<JCStatement> targetStatements, Context context) {
  checkArgument(templateStatements().size() == targetStatements.size());
  Unifier unifier = new Unifier(context);
  for (int i = 0; i < templateStatements().size() && unifier != null; i++) {
    unifier = templateStatements().get(i).unify(targetStatements.get(i), unifier);
  }
  if (unifier != null) {
    Inliner inliner = unifier.createInliner();
    try {
      return typecheck(
          unifier, inliner, new Warner(targetStatements.get(0)), expectedTypes(inliner),
          actualTypes(inliner));
    } catch (CouldNotResolveImportException e) {
      logger.log(FINE, "Failure to resolve import", e);
    }
  }
  return null;
}
 
Example #15
Source File: InferenceContext.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Instantiate inference variables in legacy mode (JLS 15.12.2.7, 15.12.2.8).
 * During overload resolution, instantiation is done by doing a partial
 * inference process using eq/lower bound instantiation. During check,
 * we also instantiate any remaining vars by repeatedly using eq/upper
 * instantiation, until all variables are solved.
 */
public void solveLegacy(boolean partial, Warner warn, EnumSet<InferenceStep> steps) {
    while (true) {
        List<Type> solvedVars = solveBasic(steps);
        if (restvars().isEmpty() || partial) {
            //all variables have been instantiated - exit
            break;
        } else if (solvedVars.isEmpty()) {
            //some variables could not be instantiated because of cycles in
            //upper bounds - provide a (possibly recursive) default instantiation
            infer.instantiateAsUninferredVars(restvars(), this);
            break;
        } else {
            //some variables have been instantiated - replace newly instantiated
            //variables in remaining upper bounds and continue
            for (Type t : undetvars) {
                UndetVar uv = (UndetVar)t;
                uv.substBounds(solvedVars, asInstTypes(solvedVars), types);
            }
        }
    }
    infer.doIncorporation(this, warn);
}
 
Example #16
Source File: Template.java    From Refaster with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the inferred method type of the template based on the given actual argument types.
 *
 * @throws NoInstanceException if no instances of the specified type variables would allow the
 *         {@code actualArgTypes} to match the {@code expectedArgTypes}
 */
private Type infer(Warner warner,
    Inliner inliner,
    List<Type> freeTypeVariables,
    List<Type> expectedArgTypes,
    Type returnType,
    List<Type> actualArgTypes) throws NoInstanceException {
  Symtab symtab = inliner.symtab();
  MethodType methodType =
      new MethodType(expectedArgTypes, returnType, List.<Type>nil(), symtab.methodClass);
  Enter enter = inliner.enter();
  MethodSymbol methodSymbol =
      new MethodSymbol(0, inliner.asName("__m__"), methodType, symtab.unknownSymbol);
  return inliner.infer().instantiateMethod(enter.getEnv(methodType.tsym),
      freeTypeVariables,
      methodType,
      methodSymbol,
      actualArgTypes,
      autoboxing(),
      false,
      warner);
}
 
Example #17
Source File: InferenceContext.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Solve at least one variable in given list.
 */
public void solveAny(List<Type> varsToSolve, Warner warn) {
    solve(infer.new BestLeafSolver(varsToSolve.intersect(restvars())) {
        public boolean done() {
            return instvars().intersect(varsToSolve).nonEmpty();
        }
    }, warn);
}
 
Example #18
Source File: Types.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private boolean sideCastFinal(Type from, Type to, Warner warn) {
    // We are casting from type $from$ to type $to$, which are
    // unrelated types one of which is final and the other of
    // which is an interface.  This method
    // tries to reject a cast by transferring type parameters
    // from the final class to the interface.
    boolean reverse = false;
    Type target = to;
    if ((to.tsym.flags() & INTERFACE) == 0) {
        Assert.check((from.tsym.flags() & INTERFACE) != 0);
        reverse = true;
        to = from;
        from = target;
    }
    Assert.check((from.tsym.flags() & FINAL) != 0);
    Type t1 = asSuper(from, to.tsym);
    if (t1 == null) return false;
    Type t2 = to;
    if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
        return false;
    if (!allowCovariantReturns)
        // reject if there is a common method signature with
        // incompatible return types.
        chk.checkCompatibleAbstracts(warn.pos(), from, to);
    if (!isReifiable(target) &&
        (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2)))
        warn.warn(LintCategory.UNCHECKED);
    return true;
}
 
Example #19
Source File: Types.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Is t an appropriate return type in an overrider for a
 * method that returns s?
 */
public boolean covariantReturnType(Type t, Type s, Warner warner) {
    return
        isSameType(t, s) ||
        allowCovariantReturns &&
        !t.isPrimitive() &&
        !s.isPrimitive() &&
        isAssignable(t, s, warner);
}
 
Example #20
Source File: Types.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Is t assignable to s?<br>
 * Equivalent to subtype except for constant values and raw
 * types.<br>
 * (not defined for Method and ForAll types)
 */
public boolean isAssignable(Type t, Type s, Warner warn) {
    if (t.tag == ERROR)
        return true;
    if (t.tag <= INT && t.constValue() != null) {
        int value = ((Number)t.constValue()).intValue();
        switch (s.tag) {
        case BYTE:
            if (Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE)
                return true;
            break;
        case CHAR:
            if (Character.MIN_VALUE <= value && value <= Character.MAX_VALUE)
                return true;
            break;
        case SHORT:
            if (Short.MIN_VALUE <= value && value <= Short.MAX_VALUE)
                return true;
            break;
        case INT:
            return true;
        case CLASS:
            switch (unboxedType(s).tag) {
            case BYTE:
            case CHAR:
            case SHORT:
                return isAssignable(t, unboxedType(s), warn);
            }
            break;
        }
    }
    return isConvertible(t, s, warn);
}
 
Example #21
Source File: Types.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return-Type-Substitutable.
 * @jls section 8.4.5
 */
public boolean returnTypeSubstitutable(Type r1, Type r2) {
    if (hasSameArgs(r1, r2))
        return resultSubtype(r1, r2, Warner.noWarnings);
    else
        return covariantReturnType(r1.getReturnType(),
                                   erasure(r2.getReturnType()),
                                   Warner.noWarnings);
}
 
Example #22
Source File: Types.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * This relation answers the question: is impossible that
 * something of type `t' can be a subtype of `s'? This is
 * different from the question "is `t' not a subtype of `s'?"
 * when type variables are involved: Integer is not a subtype of T
 * where <T extends Number> but it is not true that Integer cannot
 * possibly be a subtype of T.
 */
public boolean notSoftSubtype(Type t, Type s) {
    if (t == s) return false;
    if (t.tag == TYPEVAR) {
        TypeVar tv = (TypeVar) t;
        return !isCastable(tv.bound,
                           relaxBound(s),
                           Warner.noWarnings);
    }
    if (s.tag != WILDCARD)
        s = upperBound(s);

    return !isSubtype(t, relaxBound(s));
}
 
Example #23
Source File: Types.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Are corresponding elements of ts subtypes of ss, allowing
 * unchecked conversions?  If lists are of different length,
 * return false.
 **/
public boolean isSubtypesUnchecked(List<Type> ts, List<Type> ss, Warner warn) {
    while (ts.tail != null && ss.tail != null
           /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
           isSubtypeUnchecked(ts.head, ss.head, warn)) {
        ts = ts.tail;
        ss = ss.tail;
    }
    return ts.tail == null && ss.tail == null;
    /*inlined: ts.isEmpty() && ss.isEmpty();*/
}
 
Example #24
Source File: InferenceContext.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Solve all variables in the given list.
 */
public void solve(final List<Type> vars, Warner warn) {
    solve(infer.new BestLeafSolver(vars) {
        public boolean done() {
            return !free(asInstTypes(vars));
        }
    }, warn);
}
 
Example #25
Source File: InferenceContext.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Solve all variables in this context.
 */
public void solve(Warner warn) {
    solve(infer.new LeafSolver() {
        public boolean done() {
            return restvars().isEmpty();
        }
    }, warn);
}
 
Example #26
Source File: DeferredAttr.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
DeferredAttrContext(AttrMode mode, Symbol msym, MethodResolutionPhase phase,
        InferenceContext inferenceContext, DeferredAttrContext parent, Warner warn) {
    this.mode = mode;
    this.msym = msym;
    this.phase = phase;
    this.parent = parent;
    this.warn = warn;
    this.inferenceContext = inferenceContext;
}
 
Example #27
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn) {
    IncorporationBinaryOp newOp = new IncorporationBinaryOp(opKind, op1, op2);
    Boolean res = incorporationCache.get(newOp);
    if (res == null) {
        incorporationCache.put(newOp, res = newOp.apply(warn));
    }
    return res;
}
 
Example #28
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
void apply(InferenceContext inferenceContext, Warner warn) {
    List<Type> boundList = StreamSupport.stream(uv.getBounds(InferenceBound.UPPER))
            .collect(types.closureCollector(true, types::isSameType));
    for (Type b2 : boundList) {
        if (t == b2) continue;
            /* This wildcard check is temporary workaround. This code may need to be
             * revisited once spec bug JDK-7034922 is fixed.
             */
        if (t != b2 && !t.hasTag(WILDCARD) && !b2.hasTag(WILDCARD)) {
            for (Pair<Type, Type> commonSupers : getParameterizedSupers(t, b2)) {
                List<Type> allParamsSuperBound1 = commonSupers.fst.allparams();
                List<Type> allParamsSuperBound2 = commonSupers.snd.allparams();
                while (allParamsSuperBound1.nonEmpty() && allParamsSuperBound2.nonEmpty()) {
                    //traverse the list of all params comparing them
                    if (!allParamsSuperBound1.head.hasTag(WILDCARD) &&
                            !allParamsSuperBound2.head.hasTag(WILDCARD)) {
                        if (!isSameType(inferenceContext.asUndetVar(allParamsSuperBound1.head),
                                inferenceContext.asUndetVar(allParamsSuperBound2.head))) {
                            reportBoundError(uv, InferenceBound.UPPER);
                        }
                    }
                    allParamsSuperBound1 = allParamsSuperBound1.tail;
                    allParamsSuperBound2 = allParamsSuperBound2.tail;
                }
                Assert.check(allParamsSuperBound1.isEmpty() && allParamsSuperBound2.isEmpty());
            }
        }
    }
}
 
Example #29
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
void apply(InferenceContext inferenceContext, Warner warn) {
    for (Type undet : inferenceContext.undetvars) {
        //we could filter out variables not mentioning uv2...
        UndetVar uv2 = (UndetVar)undet;
        uv2.substBounds(List.of(uv.qtype), List.of(uv.getInst()), types);
        checkCompatibleUpperBounds(uv2, inferenceContext);
    }
    super.apply(inferenceContext, warn);
}
 
Example #30
Source File: Infer.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Is source type 's' compatible with target type 't' given source and target bound kinds?
 */
boolean checkBound(Type s, Type t, InferenceBound ib_s, InferenceBound ib_t, Warner warn) {
    if (ib_s.lessThan(ib_t)) {
        return isSubtype(s, t, warn);
    } else if (ib_t.lessThan(ib_s)) {
        return isSubtype(t, s, warn);
    } else {
        return isSameType(s, t);
    }
}