use of com.sun.source.tree.ClassTree in project j2objc by google.
the class TreeConverter method convertClassDeclaration.
private TreeNode convertClassDeclaration(ClassTree node, TreePath parent) {
TreePath path = getTreePath(parent, node);
TypeElement element = (TypeElement) getElement(path);
// to support our different declaration nodes.
if (element.getKind() == ElementKind.ANNOTATION_TYPE) {
throw new AssertionError("Annotation type declaration tree conversion not implemented");
}
TypeDeclaration newNode = convertClassDeclarationHelper(node, parent);
newNode.setInterface(node.getKind() == Kind.INTERFACE || node.getKind() == Kind.ANNOTATION_TYPE);
if (ElementUtil.isAnonymous(element)) {
TypeMirror classType = getTypeMirror(path);
if (!classType.getAnnotationMirrors().isEmpty()) {
newUnit.getEnv().elementUtil().mapElementType(element, classType);
} else {
// With javac 12 and above, annotations are no longer part of the supertype,
// so check to see if a mutated type that has them is necessary.
GeneratedTypeElement newElement = GeneratedTypeElement.mutableCopy(element);
boolean annotationFound = copyAnnotations(node.getExtendsClause(), path, newNode, newElement);
for (Tree clause : node.getImplementsClause()) {
annotationFound |= copyAnnotations(clause, path, newNode, newElement);
}
if (annotationFound) {
newUnit.getEnv().elementUtil().mapElementType(element, newElement.asType());
}
}
}
return newNode;
}
use of com.sun.source.tree.ClassTree in project jOOQ by jOOQ.
the class Tools method enclosing.
static Element enclosing(TreePath path) {
MethodTree enclosingMethod = enclosingMethod(path);
if (enclosingMethod != null)
return elementFromDeclaration(enclosingMethod);
ClassTree enclosingClass = enclosingClass(path);
return elementFromDeclaration(enclosingClass);
}
use of com.sun.source.tree.ClassTree in project error-prone by google.
the class MutableMethodReturnType method isMethodCanBeOverridden.
private static boolean isMethodCanBeOverridden(MethodSymbol methodSymbol, VisitorState state) {
if (methodSymbol.isStatic() || methodSymbol.isPrivate() || isFinalMethod(methodSymbol)) {
return false;
}
ClassTree enclosingClassTree = ASTHelpers.findEnclosingNode(state.getPath(), ClassTree.class);
boolean isEnclosingClassFinal = isFinalClass(enclosingClassTree);
if (isEnclosingClassFinal) {
return false;
}
return true;
}
use of com.sun.source.tree.ClassTree in project error-prone by google.
the class Comments method getNextNodeOrParent.
/**
* Find the node which (approximately) follows this one in the tree. This works by walking upwards
* to find enclosing block (or class) and then looking for the node after the subtree we walked.
* If our subtree is the last of the block then we return the node for the block instead, if we
* can't find a suitable block we return the parent node.
*/
private static Tree getNextNodeOrParent(Tree current, VisitorState state) {
Tree predecessorNode = current;
TreePath enclosingPath = state.getPath();
while (enclosingPath != null && !(enclosingPath.getLeaf() instanceof BlockTree) && !(enclosingPath.getLeaf() instanceof ClassTree)) {
predecessorNode = enclosingPath.getLeaf();
enclosingPath = enclosingPath.getParentPath();
}
if (enclosingPath == null) {
return state.getPath().getParentPath().getLeaf();
}
Tree parent = enclosingPath.getLeaf();
if (parent instanceof BlockTree) {
return after(predecessorNode, ((BlockTree) parent).getStatements(), parent);
} else if (parent instanceof ClassTree) {
return after(predecessorNode, ((ClassTree) parent).getMembers(), parent);
}
return parent;
}
use of com.sun.source.tree.ClassTree in project error-prone by google.
the class FindIdentifiers method findAllIdents.
/**
* Finds the set of all bare variable identifiers in scope at the current location. Identifiers
* are ordered by ascending distance/scope count from the current location to match shadowing
* rules. That is, if two variables with the same simple names appear in the set, the one that
* appears first in iteration order is the one you get if you use the bare name in the source
* code.
*
* <p>We do not report variables that would require a qualfied access. We also do not handle
* wildcard imports.
*/
public static LinkedHashSet<VarSymbol> findAllIdents(VisitorState state) {
ImmutableSet.Builder<VarSymbol> result = new ImmutableSet.Builder<>();
Tree prev = state.getPath().getLeaf();
for (Tree curr : state.getPath().getParentPath()) {
switch(curr.getKind()) {
case BLOCK:
for (StatementTree stmt : ((BlockTree) curr).getStatements()) {
if (stmt.equals(prev)) {
break;
}
addIfVariable(stmt, result);
}
break;
case METHOD:
for (VariableTree param : ((MethodTree) curr).getParameters()) {
result.add(ASTHelpers.getSymbol(param));
}
break;
case CATCH:
result.add(ASTHelpers.getSymbol(((CatchTree) curr).getParameter()));
break;
case CLASS:
case INTERFACE:
case ENUM:
case ANNOTATION_TYPE:
// field is referred to by qualified name, but we don't support that.
for (Tree member : ((ClassTree) curr).getMembers()) {
if (member.equals(prev)) {
break;
}
addIfVariable(member, result);
}
// Collect inherited fields.
Type classType = ASTHelpers.getType(curr);
List<Type> classTypeClosure = state.getTypes().closure(classType);
List<Type> superTypes = classTypeClosure.size() <= 1 ? Collections.emptyList() : classTypeClosure.subList(1, classTypeClosure.size());
for (Type type : superTypes) {
Scope scope = type.tsym.members();
ImmutableList.Builder<VarSymbol> varsList = ImmutableList.builder();
for (Symbol var : scope.getSymbols(VarSymbol.class::isInstance)) {
varsList.add((VarSymbol) var);
}
result.addAll(varsList.build().reverse());
}
break;
case FOR_LOOP:
addAllIfVariable(((ForLoopTree) curr).getInitializer(), result);
break;
case ENHANCED_FOR_LOOP:
result.add(ASTHelpers.getSymbol(((EnhancedForLoopTree) curr).getVariable()));
break;
case TRY:
TryTree tryTree = (TryTree) curr;
boolean inResources = false;
for (Tree resource : tryTree.getResources()) {
if (resource.equals(prev)) {
inResources = true;
break;
}
}
if (inResources) {
// Case 1: we're in one of the resource declarations
for (Tree resource : tryTree.getResources()) {
if (resource.equals(prev)) {
break;
}
addIfVariable(resource, result);
}
} else if (tryTree.getBlock().equals(prev)) {
// Case 2: We're in the block (not a catch or finally)
addAllIfVariable(tryTree.getResources(), result);
}
break;
case COMPILATION_UNIT:
for (ImportTree importTree : ((CompilationUnitTree) curr).getImports()) {
if (importTree.isStatic() && importTree.getQualifiedIdentifier().getKind() == Kind.MEMBER_SELECT) {
MemberSelectTree memberSelectTree = (MemberSelectTree) importTree.getQualifiedIdentifier();
Scope scope = state.getTypes().membersClosure(ASTHelpers.getType(memberSelectTree.getExpression()), /* skipInterface= */
false);
for (Symbol var : scope.getSymbols(sym -> sym instanceof VarSymbol && sym.getSimpleName().equals(memberSelectTree.getIdentifier()))) {
result.add((VarSymbol) var);
}
}
}
break;
default:
// other node types don't introduce variables
break;
}
prev = curr;
}
// TODO(eaftan): switch out collector for ImmutableSet.toImmutableSet()
return result.build().stream().filter(var -> isVisible(var, state.getPath())).collect(Collectors.toCollection(LinkedHashSet::new));
}
Aggregations