Get rid of unnecessary exceptions in JS translator

Before this commit, an internal error during JS translation resulted in the
actual exception wrapped in TranslationRuntimeException wrapped in
TranslationInternalException wrapped in RuntimeException being thrown. This
change gets rid of the two latter wrappings.

Also delete unthrown MainFunctionNotFoundException and related unused
constructors in TranslationException
This commit is contained in:
Alexander Udalov
2016-05-23 18:04:49 +03:00
parent 9418311bda
commit 2bd3211ba8
6 changed files with 21 additions and 81 deletions
@@ -55,6 +55,7 @@ import org.jetbrains.kotlin.js.facade.MainCallParameters;
import org.jetbrains.kotlin.js.facade.TranslationResult;
import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus;
import org.jetbrains.kotlin.psi.KtFile;
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
import org.jetbrains.kotlin.utils.PathUtil;
import java.io.File;
@@ -169,8 +170,9 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
try {
//noinspection unchecked
translationResult = translator.translate(sourcesFiles, mainCallParameters, jsAnalysisResult);
} catch (Exception e) {
throw new RuntimeException(e);
}
catch (Exception e) {
throw ExceptionUtilsKt.rethrow(e);
}
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
@@ -16,9 +16,7 @@
package org.jetbrains.kotlin.js.test.semantics;
import org.jetbrains.kotlin.js.facade.exceptions.TranslationInternalException;
import org.jetbrains.kotlin.js.test.SingleFileTranslationTest;
import org.junit.Assert;
public final class NumberTest extends SingleFileTranslationTest {
public NumberTest() {
@@ -56,14 +54,7 @@ public final class NumberTest extends SingleFileTranslationTest {
}
public void testHexadecimalConstant() throws Exception {
try {
fooBoxTest();
}
catch (TranslationInternalException e) {
Throwable cause = e.getCause();
Assert.assertTrue(cause instanceof IllegalStateException);
Assert.assertTrue(cause.getMessage().startsWith("Unsupported long constant "));
}
fooBoxTest();
}
public void testNumberCompareTo() throws Exception {
@@ -1,25 +0,0 @@
/*
* Copyright 2010-2015 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.js.facade.exceptions;
import org.jetbrains.annotations.NotNull;
public class MainFunctionNotFoundException extends TranslationException {
public MainFunctionNotFoundException(@NotNull String message) {
super(message);
}
}
@@ -19,15 +19,6 @@ package org.jetbrains.kotlin.js.facade.exceptions;
import org.jetbrains.annotations.NotNull;
public class TranslationException extends Exception {
public TranslationException(@NotNull String message) {
super(message);
}
public TranslationException(@NotNull Throwable cause) {
super(cause);
}
public TranslationException(@NotNull String message, @NotNull Exception cause) {
super(message, cause);
}
@@ -1,26 +0,0 @@
/*
* Copyright 2010-2015 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.js.facade.exceptions;
import org.jetbrains.annotations.NotNull;
public class TranslationInternalException extends TranslationException {
public TranslationInternalException(@NotNull Throwable cause) {
super(cause);
}
}
@@ -24,7 +24,9 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor;
import org.jetbrains.kotlin.idea.MainFunctionDetector;
import org.jetbrains.kotlin.js.config.Config;
import org.jetbrains.kotlin.js.facade.MainCallParameters;
import org.jetbrains.kotlin.js.facade.exceptions.*;
import org.jetbrains.kotlin.js.facade.exceptions.TranslationException;
import org.jetbrains.kotlin.js.facade.exceptions.TranslationRuntimeException;
import org.jetbrains.kotlin.js.facade.exceptions.UnsupportedFeatureException;
import org.jetbrains.kotlin.js.translate.callTranslator.CallTranslator;
import org.jetbrains.kotlin.js.translate.context.Namer;
import org.jetbrains.kotlin.js.translate.context.StaticContext;
@@ -46,6 +48,7 @@ import org.jetbrains.kotlin.psi.KtFile;
import org.jetbrains.kotlin.psi.KtNamedFunction;
import org.jetbrains.kotlin.resolve.BindingTrace;
import org.jetbrains.kotlin.resolve.bindingContextUtil.BindingContextUtilsKt;
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
import java.util.Collection;
import java.util.Collections;
@@ -164,11 +167,13 @@ public final class Translation {
}
@NotNull
public static TranslationContext generateAst(@NotNull BindingTrace bindingTrace,
@NotNull Collection<KtFile> files, @NotNull MainCallParameters mainCallParameters,
public static TranslationContext generateAst(
@NotNull BindingTrace bindingTrace,
@NotNull Collection<KtFile> files,
@NotNull MainCallParameters mainCallParameters,
@NotNull ModuleDescriptor moduleDescriptor,
@NotNull Config config)
throws TranslationException {
@NotNull Config config
) throws TranslationException {
try {
return doGenerateAst(bindingTrace, files, mainCallParameters, moduleDescriptor, config);
}
@@ -176,7 +181,7 @@ public final class Translation {
throw new UnsupportedFeatureException("Unsupported feature used.", e);
}
catch (Throwable e) {
throw new TranslationInternalException(e);
throw ExceptionUtilsKt.rethrow(e);
}
}
@@ -184,7 +189,8 @@ public final class Translation {
private static TranslationContext doGenerateAst(@NotNull BindingTrace bindingTrace, @NotNull Collection<KtFile> files,
@NotNull MainCallParameters mainCallParameters,
@NotNull ModuleDescriptor moduleDescriptor,
@NotNull Config config) throws MainFunctionNotFoundException {
@NotNull Config config
) {
StaticContext staticContext = StaticContext.generateStaticContext(bindingTrace, config, moduleDescriptor);
JsProgram program = staticContext.getProgram();
JsBlock block = program.getGlobalBlock();
@@ -226,8 +232,9 @@ public final class Translation {
//TODO: determine whether should throw exception
@Nullable
private static JsStatement generateCallToMain(@NotNull TranslationContext context, @NotNull Collection<KtFile> files,
@NotNull List<String> arguments) throws MainFunctionNotFoundException {
private static JsStatement generateCallToMain(
@NotNull TranslationContext context, @NotNull Collection<KtFile> files, @NotNull List<String> arguments
) {
MainFunctionDetector mainFunctionDetector = new MainFunctionDetector(context.bindingContext());
KtNamedFunction mainFunction = mainFunctionDetector.getMainFunction(files);
if (mainFunction == null) {