Java Code Examples for com.sun.source.tree.Tree#accept()

The following examples show how to use com.sun.source.tree.Tree#accept() . 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: EditorContextSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static MethodArgument[] computeMethodArguments(CompilationController ci,
                                                      EditorContext.Operation operation,
                                                      ASTOperationCreationDelegate opCreationDelegate)
                                                        throws IOException {
    EditorContext.MethodArgument args[];
    if (!PreferredCCParser.toPhase(ci, JavaSource.Phase.RESOLVED, LOG)) {
        return null;
    }
    int offset = operation.getMethodEndPosition().getOffset();
    Scope scope = ci.getTreeUtilities().scopeFor(offset);
    Element method = scope.getEnclosingMethod();
    if (method == null) {
        return null;
    }
    Tree methodTree = ci.getTrees().getTree(method);
    CompilationUnitTree cu = ci.getCompilationUnit();
    MethodArgumentsScanner scanner =
            new MethodArgumentsScanner(offset, cu, ci.getTrees().getSourcePositions(), true,
                                       opCreationDelegate);
    args = methodTree.accept(scanner, null);
    args = scanner.getArguments();
    return args;
}
 
Example 2
Source File: EditorContextSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static MethodArgument[] computeMethodArguments(CompilationController ci,
                                                      int methodLineNumber,
                                                      int offset,
                                                      ASTOperationCreationDelegate opCreationDelegate)
                                                        throws IOException {
    MethodArgument args[];
    if (!PreferredCCParser.toPhase(ci, JavaSource.Phase.RESOLVED, LOG)) {
        return null;
    }
    Scope scope = ci.getTreeUtilities().scopeFor(offset);
    Element clazz = scope.getEnclosingClass();
    if (clazz == null) {
        return null;
    }
    Tree methodTree = ci.getTrees().getTree(clazz);
    CompilationUnitTree cu = ci.getCompilationUnit();
    MethodArgumentsScanner scanner =
            new MethodArgumentsScanner(methodLineNumber, cu, ci.getTrees().getSourcePositions(), false,
                                       opCreationDelegate);
    args = methodTree.accept(scanner, null);
    args = scanner.getArguments();
    return args;
}
 
Example 3
Source File: TreePathHandle.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static List<Tree> listChildren(@NonNull Tree t) {
    final List<Tree> result = new LinkedList<Tree>();

    t.accept(new ErrorAwareTreeScanner<Void, Void>() {
        @Override
        public Void scan(Tree node, Void p) {
            result.add(node);
            return null;
        }
    }, null);

    return result;
}
 
Example 4
Source File: JavaFixUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Set<Tree> immediateChildren(Tree t) {
    Set<Tree> children = new HashSet<>();

    t.accept(new TreeScanner<Void, Void>() {
        @Override
        public Void scan(Tree tree, Void p) {
            if (tree != null)
                children.add(tree);
            return null;
        }
    }, null);

    return children;
}
 
Example 5
Source File: TreeDiffer.java    From compile-testing with Apache License 2.0 5 votes vote down vote up
/**
 * Pushes the {@code expected} and {@code actual} {@link Tree}s onto their respective
 * {@link TreePath}s and recurses with {@code expected.accept(this, actual)}, popping the
 * stack when the call completes.
 *
 * <p>This should be the ONLY place where either {@link TreePath} is mutated.
 */
private Void pushPathAndAccept(Tree expected, Tree actual) {
  TreePath prevExpectedPath = expectedPath;
  TreePath prevActualPath = actualPath;
  expectedPath = expectedPathPlus(expected);
  actualPath = actualPathPlus(actual);
  try {
    return expected.accept(this, actual);
  } finally {
    expectedPath = prevExpectedPath;
    actualPath = prevActualPath;
  }
}
 
