0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-19 19:42:17 +02:00

Allow dates created with long time

This commit is contained in:
lukstbit 2020-08-29 08:50:32 +03:00 committed by Mike Hardy
parent 018c1db71f
commit 1eea5d7126
3 changed files with 52 additions and 11 deletions

View File

@ -1,14 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- We use this file bacause we want to enable for the release build the NewApi, InlinedApi and
our Anki Time related checks. Using the check() method on lintOptions would be a better option
but it doesn't work as it will NOT recognize any custom Lint checks(from local module or
dependency library) .
To add more checks for the release build you will need to modify this file to change the
severity of that check to fatal.
Also, note that any new issues will appear when linting as this is not an exclusive list of
issues to use in linting, it's a file to change issues on top of other default checks.
If a new issue was created by the framework or other libraries which should be disabled for
release then add it to this file like this:
<!-- INFORMATION ABOUT USAGE
We use this file bacause we want to enable for the release build various checks from the
Android SDK(initially just NewApi and InlinedApi) plus our own custom Anki time usage related
checks. Using the check() method on lintOptions in the lint.gradle file would be nice but it
doesn't work as it will NOT recognize any custom Lint checks(from a local module(like lint-rules)
or from a library added as a dependency) .
To allow more checks for the release build you will need to modify this file to change the
severity of that check to fatal. For example, to enable for release the WrongViewCast check
change:
<issue id="WrongViewCast" severity="fatal" />
Also, note that any NEW issue will appear when linting as this is not an exclusive list of
issues to use in linting, it's a file used to declare how the issues are applied to the project.
If a NEW issue is created by the Android framework or added by other libraries which should be
disabled for release then add it to this file like this:
<issue id="NewIssueId" severity="ignore"/>
-->

View File

@ -5,17 +5,18 @@ 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.Severity;
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.intellij.psi.PsiType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.uast.UCallExpression;
import org.jetbrains.uast.UClass;
import org.jetbrains.uast.UExpression;
import java.util.ArrayList;
import java.util.List;
@ -61,6 +62,14 @@ public class DirectDateInstantiation extends Detector implements SourceCodeScann
public void visitConstructor(@NotNull JavaContext context, @NotNull UCallExpression node, @NotNull PsiMethod constructor) {
super.visitConstructor(context, node, constructor);
List<UClass> foundClasses = context.getUastFile().getClasses();
// this checks for usage of new Date(ms) which we allow
List<UExpression> argTypes = node.getValueArguments();
if (argTypes != null && argTypes.size() == 1) {
UExpression onlyArgument = argTypes.get(0);
if (onlyArgument != null && onlyArgument.getExpressionType().equalsToText("long")) {
return;
}
}
if (!LintUtils.isAnAllowedClass(foundClasses, "Time")) {
context.report(
ISSUE,

View File

@ -15,6 +15,9 @@ public class DirectDateInstantiationTest {
" public Date() { \n" +
" \n" +
" } \n" +
" public Date(long time) { \n" +
" \n" +
" } \n" +
"} \n";
private final String javaFileToBeTested = " \n" +
@ -39,6 +42,17 @@ public class DirectDateInstantiationTest {
" Date d = new Date(); \n" +
" } \n" +
"} \n";
private final String javaFileUsingDateWithLong = " \n" +
"package com.ichi2.anki.lint.rules; \n" +
" \n" +
"import java.util.Date; \n" +
" \n" +
"public class TestJavaClass { \n" +
" \n" +
" public static void main(String[] args) { \n" +
" Date d = new Date(1L); \n" +
" } \n" +
"} \n";
@Test
@ -66,4 +80,15 @@ public class DirectDateInstantiationTest {
.run()
.expectClean();
}
@Test
public void allowsUsageWithLongValue() {
lint().
allowMissingSdk().
allowCompilationErrors()
.files(create(stubDate), create(javaFileUsingDateWithLong))
.issues(DirectDateInstantiation.ISSUE)
.run()
.expectClean();
}
}