use of com.sun.source.tree.AssignmentTree in project bazel by bazelbuild.
the class TreeUtils method getAssignmentContext.
/**
* Returns the tree with the assignment context for the treePath
* leaf node.
*
* The assignment context for the treepath is the most enclosing
* tree of type:
* <ul>
* <li>AssignmentTree </li>
* <li>CompoundAssignmentTree </li>
* <li>MethodInvocationTree</li>
* <li>NewArrayTree</li>
* <li>NewClassTree</li>
* <li>ReturnTree</li>
* <li>VariableTree</li>
* </ul>
*
* @param treePath
* @return the assignment context as described.
*/
public static Tree getAssignmentContext(final TreePath treePath) {
TreePath path = treePath.getParentPath();
if (path == null)
return null;
Tree node = path.getLeaf();
if ((node instanceof AssignmentTree) || (node instanceof CompoundAssignmentTree) || (node instanceof MethodInvocationTree) || (node instanceof NewArrayTree) || (node instanceof NewClassTree) || (node instanceof ReturnTree) || (node instanceof VariableTree))
return node;
return null;
}
use of com.sun.source.tree.AssignmentTree 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.AssignmentTree in project error-prone by google.
the class CompileTimeConstantExpressionMatcherTest method assertCompilerMatchesOnAssignment.
// Helper methods.
private void assertCompilerMatchesOnAssignment(final Map<String, Boolean> expectedMatches, String... lines) {
final Matcher<ExpressionTree> matcher = new CompileTimeConstantExpressionMatcher();
final Scanner scanner = new Scanner() {
@Override
public Void visitAssignment(AssignmentTree t, VisitorState state) {
ExpressionTree lhs = t.getVariable();
if (expectedMatches.containsKey(lhs.toString())) {
boolean matches = matcher.matches(t.getExpression(), state);
if (expectedMatches.get(lhs.toString())) {
assertTrue("Matcher should match expression" + t.getExpression(), matches);
} else {
assertFalse("Matcher should not match expression" + t.getExpression(), matches);
}
}
return super.visitAssignment(t, state);
}
};
CompilationTestHelper.newInstance(ScannerSupplier.fromScanner(scanner), getClass()).expectResult(Result.OK).addSourceLines("test/CompileTimeConstantExpressionMatcherTestCase.java", lines).doTest();
}
use of com.sun.source.tree.AssignmentTree in project checker-framework by typetools.
the class BaseTypeVisitor method visitAnnotation.
/* TODO: something similar to visitReturn should be done.
* public Void visitThrow(ThrowTree node, Void p) {
* return super.visitThrow(node, p);
* }
*/
/**
* Ensure that the annotation arguments comply to their declarations. This needs some special
* casing, as annotation arguments form special trees.
*/
@Override
public Void visitAnnotation(AnnotationTree node, Void p) {
List<? extends ExpressionTree> args = node.getArguments();
if (args.isEmpty()) {
// Nothing to do if there are no annotation arguments.
return null;
}
TypeElement anno = (TypeElement) TreeInfo.symbol((JCTree) node.getAnnotationType());
Name annoName = anno.getQualifiedName();
if (annoName.contentEquals(DefaultQualifier.class.getName()) || annoName.contentEquals(SuppressWarnings.class.getName())) {
// Skip these two annotations, as we don't care about the arguments to them.
return null;
}
// Mapping from argument simple name to its annotated type.
Map<String, AnnotatedTypeMirror> annoTypes = new HashMap<>();
for (Element encl : ElementFilter.methodsIn(anno.getEnclosedElements())) {
AnnotatedExecutableType exeatm = (AnnotatedExecutableType) atypeFactory.getAnnotatedType(encl);
AnnotatedTypeMirror retty = exeatm.getReturnType();
annoTypes.put(encl.getSimpleName().toString(), retty);
}
for (ExpressionTree arg : args) {
if (!(arg instanceof AssignmentTree)) {
// TODO: when can this happen?
continue;
}
AssignmentTree at = (AssignmentTree) arg;
// we don't have a type for annotations.
if (at.getExpression().getKind() == Tree.Kind.ANNOTATION) {
visitAnnotation((AnnotationTree) at.getExpression(), p);
continue;
}
if (at.getExpression().getKind() == Tree.Kind.NEW_ARRAY) {
NewArrayTree nat = (NewArrayTree) at.getExpression();
boolean isAnno = false;
for (ExpressionTree init : nat.getInitializers()) {
if (init.getKind() == Tree.Kind.ANNOTATION) {
visitAnnotation((AnnotationTree) init, p);
isAnno = true;
}
}
if (isAnno) {
continue;
}
}
AnnotatedTypeMirror expected = annoTypes.get(at.getVariable().toString());
Pair<Tree, AnnotatedTypeMirror> preAssCtxt = visitorState.getAssignmentContext();
{
// Determine and set the new assignment context.
ExpressionTree var = at.getVariable();
assert var instanceof IdentifierTree : "Expected IdentifierTree as context. Found: " + var;
AnnotatedTypeMirror meth = atypeFactory.getAnnotatedType(var);
assert meth instanceof AnnotatedExecutableType : "Expected AnnotatedExecutableType as context. Found: " + meth;
AnnotatedTypeMirror newctx = ((AnnotatedExecutableType) meth).getReturnType();
visitorState.setAssignmentContext(Pair.of((Tree) null, newctx));
}
try {
AnnotatedTypeMirror actual = atypeFactory.getAnnotatedType(at.getExpression());
if (expected.getKind() != TypeKind.ARRAY) {
// Expected is not an array -> direct comparison.
commonAssignmentCheck(expected, actual, at.getExpression(), "annotation.type.incompatible");
} else {
if (actual.getKind() == TypeKind.ARRAY) {
// Both actual and expected are arrays.
commonAssignmentCheck(expected, actual, at.getExpression(), "annotation.type.incompatible");
} else {
// The declaration is an array type, but just a single
// element is given.
commonAssignmentCheck(((AnnotatedArrayType) expected).getComponentType(), actual, at.getExpression(), "annotation.type.incompatible");
}
}
} finally {
visitorState.setAssignmentContext(preAssCtxt);
}
}
return null;
}
use of com.sun.source.tree.AssignmentTree in project st-js by st-js.
the class TreeUtils method getAssignmentContext.
/**
* Returns the tree with the assignment context for the treePath leaf node.
*
* The assignment context for the treepath is the most enclosing tree of type:
* <ul>
* <li>AssignmentTree</li>
* <li>CompoundAssignmentTree</li>
* <li>MethodInvocationTree</li>
* <li>NewArrayTree</li>
* <li>NewClassTree</li>
* <li>ReturnTree</li>
* <li>VariableTree</li>
* </ul>
*
* @param treePath
* a {@link com.sun.source.util.TreePath} object.
* @return the assignment context as described.
*/
public static Tree getAssignmentContext(final TreePath treePath) {
TreePath path = treePath.getParentPath();
if (path == null) {
return null;
}
Tree node = path.getLeaf();
if ((node instanceof AssignmentTree) || (node instanceof CompoundAssignmentTree) || (node instanceof MethodInvocationTree) || (node instanceof NewArrayTree) || (node instanceof NewClassTree) || (node instanceof ReturnTree) || (node instanceof VariableTree)) {
return node;
}
return null;
}
Aggregations