本文整理汇总了Java中com.sun.source.util.SourcePositions类的典型用法代码示例。如果您正苦于以下问题:Java SourcePositions类的具体用法?Java SourcePositions怎么用?Java SourcePositions使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SourcePositions类属于com.sun.source.util包,在下文中一共展示了SourcePositions类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: adjustSpans
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
private void adjustSpans(Iterable<? extends Tree> original, String code) {
if (tree2Tag == null) {
return; //nothing to copy
}
java.util.List<Tree> linearized = new LinkedList<Tree>();
if (!new Linearize().scan(original, linearized) != Boolean.TRUE) {
return; //nothing to copy
}
ClassPath empty = ClassPathSupport.createClassPath(new URL[0]);
ClasspathInfo cpInfo = ClasspathInfo.create(JavaPlatformManager.getDefault().getDefaultPlatform().getBootstrapLibraries(), empty, empty);
JavacTaskImpl javacTask = JavacParser.createJavacTask(cpInfo, null, null, null, null, null, null, null, Arrays.asList(FileObjects.memoryFileObject("", "Scratch.java", code)));
com.sun.tools.javac.util.Context ctx = javacTask.getContext();
JavaCompiler.instance(ctx).genEndPos = true;
CompilationUnitTree tree = javacTask.parse().iterator().next(); //NOI18N
SourcePositions sp = JavacTrees.instance(ctx).getSourcePositions();
ClassTree clazz = (ClassTree) tree.getTypeDecls().get(0);
new CopyTags(tree, sp).scan(clazz.getModifiers().getAnnotations(), linearized);
}
示例2: findExactStatement
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
private StatementTree findExactStatement(CompilationInfo info, BlockTree block, int offset, boolean start) {
if (offset == (-1)) return null;
SourcePositions sp = info.getTrees().getSourcePositions();
CompilationUnitTree cut = info.getCompilationUnit();
for (StatementTree t : block.getStatements()) {
long pos = start ? sp.getStartPosition(info.getCompilationUnit(), t) : sp.getEndPosition( cut, t);
if (offset == pos) {
return t;
}
}
return null;
}
示例3: fullPrepareThis
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
private Iterable<? extends TreePath> fullPrepareThis(TreePath tp) {
//XXX: is there a faster way to do this?
Collection<TreePath> result = new LinkedList<TreePath>();
Scope scope = info.getTrees().getScope(tp);
TypeElement lastClass = null;
while (scope != null && scope.getEnclosingClass() != null) {
if (lastClass != scope.getEnclosingClass()) {
ExpressionTree thisTree = info.getTreeUtilities().parseExpression("this", new SourcePositions[1]);
info.getTreeUtilities().attributeTree(thisTree, scope);
result.add(new TreePath(tp, thisTree));
}
scope = scope.getEnclosingScope();
}
return result;
}
示例4: run
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
public boolean run(DocletEnvironment root) {
DocTrees trees = root.getDocTrees();
SourcePositions sourcePositions = trees.getSourcePositions();
for (TypeElement klass : ElementFilter.typesIn(root.getIncludedElements())) {
for (ExecutableElement method : getMethods(klass)) {
if (method.getSimpleName().toString().equals("tabbedMethod")) {
TreePath path = trees.getPath(method);
CompilationUnitTree cu = path.getCompilationUnit();
long pos = sourcePositions.getStartPosition(cu, path.getLeaf());
LineMap lineMap = cu.getLineMap();
long columnNumber = lineMap.getColumnNumber(pos);
if (columnNumber == 9) {
System.out.println(columnNumber + ": OK!");
return true;
} else {
System.err.println(columnNumber + ": wrong tab expansion");
return false;
}
}
}
}
return false;
}
示例5: testWrapMethod1
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
public void testWrapMethod1() throws Exception {
String code = "package hierbas.del.litoral;\n\n" +
"import java.util.concurrent.atomic.AtomicBoolean;\n\n" +
"public class Test {\n" +
"}\n";
runWrappingTest(code, new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
ExpressionTree parsed = workingCopy.getTreeUtilities().parseExpression("new Object() { private void test(int a, int b, int c) throws java.io.FileNotFound, java.net.MalformedURLException { } }", new SourcePositions[1]);
parsed = GeneratorUtilities.get(workingCopy).importFQNs(parsed);
MethodTree method = (MethodTree) ((NewClassTree) parsed).getClassBody().getMembers().get(0);
workingCopy.rewrite(clazz, make.addClassMember(clazz, method));
}
},
FmtOptions.wrapMethodParams, WrapStyle.WRAP_IF_LONG.name(),
FmtOptions.wrapThrowsKeyword, WrapStyle.WRAP_IF_LONG.name(),
FmtOptions.wrapThrowsList, WrapStyle.WRAP_IF_LONG.name());
}
示例6: testWrapMethod2
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
public void testWrapMethod2() throws Exception {
String code = "package hierbas.del.litoral;\n\n" +
"import java.util.concurrent.atomic.AtomicBoolean;\n\n" +
"public class Test {\n" +
"}\n";
runWrappingTest(code, new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
ExpressionTree parsed = workingCopy.getTreeUtilities().parseExpression("new Object() { private void test(int a, int b, int c) throws java.io.FileNotFoundException, java.net.MalformedURLException { } }", new SourcePositions[1]);
parsed = GeneratorUtilities.get(workingCopy).importFQNs(parsed);
MethodTree method = (MethodTree) ((NewClassTree) parsed).getClassBody().getMembers().get(0);
workingCopy.rewrite(clazz, make.addClassMember(clazz, method));
}
},
FmtOptions.wrapMethodParams, WrapStyle.WRAP_ALWAYS.name(),
FmtOptions.wrapThrowsKeyword, WrapStyle.WRAP_ALWAYS.name(),
FmtOptions.wrapThrowsList, WrapStyle.WRAP_ALWAYS.name());
}
示例7: testWrapMethod3
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
public void testWrapMethod3() throws Exception {
String code = "package hierbas.del.litoral;\n\n" +
"import java.util.concurrent.atomic.AtomicBoolean;\n\n" +
"public class Test {\n" +
"}\n";
runWrappingTest(code, new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
ExpressionTree parsed = workingCopy.getTreeUtilities().parseExpression("new Object() { public <T> void test(java.lang.Class<T> c) { } }", new SourcePositions[1]);
parsed = GeneratorUtilities.get(workingCopy).importFQNs(parsed);
MethodTree method = (MethodTree) ((NewClassTree) parsed).getClassBody().getMembers().get(0);
workingCopy.rewrite(clazz, make.addClassMember(clazz, method));
}
},
FmtOptions.wrapMethodParams, WrapStyle.WRAP_ALWAYS.name(),
FmtOptions.wrapThrowsKeyword, WrapStyle.WRAP_ALWAYS.name(),
FmtOptions.wrapThrowsList, WrapStyle.WRAP_ALWAYS.name());
}
示例8: testDisableAccessRightsCrash
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
public void testDisableAccessRightsCrash() throws Exception {
ClassPath boot = ClassPathSupport.createClassPath(SourceUtilsTestUtil.getBootClassPath().toArray(new URL[0]));
FileObject testFile = FileUtil.createData(FileUtil.createMemoryFileSystem().getRoot(), "Test.java");
try (Writer w = new OutputStreamWriter(testFile.getOutputStream())) {
w.append("public class Test {}");
}
JavaSource js = JavaSource.create(ClasspathInfo.create(boot, ClassPath.EMPTY, ClassPath.EMPTY), testFile);
js.runUserActionTask(new Task<CompilationController>() {
@Override
public void run(CompilationController parameter) throws Exception {
parameter.toPhase(Phase.RESOLVED);
TreePath clazzPath = new TreePath(new TreePath(parameter.getCompilationUnit()),
parameter.getCompilationUnit().getTypeDecls().get(0));
Scope scope = parameter.getTrees().getScope(clazzPath);
Scope disableScope = parameter.getTreeUtilities().toScopeWithDisabledAccessibilityChecks(scope);
ExpressionTree et = parameter.getTreeUtilities().parseExpression("1 + 1", new SourcePositions[1]);
parameter.getTreeUtilities().attributeTree(et, disableScope);
}
}, true);
}
示例9: findTokenWithText
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
private static Token<JavaTokenId> findTokenWithText(CompilationInfo info, String text, int start, int end) {
TokenHierarchy<?> th = info.getTokenHierarchy();
TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language()).subSequence(start, end);
while (ts.moveNext()) {
Token<JavaTokenId> t = ts.token();
if (t.id() == JavaTokenId.IDENTIFIER) {
boolean nameMatches;
if (!(nameMatches = text.equals(info.getTreeUtilities().decodeIdentifier(t.text()).toString()))) {
ExpressionTree expr = info.getTreeUtilities().parseExpression(t.text().toString(), new SourcePositions[1]);
nameMatches = expr.getKind() == Kind.IDENTIFIER && text.contentEquals(((IdentifierTree) expr).getName());
}
if (nameMatches) {
return t;
}
}
}
return null;
}
示例10: findIdentifierSpanImpl
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
private static Token<JavaTokenId> findIdentifierSpanImpl(CompilationInfo info, IdentifierTree tree, CompilationUnitTree cu, SourcePositions positions) {
int start = (int)positions.getStartPosition(cu, tree);
int endPosition = (int)positions.getEndPosition(cu, tree);
if (start == (-1) || endPosition == (-1))
return null;
TokenHierarchy<?> th = info.getTokenHierarchy();
TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());
if (ts.move(start) == Integer.MAX_VALUE) {
return null;
}
if (ts.moveNext()) {
if (ts.offset() >= start) {
Token<JavaTokenId> t = ts.token();
return t;
}
}
return null;
}
示例11: getNewCompatibleArguments
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
private List<ExpressionTree> getNewCompatibleArguments(List<? extends VariableTree> parameters) {
List<ExpressionTree> arguments = new ArrayList();
ParameterInfo[] pi = paramInfos;
for (int i = 0; i < pi.length; i++) {
int originalIndex = pi[i].getOriginalIndex();
ExpressionTree vt;
String value;
if (originalIndex < 0) {
value = pi[i].getDefaultValue();
} else {
value = parameters.get(originalIndex).getName().toString();
}
SourcePositions pos[] = new SourcePositions[1];
vt = workingCopy.getTreeUtilities().parseExpression(value, pos);
arguments.add(vt);
}
return arguments;
}
示例12: treeEquals
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
private boolean treeEquals(WorkingCopy copy, Collection<? extends TreePath> value, String defaultValue) {
if (defaultValue == null) {
return false;
}
String[] values = defaultValue.split(",");
if(values.length != value.size()) {
return false;
}
for (String defValue : values) {
defValue = defValue.trim();
boolean parsed = false;
if (value instanceof LiteralTree) {
ExpressionTree parseExpression = copy.getTreeUtilities().parseExpression(defValue, new SourcePositions[1]);
parsed = parseExpression instanceof LiteralTree;
if (parsed && !((LiteralTree) value).getValue().equals(((LiteralTree) parseExpression).getValue())) {
return false;
}
}
if(!parsed && !defValue.equals(value.toString())) {
return false;
}
}
return true;
}
示例13: testNewClassWithEnclosing
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
@Test
void testNewClassWithEnclosing() throws IOException {
final String theString = "Test.this.new d()";
String code = "package test; class Test { " +
"class d {} private void method() { " +
"Object o = " + theString + "; } }";
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next();
SourcePositions pos = Trees.instance(ct).getSourcePositions();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
ExpressionTree est =
((VariableTree) ((MethodTree) clazz.getMembers().get(1)).getBody().getStatements().get(0)).getInitializer();
final int spos = code.indexOf(theString);
final int epos = spos + theString.length();
assertEquals("testNewClassWithEnclosing",
spos, pos.getStartPosition(cut, est));
assertEquals("testNewClassWithEnclosing",
epos, pos.getEndPosition(cut, est));
}
示例14: visitClass
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
@Override
public Void visitClass(ClassTree node, Void p) {
final SourcePositions sourcePositions = wc.getTrees().getSourcePositions();
final TreeMaker make = wc.getTreeMaker();
List<Tree> members = new LinkedList<Tree>();
ClassTree classTree = node;
for (Tree member : node.getMembers()) {
int s = (int) sourcePositions.getStartPosition(wc.getCompilationUnit(), member);
int e = (int) sourcePositions.getEndPosition(wc.getCompilationUnit(), member);
if (s >= start && e <= end) {
classTree = make.removeClassMember(classTree, member);
members.add(member);
}
}
classTree = GeneratorUtils.insertClassMembers(wc, classTree, members, start);
wc.rewrite(node, classTree);
return super.visitClass(classTree, p);
}
示例15: testPositionForEnumModifiers
import com.sun.source.util.SourcePositions; //导入依赖的package包/类
@Test
void testPositionForEnumModifiers() throws IOException {
final String theString = "public";
String code = "package test; " + theString + " enum Test {A;}";
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next();
SourcePositions pos = Trees.instance(ct).getSourcePositions();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
ModifiersTree mt = clazz.getModifiers();
int spos = code.indexOf(theString);
int epos = spos + theString.length();
assertEquals("testPositionForEnumModifiers",
spos, pos.getStartPosition(cut, mt));
assertEquals("testPositionForEnumModifiers",
epos, pos.getEndPosition(cut, mt));
}