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