Java Code Examples for com.sun.tools.javac.tree.TreeInfo#symbol()

The following examples show how to use com.sun.tools.javac.tree.TreeInfo#symbol() . 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: Check.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
boolean validateTargetAnnotationValue(JCAnnotation a) {
    // special case: java.lang.annotation.Target must not have
    // repeated values in its value member
    if (a.annotationType.type.tsym != syms.annotationTargetType.tsym ||
            a.args.tail == null)
        return true;

    boolean isValid = true;
    if (!a.args.head.hasTag(ASSIGN)) return false; // error recovery
    JCAssign assign = (JCAssign) a.args.head;
    Symbol m = TreeInfo.symbol(assign.lhs);
    if (m.name != names.value) return false;
    JCTree rhs = assign.rhs;
    if (!rhs.hasTag(NEWARRAY)) return false;
    JCNewArray na = (JCNewArray) rhs;
    Set<Symbol> targets = new HashSet<>();
    for (JCTree elem : na.elems) {
        if (!targets.add(TreeInfo.symbol(elem))) {
            isValid = false;
            log.error(elem.pos(), Errors.RepeatedAnnotationTarget);
        }
    }
    return isValid;
}
 
Example 2
Source File: Check.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void checkImportsResolvable(final JCCompilationUnit toplevel) {
    for (final JCImport imp : toplevel.getImports()) {
        if (!imp.staticImport || !imp.qualid.hasTag(SELECT))
            continue;
        final JCFieldAccess select = (JCFieldAccess) imp.qualid;
        final Symbol origin;
        if (select.name == names.asterisk || (origin = TreeInfo.symbol(select.selected)) == null || origin.kind != TYP)
            continue;

        TypeSymbol site = (TypeSymbol) TreeInfo.symbol(select.selected);
        if (!checkTypeContainsImportableElement(site, site, toplevel.packge, select.name, new HashSet<Symbol>())) {
            log.error(imp.pos(),
                      Errors.CantResolveLocation(KindName.STATIC,
                                                 select.name,
                                                 null,
                                                 null,
                                                 Fragments.Location(kindName(site),
                                                                    site,
                                                                    null)));
        }
    }
}
 
Example 3
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void visitThrow(JCThrow tree) {
    scan(tree.expr);
    Symbol sym = TreeInfo.symbol(tree.expr);
    if (sym != null &&
        sym.kind == VAR &&
        (sym.flags() & (FINAL | EFFECTIVELY_FINAL)) != 0 &&
        preciseRethrowTypes.get(sym) != null &&
        allowImprovedRethrowAnalysis) {
        for (Type t : preciseRethrowTypes.get(sym)) {
            markThrown(tree, t);
        }
    }
    else {
        markThrown(tree, tree.expr.type);
    }
    markDead();
}
 
Example 4
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings("fallthrough")
void letInit(JCTree tree) {
    tree = TreeInfo.skipParens(tree);
    if (tree.hasTag(IDENT) || tree.hasTag(SELECT)) {
        Symbol sym = TreeInfo.symbol(tree);
        if (currentTree != null &&
                sym.kind == VAR &&
                sym.owner.kind == MTH &&
                ((VarSymbol)sym).pos < currentTree.getStartPosition()) {
            switch (currentTree.getTag()) {
                case CLASSDEF:
                    if (!allowEffectivelyFinalInInnerClasses) {
                        reportInnerClsNeedsFinalError(tree, sym);
                        break;
                    }
                case LAMBDA:
                    reportEffectivelyFinalError(tree, sym);
            }
        }
    }
}
 
Example 5
Source File: Modules.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visitUses(JCUses tree) {
    Type st = attr.attribType(tree.qualid, env, syms.objectType);
    Symbol sym = TreeInfo.symbol(tree.qualid);
    if ((sym.flags() & ENUM) != 0) {
        log.error(tree.qualid.pos(), Errors.ServiceDefinitionIsEnum(st.tsym));
    } else if (st.hasTag(CLASS)) {
        ClassSymbol service = (ClassSymbol) st.tsym;
        if (allUses.add(service)) {
            Directive.UsesDirective d = new Directive.UsesDirective(service);
            msym.uses = msym.uses.prepend(d);
            msym.directives = msym.directives.prepend(d);
        } else {
            log.error(tree.pos(), Errors.DuplicateUses(service));
        }
    }
}
 
