use of com.android.tools.klint.client.api.JavaEvaluator in project kotlin by JetBrains.
the class AlarmDetector method visitMethod.
@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression node, @NonNull UMethod method) {
JavaEvaluator evaluator = context.getEvaluator();
if (evaluator.isMemberInClass(method, "android.app.AlarmManager") && evaluator.getParameterCount(method) == 4) {
ensureAtLeast(context, node, 1, 5000L);
ensureAtLeast(context, node, 2, 60000L);
}
}
use of com.android.tools.klint.client.api.JavaEvaluator in project kotlin by JetBrains.
the class AllowAllHostnameVerifierDetector method visitMethod.
@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression node, @NonNull UMethod method) {
JavaEvaluator evaluator = context.getEvaluator();
if (evaluator.methodMatches(method, null, false, "javax.net.ssl.HostnameVerifier")) {
UExpression argument = node.getValueArguments().get(0);
PsiElement resolvedArgument = UastUtils.tryResolve(argument);
if (resolvedArgument instanceof PsiField) {
PsiField field = (PsiField) resolvedArgument;
if ("ALLOW_ALL_HOSTNAME_VERIFIER".equals(field.getName())) {
Location location = context.getUastLocation(argument);
String message = "Using the ALLOW_ALL_HOSTNAME_VERIFIER HostnameVerifier " + "is unsafe because it always returns true, which could cause " + "insecure network traffic due to trusting TLS/SSL server " + "certificates for wrong hostnames";
context.report(ISSUE, argument, location, message);
}
}
}
}
use of com.android.tools.klint.client.api.JavaEvaluator in project kotlin by JetBrains.
the class ViewConstructorDetector method checkClass.
@Override
public void checkClass(@NonNull JavaContext context, @NonNull UClass declaration) {
// Only applies to concrete classes
JavaEvaluator evaluator = context.getEvaluator();
if (evaluator.isAbstract(declaration) || declaration instanceof PsiAnonymousClass) {
// Ignore abstract classes
return;
}
if (declaration.getContainingClass() != null && !evaluator.isStatic(declaration)) {
// anyway since we'd need the outer instance
return;
}
boolean found = false;
for (PsiMethod constructor : declaration.getConstructors()) {
if (isXmlConstructor(evaluator, constructor)) {
found = true;
break;
}
}
if (!found) {
String message = String.format("Custom view `%1$s` is missing constructor used by tools: " + "`(Context)` or `(Context,AttributeSet)` " + "or `(Context,AttributeSet,int)`", declaration.getName());
Location location = context.getUastNameLocation(declaration);
context.reportUast(ISSUE, declaration, location, message);
}
}
use of com.android.tools.klint.client.api.JavaEvaluator in project kotlin by JetBrains.
the class ViewTagDetector method visitMethod.
@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression call, @NonNull UMethod method) {
// http://code.google.com/p/android/issues/detail?id=18273
if (context.getMainProject().getMinSdk() >= 14) {
return;
}
JavaEvaluator evaluator = context.getEvaluator();
if (!evaluator.isMemberInSubClassOf(method, CLASS_VIEW, false)) {
return;
}
List<UExpression> arguments = call.getValueArguments();
if (arguments.size() != 2) {
return;
}
UExpression tagArgument = arguments.get(1);
if (tagArgument == null) {
return;
}
PsiType type = TypeEvaluator.evaluate(context, tagArgument);
if ((!(type instanceof PsiClassType))) {
return;
}
PsiClass typeClass = ((PsiClassType) type).resolve();
if (typeClass == null) {
return;
}
String objectType;
if (InheritanceUtil.isInheritor(typeClass, false, CLASS_VIEW)) {
objectType = "views";
} else if (InheritanceUtil.isInheritor(typeClass, false, CURSOR_CLS)) {
objectType = "cursors";
} else if (typeClass.getName() != null && typeClass.getName().endsWith("ViewHolder")) {
objectType = "view holders";
} else {
return;
}
String message = String.format("Avoid setting %1$s as values for `setTag`: " + "Can lead to memory leaks in versions older than Android 4.0", objectType);
context.report(ISSUE, call, context.getUastLocation(tagArgument), message);
}
use of com.android.tools.klint.client.api.JavaEvaluator in project kotlin by JetBrains.
the class StringFormatDetector method visitMethod.
@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression node, @NonNull UMethod method) {
if (mFormatStrings == null && !context.getClient().supportsProjectResources()) {
return;
}
JavaEvaluator evaluator = context.getEvaluator();
String methodName = method.getName();
if (methodName.equals(FORMAT_METHOD)) {
if (JavaEvaluator.isMemberInClass(method, TYPE_STRING)) {
// Check formatting parameters for
// java.lang.String#format(String format, Object... formatArgs)
// java.lang.String#format(Locale locale, String format, Object... formatArgs)
checkStringFormatCall(context, method, node, method.getParameterList().getParametersCount() == 3);
// TODO: Consider also enforcing
// java.util.Formatter#format(String string, Object... formatArgs)
}
} else {
// an error.
if (method.getParameterList().getParametersCount() < 2) {
return;
}
if (evaluator.isMemberInSubClassOf(method, CLASS_RESOURCES, false) || evaluator.isMemberInSubClassOf(method, CLASS_CONTEXT, false) || evaluator.isMemberInSubClassOf(method, CLASS_FRAGMENT, false) || evaluator.isMemberInSubClassOf(method, CLASS_V4_FRAGMENT, false)) {
checkStringFormatCall(context, method, node, false);
}
// TODO: Consider also looking up
// android.content.res.Resources#getQuantityString(@PluralsRes int id, int quantity,
// Object... formatArgs)
// though this will require being smarter about cross referencing formatting
// strings since we'll need to go via the quantity string definitions
}
}
Aggregations