Java Code Examples for com.sun.source.util.Trees#getSourcePositions()

The following examples show how to use com.sun.source.util.Trees#getSourcePositions() . 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: T6993305.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override public Void visitCompilationUnit(CompilationUnitTree tree, JavacTask task) {
    cu = tree;
    Trees trees = Trees.instance(task);
    sourcePositions = trees.getSourcePositions();
    try {
        source = String.valueOf(tree.getSourceFile().getCharContent(true));
    } catch (IOException e) {
        throw new Error(e);
    }
    return super.visitCompilationUnit(tree, task);
}
 
Example 2
Source File: T6993305.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override public Void visitCompilationUnit(CompilationUnitTree tree, JavacTask task) {
    cu = tree;
    Trees trees = Trees.instance(task);
    sourcePositions = trees.getSourcePositions();
    try {
        source = String.valueOf(tree.getSourceFile().getCharContent(true));
    } catch (IOException e) {
        throw new Error(e);
    }
    return super.visitCompilationUnit(tree, task);
}
 
Example 3
Source File: T6993305.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override public Void visitCompilationUnit(CompilationUnitTree tree, JavacTask task) {
    cu = tree;
    Trees trees = Trees.instance(task);
    sourcePositions = trees.getSourcePositions();
    try {
        source = String.valueOf(tree.getSourceFile().getCharContent(true));
    } catch (IOException e) {
        throw new Error(e);
    }
    return super.visitCompilationUnit(tree, task);
}
 
Example 4
Source File: BIGuardedBlockHandlerFactory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean checkChange(CompilationController javac, PositionBounds span) throws IOException, BadLocationException {
    final int begin = span.getBegin().getOffset();
    final Trees trees = javac.getTrees();
    TreePath path = javac.getTreeUtilities().pathFor(begin + 1);
    if (path == null) {
        return false;
    }
    
    Element element = trees.getElement(path);
    if (element == null) {
        return false;
    }

    TreePath decl = trees.getPath(element);
    if (decl != null) {
        SourcePositions sourcePositions = trees.getSourcePositions();
        long declBegin = sourcePositions.getStartPosition(decl.getCompilationUnit(), decl.getLeaf());
        FileObject fo = SourceUtils.getFile(element, javac.getClasspathInfo());
        Document doc = javac.getDocument();
        GuardedSectionManager guards = GuardedSectionManager.getInstance((StyledDocument) doc);
        
        if (fo != javac.getFileObject() || guards != null && !isGuarded(guards, doc.createPosition((int) declBegin))) {
            // tree being refactored is declared outside of this file
            // or out of guarded sections. It should be safe to make change
            return true;
        }
    } else {
        // e.g. package; change is OK
            return true;
    }
    return false;
}
 
Example 5
Source File: JSEmbeddingProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static void colorizeJSB(final CompilationInfo ci) {
    final CompilationUnitTree cu = ci.getCompilationUnit();
    final Trees trees = ci.getTrees();
    final SourcePositions sp = trees.getSourcePositions();
    final Finder f = new Finder(trees);
    final List<LiteralTree> result = new ArrayList<>();
    f.scan(cu, result);
    if (!result.isEmpty()) {
        try {
            final TokenHierarchy<Document> tk = TokenHierarchy.get(ci.getDocument());
            final Language<?> java = Language.find(JAVA_MIME_TYPE);
            final Language<?> javaScript = Language.find(JAVASCRIPT_MIME_TYPE);
            if (java != null && javaScript != null) {
                final TokenSequence<?> seq = tk.tokenSequence(java);
                if (seq != null) {
                    for (LiteralTree lt : result) {
                        final int start = (int) sp.getStartPosition(cu, lt);
                        final int end = (int) sp.getEndPosition(cu, lt);
                        seq.move(start);
                        while (seq.moveNext() && seq.offset() < end) {
                            if (
                                seq.embedded() != null &&
                                seq.embedded().language() != null &&
                                "text/x-java-string".equals(seq.embedded().language().mimeType())
                            ) {
                                seq.removeEmbedding(seq.embedded().language());
                            }
                            seq.createEmbedding(javaScript, 1, 1, true);
                        }
                    }
                }
            }
        } catch (IOException ioe) {
            LOG.log(Level.WARNING, null, ioe);
        }
    }
}
 
Example 6
Source File: OpenTestAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 */
public void run(CompilationController controller) throws IOException {
    try {
        controller.toPhase(Phase.RESOLVED);     //cursor position needed
    } catch (IOException ex) {
        Logger.getLogger("global").log(Level.SEVERE, null, ex); //NOI18N
    }
    if (cancelled) {
        return;
    }
    
    Element element = elemHandle.resolve(controller);
    if (cancelled || (element == null)) {
        return;
    }

    Trees trees = controller.getTrees();
    CompilationUnitTree compUnit = controller.getCompilationUnit();
    DeclarationTreeFinder treeFinder = new DeclarationTreeFinder(
                                                element, trees);
    treeFinder.scan(compUnit, null);
    Tree tree = treeFinder.getDeclarationTree();

    if (tree != null) {
        SourcePositions srcPositions = trees.getSourcePositions();
        long startPos = srcPositions.getStartPosition(compUnit, tree);
        long endPos = srcPositions.getEndPosition(compUnit, tree);
        
        if ((startPos >= 0) && (startPos <= (long) Integer.MAX_VALUE)
             && (endPos >= 0) && (endPos <= (long) Integer.MAX_VALUE)) {
            positionRange = new int[2];
            positionRange[0] = (int) startPos;
            positionRange[1] = (int) endPos;
        }
    }
}
 
