Add a compiler parameter to generate not-null parameter assertions

Enable/disable assertion generation on parameters in the beginning of
methods accessible from Java
This commit is contained in:
Alexander Udalov
2012-10-04 16:05:03 +04:00
parent 12ce447c24
commit 753ae9e550
8 changed files with 29 additions and 8 deletions
@@ -70,8 +70,10 @@ public class GenerationState {
private final boolean generateNotNullAssertions;
private final boolean generateNotNullParamAssertions;
public GenerationState(Project project, ClassBuilderFactory builderFactory, AnalyzeExhaust analyzeExhaust, List<JetFile> 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<JetFile> 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();
@@ -44,4 +44,6 @@ public class JVMConfigurationKeys {
public static final CompilerConfigurationKey<Boolean> GENERATE_NOT_NULL_ASSERTIONS =
CompilerConfigurationKey.create("generate not-null assertions");
public static final CompilerConfigurationKey<Boolean> GENERATE_NOT_NULL_PARAMETER_ASSERTIONS =
CompilerConfigurationKey.create("generate not-null parameter assertions");
}
@@ -111,6 +111,7 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
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);
@@ -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;
@@ -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;
+1
View File
@@ -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
+1
View File
@@ -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
@@ -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;