private void checkClassAnnotations(JavaContext context, PsiClass psiClass, UCallExpression node) {
for (PsiField psiField : psiClass.getAllFields()) {
PsiAnnotation[] allAnnotations = context.getEvaluator().getAllAnnotations(psiField, true);
Set<String> annotations = new HashSet<>();
for (PsiAnnotation allAnnotation : allAnnotations) {
annotations.add(allAnnotation.getQualifiedName());
}
if (!annotations.contains("com.google.gson.annotations.SerializedName")) {
context.report(ISSUE, context.getLocation(psiField),
"The field \"" + psiField.getName() + "\" is not annotated with SerializedName: **Not annotated**");
}
}
}
context.report(ISSUE, context.getLocation(node),
"The field \"" + psiField.getName() + "\" is not annotated with SerializedName: **Not annotated**");
private PsiClass resolveType(JavaContext javaContext, PsiType type) {
if (type == null) {
return null;
}
if (isGeneric(type)) {
return resolveGeneric(javaContext, type);
} else {
return javaContext.getEvaluator().findClass(type.getCanonicalText());
}
}
The PsiClass comes from javaContext.getEvaluator().findClass(className). I couldn't find any method returning a Uclass from a String:private PsiClass resolveType(JavaContext javaContext, PsiType type) {
if (type == null) {
return null;
}
if (isGeneric(type)) {
return resolveGeneric(javaContext, type);
} else {
return javaContext.getEvaluator().findClass(type.getCanonicalText());
}
}This code seems to work since I'm getting the right class. Also, the weird thing is that when I execute ./gradlew lint the HTML report is correct, but only for java. But findClass is also returning Kotlin classes
package com.example.lint.library;
public class TestJava {
}
package com.example.lint.library;
public class TestJava2 {
String lint = "This is a lint";
}
PsiClass found = context.getEvaluator().findClass("com.example.lint.library.TestJava");
if (string.contains("lint") && string.matches(".*\\blint\\b.*")) {
context.report(ISSUE, found, context.getLocation(found),
"This code mentions `lint`: **Congratulations**");
}
Thanks for your answer Tor. Even if I convert the PsiClass to UElement Android studio doesn't highlight the code. I've noticed that there is a mismatch between the HTML report and the Android Studio report. For example, if I take the example in https://github.com/googlesamples/android-custom-lint-rules and add a couple of classes in Java: TestJava and TestJava2 to the package com.example.lint.library:package com.example.lint.library;
public class TestJava {
}package com.example.lint.library;
public class TestJava2 {
String lint = "This is a lint";
}Then I changed the detector to report to TestJava:PsiClass found = context.getEvaluator().findClass("com.example.lint.library.TestJava");
if (string.contains("lint") && string.matches(".*\\blint\\b.*")) {
context.report(ISSUE, found, context.getLocation(found),
"This code mentions `lint`: **Congratulations**");
}
public class NewThreadDetector extends Detector implements Detector.UastScanner {
}
public class NewThreadDetector extends Detector implements Detector.JavaPsiScanner {