/** * Returns the preceding immediate comment */ static String getPrecedingImmediateCommentText(CompilationInfo cinfo, Tree tree) { List<Comment> comments = cinfo.getTreeUtilities().getComments(tree, true); return comments.size() > 0 ? comments.get(comments.size()-1).getText() : null; }
/** * Only for members (i.e. generated constructor): */ public static List<? extends Tree> filterHidden(CompilationInfo info, TreePath basePath, Iterable<? extends Tree> members) { List<Tree> result = new LinkedList<Tree>(); for (Tree t : members) { if (!info.getTreeUtilities().isSynthetic(new TreePath(basePath, t))) { result.add(t); } } return result; }
private Iterable<? extends TreePath> unattributedPrepareThis(TreePath tp) { ExpressionTree thisTree = info.getTreeUtilities().parseExpression("this", new SourcePositions[1]); return Collections.singleton(new TreePath(tp, thisTree)); }
private TreePath findUnresolvedElement(CompilationInfo info, int offset) throws IOException { int[] span = findUnresolvedElementSpan(info, offset); if (span != null) { return info.getTreeUtilities().pathFor(span[0] + 1); } else { return null; } }
private static Iterable<? extends TreePath> referenceEmbeddedSourceNodes(CompilationInfo info, DocTreePath ref) { List<TreePath> result = new ArrayList<TreePath>(); if (info.getTreeUtilities().getReferenceClass(ref) != null) { result.add(new TreePath(ref.getTreePath(), info.getTreeUtilities().getReferenceClass(ref))); } List<? extends Tree> params = info.getTreeUtilities().getReferenceParameters(ref); if (params != null) { for (Tree et : params) { result.add(new TreePath(ref.getTreePath(), et)); } } return result; }
private UsageStatus getUseStatus(TreePath path) { if (cinfo.getTreeUtilities().isSynthetic(path)) { return UsageStatus.NOT_USED; } Element el = cinfo.getTrees().getElement(path); if (el != null && elementAndNames.containsKey(el)) { if(getEnclosingMethodName(path).equals("_init")) { return UsageStatus.INIT_USE_ONLY; }else { return UsageStatus.USED; } } return UsageStatus.NOT_USED; }
private MethodTree getPublicConstructor(CompilationInfo cinfo, ClassTree ctree) { for(Tree tree : ctree.getMembers()) { if(Tree.Kind.METHOD == tree.getKind()) { MethodTree mtree = (MethodTree)tree; if(mtree.getName().toString().equals(Method.CTOR) && !cinfo.getTreeUtilities().isSynthetic(TreeUtils.getTreePath(cinfo, ctree))) { return mtree; } } } return null; }
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; }
@Override public void run() { if (cancel.get()) { return ; } if (ts != null && !ts.isValid()) { cancel.set(true); return ; } if (ts == null) { return ; } Token t = ts.token(); if (t.id() == JavaTokenId.IDENTIFIER && tree.getName().toString().equals(info.getTreeUtilities().decodeIdentifier(t.text()).toString())) { // System.err.println("visit ident 1"); tree2Token.put(tree, ts.token()); } else { // System.err.println("visit ident 2"); } } });
private DocTreePath resolvePathForPos(CompilationInfo javac, TreePath treePath, DocCommentTree doc, int pos) { DocTreePath tp = javac.getTreeUtilities().pathFor(treePath, doc, pos); while (tp != null) { KindPath kindPath1 = new KindPath(tp); kindPath.getList().remove(Tree.Kind.ERRONEOUS); if (kindPath1.equals(kindPath)) { return tp; } tp = tp.getParentPath(); } return null; }
private TreePath resolvePathForPos(final CompilationInfo compilationInfo, int pos) { TreePath tp = compilationInfo.getTreeUtilities().pathFor(pos); while (tp != null) { KindPath kindPath1 = new KindPath(tp); kindPath.getList().remove(Tree.Kind.ERRONEOUS); if (kindPath1.equals(kindPath)) { return tp; } tp = tp.getParentPath(); } return null; }
@Override public Void visitMethod(MethodTree node, Void p) { if (info.getTreeUtilities().isSynthetic(/*should not be used:*/getCurrentPath().getCompilationUnit(), node)) return null; return super.visitMethod(node, p); }
private boolean canSkip(TreePath path) { if (cinfo.getTreeUtilities().isSynthetic(path)) { return false; } Element el = cinfo.getTrees().getElement(path); if (el != null && elementAndNames.containsKey(el)) { TreePath declPath = cinfo.getTrees().getPath(el); if(declPath.getLeaf().equals(path.getLeaf())) { return true; } } return false; }
@Override public void run() { if (cancel.get()) { return ; } if (ts != null && !ts.isValid()) { cancel.set(true); return ; } if (ts == null) { return ; } boolean next = true; while (ts.token().id() != JavaTokenId.IDENTIFIER && (next = ts.moveNext())) ; if (next) { if (name.equals(info.getTreeUtilities().decodeIdentifier(ts.token().text()).toString())) { tree2Token.put(tp.getLeaf(), ts.token()); } else { // System.err.println("looking for: " + name + ", not found"); } } } });
static void ensureCommentsMapped(CompilationInfo info, @NullAllowed Tree tree, CommentSetImpl set) { if (!set.areCommentsMapped() && tree != null) { boolean assertsEnabled = false; boolean automap = true; assert assertsEnabled = true; TreePath tp = info.getCompilationUnit() == tree ? new TreePath(info.getCompilationUnit()) : TreePath.getPath(info.getCompilationUnit(), tree); if (tp == null) { if (assertsEnabled && !info.getTreeUtilities().isSynthetic(info.getCompilationUnit(), tree)) { // HACK: if info is a working copy, the tree might be introduced by rewriting; // in that case, no log should be printed if (!(info instanceof WorkingCopy) || !((WorkingCopy)info).validateIsReplacement(tree)) { Logger.getLogger(TreeUtilities.class.getName()).log(assertsEnabled ? Level.WARNING : Level.FINE, "Comment automap requested for Tree not from the root compilation info. Please, make sure to call GeneratorUtilities.importComments before Treeutilities.getComments. Tree: {0}", tree); Logger.getLogger(TreeUtilities.class.getName()).log(assertsEnabled ? Level.INFO : Level.FINE, "Caller", new Exception()); } } automap = false; } if (automap) { GeneratorUtilities.importComments(info, tree, info.getCompilationUnit()); } } }
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; }
private static Token<JavaTokenId> findIdentifierSpanImpl(CompilationInfo info, MemberSelectTree 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; String member = tree.getIdentifier().toString(); TokenHierarchy<?> th = info.getTokenHierarchy(); TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language()); if (ts.move(endPosition) == Integer.MAX_VALUE) { return null; } if (ts.moveNext()) { while (ts.offset() >= start) { Token<JavaTokenId> t = ts.token(); if (t.id() == JavaTokenId.IDENTIFIER && member.equals(info.getTreeUtilities().decodeIdentifier(t.text()).toString())) { return t; } if (!ts.movePrevious()) break; } } return null; }
private static Token<JavaTokenId> findIdentifierSpanImpl(CompilationInfo info, MemberReferenceTree 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; String member = tree.getName().toString(); TokenHierarchy<?> th = info.getTokenHierarchy(); TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language()); if (ts.move(endPosition) == Integer.MAX_VALUE) { return null; } if (ts.moveNext()) { while (ts.offset() >= start) { Token<JavaTokenId> t = ts.token(); if (t.id() == JavaTokenId.IDENTIFIER && member.equals(info.getTreeUtilities().decodeIdentifier(t.text()).toString())) { return t; } if (!ts.movePrevious()) break; } } return null; }
@Override public Void visitIdentifier(IdentifierTree tree, EnumSet<UseTypes> d) { if (info.getTreeUtilities().isSynthetic(getCurrentPath())) return null; // if ("l".equals(tree.toString())) { // Thread.dumpStack(); // } // handlePossibleIdentifier(tree); // //also possible type: (like in Collections.EMPTY_LIST): // resolveType(tree); // Thread.dumpStack(); tl.moveToOffset(sourcePositions.getStartPosition(info.getCompilationUnit(), tree)); if (memberSelectBypass != (-1)) { tl.moveToOffset(memberSelectBypass); memberSelectBypass = -1; } tl.identifierHere(tree, tree2Token); if (d != null) { handlePossibleIdentifier(getCurrentPath(), d); } super.visitIdentifier(tree, null); return null; } //
TreePath tp = javac.getTreeUtilities().pathFor(offsetBehindJavadoc);