Search in sources :

Example 56 with IdentifierTree

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

the class LambdaExpressionWriter method accessOuterScope.

private boolean accessOuterScope(LambdaExpressionTree lambda) {
    AtomicBoolean outerScopeAccess = new AtomicBoolean(false);
    lambda.accept(new TreeScanner<Void, Void>() {

        private boolean checkStopped;

        @Override
        public Void visitIdentifier(IdentifierTree tree, Void arg1) {
            if (checkStopped) {
                return super.visitIdentifier(tree, arg1);
            }
            Element fieldElement = TreeUtils.elementFromUse(tree);
            if (IdentifierAccessOuterScopeCheck.isRegularInstanceField(fieldElement, tree) || GeneratorConstants.THIS.equals(tree.getName().toString())) {
                outerScopeAccess.set(true);
            }
            return super.visitIdentifier(tree, arg1);
        }

        @Override
        public Void visitClass(ClassTree arg0, Void arg1) {
            // stop the checks if a new type is encountered
            checkStopped = true;
            return super.visitClass(arg0, arg1);
        }

        @Override
        public Void visitMethodInvocation(MethodInvocationTree tree, Void arg1) {
            if (checkStopped) {
                return super.visitMethodInvocation(tree, arg1);
            }
            Element methodElement = TreeUtils.elementFromUse(tree);
            if (JavaNodes.isStatic(methodElement)) {
                // only instance methods
                return super.visitMethodInvocation(tree, arg1);
            }
            String name = MethodInvocationWriter.buildMethodName(tree);
            if (GeneratorConstants.THIS.equals(name) || GeneratorConstants.SUPER.equals(name)) {
                // this and super call are ok
                return super.visitMethodInvocation(tree, arg1);
            }
            if (!(tree.getMethodSelect() instanceof IdentifierTree)) {
                // check for Outer.this check
                return super.visitMethodInvocation(tree, arg1);
            }
            outerScopeAccess.set(true);
            return super.visitMethodInvocation(tree, arg1);
        }
    }, null);
    return outerScopeAccess.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) Element(javax.lang.model.element.Element) ClassTree(com.sun.source.tree.ClassTree) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 57 with IdentifierTree

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

the class TreeUtils method isSelfAccess.

/**
 * Returns true if the tree is a tree that 'looks like' either an access of a field or an invocation of a method
 * that are owned by the same accessing instance.
 *
 * It would only return true if the access tree is of the form:
 *
 * <pre>
 *   field
 *   this.field
 *
 *   method()
 *   this.method()
 * </pre>
 *
 * It does not perform any semantical check to differentiate between fields and local variables; local methods or
 * imported static methods.
 *
 * @param tree
 *            expression tree representing an access to object member
 * @return {@code true} iff the member is a member of {@code this} instance
 */
public static boolean isSelfAccess(final ExpressionTree tree) {
    ExpressionTree tr = TreeUtils.skipParens(tree);
    // If method invocation check the method select
    if (tr.getKind() == Tree.Kind.ARRAY_ACCESS) {
        return false;
    }
    if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) {
        tr = ((MethodInvocationTree) tree).getMethodSelect();
    }
    tr = TreeUtils.skipParens(tr);
    if (tr.getKind() == Tree.Kind.TYPE_CAST) {
        tr = ((TypeCastTree) tr).getExpression();
    }
    tr = TreeUtils.skipParens(tr);
    if (tr.getKind() == Tree.Kind.IDENTIFIER) {
        return true;
    }
    if (tr.getKind() == Tree.Kind.MEMBER_SELECT) {
        tr = ((MemberSelectTree) tr).getExpression();
        if (tr.getKind() == Tree.Kind.IDENTIFIER) {
            Name ident = ((IdentifierTree) tr).getName();
            return ident.contentEquals("this") || ident.contentEquals("super");
        }
    }
    return false;
}
Also used : ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Name(javax.lang.model.element.Name)

Example 58 with IdentifierTree

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

the class MethodInvocationWriter method buildMethodName.

public static String buildMethodName(MethodInvocationTree tree) {
    ExpressionTree select = tree.getMethodSelect();
    if (select instanceof IdentifierTree) {
        // simple call: method(args)
        return ((IdentifierTree) select).getName().toString();
    }
    // calls with target: target.method(args)
    MemberSelectTree memberSelect = (MemberSelectTree) select;
    return memberSelect.getIdentifier().toString();
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 59 with IdentifierTree

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

the class MethodInvocationWriter method buildTarget.

public static <JS, T extends MethodInvocationTree> JS buildTarget(WriterVisitor<JS> visitor, TreeWrapper<T, JS> tw) {
    ExpressionTree select = tw.getTree().getMethodSelect();
    if (select instanceof IdentifierTree) {
        // simple call: method(args)
        return MemberWriters.buildTarget(tw);
    }
    // calls with target: target.method(args)
    if (TreeUtils.isSuperCall(tw.getTree())) {
        // staticMethod
        return MemberWriters.buildTarget(tw);
    }
    MemberSelectTree memberSelect = (MemberSelectTree) select;
    JS targetJS = visitor.scan(memberSelect.getExpression(), tw.getContext());
    if (tw.isStatic() && !ElementUtils.isTypeKind(tw.child(memberSelect).child(memberSelect.getExpression()).getElement())) {
        // this is static method called from an instances: e.g. x.staticMethod()
        targetJS = tw.getContext().js().property(targetJS, JavascriptKeywords.CONSTRUCTOR);
    }
    return targetJS;
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 60 with IdentifierTree

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

the class DefaultIdentifierTemplate method visit.

@Override
@SuppressWarnings("PMD.CyclomaticComplexity")
public JS visit(WriterVisitor<JS> visitor, IdentifierTree tree, GenerationContext<JS> context) {
    String name = tree.getName().toString();
    if (GeneratorConstants.THIS.equals(name)) {
        return context.js().keyword(Keyword.THIS);
    }
    TreeWrapper<IdentifierTree, JS> tw = context.getCurrentWrapper();
    Element def = tw.getElement();
    if (def.getKind() == ElementKind.PACKAGE) {
        return null;
    }
    if (def.getKind() == ElementKind.FIELD) {
        return visitField(tree, context);
    }
    if (def.getKind() == ElementKind.ENUM_CONSTANT) {
        return visitEnumConstant(def, tree, context);
    }
    if (ElementUtils.isTypeKind(def)) {
        if (tw.isGlobal()) {
            // use this to register the class name - to build the dependencies
            context.getNames().getTypeName(context, def, DependencyType.STATIC);
            return null;
        }
        name = context.getNames().getTypeName(context, def, DependencyType.STATIC);
    }
    // assume variable
    return context.js().name(name);
}
Also used : Element(javax.lang.model.element.Element) IdentifierTree(com.sun.source.tree.IdentifierTree)

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