Example 6
Source File: PostFlowAnalysis.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void visitApply(JCMethodInvocation tree) {
    boolean prevCheckThis = checkThis;
    try {
        Symbol meth = TreeInfo.symbol(tree.meth);
        Name methName = TreeInfo.name(tree.meth);
        if (meth != null && meth.name == names.init) {
            Symbol c = meth.owner;
            if (c.hasOuterInstance()) {
                checkThis = false;
                if (tree.meth.getTag() != JCTree.Tag.SELECT && (c.isLocal() || methName == names._this)) {
                    checkThis(tree.meth.pos(), c.type.getEnclosingType().tsym);
                }
            }
        }
        super.visitApply(tree);
    } finally {
        checkThis = prevCheckThis;
    }
}
 
Example 7
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
JCExpression abstractLval(JCExpression lval, final TreeBuilder builder) {
    lval = TreeInfo.skipParens(lval);
    switch (lval.getTag()) {
    case IDENT:
        return builder.build(lval);
    case SELECT: {
        final JCFieldAccess s = (JCFieldAccess)lval;
        Symbol lid = TreeInfo.symbol(s.selected);
        if (lid != null && lid.kind == TYP) return builder.build(lval);
        return abstractRval(s.selected, selected -> builder.build(make.Select(selected, s.sym)));
    }
    case INDEXED: {
        final JCArrayAccess i = (JCArrayAccess)lval;
        return abstractRval(i.indexed, indexed -> abstractRval(i.index, syms.intType, index -> {
            JCExpression newLval = make.Indexed(indexed, index);
            newLval.setType(i.type);
            return builder.build(newLval);
        }));
    }
    case TYPECAST: {
        return abstractLval(((JCTypeCast)lval).expr, builder);
    }
    }
    throw new AssertionError(lval);
}
 
Example 8
Source File: Modules.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void visitUses(JCUses tree) {
    Type st = attr.attribType(tree.qualid, env, syms.objectType);
    Symbol sym = TreeInfo.symbol(tree.qualid);
    if ((sym.flags() & ENUM) != 0) {
        log.error(tree.qualid.pos(), Errors.ServiceDefinitionIsEnum(st.tsym));
    } else if (st.hasTag(CLASS)) {
        ClassSymbol service = (ClassSymbol) st.tsym;
        if (allUses.add(service)) {
            Directive.UsesDirective d = new Directive.UsesDirective(service);
            msym.uses = msym.uses.prepend(d);
            msym.directives = msym.directives.prepend(d);
        } else {
            log.error(tree.pos(), Errors.DuplicateUses(service));
        }
    }
}
 
Example 9
Source File: Annotate.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private Attribute getAnnotationEnumValue(Type expectedElementType, JCExpression tree, Env<AttrContext> env) {
    Type result = attr.attribExpr(tree, env, expectedElementType);
    Symbol sym = TreeInfo.symbol(tree);
    if (sym == null ||
            TreeInfo.nonstaticSelect(tree) ||
            sym.kind != VAR ||
            (sym.flags() & Flags.ENUM) == 0) {
        log.error(tree.pos(), "enum.annotation.must.be.enum.constant");
        return new Attribute.Error(result.getOriginalType());
    }
    VarSymbol enumerator = (VarSymbol) sym;
    return new Attribute.Enum(expectedElementType, enumerator);
}
 
Example 10
Source File: Check.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void checkAccessFromSerializableElement(final JCTree tree, boolean isLambda) {
    if (warnOnAnyAccessToMembers ||
        (lint.isEnabled(LintCategory.SERIAL) &&
        !lint.isSuppressed(LintCategory.SERIAL) &&
        isLambda)) {
        Symbol sym = TreeInfo.symbol(tree);
        if (!sym.kind.matches(KindSelector.VAL_MTH)) {
            return;
        }

        if (sym.kind == VAR) {
            if ((sym.flags() & PARAMETER) != 0 ||
                sym.isLocal() ||
                sym.name == names._this ||
                sym.name == names._super) {
                return;
            }
        }

        if (!types.isSubtype(sym.owner.type, syms.serializableType) &&
            isEffectivelyNonPublic(sym)) {
            if (isLambda) {
                if (belongsToRestrictedPackage(sym)) {
                    log.warning(LintCategory.SERIAL, tree.pos(),
                                Warnings.AccessToMemberFromSerializableLambda(sym));
                }
            } else {
                log.warning(tree.pos(),
                            Warnings.AccessToMemberFromSerializableElement(sym));
            }
        }
    }
}
 
