Java Code Examples for com.sun.source.tree.Tree.Kind#EXPRESSION_STATEMENT

The following examples show how to use com.sun.source.tree.Tree.Kind#EXPRESSION_STATEMENT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WorkingCopy.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addSyntheticTrees(DiffContext diffContext, Tree node) {
    if (node == null) return ;
    
    if (((JCTree) node).pos == (-1)) {
        diffContext.syntheticTrees.add(node);
        return ;
    }
    
    if (node.getKind() == Kind.EXPRESSION_STATEMENT) {
        ExpressionTree est = ((ExpressionStatementTree) node).getExpression();

        if (est.getKind() == Kind.METHOD_INVOCATION) {
            ExpressionTree select = ((MethodInvocationTree) est).getMethodSelect();

            if (select.getKind() == Kind.IDENTIFIER && ((IdentifierTree) select).getName().contentEquals("super")) {
                if (getTreeUtilities().isSynthetic(diffContext.origUnit, node)) {
                    diffContext.syntheticTrees.add(node);
                }
            }
        }
    }
}
 
Example 2
Source File: ImplementAllAbstractMethods.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static TreePath deepTreePath(CompilationInfo info, int offset) {
    TreePath basic = info.getTreeUtilities().pathFor(offset);
    TreePath plusOne = info.getTreeUtilities().pathFor(offset + 1);
    
    TreePath parent = plusOne.getParentPath();
    if (parent == null) {
        return basic;
    }
    if (plusOne.getLeaf().getKind() == Kind.NEW_CLASS &&
        parent.getLeaf().getKind() == Kind.EXPRESSION_STATEMENT) {
        parent = parent.getParentPath();
        if (parent == null) {
            return basic;
        }
    }
    if (parent.getLeaf() == basic.getLeaf()) {
        return plusOne;
    }
    return basic;
}
 
Example 3
Source File: AssignResultToVariable.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private StatementTree findMatchingMethodInvocation(CompilationInfo info, BlockTree block, int offset) {
    for (StatementTree t : block.getStatements()) {
        if (t.getKind() != Kind.EXPRESSION_STATEMENT) continue;

        long statementStart = info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), t);

        if (offset < statementStart) return null;

        ExpressionStatementTree est = (ExpressionStatementTree) t;
        long statementEnd = info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), t);
        long expressionEnd = info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), est.getExpression());

        if (expressionEnd <= offset && offset < statementEnd) {
            return t;
        }
    }

    return null;
}
 
Example 4
Source File: TreePruner.java    From bazel with Apache License 2.0 6 votes vote down vote up
private static boolean delegatingConstructor(List<JCStatement> stats) {
  if (stats.isEmpty()) {
    return false;
  }
  JCStatement stat = stats.get(0);
  if (stat.getKind() != Kind.EXPRESSION_STATEMENT) {
    return false;
  }
  JCExpression expr = ((JCExpressionStatement) stat).getExpression();
  if (expr.getKind() != Kind.METHOD_INVOCATION) {
    return false;
  }
  JCExpression method = ((JCMethodInvocation) expr).getMethodSelect();
  Name name;
  switch (method.getKind()) {
    case IDENTIFIER:
      name = ((JCIdent) method).getName();
      break;
    case MEMBER_SELECT:
      name = ((JCFieldAccess) method).getIdentifier();
      break;
    default:
      return false;
  }
  return name.contentEquals("this") || name.contentEquals("super");
}
 
Example 5
Source File: TreeUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
boolean isSynthetic(CompilationUnitTree cut, Tree leaf) throws NullPointerException {
    JCTree tree = (JCTree) leaf;
    
    if (tree.pos == (-1))
        return true;
    
    if (leaf.getKind() == Kind.METHOD) {
        //check for synthetic constructor:
        return (((JCMethodDecl)leaf).mods.flags & Flags.GENERATEDCONSTR) != 0L;
    }
    
    //check for synthetic superconstructor call:
    if (leaf.getKind() == Kind.EXPRESSION_STATEMENT) {
        ExpressionStatementTree est = (ExpressionStatementTree) leaf;
        
        if (est.getExpression().getKind() == Kind.METHOD_INVOCATION) {
            MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
            
            if (mit.getMethodSelect().getKind() == Kind.IDENTIFIER) {
                IdentifierTree it = (IdentifierTree) mit.getMethodSelect();
                
                if ("super".equals(it.getName().toString())) {
                    SourcePositions sp = info.getTrees().getSourcePositions();
                    
                    return sp.getEndPosition(cut, leaf) == (-1);
                }
            }
        }
    }
    
    return false;
}
 
