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))); }
static VisitorState makeVisitorState(Tree target, Unifier unifier) { Context context = unifier.getContext(); TreePath path = TreePath.getPath(context.get(JCCompilationUnit.class), target); return new VisitorState(context).withPath(path); } }
private static Tree findParent(Tree node, VisitorState state) { return TreePath.getPath(state.getPath().getCompilationUnit(), node).getParentPath().getLeaf(); }
private void maybeReplaceFieldRef(ExpressionTree subject) { Symbol symbol = getSymbol(subject); if (symbol != null && symbol.getKind() == ElementKind.FIELD) { TreePath subjectPath = TreePath.getPath(compilationUnit, subject); if (symbol.equals(fieldSymbol) && isPartOfMethodInvocation(subjectPath)) { String fieldRef = subject.toString().startsWith("this.") ? "this." + newFieldName : newFieldName; JCTree replaceNode = (JCTree) subject; Tree container = subjectPath.getParentPath().getLeaf(); if (container instanceof JCFieldAccess) { JCFieldAccess fieldAccess = (JCFieldAccess) container; JCMethodInvocation newShadowOfCall = createSyntheticShadowAccess(shadowOfCall, newFieldName, symbol, state); replaceFieldSelected(fieldAccess, newShadowOfCall, state); replaceNode = newShadowOfCall; } String replaceWith = shouldCallDirectlyOnFramework(subjectPath) ? fieldRef : shadowOfCall.getMethodSelect() + "(" + fieldRef + ")"; possibleFixes.put( replaceNode, possibleFixes.new ReplacementFix(subject, replaceWith)); } } } }.scan(compilationUnit, null);
@Override public Void visitMethodInvocation(MethodInvocationTree tree, VisitorState state) { VisitorState nowState = state.withPath(TreePath.getPath(state.getPath(), tree)); if (!inShadowClass) { for (MethodInvocationMatcher matcher : matchers) { if (matcher.matcher().matches(tree, state)) { matcher.replace(tree, nowState, fixBuilder, possibleFixes); return null; } } } return super.visitMethodInvocation(tree, nowState); } }.scan(tree, state);
TreePath path = TreePath.getPath(state.getPath(), formatStringTree); while (path != null && ASTHelpers.getSymbol(path.getLeaf()) != owner) { path = path.getParentPath();
private static MethodCall getSurroundingMethodCall(Tree node, VisitorState state) { TreePath nodePath = TreePath.getPath(state.getPath(), node); TreePath parentPath = nodePath.getParentPath(); if (parentPath.getLeaf().getKind() == Kind.MEMBER_SELECT) { Tree grandparentNode = parentPath.getParentPath().getLeaf(); if (grandparentNode.getKind() == Kind.METHOD_INVOCATION) { return new MethodCall((JCMethodInvocation) grandparentNode); } } return null; }
VisitorState state) { CompilationUnitTree compilationUnit = state.getPath().getCompilationUnit(); TreePath path = TreePath.getPath(compilationUnit, compilationUnit); IdentifierTree firstFound = new TreePathScanner<IdentifierTree, Void>() {
@Override public Void visitIdentifier(IdentifierTree identifierTreeX, Void aVoid) { JCIdent identifierTree = (JCIdent) identifierTreeX; Symbol symbol = getSymbol(identifierTree); if (variableDecl.sym.equals(symbol) && !isLeftSideOfAssignment(identifierTree)) { TreePath idPath = TreePath.getPath(compilationUnit, identifierTree); Tree parent = idPath.getParentPath().getLeaf(); boolean callDirectlyOnFramework = shouldCallDirectlyOnFramework(idPath); JCTree replaceNode; if (parent instanceof JCFieldAccess && !callDirectlyOnFramework) { JCFieldAccess fieldAccess = (JCFieldAccess) parent; JCMethodInvocation newShadowOfCall = createSyntheticShadowAccess(shadowOfCall, newVarName, symbol, state); replaceFieldSelected(fieldAccess, newShadowOfCall, state); replaceNode = newShadowOfCall; } else { identifierTree.name = state.getName(newVarName); identifierTree.sym.name = state.getName(newVarName); replaceNode = identifierTree; } String replaceWith = callDirectlyOnFramework ? newVarName : shadowOfCall.getMethodSelect() + "(" + newVarName + ")"; possibleFixes.put( replaceNode, possibleFixes.new ReplacementFix(identifierTree, replaceWith)); } return super.visitIdentifier(identifierTree, aVoid); }
@Override public Void visitMethodInvocation(MethodInvocationTree tree, VisitorState state) { VisitorState nowState = state.withPath(TreePath.getPath(state.getPath(), tree)); if (!inShadowClass && shadowStaticMatcher.matches(tree, nowState)) { // Replace ShadowXxx.method() with Xxx.method() where possible... JCFieldAccess methodSelect = (JCFieldAccess) tree.getMethodSelect(); ClassSymbol owner = (ClassSymbol) methodSelect.sym.owner; ClassType shadowedClass = determineShadowedClassName(owner, nowState); String shadowedTypeName = SuggestedFixes.prettyType(state, possibleFixes.fixBuilder, shadowedClass); possibleFixes.fixByReplacing(methodSelect.selected, shadowedTypeName); } if (!inShadowClass && shadowOfMatcher.matches(tree, nowState)) { matchedShadowOf(tree, nowState); } return super.visitMethodInvocation(tree, nowState); } }
TreePath initializerPath = TreePath.getPath(fieldDeclPath, initializer); UnderlyingAST ast = new UnderlyingAST.CFGStatement(initializerPath.getLeaf(), classTree); ControlFlowGraph cfg =
if (newFieldName.equals(shadowOfArg.toString())) { TreePath assignmentPath = TreePath.getPath(compilationUnit, assignment); Tree assignmentParent = assignmentPath.getParentPath().getLeaf(); if (assignmentParent instanceof ExpressionStatementTree) { TreePath.getPath(compilationUnit, assignment), ExpressionStatementTree.class); if (enclosingNode != null) { possibleFixes.fixByDeleting(enclosingNode);
TreePath initializerPath = TreePath.getPath(fieldDeclPath, initializer); ClassTree classTree = (ClassTree) fieldDeclPath.getParentPath().getLeaf(); JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(context);
public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) { final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v); if (treeTopLevel == null) return null; return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst); }
public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) { final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v); if (treeTopLevel == null) return null; return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst); }
public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) { final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v); if (treeTopLevel == null) return null; return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst); }
public void run(CompilationController co) throws Exception { co.toPhase(JavaSource.Phase.RESOLVED); CompilationUnitTree cut = co.getCompilationUnit(); List<? extends Tree> typeDecls = cut.getTypeDecls(); if (!typeDecls.isEmpty()){ treePathHandles.add(TreePathHandle.create(TreePath.getPath(cut, typeDecls.get(0)), co)); } } }, false);
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))); }
public void run(CompilationController co) throws Exception { co.toPhase(JavaSource.Phase.RESOLVED); CompilationUnitTree cut = co.getCompilationUnit(); if (cut.getTypeDecls().isEmpty()) { return; } result[0] = TreePathHandle.create(TreePath.getPath(cut, cut.getTypeDecls().get(0)), co); } }, true);
static VisitorState makeVisitorState(Tree target, Unifier unifier) { Context context = unifier.getContext(); TreePath path = TreePath.getPath(context.get(JCCompilationUnit.class), target); return new VisitorState(context).withPath(path); } }