use of com.sun.source.util.TreePath in project error-prone by google.
the class FieldMissingNullable method findDeclaration.
@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;
}
use of com.sun.source.util.TreePath in project error-prone by google.
the class ParameterNotNullable method matchDereference.
private Description matchDereference(ExpressionTree dereferencedExpression, VisitorState state) {
Symbol dereferenced = ASTHelpers.getSymbol(dereferencedExpression);
if (dereferenced == null || dereferenced.getKind() != ElementKind.PARAMETER || dereferenced.type.isPrimitive()) {
// not a parameter dereference
return Description.NO_MATCH;
}
if (!TrustingNullnessAnalysis.hasNullableAnnotation(dereferenced)) {
return Description.NO_MATCH;
}
Nullness nullness = TrustingNullnessAnalysis.instance(state.context).getNullness(new TreePath(state.getPath(), dereferencedExpression), state.context);
if (nullness != Nullness.NULLABLE) {
return Description.NO_MATCH;
}
for (AnnotationTree anno : findDeclaration(state, dereferenced).getModifiers().getAnnotations()) {
if (ASTHelpers.getSymbol(anno).type.toString().endsWith(".Nullable")) {
return buildDescription(dereferencedExpression).setMessage("Nullable parameter not checked for null").addFix(SuggestedFix.delete(anno)).build();
}
}
// Shouldn't get here
return Description.NO_MATCH;
}
use of com.sun.source.util.TreePath in project error-prone by google.
the class ParameterNotNullable method findDeclaration.
@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;
}
use of com.sun.source.util.TreePath 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.source.util.TreePath in project error-prone by google.
the class AbstractArgumentParameterChecker method findReplacements.
private Description findReplacements(List<? extends ExpressionTree> args, com.sun.tools.javac.util.List<VarSymbol> params, boolean isVarArgs, VisitorState state, Tree tree) {
if (args.isEmpty()) {
return Description.NO_MATCH;
}
ImmutableSet<PotentialReplacement> potentialReplacements = potentialReplacementsFunction.apply(state.withPath(new TreePath(state.getPath(), args.get(0))));
SuggestedFix.Builder fix = SuggestedFix.builder();
// Don't suggest for the varargs parameter.
// TODO(eaftan): Reconsider this, especially if the argument is of array type or is itself
// a varargs parameter.
int maxArg = isVarArgs ? params.size() - 1 : params.size();
for (int i = 0; i < maxArg; i++) {
ExpressionTree arg = args.get(i);
VarSymbol param = params.get(i);
if (!validKinds.contains(arg.getKind()) || !parameterPredicate.test(param)) {
continue;
}
String extractedArgumentName = extractArgumentName(arg);
if (extractedArgumentName == null) {
continue;
}
double currSimilarity = similarityMetric.applyAsDouble(extractedArgumentName, param.getSimpleName().toString());
if (1.0 - currSimilarity < beta) {
// No way for any replacement to be at least BETA better than the current argument
continue;
}
ReplacementWithSimilarity bestReplacement = potentialReplacements.stream().filter(replacement -> !replacement.sym().equals(ASTHelpers.getSymbol(arg))).filter(replacement -> isSubtypeHandleCompletionFailures(replacement.sym(), param, state)).map(replacement -> ReplacementWithSimilarity.create(replacement, similarityMetric.applyAsDouble(replacement.argumentName(), param.getSimpleName().toString()))).max(Comparator.comparingDouble(ReplacementWithSimilarity::similarity)).orElse(null);
if ((bestReplacement != null) && (bestReplacement.similarity() - currSimilarity >= beta)) {
fix.replace(arg, bestReplacement.replacement().replacementString());
}
}
if (fix.isEmpty()) {
return Description.NO_MATCH;
} else {
return describeMatch(tree, fix.build());
}
}
Aggregations