0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 03:52:15 +02:00

refactor: convert DirectSnackbarMakeUsage to Kotlin

com.ichi2.anki.lint.rules.DirectSnackbarMakeUsage

Moved the companion object to the top of the class to keep the diff clean
This commit is contained in:
David Allison 2022-04-29 10:46:53 +01:00 committed by Mike Hardy
parent d84360ccbc
commit 6d42573189
2 changed files with 42 additions and 61 deletions

View File

@ -43,7 +43,7 @@ permission notice:
// Example of class name: "/com/ichi2/anki/UIUtils.kt"
// Ensure that it starts with '/' (slash)
def source = Source.MAIN
def className = "/com/ichi2/anki/lint/rules/DirectSnackbarMakeUsage.kt"
def className = ""
enum Source {
MAIN("/src/main/java"),

View File

@ -13,48 +13,38 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.anki.lint.rules
package com.ichi2.anki.lint.rules;
import com.android.tools.lint.client.api.JavaEvaluator;
import com.android.tools.lint.detector.api.Detector;
import com.android.tools.lint.detector.api.Implementation;
import com.android.tools.lint.detector.api.Issue;
import com.android.tools.lint.detector.api.JavaContext;
import com.android.tools.lint.detector.api.Scope;
import com.android.tools.lint.detector.api.SourceCodeScanner;
import com.google.common.annotations.VisibleForTesting;
import com.ichi2.anki.lint.utils.Constants;
import com.ichi2.anki.lint.utils.LintUtils;
import com.intellij.psi.PsiMethod;
import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import org.jetbrains.uast.UCallExpression;
import org.jetbrains.uast.UClass;
import java.util.ArrayList;
import java.util.List;
import com.android.tools.lint.detector.api.*
import com.google.common.annotations.VisibleForTesting
import com.ichi2.anki.lint.utils.Constants
import com.ichi2.anki.lint.utils.KotlinCleanup
import com.ichi2.anki.lint.utils.LintUtils
import com.intellij.psi.PsiMethod
import org.jetbrains.uast.UCallExpression
/**
* This custom Lint rules will raise an error if a developer uses the {com.google.android.material.snackbar.Snackbar#make(...)} method
* instead of using the method provided by the UIUtils class {com.ichi2.anki.UIUtils#showSimpleSnackbar(...)}
* or {com.ichi2.anki.UIUtils#showSnackbar(...)}.
*/
public class DirectSnackbarMakeUsage extends Detector implements SourceCodeScanner {
@KotlinCleanup("IDE lint")
@KotlinCleanup("mutableListOf")
class DirectSnackbarMakeUsage : Detector(), SourceCodeScanner {
@VisibleForTesting
static final String ID = "DirectSnackbarMakeUsage";
companion object {
@JvmField
@VisibleForTesting
val ID = "DirectSnackbarMakeUsage"
@VisibleForTesting
static final String DESCRIPTION = "Use UIUtils.showSimpleSnackbar or UIUtils.showSnackbar instead of Snackbar.make";
private static final String EXPLANATION = "To improve code consistency within the codebase you should use UIUtils.showSimpleSnackbar or UIUtils.showSnackbar " +
"in place of the library Snackbar.make(...).show()";
private static final Implementation implementation = new Implementation(DirectSnackbarMakeUsage.class, Scope.JAVA_FILE_SCOPE);
public static final Issue ISSUE = Issue.create(
@JvmField
@VisibleForTesting
val DESCRIPTION = "Use UIUtils.showSimpleSnackbar or UIUtils.showSnackbar instead of Snackbar.make"
private const val EXPLANATION = "To improve code consistency within the codebase you should use UIUtils.showSimpleSnackbar or UIUtils.showSnackbar " +
"in place of the library Snackbar.make(...).show()"
private val implementation = Implementation(DirectSnackbarMakeUsage::class.java, Scope.JAVA_FILE_SCOPE)
@JvmField
val ISSUE: Issue = Issue.create(
ID,
DESCRIPTION,
EXPLANATION,
@ -62,37 +52,28 @@ public class DirectSnackbarMakeUsage extends Detector implements SourceCodeScann
Constants.ANKI_CODE_STYLE_PRIORITY,
Constants.ANKI_CODE_STYLE_SEVERITY,
implementation
);
public DirectSnackbarMakeUsage() {
)
}
@Nullable
@Override
public List<String> getApplicableMethodNames() {
List<String> forbiddenMethods = new ArrayList<>();
forbiddenMethods.add("make");
return forbiddenMethods;
override fun getApplicableMethodNames(): List<String>? {
val forbiddenMethods: MutableList<String> = ArrayList()
forbiddenMethods.add("make")
return forbiddenMethods
}
@Override
public void visitMethodCall(@NonNull JavaContext context, @NonNull UCallExpression node, @NonNull PsiMethod method) {
super.visitMethodCall(context, node, method);
JavaEvaluator evaluator = context.getEvaluator();
List<UClass> foundClasses = context.getUastFile().getClasses();
if (!LintUtils.isAnAllowedClass(foundClasses, "UIUtils")
&& evaluator.isMemberInClass(method, "com.google.android.material.snackbar.Snackbar")) {
override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {
super.visitMethodCall(context, node, method)
val evaluator = context.evaluator
val foundClasses = context.uastFile!!.classes
if (!LintUtils.isAnAllowedClass(foundClasses, "UIUtils") &&
evaluator.isMemberInClass(method, "com.google.android.material.snackbar.Snackbar")
) {
context.report(
ISSUE,
node,
context.getCallLocation(node, true, true),
DESCRIPTION
);
ISSUE,
node,
context.getCallLocation(node, true, true),
DESCRIPTION
)
}
}
}
}