本文整理匯總了Java中com.sun.source.util.Trees.getSourcePositions方法的典型用法代碼示例。如果您正苦於以下問題:Java Trees.getSourcePositions方法的具體用法?Java Trees.getSourcePositions怎麽用?Java Trees.getSourcePositions使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.source.util.Trees
的用法示例。
在下文中一共展示了Trees.getSourcePositions方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: checkChange
import com.sun.source.util.Trees; //導入方法依賴的package包/類
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;
}
示例2: run
import com.sun.source.util.Trees; //導入方法依賴的package包/類
/**
*/
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;
}
}
}
示例3: getPosition
import com.sun.source.util.Trees; //導入方法依賴的package包/類
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)};
}
示例4: visitCompilationUnit
import com.sun.source.util.Trees; //導入方法依賴的package包/類
@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);
}
示例5: testFile
import com.sun.source.util.Trees; //導入方法依賴的package包/類
@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());
}
}
示例6: ClearAnnotations
import com.sun.source.util.Trees; //導入方法依賴的package包/類
ClearAnnotations(Trees trees) {
this.positions = trees.getSourcePositions();
}