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
}
}
use of com.android.tools.klint.client.api.JavaEvaluator in project kotlin by JetBrains.
the class GetSignaturesDetector 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, PACKAGE_MANAGER_CLASS, true, JavaParser.TYPE_STRING, JavaParser.TYPE_INT)) {
return;
}
List<UExpression> arguments = node.getValueArguments();
UExpression second = arguments.get(1);
Object number = ConstantEvaluator.evaluate(context, second);
if (number instanceof Number) {
int flagValue = ((Number) number).intValue();
maybeReportIssue(flagValue, context, node, second);
}
}
use of com.android.tools.klint.client.api.JavaEvaluator in project kotlin by JetBrains.
the class TrustAllX509TrustManagerDetector method checkMethod.
private static void checkMethod(@NonNull JavaContext context, @NonNull UClass cls, @NonNull String methodName) {
JavaEvaluator evaluator = context.getEvaluator();
for (PsiMethod method : cls.findMethodsByName(methodName, true)) {
if (evaluator.isAbstract(method)) {
continue;
}
// For now very simple; only checks if nothing is done.
// Future work: Improve this check to be less sensitive to irrelevant
// instructions/statements/invocations (e.g. System.out.println) by
// looking for calls that could lead to a CertificateException being
// thrown, e.g. throw statement within the method itself or invocation
// of another method that may throw a CertificateException, and only
// reporting an issue if none of these calls are found. ControlFlowGraph
// may be useful here.
UExpression body = context.getUastContext().getMethodBody(method);
ComplexBodyVisitor visitor = new ComplexBodyVisitor();
body.accept(visitor);
if (!visitor.isComplex()) {
Location location = context.getNameLocation(method);
String message = getErrorMessage(methodName);
context.report(ISSUE, method, location, message);
}
}
}
use of com.android.tools.klint.client.api.JavaEvaluator in project kotlin by JetBrains.
the class UnsafeBroadcastReceiverDetector method checkClass.
@Override
public void checkClass(@NonNull JavaContext context, @NonNull UClass declaration) {
String name = declaration.getName();
if (name == null) {
// anonymous classes can't be the ones referenced in the manifest
return;
}
String qualifiedName = declaration.getQualifiedName();
if (qualifiedName == null) {
return;
}
if (!mReceiversWithProtectedBroadcastIntentFilter.contains(qualifiedName)) {
return;
}
JavaEvaluator evaluator = context.getEvaluator();
for (PsiMethod method : declaration.findMethodsByName("onReceive", false)) {
if (evaluator.parametersMatch(method, CLASS_CONTEXT, CLASS_INTENT)) {
checkOnReceive(context, method);
}
}
}
use of com.android.tools.klint.client.api.JavaEvaluator in project kotlin by JetBrains.
the class LogDetector method visitMethod.
@Override
public void visitMethod(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UCallExpression node, @NonNull UMethod method) {
JavaEvaluator evaluator = context.getEvaluator();
if (!JavaEvaluator.isMemberInClass(method, LOG_CLS)) {
return;
}
String name = method.getName();
boolean withinConditional = IS_LOGGABLE.equals(name) || checkWithinConditional(context, node.getUastParent(), node);
// log methods (info, verbose, etc))
if (("i".equals(name) || "d".equals(name) || "v".equals(name) || PRINTLN.equals(name)) && !withinConditional && performsWork(node) && context.isEnabled(CONDITIONAL)) {
String message = String.format("The log call Log.%1$s(...) should be " + "conditional: surround with `if (Log.isLoggable(...))` or " + "`if (BuildConfig.DEBUG) { ... }`", name);
context.report(CONDITIONAL, node, context.getUastLocation(node), message);
}
// Check tag length
if (context.isEnabled(LONG_TAG)) {
int tagArgumentIndex = PRINTLN.equals(name) ? 1 : 0;
PsiParameterList parameterList = method.getParameterList();
List<UExpression> argumentList = node.getValueArguments();
if (evaluator.parameterHasType(method, tagArgumentIndex, TYPE_STRING) && parameterList.getParametersCount() == argumentList.size()) {
UExpression argument = argumentList.get(tagArgumentIndex);
String tag = ConstantEvaluator.evaluateString(context, argument, true);
if (tag != null && tag.length() > 23) {
String message = String.format("The logging tag can be at most 23 characters, was %1$d (%2$s)", tag.length(), tag);
context.report(LONG_TAG, node, context.getUastLocation(node), message);
}
}
}
}
Aggregations