From 75a8088f65fc0d395f31764b856e24a4f1709d28 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Thu, 10 Nov 2016 20:59:28 +0300 Subject: [PATCH] Kapt3: Minor: Changes on review --- .../org/jetbrains/kotlin/kapt3/KaptContext.kt | 4 ++-- .../kotlin/kapt3/annotationProcessing.kt | 24 ++++++++++--------- .../kotlin/kapt3/diagnostic/KaptError.kt | 1 + .../kotlin/kapt3/test/JavaKaptContextTest.kt | 10 ++++---- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/KaptContext.kt b/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/KaptContext.kt index 32704bb42f5..a08826a9ee6 100644 --- a/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/KaptContext.kt +++ b/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/KaptContext.kt @@ -32,7 +32,7 @@ class KaptContext( val compiledClasses: List, val origins: Map, processorOptions: Map -) { +) : 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() } diff --git a/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/annotationProcessing.kt b/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/annotationProcessing.kt index 48a1e15d9e0..ecfab8ee3c3 100644 --- a/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/annotationProcessing.kt +++ b/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/annotationProcessing.kt @@ -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() diff --git a/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/diagnostic/KaptError.kt b/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/diagnostic/KaptError.kt index c082fe9e96b..b752ae6eaeb 100644 --- a/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/diagnostic/KaptError.kt +++ b/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/diagnostic/KaptError.kt @@ -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") diff --git a/plugins/kapt3/test/org/jetbrains/kotlin/kapt3/test/JavaKaptContextTest.kt b/plugins/kapt3/test/org/jetbrains/kotlin/kapt3/test/JavaKaptContextTest.kt index a3bff9586b5..69c3bf23d86 100644 --- a/plugins/kapt3/test/org/jetbrains/kotlin/kapt3/test/JavaKaptContextTest.kt +++ b/plugins/kapt3/test/org/jetbrains/kotlin/kapt3/test/JavaKaptContextTest.kt @@ -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) } } \ No newline at end of file