Generate not-null assertions on method parameters

Intrinsics.checkParameterIsNotNull() gets its caller's class and method names
from the stack trace to render them in an exception message.

Fix codegen tests because now it's now allowed to pass null to non-null
argument in tests
This commit is contained in:
Alexander Udalov
2012-10-04 15:46:25 +04:00
parent 753ae9e550
commit c9984c3d06
10 changed files with 108 additions and 9 deletions
@@ -422,6 +422,31 @@ public class AsmUtil {
genMethodThrow(mv, STUB_EXCEPTION, STUB_EXCEPTION_MESSAGE);
}
public static void genNotNullAssertionsForParameters(
@NotNull InstructionAdapter v,
@NotNull GenerationState state,
@NotNull FunctionDescriptor descriptor,
@NotNull FrameMap frameMap
) {
if (!state.isGenerateNotNullParamAssertions()) return;
// Private method is not accessible from other classes, no assertions needed
if (getVisibilityAccessFlag(descriptor) == ACC_PRIVATE) return;
for (ValueParameterDescriptor parameter : descriptor.getValueParameters()) {
JetType type = parameter.getReturnType();
if (type == null || type.isNullable()) continue;
int index = frameMap.getIndex(parameter);
Type asmType = state.getTypeMapper().mapReturnType(type);
if (asmType.getSort() == Type.OBJECT || asmType.getSort() == Type.ARRAY) {
v.load(index, asmType);
v.visitLdcInsn(descriptor.getName().getName());
v.invokestatic("jet/runtime/Intrinsics", "checkParameterIsNotNull", "(Ljava/lang/Object;Ljava/lang/String;)V");
}
}
}
public static void genNotNullAssertionForField(
@NotNull InstructionAdapter v,
@NotNull GenerationState state,
@@ -211,6 +211,8 @@ public class FunctionCodegen extends GenerationStateAware {
createSharedVarsForParameters(mv, functionDescriptor, frameMap, localVariablesInfo);
genNotNullAssertionsForParameters(new InstructionAdapter(mv), state, functionDescriptor, frameMap);
ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, asmMethod.getReturnType(), context, state);
codegen.returnExpression(fun.getBodyExpression());