Search in sources :

Example 6 with IdentifierTree

use of com.sun.source.tree.IdentifierTree in project checker-framework by typetools.

the class TreeParserTest method parsesMethodInvocations.

@Test
public void parsesMethodInvocations() {
    String value = "test()";
    ExpressionTree parsed = parser.parseTree(value);
    Assert.assertTrue(parsed instanceof MethodInvocationTree);
    MethodInvocationTree invocation = (MethodInvocationTree) parsed;
    Assert.assertTrue(invocation.getMethodSelect() instanceof IdentifierTree);
    Assert.assertEquals("test", ((IdentifierTree) invocation.getMethodSelect()).getName().toString());
}
Also used : MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Test(org.junit.Test)

Example 7 with IdentifierTree

use of com.sun.source.tree.IdentifierTree in project checker-framework by typetools.

the class AnnotatedTypeFactory method isAnyEnclosingThisDeref.

/**
 * Does this expression have (the innermost or an outer) "this" as receiver? Note that the
 * receiver can be either explicit or implicit.
 *
 * @param tree the tree to test
 * @return true, iff the expression uses (the innermost or an outer) "this" as receiver
 */
public final boolean isAnyEnclosingThisDeref(ExpressionTree tree) {
    if (!TreeUtils.isUseOfElement(tree)) {
        return false;
    }
    ExpressionTree recv = TreeUtils.getReceiverTree(tree);
    if (recv == null) {
        Element element = TreeUtils.elementFromUse(tree);
        if (!ElementUtils.hasReceiver(element)) {
            return false;
        }
        tree = TreeUtils.skipParens(tree);
        if (tree.getKind() == Tree.Kind.IDENTIFIER) {
            Name n = ((IdentifierTree) tree).getName();
            if ("this".contentEquals(n) || "super".contentEquals(n)) {
                // An explicit reference to "this"/"super" has no receiver.
                return false;
            }
        }
        // Must be some access through this.
        return true;
    } else if (!TreeUtils.isUseOfElement(recv)) {
        // The receiver is e.g. a String literal.
        return false;
    // TODO: I think this:
    // (i==9 ? this : this).toString();
    // is not a use of an element, as the receiver is an
    // expression. How should this be handled?
    }
    Element element = TreeUtils.elementFromUse(recv);
    if (!ElementUtils.hasReceiver(element)) {
        return false;
    }
    return TreeUtils.isExplicitThisDereference(recv);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Name(javax.lang.model.element.Name)

Example 8 with IdentifierTree

use of com.sun.source.tree.IdentifierTree in project checker-framework by typetools.

the class FormatterVisitor method isWrappedFormatCall.

/**
 * Returns true if fc is within a method m annotated as {@code @FormatMethod}, and fc's
 * arguments are m's formal parameters. In other words, fc forwards m's arguments to another
 * format method.
 */
private boolean isWrappedFormatCall(FormatCall fc) {
    MethodTree enclosingMethod = TreeUtils.enclosingMethod(atypeFactory.getPath(fc.node));
    if (enclosingMethod == null) {
        return false;
    }
    ExecutableElement enclosingMethodElement = TreeUtils.elementFromDeclaration(enclosingMethod);
    boolean withinFormatMethod = (atypeFactory.getDeclAnnotation(enclosingMethodElement, FormatMethod.class) != null);
    if (!withinFormatMethod) {
        return false;
    }
    List<? extends ExpressionTree> args = fc.node.getArguments();
    List<? extends VariableTree> params = enclosingMethod.getParameters();
    List<? extends VariableElement> paramElements = enclosingMethodElement.getParameters();
    // Strip off leading Locale arguments.
    if (args.size() > 0 && FormatterTreeUtil.isLocale(args.get(0), atypeFactory)) {
        args = args.subList(1, args.size());
    }
    if (params.size() > 0 && TypesUtils.isDeclaredOfName(paramElements.get(0).asType(), "java.util.Locale")) {
        params = params.subList(1, params.size());
    }
    if (args.size() == params.size()) {
        for (int i = 0; i < args.size(); i++) {
            ExpressionTree arg = args.get(i);
            if (!(arg instanceof IdentifierTree && ((IdentifierTree) arg).getName().equals(params.get(i).getName()))) {
                return false;
            }
        }
    }
    return true;
}
Also used : MethodTree(com.sun.source.tree.MethodTree) ExecutableElement(javax.lang.model.element.ExecutableElement) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 9 with IdentifierTree

use of com.sun.source.tree.IdentifierTree in project st-js by st-js.

the class DefaultMemberSelectTemplate method getTarget.

private JS getTarget(WriterVisitor<JS> visitor, MemberSelectTree tree, GenerationContext<JS> context) {
    if (JavaNodes.isSuper(tree.getExpression())) {
        // super.field does not make sense, so convert it to this
        return context.js().keyword(Keyword.THIS);
    }
    TreeWrapper<IdentifierTree, JS> tw = context.getCurrentWrapper();
    Tree target = tree.getExpression();
    JS targetJS = visitor.scan(target, context);
    if (tw.isStatic() && !ElementUtils.isTypeKind(tw.child(target).getElement())) {
        // this is static method called from an instances: e.g. x.staticField
        targetJS = tw.getContext().js().property(targetJS, JavascriptKeywords.CONSTRUCTOR);
    }
    return targetJS;
}
Also used : IdentifierTree(com.sun.source.tree.IdentifierTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree)

Example 10 with IdentifierTree

use of com.sun.source.tree.IdentifierTree in project st-js by st-js.

the class GetterMemberSelectTemplate method getTarget.

private JS getTarget(WriterVisitor<JS> visitor, MemberSelectTree tree, GenerationContext<JS> context) {
    if (JavaNodes.isSuper(tree.getExpression())) {
        // super.field does not make sense, so convert it to this
        return context.js().keyword(Keyword.THIS);
    }
    TreeWrapper<IdentifierTree, JS> tw = context.getCurrentWrapper();
    Tree target = tree.getExpression();
    JS targetJS = visitor.scan(target, context);
    if (tw.isStatic() && !ElementUtils.isTypeKind(tw.child(target).getElement())) {
        // this is static method called from an instances: e.g. x.staticField
        targetJS = tw.getContext().js().property(targetJS, JavascriptKeywords.CONSTRUCTOR);
    }
    return targetJS;
}
Also used : IdentifierTree(com.sun.source.tree.IdentifierTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree)

Aggregations

IdentifierTree (com.sun.source.tree.IdentifierTree)82 ExpressionTree (com.sun.source.tree.ExpressionTree)41 MemberSelectTree (com.sun.source.tree.MemberSelectTree)36 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)28 Element (javax.lang.model.element.Element)24 Tree (com.sun.source.tree.Tree)21 ExecutableElement (javax.lang.model.element.ExecutableElement)18 VariableTree (com.sun.source.tree.VariableTree)17 TypeElement (javax.lang.model.element.TypeElement)16 MethodTree (com.sun.source.tree.MethodTree)13 VariableElement (javax.lang.model.element.VariableElement)13 ClassTree (com.sun.source.tree.ClassTree)12 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)11 ArrayAccessTree (com.sun.source.tree.ArrayAccessTree)10 NewClassTree (com.sun.source.tree.NewClassTree)10 AssignmentTree (com.sun.source.tree.AssignmentTree)9 BinaryTree (com.sun.source.tree.BinaryTree)9 LiteralTree (com.sun.source.tree.LiteralTree)8 StatementTree (com.sun.source.tree.StatementTree)8 Symbol (com.sun.tools.javac.code.Symbol)8