use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class FilesLinesLeak method inTWR.
private boolean inTWR(VisitorState state) {
TreePath path = state.getPath().getParentPath();
while (path.getLeaf().getKind() == Tree.Kind.CONDITIONAL_EXPRESSION) {
path = path.getParentPath();
}
Symbol sym = ASTHelpers.getSymbol(path.getLeaf());
return sym != null && sym.getKind() == ElementKind.RESOURCE_VARIABLE;
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class CompileTimeConstantChecker method handleMatch.
/**
* If the non-constant variable is annotated with @CompileTimeConstant, it must have been
* non-final. Suggest making it final in the error message.
*/
private Description handleMatch(ExpressionTree actualParam, VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(actualParam);
if (!(sym instanceof VarSymbol)) {
return describeMatch(actualParam);
}
VarSymbol var = (VarSymbol) sym;
if (!hasCompileTimeConstantAnnotation(state, var)) {
return describeMatch(actualParam);
}
return buildDescription(actualParam).setMessage(this.message() + String.format(DID_YOU_MEAN_FINAL_FMT_MESSAGE, var.getSimpleName())).build();
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class DefaultCharset method variableTypeFix.
private void variableTypeFix(SuggestedFix.Builder fix, VisitorState state, Class<?> original, Class<?> replacement) {
Tree parent = state.getPath().getParentPath().getLeaf();
Symbol sym;
switch(parent.getKind()) {
case VARIABLE:
sym = ASTHelpers.getSymbol((VariableTree) parent);
break;
case ASSIGNMENT:
sym = ASTHelpers.getSymbol(((AssignmentTree) parent).getVariable());
break;
default:
return;
}
if (!ASTHelpers.isSameType(sym.type, state.getTypeFromString(original.getCanonicalName()), state)) {
return;
}
state.getPath().getCompilationUnit().accept(new TreeScanner<Void, Void>() {
@Override
public Void visitVariable(VariableTree node, Void aVoid) {
if (sym.equals(ASTHelpers.getSymbol(node))) {
fix.replace(node.getType(), replacement.getSimpleName()).addImport(replacement.getCanonicalName());
}
return null;
}
}, null);
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class PreconditionsCheckNotNullPrimitive method hasMethodParameter.
/**
* Determines whether the expression contains a reference to one of the
* enclosing method's parameters.
*
* TODO(eaftan): Extract this to ASTHelpers.
*
* @param path the path to the current tree node
* @param tree the node to compare against the parameters
* @return whether the argument is a parameter to the enclosing method
*/
private static boolean hasMethodParameter(TreePath path, ExpressionTree tree) {
Set<Symbol> symbols = new HashSet<>();
for (IdentifierTree ident : getVariableUses(tree)) {
Symbol sym = ASTHelpers.getSymbol(ident);
if (sym.isLocal()) {
symbols.add(sym);
}
}
// Find enclosing method declaration.
while (path != null && !(path.getLeaf() instanceof MethodTree)) {
path = path.getParentPath();
}
if (path == null) {
throw new IllegalStateException("Should have an enclosing method declaration");
}
MethodTree methodDecl = (MethodTree) path.getLeaf();
for (VariableTree param : methodDecl.getParameters()) {
if (symbols.contains(ASTHelpers.getSymbol(param))) {
return true;
}
}
return false;
}
use of com.sun.tools.javac.code.Symbol in project error-prone by google.
the class RemoveUnusedImports method matchCompilationUnit.
@Override
public Description matchCompilationUnit(CompilationUnitTree compilationUnitTree, VisitorState state) {
final ImmutableSetMultimap<ImportTree, Symbol> importedSymbols = getImportedSymbols(compilationUnitTree, state);
if (importedSymbols.isEmpty()) {
return NO_MATCH;
}
final Set<ImportTree> unusedImports = new HashSet<>(importedSymbols.keySet());
new TreeSymbolScanner(JavacTrees.instance(state.context), state.getTypes()).scan(compilationUnitTree, new SymbolSink() {
@Override
public boolean keepScanning() {
return !unusedImports.isEmpty();
}
@Override
public void accept(Symbol symbol) {
unusedImports.removeAll(importedSymbols.inverse().get(symbol));
}
});
if (unusedImports.isEmpty()) {
return NO_MATCH;
}
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
for (ImportTree unusedImport : unusedImports) {
fixBuilder.delete(unusedImport);
}
return describeMatch(unusedImports.iterator().next(), fixBuilder.build());
}
Aggregations