Kapt: Allow to ignore specific methods/fields from being put into kapt stubs
Classes can't be ignored yet as it requires to ignore also all type usages which is a non-trivial operation.
This commit is contained in:
@@ -14,9 +14,11 @@ dependencies {
|
||||
testCompile(project(":compiler:tests-common"))
|
||||
testCompile(projectTests(":compiler:tests-common"))
|
||||
testCompile(ideaSdkDeps("idea", "idea_rt", "openapi"))
|
||||
testCompile(commonDep("junit:junit"))
|
||||
|
||||
|
||||
compileOnly(project(":kotlin-annotation-processing-runtime"))
|
||||
|
||||
testCompile(commonDep("junit:junit"))
|
||||
testCompile(project(":kotlin-annotation-processing-runtime"))
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
|
||||
+8
-1
@@ -22,6 +22,7 @@ import com.sun.tools.javac.parser.Tokens
|
||||
import com.sun.tools.javac.tree.JCTree
|
||||
import com.sun.tools.javac.tree.JCTree.*
|
||||
import com.sun.tools.javac.tree.TreeMaker
|
||||
import kotlinx.kapt.KaptIgnored
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
|
||||
@@ -361,7 +362,7 @@ class ClassFileToSourceStubConverter(
|
||||
packageFqName: String,
|
||||
explicitInitializer: JCExpression? = null
|
||||
): JCVariableDecl? {
|
||||
if (isSynthetic(field.access)) return null
|
||||
if (isSynthetic(field.access) || isIgnored(field.invisibleAnnotations)) return null
|
||||
// not needed anymore
|
||||
val origin = kaptContext.origins[field]
|
||||
val descriptor = origin?.descriptor
|
||||
@@ -398,6 +399,7 @@ class ClassFileToSourceStubConverter(
|
||||
}
|
||||
|
||||
private fun convertMethod(method: MethodNode, containingClass: ClassNode, packageFqName: String): JCMethodDecl? {
|
||||
if (isIgnored(method.invisibleAnnotations)) return null
|
||||
val descriptor = kaptContext.origins[method]?.descriptor as? CallableDescriptor ?: return null
|
||||
|
||||
val isAnnotationHolderForProperty = descriptor is PropertyDescriptor && isSynthetic(method.access)
|
||||
@@ -496,6 +498,11 @@ class ClassFileToSourceStubConverter(
|
||||
body, defaultValue)
|
||||
}
|
||||
|
||||
private fun isIgnored(annotations: List<AnnotationNode>?): Boolean {
|
||||
val kaptIgnoredAnnotationFqName = KaptIgnored::class.java.name
|
||||
return annotations?.any { Type.getType(it.desc).className == kaptIgnoredAnnotationFqName } ?: false
|
||||
}
|
||||
|
||||
private fun extractMethodSignatureTypes(
|
||||
descriptor: CallableDescriptor,
|
||||
exceptionTypes: JavacList<JCExpression>,
|
||||
|
||||
+10
@@ -23,6 +23,8 @@ import com.sun.tools.javac.util.JCDiagnostic
|
||||
import com.sun.tools.javac.util.Log
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
|
||||
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot
|
||||
import org.jetbrains.kotlin.codegen.CodegenTestCase
|
||||
import org.jetbrains.kotlin.codegen.GenerationUtils
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState
|
||||
@@ -53,6 +55,7 @@ abstract class AbstractKotlinKapt3Test : CodegenTestCase() {
|
||||
val javaSources = javaFilesDir?.let { arrayOf(it) } ?: emptyArray()
|
||||
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.ALL, *javaSources)
|
||||
addAnnotationProcessingRuntimeLibrary(myEnvironment)
|
||||
|
||||
// Use light analysis mode in tests
|
||||
AnalysisHandlerExtension.registerExtension(myEnvironment.project, PartialAnalysisHandlerExtension())
|
||||
@@ -164,3 +167,10 @@ abstract class AbstractKotlinKaptContextTest : AbstractKotlinKapt3Test() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun addAnnotationProcessingRuntimeLibrary(environment: KotlinCoreEnvironment) {
|
||||
environment.apply {
|
||||
val runtimeLibrary = File(PathUtil.kotlinPathsForCompiler.libPath, "kotlin-annotation-processing-runtime.jar")
|
||||
updateClasspath(listOf(JvmClasspathRoot(runtimeLibrary)))
|
||||
}
|
||||
}
|
||||
+6
@@ -102,6 +102,12 @@ public class ClassFileToSourceStubConverterTestGenerated extends AbstractClassFi
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("ignoredMembers.kt")
|
||||
public void testIgnoredMembers() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/kapt3/kapt3-compiler/testData/converter/ignoredMembers.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("importsForErrorTypes.kt")
|
||||
public void testImportsForErrorTypes() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("plugins/kapt3/kapt3-compiler/testData/converter/importsForErrorTypes.kt");
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import kotlinx.kapt.*
|
||||
|
||||
class Test {
|
||||
@KaptIgnored
|
||||
fun ignoredFun() {}
|
||||
|
||||
@KaptIgnored @get:KaptIgnored
|
||||
val ignoredProperty: String = ""
|
||||
|
||||
fun nonIgnoredFun() {}
|
||||
|
||||
val nonIgnoredProperty: String = ""
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
@kotlin.Metadata()
|
||||
public final class Test {
|
||||
@org.jetbrains.annotations.NotNull()
|
||||
private final java.lang.String nonIgnoredProperty = "";
|
||||
|
||||
public final void nonIgnoredFun() {
|
||||
}
|
||||
|
||||
@org.jetbrains.annotations.NotNull()
|
||||
public final java.lang.String getNonIgnoredProperty() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Test() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ package kotlinx.kapt
|
||||
/**
|
||||
* Declaration annotated with [KaptIgnored] would not be included in stubs passed to annotation processors.
|
||||
*/
|
||||
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.FIELD)
|
||||
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.FIELD,
|
||||
AnnotationTarget.PROPERTY, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
|
||||
@Retention(AnnotationRetention.SOURCE)
|
||||
public annotation class KaptIgnored
|
||||
Reference in New Issue
Block a user