use of com.sun.source.tree.ClassTree in project error-prone by google.
the class PrivateConstructorForUtilityClass method isInPrivateScope.
private static boolean isInPrivateScope(VisitorState state) {
TreePath treePath = state.getPath();
do {
Tree currentLeaf = treePath.getLeaf();
if (ClassTree.class.isInstance(currentLeaf)) {
ClassTree currentClassTree = (ClassTree) currentLeaf;
if (currentClassTree.getModifiers().getFlags().contains(PRIVATE)) {
return true;
}
}
treePath = treePath.getParentPath();
} while (treePath != null);
return false;
}
use of com.sun.source.tree.ClassTree in project error-prone by google.
the class IterableAndIterator method matchAnySuperType.
private boolean matchAnySuperType(ClassTree tree, VisitorState state) {
List<Tree> superTypes = Lists.<Tree>newArrayList(tree.getImplementsClause());
Tree superClass = tree.getExtendsClause();
if (superClass != null) {
superTypes.add(superClass);
}
/* NOTE: at "Eight day", use Java 8 feature below
return superTypes.stream()
.anyMatch(superType -> ITERABLE_AND_ITERATOR_MATCHER.matches(superType, state));
*/
for (Tree superType : superTypes) {
if (ITERABLE_AND_ITERATOR_MATCHER.matches(superType, state)) {
return true;
}
}
return false;
}
use of com.sun.source.tree.ClassTree in project checker-framework by typetools.
the class AnnotatedTypeFactory method getSelfType.
/**
* Returns the type of {@code this} in the current location, which can be used if {@code this}
* has a special semantics (e.g. {@code this} is non-null).
*
* <p>The parameter is an arbitrary tree and does not have to mention "this", neither explicitly
* nor implicitly. This method should be overridden for type-system specific behavior.
*
* <p>TODO: in 1.8.2, handle all receiver type annotations. TODO: handle enclosing classes
* correctly.
*/
public AnnotatedDeclaredType getSelfType(Tree tree) {
TreePath path = getPath(tree);
ClassTree enclosingClass = TreeUtils.enclosingClass(path);
if (enclosingClass == null) {
// I hope this only happens when tree is a fake tree that
// we created, e.g. when desugaring enhanced-for-loops.
enclosingClass = getCurrentClassTree(tree);
}
AnnotatedDeclaredType type = getAnnotatedType(enclosingClass);
MethodTree enclosingMethod = TreeUtils.enclosingMethod(path);
if (enclosingClass.getSimpleName().length() != 0 && enclosingMethod != null) {
AnnotatedDeclaredType methodReceiver;
if (TreeUtils.isConstructor(enclosingMethod)) {
methodReceiver = (AnnotatedDeclaredType) getAnnotatedType(enclosingMethod).getReturnType();
} else {
methodReceiver = getAnnotatedType(enclosingMethod).getReceiverType();
}
if (shouldTakeFromReceiver(methodReceiver)) {
// TODO what about all annotations on the receiver?
// Code is also duplicated above.
type.clearAnnotations();
type.addAnnotations(methodReceiver.getAnnotations());
}
}
return type;
}
use of com.sun.source.tree.ClassTree in project checker-framework by typetools.
the class AnnotatedTypeFactory method isMostEnclosingThisDeref.
/**
* Determine whether the tree dereferences the most enclosing "this" object. That is, we have an
* expression like "f.g" and want to know whether it is an access "this.f.g" or whether e.g. f
* is a field of an outer class or e.g. f is a local variable.
*
* @param tree the tree to check
* @return true, iff the tree is an explicit or implicit reference to the most enclosing "this"
*/
public final boolean isMostEnclosingThisDeref(ExpressionTree tree) {
if (!isAnyEnclosingThisDeref(tree)) {
return false;
}
Element element = TreeUtils.elementFromUse(tree);
TypeElement typeElt = ElementUtils.enclosingClass(element);
ClassTree enclosingClass = getCurrentClassTree(tree);
if (enclosingClass != null && isSubtype(TreeUtils.elementFromDeclaration(enclosingClass), typeElt)) {
return true;
}
// ran out of options
return false;
}
use of com.sun.source.tree.ClassTree in project checker-framework by typetools.
the class InitializationTransfer method initializedFieldsAfterCall.
/**
* Returns the fields that can safely be considered initialized after the method call {@code
* node}.
*/
protected List<VariableElement> initializedFieldsAfterCall(MethodInvocationNode node, ConditionalTransferResult<V, S> transferResult) {
List<VariableElement> result = new ArrayList<>();
MethodInvocationTree tree = node.getTree();
ExecutableElement method = TreeUtils.elementFromUse(tree);
boolean isConstructor = method.getSimpleName().contentEquals("<init>");
Node receiver = node.getTarget().getReceiver();
String methodString = tree.getMethodSelect().toString();
// invariant fields are guaranteed to be initialized.
if (isConstructor && receiver instanceof ThisLiteralNode && methodString.equals("this")) {
ClassTree clazz = TreeUtils.enclosingClass(analysis.getTypeFactory().getPath(tree));
TypeElement clazzElem = TreeUtils.elementFromDeclaration(clazz);
markInvariantFieldsAsInitialized(result, clazzElem);
}
// invariant fields of any super class are guaranteed to be initialized.
if (isConstructor && receiver instanceof ThisLiteralNode && methodString.equals("super")) {
ClassTree clazz = TreeUtils.enclosingClass(analysis.getTypeFactory().getPath(tree));
TypeElement clazzElem = TreeUtils.elementFromDeclaration(clazz);
TypeMirror superClass = clazzElem.getSuperclass();
while (superClass != null && superClass.getKind() != TypeKind.NONE) {
clazzElem = (TypeElement) analysis.getTypes().asElement(superClass);
superClass = clazzElem.getSuperclass();
markInvariantFieldsAsInitialized(result, clazzElem);
}
}
return result;
}
Aggregations