Kapt3: Minor: Changes on review
This commit is contained in:
committed by
Yan Zhulanow
parent
95d1210317
commit
75a8088f65
@@ -32,7 +32,7 @@ class KaptContext(
|
|||||||
val compiledClasses: List<ClassNode>,
|
val compiledClasses: List<ClassNode>,
|
||||||
val origins: Map<Any, JvmDeclarationOrigin>,
|
val origins: Map<Any, JvmDeclarationOrigin>,
|
||||||
processorOptions: Map<String, String>
|
processorOptions: Map<String, String>
|
||||||
) {
|
) : AutoCloseable {
|
||||||
val context = Context()
|
val context = Context()
|
||||||
val compiler: KaptJavaCompiler
|
val compiler: KaptJavaCompiler
|
||||||
val fileManager: JavacFileManager
|
val fileManager: JavacFileManager
|
||||||
@@ -53,7 +53,7 @@ class KaptContext(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun close() {
|
override fun close() {
|
||||||
compiler.close()
|
compiler.close()
|
||||||
fileManager.close()
|
fileManager.close()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.kapt3
|
|||||||
|
|
||||||
import com.sun.tools.javac.comp.CompileStates
|
import com.sun.tools.javac.comp.CompileStates
|
||||||
import com.sun.tools.javac.file.JavacFileManager
|
import com.sun.tools.javac.file.JavacFileManager
|
||||||
import com.sun.tools.javac.main.JavaCompiler
|
|
||||||
import com.sun.tools.javac.main.Option
|
import com.sun.tools.javac.main.Option
|
||||||
import com.sun.tools.javac.processing.AnnotationProcessingError
|
import com.sun.tools.javac.processing.AnnotationProcessingError
|
||||||
import com.sun.tools.javac.processing.JavacFiler
|
import com.sun.tools.javac.processing.JavacFiler
|
||||||
@@ -54,23 +53,28 @@ fun KaptContext.doAnnotationProcessing(
|
|||||||
|
|
||||||
val javaFileObjects = fileManager.getJavaFileObjectsFromFiles(javaSourceFiles)
|
val javaFileObjects = fileManager.getJavaFileObjectsFromFiles(javaSourceFiles)
|
||||||
val parsedJavaFiles = compiler.parseFiles(javaFileObjects)
|
val parsedJavaFiles = compiler.parseFiles(javaFileObjects)
|
||||||
if (compiler.shouldStop(CompileStates.CompileState.PARSE)) {
|
|
||||||
|
val log = Log.instance(context)
|
||||||
|
if (compiler.shouldStop(CompileStates.CompileState.PARSE) || log.nerrors > 0) {
|
||||||
throw KaptError(KaptError.Kind.JAVA_FILE_PARSING_ERROR)
|
throw KaptError(KaptError.Kind.JAVA_FILE_PARSING_ERROR)
|
||||||
}
|
}
|
||||||
|
|
||||||
val log = Log.instance(context)
|
val warningsBeforeAp: Int
|
||||||
val errorCountBeforeAp = log.nerrors
|
|
||||||
val warningsBeforeAp = log.nwarnings
|
|
||||||
|
|
||||||
val compilerAfterAnnotationProcessing: JavaCompiler? = null
|
|
||||||
try {
|
try {
|
||||||
compiler.processAnnotations(compiler.enterTrees(parsedJavaFiles + additionalSources))
|
val analyzedFiles = compiler.enterTrees(parsedJavaFiles + additionalSources)
|
||||||
|
if (log.nerrors > 0) {
|
||||||
|
throw KaptError(KaptError.Kind.ERROR_WHILE_ANALYSIS)
|
||||||
|
}
|
||||||
|
|
||||||
|
warningsBeforeAp = log.nwarnings
|
||||||
|
|
||||||
|
compiler.processAnnotations(analyzedFiles)
|
||||||
} catch (e: AnnotationProcessingError) {
|
} catch (e: AnnotationProcessingError) {
|
||||||
throw KaptError(KaptError.Kind.EXCEPTION, e.cause ?: e)
|
throw KaptError(KaptError.Kind.EXCEPTION, e.cause ?: e)
|
||||||
}
|
}
|
||||||
|
|
||||||
val filer = processingEnvironment.filer as JavacFiler
|
val filer = processingEnvironment.filer as JavacFiler
|
||||||
val errorCount = Math.max(log.nerrors, errorCountBeforeAp)
|
val errorCount = log.nerrors
|
||||||
val warningCount = log.nwarnings - warningsBeforeAp
|
val warningCount = log.nwarnings - warningsBeforeAp
|
||||||
|
|
||||||
logger.info { "Annotation processing complete, errors: $errorCount, warnings: $warningCount" }
|
logger.info { "Annotation processing complete, errors: $errorCount, warnings: $warningCount" }
|
||||||
@@ -81,8 +85,6 @@ fun KaptContext.doAnnotationProcessing(
|
|||||||
if (log.nerrors > 0) {
|
if (log.nerrors > 0) {
|
||||||
throw KaptError(KaptError.Kind.ERROR_RAISED)
|
throw KaptError(KaptError.Kind.ERROR_RAISED)
|
||||||
}
|
}
|
||||||
|
|
||||||
compilerAfterAnnotationProcessing?.close()
|
|
||||||
} finally {
|
} finally {
|
||||||
processingEnvironment.close()
|
processingEnvironment.close()
|
||||||
this@doAnnotationProcessing.close()
|
this@doAnnotationProcessing.close()
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ class KaptError : RuntimeException {
|
|||||||
|
|
||||||
enum class Kind(val message: String) {
|
enum class Kind(val message: String) {
|
||||||
JAVA_FILE_PARSING_ERROR("Java file parsing error"),
|
JAVA_FILE_PARSING_ERROR("Java file parsing error"),
|
||||||
|
ERROR_WHILE_ANALYSIS("Java file analysis error"),
|
||||||
EXCEPTION("Exception while annotation processing"),
|
EXCEPTION("Exception while annotation processing"),
|
||||||
ERROR_RAISED("Error while annotation processing"),
|
ERROR_RAISED("Error while annotation processing"),
|
||||||
UNKNOWN("Unknown error while annotation processing")
|
UNKNOWN("Unknown error while annotation processing")
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ class JavaKaptContextTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = KaptError::class)
|
||||||
fun testException() {
|
fun testException() {
|
||||||
val exceptionMessage = "Here we are!"
|
val exceptionMessage = "Here we are!"
|
||||||
|
|
||||||
@@ -116,19 +116,17 @@ class JavaKaptContextTest {
|
|||||||
} catch (e: KaptError) {
|
} catch (e: KaptError) {
|
||||||
assertEquals(KaptError.Kind.EXCEPTION, e.kind)
|
assertEquals(KaptError.Kind.EXCEPTION, e.kind)
|
||||||
assertEquals("Here we are!", e.cause!!.message)
|
assertEquals("Here we are!", e.cause!!.message)
|
||||||
|
throw e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = KaptError::class)
|
||||||
fun testParsingError() {
|
fun testParsingError() {
|
||||||
var catched = false
|
|
||||||
try {
|
try {
|
||||||
doAnnotationProcessing(File(TEST_DATA_DIR, "ParseError.java"), simpleProcessor(), TEST_DATA_DIR)
|
doAnnotationProcessing(File(TEST_DATA_DIR, "ParseError.java"), simpleProcessor(), TEST_DATA_DIR)
|
||||||
} catch (e: KaptError) {
|
} catch (e: KaptError) {
|
||||||
assertEquals(KaptError.Kind.JAVA_FILE_PARSING_ERROR, e.kind)
|
assertEquals(KaptError.Kind.JAVA_FILE_PARSING_ERROR, e.kind)
|
||||||
catched = true
|
throw e
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue("Exception was not catched", catched)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user