Kapt: Remove artificial KaptError exception on errors from annotation processor (KT-21262)
This commit is contained in:
@@ -22,6 +22,7 @@ import kotlin.collections.ArraysKt;
|
|||||||
import kotlin.jvm.functions.Function1;
|
import kotlin.jvm.functions.Function1;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
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.arguments.CommonCompilerArguments;
|
||||||
import org.jetbrains.kotlin.cli.common.messages.GroupingMessageCollector;
|
import org.jetbrains.kotlin.cli.common.messages.GroupingMessageCollector;
|
||||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
|
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
|
||||||
@@ -127,6 +128,9 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> extends CLI
|
|||||||
}
|
}
|
||||||
return exitCode;
|
return exitCode;
|
||||||
}
|
}
|
||||||
|
catch (AnalysisResult.CompilationErrorException e) {
|
||||||
|
return COMPILATION_ERROR;
|
||||||
|
}
|
||||||
catch (Throwable t) {
|
catch (Throwable t) {
|
||||||
MessageCollectorUtil.reportException(groupingCollector, t);
|
MessageCollectorUtil.reportException(groupingCollector, t);
|
||||||
return INTERNAL_ERROR;
|
return INTERNAL_ERROR;
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ open class AnalysisResult protected constructor(
|
|||||||
val moduleDescriptor: ModuleDescriptor,
|
val moduleDescriptor: ModuleDescriptor,
|
||||||
val shouldGenerateCode: Boolean = true
|
val shouldGenerateCode: Boolean = true
|
||||||
) {
|
) {
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
override fun equals(other: Any?): Boolean {
|
||||||
if (this === other) return true
|
if (this === other) return true
|
||||||
return (other is AnalysisResult && bindingContext == other.bindingContext &&
|
return (other is AnalysisResult && bindingContext == other.bindingContext &&
|
||||||
@@ -48,17 +47,25 @@ open class AnalysisResult protected constructor(
|
|||||||
operator fun component3() = shouldGenerateCode
|
operator fun component3() = shouldGenerateCode
|
||||||
|
|
||||||
val error: Throwable
|
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() {
|
fun throwIfError() {
|
||||||
if (isError()) {
|
when {
|
||||||
throw IllegalStateException("failed to analyze: " + error, error)
|
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(
|
class RetryWithAdditionalJavaRoots(
|
||||||
bindingContext: BindingContext,
|
bindingContext: BindingContext,
|
||||||
@@ -78,8 +85,12 @@ open class AnalysisResult protected constructor(
|
|||||||
return AnalysisResult(bindingContext, module, shouldGenerateCode)
|
return AnalysisResult(bindingContext, module, shouldGenerateCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic fun error(bindingContext: BindingContext, error: Throwable): AnalysisResult {
|
@JvmStatic fun internalError(bindingContext: BindingContext, error: Throwable): AnalysisResult {
|
||||||
return Error(bindingContext, error)
|
return InternalError(bindingContext, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic fun compilationError(bindingContext: BindingContext): AnalysisResult {
|
||||||
|
return CompilationError(bindingContext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -104,7 +104,7 @@ internal class PerFileAnalysisCache(val file: KtFile, val componentProvider: Com
|
|||||||
DiagnosticUtils.throwIfRunningOnServer(e)
|
DiagnosticUtils.throwIfRunningOnServer(e)
|
||||||
LOG.error(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)
|
DiagnosticUtils.throwIfRunningOnServer(e)
|
||||||
LOG.error(e)
|
LOG.error(e)
|
||||||
|
|
||||||
return AnalysisResult.error(BindingContext.EMPTY, e)
|
return AnalysisResult.internalError(BindingContext.EMPTY, e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -105,7 +105,7 @@ internal class ProjectResolutionFacade(
|
|||||||
val withError = results.firstOrNull { it.isError() }
|
val withError = results.firstOrNull { it.isError() }
|
||||||
val bindingContext = CompositeBindingContext.create(results.map { it.bindingContext })
|
val bindingContext = CompositeBindingContext.create(results.map { it.bindingContext })
|
||||||
return if (withError != null)
|
return if (withError != null)
|
||||||
AnalysisResult.error(bindingContext, withError.error)
|
AnalysisResult.internalError(bindingContext, withError.error)
|
||||||
else
|
else
|
||||||
//TODO: (module refactoring) several elements are passed here in debugger
|
//TODO: (module refactoring) several elements are passed here in debugger
|
||||||
AnalysisResult.success(bindingContext, findModuleDescriptor(elements.first().getModuleInfo()))
|
AnalysisResult.success(bindingContext, findModuleDescriptor(elements.first().getModuleInfo()))
|
||||||
|
|||||||
@@ -166,10 +166,15 @@ abstract class AbstractKapt3Extension(
|
|||||||
try {
|
try {
|
||||||
runAnnotationProcessing(kaptContext, processors)
|
runAnnotationProcessing(kaptContext, processors)
|
||||||
} catch (error: KaptError) {
|
} catch (error: KaptError) {
|
||||||
val originalException = error.cause ?: error
|
val cause = error.cause
|
||||||
return AnalysisResult.error(bindingTrace.bindingContext, originalException)
|
|
||||||
|
if (cause != null) {
|
||||||
|
kaptContext.logger.exception(cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
return AnalysisResult.compilationError(bindingTrace.bindingContext)
|
||||||
} catch (thr: Throwable) {
|
} catch (thr: Throwable) {
|
||||||
return AnalysisResult.error(bindingTrace.bindingContext, thr)
|
return AnalysisResult.internalError(bindingTrace.bindingContext, thr)
|
||||||
} finally {
|
} finally {
|
||||||
kaptContext.close()
|
kaptContext.close()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user