Java Code Examples for lombok.ast.MethodInvocation#astOperand()

The following examples show how to use lombok.ast.MethodInvocation#astOperand() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WrongCallDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void visitMethod(@NonNull JavaContext context, @Nullable AstVisitor visitor,
        @NonNull MethodInvocation node) {

    // Call is only allowed if it is both only called on the super class (invoke special)
    // as well as within the same overriding method (e.g. you can't call super.onLayout
    // from the onMeasure method)
    Expression operand = node.astOperand();
    if (!(operand instanceof Super)) {
        report(context, node);
        return;
    }

    Node method = StringFormatDetector.getParentMethod(node);
    if (!(method instanceof MethodDeclaration) ||
            !((MethodDeclaration)method).astMethodName().astValue().equals(
                    node.astName().astValue())) {
        report(context, node);
    }
}
 
Example 2
Source File: ViewHolderDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visitMethodInvocation(MethodInvocation node) {
    if (node.astOperand() != null) {
        String methodName = node.astName().astValue();
        if (methodName.equals(INFLATE) && node.astArguments().size() >= 1) {
            // See if we're inside a conditional
            boolean insideIf = false;
            Node p = node.getParent();
            while (p != null) {
                if (p instanceof If || p instanceof InlineIfExpression
                        || p instanceof Switch) {
                    insideIf = true;
                    mHaveConditional = true;
                    break;
                } else if (p == node) {
                    break;
                }
                p = p.getParent();
            }
            if (!insideIf) {
                // Rather than reporting immediately, we only report if we didn't
                // find any conditionally executed inflate statements in the method.
                // This is because there are cases where getView method is complicated
                // and inflates not just the top level layout but also children
                // of the view, and we don't want to flag these. (To be more accurate
                // should perform flow analysis and only report unconditional inflation
                // of layouts that wind up flowing to the return value; that requires
                // more work, and this simple heuristic is good enough for nearly all test
                // cases I've come across.
                if (mNodes == null) {
                    mNodes = Lists.newArrayList();
                }
                mNodes.add(node);
            }
        }
    }

    return super.visitMethodInvocation(node);
}
 
Example 3
Source File: StringFormatDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void visitMethod(@NonNull JavaContext context, @Nullable AstVisitor visitor,
        @NonNull MethodInvocation node) {
    if (mFormatStrings == null && !context.getClient().supportsProjectResources()) {
        return;
    }

    String methodName = node.astName().astValue();
    if (methodName.equals(FORMAT_METHOD)) {
        // String.format(getResources().getString(R.string.foo), arg1, arg2, ...)
        // Check that the arguments in R.string.foo match arg1, arg2, ...
        if (node.astOperand() instanceof VariableReference) {
            VariableReference ref = (VariableReference) node.astOperand();
            if ("String".equals(ref.astIdentifier().astValue())) { //$NON-NLS-1$
                // Found a String.format call
                // Look inside to see if we can find an R string
                // Find surrounding method
                checkFormatCall(context, node);
            }
        }
    } else {
        // getResources().getString(R.string.foo, arg1, arg2, ...)
        // Check that the arguments in R.string.foo match arg1, arg2, ...
        if (node.astArguments().size() > 1 && node.astOperand() != null ) {
            checkFormatCall(context, node);
        }
    }
}
 
Example 4
Source File: ToastDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visitMethodInvocation(MethodInvocation node) {
    if (node == mTarget) {
        mSeenTarget = true;
    } else if ((mSeenTarget || node.astOperand() == mTarget)
            && "show".equals(node.astName().astValue())) { //$NON-NLS-1$
        // TODO: Do more flow analysis to see whether we're really calling show
        // on the right type of object?
        mFound = true;
    }

    return true;
}
 
Example 5
Source File: UnsafeAndroidDetector.java    From SafelyAndroid with MIT License 5 votes vote down vote up
@Override
public boolean visitMethodInvocation(MethodInvocation node) {
    System.out.println("ReceiverFinder::visitMethodInvocation " + node);
    if(node == this.mTarget) {
        this.mSeenTarget = true;
    } else if((this.mSeenTarget || node.astOperand() == this.mTarget) && "show".equals(node.astName().astValue())) {
        this.mFound = true;
    }

    return true;
}
 
Example 6
Source File: ToastDetector.java    From MeituanLintDemo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visitMethodInvocation(MethodInvocation node) {
    if (node == mTarget) {
        mSeenTarget = true;
    } else if ((mSeenTarget || node.astOperand() == mTarget)
            && "show".equals(node.astName().astValue())) { //$NON-NLS-1$
        // TODO: Do more flow analysis to see whether we're really calling show
        // on the right type of object?
        mFound = true;
    }

    return true;
}
 
Example 7
Source File: CutPasteDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void visitMethod(@NonNull JavaContext context, @Nullable AstVisitor visitor,
        @NonNull MethodInvocation call) {
    String lhs = getLhs(call);
    if (lhs == null) {
        return;
    }

    Node method = JavaContext.findSurroundingMethod(call);
    if (method == null) {
        return;
    } else if (method != mLastMethod) {
        mIds = Maps.newHashMap();
        mLhs = Maps.newHashMap();
        mCallOperands = Maps.newHashMap();
        mLastMethod = method;
    }

    String callOperand = call.astOperand() != null ? call.astOperand().toString() : "";

    Expression first = call.astArguments().first();
    if (first instanceof Select) {
        Select select = (Select) first;
        String id = select.astIdentifier().astValue();
        Expression operand = select.astOperand();
        if (operand instanceof Select) {
            Select type = (Select) operand;
            if (type.astIdentifier().astValue().equals(RESOURCE_CLZ_ID)) {
                if (mIds.containsKey(id)) {
                    if (lhs.equals(mLhs.get(id))) {
                        return;
                    }
                    if (!callOperand.equals(mCallOperands.get(id))) {
                        return;
                    }
                    MethodInvocation earlierCall = mIds.get(id);
                    if (!isReachableFrom(method, earlierCall, call)) {
                        return;
                    }
                    Location location = context.getLocation(call);
                    Location secondary = context.getLocation(earlierCall);
                    secondary.setMessage("First usage here");
                    location.setSecondary(secondary);
                    context.report(ISSUE, call, location, String.format(
                        "The id `%1$s` has already been looked up in this method; possible " +
                        "cut & paste error?", first.toString()));
                } else {
                    mIds.put(id, call);
                    mLhs.put(id, lhs);
                    mCallOperands.put(id, callOperand);
                }
            }
        }
    }
}
 