Example 6
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isSynthetic(CompilationUnitTree cut, Tree leaf) throws NullPointerException {
    JCTree tree = (JCTree) leaf;

    if (tree.pos == (-1))
        return true;

    if (leaf.getKind() == Kind.METHOD) {
        //check for synthetic constructor:
        return (((JCMethodDecl)leaf).mods.flags & Flags.GENERATEDCONSTR) != 0L;
    }

    //check for synthetic superconstructor call:
    if (cut != null && leaf.getKind() == Kind.EXPRESSION_STATEMENT) {
        ExpressionStatementTree est = (ExpressionStatementTree) leaf;

        if (est.getExpression().getKind() == Kind.METHOD_INVOCATION) {
            MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();

            if (mit.getMethodSelect().getKind() == Kind.IDENTIFIER) {
                IdentifierTree it = (IdentifierTree) mit.getMethodSelect();

                if ("super".equals(it.getName().toString())) {
                    return ((JCCompilationUnit) cut).endPositions.getEndPos(tree) == (-1);
                }
            }
        }
    }

    return false;
}
 
Example 7
Source File: JavaPluginUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static boolean isSynthetic(CompilationInfo info, CompilationUnitTree cut, Tree leaf) throws NullPointerException {
    JCTree tree = (JCTree) leaf;
    
    if (tree.pos == (-1))
        return true;
    
    if (leaf.getKind() == Kind.METHOD) {
        //check for synthetic constructor:
        return (((JCTree.JCMethodDecl)leaf).mods.flags & Flags.GENERATEDCONSTR) != 0L;
    }
    
    //check for synthetic superconstructor call:
    if (leaf.getKind() == Kind.EXPRESSION_STATEMENT) {
        ExpressionStatementTree est = (ExpressionStatementTree) leaf;
        
        if (est.getExpression().getKind() == Kind.METHOD_INVOCATION) {
            MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
            
            if (mit.getMethodSelect().getKind() == Kind.IDENTIFIER) {
                IdentifierTree it = (IdentifierTree) mit.getMethodSelect();
                
                if ("super".equals(it.getName().toString())) {
                    SourcePositions sp = info.getTrees().getSourcePositions();
                    
                    return sp.getEndPosition(cut, leaf) == (-1);
                }
            }
        }
    }
    
    return false;
}
 
Example 8
Source File: ConvertToARM.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static TryTree findNestedARM(
        final Collection<? extends StatementTree> stms,
        final StatementTree var) {
    int state = var != null ? 0 : 1;
    for (StatementTree stm : stms) {
        if (stm == var) {
            state = 1;
        }
        if (state == 1) {
            if (stm.getKind() == Kind.TRY) {
                final TryTree tryTree = (TryTree)stm;
                if (tryTree.getResources() != null && !tryTree.getResources().isEmpty()) {
                    return tryTree;
                } else {
                    final Iterator<? extends StatementTree> blkStms = tryTree.getBlock().getStatements().iterator();
                    if (blkStms.hasNext()) {
                        StatementTree bstm = blkStms.next();
                        if (bstm.getKind() == Kind.TRY) {
                            return (TryTree)bstm;
                        }
                        if (bstm.getKind() == Kind.EXPRESSION_STATEMENT && blkStms.hasNext()) {
                            bstm = blkStms.next();
                            if (bstm.getKind() == Kind.TRY) {
                                return (TryTree)bstm;
                            }
                        }
                    }
                }
            }
            if (stm != var) {
                break;
            }
        }
    }
    return null;
}
 
