use of com.sun.source.tree.MethodTree in project bazel by bazelbuild.
the class Analysis method init.
/** Initialize the analysis with a new control flow graph. */
protected void init(ControlFlowGraph cfg) {
this.cfg = cfg;
thenStores = new IdentityHashMap<>();
elseStores = new IdentityHashMap<>();
inputs = new IdentityHashMap<>();
storesAtReturnStatements = new IdentityHashMap<>();
worklist = new Worklist(cfg);
nodeValues = new IdentityHashMap<>();
finalLocalValues = new HashMap<>();
worklist.add(cfg.getEntryBlock());
List<LocalVariableNode> parameters = null;
UnderlyingAST underlyingAST = cfg.getUnderlyingAST();
if (underlyingAST.getKind() == Kind.METHOD) {
MethodTree tree = ((CFGMethod) underlyingAST).getMethod();
parameters = new ArrayList<>();
for (VariableTree p : tree.getParameters()) {
LocalVariableNode var = new LocalVariableNode(p);
parameters.add(var);
// TODO: document that LocalVariableNode has no block that it
// belongs to
}
} else if (underlyingAST.getKind() == Kind.LAMBDA) {
LambdaExpressionTree lambda = ((CFGLambda) underlyingAST).getLambdaTree();
parameters = new ArrayList<>();
for (VariableTree p : lambda.getParameters()) {
LocalVariableNode var = new LocalVariableNode(p);
parameters.add(var);
// TODO: document that LocalVariableNode has no block that it
// belongs to
}
} else {
// nothing to do
}
S initialStore = transferFunction.initialStore(underlyingAST, parameters);
Block entry = cfg.getEntryBlock();
thenStores.put(entry, initialStore);
elseStores.put(entry, initialStore);
inputs.put(entry, new TransferInput<>(null, this, initialStore));
}
use of com.sun.source.tree.MethodTree in project error-prone by google.
the class FragmentInjection method matchClass.
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
// Only examine classes that extend PreferenceActivity.
Type preferenceActivityType = state.getTypeFromString("android.preference.PreferenceActivity");
if (!isSubtype(getType(tree), preferenceActivityType, state)) {
return NO_MATCH;
}
// Examine each method in the class. Complain if isValidFragment not implemented.
TypeSymbol preferenceActivityTypeSymbol = preferenceActivityType.tsym;
boolean methodNotImplemented = true;
try {
MethodSymbol isValidFragmentMethodSymbol = resolveExistingMethod(state, getSymbol(tree), state.getName("isValidFragment"), ImmutableList.<Type>of(state.getTypeFromString("java.lang.String")), ImmutableList.<Type>of());
methodNotImplemented = isValidFragmentMethodSymbol.owner.equals(preferenceActivityTypeSymbol);
} catch (FatalError e) {
// If isValidFragment method symbol is not found, then we must be compiling against an old SDK
// version (< 19) in which isValidFragment is not yet implemented, and neither this class nor
// any of its super classes have implemented it.
}
// isValidFragment, and this is not an abstract class, emit warning.
if (methodNotImplemented && not(hasModifier(Modifier.ABSTRACT)).matches(tree, state)) {
return buildDescription(tree).setMessage("Class extending PreferenceActivity does not implement isValidFragment.").build();
}
// Check the implementation of isValidFragment. Complain if it always returns true.
MethodTree isValidFragmentMethodTree = getMethod(OVERRIDES_IS_VALID_FRAGMENT, tree, state);
if (isValidFragmentMethodTree != null) {
if (isValidFragmentMethodTree.accept(ALWAYS_RETURNS_TRUE, null)) {
return buildDescription(isValidFragmentMethodTree).setMessage("isValidFragment unconditionally returns true.").build();
}
}
return NO_MATCH;
}
use of com.sun.source.tree.MethodTree in project error-prone by google.
the class Util method addPrivateConstructor.
// TODO(dpb): Account for indentation level.
private static SuggestedFix.Builder addPrivateConstructor(ClassTree classTree) {
SuggestedFix.Builder fix = SuggestedFix.builder();
String indent = " ";
for (Tree member : classTree.getMembers()) {
if (member.getKind().equals(METHOD) && !isGeneratedConstructor((MethodTree) member)) {
fix.prefixWith(member, indent + "private " + classTree.getSimpleName() + "() {} // no instances\n" + indent);
break;
}
if (!member.getKind().equals(METHOD)) {
indent = "";
}
}
return fix;
}
use of com.sun.source.tree.MethodTree in project error-prone by google.
the class JUnit3TestNotRun method matchMethod.
/**
* Matches if:
* 1) Method's name begins with misspelled variation of "test".
* 2) Method is public, returns void, and has no parameters.
* 3) Enclosing class is JUnit3 test (extends TestCase, has no RunWith annotation,
* and is not abstract).
*/
@Override
public Description matchMethod(MethodTree methodTree, VisitorState state) {
Matcher<MethodTree> methodMatcher = allOf(not(methodNameStartsWith("test")), Matchers.<MethodTree>hasModifier(Modifier.PUBLIC), methodReturns(VOID_TYPE), methodHasParameters(), enclosingClass(isJUnit3TestClass));
if (!methodMatcher.matches(methodTree, state)) {
return Description.NO_MATCH;
}
String name = methodTree.getName().toString();
String fixedName;
// regex.Matcher class name collides with errorprone.Matcher
java.util.regex.Matcher matcher = MISSPELLED_NAME.matcher(name);
if (matcher.lookingAt()) {
fixedName = matcher.replaceFirst("test");
} else if (wouldRunInJUnit4.matches(methodTree, state)) {
fixedName = "test" + name.substring(0, 1).toUpperCase() + name.substring(1);
} else {
return Description.NO_MATCH;
}
// We don't have start position for a method symbol, so we replace everything between result
// type and body.
JCMethodDecl decl = (JCMethodDecl) methodTree;
Fix fix = SuggestedFix.replace(decl.restype.getStartPosition() + 4, decl.body.getStartPosition(), " " + fixedName + "() ");
return describeMatch(methodTree, fix);
}
use of com.sun.source.tree.MethodTree in project error-prone by google.
the class InputStreamSlowMultibyteRead method matchClass.
@Override
public Description matchClass(ClassTree classTree, VisitorState state) {
if (!IS_INPUT_STREAM.matches(classTree, state)) {
return Description.NO_MATCH;
}
TypeSymbol thisClassSymbol = ASTHelpers.getSymbol(classTree);
if (thisClassSymbol.getKind() != ElementKind.CLASS) {
return Description.NO_MATCH;
}
// Find the method that overrides the single-byte read. It should also override the multibyte
// read.
MethodTree readByteMethod = classTree.getMembers().stream().filter(MethodTree.class::isInstance).map(MethodTree.class::cast).filter(m -> READ_INT_METHOD.matches(m, state)).findFirst().orElse(null);
if (readByteMethod == null) {
return Description.NO_MATCH;
}
Type byteArrayType = state.arrayTypeForType(state.getSymtab().byteType);
Type intType = state.getSymtab().intType;
MethodSymbol multiByteReadMethod = ASTHelpers.resolveExistingMethod(state, thisClassSymbol, state.getName("read"), ImmutableList.of(byteArrayType, intType, intType), ImmutableList.of());
return multiByteReadMethod.owner.equals(thisClassSymbol) ? Description.NO_MATCH : maybeMatchReadByte(readByteMethod, state);
}
Aggregations