use of com.sun.source.tree.VariableTree in project error-prone by google.
the class Overrides method matchMethod.
@Override
public Description matchMethod(MethodTree methodTree, VisitorState state) {
MethodSymbol methodSymbol = ASTHelpers.getSymbol(methodTree);
boolean isVarargs = (methodSymbol.flags() & Flags.VARARGS) != 0;
Set<MethodSymbol> superMethods = ASTHelpers.findSuperMethods(methodSymbol, state.getTypes());
// If there are no super methods, we're fine:
if (superMethods.isEmpty()) {
return Description.NO_MATCH;
}
Iterator<MethodSymbol> superMethodsIterator = superMethods.iterator();
boolean areSupersVarargs = superMethodsIterator.next().isVarArgs();
while (superMethodsIterator.hasNext()) {
if (areSupersVarargs != superMethodsIterator.next().isVarArgs()) {
// current method is inconsistent with some of its supermethods, so report a match.
return describeMatch(methodTree);
}
}
// The current method is consistent with all of its supermethods:
if (isVarargs == areSupersVarargs) {
return Description.NO_MATCH;
}
// The current method is inconsistent with all of its supermethods, so flip the varargs-ness
// of the current method.
List<? extends VariableTree> parameterTree = methodTree.getParameters();
Tree paramType = parameterTree.get(parameterTree.size() - 1).getType();
CharSequence paramTypeSource = state.getSourceForNode(paramType);
if (paramTypeSource == null) {
// No fix if we don't have tree end positions.
return describeMatch(methodTree);
}
Description.Builder descriptionBuilder = buildDescription(methodTree);
if (isVarargs) {
descriptionBuilder.addFix(SuggestedFix.replace(paramType, "[]", paramTypeSource.length() - 3, 0));
} else {
// There may be a comment that includes a '[' character between the open and closed
// brackets of the array type. If so, we don't return a fix.
int arrayOpenIndex = paramTypeSource.length() - 2;
while (paramTypeSource.charAt(arrayOpenIndex) == ' ') {
arrayOpenIndex--;
}
if (paramTypeSource.charAt(arrayOpenIndex) == '[') {
descriptionBuilder.addFix(SuggestedFix.replace(paramType, "...", arrayOpenIndex, 0));
}
}
return descriptionBuilder.build();
}
use of com.sun.source.tree.VariableTree in project error-prone by google.
the class BoxedPrimitiveConstructor method maybeCast.
private String maybeCast(VisitorState state, Type type, Type argType) {
if (doubleAndFloatStatus(state, type, argType) == DoubleAndFloatStatus.PRIMITIVE_DOUBLE_INTO_FLOAT) {
// e.g.: new Float(3.0d) => (float) 3.0d
return "(float) ";
}
// primitive widening conversions can't be combined with autoboxing, so add a
// explicit widening cast unless we're sure the expression doesn't get autoboxed
Tree parent = state.getPath().getParentPath().getLeaf();
// TODO(cushon): de-dupe with UnnecessaryCast
Type targetType = parent.accept(new TreeScanner<Type, Void>() {
@Override
public Type visitAssignment(AssignmentTree node, Void unused) {
return getType(node.getVariable());
}
@Override
public Type visitCompoundAssignment(CompoundAssignmentTree node, Void unused) {
return getType(node.getVariable());
}
@Override
public Type visitReturn(ReturnTree node, Void unused) {
return getType(state.findEnclosing(MethodTree.class).getReturnType());
}
@Override
public Type visitVariable(VariableTree node, Void unused) {
return getType(node.getType());
}
}, null);
if (!isSameType(type, argType, state) && !isSameType(targetType, type, state)) {
return String.format("(%s) ", type);
}
return "";
}
use of com.sun.source.tree.VariableTree in project checker-framework by typetools.
the class TreeUtils method isTreeInStaticScope.
/**
* Returns whether or not the leaf of the tree path is in a static scope.
*
* @param path TreePath whose leaf may or may not be in static scope
* @return returns whether or not the leaf of the tree path is in a static scope
*/
public static boolean isTreeInStaticScope(TreePath path) {
MethodTree enclosingMethod = TreeUtils.enclosingMethod(path);
if (enclosingMethod != null) {
return enclosingMethod.getModifiers().getFlags().contains(Modifier.STATIC);
}
// no enclosing method, check for static or initializer block
BlockTree block = enclosingTopLevelBlock(path);
if (block != null) {
return block.isStatic();
}
// check if its in a variable initializer
Tree t = enclosingVariable(path);
if (t != null) {
return ((VariableTree) t).getModifiers().getFlags().contains((Modifier.STATIC));
}
ClassTree classTree = enclosingClass(path);
if (classTree != null) {
return classTree.getModifiers().getFlags().contains((Modifier.STATIC));
}
return false;
}
use of com.sun.source.tree.VariableTree in project checker-framework by typetools.
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 checker-framework by typetools.
the class WholeProgramInferenceScenes method updateInferredParameterType.
/**
* Updates the parameter type represented by lhs of the method methodTree in the Scene of the
* receiverTree's enclosing class based on assignments to the parameter inside the method body.
*
* <ul>
* <li>If the Scene does not contain an annotated type for that parameter, then the type of
* the respective value passed as argument in the method call methodInvNode will be added
* to the parameter in the Scene.
* <li>If the Scene previously contained an annotated type for that parameter, then its new
* type will be the LUB between the previous type and the type of the respective value
* passed as argument in the method call.
* </ul>
*
* <p>
*
* @param lhs the node representing the parameter
* @param rhs the node being assigned to the parameter
* @param classTree the tree of the class that contains the parameter
* @param methodTree the tree of the method that contains the parameter
* @param atf the annotated type factory of a given type system, whose type hierarchy will be
* used to update the parameter type
*/
@Override
public void updateInferredParameterType(LocalVariableNode lhs, Node rhs, ClassTree classTree, MethodTree methodTree, AnnotatedTypeFactory atf) {
ClassSymbol classSymbol = getEnclosingClassSymbol(classTree, lhs);
// https://github.com/typetools/checker-framework/issues/682
if (classSymbol == null)
return;
String className = classSymbol.flatname.toString();
String jaifPath = helper.getJaifPath(className);
AClass clazz = helper.getAClass(className, jaifPath);
String methodName = JVMNames.getJVMMethodName(methodTree);
AMethod method = clazz.methods.vivify(methodName);
List<? extends VariableTree> params = methodTree.getParameters();
// Look-up parameter by name:
for (int i = 0; i < params.size(); i++) {
VariableTree vt = params.get(i);
if (vt.getName().contentEquals(lhs.getName())) {
Tree treeNode = rhs.getTree();
if (treeNode == null) {
// https://github.com/typetools/checker-framework/issues/682
continue;
}
AnnotatedTypeMirror paramATM = atf.getAnnotatedType(vt);
AnnotatedTypeMirror argATM = atf.getAnnotatedType(treeNode);
AField param = method.parameters.vivify(i);
helper.updateAnnotationSetInScene(param.type, atf, jaifPath, argATM, paramATM, TypeUseLocation.PARAMETER);
break;
}
}
}
Aggregations