Example 11
Source File: LambdaToMethod.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
LambdaTranslationContext(JCLambda tree) {
    super(tree);
    Frame frame = frameStack.head;
    switch (frame.tree.getTag()) {
        case VARDEF:
            assignedTo = self = ((JCVariableDecl) frame.tree).sym;
            break;
        case ASSIGN:
            self = null;
            assignedTo = TreeInfo.symbol(((JCAssign) frame.tree).getVariable());
            break;
        default:
            assignedTo = self = null;
            break;
     }

    // This symbol will be filled-in in complete
    this.translatedSym = makePrivateSyntheticMethod(0, null, null, owner.enclClass());

    translatedSymbols = new EnumMap<>(LambdaSymbolKind.class);

    translatedSymbols.put(PARAM, new LinkedHashMap<Symbol, Symbol>());
    translatedSymbols.put(LOCAL_VAR, new LinkedHashMap<Symbol, Symbol>());
    translatedSymbols.put(CAPTURED_VAR, new LinkedHashMap<Symbol, Symbol>());
    translatedSymbols.put(CAPTURED_THIS, new LinkedHashMap<Symbol, Symbol>());
    translatedSymbols.put(CAPTURED_OUTER_THIS, new LinkedHashMap<Symbol, Symbol>());
    translatedSymbols.put(TYPE_VAR, new LinkedHashMap<Symbol, Symbol>());

    freeVarProcessedLocalClasses = new HashSet<>();
}
 
Example 12
Source File: Annotate.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Attribute getAnnotationEnumValue(Type expectedElementType, JCExpression tree, Env<AttrContext> env) {
    Type result = attr.attribTree(tree, env, annotationValueInfo(expectedElementType));
    Symbol sym = TreeInfo.symbol(tree);
    if (sym == null ||
            TreeInfo.nonstaticSelect(tree) ||
            sym.kind != VAR ||
            (sym.flags() & Flags.ENUM) == 0) {
        log.error(tree.pos(), Errors.EnumAnnotationMustBeEnumConstant);
        return new Attribute.Error(result.getOriginalType());
    }
    VarSymbol enumerator = (VarSymbol) sym;
    return new Attribute.Enum(expectedElementType, enumerator);
}
 
Example 13
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void visitUnary(JCUnary tree) {
    if (TreeInfo.symbol(tree.arg) == sym) {
        dependencyFound = true;
        return;
    }
    super.visitUnary(tree);
}
 
Example 14
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void visitAssignop(JCAssignOp tree) {
    if (TreeInfo.symbol(tree.lhs) == sym) {
        dependencyFound = true;
        return;
    }
    super.visitAssignop(tree);
}
 
Example 15
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** If tree refers to a superclass constructor call,
 *  add all free variables of the superclass.
 */
public void visitApply(JCMethodInvocation tree) {
    if (TreeInfo.name(tree.meth) == names._super) {
        Symbol constructor = TreeInfo.symbol(tree.meth);
        ClassSymbol c = (ClassSymbol)constructor.owner;
        if (c.hasOuterInstance() &&
            !tree.meth.hasTag(SELECT) &&
            outerThisStack.head != null)
            visitSymbol(outerThisStack.head);
    }
    super.visitApply(tree);
}
 
Example 16
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void visitTry(JCTry tree) {
    for (JCTree resource : tree.resources) {
        if (!resource.hasTag(VARDEF)) {
            Symbol var = TreeInfo.symbol(resource);
            if (var != null && (var.flags() & (FINAL | EFFECTIVELY_FINAL)) == 0) {
                log.error(resource.pos(), Errors.TryWithResourcesExprEffectivelyFinalVar(var));
            }
        }
    }
    super.visitTry(tree);
}
 
