Kapt3: Simplify handling of annotation processing exceptions
The previous handling method was unreliable ("Don't know how to render diagnostic of type KAPT3_PROCESSING_ERROR", exceptions being lost).
This commit is contained in:
committed by
Yan Zhulanow
parent
f8b785fc31
commit
7bbf9861d0
@@ -33,7 +33,6 @@ import org.jetbrains.kotlin.container.ComponentProvider
|
|||||||
import org.jetbrains.kotlin.context.ProjectContext
|
import org.jetbrains.kotlin.context.ProjectContext
|
||||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||||
import org.jetbrains.kotlin.kapt3.AptMode.*
|
import org.jetbrains.kotlin.kapt3.AptMode.*
|
||||||
import org.jetbrains.kotlin.kapt3.diagnostic.ErrorsKapt3
|
|
||||||
import org.jetbrains.kotlin.kapt3.diagnostic.KaptError
|
import org.jetbrains.kotlin.kapt3.diagnostic.KaptError
|
||||||
import org.jetbrains.kotlin.kapt3.stubs.ClassFileToSourceStubConverter
|
import org.jetbrains.kotlin.kapt3.stubs.ClassFileToSourceStubConverter
|
||||||
import org.jetbrains.kotlin.kapt3.util.KaptLogger
|
import org.jetbrains.kotlin.kapt3.util.KaptLogger
|
||||||
@@ -162,18 +161,11 @@ abstract class AbstractKapt3Extension(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
runAnnotationProcessing(kaptContext, processors)
|
runAnnotationProcessing(kaptContext, processors)
|
||||||
|
} catch (error: KaptError) {
|
||||||
|
val originalException = error.cause ?: error
|
||||||
|
return AnalysisResult.error(bindingTrace.bindingContext, originalException)
|
||||||
} catch (thr: Throwable) {
|
} catch (thr: Throwable) {
|
||||||
if (thr !is KaptError || thr.kind != KaptError.Kind.ERROR_RAISED) {
|
return AnalysisResult.error(bindingTrace.bindingContext, thr)
|
||||||
logger.exception(thr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// We don't have any Kotlin files, so there isn't anything we can report diagnostic on
|
|
||||||
if (files.isEmpty()) {
|
|
||||||
throw thr
|
|
||||||
}
|
|
||||||
|
|
||||||
bindingTrace.report(ErrorsKapt3.KAPT3_PROCESSING_ERROR.on(files.first()))
|
|
||||||
return null // Compilation will be aborted anyway because of the error above
|
|
||||||
} finally {
|
} finally {
|
||||||
kaptContext.close()
|
kaptContext.close()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,8 +38,6 @@ import org.jetbrains.kotlin.config.JVMConfigurationKeys
|
|||||||
import org.jetbrains.kotlin.container.ComponentProvider
|
import org.jetbrains.kotlin.container.ComponentProvider
|
||||||
import org.jetbrains.kotlin.context.ProjectContext
|
import org.jetbrains.kotlin.context.ProjectContext
|
||||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||||
import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages
|
|
||||||
import org.jetbrains.kotlin.kapt3.diagnostic.DefaultErrorMessagesKapt3
|
|
||||||
import org.jetbrains.kotlin.kapt3.util.KaptLogger
|
import org.jetbrains.kotlin.kapt3.util.KaptLogger
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
import org.jetbrains.kotlin.resolve.BindingTrace
|
import org.jetbrains.kotlin.resolve.BindingTrace
|
||||||
@@ -246,8 +244,6 @@ class Kapt3ComponentRegistrar : ComponentRegistrar {
|
|||||||
val useLightAnalysis = configuration.get(Kapt3ConfigurationKeys.USE_LIGHT_ANALYSIS) == "true"
|
val useLightAnalysis = configuration.get(Kapt3ConfigurationKeys.USE_LIGHT_ANALYSIS) == "true"
|
||||||
val correctErrorTypes = configuration.get(Kapt3ConfigurationKeys.CORRECT_ERROR_TYPES) == "true"
|
val correctErrorTypes = configuration.get(Kapt3ConfigurationKeys.CORRECT_ERROR_TYPES) == "true"
|
||||||
|
|
||||||
Extensions.getRootArea().getExtensionPoint(DefaultErrorMessages.Extension.EP_NAME).registerExtension(DefaultErrorMessagesKapt3())
|
|
||||||
|
|
||||||
if (isVerbose) {
|
if (isVerbose) {
|
||||||
logger.info("Kapt3 is enabled.")
|
logger.info("Kapt3 is enabled.")
|
||||||
logger.info("Annotation processing mode: $aptMode")
|
logger.info("Annotation processing mode: $aptMode")
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2016 JetBrains s.r.o.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.kotlin.kapt3.diagnostic
|
|
||||||
|
|
||||||
import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages
|
|
||||||
import org.jetbrains.kotlin.diagnostics.rendering.DiagnosticFactoryToRendererMap
|
|
||||||
|
|
||||||
class DefaultErrorMessagesKapt3 : DefaultErrorMessages.Extension {
|
|
||||||
|
|
||||||
private companion object {
|
|
||||||
val MAP = DiagnosticFactoryToRendererMap("Kapt3")
|
|
||||||
|
|
||||||
init {
|
|
||||||
MAP.put(ErrorsKapt3.KAPT3_PROCESSING_ERROR,
|
|
||||||
"Some error(s) occurred while processing annotations. Please see the error messages above.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getMap() = MAP
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2016 JetBrains s.r.o.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.kotlin.kapt3.diagnostic;
|
|
||||||
|
|
||||||
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory0;
|
|
||||||
import org.jetbrains.kotlin.diagnostics.Errors;
|
|
||||||
import org.jetbrains.kotlin.psi.KtElement;
|
|
||||||
|
|
||||||
import static org.jetbrains.kotlin.diagnostics.Severity.ERROR;
|
|
||||||
|
|
||||||
public interface ErrorsKapt3 {
|
|
||||||
DiagnosticFactory0<KtElement> KAPT3_PROCESSING_ERROR = DiagnosticFactory0.create(ERROR);
|
|
||||||
|
|
||||||
@SuppressWarnings("UnusedDeclaration")
|
|
||||||
Object _initializer = new Object() {
|
|
||||||
{
|
|
||||||
Errors.Initializer.initializeFactoryNames(ErrorsKapt3.class);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -57,6 +57,6 @@ class KaptLogger(
|
|||||||
e.printStackTrace(PrintWriter(writer))
|
e.printStackTrace(PrintWriter(writer))
|
||||||
writer.toString()
|
writer.toString()
|
||||||
}
|
}
|
||||||
messageCollector.report(CompilerMessageSeverity.EXCEPTION, PREFIX + "An exception occurred: " + stacktrace)
|
messageCollector.report(CompilerMessageSeverity.ERROR, PREFIX + "An exception occurred: " + stacktrace)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-6
@@ -16,20 +16,16 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.kapt3.test
|
package org.jetbrains.kotlin.kapt3.test
|
||||||
|
|
||||||
import com.intellij.openapi.extensions.Extensions
|
|
||||||
import com.intellij.openapi.util.text.StringUtil
|
import com.intellij.openapi.util.text.StringUtil
|
||||||
import com.sun.tools.javac.tree.JCTree
|
import com.sun.tools.javac.tree.JCTree
|
||||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
||||||
import org.jetbrains.kotlin.codegen.CodegenTestCase
|
import org.jetbrains.kotlin.codegen.CodegenTestCase
|
||||||
import org.jetbrains.kotlin.codegen.GenerationUtils
|
import org.jetbrains.kotlin.codegen.GenerationUtils
|
||||||
import org.jetbrains.kotlin.codegen.state.GenerationState
|
import org.jetbrains.kotlin.codegen.state.GenerationState
|
||||||
import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages
|
|
||||||
import org.jetbrains.kotlin.kapt3.AbstractKapt3Extension
|
import org.jetbrains.kotlin.kapt3.AbstractKapt3Extension
|
||||||
import org.jetbrains.kotlin.kapt3.AptMode
|
|
||||||
import org.jetbrains.kotlin.kapt3.AptMode.STUBS_AND_APT
|
import org.jetbrains.kotlin.kapt3.AptMode.STUBS_AND_APT
|
||||||
import org.jetbrains.kotlin.kapt3.Kapt3BuilderFactory
|
import org.jetbrains.kotlin.kapt3.Kapt3BuilderFactory
|
||||||
import org.jetbrains.kotlin.kapt3.KaptContext
|
import org.jetbrains.kotlin.kapt3.KaptContext
|
||||||
import org.jetbrains.kotlin.kapt3.diagnostic.DefaultErrorMessagesKapt3
|
|
||||||
import org.jetbrains.kotlin.kapt3.javac.KaptJavaFileObject
|
import org.jetbrains.kotlin.kapt3.javac.KaptJavaFileObject
|
||||||
import org.jetbrains.kotlin.kapt3.stubs.ClassFileToSourceStubConverter
|
import org.jetbrains.kotlin.kapt3.stubs.ClassFileToSourceStubConverter
|
||||||
import org.jetbrains.kotlin.kapt3.util.KaptLogger
|
import org.jetbrains.kotlin.kapt3.util.KaptLogger
|
||||||
@@ -130,8 +126,6 @@ abstract class AbstractKotlinKapt3IntegrationTest : CodegenTestCase() {
|
|||||||
|
|
||||||
AnalysisHandlerExtension.registerExtension(myEnvironment.project, kapt3Extension)
|
AnalysisHandlerExtension.registerExtension(myEnvironment.project, kapt3Extension)
|
||||||
|
|
||||||
Extensions.getRootArea().getExtensionPoint(DefaultErrorMessages.Extension.EP_NAME).registerExtension(DefaultErrorMessagesKapt3())
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
loadMultiFiles(files)
|
loadMultiFiles(files)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user