Example 6
Source File: EditorContextSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static EditorContext.Operation[] computeOperations(CompilationController ci,
                                                          int offset,
                                                          int lineNumber,
                                                          EditorContext.BytecodeProvider bytecodeProvider,
                                                          ASTOperationCreationDelegate opCreationDelegate) throws IOException {
    if (!PreferredCCParser.toPhase(ci, JavaSource.Phase.RESOLVED, LOG)) {//TODO: ELEMENTS_RESOLVED may be sufficient
        return new EditorContext.Operation[] {};
    }
    // We need the enclosing statement/block
    Tree statementTree = findStatementInScope(ci.getTreeUtilities().pathFor(offset));
    LOG.log(Level.FINE, "Statement tree found at line {0}:\n{1}\n", new Object[]{ lineNumber, statementTree });
    if (statementTree == null) {
        Scope scope = ci.getTreeUtilities().scopeFor(offset);
        Element method = scope.getEnclosingMethod();
        if (method == null) {
            return new EditorContext.Operation[] {};
        }
        statementTree = ci.getTrees().getTree(method);
    }
    if (statementTree == null) { // method not found
        return new EditorContext.Operation[] {};
    }
    CompilationUnitTree cu = ci.getCompilationUnit();
    SourcePositions sp = ci.getTrees().getSourcePositions();
    int statementStart = (int) cu.getLineMap().getLineNumber(sp.getStartPosition(cu, statementTree));
    int statementEnd = (int) cu.getLineMap().getLineNumber(sp.getEndPosition(cu, statementTree));
    ExpressionScanner scanner = new ExpressionScanner(lineNumber, statementStart, statementEnd,
                                                      cu, ci.getTrees().getSourcePositions());
    ExpressionScanner.ExpressionsInfo info = new ExpressionScanner.ExpressionsInfo();
    List<Tree> expTrees = statementTree.accept(scanner, info);

    LOG.log(Level.FINE, "expression trees = {0}", expTrees);
    
    //com.sun.source.tree.ExpressionTree expTree = scanner.getExpressionTree();
    if (expTrees == null || expTrees.isEmpty()) {
        return new EditorContext.Operation[] {};
    }
    int treeStartLine = Integer.MAX_VALUE;
    int treeEndLine = 0;
    for (int i = 0; i < expTrees.size(); i++) {
        Tree tree = expTrees.get(i);
        int start = (int) cu.getLineMap().getLineNumber(
            sp.getStartPosition(cu, tree));
        int end = (int) cu.getLineMap().getLineNumber(
            sp.getEndPosition(cu, tree));
        if (start == Diagnostic.NOPOS || end == Diagnostic.NOPOS) {
            continue;
        }
        if (start < treeStartLine) {
            treeStartLine = start;
        }
        if (end > treeEndLine) {
            treeEndLine = end;
        }
    }
    if (treeStartLine == Integer.MAX_VALUE) {
        return null;
    }
    //t3 = System.nanoTime();
    int[] indexes = bytecodeProvider.indexAtLines(treeStartLine, treeEndLine);
    if (indexes == null) {
        return null;
    }
    Map<Tree, EditorContext.Operation> nodeOperations = new HashMap<Tree, EditorContext.Operation>();
    EditorContext.Operation[] ops = AST2Bytecode.matchSourceTree2Bytecode(
            cu,
            ci,
            expTrees, info, bytecodeProvider.byteCodes(),
            indexes,
            bytecodeProvider.constantPool(),
            opCreationDelegate,
            nodeOperations);
    if (ops != null) {
        assignNextOperations(statementTree, cu, ci,
                             bytecodeProvider, opCreationDelegate,
                             expTrees, info, nodeOperations);
    }
    return ops;
}
 
