private static boolean maybeFunctionalInterface(Type type, Types types, VisitorState state) { try { return types.isFunctionalInterface(type); } catch (CompletionFailure e) { // Report completion errors to avoid e.g. https://github.com/bazelbuild/bazel/issues/4105 Check.instance(state.context) .completionError((DiagnosticPosition) state.getPath().getLeaf(), e); return false; } } }
Symbol on) { T firstOccurrence = annotations.head; List<Attribute> repeated = List.nil(); Type origAnnoType = null; Type arrayOfOrigAnnoType = null; MethodSymbol containerValueSymbol = null; Assert.check(!annotations.isEmpty() && !annotations.tail.isEmpty()); // i.e. size() > 1 JCAnnotation annoTree = m.Annotation(c); if (!chk.annotationApplicable(annoTree, on)) log.error(annoTree.pos(), "invalid.repeatable.annotation.incompatible.target", targetContainerType, origAnnoType); if (!chk.validateAnnotationDeferErrors(annoTree)) log.error(annoTree.pos(), "duplicate.annotation.invalid.repeated", origAnnoType);
a.type = chk.checkType(a.annotationType.pos(), at, expected); if (a.type.isErroneous()) return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil()); if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) { log.error(a.annotationType.pos(), "not.annotation.type", a.type.toString()); return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil()); if (args.length() == 1 && args.head.tag != JCTree.ASSIGN) { if (method.owner != a.type.tsym) log.error(left.pos(), "no.annotation.member", left.name, a.type); Type result = method.type.getReturnType(); Attribute value = enterAttributeValue(result, assign.rhs, env); if (!method.type.isErroneous())
(other.flags() & STATIC) == 0) { log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.static", cannotOverride(m, other)); return; (other.flags() & STATIC) != 0) { log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.meth", cannotOverride(m, other), TreeInfo.flagNames(other.flags() & (FINAL | STATIC))); return; protection(m.flags()) > protection(other.flags())) { log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.weaker.access", cannotOverride(m, other), protectionString(other.flags())); return; typeError(TreeInfo.diagnosticPositionFor(m, tree), JCDiagnostic.fragment("override.incompatible.ret", cannotOverride(m, other)), mtres, otres); return; warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree), "prob.found.req", JCDiagnostic.fragment("override.unchecked.ret", uncheckedOverrides(m, other)), mtres, otres);
(other.flags() & STATIC) == 0) { log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.static", cannotOverride(m, other)); m.flags_field |= BAD_OVERRIDE; return; (other.flags() & STATIC) != 0) { log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.meth", cannotOverride(m, other), asFlagSet(other.flags() & (FINAL | STATIC))); m.flags_field |= BAD_OVERRIDE; protection(m.flags()) > protection(other.flags())) { log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.weaker.access", cannotOverride(m, other), other.flags() == 0 ? "package" : log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.incompatible.ret", cannotOverride(m, other), mtres, otres); m.flags_field |= BAD_OVERRIDE; warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree), "override.unchecked.ret", uncheckedOverrides(m, other), mtres, otres);
classDollar, new MethodType( List.of(syms.stringType), types.erasure(syms.classType), List.<Type>nil(), syms.methodClass), outerCacheClass); md.body = classDollarSymBody(pos, md); } catch (CompletionFailure ex) { md.body = make.Block(0, List.<JCStatement>nil()); chk.completionError(pos, ex);
chk.validateAnnotations(tree.mods.annotations, c); chk.validateTypeParams(tree.typarams); chk.validate(tree.extending); chk.validate(tree.implementing); chk.checkAllDefined(tree.pos(), c); chk.checkCompatibleSupertypes(tree.pos(), c.type); chk.checkClassBounds(tree.pos(), c.type); if (assertsEnabled) { for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail) assert env.info.scope.lookup(l.head.name).scope != null; if (!c.type.allparams().isEmpty() && types.isSubtype(c.type, syms.throwableType)) log.error(tree.extending.pos(), "generic.throwable"); chk.checkImplementations(tree); for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { chk.checkCyclicConstructors(tree); chk.checkNonCyclicElements(tree);
if (!c.isAnonymous()) { chk.validate(tree.typarams, env); chk.validate(tree.extending, env); chk.validate(tree.implementing, env); chk.checkAllDefined(tree.pos(), c); Assert.checkNonNull(cbPos); chk.validateRepeatable(c, repeatable, cbPos); chk.checkCompatibleSupertypes(tree.pos(), c.type); if (allowDefaultMethods) { chk.checkDefaultMethodClashes(tree.pos(), c.type); chk.checkClassBounds(tree.pos(), c.type); l.nonEmpty(); l = l.tail) { Assert.checkNonNull(env.info.scope.lookup(l.head.name).scope); chk.checkImplementations(tree); chk.checkCyclicConstructors(tree); chk.checkNonCyclicElements(tree);
public void visitForeachLoop(JCEnhancedForLoop tree) { Env<AttrContext> loopEnv = env.dup(env.tree, env.info.dup(env.info.scope.dup())); attribStat(tree.var, loopEnv); Type exprType = types.upperBound(attribExpr(tree.expr, loopEnv)); chk.checkNonVoid(tree.pos(), exprType); Type elemtype = types.elemtype(exprType); // perhaps expr is an array? if (elemtype == null) { // or perhaps expr implements Iterable<T>? Type base = types.asSuper(exprType, syms.iterableType.tsym); if (base == null) { log.error(tree.expr.pos(), "foreach.not.applicable.to.type"); elemtype = syms.errType; } else { List<Type> iterableParams = base.allparams(); elemtype = iterableParams.isEmpty() ? syms.objectType : types.upperBound(iterableParams.head); } } chk.checkType(tree.expr.pos(), elemtype, tree.var.sym.type); loopEnv.tree = tree; // before, we were not in loop! attribStat(tree.body, loopEnv); loopEnv.info.scope.leave(); result = null; }
public static boolean checkTypesAssignable(CompilationInfo info, TypeMirror from, TypeMirror to) { Context c = ((JavacTaskImpl) info.impl.getJavacTask()).getContext(); if (from.getKind() == TypeKind.TYPEVAR) { Types types = Types.instance(c); TypeVar t = types.substBound((TypeVar)from, com.sun.tools.javac.util.List.of((Type)from), com.sun.tools.javac.util.List.of(types.boxedTypeOrType((Type)to))); return info.getTypes().isAssignable(t.getUpperBound(), to) || info.getTypes().isAssignable(to, t.getUpperBound()); } if (from.getKind() == TypeKind.WILDCARD) { from = Types.instance(c).wildUpperBound((Type)from); } return Check.instance(c).checkType(null, (Type)from, (Type)to).getKind() != TypeKind.ERROR; }
protected Enter(Context context) { context.put(enterKey, this); log = Log.instance(context); reader = ClassReader.instance(context); make = TreeMaker.instance(context); syms = Symtab.instance(context); chk = Check.instance(context); memberEnter = MemberEnter.instance(context); types = Types.instance(context); annotate = Annotate.instance(context); lint = Lint.instance(context); names = Names.instance(context); predefClassDef = make.ClassDef( make.Modifiers(PUBLIC), syms.predefClass.name, List.<JCTypeParameter>nil(), null, List.<JCExpression>nil(), List.<JCTree>nil()); predefClassDef.sym = syms.predefClass; todo = Todo.instance(context); fileManager = context.get(JavaFileManager.class); Options options = Options.instance(context); pkginfoOpt = PkgInfo.get(options); }
seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType); for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) { JCCase c = l.head; Env<AttrContext> caseEnv = if (!pattype.hasTag(ERROR)) { if (pattype.constValue() == null) { log.error(c.pat.pos(), (stringSwitch ? "string.const.req" : "const.expr.req")); } else if (labels.contains(pattype.constValue())) { log.error(c.pos(), "duplicate.case.label"); } else {
public void visitAssignop(JCAssignOp tree) { // Attribute arguments. Type owntype = attribTree(tree.lhs, env, VAR, Type.noType); Type operand = attribExpr(tree.rhs, env); // Find operator. Symbol operator = tree.operator = rs.resolveBinaryOperator( tree.pos(), tree.tag - JCTree.ASGOffset, env, owntype, operand); if (operator.kind == MTH) { chk.checkOperator(tree.pos(), (OperatorSymbol)operator, tree.tag - JCTree.ASGOffset, owntype, operand); if (types.isSameType(operator.type.getReturnType(), syms.stringType)) { // String assignment; make sure the lhs is a string chk.checkType(tree.lhs.pos(), owntype, syms.stringType); } else { chk.checkDivZero(tree.rhs.pos(), operator, operand); chk.checkCastable(tree.rhs.pos(), operator.type.getReturnType(), owntype); } } result = check(tree, owntype, VAL, pkind, pt); }
(seltype.tsym.flags() & Flags.ENUM) != 0; if (!enumSwitch) seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType); for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) { JCCase c = l.head; Env<AttrContext> caseEnv = Type pattype = attribExpr(c.pat, switchEnv, seltype); if (pattype.tag != ERROR) { if (pattype.constValue() == null) { log.error(c.pat.pos(), "const.expr.req"); } else if (labels.contains(pattype.constValue())) { log.error(c.pos(), "duplicate.case.label"); } else { labels.add(pattype.constValue());
void checkNonCyclicElementsInternal(DiagnosticPosition pos, TypeSymbol tsym) { if ((tsym.flags_field & ACYCLIC_ANN) != 0) return; if ((tsym.flags_field & LOCKED) != 0) { log.error(pos, "cyclic.annotation.element"); return; } try { tsym.flags_field |= LOCKED; for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) { Symbol s = e.sym; if (s.kind != Kinds.MTH) continue; checkAnnotationResType(pos, ((MethodSymbol)s).type.getReturnType()); } } finally { tsym.flags_field &= ~LOCKED; tsym.flags_field |= ACYCLIC_ANN; } }
protected Infer(Context context) { context.put(inferKey, this); syms = Symtab.instance(context); types = Types.instance(context); rs = Resolve.instance(context); chk = Check.instance(context); }
Type to, Warner warn) throws InferenceException { List<Type> undetvars = Type.map(that.tvars, fromTypeVarFun); for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail) { UndetVar uv = (UndetVar) l.head; TypeVar tv = (TypeVar)uv.qtype; ListBuffer<Type> hibounds = new ListBuffer<Type>(); for (Type t : that.getConstraints(tv, ConstraintKind.EXTENDS).prependList(types.getBounds(tv))) { if (!t.containsSome(that.tvars) && t.tag != BOT) { hibounds.append(t); if (inst.nonEmpty() && inst.head.tag != BOT) { uv.inst = inst.head; List<Type> targs = Type.map(undetvars, getInstFun); targs = types.subst(targs, that.tvars, targs); checkWithinBounds(that.tvars, targs, warn); return chk.checkType(warn.pos(), that.inst(targs, types), to);
public void visitTry(JCTry tree) { // Attribute body attribStat(tree.body, env.dup(tree, env.info.dup())); // Attribute catch clauses for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) { JCCatch c = l.head; Env<AttrContext> catchEnv = env.dup(c, env.info.dup(env.info.scope.dup())); Type ctype = attribStat(c.param, catchEnv); if (c.param.type.tsym.kind == Kinds.VAR) { c.param.sym.setData(ElementKind.EXCEPTION_PARAMETER); } chk.checkType(c.param.vartype.pos(), chk.checkClassType(c.param.vartype.pos(), ctype), syms.throwableType); attribStat(c.body, catchEnv); catchEnv.info.scope.leave(); } // Attribute finalizer if (tree.finalizer != null) attribStat(tree.finalizer, env); result = null; }
/** Visitor method: generate code for an expression, catching and reporting * any completion failures. * @param tree The expression to be visited. * @param pt The expression's expected type (proto-type). */ public Item genExpr(JCTree tree, Type pt) { Type prevPt = this.pt; try { if (tree.type.constValue() != null) { // Short circuit any expressions which are constants checkStringConstant(tree.pos(), tree.type.constValue()); result = items.makeImmediateItem(tree.type, tree.type.constValue()); } else { this.pt = pt; tree.accept(this); } return result.coerce(pt); } catch (CompletionFailure ex) { chk.completionError(tree.pos(), ex); code.state.stacksize = 1; return items.makeStackItem(pt); } finally { this.pt = prevPt; } }
void checkAnnotationResType(DiagnosticPosition pos, Type type) { switch (type.getTag()) { case CLASS: if ((type.tsym.flags() & ANNOTATION) != 0) checkNonCyclicElementsInternal(pos, type.tsym); break; case ARRAY: checkAnnotationResType(pos, types.elemtype(type)); break; default: break; // int etc } }