use of com.sun.source.tree.StatementTree in project checker-framework by typetools.
the class TreeUtils method containsThisConstructorInvocation.
/**
* @return true if the first statement in the body is a self constructor invocation within a
* constructor
*/
public static final boolean containsThisConstructorInvocation(MethodTree node) {
if (!TreeUtils.isConstructor(node) || node.getBody().getStatements().isEmpty())
return false;
StatementTree st = node.getBody().getStatements().get(0);
if (!(st instanceof ExpressionStatementTree) || !(((ExpressionStatementTree) st).getExpression() instanceof MethodInvocationTree)) {
return false;
}
MethodInvocationTree invocation = (MethodInvocationTree) ((ExpressionStatementTree) st).getExpression();
return "this".contentEquals(TreeUtils.methodName(invocation));
}
use of com.sun.source.tree.StatementTree in project checker-framework by typetools.
the class OptionalVisitor method handleConditionalStatementIsPresentGet.
/**
* Part of rule #3.
*
* <p>Pattern match for: {@code if (VAR.isPresent()) { METHOD(VAR.get()); }}
*
* <p>Prefer: {@code VAR.ifPresent(METHOD);}
*/
public void handleConditionalStatementIsPresentGet(IfTree node) {
ExpressionTree condExpr = TreeUtils.skipParens(node.getCondition());
StatementTree thenStmt = skipBlocks(node.getThenStatement());
StatementTree elseStmt = skipBlocks(node.getElseStatement());
if (!(elseStmt == null || (elseStmt.getKind() == Tree.Kind.BLOCK && ((BlockTree) elseStmt).getStatements().isEmpty()))) {
// else block is missing or is an empty block: "{}"
return;
}
if (!isCallToIsPresent(condExpr)) {
return;
}
ExpressionTree receiver = TreeUtils.getReceiverTree(condExpr);
if (thenStmt.getKind() != Kind.EXPRESSION_STATEMENT) {
return;
}
ExpressionTree thenExpr = ((ExpressionStatementTree) thenStmt).getExpression();
if (thenExpr.getKind() != Kind.METHOD_INVOCATION) {
return;
}
MethodInvocationTree invok = (MethodInvocationTree) thenExpr;
List<? extends ExpressionTree> args = invok.getArguments();
if (args.size() != 1) {
return;
}
ExpressionTree arg = TreeUtils.skipParens(args.get(0));
if (!isCallToGet(arg)) {
return;
}
ExpressionTree getReceiver = TreeUtils.getReceiverTree(arg);
if (!receiver.toString().equals(getReceiver.toString())) {
return;
}
ExpressionTree method = invok.getMethodSelect();
String methodString = method.toString();
int dotPos = methodString.lastIndexOf(".");
if (dotPos != -1) {
methodString = methodString.substring(0, dotPos) + "::" + methodString.substring(dotPos + 1);
}
checker.report(Result.warning("prefer.ifpresent", receiver, methodString), node);
}
use of com.sun.source.tree.StatementTree in project st-js by st-js.
the class TreeUtils method containsThisConstructorInvocation.
/**
* <p>
* containsThisConstructorInvocation.
* </p>
*
* @return true if the first statement in the body is a self constructor invocation within a constructor
* @param node
* a {@link com.sun.source.tree.MethodTree} object.
*/
public static final boolean containsThisConstructorInvocation(MethodTree node) {
if (!TreeUtils.isConstructor(node) || node.getBody().getStatements().isEmpty()) {
return false;
}
StatementTree st = node.getBody().getStatements().get(0);
if (!(st instanceof ExpressionStatementTree) || !(((ExpressionStatementTree) st).getExpression() instanceof MethodInvocationTree)) {
return false;
}
MethodInvocationTree invocation = (MethodInvocationTree) ((ExpressionStatementTree) st).getExpression();
return "this".contentEquals(TreeUtils.methodName(invocation));
}
use of com.sun.source.tree.StatementTree in project st-js by st-js.
the class NewClassWriter method getObjectInitializer.
/**
* special construction for object initialization new Object(){{x = 1; y = 2; }};
*/
private JS getObjectInitializer(WriterVisitor<JS> visitor, TreeWrapper<NewClassTree, JS> tw) {
NewClassTree tree = tw.getTree();
BlockTree initBlock = getDoubleBracesBlock(tree);
if (initBlock == null) {
if (tw.child(tree.getIdentifier()).isSyntheticType()) {
// the syntethic type will generate {} constructor even without the initBlock
return tw.getContext().js().object(new ArrayList<NameValue<JS>>());
}
return null;
}
if (!tw.child(tree.getIdentifier()).isSyntheticType()) {
// this is already checked and not allowed
return null;
}
List<NameValue<JS>> props = new ArrayList<NameValue<JS>>();
for (StatementTree stmt : initBlock.getStatements()) {
// check the right type of statements x=y is done in NewClassObjectInitCheck
ExpressionTree expr = ((ExpressionStatementTree) stmt).getExpression();
if (expr instanceof AssignmentTree) {
AssignmentTree assign = (AssignmentTree) expr;
props.add(NameValue.of(getPropertyName(assign.getVariable()), visitor.scan(assign.getExpression(), tw.getContext())));
} else {
MethodInvocationTree meth = (MethodInvocationTree) expr;
String propertyName = MethodToPropertyTemplate.getPropertyName(meth);
JS value = visitor.scan(meth.getArguments().get(0), tw.getContext());
props.add(NameValue.of(propertyName, value));
}
}
return tw.getContext().js().object(props);
}
use of com.sun.source.tree.StatementTree in project st-js by st-js.
the class NewClassObjectInitCheck method visit.
/**
* {@inheritDoc}
*/
@Override
public Void visit(CheckVisitor visitor, NewClassTree tree, GenerationContext<Void> context) {
BlockTree initBlock = NewClassWriter.getDoubleBracesBlock(tree);
TreeWrapper<ClassTree, Void> tw = context.getCurrentWrapper();
if (initBlock == null && !tw.child(tree.getIdentifier()).isSyntheticType()) {
return null;
}
if (initBlock != null) {
if (!tw.child(tree.getIdentifier()).isSyntheticType()) {
context.addError(tree, "Object creation block (double braces {{}}) is only allowed for classes annotated with @SyntheticType");
}
for (StatementTree stmt : initBlock.getStatements()) {
checkStatement(stmt, context);
}
}
return null;
}
Aggregations