Java Code Examples for com.android.tools.lint.detector.api.JavaContext#resolve()
The following examples show how to use
com.android.tools.lint.detector.api.JavaContext#resolve() .
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: SupportAnnotationDetector.java From javaide with GNU General Public License v3.0 | 6 votes |
@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; }
Example 2
Source File: GetSignaturesDetector.java From javaide with GNU General Public License v3.0 | 6 votes |
@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); } } }
Example 3
Source File: WrongCallDetector.java From javaide with GNU General Public License v3.0 | 6 votes |
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 4
Source File: AppCompatCallDetector.java From javaide with GNU General Public License v3.0 | 6 votes |
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; }
Example 5
Source File: LogDetector.java From MeituanLintDemo with Apache License 2.0 | 6 votes |
@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 6
Source File: AlarmDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
@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); } } }
Example 7
Source File: AddJavascriptInterfaceDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
@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); }
Example 8
Source File: GetSignaturesDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
private static int calculateValue(JavaContext context, Expression expression) { // This function assumes that the only inputs to the expression are static integer // flags that combined via bitwise operands. if (expression instanceof IntegralLiteral) { return ((IntegralLiteral) expression).astIntValue(); } ResolvedNode resolvedNode = context.resolve(expression); if (resolvedNode instanceof ResolvedField) { Object value = ((ResolvedField) resolvedNode).getValue(); if (value instanceof Integer) { return (Integer) value; } } if (expression instanceof BinaryExpression) { BinaryExpression binaryExpression = (BinaryExpression) expression; BinaryOperator operator = binaryExpression.astOperator(); int leftValue = calculateValue(context, binaryExpression.astLeft()); int rightValue = calculateValue(context, binaryExpression.astRight()); if (operator == BinaryOperator.BITWISE_OR) { return leftValue | rightValue; } if (operator == BinaryOperator.BITWISE_AND) { return leftValue & rightValue; } if (operator == BinaryOperator.BITWISE_XOR) { return leftValue ^ rightValue; } } return 0; }
Example 9
Source File: GetSignaturesDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
private static boolean isStringParameter( @NonNull Expression expression, @NonNull JavaContext context) { if (expression instanceof StringLiteral) { return true; } else { ResolvedNode resolvedNode = context.resolve(expression); if (resolvedNode instanceof ResolvedField) { if (((ResolvedField) resolvedNode).getValue() instanceof String) { return true; } } } return false; }
Example 10
Source File: JavaScriptInterfaceDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
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); }
Example 11
Source File: StringFormatDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
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 12
Source File: CleanupDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
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; }
Example 13
Source File: CallSuperDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
@Override public AstVisitor createJavaVisitor(@NonNull final JavaContext context) { return new ForwardingAstVisitor() { @Override public boolean visitMethodDeclaration(MethodDeclaration node) { ResolvedNode resolved = context.resolve(node); if (resolved instanceof ResolvedMethod) { ResolvedMethod method = (ResolvedMethod) resolved; checkCallSuper(context, node, method); } return false; } }; }
Example 14
Source File: SQLiteDetector.java From javaide with GNU General Public License v3.0 | 5 votes |
@Override public void visitMethod(@NonNull JavaContext context, @Nullable AstVisitor visitor, @NonNull MethodInvocation node) { ResolvedNode resolved = context.resolve(node); if (!(resolved instanceof ResolvedMethod)) { return; } ResolvedMethod method = (ResolvedMethod) resolved; if (!method.getContainingClass().matches("android.database.sqlite.SQLiteDatabase")) { return; } // Try to resolve the String and look for STRING keys if (method.getArgumentCount() > 0 && method.getArgumentType(0).matchesSignature(TYPE_STRING) && node.astArguments().size() == method.getArgumentCount()) { Iterator<Expression> iterator = node.astArguments().iterator(); Node argument = iterator.next(); String sql = ConstantEvaluator.evaluateString(context, argument, true); if (sql != null && (sql.startsWith("CREATE TABLE") || sql.startsWith("ALTER TABLE")) && sql.matches(".*\\bSTRING\\b.*")) { String message = "Using column type STRING; did you mean to use TEXT? " + "(STRING is a numeric type and its value can be adjusted; for example," + "strings that look like integers can drop leading zeroes. See issue " + "explanation for details.)"; context.report(ISSUE, node, context.getLocation(node), message); } } }
Example 15
Source File: UnsafeAndroidDetector.java From SafelyAndroid with MIT License | 5 votes |
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 16
Source File: CleanupDetector.java From javaide with GNU General Public License v3.0 | 4 votes |
private static void checkResourceRecycled(@NonNull JavaContext context, @NonNull MethodInvocation node, @NonNull String name) { // Recycle detector ResolvedNode resolved = context.resolve(node); if (!(resolved instanceof ResolvedMethod)) { return; } ResolvedMethod method = (ResolvedMethod) resolved; ResolvedClass containingClass = method.getContainingClass(); if ((OBTAIN.equals(name) || OBTAIN_NO_HISTORY.equals(name)) && containingClass.isSubclassOf(MOTION_EVENT_CLS, false)) { checkRecycled(context, node, MOTION_EVENT_CLS, RECYCLE); } else if (OBTAIN.equals(name) && containingClass.isSubclassOf(PARCEL_CLS, false)) { checkRecycled(context, node, PARCEL_CLS, RECYCLE); } else if (OBTAIN.equals(name) && containingClass.isSubclassOf(VELOCITY_TRACKER_CLS, false)) { checkRecycled(context, node, VELOCITY_TRACKER_CLS, RECYCLE); } else if ((OBTAIN_STYLED_ATTRIBUTES.equals(name) || OBTAIN_ATTRIBUTES.equals(name) || OBTAIN_TYPED_ARRAY.equals(name)) && (containingClass.isSubclassOf(CLASS_CONTEXT, false) || containingClass.isSubclassOf(RESOURCES_CLS, false))) { TypeDescriptor returnType = method.getReturnType(); if (returnType != null && returnType.matchesSignature(TYPED_ARRAY_CLS)) { checkRecycled(context, node, TYPED_ARRAY_CLS, RECYCLE); } } else if (ACQUIRE_CPC.equals(name) && containingClass.isSubclassOf( CONTENT_RESOLVER_CLS, false)) { checkRecycled(context, node, CONTENT_PROVIDER_CLIENT_CLS, RELEASE); } else if ((QUERY.equals(name) || RAW_QUERY.equals(name) || QUERY_WITH_FACTORY.equals(name) || RAW_QUERY_WITH_FACTORY.equals(name)) && (containingClass.isSubclassOf(SQLITE_DATABASE_CLS, false) || containingClass.isSubclassOf(CONTENT_RESOLVER_CLS, false) || containingClass.isSubclassOf(CONTENT_PROVIDER_CLS, false) || containingClass.isSubclassOf(CONTENT_PROVIDER_CLIENT_CLS, false))) { // Other potential cursors-returning methods that should be tracked: // android.app.DownloadManager#query // android.content.ContentProviderClient#query // android.content.ContentResolver#query // android.database.sqlite.SQLiteQueryBuilder#query // android.provider.Browser#getAllBookmarks // android.provider.Browser#getAllVisitedUrls // android.provider.DocumentsProvider#queryChildDocuments // android.provider.DocumentsProvider#qqueryDocument // android.provider.DocumentsProvider#queryRecentDocuments // android.provider.DocumentsProvider#queryRoots // android.provider.DocumentsProvider#querySearchDocuments // android.provider.MediaStore$Images$Media#query // android.widget.FilterQueryProvider#runQuery checkRecycled(context, node, CURSOR_CLS, CLOSE); } }