@Override public Void visitReference(ReferenceTree referenceTree, Void sink) { // do this first, it attributes the referenceTree as a side-effect trees.getElement(getCurrentPath()); com.sun.source.util.TreeScanner<Void, Void> nonRecursiveScanner = new com.sun.source.util.TreeScanner<Void, Void>() { @Override public Void visitIdentifier(IdentifierTree tree, Void sink) { Symbol sym = ASTHelpers.getSymbol(tree); if (sym != null) { System.out.println("sym = " + sym); } return null; } }; DCReference reference = (DCReference) referenceTree; nonRecursiveScanner.scan(reference.qualifierExpression, sink); nonRecursiveScanner.scan(reference.paramTypes, sink); return null; } }
@Override public Void visitReference(ReferenceTree referenceTree, SymbolSink sink) { // do this first, it attributes the referenceTree as a side-effect trees.getElement(getCurrentPath()); TreeScanner<Void, SymbolSink> nonRecursiveScanner = new TreeScanner<Void, SymbolSink>() { @Override public Void visitIdentifier(IdentifierTree tree, SymbolSink sink) { Symbol sym = ASTHelpers.getSymbol(tree); if (sym != null) { sink.accept(sym); } return null; } }; DCReference reference = (DCReference) referenceTree; nonRecursiveScanner.scan(reference.qualifierExpression, sink); nonRecursiveScanner.scan(reference.paramTypes, sink); return null; } }
/** * Fully qualifies a javadoc reference, e.g. for replacing {@code {@link List}} with {@code {@link * java.util.List}} * * @param fix the fix builder to add to * @param docPath the path to a {@link DCTree.DCReference} element */ public static void qualifyDocReference( SuggestedFix.Builder fix, DocTreePath docPath, VisitorState state) { DocTree leaf = docPath.getLeaf(); checkArgument( leaf.getKind() == DocTree.Kind.REFERENCE, "expected a path to a reference, got %s instead", leaf.getKind()); DCTree.DCReference reference = (DCTree.DCReference) leaf; Symbol sym = (Symbol) JavacTrees.instance(state.context).getElement(docPath); if (sym == null) { return; } String refString = reference.toString(); String qualifiedName; int idx = refString.indexOf('#'); if (idx >= 0) { qualifiedName = sym.owner.getQualifiedName() + refString.substring(idx, refString.length()); } else { qualifiedName = sym.getQualifiedName().toString(); } replaceDocTree(fix, docPath, qualifiedName); }
@Override public Void visitThrows(ThrowsTree throwsTree, Void unused) { ReferenceTree exName = throwsTree.getExceptionName(); Type type = (Type) JavacTrees.instance(state.context) .getElement(new DocTreePath(getCurrentPath(), exName)) .asType(); if (type != null && isCheckedException(type)) { if (methodTree.getThrows().stream().noneMatch(t -> isSubtype(type, getType(t), state))) { state.reportMatch( buildDescription(diagnosticPosition(getCurrentPath(), state)) .addFix(Utils.replace(throwsTree, "", state)) .build()); } } return super.visitThrows(throwsTree, null); }
@Override public Void visitReference(ReferenceTree referenceTree, SymbolSink sink) { // do this first, it attributes the referenceTree as a side-effect trees.getElement(getCurrentPath()); TreeScanner<Void, SymbolSink> nonRecursiveScanner = new TreeScanner<Void, SymbolSink>() { @Override public Void visitIdentifier(IdentifierTree tree, SymbolSink sink) { Symbol sym = ASTHelpers.getSymbol(tree); if (sym != null) { sink.accept(sym); } return null; } }; DCReference reference = (DCReference) referenceTree; nonRecursiveScanner.scan(reference.qualifierExpression, sink); nonRecursiveScanner.scan(reference.paramTypes, sink); return null; } }
/** * Fully qualifies a javadoc reference, e.g. for replacing {@code {@link List}} with {@code {@link * java.util.List}} * * @param fix the fix builder to add to * @param docPath the path to a {@link DCTree.DCReference} element */ public static void qualifyDocReference( SuggestedFix.Builder fix, DocTreePath docPath, VisitorState state) { DocTree leaf = docPath.getLeaf(); checkArgument( leaf.getKind() == DocTree.Kind.REFERENCE, "expected a path to a reference, got %s instead", leaf.getKind()); DCTree.DCReference reference = (DCTree.DCReference) leaf; Symbol sym = (Symbol) JavacTrees.instance(state.context).getElement(docPath); if (sym == null) { return; } String refString = reference.toString(); String qualifiedName; int idx = refString.indexOf('#'); if (idx >= 0) { qualifiedName = sym.owner.getQualifiedName() + refString.substring(idx, refString.length()); } else { qualifiedName = sym.getQualifiedName().toString(); } replaceDocTree(fix, docPath, qualifiedName); }
@Override public Void visitThrows(ThrowsTree throwsTree, Void unused) { ReferenceTree exName = throwsTree.getExceptionName(); Type type = (Type) JavacTrees.instance(state.context) .getElement(new DocTreePath(getCurrentPath(), exName)) .asType(); if (type != null && isCheckedException(type)) { if (methodTree.getThrows().stream().noneMatch(t -> isSubtype(type, getType(t), state))) { state.reportMatch( buildDescription(diagnosticPosition(getCurrentPath(), state)) .addFix(Utils.replace(throwsTree, "", state)) .build()); } } return super.visitThrows(throwsTree, null); }
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) { Symbol javadocSymbol = getElement(path); if (javadocSymbol == null) return null; ElementKind kind = javadocSymbol.getKind(); List<? extends Symbol> params = List.nil(); if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) { MethodSymbol ee = (MethodSymbol) javadocSymbol; params = ptag.isTypeParameter() ? ee.getTypeParameters() : ee.getParameters(); } else if (kind.isClass() || kind.isInterface()) { ClassSymbol te = (ClassSymbol) javadocSymbol; params = te.getTypeParameters(); } for (Symbol param : params) { if (param.getSimpleName() == ptag.getName().getName()) { return param; } } return null; }
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) { Symbol javadocSymbol = getElement(path); if (javadocSymbol == null) return null; ElementKind kind = javadocSymbol.getKind(); List<? extends Symbol> params = List.nil(); if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) { MethodSymbol ee = (MethodSymbol) javadocSymbol; params = ptag.isTypeParameter() ? ee.getTypeParameters() : ee.getParameters(); } else if (kind.isClass() || kind.isInterface()) { ClassSymbol te = (ClassSymbol) javadocSymbol; params = te.getTypeParameters(); } for (Symbol param : params) { if (param.getSimpleName() == ptag.getName().getName()) { return param; } } return null; }