trying the android linter for code inspection

This commit is contained in:
minjaesong
2023-05-22 21:33:39 +09:00
parent 8446cb2ecf
commit c554df9b98
20 changed files with 281 additions and 0 deletions

10
.idea/libraries/android_lint.xml generated Normal file
View File

@@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="android-lint">
<CLASSES>
<root url="file://$PROJECT_DIR$/lib/android-lint" />
</CLASSES>
<JAVADOC />
<SOURCES />
<jarDirectory url="file://$PROJECT_DIR$/lib/android-lint" recursive="false" />
</library>
</component>

View File

@@ -16,5 +16,6 @@
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
<orderEntry type="library" name="jetbrains.kotlin.reflect" level="project" />
<orderEntry type="library" name="jetbrains.kotlin.test" level="project" />
<orderEntry type="library" name="android-lint" level="project" />
</component>
</module>

View File

@@ -24,5 +24,6 @@
<orderEntry type="library" name="badlogicgames.gdx.backend.lwjgl3" level="project" />
<orderEntry type="library" name="jetbrains.kotlin.reflect" level="project" />
<orderEntry type="library" name="jetbrains.kotlin.test" level="project" />
<orderEntry type="library" name="android-lint" level="project" />
</component>
</module>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
lib/android-lint/kxml2-2.3.0.jar LFS Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,127 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.android.tools.lint</groupId>
<artifactId>lint</artifactId>
<version>31.0.1</version>
<name>com.android.tools.lint.lint</name>
<description>Lint tools. Both a Command line tool and a library to add lint features to other tools</description>
<url>http://tools.android.com/</url>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<name>The Android Open Source Project</name>
</developer>
</developers>
<dependencies>
<dependency>
<groupId>com.android.tools.lint</groupId>
<artifactId>lint-api</artifactId>
<version>31.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.android.tools.lint</groupId>
<artifactId>lint-checks</artifactId>
<version>31.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.android.tools.external.com-intellij</groupId>
<artifactId>intellij-core</artifactId>
<version>31.0.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.android.tools.external.com-intellij</groupId>
<artifactId>kotlin-compiler</artifactId>
<version>31.0.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.android.tools.external.org-jetbrains</groupId>
<artifactId>uast</artifactId>
<version>31.0.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.android.tools.analytics-library</groupId>
<artifactId>protos</artifactId>
<version>31.0.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.android.tools.analytics-library</groupId>
<artifactId>shared</artifactId>
<version>31.0.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.android.tools.analytics-library</groupId>
<artifactId>tracker</artifactId>
<version>31.0.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.android.tools.build</groupId>
<artifactId>manifest-merger</artifactId>
<version>31.0.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.android.tools</groupId>
<artifactId>common</artifactId>
<version>31.0.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.android.tools.layoutlib</groupId>
<artifactId>layoutlib-api</artifactId>
<version>31.0.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.android.tools</groupId>
<artifactId>sdk-common</artifactId>
<version>31.0.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.android.tools</groupId>
<artifactId>sdklib</artifactId>
<version>31.0.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>net.sf.kxml</groupId>
<artifactId>kxml2</artifactId>
<version>2.3.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>1.7.10</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>1.7.10</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
lib/android-lint/uast-31.0.1.jar LFS Normal file

Binary file not shown.

View File

@@ -0,0 +1,97 @@
package net.torvald.terrarum
import com.android.tools.lint.client.api.IssueRegistry
import com.android.tools.lint.client.api.UElementHandler
import com.android.tools.lint.detector.api.*
import net.torvald.random.HQRNG
import net.torvald.terrarum.gameactors.Actor
import net.torvald.terrarum.gameitems.GameItem
import net.torvald.terrarum.gameworld.BlockLayer
import net.torvald.terrarum.gameworld.WorldTime
import net.torvald.terrarum.savegame.ByteArray64
import net.torvald.terrarum.utils.*
import org.jetbrains.uast.UClass
import org.jetbrains.uast.UField
import org.jetbrains.uast.util.classSetOf
import org.jetbrains.uast.util.isInstanceOf
import java.math.BigInteger
import java.util.*
/**
* https://medium.com/@vanniktech/writing-your-first-lint-check-39ad0e90b9e6
* https://medium.com/mobile-app-development-publication/making-custom-lint-for-kotlin-code-8a6c203bf474
* https://github.com/googlesamples/android-custom-lint-rules
* https://googlesamples.github.io/android-custom-lint-rules/api-guide.html
*
* Created by minjaesong on 2023-05-22.
*/
class UnserialisableTypeIssueRegistry : IssueRegistry() {
override val issues = listOf(ISSUE_SAVEGAME_UNSERIALISABLE_TYPE_USED_WITHOUT_TRANSIENT)
}
val ISSUE_SAVEGAME_UNSERIALISABLE_TYPE_USED_WITHOUT_TRANSIENT = Issue.create("TerrarumNonTransientUnserialisableType",
"Unserialisable Type Used Without Care",
"Unserialisable type is used on the potentially serialised class without @Transient annotation",
Category.CORRECTNESS,
9,
Severity.ERROR,
Implementation(TerrarumNonTransientUnserialisableType::class.java, EnumSet.of(Scope.JAVA_FILE))
)
class TerrarumNonTransientUnserialisableType : Detector(), Detector.UastScanner {
override fun getApplicablePsiTypes() = listOf(UClass::class.java)
override fun createUastHandler(context: JavaContext) = TerrarumNonTransientUnserialisableTypeHandler(context)
class TerrarumNonTransientUnserialisableTypeHandler(private val context: JavaContext) : UElementHandler() {
override fun visitClass(clazz: UClass) {
/*if (clazz.name?.isDefinedCamelCase() == false) {
context.report(ISSUE_SAVEGAME_UNSERIALISABLE_TYPE_USED_WITHOUT_TRANSIENT, clazz,
context.getNameLocation(clazz),
"Not named in defined camel case.")
}*/
}
override fun visitField(node: UField) {
if (node.uastParent.isInstanceOf(classSetOf(Actor::class.java, GameItem::class.java)) &&
!node.hasAnnotation("Transient") &&
!node.isInstanceOf(classSetOf(
// primitives
String::class.java,
Array<String>::class.java,
Boolean::class.java,
Byte::class.java,
ByteArray::class.java,
ByteArray64::class.java,
Short::class.java,
ShortArray::class.java,
Int::class.java,
IntArray::class.java,
Long::class.java,
LongArray::class.java,
Float::class.java,
FloatArray::class.java,
Double::class.java,
DoubleArray::class.java,
// has serialiser on net.torvald.terrarum.serialise.Common
BigInteger::class.java,
ZipCodedStr::class.java,
BlockLayer::class.java,
WorldTime::class.java,
HashArray::class.java,
HashedWirings::class.java,
HashedWiringGraph::class.java,
WiringGraphMap::class.java,
UUID::class.java,
HQRNG::class.java,
))) {
context.report(
ISSUE_SAVEGAME_UNSERIALISABLE_TYPE_USED_WITHOUT_TRANSIENT,
node,
context.getNameLocation(node),
"Unserialisable type is used on the potentially serialised class without @Transient annotation"
)
}
}
}
}