Kapt3: Minor: Changes on review

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