Java Code Examples for com.android.tools.lint.client.api.JavaParser#ResolvedMethod

The following examples show how to use com.android.tools.lint.client.api.JavaParser#ResolvedMethod . 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
private static void report(JavaContext context, MethodInvocation node) {
    // Make sure the call is on a view
    JavaParser.ResolvedNode resolved = context.resolve(node);
    if (resolved instanceof JavaParser.ResolvedMethod) {
        JavaParser.ResolvedMethod method = (JavaParser.ResolvedMethod) resolved;
        JavaParser.ResolvedClass containingClass = method.getContainingClass();
        if (!containingClass.isSubclassOf(CLASS_VIEW, false)) {
            return;
        }
    }

    String name = node.astName().astValue();
    String suggestion = Character.toLowerCase(name.charAt(2)) + name.substring(3);
    String message = String.format(
            // Keep in sync with {@link #getOldValue} and {@link #getNewValue} below!
            "Suspicious method call; should probably call \"`%1$s`\" rather than \"`%2$s`\"",
            suggestion, name);
    context.report(ISSUE, node, context.getLocation(node.astName()), message);
}
 
Example 2
Source File: LogDetector.java    From MeituanLintDemo with Apache License 2.0 6 votes vote down vote up
@Override
public AstVisitor createJavaVisitor(final JavaContext context) {
    return new ForwardingAstVisitor() {
        @Override
        public boolean visitMethodInvocation(MethodInvocation node) {
            JavaParser.ResolvedNode resolve = context.resolve(node);
            if (resolve instanceof JavaParser.ResolvedMethod) {
                JavaParser.ResolvedMethod method = (JavaParser.ResolvedMethod) resolve;
                // 方法所在的类校验
                JavaParser.ResolvedClass containingClass = method.getContainingClass();
                if (containingClass.matches("android.util.Log")) {
                    context.report(ISSUE, node, context.getLocation(node),
                                   "请使用Ln,避免使用Log");
                    return true;
                }
                if (node.toString().startsWith("System.out.println")) {
                    context.report(ISSUE, node, context.getLocation(node),
                                   "请使用Ln,避免使用System.out.println");
                    return true;
                }
            }
            return super.visitMethodInvocation(node);
        }
    };
}
 
Example 3
Source File: JavaContext.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns true if the given method invocation node corresponds to a call on a
 * {@code android.content.Context}
 *
 * @param node the method call node
 * @return true iff the method call is on a class extending context
 */
public boolean isContextMethod(@NonNull MethodInvocation node) {
    // Method name used in many other contexts where it doesn't have the
    // same semantics; only use this one if we can resolve types
    // and we're certain this is the Context method
    ResolvedNode resolved = resolve(node);
    if (resolved instanceof JavaParser.ResolvedMethod) {
        JavaParser.ResolvedMethod method = (JavaParser.ResolvedMethod) resolved;
        ResolvedClass containingClass = method.getContainingClass();
        if (containingClass.isSubclassOf(CLASS_CONTEXT, false)) {
            return true;
        }
    }
    return false;
}
 
Example 4
Source File: StringFormatDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static boolean isSharedPreferenceGetString(@NonNull JavaContext context,
        @NonNull MethodInvocation call) {
    if (!GET_STRING_METHOD.equals(call.astName().astValue())) {
        return false;
    }

    JavaParser.ResolvedNode resolved = context.resolve(call);
    if (resolved instanceof JavaParser.ResolvedMethod) {
        JavaParser.ResolvedMethod resolvedMethod = (JavaParser.ResolvedMethod) resolved;
        JavaParser.ResolvedClass containingClass = resolvedMethod.getContainingClass();
        return containingClass.isSubclassOf(ANDROID_CONTENT_SHARED_PREFERENCES, false);
    }

    return false; // not certain
}
 
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);
}