private static Set<String> getImports(VisitorState state) { Set<String> imports = new HashSet<>(); for (ImportTree importTree : state.getPath().getCompilationUnit().getImports()) { imports.add(importTree.getQualifiedIdentifier().toString()); } return imports; }
void delete() { new TreePathScanner<Void, Void>() { @Override public Void visitVariable(VariableTree variableTree, Void v) { if (getSymbol(variableTree).equals(symbol)) { possibleFixes.fixByDeleting(variableTree); } return super.visitVariable(variableTree, v); } }.scan(state.getPath().getCompilationUnit(), null); } }
/** Returns the end position of the node, or -1 if it is not available. */ public int getEndPosition(Tree node) { JCCompilationUnit compilationUnit = (JCCompilationUnit) getPath().getCompilationUnit(); if (compilationUnit.endPositions == null) { return -1; } return ((JCTree) node).getEndPosition(compilationUnit.endPositions); }
static DCDocComment getDocComment(VisitorState state, Tree tree) { return ((JCCompilationUnit) state.getPath().getCompilationUnit()) .docComments.getCommentTree((JCTree) tree); }
private static String getPackageFullName(VisitorState state) { JCCompilationUnit compilationUnit = (JCCompilationUnit) state.getPath().getCompilationUnit(); return compilationUnit.packge.fullname.toString(); }
private void setEndPosition(VisitorState state, JCTree tree, int endPosition) { JCCompilationUnit compilationUnit = (JCCompilationUnit) state.getPath().getCompilationUnit(); compilationUnit.endPositions.storeEnd(tree, endPosition); }
/** * Gets the current source file. * * @return the source file as a sequence of characters, or null if it is not available */ @Nullable public CharSequence getSourceCode() { try { return getPath().getCompilationUnit().getSourceFile().getCharContent(false); } catch (IOException e) { return null; } }
@Nullable private VariableTree findDeclaration(VisitorState state, Symbol parameter) { JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context); TreePath declPath = Trees.instance(javacEnv).getPath(parameter); if (declPath != null && declPath.getCompilationUnit() == state.getPath().getCompilationUnit() && (declPath.getLeaf() instanceof VariableTree)) { return (VariableTree) declPath.getLeaf(); } return null; } }
@Nullable private VariableTree findDeclaration(VisitorState state, Symbol field) { JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context); TreePath fieldDeclPath = Trees.instance(javacEnv).getPath(field); // Skip fields declared in other compilation units since we can't make a fix for them here. if (fieldDeclPath != null && fieldDeclPath.getCompilationUnit() == state.getPath().getCompilationUnit() && (fieldDeclPath.getLeaf() instanceof VariableTree)) { return (VariableTree) fieldDeclPath.getLeaf(); } return null; }
static Set<VarSymbol> scan(VisitorState state, Set<Symbol> badAnswers) { MockInitializationScanner scanner = new MockInitializationScanner(badAnswers); state.getPath().getCompilationUnit().accept(scanner, null); return scanner.mockVariables; }
/** Return true if the given symbol is defined in the current package. */ public static boolean inSamePackage(Symbol targetSymbol, VisitorState state) { JCCompilationUnit compilationUnit = (JCCompilationUnit) state.getPath().getCompilationUnit(); PackageSymbol usePackage = compilationUnit.packge; PackageSymbol targetPackage = targetSymbol.packge(); return targetPackage != null && usePackage != null && targetPackage.getQualifiedName().equals(usePackage.getQualifiedName()); }
private static VisitorState getCheckState(VisitorState state) { // Gets the VisitorState to start from when checking how to qualify the name. This won't work // correctly in all cases 1) it assumes there is only 1 top level type; 2) it doesn't look for // all of the locations where the symbol-to-be-replaced is used in the compilation unit. // Really, we should gather all of the usages first, and check them all. // It is assumed that this will work sufficiently until proven otherwise. CompilationUnitTree compilationUnit = state.getPath().getCompilationUnit(); if (compilationUnit.getTypeDecls().isEmpty()) { return state; } return state.withPath(TreePath.getPath(compilationUnit, compilationUnit.getTypeDecls().get(0))); }
@Override public void apply(TreePath path, Context context, DescriptionListener listener) { RefasterScanner.create(this, listener) .scan( path.getLeaf(), prepareContext(context, (JCCompilationUnit) path.getCompilationUnit())); }
public static GuardedBySymbolResolver from(Tree tree, VisitorState visitorState) { return GuardedBySymbolResolver.from( ASTHelpers.getSymbol(tree).owner.enclClass(), visitorState.getPath().getCompilationUnit(), visitorState.context, tree); }
static int getStartPosition(DocTree docTree, VisitorState state) { DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions(); CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit(); return (int) positions.getStartPosition(compilationUnitTree, getDocCommentTree(state), docTree); }
static int getEndPosition(DocTree docTree, VisitorState state) { DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions(); CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit(); return (int) positions.getEndPosition(compilationUnitTree, getDocCommentTree(state), docTree); }
boolean shouldUseGuava(VisitorState state) { for (ImportTree importTree : state.getPath().getCompilationUnit().getImports()) { Symbol sym = ASTHelpers.getSymbol(importTree.getQualifiedIdentifier()); if (sym == null) { continue; } if (sym.getQualifiedName().contentEquals("com.google.common.io.Files")) { return true; } } return false; }
static SuggestedFix replace(DocTree docTree, String replacement, VisitorState state) { DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions(); CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit(); int startPos = getStartPosition(docTree, state); int endPos = (int) positions.getEndPosition(compilationUnitTree, getDocCommentTree(state), docTree); return SuggestedFix.replace(startPos, endPos, replacement); }
@Override public Description matchClass(ClassTree tree, VisitorState state) { if (isInShadowClass(state.getPath(), state)) { return NO_MATCH; } final ShadowInliner shadowInliner = new ShadowInliner( (JCCompilationUnit) state.getPath().getCompilationUnit()); shadowInliner.scan(tree, state); Fix fix = shadowInliner.possibleFixes.getFix(); return fix.isEmpty() ? NO_MATCH : describeMatch(tree, fix); }
@Override public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) { Symbol baseSym = ASTHelpers.getSymbol(tree.getExpression()); if (baseSym == null || baseSym.getKind() != ElementKind.TYPE_PARAMETER) { return Description.NO_MATCH; } TreeMaker make = TreeMaker.instance(state.context) .forToplevel((JCCompilationUnit) state.getPath().getCompilationUnit()); JCExpression qual = make.QualIdent(ASTHelpers.getSymbol(tree)); return describeMatch(tree, SuggestedFix.replace(tree, qual.toString())); } }