本文整理汇总了Java中com.sun.source.util.TreePath类的典型用法代码示例。如果您正苦于以下问题:Java TreePath类的具体用法?Java TreePath怎么用?Java TreePath使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TreePath类属于com.sun.source.util包,在下文中一共展示了TreePath类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: computeMethodInvocation
import com.sun.source.util.TreePath; //导入依赖的package包/类
private static List<? extends TypeMirror> computeMethodInvocation(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
MethodInvocationTree nat = (MethodInvocationTree) parent.getLeaf();
boolean errorInRealArguments = false;
for (Tree param : nat.getArguments()) {
errorInRealArguments |= param == error;
}
if (errorInRealArguments) {
List<TypeMirror> proposedTypes = new ArrayList<TypeMirror>();
int[] proposedIndex = new int[1];
List<ExecutableElement> ee = fuzzyResolveMethodInvocation(info, parent, proposedTypes, proposedIndex);
if (ee.isEmpty()) { //cannot be resolved
return null;
}
types.add(ElementKind.PARAMETER);
types.add(ElementKind.LOCAL_VARIABLE);
types.add(ElementKind.FIELD);
return proposedTypes;
}
return null;
}
示例2: visitNewClass
import com.sun.source.util.TreePath; //导入依赖的package包/类
public Boolean visitNewClass(NewClassTree node, TreePath p) {
if (p == null)
return super.visitNewClass(node, p);
NewClassTree t = (NewClassTree) p.getLeaf();
if (!scan(node.getIdentifier(), t.getIdentifier(), p))
return false;
if (!scan(node.getEnclosingExpression(), t.getEnclosingExpression(), p))
return false;
if (!checkLists(node.getTypeArguments(), t.getTypeArguments(), p))
return false;
if (!checkLists(node.getArguments(), t.getArguments(), p))
return false;
return scan(node.getClassBody(), t.getClassBody(), p);
}
示例3: run
import com.sun.source.util.TreePath; //导入依赖的package包/类
public List<Fix> run(CompilationInfo info, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
List<Fix> result = new ArrayList<Fix>();
List<TypeMirror> targetType = new ArrayList<TypeMirror>();
TreePath[] tmTree = new TreePath[1];
ExpressionTree[] expression = new ExpressionTree[1];
Tree[] leaf = new Tree[1];
computeType(info, offset, targetType, tmTree, expression, leaf);
if (!targetType.isEmpty()) {
TreePath expressionPath = TreePath.getPath(info.getCompilationUnit(), expression[0]); //XXX: performance
for (TypeMirror type : targetType) {
if (type.getKind() != TypeKind.NULL) {
result.add(new AddCastFix(info, expressionPath, tmTree[0], type).toEditorFix());
}
}
}
return result;
}
示例4: test126732
import com.sun.source.util.TreePath; //导入依赖的package包/类
public void test126732() throws Exception {
FileObject file = FileUtil.createData(sourceRoot, "test/test.java");
String code = "package test;\n" +
"public class Test {\n" +
" public static void test() {\n" +
" return Runnable() {\n" +
" new Runnable() {\n" +
" };\n" +
" }\n" +
"}";
writeIntoFile(file,code);
JavaSource js = JavaSource.forFileObject(file);
CompilationInfo info = SourceUtilsTestUtil.getCompilationInfo(js, Phase.RESOLVED);
TreePath tp = info.getTreeUtilities().pathFor(code.indexOf("new Runnable() {"));
TreePathHandle handle = TreePathHandle.create(tp, info);
TreePath resolved = handle.resolve(info);
assertNotNull(resolved);
assertTrue(tp.getLeaf() == resolved.getLeaf());
}
示例5: createSuppressWarnings
import com.sun.source.util.TreePath; //导入依赖的package包/类
/** Creates a fix, which when invoked adds @SuppresWarnings(keys) to
* nearest declaration.
* @param compilationInfo CompilationInfo to work on
* @param treePath TreePath to a tree. The method will find nearest outer
* declaration. (type, method, field or local variable)
* @param keys keys to be contained in the SuppresWarnings annotation. E.g.
* @SuppresWarnings( "key" ) or @SuppresWarnings( {"key1", "key2", ..., "keyN" } ).
* @throws IllegalArgumentException if keys are null or empty or id no suitable element
* to put the annotation on is found (e.g. if TreePath to CompilationUnit is given")
*/
static List<Fix> createSuppressWarnings(CompilationInfo compilationInfo, TreePath treePath, String... keys ) {
Parameters.notNull("compilationInfo", compilationInfo);
Parameters.notNull("treePath", treePath);
Parameters.notNull("keys", keys);
if (keys.length == 0) {
throw new IllegalArgumentException("key must not be empty"); // NOI18N
}
Fix f = createSuppressWarningsFix(compilationInfo, treePath, keys);
if (f != null) {
return Collections.<Fix>singletonList(f);
} else {
return Collections.emptyList();
}
}
示例6: TreeNode
import com.sun.source.util.TreePath; //导入依赖的package包/类
public TreeNode(CompilationInfo info, TreePath tree, List<Node> nodes) {
super(nodes.isEmpty() ? Children.LEAF: new NodeChilren(nodes), Lookups.singleton(tree.getLeaf()));
this.tree = tree;
this.info = info;
this.synthetic = info.getTreeUtilities().isSynthetic(tree);
int start = (int) info.getTrees().getSourcePositions().getStartPosition(tree.getCompilationUnit(), tree.getLeaf());
int end = (int) info.getTrees().getSourcePositions().getEndPosition(tree.getCompilationUnit(), tree.getLeaf());
String text;
if (start >= 0 && end >= 0 && end > start) {
text = info.getText().substring(start, end);
} else {
text = tree.getLeaf().toString();
}
setDisplayName(tree.getLeaf().getKind().toString() + ":" + text); //NOI18N
setIconBaseWithExtension("org/netbeans/modules/java/debug/resources/tree.png"); //NOI18N
}
示例7: testOutVariables1
import com.sun.source.util.TreePath; //导入依赖的package包/类
public void testOutVariables1() throws Exception {
String code = "package test; public class Test { private int test() { int i = test(|); } }";
int pos = code.indexOf("|");
code = code.replaceAll(Pattern.quote("|"), "");
prepareTest("test/Test.java", code);
TreePath tp = info.getTreeUtilities().pathFor(pos);
HintContext ctx = SPIAccessor.getINSTANCE().createHintContext(info, HintsSettings.getGlobalSettings(), null, tp, Collections.<String, TreePath>emptyMap(), Collections.<String, Collection<? extends TreePath>>emptyMap(), Collections.<String, String>emptyMap());
Map<String, TreePath> outVariables = new HashMap<String, TreePath>();
Map<String, Collection<? extends TreePath>> outMultiVariables = new HashMap<String, Collection<? extends TreePath>>();
Map<String, String> outVariables2Names = new HashMap<String, String>();
assertTrue(MatcherUtilities.matches(ctx, ctx.getPath().getParentPath(), "$1 $0 = $_;", outVariables, outMultiVariables, outVariables2Names));
assertEquals("int", outVariables.get("$1").getLeaf().toString());
assertEquals("i", outVariables2Names.get("$0"));
}
示例8: checkZeroSizeArray
import com.sun.source.util.TreePath; //导入依赖的package包/类
private static boolean checkZeroSizeArray(CompilationInfo info, TreePath val) {
if (val.getLeaf().getKind() != Tree.Kind.VARIABLE) {
return false;
}
VariableTree vt = (VariableTree)val.getLeaf();
ExpressionTree xpr = vt.getInitializer();
if (xpr == null) {
return false;
}
if (xpr.getKind() == Tree.Kind.NEW_ARRAY) {
NewArrayTree nat = (NewArrayTree)xpr;
List<? extends ExpressionTree> dims = nat.getDimensions();
if (dims != null && !dims.isEmpty()) {
Object size = ArithmeticUtilities.compute(info,
new TreePath(
new TreePath(val, xpr),
dims.get(dims.size() -1)),
true);
return ArithmeticUtilities.isRealValue(size) && Integer.valueOf(0).equals(size);
} else {
return nat.getInitializers() != null && nat.getInitializers().isEmpty();
}
}
return false;
}
示例9: analyzeCollectionAccess
import com.sun.source.util.TreePath; //导入依赖的package包/类
private JavaWhereUsedFilters.ReadWrite analyzeCollectionAccess(TreePath path) {
JavaWhereUsedFilters.ReadWrite result = null;
TreePath parentPath = path.getParentPath();
Tree parentTree = parentPath.getLeaf();
Kind parentKind = parentTree.getKind();
if(parentKind == Kind.MEMBER_SELECT) {
Element member = workingCopy.getTrees().getElement(parentPath);
if(member != null && member.getKind() == ElementKind.METHOD) {
ExecutableElement method = (ExecutableElement) member;
if (writeMethods.contains(method.getSimpleName().toString())) {
result = JavaWhereUsedFilters.ReadWrite.WRITE;
} else if (readMethods.contains(method.getSimpleName().toString())) {
result = JavaWhereUsedFilters.ReadWrite.READ;
} else if (readWriteMethods.contains(method.getSimpleName().toString())) {
result = JavaWhereUsedFilters.ReadWrite.READ_WRITE;
}
}
}
return result;
}
示例10: findIdentifierSpan
import com.sun.source.util.TreePath; //导入依赖的package包/类
public static int[] findIdentifierSpan( final TreePath decl, final CompilationInfo info, final Document doc) {
final int[] result = new int[] {-1, -1};
Runnable r = new Runnable() {
public void run() {
Token<JavaTokenId> t = findIdentifierSpan(info, doc, decl);
if (t != null) {
result[0] = t.offset(null);
result[1] = t.offset(null) + t.length();
}
}
};
if (doc != null) {
doc.render(r);
} else {
r.run();
}
return result;
}
示例11: visitReference
import com.sun.source.util.TreePath; //导入依赖的package包/类
@Override
public DocTree visitReference(ReferenceTree node, Element elementToFind) {
DocTreePath currentDocPath = getCurrentDocPath();
DocTrees trees = workingCopy.getDocTrees();
Element el = trees.getElement(currentDocPath);
ExpressionTree classReference = workingCopy.getTreeUtilities().getReferenceClass(currentDocPath);
if((el == null || !(el.equals(elementToFind) || isMethodMatch(el))) && classReference != null) {
el = trees.getElement(new TreePath(getCurrentPath(), classReference));
}
if (el != null && (el.equals(elementToFind) || isMethodMatch(el))) {
ReferenceTree newRef;
Name memberName = workingCopy.getTreeUtilities().getReferenceName(currentDocPath);
List<? extends Tree> methodParameters = workingCopy.getTreeUtilities().getReferenceParameters(currentDocPath);
if(el.getKind().isClass() || el.getKind().isInterface()) {
newRef = make.Reference(make.setLabel(classReference, newName), memberName, methodParameters);
} else {
newRef = make.Reference(classReference, newName, methodParameters);
}
rewrite(currentDocPath.getTreePath().getLeaf(), node, newRef);
}
return super.visitReference(node, elementToFind);
}
示例12: resolveDeclarationContext
import com.sun.source.util.TreePath; //导入依赖的package包/类
private static TreePath resolveDeclarationContext(TreePath usage) {
TreePath declaration = usage;
while (declaration != null) {
switch (declaration.getLeaf().getKind()) {
case BLOCK:
if (TreeUtilities.CLASS_TREE_KINDS.contains(declaration.getParentPath().getLeaf().getKind())) {
// it is static or instance initializer
return declaration;
}
break;
case METHOD:
return declaration;
case VARIABLE:
if (TreeUtilities.CLASS_TREE_KINDS.contains(declaration.getParentPath().getLeaf().getKind())) {
// it is field declaration
// private int field = init();
return declaration;
}
break;
}
declaration = declaration.getParentPath();
}
return null;
}
示例13: scan
import com.sun.source.util.TreePath; //导入依赖的package包/类
@Override
public Void scan(TreePath path, Map<HintDescription, List<ErrorDescription>> p) {
Kind k = path.getLeaf().getKind();
boolean b = pushSuppressWarrnings(path);
try {
runAndAdd(path, hints.get(k), p);
if (isCanceled()) {
return null;
}
return super.scan(path, p);
} finally {
if (b) {
suppresWarnings.pop();
}
}
}
示例14: visitMethodDef
import com.sun.source.util.TreePath; //导入依赖的package包/类
@Override
public void visitMethodDef(JCMethodDecl tree) {
cancelService.abortIfCanceled();
JCBlock body = tree.body;
try {
super.visitMethodDef(tree);
} finally {
//reinstall body:
tree.body = body;
}
if (trees instanceof NBJavacTrees && !env.enclClass.defs.contains(tree)) {
TreePath path = trees.getPath(env.toplevel, env.enclClass);
if (path != null) {
((NBJavacTrees)trees).addPathForElement(tree.sym, new TreePath(path, tree));
}
}
}
示例15: performRewrite
import com.sun.source.util.TreePath; //导入依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
// path should be the typecast expression
TreePath path = ctx.getPath();
TreePath exprPath = exprHandle.resolve(ctx.getWorkingCopy());
if (path.getLeaf().getKind() != Tree.Kind.TYPE_CAST || exprPath == null || !EXPRESSION_KINDS.contains(exprPath.getLeaf().getKind())) {
// PENDING - some message ?
return;
}
WorkingCopy copy = ctx.getWorkingCopy();
TreeMaker make = ctx.getWorkingCopy().getTreeMaker();
TypeCastTree cast = (TypeCastTree)path.getLeaf();
// rewrite the type cast to the casted Math.random()
copy.rewrite(path.getLeaf(), cast.getExpression());
// rewrite the outermost expression to a typecast of it
ExpressionTree expr = (ExpressionTree)exprPath.getLeaf();
if (expr.getKind() != Tree.Kind.PARENTHESIZED) {
expr = make.Parenthesized(expr);
}
copy.rewrite(exprPath.getLeaf(), make.TypeCast(cast.getType(), (ExpressionTree)expr));
}