Search in sources :

Example 11 with IdentifierTree

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

the class GetterMemberSelectTemplate method doVisit.

protected JS doVisit(WriterVisitor<JS> visitor, MemberSelectTree tree, GenerationContext<JS> context, boolean global) {
    // this is only for fields.
    TreeWrapper<IdentifierTree, JS> tw = context.getCurrentWrapper();
    Element element = tw.getElement();
    if (element == null || element.getKind() == ElementKind.PACKAGE) {
        // package names are ignored
        return null;
    }
    JS target = getTarget(visitor, tree, context);
    JS name = context.js().string(tree.getIdentifier().toString());
    List<JS> arguments = new ArrayList<JS>();
    arguments.add(name);
    if (global) {
        arguments.add(0, target);
        return context.js().functionCall(context.js().property(context.js().name("stjs"), "getField"), arguments);
    }
    return context.js().functionCall(context.js().property(target, "get"), arguments);
}
Also used : Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 12 with IdentifierTree

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

the class PathGetterMemberSelectTemplate method getTarget.

private JS getTarget(WriterVisitor<JS> visitor, TreeWrapper<ExpressionTree, JS> currentWrapper, ExpressionTree tree, GenerationContext<JS> context, boolean targetIsPartOfPath) {
    if (!targetIsPartOfPath) {
        return visitor.scan(currentWrapper.getTree(), context);
    }
    if (tree instanceof MemberSelectTree) {
        MemberSelectTree ms = (MemberSelectTree) tree;
        if (JavaNodes.isSuper(ms.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 = ms.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;
    }
    return MemberWriters.buildTarget(context.getCurrentWrapper());
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) IdentifierTree(com.sun.source.tree.IdentifierTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree)

Example 13 with IdentifierTree

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

the class TreeUtils method isMethodAccess.

/**
 * Determine whether <code>tree</code> refers to a method element, such as
 *
 * <pre>
 *   <em>m</em>(...)
 *   <em>obj</em> . <em>m</em>(...)
 * </pre>
 *
 * @return true iff if tree is a method access expression (implicit or explicit).
 * @param tree
 *            a {@link com.sun.source.tree.Tree} object.
 */
public static boolean isMethodAccess(Tree tree) {
    if (tree.getKind().equals(Tree.Kind.MEMBER_SELECT)) {
        // explicit method access
        MemberSelectTree memberSelect = (MemberSelectTree) tree;
        Element el = TreeUtils.elementFromUse(memberSelect);
        return el.getKind() == ElementKind.METHOD || el.getKind() == ElementKind.CONSTRUCTOR;
    } else if (tree.getKind().equals(Tree.Kind.IDENTIFIER)) {
        // implicit method access
        IdentifierTree ident = (IdentifierTree) tree;
        // The field "super" and "this" are also legal methods
        if (ident.getName().contentEquals("super") || ident.getName().contentEquals("this")) {
            return true;
        }
        Element el = TreeUtils.elementFromUse(ident);
        return el.getKind() == ElementKind.METHOD || el.getKind() == ElementKind.CONSTRUCTOR;
    }
    return false;
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 14 with IdentifierTree

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

the class TreeUtils method isFieldAccess.

/**
 * Determine whether <code>tree</code> is a field access expressions, such as
 *
 * <pre>
 *   <em>f</em>
 *   <em>obj</em> . <em>f</em>
 * </pre>
 *
 * @return true iff if tree is a field access expression (implicit or explicit).
 * @param tree
 *            a {@link com.sun.source.tree.Tree} object.
 */
public static boolean isFieldAccess(Tree tree) {
    if (tree.getKind().equals(Tree.Kind.MEMBER_SELECT)) {
        // explicit field access
        MemberSelectTree memberSelect = (MemberSelectTree) tree;
        Element el = TreeUtils.elementFromUse(memberSelect);
        return el.getKind().isField();
    } else if (tree.getKind().equals(Tree.Kind.IDENTIFIER)) {
        // implicit field access
        IdentifierTree ident = (IdentifierTree) tree;
        Element el = TreeUtils.elementFromUse(ident);
        return el.getKind().isField() && !ident.getName().contentEquals("this") && !ident.getName().contentEquals("super");
    }
    return false;
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 15 with IdentifierTree

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

the class TreeUtils method methodName.

/**
 * <p>
 * methodName.
 * </p>
 *
 * @return the name of the invoked method
 * @param node
 *            a {@link com.sun.source.tree.MethodInvocationTree} object.
 */
public static final Name methodName(MethodInvocationTree node) {
    ExpressionTree expr = node.getMethodSelect();
    if (expr.getKind() == Tree.Kind.IDENTIFIER) {
        return ((IdentifierTree) expr).getName();
    } else if (expr.getKind() == Tree.Kind.MEMBER_SELECT) {
        return ((MemberSelectTree) expr).getIdentifier();
    }
    ErrorReporter.errorAbort("TreeUtils.methodName: cannot be here: " + node);
    // dead code
    return null;
}
Also used : ExpressionTree(com.sun.source.tree.ExpressionTree) 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