Example 7
Source File: ElementVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private long[] getPosition( Element e ) {
    Trees trees = cc.getTrees();
    CompilationUnitTree cut = cc.getCompilationUnit();
    Tree t = trees.getTree(e);        
    if ( t == null ) {            
        return new long[]{-1,-1};
    }        
    SourcePositions sourcePositions = trees.getSourcePositions();        
    return new long[] {sourcePositions.getStartPosition(cut, t),sourcePositions.getEndPosition(cut, t)};
}
 
Example 8
Source File: T6993305.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override public Void visitCompilationUnit(CompilationUnitTree tree, JavacTask task) {
    cu = tree;
    Trees trees = Trees.instance(task);
    sourcePositions = trees.getSourcePositions();
    try {
        source = String.valueOf(tree.getSourceFile().getCharContent(true));
    } catch (IOException e) {
        throw new Error(e);
    }
    return super.visitCompilationUnit(tree, task);
}
 
Example 9
Source File: T6993305.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override public Void visitCompilationUnit(CompilationUnitTree tree, JavacTask task) {
    cu = tree;
    Trees trees = Trees.instance(task);
    sourcePositions = trees.getSourcePositions();
    try {
        source = String.valueOf(tree.getSourceFile().getCharContent(true));
    } catch (IOException e) {
        throw new Error(e);
    }
    return super.visitCompilationUnit(tree, task);
}
 
Example 10
Source File: T6993305.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override public Void visitCompilationUnit(CompilationUnitTree tree, JavacTask task) {
    cu = tree;
    Trees trees = Trees.instance(task);
    sourcePositions = trees.getSourcePositions();
    try {
        source = String.valueOf(tree.getSourceFile().getCharContent(true));
    } catch (IOException e) {
        throw new Error(e);
    }
    return super.visitCompilationUnit(tree, task);
}
 
Example 11
Source File: T6993305.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override public Void visitCompilationUnit(CompilationUnitTree tree, JavacTask task) {
    cu = tree;
    Trees trees = Trees.instance(task);
    sourcePositions = trees.getSourcePositions();
    try {
        source = String.valueOf(tree.getSourceFile().getCharContent(true));
    } catch (IOException e) {
        throw new Error(e);
    }
    return super.visitCompilationUnit(tree, task);
}
 
Example 12
Source File: T6993305.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override public Void visitCompilationUnit(CompilationUnitTree tree, JavacTask task) {
    cu = tree;
    Trees trees = Trees.instance(task);
    sourcePositions = trees.getSourcePositions();
    try {
        source = String.valueOf(tree.getSourceFile().getCharContent(true));
    } catch (IOException e) {
        throw new Error(e);
    }
    return super.visitCompilationUnit(tree, task);
}
 
Example 13
Source File: CompletenessStressTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider = "crawler")
public void testFile(String fileName) throws IOException {
    File file = getSourceFile(fileName);
    final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    boolean success = true;
    StringWriter writer = new StringWriter();
    writer.write("Testing : " + file.toString() + "\n");
    String text = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file);
    JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, fileManager, null, null, null, compilationUnits);
    Iterable<? extends CompilationUnitTree> asts = task.parse();
    Trees trees = Trees.instance(task);
    SourcePositions sp = trees.getSourcePositions();

    for (CompilationUnitTree cut : asts) {
        for (ImportTree imp : cut.getImports()) {
            success &= testStatement(writer, sp, text, cut, imp);
        }
        for (Tree decl : cut.getTypeDecls()) {
            success &= testStatement(writer, sp, text, cut, decl);
            if (decl instanceof ClassTree) {
                ClassTree ct = (ClassTree) decl;
                for (Tree mem : ct.getMembers()) {
                    if (mem instanceof MethodTree) {
                        MethodTree mt = (MethodTree) mem;
                        BlockTree bt = mt.getBody();
                        // No abstract methods or constructors
                        if (bt != null && mt.getReturnType() != null) {
                            // The modifiers synchronized, abstract, and default are not allowed on
                            // top-level declarations and are errors.
                            Set<Modifier> modifier = mt.getModifiers().getFlags();
                            if (!modifier.contains(Modifier.ABSTRACT)
                                    && !modifier.contains(Modifier.SYNCHRONIZED)
                                    && !modifier.contains(Modifier.DEFAULT)) {
                                success &= testStatement(writer, sp, text, cut, mt);
                            }
                            testBlock(writer, sp, text, cut, bt);
                        }
                    }
                }
            }
        }
    }
    fileManager.close();
    if (!success) {
        throw new AssertionError(writer.toString());
    }
}
 
Example 14
Source File: DependenciesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
ClearAnnotations(Trees trees) {
    this.positions = trees.getSourcePositions();
}
 
Example 15
Source File: TreeContext.java    From compile-testing with Apache License 2.0 4 votes vote down vote up
TreeContext(CompilationUnitTree compilationUnit, Trees trees) {
  this.compilationUnit = compilationUnit;
  this.trees = trees;
  this.sourcePositions = trees.getSourcePositions();
  this.lineMap = compilationUnit.getLineMap();
}