/** Returns true if the tree references its enclosing class. */ public static boolean referencesOuter(Tree tree, Symbol owner, VisitorState state) { CanBeStaticAnalyzer scanner = new CanBeStaticAnalyzer(owner, state); ((JCTree) tree).accept(scanner); return !scanner.canPossiblyBeStatic || !scanner.outerReferences.isEmpty(); }
private static boolean shouldSkipImportTreeFix(DiagnosticPosition position, Fix f) { if (position.getTree() != null && position.getTree().getKind() != Kind.IMPORT) { return false; } return !f.getImportsToAdd().isEmpty() || !f.getImportsToRemove().isEmpty(); }
public String getRange(JCCompilationUnit unit) { try { CharSequence sequence = unit.getSourceFile().getCharContent(true); return sequence .subSequence(location.getStartPosition(), location.getEndPosition(unit.endPositions)) .toString(); } catch (IOException e) { throw new RuntimeException(e); } } }
@Override public void printExpr(JCTree tree, int prec) throws IOException { EndPosTable endPositions = unit.endPositions; /* * Modifiers, and specifically flags like final, appear to just need weird special * handling. * * Note: we can't use {@code TreeInfo.getEndPos()} or {@code JCTree.getEndPosition()} * here, because they will return the end position of an enclosing AST node for trees * whose real end positions aren't stored. */ int endPos = endPositions.getEndPos(tree); boolean hasRealEndPosition = endPos != Position.NOPOS; if (tree.getKind() != Kind.MODIFIERS && hasRealEndPosition) { writer.append(unitContents.substring(tree.getStartPosition(), endPos)); } else { super.printExpr(tree, prec); } }
/** * Renames the given {@link VariableTree} and its usages in the current compilation unit to {@code * replacement}. */ public static SuggestedFix renameVariable( VariableTree tree, final String replacement, VisitorState state) { String name = tree.getName().toString(); int typeEndPos = state.getEndPosition(tree.getType()); // handle implicit lambda parameter types int searchOffset = typeEndPos == -1 ? 0 : (typeEndPos - ((JCTree) tree).getStartPosition()); int pos = ((JCTree) tree).getStartPosition() + state.getSourceForNode(tree).indexOf(name, searchOffset); final SuggestedFix.Builder fix = SuggestedFix.builder().replace(pos, pos + name.length(), replacement); final Symbol.VarSymbol sym = getSymbol(tree); ((JCTree) state.getPath().getCompilationUnit()) .accept( new TreeScanner() { @Override public void visitIdent(JCTree.JCIdent tree) { if (sym.equals(getSymbol(tree))) { fix.replace(tree, replacement); } } }); return fix.build(); }
/** * Returns a start position of given {@code tree}. * * <p>The only purpose of this method is to avoid doing a hacky casting to {@link JCTree}. */ private static int getStartPosition(Tree tree) { return ((JCTree) tree).getStartPosition(); } }
return TreeInfo.assignopPrec + ((assignOp.lhs == leaf) ? 1 : 0); } else if (parent instanceof JCUnary) { return TreeInfo.opPrec(parent.getTag()); } else if (parent instanceof JCBinary) { JCBinary binary = (JCBinary) parent; return TreeInfo.opPrec(parent.getTag()) + ((binary.rhs == leaf) ? 1 : 0); } else if (parent instanceof JCTypeCast) { JCTypeCast typeCast = (JCTypeCast) parent;
/** If tree is either a simple name or of the form this.name or * C.this.name, and tree represents a trackable variable, * record an initialization of the variable. */ void letInit(JCTree tree) { tree = TreeInfo.skipParens(tree); if (tree.getTag() == JCTree.IDENT || tree.getTag() == JCTree.SELECT) { Symbol sym = TreeInfo.symbol(tree); letInit(tree.pos(), (VarSymbol)sym); } }
private int getStartPos(JCTree tree) { return tree.pos().getStartPosition(); }
@Override public int getEndPosition(EndPosTable endPositions) { return position.getEndPosition(endPositions) + endPositionAdjustment; } }
int pos = ((JCTree) typeParameter).getStartPosition(); SuggestedFix.Builder fixBuilder = SuggestedFix.builder().replace(pos, pos + name.length(), typeVarReplacement); .accept( new TreeScanner() { @Override
@Override public int getStartPosition() { return position.getStartPosition() + startPositionAdjustment; }
@Override public String getRange(JCCompilationUnit unit) { try { CharSequence sequence = unit.getSourceFile().getCharContent(true); JCTree firstStatement = statements.get(0); JCTree lastStatement = Iterables.getLast(statements); return sequence .subSequence( firstStatement.getStartPosition(), lastStatement.getEndPosition(unit.endPositions)) .toString(); } catch (IOException e) { throw new RuntimeException(e); } } }
.map( ctx -> ctx.tree.getTag() == Tag.CLASSDEF ? ((ClassSymbol) ASTHelpers.getSymbol(ctx.tree)).members().getSymbols() : ctx.info.getLocalElements())
/** Find the position for reporting an error about a symbol, where * that symbol is defined somewhere in the given tree. */ public static DiagnosticPosition diagnosticPositionFor(final Symbol sym, final JCTree tree) { JCTree decl = declarationFor(sym, tree); return ((decl != null) ? decl : tree).pos(); }
/** 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); }