Example 9
Source File: Unbalanced.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@TriggerTreeKind({Kind.IDENTIFIER, Kind.MEMBER_SELECT})
@TriggerOptions(TriggerOptions.PROCESS_GUARDED)
public static ErrorDescription before(HintContext ctx) {
    TreePath tp = ctx.getPath();
    VariableElement var = testElement(ctx);

    if (var == null) return null;

    if (tp.getParentPath().getLeaf().getKind() == Kind.MEMBER_SELECT && tp.getParentPath().getParentPath().getLeaf().getKind() == Kind.METHOD_INVOCATION) {
        String methodName = ((MemberSelectTree) tp.getParentPath().getLeaf()).getIdentifier().toString();
        if (READ_METHODS.contains(methodName)) {
            if (tp.getParentPath().getParentPath().getParentPath().getLeaf().getKind() != Kind.EXPRESSION_STATEMENT) {
                record(ctx.getInfo(), var, State.READ);
            }
            return null;
        } else if (WRITE_METHODS.contains(methodName)) {
            if (tp.getParentPath().getParentPath().getParentPath().getLeaf().getKind() != Kind.EXPRESSION_STATEMENT) {
                record(ctx.getInfo(), var, State.WRITE, State.READ);
            } else {
                record(ctx.getInfo(), var, State.WRITE);
            }
            return null;
        }
    }

    record(ctx.getInfo(), var, State.WRITE, State.READ);

    return null;
}
 
Example 10
Source File: AddParameterOrLocalFix.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void resolveLocalVariable55(final WorkingCopy wc, TreePath tp, TreeMaker make, TypeMirror proposedType) {
    final String name = ((IdentifierTree) tp.getLeaf()).getName().toString();
    TreePath blockPath = findOutmostBlock(tp);

    if (blockPath == null) {
        return;
    }
    
    int index = 0;
    BlockTree block = ((BlockTree) blockPath.getLeaf());
    
    TreePath method = findMethod(tp);

    if (method != null && ((MethodTree) method.getLeaf()).getReturnType() == null && !block.getStatements().isEmpty()) {
        StatementTree stat = block.getStatements().get(0);
        
        if (stat.getKind() == Kind.EXPRESSION_STATEMENT) {
            Element thisMethodEl = wc.getTrees().getElement(method);
            TreePath pathToFirst = new TreePath(new TreePath(new TreePath(method, block), stat), ((ExpressionStatementTree) stat).getExpression());
            Element superCall = wc.getTrees().getElement(pathToFirst);

            if (thisMethodEl != null && superCall != null && thisMethodEl.getKind() == ElementKind.CONSTRUCTOR && superCall.getKind() == ElementKind.CONSTRUCTOR) {
                index = 1;
            }
        }
    }
    
    VariableTree vt = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), name, make.Type(proposedType), null);
    
    wc.rewrite(block, wc.getTreeMaker().insertBlockStatement(block, index, vt));
}
 
Example 11
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Name getLeftTreeName(StatementTree statement) {
    if (statement.getKind() != Kind.EXPRESSION_STATEMENT) {
        return null;
    }
    JCTree.JCExpressionStatement jceTree = (JCTree.JCExpressionStatement) statement;
    if (jceTree.expr.getKind() != Kind.ASSIGNMENT) {
        return null;
    }
    JCTree.JCAssign assignTree = (JCTree.JCAssign) jceTree.expr;
    return ((JCTree.JCIdent) assignTree.lhs).name;
}
 
Example 12
Source File: AssignmentIssues.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Hint(displayName = "#DN_org.netbeans.modules.java.hints.AssignmentIssues.nestedAssignment", description = "#DESC_org.netbeans.modules.java.hints.AssignmentIssues.nestedAssignment", category = "assignment_issues", enabled = false, suppressWarnings = "NestedAssignment", options=Options.QUERY) //NOI18N
@TriggerTreeKind({Kind.ASSIGNMENT, Kind.AND_ASSIGNMENT, Kind.DIVIDE_ASSIGNMENT,
    Kind.LEFT_SHIFT_ASSIGNMENT, Kind.MINUS_ASSIGNMENT, Kind.MULTIPLY_ASSIGNMENT,
    Kind.OR_ASSIGNMENT, Kind.PLUS_ASSIGNMENT, Kind.REMAINDER_ASSIGNMENT, Kind.RIGHT_SHIFT_ASSIGNMENT,
    Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT, Kind.XOR_ASSIGNMENT})
public static ErrorDescription nestedAssignment(HintContext context) {
    final TreePath path = context.getPath();
    final Kind parentKind = path.getParentPath().getLeaf().getKind();
    if (parentKind != Kind.EXPRESSION_STATEMENT && parentKind != Kind.ANNOTATION) {
        return ErrorDescriptionFactory.forTree(context, path, NbBundle.getMessage(AssignmentIssues.class, "MSG_NestedAssignment", path.getLeaf())); //NOI18N
    }
    return null;
}
 
