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);
}
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());
}
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;
}
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;
}
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;
}
Aggregations