Kapt: Remove artificial KaptError exception on errors from annotation processor (KT-21262)

This commit is contained in:
Yan Zhulanow
2017-11-16 19:18:57 +09:00
parent 0a0de8da29
commit 3d1ca61f9f
5 changed files with 34 additions and 14 deletions
@@ -22,6 +22,7 @@ import kotlin.collections.ArraysKt;
import kotlin.jvm.functions.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.analyzer.AnalysisResult;
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments;
import org.jetbrains.kotlin.cli.common.messages.GroupingMessageCollector;
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
@@ -127,6 +128,9 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> extends CLI
}
return exitCode;
}
catch (AnalysisResult.CompilationErrorException e) {
return COMPILATION_ERROR;
}
catch (Throwable t) {
MessageCollectorUtil.reportException(groupingCollector, t);
return INTERNAL_ERROR;
@@ -26,7 +26,6 @@ open class AnalysisResult protected constructor(
val moduleDescriptor: ModuleDescriptor,
val shouldGenerateCode: Boolean = true
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
return (other is AnalysisResult && bindingContext == other.bindingContext &&
@@ -48,17 +47,25 @@ open class AnalysisResult protected constructor(
operator fun component3() = shouldGenerateCode
val error: Throwable
get() = if (this is Error) this.exception else throw IllegalStateException("Should only be called for error analysis result")
get() = if (this is InternalError) this.exception else throw IllegalStateException("Should only be called for error analysis result")
fun isError(): Boolean = this is Error
fun isError(): Boolean = this is InternalError || this is CompilationError
fun throwIfError() {
if (isError()) {
throw IllegalStateException("failed to analyze: " + error, error)
when {
this is InternalError -> throw IllegalStateException("failed to analyze: " + error, error)
this is CompilationError -> throw CompilationErrorException()
}
}
private class Error(bindingContext: BindingContext, val exception: Throwable) : AnalysisResult(bindingContext, ErrorUtils.getErrorModule())
class CompilationErrorException : RuntimeException()
private class CompilationError(bindingContext: BindingContext) : AnalysisResult(bindingContext, ErrorUtils.getErrorModule())
private class InternalError(
bindingContext: BindingContext,
val exception: Throwable
) : AnalysisResult(bindingContext, ErrorUtils.getErrorModule())
class RetryWithAdditionalJavaRoots(
bindingContext: BindingContext,
@@ -78,8 +85,12 @@ open class AnalysisResult protected constructor(
return AnalysisResult(bindingContext, module, shouldGenerateCode)
}
@JvmStatic fun error(bindingContext: BindingContext, error: Throwable): AnalysisResult {
return Error(bindingContext, error)
@JvmStatic fun internalError(bindingContext: BindingContext, error: Throwable): AnalysisResult {
return InternalError(bindingContext, error)
}
@JvmStatic fun compilationError(bindingContext: BindingContext): AnalysisResult {
return CompilationError(bindingContext)
}
}
}
@@ -104,7 +104,7 @@ internal class PerFileAnalysisCache(val file: KtFile, val componentProvider: Com
DiagnosticUtils.throwIfRunningOnServer(e)
LOG.error(e)
return AnalysisResult.error(BindingContext.EMPTY, e)
return AnalysisResult.internalError(BindingContext.EMPTY, e)
}
}
}
@@ -188,7 +188,7 @@ private object KotlinResolveDataProvider {
DiagnosticUtils.throwIfRunningOnServer(e)
LOG.error(e)
return AnalysisResult.error(BindingContext.EMPTY, e)
return AnalysisResult.internalError(BindingContext.EMPTY, e)
}
}
@@ -105,7 +105,7 @@ internal class ProjectResolutionFacade(
val withError = results.firstOrNull { it.isError() }
val bindingContext = CompositeBindingContext.create(results.map { it.bindingContext })
return if (withError != null)
AnalysisResult.error(bindingContext, withError.error)
AnalysisResult.internalError(bindingContext, withError.error)
else
//TODO: (module refactoring) several elements are passed here in debugger
AnalysisResult.success(bindingContext, findModuleDescriptor(elements.first().getModuleInfo()))
@@ -166,10 +166,15 @@ abstract class AbstractKapt3Extension(
try {
runAnnotationProcessing(kaptContext, processors)
} catch (error: KaptError) {
val originalException = error.cause ?: error
return AnalysisResult.error(bindingTrace.bindingContext, originalException)
val cause = error.cause
if (cause != null) {
kaptContext.logger.exception(cause)
}
return AnalysisResult.compilationError(bindingTrace.bindingContext)
} catch (thr: Throwable) {
return AnalysisResult.error(bindingTrace.bindingContext, thr)
return AnalysisResult.internalError(bindingTrace.bindingContext, thr)
} finally {
kaptContext.close()
}