use of com.sun.source.tree.VariableTree 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.VariableTree in project bazel by bazelbuild.
the class TreeBuilder method buildVariableDecl.
/**
* Builds an AST Tree to declare and initialize a variable, with no modifiers.
*
* @param type the type of the variable
* @param name the name of the variable
* @param owner the element containing the new symbol
* @param initializer the initializer expression
* @return a VariableDeclTree declaring the new variable
*/
public VariableTree buildVariableDecl(TypeMirror type, String name, Element owner, ExpressionTree initializer) {
DetachedVarSymbol sym = new DetachedVarSymbol(0, names.fromString(name), (Type) type, (Symbol) owner);
VariableTree tree = maker.VarDef(sym, (JCTree.JCExpression) initializer);
sym.setDeclaration(tree);
return tree;
}
use of com.sun.source.tree.VariableTree in project error-prone by google.
the class WrongParameterPackage method describe.
public Description describe(MethodTree tree, VisitorState state) {
SuggestedFix.Builder builder = null;
MethodSymbol method = ASTHelpers.getSymbol(tree);
if (supermethod == null) {
throw new IllegalStateException("Matching supermethod was not found");
}
for (int x = 0; x < method.params().size(); x++) {
Type methodParamType = method.params().get(x).type;
Type supermethodParamType = supermethod.params().get(x).type;
if (methodParamType.tsym.name.contentEquals(supermethodParamType.tsym.name) && !state.getTypes().isSameType(methodParamType, supermethodParamType)) {
VariableTree param = tree.getParameters().get(x);
// TODO(user): Name is most likely more qualified than necessary.
Name replacement = supermethodParamType.tsym.getQualifiedName();
if (builder == null) {
builder = SuggestedFix.builder();
}
builder.replace(param, replacement + " " + param.getName());
}
}
return (builder != null) ? describeMatch(tree, builder.build()) : describeMatch(tree);
}
use of com.sun.source.tree.VariableTree in project error-prone by google.
the class IsLoggableTagLength method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!IS_LOGGABLE_CALL.matches(tree, state)) {
return NO_MATCH;
}
ExpressionTree tagArg = tree.getArguments().get(0);
// Check for constant value.
String tagConstantValue = ASTHelpers.constValue(tagArg, String.class);
if (tagConstantValue != null) {
return isValidTag(tagConstantValue) ? NO_MATCH : describeMatch(tagArg);
}
// Check for class literal simple name (e.g. MyClass.class.getSimpleName().
ExpressionTree tagExpr = tagArg;
// If the tag argument is a final field, retrieve the initializer.
if (kindIs(IDENTIFIER).matches(tagArg, state)) {
VariableTree declaredField = findEnclosingIdentifier((IdentifierTree) tagArg, state);
if (declaredField == null || !hasModifier(FINAL).matches(declaredField, state)) {
return NO_MATCH;
}
tagExpr = declaredField.getInitializer();
}
if (GET_SIMPLE_NAME_CALL.matches(tagExpr, state) && RECEIVER_IS_CLASS_LITERAL.matches((MethodInvocationTree) tagExpr, state)) {
String tagName = getSymbol(getReceiver(getReceiver(tagExpr))).getSimpleName().toString();
return isValidTag(tagName) ? NO_MATCH : describeMatch(tagArg);
}
return NO_MATCH;
}
use of com.sun.source.tree.VariableTree in project error-prone by google.
the class FormatStringAnnotationChecker method matchMethod.
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
Type stringType = state.getSymtab().stringType;
boolean isFormatMethod = ASTHelpers.hasAnnotation(ASTHelpers.getSymbol(tree), FormatMethod.class, state);
boolean foundFormatString = false;
boolean foundString = false;
for (VariableTree param : tree.getParameters()) {
VarSymbol paramSymbol = ASTHelpers.getSymbol(param);
boolean isStringParam = ASTHelpers.isSameType(paramSymbol.type, stringType, state);
if (isStringParam) {
foundString = true;
}
if (ASTHelpers.hasAnnotation(paramSymbol, FormatString.class, state)) {
if (!isFormatMethod) {
return buildDescription(tree).setMessage("A parameter can only be annotated @FormatString in a method annotated " + "@FormatMethod: " + param).build();
}
if (!isStringParam) {
return buildDescription(param).setMessage("Only strings can be annotated @FormatString.").build();
}
if (foundFormatString) {
return buildDescription(tree).setMessage("A method cannot have more than one @FormatString parameter.").build();
}
foundFormatString = true;
}
}
if (isFormatMethod && !foundString) {
return buildDescription(tree).setMessage("An @FormatMethod must contain at least one String parameter.").build();
}
return Description.NO_MATCH;
}
Aggregations