From 753ae9e55035d7dfa6c73dce09e3a6019c4fca0e Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 4 Oct 2012 16:05:03 +0400 Subject: [PATCH] Add a compiler parameter to generate not-null parameter assertions Enable/disable assertion generation on parameters in the beginning of methods accessible from Java --- .../jetbrains/jet/codegen/state/GenerationState.java | 12 ++++++++++-- .../jetbrains/jet/cli/jvm/JVMConfigurationKeys.java | 2 ++ .../src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java | 1 + .../jet/cli/jvm/K2JVMCompilerArguments.java | 3 +++ .../jvm/compiler/KotlinToJVMBytecodeCompiler.java | 10 ++++++---- compiler/testData/cli/help.out | 1 + compiler/testData/cli/wrongArgument.out | 1 + .../org/jetbrains/jet/codegen/CodegenTestCase.java | 7 +++++-- 8 files changed, 29 insertions(+), 8 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/state/GenerationState.java b/compiler/backend/src/org/jetbrains/jet/codegen/state/GenerationState.java index e59e0889427..19effe34a63 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/state/GenerationState.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/state/GenerationState.java @@ -70,8 +70,10 @@ public class GenerationState { private final boolean generateNotNullAssertions; + private final boolean generateNotNullParamAssertions; + public GenerationState(Project project, ClassBuilderFactory builderFactory, AnalyzeExhaust analyzeExhaust, List files) { - this(project, builderFactory, Progress.DEAF, analyzeExhaust, files, BuiltinToJavaTypesMapping.ENABLED, true); + this(project, builderFactory, Progress.DEAF, analyzeExhaust, files, BuiltinToJavaTypesMapping.ENABLED, true, false); } public GenerationState( @@ -81,7 +83,8 @@ public class GenerationState { @NotNull AnalyzeExhaust exhaust, @NotNull List files, @NotNull BuiltinToJavaTypesMapping builtinToJavaTypesMapping, - boolean generateNotNullAssertions + boolean generateNotNullAssertions, + boolean generateNotNullParamAssertions ) { this.project = project; this.progress = progress; @@ -102,6 +105,7 @@ public class GenerationState { this.classFileFactory = injector.getClassFileFactory(); this.generateNotNullAssertions = generateNotNullAssertions; + this.generateNotNullParamAssertions = generateNotNullParamAssertions; } @NotNull @@ -158,6 +162,10 @@ public class GenerationState { return generateNotNullAssertions; } + public boolean isGenerateNotNullParamAssertions() { + return generateNotNullParamAssertions; + } + public void beforeCompile() { markUsed(); diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/JVMConfigurationKeys.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/JVMConfigurationKeys.java index ba15c94c306..41605e0bd92 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/JVMConfigurationKeys.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/JVMConfigurationKeys.java @@ -44,4 +44,6 @@ public class JVMConfigurationKeys { public static final CompilerConfigurationKey GENERATE_NOT_NULL_ASSERTIONS = CompilerConfigurationKey.create("generate not-null assertions"); + public static final CompilerConfigurationKey GENERATE_NOT_NULL_PARAMETER_ASSERTIONS = + CompilerConfigurationKey.create("generate not-null parameter assertions"); } diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java index 28bb27324b8..2caaa91a2ec 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java @@ -111,6 +111,7 @@ public class K2JVMCompiler extends CLICompiler { builtins ? BuiltinToJavaTypesMapping.DISABLED : BuiltinToJavaTypesMapping.ENABLED); configuration.put(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, arguments.notNullAssertions); + configuration.put(JVMConfigurationKeys.GENERATE_NOT_NULL_PARAMETER_ASSERTIONS, arguments.notNullParamAssertions); configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector); diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompilerArguments.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompilerArguments.java index c5544b4aed7..adbe38d3005 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompilerArguments.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompilerArguments.java @@ -67,6 +67,9 @@ public class K2JVMCompilerArguments extends CompilerArguments { @Argument(value = "notNullAssertions", description = "generate not-null assertion after each invokation of method returning not-null") public boolean notNullAssertions; + @Argument(value = "notNullParamAssertions", description = "generate not-null assertions on parameters of methods accessible from Java") + public boolean notNullParamAssertions; + @Argument(value = "builtins", description = "compile builtin classes (internal)") public boolean builtins; diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java index 6c3b03a5568..4756ffe63da 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java @@ -335,21 +335,23 @@ public class KotlinToJVMBytecodeCompiler { AnalyzeExhaust exhaust, boolean stubs) { Project project = environment.getProject(); + final CompilerConfiguration configuration = environment.getConfiguration(); Progress backendProgress = new Progress() { @Override public void log(String message) { - environment.getConfiguration().get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY).report(CompilerMessageSeverity.LOGGING, message, CompilerMessageLocation.NO_LOCATION); + configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY).report(CompilerMessageSeverity.LOGGING, message, CompilerMessageLocation.NO_LOCATION); } }; GenerationState generationState = new GenerationState( project, ClassBuilderFactories.binaries(stubs), backendProgress, exhaust, environment.getSourceFiles(), - environment.getConfiguration().get(JVMConfigurationKeys.BUILTIN_TO_JAVA_TYPES_MAPPING_KEY, BuiltinToJavaTypesMapping.ENABLED), - environment.getConfiguration().get(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, false) + configuration.get(JVMConfigurationKeys.BUILTIN_TO_JAVA_TYPES_MAPPING_KEY, BuiltinToJavaTypesMapping.ENABLED), + configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, false), + configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_PARAMETER_ASSERTIONS, false) ); GenerationStrategy.STANDARD.compileCorrectFiles(generationState, CompilationErrorHandler.THROW_EXCEPTION); CompilerPluginContext context = new CompilerPluginContext(project, exhaust.getBindingContext(), environment.getSourceFiles()); - for (CompilerPlugin plugin : environment.getConfiguration().getList(CLIConfigurationKeys.COMPILER_PLUGINS)) { + for (CompilerPlugin plugin : configuration.getList(CLIConfigurationKeys.COMPILER_PLUGINS)) { plugin.processFiles(context); } return generationState; diff --git a/compiler/testData/cli/help.out b/compiler/testData/cli/help.out index eed5e8d9df8..d917c1fb853 100644 --- a/compiler/testData/cli/help.out +++ b/compiler/testData/cli/help.out @@ -8,6 +8,7 @@ Usage: org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments -noStdlib [flag] don't include Kotlin runtime into classpath -noJdkAnnotations [flag] don't include JDK external annotations into classpath -notNullAssertions [flag] generate not-null assertion after each invokation of method returning not-null + -notNullParamAssertions [flag] generate not-null assertions on parameters of methods accessible from Java -builtins [flag] compile builtin classes (internal) -output [String] output directory -module [String] module to compile diff --git a/compiler/testData/cli/wrongArgument.out b/compiler/testData/cli/wrongArgument.out index b722b4033e5..4f4c71db1bc 100644 --- a/compiler/testData/cli/wrongArgument.out +++ b/compiler/testData/cli/wrongArgument.out @@ -9,6 +9,7 @@ Usage: org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments -noStdlib [flag] don't include Kotlin runtime into classpath -noJdkAnnotations [flag] don't include JDK external annotations into classpath -notNullAssertions [flag] generate not-null assertion after each invokation of method returning not-null + -notNullParamAssertions [flag] generate not-null assertions on parameters of methods accessible from Java -builtins [flag] compile builtin classes (internal) -output [String] output directory -module [String] module to compile diff --git a/compiler/tests/org/jetbrains/jet/codegen/CodegenTestCase.java b/compiler/tests/org/jetbrains/jet/codegen/CodegenTestCase.java index 95952eb6c28..4b5cbe9db91 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/CodegenTestCase.java +++ b/compiler/tests/org/jetbrains/jet/codegen/CodegenTestCase.java @@ -35,6 +35,7 @@ import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys; import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; import org.jetbrains.jet.codegen.state.GenerationState; import org.jetbrains.jet.codegen.state.GenerationStrategy; +import org.jetbrains.jet.config.CompilerConfiguration; import org.jetbrains.jet.lang.BuiltinsScopeExtensionMode; import org.jetbrains.jet.lang.psi.JetPsiUtil; import org.jetbrains.jet.lang.resolve.AnalyzingUtils; @@ -359,10 +360,12 @@ public abstract class CodegenTestCase extends UsefulTestCase { BuiltinsScopeExtensionMode.ALL); analyzeExhaust.throwIfError(); AnalyzingUtils.throwExceptionOnErrors(analyzeExhaust.getBindingContext()); + CompilerConfiguration configuration = environment.getConfiguration(); GenerationState state = new GenerationState( environment.getProject(), classBuilderFactory, Progress.DEAF, analyzeExhaust, files.getPsiFiles(), - environment.getConfiguration().get(JVMConfigurationKeys.BUILTIN_TO_JAVA_TYPES_MAPPING_KEY, BuiltinToJavaTypesMapping.ENABLED), - environment.getConfiguration().get(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, true) + configuration.get(JVMConfigurationKeys.BUILTIN_TO_JAVA_TYPES_MAPPING_KEY, BuiltinToJavaTypesMapping.ENABLED), + configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, true), + configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_PARAMETER_ASSERTIONS, true) ); GenerationStrategy.STANDARD.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION); return state;