Example 17
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** If tree is either a simple name or of the form this.name or
 *  C.this.name, and tree represents a trackable variable,
 *  record an initialization of the variable.
 */
void letInit(JCTree tree) {
    tree = TreeInfo.skipParens(tree);
    if (tree.hasTag(IDENT) || tree.hasTag(SELECT)) {
        Symbol sym = TreeInfo.symbol(tree);
        if (sym.kind == VAR) {
            letInit(tree.pos(), (VarSymbol)sym);
        }
    }
}
 
Example 18
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
AssignopDependencyScanner(JCAssignOp tree) {
    this.sym = TreeInfo.symbol(tree.lhs);
}
 
Example 19
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void visitApply(JCMethodInvocation tree) {
    Symbol meth = TreeInfo.symbol(tree.meth);
    List<Type> argtypes = meth.type.getParameterTypes();
    if (meth.name == names.init && meth.owner == syms.enumSym)
        argtypes = argtypes.tail.tail;
    tree.args = boxArgs(argtypes, tree.args, tree.varargsElement);
    tree.varargsElement = null;
    Name methName = TreeInfo.name(tree.meth);
    if (meth.name==names.init) {
        // We are seeing a this(...) or super(...) constructor call.
        // If an access constructor is used, append null as a last argument.
        Symbol constructor = accessConstructor(tree.pos(), meth);
        if (constructor != meth) {
            tree.args = tree.args.append(makeNull());
            TreeInfo.setSymbol(tree.meth, constructor);
        }

        // If we are calling a constructor of a local class, add
        // free variables after explicit constructor arguments.
        ClassSymbol c = (ClassSymbol)constructor.owner;
        if (c.isLocal()) {
            tree.args = tree.args.appendList(loadFreevars(tree.pos(), freevars(c)));
        }

        // If we are calling a constructor of an enum class, pass
        // along the name and ordinal arguments
        if ((c.flags_field&ENUM) != 0 || c.getQualifiedName() == names.java_lang_Enum) {
            List<JCVariableDecl> params = currentMethodDef.params;
            if (currentMethodSym.owner.hasOuterInstance())
                params = params.tail; // drop this$n
            tree.args = tree.args
                .prepend(make_at(tree.pos()).Ident(params.tail.head.sym)) // ordinal
                .prepend(make.Ident(params.head.sym)); // name
        }

        // If we are calling a constructor of a class with an outer
        // instance, and the call
        // is qualified, pass qualifier as first argument in front of
        // the explicit constructor arguments. If the call
        // is not qualified, pass the correct outer instance as
        // first argument.
        if (c.hasOuterInstance()) {
            JCExpression thisArg;
            if (tree.meth.hasTag(SELECT)) {
                thisArg = attr.
                    makeNullCheck(translate(((JCFieldAccess) tree.meth).selected));
                tree.meth = make.Ident(constructor);
                ((JCIdent) tree.meth).name = methName;
            } else if (c.isLocal() || methName == names._this){
                // local class or this() call
                thisArg = makeThis(tree.meth.pos(), c.type.getEnclosingType().tsym);
            } else {
                // super() call of nested class - never pick 'this'
                thisArg = makeOwnerThisN(tree.meth.pos(), c, false);
            }
            tree.args = tree.args.prepend(thisArg);
        }
    } else {
        // We are seeing a normal method invocation; translate this as usual.
        tree.meth = translate(tree.meth);

        // If the translated method itself is an Apply tree, we are
        // seeing an access method invocation. In this case, append
        // the method arguments to the arguments of the access method.
        if (tree.meth.hasTag(APPLY)) {
            JCMethodInvocation app = (JCMethodInvocation)tree.meth;
            app.args = tree.args.prependList(app.args);
            result = app;
            return;
        }
    }
    result = tree;
}
 
Example 20
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** Ensure that identifier is accessible, return tree accessing the identifier.
 *  @param tree     The identifier tree.
 */
JCExpression access(JCExpression tree) {
    Symbol sym = TreeInfo.symbol(tree);
    return sym == null ? tree : access(sym, tree, null, false);
}