Java Code Examples for com.sun.source.util.Trees#getPath()
The following examples show how to use
com.sun.source.util.Trees#getPath() .
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: TypesCachesCleared.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { new TestPathScanner<Void>() { @Override public void visit(Void t) { } }; TypeElement currentClass = elements.getTypeElement("TypesCachesCleared"); Trees trees = Trees.instance(processingEnv); TreePath path = trees.getPath(currentClass); new TreePathScanner<Void, Void>() { @Override public Void visitClass(ClassTree node, Void p) { trees.getElement(getCurrentPath()); return super.visitClass(node, p); } }.scan(path, null); return false; }
Example 2
Source File: FindUsagesVisitor.java From netbeans with Apache License 2.0 | 6 votes |
@Override public Tree visitNewClass(NewClassTree node, Element p) { if(isCancelled.get()) { return null; } Trees trees = workingCopy.getTrees(); ClassTree classTree = ((NewClassTree) node).getClassBody(); if (classTree != null && p.getKind() == ElementKind.CONSTRUCTOR) { for (Tree t : classTree.getMembers()) { Element elem = workingCopy.getTrees().getElement(TreePath.getPath(workingCopy.getCompilationUnit(), t)); if ((elem != null) && (elem.getKind() == ElementKind.CONSTRUCTOR)) { TreePath superCall = trees.getPath(workingCopy.getCompilationUnit(), ((ExpressionStatementTree) ((MethodTree) t).getBody().getStatements().get(0)).getExpression()); Element superCallElement = trees.getElement(superCall); if (superCallElement != null && superCallElement.equals(p) && !workingCopy.getTreeUtilities().isSynthetic(superCall)) { addUsage(superCall); } } } } else { addIfMatch(getCurrentPath(), node, p); } return super.visitNewClass(node, p); }
Example 3
Source File: JavadocCompletionQuery.java From netbeans with Apache License 2.0 | 6 votes |
private void addInnerClasses(TypeElement te, EnumSet<ElementKind> kinds, DeclaredType baseType, Set<? extends Element> toExclude, String prefix, int substitutionOffset, JavadocContext jdctx) { CompilationInfo controller = jdctx.javac; Element srcEl = jdctx.handle.resolve(controller); Elements elements = controller.getElements(); Types types = controller.getTypes(); Trees trees = controller.getTrees(); TreeUtilities tu = controller.getTreeUtilities(); TreePath docpath = srcEl != null ? trees.getPath(srcEl) : null; Scope scope = docpath != null ? trees.getScope(docpath) : tu.scopeFor(caretOffset); for (Element e : controller.getElementUtilities().getMembers(te.asType(), null)) { if ((e.getKind().isClass() || e.getKind().isInterface()) && (toExclude == null || !toExclude.contains(e))) { String name = e.getSimpleName().toString(); if (Utilities.startsWith(name, prefix) && (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(e)) && trees.isAccessible(scope, (TypeElement)e) && isOfKindAndType(e.asType(), e, kinds, baseType, scope, trees, types)) { items.add(JavadocCompletionItem.createTypeItem(jdctx.javac, (TypeElement) e, substitutionOffset, null, elements.isDeprecated(e)/*, isOfSmartType(env, e.asType(), smartTypes)*/)); } } } }
Example 4
Source File: BIGuardedBlockHandlerFactory.java From netbeans with Apache License 2.0 | 5 votes |
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: ModelChecker.java From hottub with GNU General Public License v2.0 | 5 votes |
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { if (roundEnv.processingOver()) return true; Trees trees = Trees.instance(processingEnv); TypeElement testAnno = elements.getTypeElement("Check"); for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) { TreePath p = trees.getPath(elem); new MulticatchParamTester(trees).scan(p, null); } return true; }
Example 6
Source File: TestMultipleErrors.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { for (Element root : roundEnv.getRootElements()) { processingEnv.getMessager().printMessage(Kind.ERROR, "error1", root); processingEnv.getMessager().printMessage(Kind.ERROR, "error2", root); Trees trees = Trees.instance(processingEnv); TreePath path = trees.getPath(root); trees.printMessage(Kind.ERROR, "error3", path.getLeaf(), path.getCompilationUnit()); trees.printMessage(Kind.ERROR, "error4", path.getLeaf(), path.getCompilationUnit()); } return true; }
Example 7
Source File: ChangeParamsTransformer.java From netbeans with Apache License 2.0 | 5 votes |
/** * special treatment for anonymous classes to resolve the proper constructor * of extended class instead of the synthetic one. * @see <a href="https://netbeans.org/bugzilla/show_bug.cgi?id=168775">#168775</a> */ private Element resolveAnonymousClassConstructor(Element el, NewClassTree tree, final Trees trees) { if (el != null && tree.getClassBody() != null) { Tree t = trees.getTree(el); if (t != null && t.getKind() == Tree.Kind.METHOD) { MethodTree constructorTree = (MethodTree) t; Tree superCall = constructorTree.getBody().getStatements().get(0); TreePath superCallPath = trees.getPath( getCurrentPath().getCompilationUnit(), ((ExpressionStatementTree) superCall).getExpression()); el = trees.getElement(superCallPath); } } return el; }
Example 8
Source File: ModelChecker.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { if (roundEnv.processingOver()) return true; Trees trees = Trees.instance(processingEnv); TypeElement testAnno = elements.getTypeElement("Check"); for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) { TreePath p = trees.getPath(elem); new IntersectionCastTester(trees).scan(p, null); } return true; }
Example 9
Source File: ModelBuilder.java From vertx-codetrans with Apache License 2.0 | 5 votes |
public ModelBuilder(Trees trees, TypeElement typeElt, DeclaredType systemType, DeclaredType throwableType, TypeMirrorFactory factory, Types typeUtils, Lang lang) { this.path = trees.getPath(typeElt); this.trees = trees; this.systemType = systemType; this.throwableType = throwableType; this.factory = factory; this.typeUtils = typeUtils; this.typeElt = typeElt; }
Example 10
Source File: JavaSourceHelper.java From netbeans with Apache License 2.0 | 5 votes |
public static TypeElement getTopLevelClassElement(CompilationController controller) { ClassTree classTree = getTopLevelClassTree(controller); if (classTree == null) { return null; } Trees trees = controller.getTrees(); TreePath path = trees.getPath(controller.getCompilationUnit(), classTree); return (TypeElement) trees.getElement(path); }
Example 11
Source File: TestGetScope.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { Trees trees = Trees.instance(processingEnv); if (round++ == 0) { for (Element e: roundEnv.getRootElements()) { TreePath p = trees.getPath(e); new Scanner().scan(p, trees); } } return false; }
Example 12
Source File: TestGetScope.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { Trees trees = Trees.instance(processingEnv); if (round++ == 0) { for (Element e: roundEnv.getRootElements()) { TreePath p = trees.getPath(e); new Scanner().scan(p, trees); } } return false; }
Example 13
Source File: ModelChecker.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { if (roundEnv.processingOver()) return true; Trees trees = Trees.instance(processingEnv); TypeElement testAnno = elements.getTypeElement("Check"); for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) { TreePath p = trees.getPath(elem); new MulticatchParamTester(trees).scan(p, null); } return true; }
Example 14
Source File: TestGetScope.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { Trees trees = Trees.instance(processingEnv); if (round++ == 0) { for (Element e: roundEnv.getRootElements()) { TreePath p = trees.getPath(e); new Scanner().scan(p, trees); } } return false; }
Example 15
Source File: IntroduceLocalExtensionTransformer.java From netbeans with Apache License 2.0 | 4 votes |
@Override public Tree visitAssignment(AssignmentTree node, Element p) { if (!refactoring.getReplace()) { return super.visitAssignment(node, p); } ExpressionTree variable = node.getVariable(); boolean isArray = false; while (variable.getKind() == Tree.Kind.ARRAY_ACCESS) { isArray = true; // int[] a; a[a[0]][a[1]] = 0; // scan also array indices scan(((ArrayAccessTree) variable).getIndex(), p); variable = ((ArrayAccessTree) variable).getExpression(); } Element el = workingCopy.getTrees().getElement(new TreePath(getCurrentPath(), variable)); if(el != null) { Element enclosingElement = el.getEnclosingElement(); if (enclosingElement != null && enclosingElement.equals(p) && (isArray || checkAssignmentInsideExpression())) { ElementHandle<Element> handle = ElementHandle.create(el); String[] getterSetter = getterSetterMap.get(handle); // TODO: Check for null and solve ! if (getterSetter != null) { if (isArray) { ExpressionTree invkgetter = createGetterInvokation(variable, getterSetter[0]); rewrite(variable, invkgetter); } else { ExpressionTree setter = createMemberSelection(variable, getterSetter[1]); // resolve types Trees trees = workingCopy.getTrees(); ExpressionTree expTree = node.getExpression(); ExpressionTree newExpTree; TreePath varPath = trees.getPath(workingCopy.getCompilationUnit(), variable); TreePath expPath = trees.getPath(workingCopy.getCompilationUnit(), expTree); TypeMirror varType = trees.getTypeMirror(varPath); TypeMirror expType = trees.getTypeMirror(expPath); if (workingCopy.getTypes().isSubtype(expType, varType)) { newExpTree = expTree; } else { newExpTree = make.TypeCast(make.Type(varType), expTree); } MethodInvocationTree invksetter = make.MethodInvocation( Collections.<ExpressionTree>emptyList(), setter, Collections.singletonList(newExpTree)); rewrite(node, invksetter); } } } } return scan(node.getExpression(), p); }
Example 16
Source File: IntroduceLocalExtensionTransformer.java From netbeans with Apache License 2.0 | 4 votes |
@Override public Tree visitUnary(UnaryTree node, Element p) { if (!refactoring.getReplace()) { return super.visitUnary(node, p); } ExpressionTree t = node.getExpression(); Tree.Kind kind = node.getKind(); boolean isArrayOrImmutable = kind != Tree.Kind.POSTFIX_DECREMENT && kind != Tree.Kind.POSTFIX_INCREMENT && kind != Tree.Kind.PREFIX_DECREMENT && kind != Tree.Kind.PREFIX_INCREMENT; while (t.getKind() == Tree.Kind.ARRAY_ACCESS) { isArrayOrImmutable = true; t = ((ArrayAccessTree) t).getExpression(); } Element el = workingCopy.getTrees().getElement(new TreePath(getCurrentPath(), t)); if (el != null) { Element enclosingElement = el.getEnclosingElement(); if (enclosingElement != null && enclosingElement.equals(p) && (isArrayOrImmutable || checkAssignmentInsideExpression())) { ElementHandle<Element> handle = ElementHandle.create(el); String[] getterSetter = getterSetterMap.get(handle); // TODO: Check for null and solve ! // check (++field + 3) if (getterSetter != null) { ExpressionTree invkgetter = createGetterInvokation(t, getterSetter[0]); if (isArrayOrImmutable) { rewrite(t, invkgetter); } else { ExpressionTree setter = createMemberSelection(node.getExpression(), getterSetter[1]); Tree.Kind operator = kind == Tree.Kind.POSTFIX_INCREMENT || kind == Tree.Kind.PREFIX_INCREMENT ? Tree.Kind.PLUS : Tree.Kind.MINUS; // resolve types Trees trees = workingCopy.getTrees(); ExpressionTree expTree = node.getExpression(); TreePath varPath = trees.getPath(workingCopy.getCompilationUnit(), expTree); TypeMirror varType = trees.getTypeMirror(varPath); TypeMirror expType = workingCopy.getTypes().getPrimitiveType(TypeKind.INT); ExpressionTree newExpTree = make.Binary(operator, invkgetter, make.Literal(1)); if (!workingCopy.getTypes().isSubtype(expType, varType)) { newExpTree = make.TypeCast(make.Type(varType), make.Parenthesized(newExpTree)); } MethodInvocationTree invksetter = make.MethodInvocation( Collections.<ExpressionTree>emptyList(), setter, Collections.singletonList(newExpTree)); rewrite(node, invksetter); } } } } return null; }
Example 17
Source File: VarUsageVisitor.java From netbeans with Apache License 2.0 | 4 votes |
private Element asElement(Tree tree) { Trees treeUtil = workingCopy.getTrees(); TreePath treePath = treeUtil.getPath(workingCopy.getCompilationUnit(), tree); Element element = treeUtil.getElement(treePath); return element; }
Example 18
Source File: FindUsagesVisitor.java From netbeans with Apache License 2.0 | 4 votes |
private void addIfMatch(TreePath path, Tree tree, Element elementToFind) { if(isCancelled.get()) { return; } if (JavaPluginUtils.isSyntheticPath(workingCopy, path)) { if (ElementKind.CONSTRUCTOR != elementToFind.getKind() || tree.getKind() != Tree.Kind.IDENTIFIER || !"super".contentEquals(((IdentifierTree) tree).getName())) { // NOI18N // do not skip synthetic usages of constructor return; } } Trees trees = workingCopy.getTrees(); Element el = trees.getElement(path); if (el == null) { path = path.getParentPath(); if (path != null && path.getLeaf().getKind() == Kind.IMPORT) { ImportTree impTree = (ImportTree) path.getLeaf(); if (!impTree.isStatic()) { return; } Tree idTree = impTree.getQualifiedIdentifier(); if (idTree.getKind() != Kind.MEMBER_SELECT) { return; } final Name id = ((MemberSelectTree) idTree).getIdentifier(); if (id.contentEquals("*")) { return; } Tree classTree = ((MemberSelectTree) idTree).getExpression(); path = trees.getPath(workingCopy.getCompilationUnit(), classTree); el = trees.getElement(path); if (el == null) { return; } Iterator iter = workingCopy.getElementUtilities().getMembers(el.asType(), new ElementUtilities.ElementAcceptor() { @Override public boolean accept(Element e, TypeMirror type) { return id.equals(e.getSimpleName()); } }).iterator(); if (iter.hasNext()) { el = (Element) iter.next(); } if (iter.hasNext()) { return; } } else { return; } } if (elementToFind != null && elementToFind.getKind() == ElementKind.METHOD && el.getKind() == ElementKind.METHOD) { for (ExecutableElement executableElement : methods) { if (el.equals(executableElement) || workingCopy.getElements().overrides((ExecutableElement) el, executableElement, (TypeElement) elementToFind.getEnclosingElement())) { addUsage(path); } } } else if (el.equals(elementToFind)) { final ElementKind kind = elementToFind.getKind(); if(kind.isField() || kind == ElementKind.LOCAL_VARIABLE || kind == ElementKind.RESOURCE_VARIABLE || kind == ElementKind.PARAMETER) { JavaWhereUsedFilters.ReadWrite access; Element collectionElement = workingCopy.getElementUtilities().findElement("java.util.Collection"); //NOI18N Element mapElement = workingCopy.getElementUtilities().findElement("java.util.Map"); //NOI18N if(collectionElement != null && workingCopy.getTypes().isSubtype( workingCopy.getTypes().erasure(el.asType()), workingCopy.getTypes().erasure(collectionElement.asType()))) { access = analyzeCollectionAccess(path); } else if(mapElement != null && workingCopy.getTypes().isSubtype( workingCopy.getTypes().erasure(el.asType()), workingCopy.getTypes().erasure(mapElement.asType()))) { access = analyzeCollectionAccess(path); } else { access = analyzeVarAccess(path, elementToFind, tree); } addUsage(path, access); } else { addUsage(path); } } }
Example 19
Source File: SupportedAnnotationTypesCompletion.java From netbeans with Apache License 2.0 | 4 votes |
@Override public Iterable<? extends Completion> getCompletions(Element element, AnnotationMirror annotation, ExecutableElement member, String userText) { ProcessingEnvironment processingEnv = this.processingEnv.get(); if (processingEnv == null) return Collections.emptyList(); TypeElement annotationObj = processingEnv.getElementUtils().getTypeElement("java.lang.annotation.Annotation"); if (annotationObj == null) return Collections.emptyList(); Trees trees = Trees.instance(processingEnv); TreePath path = trees.getPath(element); if (path == null) return Collections.emptyList(); FileObject owner; try { owner = URLMapper.findFileObject(path.getCompilationUnit().getSourceFile().toUri().toURL()); } catch (MalformedURLException ex) { Exceptions.printStackTrace(ex); return Collections.emptyList(); } ClassIndex ci = ClasspathInfo.create(owner).getClassIndex(); if (ci == null) return Collections.emptyList(); List<Completion> result = new LinkedList<Completion>(); // for (ElementHandle<TypeElement> eh : ci.getElements(ElementHandle.create(annotationObj), EnumSet.of(SearchKind.IMPLEMENTORS), EnumSet.of(SearchScope.DEPENDENCIES, SearchScope.SOURCE))) { // result.add(new CompletionImpl(eh.getQualifiedName())); // } for (ElementHandle<TypeElement> eh : ci.getDeclaredTypes("", ClassIndex.NameKind.PREFIX, EnumSet.of(SearchScope.DEPENDENCIES, SearchScope.SOURCE))) { if (eh.getKind() != ElementKind.ANNOTATION_TYPE) continue; result.add(new CompletionImpl('\"' + eh.getQualifiedName() + '\"')); } return result; }
Example 20
Source File: InlineVariableTransformer.java From netbeans with Apache License 2.0 | 4 votes |
private void replaceUsageIfMatch(TreePath path, Tree tree, Element elementToFind) { if (workingCopy.getTreeUtilities().isSynthetic(path)) { return; } Trees trees = workingCopy.getTrees(); Element el = workingCopy.getTrees().getElement(path); if (el == null) { path = path.getParentPath(); if (path != null && path.getLeaf().getKind() == Tree.Kind.IMPORT) { ImportTree impTree = (ImportTree) path.getLeaf(); if (!impTree.isStatic()) { return; } Tree idTree = impTree.getQualifiedIdentifier(); if (idTree.getKind() != Tree.Kind.MEMBER_SELECT) { return; } final Name id = ((MemberSelectTree) idTree).getIdentifier(); if (id == null || id.contentEquals("*")) { // NOI18N // skip import static java.lang.Math.* return; } Tree classTree = ((MemberSelectTree) idTree).getExpression(); path = trees.getPath(workingCopy.getCompilationUnit(), classTree); el = trees.getElement(path); if (el == null) { return; } Iterator<? extends Element> iter = workingCopy.getElementUtilities().getMembers(el.asType(), new ElementUtilities.ElementAcceptor() { @Override public boolean accept(Element e, TypeMirror type) { return id.equals(e.getSimpleName()); } }).iterator(); if (iter.hasNext()) { el = iter.next(); } if (iter.hasNext()) { return; } } else { return; } } if (el.equals(elementToFind)) { GeneratorUtilities genUtils = GeneratorUtilities.get(workingCopy); TreePath resolvedPath = trees.getPath(elementToFind); VariableTree varTree = (VariableTree)resolvedPath.getLeaf(); varTree = genUtils.importComments(varTree, resolvedPath.getCompilationUnit()); ExpressionTree body = varTree.getInitializer(); boolean parenthesize = OperatorPrecedence.needsParentheses(path, elementToFind, varTree.getInitializer(), workingCopy); if (parenthesize) { body = make.Parenthesized((ExpressionTree) body); } genUtils.copyComments(varTree, body, true); rewrite(tree, body); } }