private static List<? extends TypeMirror> computeMethodInvocation( Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) { MethodInvocationTree nat = (MethodInvocationTree) parent.getLeaf(); boolean errorInRealArguments = false; for (Tree param : nat.getArguments()) { errorInRealArguments |= param == error; } if (errorInRealArguments) { TypeMirror[] proposedType = new TypeMirror[1]; int[] proposedIndex = new int[1]; ExecutableElement ee = org.netbeans.modules.editor.java.Utilities.fuzzyResolveMethodInvocation( info, parent, proposedType, proposedIndex); if (ee == null) { // cannot be resolved return null; } types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); return Collections.singletonList(proposedType[0]); } return null; }
@Override public boolean matches(MethodInvocationTree methodInvocationTree, VisitorState state) { if (methodInvocationTree.getArguments().size() <= position) { return false; } return argumentMatcher.matches(methodInvocationTree.getArguments().get(position), state); }
@Override public Description matchMethodInvocation( MethodInvocationTree methodInvocationTree, VisitorState state) { if (methodInvocationTree.getArguments().isEmpty()) { return Description.NO_MATCH; } if (!TRUTH_SUBJECT_CALL.matches(methodInvocationTree, state)) { return Description.NO_MATCH; } ExpressionTree rec = ASTHelpers.getReceiver(methodInvocationTree); if (rec == null) { return Description.NO_MATCH; } if (!ASSERT_THAT.matches(rec, state)) { return Description.NO_MATCH; } ExpressionTree expr = getOnlyElement(((MethodInvocationTree) rec).getArguments()); if (expr == null) { return Description.NO_MATCH; } // check that argument of assertThat is a constant if (ASTHelpers.constValue(expr) == null) { return Description.NO_MATCH; } // check that expectation isn't a constant ExpressionTree expectation = getOnlyElement(methodInvocationTree.getArguments()); if (ASTHelpers.constValue(expectation) != null) { return Description.NO_MATCH; } SuggestedFix fix = SuggestedFix.swap(expr, expectation); return buildDescription(methodInvocationTree).addFix(fix).build(); }
/** * Find the root assignable expression of a chain of field accesses. If there is no root (i.e, a * bare method call or a static method call), return null. * * <p>Examples: * * <pre>{@code * a.trim().intern() ==> a * a.b.trim().intern() ==> a.b * this.intValue.foo() ==> this.intValue * this.foo() ==> this * intern() ==> null * String.format() ==> null * java.lang.String.format() ==> null * }</pre> */ public static ExpressionTree getRootAssignable(MethodInvocationTree methodInvocationTree) { if (!(methodInvocationTree instanceof JCMethodInvocation)) { throw new IllegalArgumentException( "Expected type to be JCMethodInvocation, but was " + methodInvocationTree.getClass()); } // Check for bare method call, e.g. intern(). if (((JCMethodInvocation) methodInvocationTree).getMethodSelect() instanceof JCIdent) { return null; } // Unwrap the field accesses until you get to an identifier. ExpressionTree expr = methodInvocationTree; while (expr instanceof JCMethodInvocation) { expr = ((JCMethodInvocation) expr).getMethodSelect(); if (expr instanceof JCFieldAccess) { expr = ((JCFieldAccess) expr).getExpression(); } } // We only want assignable identifiers. Symbol sym = getSymbol(expr); if (sym instanceof VarSymbol) { return expr; } return null; }
/** Gets the symbol for a method invocation. */ public static MethodSymbol getSymbol(MethodInvocationTree tree) { Symbol sym = ASTHelpers.getSymbol(tree.getMethodSelect()); if (!(sym instanceof MethodSymbol)) { // Defensive. Would only occur if there are errors in the AST. return null; } return (MethodSymbol) sym; }
/** * Suggests replacing with Arrays.equals(a, b). Also adds the necessary import statement for * java.util.Arrays. */ @Override public Description matchMethodInvocation(MethodInvocationTree t, VisitorState state) { String arg1; String arg2; if (instanceEqualsMatcher.matches(t, state)) { arg1 = ((JCFieldAccess) t.getMethodSelect()).getExpression().toString(); arg2 = t.getArguments().get(0).toString(); } else if (staticEqualsMatcher.matches(t, state)) { arg1 = t.getArguments().get(0).toString(); arg2 = t.getArguments().get(1).toString(); } else { return NO_MATCH; } Fix fix = SuggestedFix.builder() .replace(t, "Arrays.equals(" + arg1 + ", " + arg2 + ")") .addImport("java.util.Arrays") .build(); return describeMatch(t, fix); }
public void handle(MethodInvocationTree tree, AnnotatedExecutableType method) { if (TreeUtils.isMethodInvocation(tree, systemGetProperty, env)) { List<? extends ExpressionTree> args = tree.getArguments(); assert args.size() == 1; ExpressionTree arg = args.get(0); if (arg.getKind() == Tree.Kind.STRING_LITERAL) { String literal = (String) ((LiteralTree) arg).getValue(); if (systemProperties.contains(literal)) { AnnotatedTypeMirror type = method.getReturnType(); type.replaceAnnotation(factory.NONNULL); } } } }
@Override public Void visitMethodInvocation(MethodInvocationTree node, Void p) { if (isInvocationOfEquals(node)) { AnnotatedTypeMirror recv = atypeFactory.getReceiverType(node); AnnotatedTypeMirror comp = atypeFactory.getAnnotatedType(node.getArguments().get(0)); if (this.checker.getLintOption("dotequals", true) && recv.hasEffectiveAnnotation(INTERNED) && comp.hasEffectiveAnnotation(INTERNED)) checker.report(Result.warning("unnecessary.equals"), node); } return super.visitMethodInvocation(node, p); }
@Override public Integer visitMethodInvocation(MethodInvocationTree node, Object p) { Integer n = super.visitMethodInvocation(node, p); if (n == null) { n = 0; } String invocation = node.getMethodSelect().toString(); if (invocation.contains("assert")) { n++; } return n; }
@Override public Void visitMethodInvocation(MethodInvocationTree tree, AnnotatedTypeMirror type) { if (isUnderlyingTypeAValue(type) && methodIsStaticallyExecutable(TreeUtils.elementFromUse(tree))) { // Get argument values List<? extends ExpressionTree> arguments = tree.getArguments(); ArrayList<List<?>> argValues; if (arguments.size() > 0) { argValues = new ArrayList<List<?>>(); for (ExpressionTree argument : arguments) { AnnotatedTypeMirror argType = getAnnotatedType(argument); List<?> values = getValues(argType, argType.getUnderlyingType()); if (values.isEmpty()) { // values aren't known, so don't try to evaluate the // method return null; } argValues.add(values); } } else { argValues = null; } // Get receiver values AnnotatedTypeMirror receiver = getReceiverType(tree); List<?> receiverValues; if (receiver != null && !ElementUtils.isStatic(TreeUtils.elementFromUse(tree))) { receiverValues = getValues(receiver, receiver.getUnderlyingType()); if (receiverValues.isEmpty()) { // values aren't known, so don't try to evaluate the // method return null; } } else { receiverValues = null; } // Evaluate method List<?> returnValues = evalutator.evaluteMethodCall(argValues, receiverValues, tree); AnnotationMirror returnType = resultAnnotationHandler(type.getUnderlyingType(), returnValues, tree); type.replaceAnnotation(returnType); } return null; }
@Override public boolean matches(MethodInvocationTree methodInvocationTree, VisitorState state) { return methodSelectMatcher.matches(methodInvocationTree, state) && matches(methodInvocationTree.getArguments()); }