Search in sources :

Example 61 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.

the class UTemplater method visitIdentifier.

@Override
public UExpression visitIdentifier(IdentifierTree tree, Void v) {
    Symbol sym = ASTHelpers.getSymbol(tree);
    if (sym instanceof ClassSymbol) {
        return UClassIdent.create((ClassSymbol) sym);
    } else if (sym != null && sym.isStatic()) {
        return staticMember(sym);
    } else if (freeVariables.containsKey(tree.getName().toString())) {
        VarSymbol symbol = freeVariables.get(tree.getName().toString());
        checkState(symbol == sym);
        UExpression ident = UFreeIdent.create(tree.getName());
        Matches matches = ASTHelpers.getAnnotation(symbol, Matches.class);
        if (matches != null) {
            ident = UMatches.create(getValue(matches), /* positive= */
            true, ident);
        }
        NotMatches notMatches = ASTHelpers.getAnnotation(symbol, NotMatches.class);
        if (notMatches != null) {
            ident = UMatches.create(getValue(notMatches), /* positive= */
            false, ident);
        }
        OfKind hasKind = ASTHelpers.getAnnotation(symbol, OfKind.class);
        if (hasKind != null) {
            EnumSet<Kind> allowed = EnumSet.copyOf(Arrays.asList(hasKind.value()));
            ident = UOfKind.create(ident, ImmutableSet.copyOf(allowed));
        }
        // @Repeated annotations need to be checked last.
        Repeated repeated = ASTHelpers.getAnnotation(symbol, Repeated.class);
        if (repeated != null) {
            ident = URepeated.create(tree.getName(), ident);
        }
        return ident;
    }
    if (sym == null) {
        return UTypeVarIdent.create(tree.getName());
    }
    switch(sym.getKind()) {
        case TYPE_PARAMETER:
            return UTypeVarIdent.create(tree.getName());
        default:
            return ULocalVarIdent.create(tree.getName());
    }
}
Also used : Matches(com.google.errorprone.refaster.annotation.Matches) NotMatches(com.google.errorprone.refaster.annotation.NotMatches) OfKind(com.google.errorprone.refaster.annotation.OfKind) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Kind(com.sun.source.tree.Tree.Kind) OfKind(com.google.errorprone.refaster.annotation.OfKind) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Repeated(com.google.errorprone.refaster.annotation.Repeated) NotMatches(com.google.errorprone.refaster.annotation.NotMatches)

Example 62 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project mockito by mockito.

the class AbstractMockitoAnyForPrimitiveType method getParameterType.

/**
 * Get the type of the parameter for a supplied argument.
 *
 * @param method the method symbol that is being called.
 * @param argumentIndex the index of the argument, can be greater than the number of parameters
 *     for a var arg method.
 * @return the type of the associated parameter.
 */
private Type getParameterType(MethodSymbol method, int argumentIndex) {
    List<VarSymbol> parameters = method.getParameters();
    Type parameterType;
    int parameterCount = parameters.size();
    if (argumentIndex >= parameterCount && method.isVarArgs()) {
        VarSymbol varArgParameter = parameters.get(parameterCount - 1);
        parameterType = ((ArrayType) varArgParameter.asType()).getComponentType();
    } else {
        parameterType = parameters.get(argumentIndex).asType();
    }
    return parameterType;
}
Also used : ArrayType(com.sun.tools.javac.code.Type.ArrayType) Type(com.sun.tools.javac.code.Type) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 63 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project ceylon-compiler by ceylon.

the class Attr method checkSerialVersionUID.

/**
 * Check that an appropriate serialVersionUID member is defined.
 */
