use of com.sun.source.tree.MethodTree in project error-prone by google.
the class PreconditionsCheckNotNullPrimitive method hasMethodParameter.
/**
* Determines whether the expression contains a reference to one of the enclosing method's
* parameters.
*
* <p>TODO(eaftan): Extract this to ASTHelpers.
*
* @param path the path to the current tree node
* @param tree the node to compare against the parameters
* @return whether the argument is a parameter to the enclosing method
*/
private static boolean hasMethodParameter(TreePath path, ExpressionTree tree) {
Set<Symbol> symbols = new HashSet<>();
for (IdentifierTree ident : getVariableUses(tree)) {
Symbol sym = ASTHelpers.getSymbol(ident);
if (sym.isLocal()) {
symbols.add(sym);
}
}
// Find enclosing method declaration.
while (path != null && !(path.getLeaf() instanceof MethodTree)) {
path = path.getParentPath();
}
if (path == null) {
throw new IllegalStateException("Should have an enclosing method declaration");
}
MethodTree methodDecl = (MethodTree) path.getLeaf();
for (VariableTree param : methodDecl.getParameters()) {
if (symbols.contains(ASTHelpers.getSymbol(param))) {
return true;
}
}
return false;
}
use of com.sun.source.tree.MethodTree in project error-prone by google.
the class OverrideThrowableToString method matchClass.
@Override
public Description matchClass(ClassTree classTree, VisitorState visitorState) {
Symbol throwableClass = visitorState.getSymbolFromString("java.lang.Throwable");
if (Objects.equals(ASTHelpers.getSymbol(classTree.getExtendsClause()), throwableClass)) {
Optional<? extends Tree> methodTree = classTree.getMembers().stream().filter(m -> m instanceof MethodTree && ((MethodTree) m).getName().contentEquals("toString")).findFirst();
if (methodTree.isPresent()) {
SuggestedFix.Builder builder = SuggestedFix.builder();
MethodTree tree = (MethodTree) methodTree.get();
if (!tree.getParameters().isEmpty()) {
return Description.NO_MATCH;
}
String newTree = tree.getModifiers().toString().replaceAll("@Override[(][)]", "@Override") + "String getMessage()\n" + visitorState.getSourceForNode(tree.getBody());
builder.replace(tree, newTree);
return describeMatch(classTree, builder.build());
}
}
return Description.NO_MATCH;
}
use of com.sun.source.tree.MethodTree in project j2objc by google.
the class TreeConverter method convertAnnotationTypeDeclaration.
private TreeNode convertAnnotationTypeDeclaration(ClassTree node, TreePath parent) {
AnnotationTypeDeclaration newNode = new AnnotationTypeDeclaration();
TreePath path = getTreePath(parent, node);
Element element = getElement(path);
convertBodyDeclaration(node, path, node.getModifiers(), newNode);
for (Tree bodyDecl : node.getMembers()) {
if (bodyDecl.getKind() == Kind.METHOD) {
MethodTree methodTree = (MethodTree) bodyDecl;
TreePath methodPath = getTreePath(path, methodTree);
ExecutableElement methodElement = (ExecutableElement) getElement(methodPath);
Tree defaultValue = methodTree.getDefaultValue();
ModifiersTree modifiers = methodTree.getModifiers();
AnnotationTypeMemberDeclaration newMember = new AnnotationTypeMemberDeclaration().setDefault((Expression) convert(defaultValue, methodPath)).setExecutableElement(methodElement);
newMember.setModifiers((int) ((JCModifiers) modifiers).flags).setAnnotations(convertAnnotations(modifiers, getTreePath(methodPath, modifiers))).setJavadoc((Javadoc) getAssociatedJavaDoc(methodTree, methodPath));
newNode.addBodyDeclaration(newMember);
} else {
newNode.addBodyDeclaration((BodyDeclaration) convert(bodyDecl, path));
}
}
return newNode.setName(convertSimpleName(element, getTypeMirror(path), getNamePosition(node))).setTypeElement((TypeElement) element);
}
use of com.sun.source.tree.MethodTree in project vertx-docgen by vert-x3.
the class JavaDocGenerator method renderSource.
/**
* Render the source fragment for the Java language. Java being the pivot language, we consider this method as the
* _default_ behavior. This method is final as it must not be overridden by any extension.
*
* @param elt the element
* @param source the source
* @return the fragment
*/
@Override
public String renderSource(ExecutableElement elt, String source) {
// Get block
TreePath path = docTrees.getPath(elt);
MethodTree methodTree = (MethodTree) path.getLeaf();
BlockTree blockTree = methodTree.getBody();
List<? extends StatementTree> statements = blockTree.getStatements();
if (statements.size() > 0) {
return renderSource(path, statements, source);
} else {
return null;
}
}
use of com.sun.source.tree.MethodTree 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));
}
Aggregations