Example 7
Source File: EditorContextSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static void assignNextOperations(Tree methodTree,
                                         CompilationUnitTree cu,
                                         CompilationController ci,
                                         EditorContext.BytecodeProvider bytecodeProvider,
                                         ASTOperationCreationDelegate opCreationDelegate,
                                         List<Tree> treeNodes,
                                         ExpressionScanner.ExpressionsInfo info,
                                         Map<Tree, EditorContext.Operation> nodeOperations) {
    int length = treeNodes.size();
    for (int treeIndex = 0; treeIndex < length; treeIndex++) {
        Tree node = treeNodes.get(treeIndex);
        Set<Tree> nextNodes = info.getNextExpressions(node);
        if (nextNodes != null) {
            EditorContext.Operation op = nodeOperations.get(node);
            if (op == null) {
                for (int backIndex = treeIndex - 1; backIndex >= 0; backIndex--) {
                    node = treeNodes.get(backIndex);
                    op = nodeOperations.get(node);
                    if (op != null) {
                        break;
                    }
                }
            }
            if (op != null) {
                for (Tree t : nextNodes) {
                    EditorContext.Operation nextOp = nodeOperations.get(t);
                    if (nextOp == null) {
                        SourcePositions sp = ci.getTrees().getSourcePositions();
                        int treeStartLine =
                                (int) cu.getLineMap().getLineNumber(
                                    sp.getStartPosition(cu, t));
                        if (treeStartLine == Diagnostic.NOPOS) {
                            continue;
                        }
                        int treeEndLine =
                                (int) cu.getLineMap().getLineNumber(
                                    sp.getEndPosition(cu, t));
                        if (treeEndLine == Diagnostic.NOPOS) {
                            continue;
                        }
                        ExpressionScanner scanner = new ExpressionScanner(treeStartLine, treeStartLine, treeEndLine,
                                                                          cu, ci.getTrees().getSourcePositions());
                        ExpressionScanner.ExpressionsInfo newInfo = new ExpressionScanner.ExpressionsInfo();
                        List<Tree> newExpTrees = methodTree.accept(scanner, newInfo);
                        if (newExpTrees == null) {
                            continue;
                        }
                        treeStartLine =
                                (int) cu.getLineMap().getLineNumber(
                                    sp.getStartPosition(cu, newExpTrees.get(0)));
                        treeEndLine =
                                (int) cu.getLineMap().getLineNumber(
                                    sp.getEndPosition(cu, newExpTrees.get(newExpTrees.size() - 1)));

                        if (treeStartLine == Diagnostic.NOPOS || treeEndLine == Diagnostic.NOPOS) {
                            continue;
                        }
                        int[] indexes = bytecodeProvider.indexAtLines(treeStartLine, treeEndLine);
                        Map<Tree, EditorContext.Operation> newNodeOperations = new HashMap<Tree, EditorContext.Operation>();
                        /*Operation[] newOps = */AST2Bytecode.matchSourceTree2Bytecode(
                                cu,
                                ci,
                                newExpTrees, newInfo, bytecodeProvider.byteCodes(),
                                indexes,
                                bytecodeProvider.constantPool(),
                                opCreationDelegate,
                                newNodeOperations);
                        nextOp = newNodeOperations.get(t);
                        if (nextOp == null) {
                            // Next operation not found
                            System.err.println("Next operation not found!");
                            continue;
                        }
                    }
                    opCreationDelegate.addNextOperationTo(op, nextOp);
                }
            }
        }
    }

}
 
Example 8
Source File: JavacModuleParser.java    From pro with GNU General Public License v3.0 4 votes vote down vote up
private static void accept(TreeVisitor<?, ?> visitor, Tree node) {
  node.accept(visitor, null);
}
 
Example 9
Source File: UTemplater.java    From Refaster with Apache License 2.0 4 votes vote down vote up
public UTree<?> template(Tree tree) {
  return tree.accept(this, null);
}
 
Example 10
Source File: UTree.java    From Refaster with Apache License 2.0 4 votes vote down vote up
@Override
public Unifier unify(Tree target, Unifier unifier) {
  return (target != null && unifier != null) ? target.accept(this, unifier) : null;
}
 
Example 11
Source File: UParens.java    From Refaster with Apache License 2.0 4 votes vote down vote up
static Tree skipParens(Tree tree) {
  return tree.accept(SKIP_PARENS, null);
}