private void checkSerialVersionUID(JCClassDecl tree, ClassSymbol c) {
    // check for presence of serialVersionUID
    Scope.Entry e = c.members().lookup(names.serialVersionUID);
    while (e.scope != null && e.sym.kind != VAR) e = e.next();
    if (e.scope == null) {
        log.warning(LintCategory.SERIAL, tree.pos(), "missing.SVUID", c);
        return;
    }
    // check that it is static final
    VarSymbol svuid = (VarSymbol) e.sym;
    if ((svuid.flags() & (STATIC | FINAL)) != (STATIC | FINAL))
        log.warning(LintCategory.SERIAL, TreeInfo.diagnosticPositionFor(svuid, tree), "improper.SVUID", c);
    else // check that it is long
    if (svuid.type.tag != TypeTags.LONG)
        log.warning(LintCategory.SERIAL, TreeInfo.diagnosticPositionFor(svuid, tree), "long.SVUID", c);
    else // check constant
    if (svuid.getConstValue() == null)
        log.warning(LintCategory.SERIAL, TreeInfo.diagnosticPositionFor(svuid, tree), "constant.SVUID", c);
}
Also used : Scope(com.sun.tools.javac.code.Scope) DelegatedScope(com.sun.tools.javac.code.Scope.DelegatedScope) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 64 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project ceylon-compiler by ceylon.

the class Attr method checkId.

/**
 * Determine type of identifier or select expression and check that
 *  (1) the referenced symbol is not deprecated
 *  (2) the symbol's type is safe (@see checkSafe)
 *  (3) if symbol is a variable, check that its type and kind are
 *      compatible with the prototype and protokind.
 *  (4) if symbol is an instance field of a raw type,
 *      which is being assigned to, issue an unchecked warning if its
 *      type changes under erasure.
 *  (5) if symbol is an instance method of a raw type, issue an
 *      unchecked warning if its argument types change under erasure.
 *  If checks succeed:
 *    If symbol is a constant, return its constant type
 *    else if symbol is a method, return its result type
 *    otherwise return its type.
 *  Otherwise return errType.
 *
 *  @param tree       The syntax tree representing the identifier
 *  @param site       If this is a select, the type of the selected
 *                    expression, otherwise the type of the current class.
 *  @param sym        The symbol representing the identifier.
 *  @param env        The current environment.
 *  @param pkind      The set of expected kinds.
 *  @param pt         The expected type.
 */
Type checkId(JCTree tree, Type site, Symbol sym, Env<AttrContext> env, int pkind, Type pt, boolean useVarargs) {
    if (pt.isErroneous())
        return types.createErrorType(site);
    // The computed type of this identifier occurrence.
    Type owntype;
    switch(sym.kind) {
        case TYP:
            // For types, the computed type equals the symbol's type,
            // except for two situations:
            owntype = sym.type;
            if (owntype.tag == CLASS) {
                Type ownOuter = owntype.getEnclosingType();
                // We recover generic outer type later in visitTypeApply.
                if (owntype.tsym.type.getTypeArguments().nonEmpty()) {
                    owntype = types.erasure(owntype);
                } else // Tree<Point>.Visitor.
                if (ownOuter.tag == CLASS && site != ownOuter) {
                    Type normOuter = site;
                    if (normOuter.tag == CLASS)
                        normOuter = types.asEnclosingSuper(site, ownOuter.tsym);
                    if (// perhaps from an import
                    normOuter == null)
                        normOuter = types.erasure(ownOuter);
                    if (normOuter != ownOuter)
                        owntype = new ClassType(normOuter, List.<Type>nil(), owntype.tsym);
                }
            }
            break;
        case VAR:
            VarSymbol v = (VarSymbol) sym;
            // its type changes under erasure.
            if (allowGenerics && pkind == VAR && v.owner.kind == TYP && (v.flags() & STATIC) == 0 && (site.tag == CLASS || site.tag == TYPEVAR)) {
                Type s = types.asOuterSuper(site, v.owner);
                if (s != null && s.isRaw() && !types.isSameType(v.type, v.erasure(types))) {
                    chk.warnUnchecked(tree.pos(), "unchecked.assign.to.var", v, s);
                }
            }
            // The computed type of a variable is the type of the
            // variable symbol, taken as a member of the site type.
            owntype = (sym.owner.kind == TYP && sym.name != names._this && sym.name != names._super) ? types.memberType(site, sym) : sym.type;
            if (env.info.tvars.nonEmpty()) {
                Type owntype1 = new ForAll(env.info.tvars, owntype);
                for (List<Type> l = env.info.tvars; l.nonEmpty(); l = l.tail) if (!owntype.contains(l.head)) {
                    log.error(tree.pos(), "undetermined.type", owntype1);
                    owntype1 = types.createErrorType(owntype1);
                }
                owntype = owntype1;
            }
            // computed type.
            if (v.getConstValue() != null && isStaticReference(tree))
                owntype = owntype.constType(v.getConstValue());
            if (pkind == VAL) {
                // capture "names as expressions"
                owntype = capture(owntype);
            }
            break;
        case MTH:
            {
                JCMethodInvocation app = (JCMethodInvocation) env.tree;
                owntype = checkMethod(site, sym, env, app.args, pt.getParameterTypes(), pt.getTypeArguments(), env.info.varArgs);
                break;
            }
        case PCK:
        case ERR:
            owntype = sym.type;
            break;
        default:
            throw new AssertionError("unexpected kind: " + sym.kind + " in tree " + tree);
    }
    if (sym.name != names.init) {
        chk.checkDeprecated(tree.pos(), env.info.scope.owner, sym);
        chk.checkSunAPI(tree.pos(), sym);
    }
    // kind are compatible with the prototype and protokind.
    return check(tree, owntype, sym.kind, pkind, pt);
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) ForAll(com.sun.tools.javac.code.Type.ForAll)