Example 13
Source File: AssignmentIssues.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Hint(displayName = "#DN_org.netbeans.modules.java.hints.AssignmentIssues.incrementDecrementUsed", description = "#DESC_org.netbeans.modules.java.hints.AssignmentIssues.incrementDecrementUsed", category = "assignment_issues", enabled = false, suppressWarnings = "ValueOfIncrementOrDecrementUsed", options=Options.QUERY) //NOI18N
@TriggerTreeKind({Kind.PREFIX_INCREMENT, Kind.PREFIX_DECREMENT, Kind.POSTFIX_INCREMENT, Kind.POSTFIX_DECREMENT})
public static ErrorDescription incrementDecrementUsed(HintContext context) {
    final TreePath path = context.getPath();
    if (path.getParentPath().getLeaf().getKind() != Kind.EXPRESSION_STATEMENT) {
        final Kind kind = path.getLeaf().getKind();
        return ErrorDescriptionFactory.forTree(context, path, NbBundle.getMessage(AssignmentIssues.class,
                kind == Kind.PREFIX_INCREMENT || kind == Kind.POSTFIX_INCREMENT
                ? "MSG_IncrementUsedAsExpression" : "MSG_DecrementUsedAsExpression", path.getLeaf())); //NOI18N
    }
    return null;
}
 
Example 14
Source File: TaskFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
ParseTask parse(final String source) {
    ParseTask pt = state.taskFactory.new ParseTask(source, false);
    if (!pt.units().isEmpty()
            && pt.units().get(0).getKind() == Kind.EXPRESSION_STATEMENT
            && pt.getDiagnostics().hasOtherThanNotStatementErrors()) {
        // It failed, it may be an expression being incorrectly
        // parsed as having a leading type variable, example:   a < b
        // Try forcing interpretation as an expression
        ParseTask ept = state.taskFactory.new ParseTask(source, true);
        if (!ept.getDiagnostics().hasOtherThanNotStatementErrors()) {
            return ept;
        }
    }
    return pt;
}
 
Example 15
Source File: ErrorDescriptionFactory.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("fallthrough")
private static int[] computeNameSpan(Tree tree, HintContext context) {
    switch (tree.getKind()) {
        case LABELED_STATEMENT:
            return context.getInfo().getTreeUtilities().findNameSpan((LabeledStatementTree) tree);
        case METHOD:
            return context.getInfo().getTreeUtilities().findNameSpan((MethodTree) tree);
        case ANNOTATION_TYPE:
        case CLASS:
        case ENUM:
        case INTERFACE:
            return context.getInfo().getTreeUtilities().findNameSpan((ClassTree) tree);
        case VARIABLE:
            return context.getInfo().getTreeUtilities().findNameSpan((VariableTree) tree);
        case MEMBER_SELECT:
            //XXX:
            MemberSelectTree mst = (MemberSelectTree) tree;
            int[] span = context.getInfo().getTreeUtilities().findNameSpan(mst);

            if (span == null) {
                int end = (int) context.getInfo().getTrees().getSourcePositions().getEndPosition(context.getInfo().getCompilationUnit(), tree);
                span = new int[] {end - mst.getIdentifier().length(), end};
            }
            return span;
        case METHOD_INVOCATION:
            return computeNameSpan(((MethodInvocationTree) tree).getMethodSelect(), context);
        case BLOCK:
            Collection<? extends TreePath> prefix = context.getMultiVariables().get("$$1$");
            
            if (prefix != null) {
                BlockTree bt = (BlockTree) tree;
                
                if (bt.getStatements().size() > prefix.size()) {
                    return computeNameSpan(bt.getStatements().get(prefix.size()), context);
                }
            }
        default:
            int start = (int) context.getInfo().getTrees().getSourcePositions().getStartPosition(context.getInfo().getCompilationUnit(), tree);
            if (    StatementTree.class.isAssignableFrom(tree.getKind().asInterface())
                && tree.getKind() != Kind.EXPRESSION_STATEMENT
                && tree.getKind() != Kind.BLOCK) {
                TokenSequence<?> ts = context.getInfo().getTokenHierarchy().tokenSequence();
                ts.move(start);
                if (ts.moveNext()) {
                    return new int[] {ts.offset(), ts.offset() + ts.token().length()};
                }
            }
            return new int[] {
                start,
                Math.min((int) context.getInfo().getTrees().getSourcePositions().getEndPosition(context.getInfo().getCompilationUnit(), tree),
                         findLineEnd(context.getInfo(), start)),
            };
    }
}