@Override public Description matchMethod(MethodTree tree, VisitorState state) { return checkArrayDimensions(tree, tree.getReturnType(), state); }
@Override public boolean matches(MethodTree methodTree, VisitorState state) { Tree returnTree = methodTree.getReturnType(); if (returnTree == null) { // This is a constructor, it has no return type. return false; } return returnTypeMatcher.matches(returnTree, state); } };
@Override public UClassDecl visitClass(ClassTree tree, Void v) { ImmutableList.Builder<UMethodDecl> decls = ImmutableList.builder(); for (MethodTree decl : Iterables.filter(tree.getMembers(), MethodTree.class)) { if (decl.getReturnType() != null) { decls.add(visitMethod(decl, null)); } } return UClassDecl.create(decls.build()); }
@Nullable @Override public Type visitReturn(ReturnTree tree, Void unused) { for (TreePath path = parent; path != null; path = path.getParentPath()) { Tree enclosing = path.getLeaf(); switch (enclosing.getKind()) { case METHOD: return getType(((MethodTree) enclosing).getReturnType()); case LAMBDA_EXPRESSION: return visitLambdaExpression((LambdaExpressionTree) enclosing, null); default: // fall out } } throw new AssertionError("return not enclosed by method or lambda"); }
@Override public Boolean visitMethod(MethodTree node, VisitorState state) { boolean prevInBoxedVoidReturningMethod = inBoxedVoidReturningMethod; Type returnType = ASTHelpers.getType(node.getReturnType()); Type boxedVoidType = state.getTypeFromString("java.lang.Void"); if (ASTHelpers.isSameType(returnType, boxedVoidType, state)) { inBoxedVoidReturningMethod = true; } boolean result = node.getBody() != null && node.getBody().accept(this, state); inBoxedVoidReturningMethod = prevInBoxedVoidReturningMethod; return result; }
@Override public Choice<Unifier> visitClass(ClassTree node, Unifier unifier) { Choice<UnifierWithRemainingMembers> path = Choice.of(UnifierWithRemainingMembers.create(unifier, getMembers())); for (Tree targetMember : node.getMembers()) { if (!(targetMember instanceof MethodTree) || ((MethodTree) targetMember).getReturnType() != null) { // skip synthetic constructors path = path.thenChoose(match(targetMember)); } } return path.condition(s -> s.remainingMembers().isEmpty()) .transform(UnifierWithRemainingMembers::unifier); }
toReplace = ((ReturnTree) enclosing).getExpression(); MethodTree enclosingMethod = ASTHelpers.findEnclosingNode(path, MethodTree.class); typeTree = enclosingMethod == null ? null : enclosingMethod.getReturnType();
@Override @Nullable public Choice<Unifier> visitMethod(MethodTree decl, @Nullable Unifier unifier) { return getName() .unify(decl.getName(), unifier) .thenChoose(unifications(getReturnType(), decl.getReturnType())) .thenChoose(unifications(getParameters(), decl.getParameters())) .thenChoose(unifications(getThrows(), decl.getThrows())) .thenChoose(unifications(getBody(), decl.getBody())); }
Tree typeTree = getTypeTree(methodTree.getReturnType()); verify(typeTree != null, "Could not find return type of %s", methodTree); fixBuilder.replace( SuggestedFix fix = fixBuilder.build(); return describeMatch(methodTree.getReturnType(), fix);
@Override public Void visitReturn(ReturnTree returnTree, Void unused) { if (isSameType(getType(methodTree.getReturnType()), state.getSymtab().voidType, state)) { state.reportMatch( buildDescription(diagnosticPosition(getCurrentPath(), state)) .addFix(Utils.replace(returnTree, "", state)) .build()); } return super.visitReturn(returnTree, null); } }
@Override public UMethodDecl visitMethod(MethodTree decl, Void v) { return UMethodDecl.create( visitModifiers(decl.getModifiers(), null), decl.getName(), templateType(decl.getReturnType()), cast(templateStatements(decl.getParameters()), UVariableDecl.class), templateExpressions(decl.getThrows()), (UBlock) template(decl.getBody())); }
@Override public Description matchReturn(ReturnTree tree, VisitorState state) { if (tree.getExpression() == null) { return NO_MATCH; } Type type = null; outer: for (Tree parent : state.getPath()) { switch (parent.getKind()) { case METHOD: type = ASTHelpers.getType(((MethodTree) parent).getReturnType()); break outer; case LAMBDA_EXPRESSION: type = state.getTypes().findDescriptorType(ASTHelpers.getType(parent)).getReturnType(); break outer; default: // fall out } } if (type == null) { return NO_MATCH; } return check(type, tree.getExpression()); }
@Override public Description matchMethod(final MethodTree tree, final VisitorState state) { if ((tree.getReturnType() == null) || !tree.getReturnType().toString().startsWith("Optional<") || (tree.getBody() == null) || (!CONTAINS_RETURN_NULL.matches(tree.getBody(), state))) { return Description.NO_MATCH; } return describeMatch(tree); } }
@Override public UClassDecl visitClass(ClassTree tree, Void v) { ImmutableList.Builder<UMethodDecl> decls = ImmutableList.builder(); for (MethodTree decl : Iterables.filter(tree.getMembers(), MethodTree.class)) { if (decl.getReturnType() != null) { decls.add(visitMethod(decl, null)); } } return UClassDecl.create(decls.build()); }
private static boolean isSetter(Tree member) { return member.getKind() == Tree.Kind.METHOD && name(member).startsWith("set") //NOI18N && ((MethodTree)member).getParameters().size() == 1 && ((MethodTree)member).getReturnType().getKind() == Tree.Kind.PRIMITIVE_TYPE && ((PrimitiveTypeTree)((MethodTree)member).getReturnType()).getPrimitiveTypeKind() == TypeKind.VOID; }
private static boolean isGetter(Tree member) { return member.getKind() == Tree.Kind.METHOD && name(member).startsWith("get") //NOI18N && ((MethodTree)member).getParameters().isEmpty() && (((MethodTree)member).getReturnType().getKind() != Tree.Kind.PRIMITIVE_TYPE || ((PrimitiveTypeTree)((MethodTree)member).getReturnType()).getPrimitiveTypeKind() != TypeKind.VOID); }
@Override public Void visitMethod(MethodTree node, Void p) { Tree returnType = node.getReturnType(); if (returnType != null) { checkPackageAccess(returnType, getQualifiedPackageName(returnType)); } return super.visitMethod(node, p); }
@Override public Tree visitMethod(MethodTree tree, Void p) { MethodTree n = make.Method((ModifiersTree) tree.getModifiers(), tree.getName().toString(), (ExpressionTree) tree.getReturnType(), tree.getTypeParameters(), tree.getParameters(), tree.getThrows(), tree.getBody(), (ExpressionTree) tree.getDefaultValue()); comments.copyComments(tree, n); model.setPos(n, model.getPos(tree)); return n; }
private MethodTree modifyMethodTypeParameter(MethodTree method, int index, TypeParameterTree typeParameter, Operation op) { MethodTree copy = Method( method.getModifiers(), method.getName(), method.getReturnType(), c(method.getTypeParameters(), index, typeParameter, op), method.getParameters(), method.getThrows(), method.getBody(), (ExpressionTree) method.getDefaultValue() ); return copy; }
@Override public Void visitReturn(ReturnTree returnTree, Void unused) { if (isSameType(getType(methodTree.getReturnType()), state.getSymtab().voidType, state)) { state.reportMatch( buildDescription(diagnosticPosition(getCurrentPath(), state)) .addFix(Utils.replace(returnTree, "", state)) .build()); } return super.visitReturn(returnTree, null); } }