Example 65 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project ceylon-compiler by ceylon.

the class Attr method enumConstant.

// where
/**
 * Return the selected enumeration constant symbol, or null.
 */
private Symbol enumConstant(JCTree tree, Type enumType) {
    if (tree.getTag() != JCTree.IDENT) {
        log.error(tree.pos(), "enum.label.must.be.unqualified.enum");
        return syms.errSymbol;
    }
    JCIdent ident = (JCIdent) tree;
    Name name = ident.name;
    for (Scope.Entry e = enumType.tsym.members().lookup(name); e.scope != null; e = e.next()) {
        if (e.sym.kind == VAR) {
            Symbol s = ident.sym = e.sym;
            // ensure initializer is evaluated
            ((VarSymbol) s).getConstValue();
            ident.type = s.type;
            return ((s.flags_field & Flags.ENUM) == 0) ? null : s;
        }
    }
    return null;
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) Scope(com.sun.tools.javac.code.Scope) DelegatedScope(com.sun.tools.javac.code.Scope.DelegatedScope) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) DynamicMethodSymbol(com.sun.tools.javac.code.Symbol.DynamicMethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) OperatorSymbol(com.sun.tools.javac.code.Symbol.OperatorSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Kinds.kindName(com.sun.tools.javac.code.Kinds.kindName) Name(com.sun.tools.javac.util.Name)

Aggregations

VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)68 Symbol (com.sun.tools.javac.code.Symbol)29 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)23 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)22 Type (com.sun.tools.javac.code.Type)20 ExpressionTree (com.sun.source.tree.ExpressionTree)15 Tree (com.sun.source.tree.Tree)15 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)13 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)11 VariableTree (com.sun.source.tree.VariableTree)11 ClassTree (com.sun.source.tree.ClassTree)10 IdentifierTree (com.sun.source.tree.IdentifierTree)10 MemberSelectTree (com.sun.source.tree.MemberSelectTree)10 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)10 NewClassTree (com.sun.source.tree.NewClassTree)9 ArrayList (java.util.ArrayList)9 VisitorState (com.google.errorprone.VisitorState)7 MethodTree (com.sun.source.tree.MethodTree)7 TreePath (com.sun.source.util.TreePath)7 ClassType (com.sun.tools.javac.code.Type.ClassType)7