use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.
the class StrictFormatStringValidation method validate.
@Nullable
public static ValidationResult validate(ExpressionTree formatStringTree, List<? extends ExpressionTree> args, VisitorState state) {
String formatStringValue = ASTHelpers.constValue(formatStringTree, String.class);
// so don't bother with annotations and just check if the parameters match the format string.
if (formatStringValue != null) {
return FormatStringValidation.validate(ImmutableList.<ExpressionTree>builder().add(formatStringTree).addAll(args).build(), state);
}
// The format string is not a compile time constant. Check if it is an @FormatString method
// parameter or is in an @FormatMethod method.
Symbol formatStringSymbol = ASTHelpers.getSymbol(formatStringTree);
if (!(formatStringSymbol instanceof VarSymbol)) {
return ValidationResult.create(null, String.format("Format strings must be either a literal or a variable. Other expressions" + " are not valid.\n" + "Invalid format string: %s", formatStringTree));
}
if ((formatStringSymbol.flags() & (Flags.FINAL | Flags.EFFECTIVELY_FINAL)) == 0) {
return ValidationResult.create(null, "All variables passed as @FormatString must be final or effectively final");
}
if (formatStringSymbol.getKind() == ElementKind.PARAMETER) {
return validateFormatStringParamter(formatStringTree, formatStringSymbol, args, state);
} else {
// works with the format arguments.
return validateFormatStringVariable(formatStringTree, formatStringSymbol, args, state);
}
}
use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.
the class DoubleCheckedLocking method findDCL.
/**
* Matches an instance of DCL. The canonical pattern is:
*
* <pre>
* {@code
* if ($X == null) {
* synchronized (...) {
* if ($X == null) {
* ...
* }
* ...
* }
* }
* }
* </pre>
*
* Gaps before the synchronized or inner 'if' statement are ignored, and the operands in the
* null-checks are accepted in either order.
*/
@Nullable
static DCLInfo findDCL(IfTree outerIf) {
// TODO(cushon): Optional.ifPresent...
ExpressionTree outerIfTest = getNullCheckedExpression(outerIf.getCondition());
if (outerIfTest == null) {
return null;
}
SynchronizedTree synchTree = getChild(outerIf.getThenStatement(), SynchronizedTree.class);
if (synchTree == null) {
return null;
}
IfTree innerIf = getChild(synchTree.getBlock(), IfTree.class);
if (innerIf == null) {
return null;
}
ExpressionTree innerIfTest = getNullCheckedExpression(innerIf.getCondition());
if (innerIfTest == null) {
return null;
}
Symbol outerSym = ASTHelpers.getSymbol(outerIfTest);
if (!Objects.equals(outerSym, ASTHelpers.getSymbol(innerIfTest))) {
return null;
}
if (!(outerSym instanceof VarSymbol)) {
return null;
}
VarSymbol var = (VarSymbol) outerSym;
return DCLInfo.create(outerIf, synchTree, innerIf, var);
}
use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.
the class MockitoCast method matchCompilationUnit.
@Override
public Description matchCompilationUnit(CompilationUnitTree tree, final VisitorState state) {
Symbol mockitoSym = state.getSymbolFromString(MOCKITO_CLASS);
if (mockitoSym == null) {
// fast path if mockito isn't being used
return Description.NO_MATCH;
}
// collect variable symbols for standard Answer constants that don't support generics
final Set<Symbol> badAnswers = new LinkedHashSet<>();
for (Symbol member : mockitoSym.members().getSymbols(LookupKind.NON_RECURSIVE)) {
if (member.getKind() != ElementKind.FIELD) {
continue;
}
if (BAD_ANSWER_STRATEGIES.contains(member.getSimpleName().toString())) {
badAnswers.add(member);
}
}
// collect mocks that are initialized in this compilation unit using a bad answer strategy
final Set<VarSymbol> mockVariables = MockInitializationScanner.scan(state, badAnswers);
// check for when(...) calls on mocks using a bad answer strategy
new WhenNeedsCastScanner(mockVariables, state).scan(state.getPath(), null);
// errors are reported in WhenNeedsCastScanner
return Description.NO_MATCH;
}
use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.
the class AbstractToString method checkToString.
/**
* Tests if the given expression is converted to a String by its parent (i.e. its parent
* is a string concat expression, {@code String.format}, or {@code println(Object)}).
*/
private Description checkToString(ExpressionTree tree, VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(tree);
if (!(sym instanceof VarSymbol || sym instanceof MethodSymbol)) {
return NO_MATCH;
}
Type type = ASTHelpers.getType(tree);
if (type instanceof MethodType) {
type = type.getReturnType();
}
Tree parent = state.getPath().getParentPath().getLeaf();
ToStringKind toStringKind = isToString(parent, tree, state);
if (toStringKind == ToStringKind.NONE) {
return NO_MATCH;
}
Optional<Fix> fix;
switch(toStringKind) {
case IMPLICIT:
fix = implicitToStringFix(tree, state);
break;
case EXPLICIT:
fix = toStringFix(parent, tree, state);
break;
default:
throw new AssertionError(toStringKind);
}
if (!typePredicate().apply(type, state)) {
return NO_MATCH;
}
return maybeFix(tree, fix);
}
use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.
the class SynchronizeOnNonFinalField method matchSynchronized.
@Override
public Description matchSynchronized(SynchronizedTree tree, VisitorState state) {
Symbol symbol = ASTHelpers.getSymbol(TreeInfo.skipParens((JCExpression) tree.getExpression()));
if (!(symbol instanceof VarSymbol)) {
return Description.NO_MATCH;
}
// TODO(cushon): check that the receiver doesn't contain mutable state.
// Currently 'this.locks[i].mu' is accepted if 'mu' is final but 'locks' is non-final.
VarSymbol varSymbol = (VarSymbol) symbol;
if (varSymbol.isLocal() || varSymbol.isStatic() || (varSymbol.flags() & Flags.FINAL) != 0) {
return Description.NO_MATCH;
}
return describeMatch(tree.getExpression());
}
Aggregations