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

The following examples show how to use lombok.ast.MethodInvocation#getParent() . 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: CutPasteDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
private static String getLhs(@NonNull MethodInvocation call) {
    Node parent = call.getParent();
    if (parent instanceof Cast) {
        parent = parent.getParent();
    }

    if (parent instanceof VariableDefinitionEntry) {
        VariableDefinitionEntry vde = (VariableDefinitionEntry) parent;
        return vde.astName().astValue();
    } else if (parent instanceof BinaryExpression) {
        BinaryExpression be = (BinaryExpression) parent;
        Expression left = be.astLeft();
        if (left instanceof VariableReference || left instanceof Select) {
            return be.astLeft().toString();
        } else if (left instanceof ArrayAccess) {
            ArrayAccess aa = (ArrayAccess) left;
            return aa.astOperand().toString();
        }
    }

    return null;
}
 
Example 2
Source File: CleanupDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private static boolean isCommittedInChainedCalls(@NonNull JavaContext context,
        @NonNull MethodInvocation node) {
    // Look for chained calls since the FragmentManager methods all return "this"
    // to allow constructor chaining, e.g.
    //    getFragmentManager().beginTransaction().addToBackStack("test")
    //            .disallowAddToBackStack().hide(mFragment2).setBreadCrumbShortTitle("test")
    //            .show(mFragment2).setCustomAnimations(0, 0).commit();
    Node parent = node.getParent();
    while (parent instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) parent;
        if (isTransactionCommitMethodCall(context, methodInvocation)
                || isShowFragmentMethodCall(context, methodInvocation)) {
            return true;
        }

        parent = parent.getParent();
    }

    return false;
}
 
Example 3
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 4
Source File: ServiceCastDetector.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 (node.getParent() instanceof Cast) {
        Cast cast = (Cast) node.getParent();
        StrictListAccessor<Expression, MethodInvocation> args = node.astArguments();
        if (args.size() == 1) {
            String name = stripPackage(args.first().toString());
            String expectedClass = getExpectedType(name);
            if (expectedClass != null) {
                String castType = cast.astTypeReference().getTypeName();
                if (castType.indexOf('.') == -1) {
                    expectedClass = stripPackage(expectedClass);
                }
                if (!castType.equals(expectedClass)) {
                    // It's okay to mix and match
                    // android.content.ClipboardManager and android.text.ClipboardManager
                    if (isClipboard(castType) && isClipboard(expectedClass)) {
                        return;
                    }

                    String message = String.format(
                            "Suspicious cast to `%1$s` for a `%2$s`: expected `%3$s`",
                            stripPackage(castType), name, stripPackage(expectedClass));
                    context.report(ISSUE, node, context.getLocation(cast), message);
                }
            }

        }
    }
}
 
Example 5
Source File: UnsafeAndroidDetector.java    From SafelyAndroid with MIT License 5 votes vote down vote up
private boolean isInsideDialogFragment(JavaContext context, MethodInvocation node) {
    Node parent = node.getParent();
    while (parent != null) {
        Object resolvedNode = context.resolve(parent);
        if (resolvedNode instanceof JavaParser.ResolvedMethod) {
            JavaParser.ResolvedMethod method = (JavaParser.ResolvedMethod) resolvedNode;
            if (isDialogFragment(method.getContainingClass())) {
                return true;
            }
        }
        parent = parent.getParent();
    }
    return false;
}
 
Example 6
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);
}