use of com.sun.source.tree.IdentifierTree in project st-js by st-js.
the class DefaultMemberSelectTemplate method visit.
@Override
public JS visit(WriterVisitor<JS> visitor, MemberSelectTree tree, GenerationContext<JS> context) {
// this is only for fields. Methods are handled in MethodInvocationWriter
TreeWrapper<IdentifierTree, JS> tw = context.getCurrentWrapper();
Element element = tw.getElement();
if (element == null || element.getKind() == ElementKind.PACKAGE) {
// package names are ignored
return null;
}
if (element.getKind() == ElementKind.CLASS) {
if (tw.isGlobal()) {
// global classes are ignored
return null;
} else {
// Non global classes however, are not ignored and a translated taking the namespace into account
return context.js().name(context.getNames().getTypeName(context, element, DependencyType.STATIC));
}
}
JS target = getTarget(visitor, tree, context);
if (GeneratorConstants.CLASS.equals(tree.getIdentifier().toString())) {
// When ClassName.class > ClassName
return target;
}
return context.js().property(target, tree.getIdentifier().toString());
}
use of com.sun.source.tree.IdentifierTree in project st-js by st-js.
the class IdentifierWriter method buildTemplateName.
private String buildTemplateName(IdentifierTree tree, GenerationContext<JS> context) {
String name = tree.getName().toString();
if (GeneratorConstants.THIS.equals(name)) {
return "none";
}
TreeWrapper<IdentifierTree, JS> tw = context.getCurrentWrapper();
Element def = tw.getElement();
if (def.getKind() == ElementKind.FIELD) {
String template = tw.getFieldTemplate();
if (template != null) {
return template;
}
return "none";
}
return "none";
}
use of com.sun.source.tree.IdentifierTree in project checker-framework by typetools.
the class CFGTranslationPhaseOne method visitConditionalExpression.
@Override
public Node visitConditionalExpression(ConditionalExpressionTree tree, Void p) {
// see JLS 15.25
TypeMirror exprType = TreeUtils.typeOf(tree);
Label trueStart = new Label();
Label falseStart = new Label();
Label merge = new Label();
// create a synthetic variable for the value of the conditional expression
VariableTree condExprVarTree = treeBuilder.buildVariableDecl(exprType, uniqueName("condExpr"), findOwner(), null);
VariableDeclarationNode condExprVarNode = new VariableDeclarationNode(condExprVarTree);
condExprVarNode.setInSource(false);
extendWithNode(condExprVarNode);
Node condition = unbox(scan(tree.getCondition(), p));
ConditionalJump cjump = new ConditionalJump(trueStart, falseStart);
extendWithExtendedNode(cjump);
addLabelForNextNode(trueStart);
ExpressionTree trueExprTree = tree.getTrueExpression();
Node trueExprNode = scan(trueExprTree, p);
trueExprNode = conditionalExprPromotion(trueExprNode, exprType);
extendWithAssignmentForConditionalExpr(condExprVarTree, trueExprTree, trueExprNode);
extendWithExtendedNode(new UnconditionalJump(merge, FlowRule.BOTH_TO_THEN));
addLabelForNextNode(falseStart);
ExpressionTree falseExprTree = tree.getFalseExpression();
Node falseExprNode = scan(falseExprTree, p);
falseExprNode = conditionalExprPromotion(falseExprNode, exprType);
extendWithAssignmentForConditionalExpr(condExprVarTree, falseExprTree, falseExprNode);
extendWithExtendedNode(new UnconditionalJump(merge, FlowRule.BOTH_TO_ELSE));
addLabelForNextNode(merge);
Pair<IdentifierTree, LocalVariableNode> treeAndLocalVarNode = extendWithVarUseNode(condExprVarTree);
Node node = new TernaryExpressionNode(tree, condition, trueExprNode, falseExprNode, treeAndLocalVarNode.second);
extendWithNode(node);
return node;
}
use of com.sun.source.tree.IdentifierTree in project checker-framework by typetools.
the class JavaExpression method fromTree.
/**
* Converts a javac {@link ExpressionTree} to a CF JavaExpression. The result might contain {@link
* Unknown}.
*
* <p>We ignore operations such as widening and narrowing when computing the JavaExpression.
*
* @param tree a javac tree
* @return a JavaExpression for the given javac tree
*/
public static JavaExpression fromTree(ExpressionTree tree) {
JavaExpression result;
switch(tree.getKind()) {
case ARRAY_ACCESS:
ArrayAccessTree a = (ArrayAccessTree) tree;
JavaExpression arrayAccessExpression = fromTree(a.getExpression());
JavaExpression index = fromTree(a.getIndex());
result = new ArrayAccess(TreeUtils.typeOf(a), arrayAccessExpression, index);
break;
case BOOLEAN_LITERAL:
case CHAR_LITERAL:
case DOUBLE_LITERAL:
case FLOAT_LITERAL:
case INT_LITERAL:
case LONG_LITERAL:
case NULL_LITERAL:
case STRING_LITERAL:
LiteralTree vn = (LiteralTree) tree;
result = new ValueLiteral(TreeUtils.typeOf(tree), vn.getValue());
break;
case NEW_ARRAY:
NewArrayTree newArrayTree = (NewArrayTree) tree;
List<@Nullable JavaExpression> dimensions;
if (newArrayTree.getDimensions() == null) {
dimensions = Collections.emptyList();
} else {
dimensions = new ArrayList<>(newArrayTree.getDimensions().size());
for (ExpressionTree dimension : newArrayTree.getDimensions()) {
dimensions.add(fromTree(dimension));
}
}
List<JavaExpression> initializers;
if (newArrayTree.getInitializers() == null) {
initializers = Collections.emptyList();
} else {
initializers = new ArrayList<>(newArrayTree.getInitializers().size());
for (ExpressionTree initializer : newArrayTree.getInitializers()) {
initializers.add(fromTree(initializer));
}
}
result = new ArrayCreation(TreeUtils.typeOf(tree), dimensions, initializers);
break;
case METHOD_INVOCATION:
MethodInvocationTree mn = (MethodInvocationTree) tree;
assert TreeUtils.isUseOfElement(mn) : "@AssumeAssertion(nullness): tree kind";
ExecutableElement invokedMethod = TreeUtils.elementFromUse(mn);
// Note that the method might be nondeterministic.
List<JavaExpression> parameters = CollectionsPlume.mapList(JavaExpression::fromTree, mn.getArguments());
JavaExpression methodReceiver;
if (ElementUtils.isStatic(invokedMethod)) {
@SuppressWarnings(// enclosingTypeElement(ExecutableElement): @NonNull
"nullness:assignment") @NonNull TypeElement methodType = ElementUtils.enclosingTypeElement(invokedMethod);
methodReceiver = new ClassName(methodType.asType());
} else {
methodReceiver = getReceiver(mn);
}
TypeMirror resultType = TreeUtils.typeOf(mn);
result = new MethodCall(resultType, invokedMethod, methodReceiver, parameters);
break;
case MEMBER_SELECT:
result = fromMemberSelect((MemberSelectTree) tree);
break;
case IDENTIFIER:
IdentifierTree identifierTree = (IdentifierTree) tree;
TypeMirror typeOfId = TreeUtils.typeOf(identifierTree);
Name identifierName = identifierTree.getName();
if (identifierName.contentEquals("this") || identifierName.contentEquals("super")) {
result = new ThisReference(typeOfId);
break;
}
assert TreeUtils.isUseOfElement(identifierTree) : "@AssumeAssertion(nullness): tree kind";
Element ele = TreeUtils.elementFromUse(identifierTree);
if (ElementUtils.isTypeElement(ele)) {
result = new ClassName(ele.asType());
break;
}
result = fromVariableElement(typeOfId, ele);
break;
case UNARY_PLUS:
return fromTree(((UnaryTree) tree).getExpression());
case BITWISE_COMPLEMENT:
case LOGICAL_COMPLEMENT:
case POSTFIX_DECREMENT:
case POSTFIX_INCREMENT:
case PREFIX_DECREMENT:
case PREFIX_INCREMENT:
case UNARY_MINUS:
JavaExpression operand = fromTree(((UnaryTree) tree).getExpression());
return new UnaryOperation(TreeUtils.typeOf(tree), tree.getKind(), operand);
case CONDITIONAL_AND:
case CONDITIONAL_OR:
case DIVIDE:
case EQUAL_TO:
case GREATER_THAN:
case GREATER_THAN_EQUAL:
case LEFT_SHIFT:
case LESS_THAN:
case LESS_THAN_EQUAL:
case MINUS:
case MULTIPLY:
case NOT_EQUAL_TO:
case OR:
case PLUS:
case REMAINDER:
case RIGHT_SHIFT:
case UNSIGNED_RIGHT_SHIFT:
case XOR:
BinaryTree binaryTree = (BinaryTree) tree;
JavaExpression left = fromTree(binaryTree.getLeftOperand());
JavaExpression right = fromTree(binaryTree.getRightOperand());
return new BinaryOperation(TreeUtils.typeOf(tree), tree.getKind(), left, right);
default:
result = null;
}
if (result == null) {
result = new Unknown(tree);
}
return result;
}
use of com.sun.source.tree.IdentifierTree in project checker-framework by typetools.
the class TreeUtils method isMethodAccess.
/**
* Determine whether {@code tree} 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)
*/
public static boolean isMethodAccess(Tree tree) {
if (tree.getKind() == Tree.Kind.MEMBER_SELECT) {
// explicit method access
MemberSelectTree memberSelect = (MemberSelectTree) tree;
assert isUseOfElement(memberSelect) : "@AssumeAssertion(nullness): tree kind";
Element el = TreeUtils.elementFromUse(memberSelect);
return el.getKind() == ElementKind.METHOD || el.getKind() == ElementKind.CONSTRUCTOR;
} else if (tree.getKind() == 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;
}
assert isUseOfElement(ident) : "@AssumeAssertion(nullness): tree kind";
Element el = TreeUtils.elementFromUse(ident);
return el.getKind() == ElementKind.METHOD || el.getKind() == ElementKind.CONSTRUCTOR;
}
return false;
}
Aggregations