properly report exception if analyze failed

#KT-1831 Fixed
This commit is contained in:
Stepan Koltsov
2012-04-19 16:41:34 +04:00
parent aa4e4623d4
commit b47d37094e
10 changed files with 51 additions and 10 deletions
@@ -71,6 +71,8 @@ public class KotlinToJVMBytecodeCompiler {
return null; return null;
} }
exhaust.throwIfError();
return generate(environment, dependencies, messageCollector, exhaust, stubs); return generate(environment, dependencies, messageCollector, exhaust, stubs);
} }
@@ -96,7 +96,8 @@ public enum AnalyzerFacadeForJVM implements AnalyzerFacade {
injector.getTopDownAnalyzer().analyzeFiles(files); injector.getTopDownAnalyzer().analyzeFiles(files);
return new AnalyzeExhaust(bindingTraceContext.getBindingContext(), JetStandardLibrary.getInstance());
return AnalyzeExhaust.success(bindingTraceContext.getBindingContext(), JetStandardLibrary.getInstance());
} }
public static AnalyzeExhaust shallowAnalyzeFiles(Collection<JetFile> files, public static AnalyzeExhaust shallowAnalyzeFiles(Collection<JetFile> files,
@@ -27,12 +27,22 @@ import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
public class AnalyzeExhaust { public class AnalyzeExhaust {
@NotNull @NotNull
private final BindingContext bindingContext; private final BindingContext bindingContext;
@Nullable
private final JetStandardLibrary standardLibrary; private final JetStandardLibrary standardLibrary;
private final Throwable error;
public AnalyzeExhaust(@NotNull BindingContext bindingContext, @Nullable JetStandardLibrary standardLibrary) { private AnalyzeExhaust(@NotNull BindingContext bindingContext,
@Nullable JetStandardLibrary standardLibrary, @Nullable Throwable error) {
this.bindingContext = bindingContext; this.bindingContext = bindingContext;
this.standardLibrary = standardLibrary; this.standardLibrary = standardLibrary;
this.error = error;
}
public static AnalyzeExhaust success(@NotNull BindingContext bindingContext, @NotNull JetStandardLibrary standardLibrary) {
return new AnalyzeExhaust(bindingContext, standardLibrary, null);
}
public static AnalyzeExhaust error(@NotNull BindingContext bindingContext, @NotNull Throwable error) {
return new AnalyzeExhaust(bindingContext, null, error);
} }
@NotNull @NotNull
@@ -40,8 +50,23 @@ public class AnalyzeExhaust {
return bindingContext; return bindingContext;
} }
@Nullable @NotNull
public JetStandardLibrary getStandardLibrary() { public JetStandardLibrary getStandardLibrary() {
return standardLibrary; return standardLibrary;
} }
@NotNull
public Throwable getError() {
return error;
}
public boolean isError() {
return error != null;
}
public void throwIfError() {
if (isError()) {
throw new IllegalStateException("failed to analyze: " + error, error);
}
}
} }
@@ -165,6 +165,10 @@ public class JetLightClass extends AbstractLightClass implements JetJavaMirrorMa
// TODO: wrong environment // stepan.koltsov@ 2012-04-09 // TODO: wrong environment // stepan.koltsov@ 2012-04-09
CompilerSpecialMode.REGULAR, CompilerDependencies.compilerDependenciesForProduction(CompilerSpecialMode.REGULAR)); CompilerSpecialMode.REGULAR, CompilerDependencies.compilerDependenciesForProduction(CompilerSpecialMode.REGULAR));
if (context.isError()) {
throw new IllegalStateException("failed to analyze: " + context.getError(), context.getError());
}
final GenerationState state = new GenerationState(project, builderFactory, context, Collections.singletonList(file)) { final GenerationState state = new GenerationState(project, builderFactory, context, Collections.singletonList(file)) {
@Override @Override
protected void generateNamespace(JetFile namespace) { protected void generateNamespace(JetFile namespace) {
@@ -128,6 +128,7 @@ public abstract class CodegenTestCase extends JetLiteFixture {
final AnalyzeExhaust analyzeExhaust = AnalyzerFacadeForJVM.analyzeOneFileWithJavaIntegrationAndCheckForErrors( final AnalyzeExhaust analyzeExhaust = AnalyzerFacadeForJVM.analyzeOneFileWithJavaIntegrationAndCheckForErrors(
myFile, JetControlFlowDataTraceFactory.EMPTY, myFile, JetControlFlowDataTraceFactory.EMPTY,
CompileCompilerDependenciesTest.compilerDependenciesForTests(CompilerSpecialMode.REGULAR)); CompileCompilerDependenciesTest.compilerDependenciesForTests(CompilerSpecialMode.REGULAR));
analyzeExhaust.throwIfError();
GenerationState state = new GenerationState(getProject(), classBuilderFactory, analyzeExhaust, Collections.singletonList(myFile)); GenerationState state = new GenerationState(getProject(), classBuilderFactory, analyzeExhaust, Collections.singletonList(myFile));
state.compileCorrectFiles(CompilationErrorHandler.THROW_EXCEPTION); state.compileCorrectFiles(CompilationErrorHandler.THROW_EXCEPTION);
return state; return state;
@@ -39,6 +39,7 @@ public class GenerationUtils {
final AnalyzeExhaust analyzeExhaust = AnalyzerFacadeForJVM.analyzeOneFileWithJavaIntegrationAndCheckForErrors( final AnalyzeExhaust analyzeExhaust = AnalyzerFacadeForJVM.analyzeOneFileWithJavaIntegrationAndCheckForErrors(
psiFile, JetControlFlowDataTraceFactory.EMPTY, psiFile, JetControlFlowDataTraceFactory.EMPTY,
CompileCompilerDependenciesTest.compilerDependenciesForTests(compilerSpecialMode)); CompileCompilerDependenciesTest.compilerDependenciesForTests(compilerSpecialMode));
analyzeExhaust.throwIfError();
GenerationState state = new GenerationState(psiFile.getProject(), ClassBuilderFactories.binaries(false), analyzeExhaust, Collections.singletonList(psiFile)); GenerationState state = new GenerationState(psiFile.getProject(), ClassBuilderFactories.binaries(false), analyzeExhaust, Collections.singletonList(psiFile));
state.compileCorrectFiles(CompilationErrorHandler.THROW_EXCEPTION); state.compileCorrectFiles(CompilationErrorHandler.THROW_EXCEPTION);
return state; return state;
@@ -155,6 +155,7 @@ public class JetPositionManager implements PositionManager {
return mapper; return mapper;
} }
final AnalyzeExhaust analyzeExhaust = WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile(file); final AnalyzeExhaust analyzeExhaust = WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile(file);
analyzeExhaust.throwIfError();
JetTypeMapper typeMapper = new InjectorForJetTypeMapper( JetTypeMapper typeMapper = new InjectorForJetTypeMapper(
analyzeExhaust.getStandardLibrary(), analyzeExhaust.getBindingContext(), Collections.singletonList(file)).getJetTypeMapper(); analyzeExhaust.getStandardLibrary(), analyzeExhaust.getBindingContext(), Collections.singletonList(file)).getJetTypeMapper();
myTypeMappers.put(file, typeMapper); myTypeMappers.put(file, typeMapper);
@@ -206,13 +206,14 @@ public class BytecodeToolwindow extends JPanel implements Disposable {
try { try {
AnalyzeExhaust binding = WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile(file); AnalyzeExhaust binding = WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile(file);
// AnalyzingUtils.throwExceptionOnErrors(binding); // AnalyzingUtils.throwExceptionOnErrors(binding);
if (binding.isError()) {
return printStackTraceToString(binding.getError());
}
state = new GenerationState(myProject, ClassBuilderFactories.TEXT, binding, Collections.singletonList(file)); state = new GenerationState(myProject, ClassBuilderFactories.TEXT, binding, Collections.singletonList(file));
state.compileCorrectFiles(CompilationErrorHandler.THROW_EXCEPTION); state.compileCorrectFiles(CompilationErrorHandler.THROW_EXCEPTION);
} }
catch (Exception e) { catch (Exception e) {
StringWriter out = new StringWriter(1024); return printStackTraceToString(e);
e.printStackTrace(new PrintWriter(out));
return out.toString().replaceAll("\r", "");
} }
@@ -229,6 +230,11 @@ public class BytecodeToolwindow extends JPanel implements Disposable {
return answer.toString(); return answer.toString();
} }
private static String printStackTraceToString(Throwable e) {StringWriter out = new StringWriter(1024);
e.printStackTrace(new PrintWriter(out));
return out.toString().replaceAll("\r", "");
}
@Override @Override
public void dispose() { public void dispose() {
EditorFactory.getInstance().releaseEditor(myEditor); EditorFactory.getInstance().releaseEditor(myEditor);
@@ -96,7 +96,7 @@ public final class AnalyzerFacadeWithCache {
private Result<AnalyzeExhaust> emptyExhaustWithDiagnosticOnFile(Throwable e) { private Result<AnalyzeExhaust> emptyExhaustWithDiagnosticOnFile(Throwable e) {
BindingTraceContext bindingTraceContext = new BindingTraceContext(); BindingTraceContext bindingTraceContext = new BindingTraceContext();
bindingTraceContext.report(Errors.EXCEPTION_WHILE_ANALYZING.on(file, e)); bindingTraceContext.report(Errors.EXCEPTION_WHILE_ANALYZING.on(file, e));
AnalyzeExhaust analyzeExhaust = new AnalyzeExhaust(bindingTraceContext.getBindingContext(), null); AnalyzeExhaust analyzeExhaust = AnalyzeExhaust.error(bindingTraceContext.getBindingContext(), e);
return new Result<AnalyzeExhaust>(analyzeExhaust, PsiModificationTracker.MODIFICATION_COUNT); return new Result<AnalyzeExhaust>(analyzeExhaust, PsiModificationTracker.MODIFICATION_COUNT);
} }
}, false); }, false);
@@ -47,6 +47,6 @@ public enum JSAnalyzerFacadeForIDEA implements AnalyzerFacade {
@NotNull Predicate<PsiFile> filesToAnalyzeCompletely, @NotNull Predicate<PsiFile> filesToAnalyzeCompletely,
@NotNull JetControlFlowDataTraceFactory flowDataTraceFactory) { @NotNull JetControlFlowDataTraceFactory flowDataTraceFactory) {
BindingContext context = AnalyzerFacadeForJS.analyzeFiles(files, new IDEAConfig(project)); BindingContext context = AnalyzerFacadeForJS.analyzeFiles(files, new IDEAConfig(project));
return new AnalyzeExhaust(context, JetStandardLibrary.getInstance()); return AnalyzeExhaust.success(context, JetStandardLibrary.getInstance());
} }
} }