@Override public boolean isInPackage(@NonNull String pkg, boolean includeSubPackages) { String packageName = getContainingClass().getPackageName(); //noinspection SimplifiableIfStatement if (pkg.equals(packageName)) { return true; } return includeSubPackages && packageName.length() > pkg.length() && packageName.charAt(pkg.length()) == '.' && packageName.startsWith(pkg); } }
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); }
private static boolean isAddReplaceOpen(@NonNull JavaContext context, @NonNull MethodInvocation node) { String methodName = node.astName().astValue(); if (ADD_TRANSACTION.equals(methodName) || OPEN_TRANSACTION.equals(methodName) || REPLACE_TRANSACTION.equals(methodName)) { 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(ACTIVITY_MANAGER, false)) { return true; } } } return false; }
JavaParser.ResolvedClass clzz = method.getContainingClass(); boolean isSubClass = false;
if (!method.getContainingClass().isSubclassOf(CIPHER, false)) { return;
@Override public boolean visitConstructorInvocation(ConstructorInvocation node) { if (mVisitConstructors) { TypeReference typeReference = node.astTypeReference(); if (typeReference != null) { TypeReferencePart last = typeReference.astParts().last(); if (last != null) { String name = last.astIdentifier().astValue(); if (mConstructorSimpleNames.contains(name)) { ResolvedNode resolved = mContext.resolve(node); if (resolved instanceof ResolvedMethod) { ResolvedMethod method = (ResolvedMethod) resolved; String type = method.getContainingClass().getName(); List<VisitingDetector> list = mConstructorDetectors.get(type); if (list != null) { for (VisitingDetector v : list) { v.getJavaScanner().visitConstructor(mContext, v.getVisitor(), node, method); } } } } } } } return super.visitConstructorInvocation(node); } }
if (!method.getContainingClass().matches("android.database.sqlite.SQLiteDatabase")) { return;
@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; } };
@Override public void visitMethod(@NonNull JavaContext context, @Nullable AstVisitor visitor, @NonNull MethodInvocation node) { // Ignore the issue if we never build for any API less than 17. if (context.getMainProject().getMinSdk() >= 17) { return; } // Ignore if the method doesn't fit our description. ResolvedNode resolved = context.resolve(node); if (!(resolved instanceof ResolvedMethod)) { return; } ResolvedMethod method = (ResolvedMethod) resolved; if (!method.getContainingClass().isSubclassOf(WEB_VIEW, false)) { return; } if (method.getArgumentCount() != 2 || !method.getArgumentType(0).matchesName(TYPE_OBJECT) || !method.getArgumentType(1).matchesName(TYPE_STRING)) { return; } String message = "`WebView.addJavascriptInterface` should not be called with minSdkVersion < 17 for security reasons: " + "JavaScript can use reflection to manipulate application"; context.report(ISSUE, node, context.getLocation(node.astName()), message); } }
/** * 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 * @deprecated use {@link JavaEvaluator#isMemberInSubClassOf(PsiMember, String, boolean)} instead */ @Deprecated 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; }
private static boolean isAppBarActivityCall(@NonNull JavaContext context, @NonNull MethodInvocation node) { ResolvedNode resolved = context.resolve(node); if (resolved instanceof ResolvedMethod) { ResolvedMethod method = (ResolvedMethod) resolved; ResolvedClass containingClass = method.getContainingClass(); if (containingClass.isSubclassOf(CLASS_ACTIVITY, false)) { // Make sure that the calling context is a subclass of ActionBarActivity; // we don't want to flag these calls if they are in non-appcompat activities // such as PreferenceActivity (see b.android.com/58512) ClassDeclaration surroundingClass = JavaContext.findSurroundingClass(node); if (surroundingClass != null) { ResolvedNode clz = context.resolve(surroundingClass); return clz instanceof ResolvedClass && ((ResolvedClass)clz).isSubclassOf( "android.support.v7.app.ActionBarActivity", false); } } } return false; }
@NonNull private static PermissionHolder addLocalPermissions( @NonNull JavaContext context, @NonNull PermissionHolder permissions, @NonNull Node node) { // Accumulate @RequirePermissions available in the local context Node methodNode = JavaContext.findSurroundingMethod(node); if (methodNode == null) { return permissions; } ResolvedNode resolved = context.resolve(methodNode); if (!(resolved instanceof ResolvedMethod)) { return permissions; } ResolvedMethod method = (ResolvedMethod) resolved; ResolvedAnnotation annotation = method.getAnnotation(PERMISSION_ANNOTATION); permissions = mergeAnnotationPermissions(context, permissions, annotation); annotation = method.getContainingClass().getAnnotation(PERMISSION_ANNOTATION); permissions = mergeAnnotationPermissions(context, permissions, annotation); return permissions; }
@Override public void visitMethod(@NonNull JavaContext context, @Nullable AstVisitor visitor, @NonNull MethodInvocation node) { ResolvedNode resolved = context.resolve(node); if (!(resolved instanceof ResolvedMethod) || !((ResolvedMethod) resolved).getContainingClass() .isSubclassOf(PACKAGE_MANAGER_CLASS, false)) { return; } StrictListAccessor<Expression, MethodInvocation> argumentList = node.astArguments(); // Ignore if the method doesn't fit our description. if (argumentList != null && argumentList.size() == 2) { TypeDescriptor firstParameterType = context.getType(argumentList.first()); if (firstParameterType != null && firstParameterType.matchesSignature(JavaParser.TYPE_STRING)) { maybeReportIssue(calculateValue(context, argumentList.last()), context, node); } } }
private static boolean isBeginTransaction(@NonNull JavaContext context, @NonNull MethodInvocation node) { String methodName = node.astName().astValue(); assert methodName.equals(BEGIN_TRANSACTION) : methodName; if (BEGIN_TRANSACTION.equals(methodName)) { ResolvedNode resolved = context.resolve(node); if (resolved instanceof ResolvedMethod) { ResolvedMethod method = (ResolvedMethod) resolved; ResolvedClass containingClass = method.getContainingClass(); if (containingClass.isSubclassOf(FRAGMENT_MANAGER_CLS, false) || containingClass.isSubclassOf(FRAGMENT_MANAGER_V4_CLS, false)) { return true; } } } return false; }
@Override public void afterVisitConstructorInvocation(@NonNull ConstructorInvocation node) { ResolvedNode resolved = mContext.resolve(node); if (resolved instanceof ResolvedMethod) { ResolvedMethod method = (ResolvedMethod) resolved; mTypes.put(node, method.getContainingClass()); } else { // Implicit constructor? TypeDescriptor type = mContext.getType(node); if (type != null) { ResolvedClass typeClass = type.getTypeClass(); if (typeClass != null) { mTypes.put(node, typeClass); } } } }
@Override public void visitMethod(@NonNull JavaContext context, @Nullable AstVisitor visitor, @NonNull MethodInvocation node) { ResolvedNode resolved = context.resolve(node); if (resolved instanceof ResolvedMethod) { ResolvedMethod method = (ResolvedMethod) resolved; if (method.getContainingClass().matches("android.app.AlarmManager") && method.getArgumentCount() == 4) { ensureAtLeast(context, node, 1, 5000L); ensureAtLeast(context, node, 2, 60000L); } } }
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 }
private static boolean isMethodOnFragmentClass( @NonNull JavaContext context, @NonNull MethodInvocation call, @NonNull String fragmentClass, @NonNull String v4FragmentClass) { ResolvedNode resolved = context.resolve(call); if (resolved instanceof ResolvedMethod) { ResolvedClass containingClass = ((ResolvedMethod) resolved).getContainingClass(); return containingClass.isSubclassOf(fragmentClass, false) || containingClass.isSubclassOf(v4FragmentClass, false); } return false; }
private static boolean isCallOnWebView(JavaContext context, MethodInvocation call) { ResolvedNode resolved = context.resolve(call); if (!(resolved instanceof ResolvedMethod)) { return false; } ResolvedMethod method = (ResolvedMethod) resolved; return method.getContainingClass().matches(WEB_VIEW_CLS); }
@Override public void visitConstructor(@NonNull JavaContext context, @Nullable AstVisitor visitor, @NonNull ConstructorInvocation node, @NonNull ResolvedMethod constructor) { checkRecycled(context, node, constructor.getContainingClass().getSignature(), RELEASE); }