Example 8
Source File: SharedPrefsDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean visitMethodInvocation(MethodInvocation node) {
    if (node == mTarget) {
        mSeenTarget = true;
    } else if (mAllowCommitBeforeTarget || mSeenTarget || node.astOperand() == mTarget) {
        String name = node.astName().astValue();
        boolean isCommit = "commit".equals(name);
        if (isCommit || "apply".equals(name)) { //$NON-NLS-1$ //$NON-NLS-2$
            // TODO: Do more flow analysis to see whether we're really calling commit/apply
            // on the right type of object?
            mFound = true;

            ResolvedNode resolved = mContext.resolve(node);
            if (resolved instanceof JavaParser.ResolvedMethod) {
                ResolvedMethod method = (ResolvedMethod) resolved;
                JavaParser.ResolvedClass clz = method.getContainingClass();
                if (clz.isSubclassOf("android.content.SharedPreferences.Editor", false)
                        && mContext.getProject().getMinSdkVersion().getApiLevel() >= 9) {
                    // See if the return value is read: can only replace commit with
                    // apply if the return value is not considered
                    Node parent = node.getParent();
                    boolean returnValueIgnored = false;
                    if (parent instanceof MethodDeclaration ||
                            parent instanceof ConstructorDeclaration ||
                            parent instanceof ClassDeclaration ||
                            parent instanceof Block ||
                            parent instanceof ExpressionStatement) {
                        returnValueIgnored = true;
                    } else if (parent instanceof Statement) {
                        if (parent instanceof If) {
                            returnValueIgnored = ((If) parent).astCondition() != node;
                        } else if (parent instanceof Return) {
                            returnValueIgnored = false;
                        } else if (parent instanceof VariableDeclaration) {
                            returnValueIgnored = false;
                        } else if (parent instanceof For) {
                            returnValueIgnored = ((For) parent).astCondition() != node;
                        } else if (parent instanceof While) {
                            returnValueIgnored = ((While) parent).astCondition() != node;
                        } else if (parent instanceof DoWhile) {
                            returnValueIgnored = ((DoWhile) parent).astCondition() != node;
                        } else if (parent instanceof Case) {
                            returnValueIgnored = ((Case) parent).astCondition() != node;
                        } else if (parent instanceof Assert) {
                            returnValueIgnored = ((Assert) parent).astAssertion() != node;
                        } else {
                            returnValueIgnored = true;
                        }
                    }
                    if (returnValueIgnored && isCommit) {
                        String message = "Consider using `apply()` instead; `commit` writes "
                                + "its data to persistent storage immediately, whereas "
                                + "`apply` will handle it in the background";
                        mContext.report(ISSUE, node, mContext.getLocation(node), message);
                    }
                }
            }
        }
    }

    return super.visitMethodInvocation(node);
}
 
Example 9
Source File: CleanupDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private static void checkRecycled(@NonNull final JavaContext context, @NonNull Node node,
        @NonNull final String recycleType, @NonNull final String recycleName) {
    ResolvedVariable boundVariable = getVariable(context, node);
    if (boundVariable == null) {
        return;
    }

    Node method = JavaContext.findSurroundingMethod(node);
    if (method == null) {
        return;
    }

    FinishVisitor visitor = new FinishVisitor(context, boundVariable) {
        @Override
        protected boolean isCleanupCall(@NonNull MethodInvocation call) {
            String methodName = call.astName().astValue();
            if (!recycleName.equals(methodName)) {
                return false;
            }
            ResolvedNode resolved = mContext.resolve(call);
            if (resolved instanceof ResolvedMethod) {
                ResolvedClass containingClass = ((ResolvedMethod) resolved).getContainingClass();
                if (containingClass.isSubclassOf(recycleType, false)) {
                    // Yes, called the right recycle() method; now make sure
                    // we're calling it on the right variable
                    Expression operand = call.astOperand();
                    if (operand != null) {
                        resolved = mContext.resolve(operand);
                        //noinspection SuspiciousMethodCalls
                        if (resolved != null && mVariables.contains(resolved)) {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    };

    method.accept(visitor);
    if (visitor.isCleanedUp() || visitor.variableEscapes()) {
        return;
    }

    String className = recycleType.substring(recycleType.lastIndexOf('.') + 1);
    String message;
    if (RECYCLE.equals(recycleName)) {
        message = String.format(
                "This `%1$s` should be recycled after use with `#recycle()`", className);
    } else {
        message = String.format(
                "This `%1$s` should be freed up after use with `#%2$s()`", className,
                recycleName);
    }
    Node locationNode = node instanceof MethodInvocation ?
            ((MethodInvocation) node).astName() : node;
    Location location = context.getLocation(locationNode);
    context.report(RECYCLE_RESOURCE, node, location, message);
}