fix couple of bugs in constructor signature serialization/parsing
This commit is contained in:
@@ -362,7 +362,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
|
|||||||
|
|
||||||
CodegenContext.ConstructorContext constructorContext = context.intoConstructor(constructorDescriptor, typeMapper);
|
CodegenContext.ConstructorContext constructorContext = context.intoConstructor(constructorDescriptor, typeMapper);
|
||||||
|
|
||||||
Method constructorMethod;
|
JvmMethodSignature constructorMethod;
|
||||||
CallableMethod callableMethod;
|
CallableMethod callableMethod;
|
||||||
if (constructorDescriptor == null) {
|
if (constructorDescriptor == null) {
|
||||||
|
|
||||||
@@ -387,23 +387,22 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
|
|||||||
|
|
||||||
signatureWriter.writeVoidReturn();
|
signatureWriter.writeVoidReturn();
|
||||||
|
|
||||||
JvmMethodSignature jvmMethodSignature = signatureWriter.makeJvmMethodSignature("<init>");
|
constructorMethod = signatureWriter.makeJvmMethodSignature("<init>");
|
||||||
constructorMethod = jvmMethodSignature.getAsmMethod();
|
callableMethod = new CallableMethod("", constructorMethod, Opcodes.INVOKESPECIAL);
|
||||||
callableMethod = new CallableMethod("", jvmMethodSignature, Opcodes.INVOKESPECIAL);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
callableMethod = typeMapper.mapToCallableMethod(constructorDescriptor, kind);
|
callableMethod = typeMapper.mapToCallableMethod(constructorDescriptor, kind);
|
||||||
constructorMethod = callableMethod.getSignature().getAsmMethod();
|
constructorMethod = callableMethod.getSignature();
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectOrClosureCodegen closure = context.closure;
|
ObjectOrClosureCodegen closure = context.closure;
|
||||||
if(closure != null) {
|
if(closure != null) {
|
||||||
final List<Type> consArgTypes = new LinkedList<Type>(Arrays.asList(constructorMethod.getArgumentTypes()));
|
final LinkedList<JvmMethodParameterSignature> consArgTypes = new LinkedList<JvmMethodParameterSignature>(constructorMethod.getKotlinParameterTypes());
|
||||||
|
|
||||||
int insert = 0;
|
int insert = 0;
|
||||||
if(closure.captureThis) {
|
if(closure.captureThis) {
|
||||||
if(!CodegenUtil.hasThis0(descriptor))
|
if(!CodegenUtil.hasThis0(descriptor))
|
||||||
consArgTypes.add(insert, Type.getObjectType(context.getThisDescriptor().getName()));
|
consArgTypes.add(insert, new JvmMethodParameterSignature(Type.getObjectType(context.getThisDescriptor().getName()), "", JvmMethodParameterKind.THIS0));
|
||||||
insert++;
|
insert++;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -412,24 +411,24 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(closure.captureReceiver != null)
|
if(closure.captureReceiver != null)
|
||||||
consArgTypes.add(insert++, closure.captureReceiver);
|
consArgTypes.add(insert++, new JvmMethodParameterSignature(closure.captureReceiver, "", JvmMethodParameterKind.RECEIVER));
|
||||||
|
|
||||||
for (DeclarationDescriptor descriptor : closure.closure.keySet()) {
|
for (DeclarationDescriptor descriptor : closure.closure.keySet()) {
|
||||||
if(descriptor instanceof VariableDescriptor && !(descriptor instanceof PropertyDescriptor)) {
|
if(descriptor instanceof VariableDescriptor && !(descriptor instanceof PropertyDescriptor)) {
|
||||||
final Type sharedVarType = typeMapper.getSharedVarType(descriptor);
|
final Type sharedVarType = typeMapper.getSharedVarType(descriptor);
|
||||||
final Type type = sharedVarType != null ? sharedVarType : state.getTypeMapper().mapType(((VariableDescriptor) descriptor).getOutType());
|
final Type type = sharedVarType != null ? sharedVarType : state.getTypeMapper().mapType(((VariableDescriptor) descriptor).getOutType());
|
||||||
consArgTypes.add(insert++, type);
|
consArgTypes.add(insert++, new JvmMethodParameterSignature(type, "", JvmMethodParameterKind.SHARED_VAR));
|
||||||
}
|
}
|
||||||
else if(descriptor instanceof FunctionDescriptor) {
|
else if(descriptor instanceof FunctionDescriptor) {
|
||||||
assert closure.captureReceiver != null;
|
assert closure.captureReceiver != null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
constructorMethod = new Method("<init>", Type.VOID_TYPE, consArgTypes.toArray(new Type[consArgTypes.size()]));
|
constructorMethod = JvmMethodSignature.simple("<init>", Type.VOID_TYPE, consArgTypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
int flags = Opcodes.ACC_PUBLIC; // TODO
|
int flags = Opcodes.ACC_PUBLIC; // TODO
|
||||||
final MethodVisitor mv = v.newMethod(myClass, flags, constructorMethod.getName(), constructorMethod.getDescriptor(), null, null);
|
final MethodVisitor mv = v.newMethod(myClass, flags, constructorMethod.getName(), constructorMethod.getAsmMethod().getDescriptor(), constructorMethod.getGenericsSignature(), null);
|
||||||
if (!v.generateCode()) return;
|
if (!v.generateCode()) return;
|
||||||
|
|
||||||
AnnotationVisitor jetConstructorVisitor = mv.visitAnnotation(JvmStdlibNames.JET_CONSTRUCTOR.getDescriptor(), true);
|
AnnotationVisitor jetConstructorVisitor = mv.visitAnnotation(JvmStdlibNames.JET_CONSTRUCTOR.getDescriptor(), true);
|
||||||
@@ -613,7 +612,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
|
|||||||
mv.visitInsn(Opcodes.RETURN);
|
mv.visitInsn(Opcodes.RETURN);
|
||||||
FunctionCodegen.endVisit(mv, "constructor", myClass);
|
FunctionCodegen.endVisit(mv, "constructor", myClass);
|
||||||
|
|
||||||
FunctionCodegen.generateDefaultIfNeeded(constructorContext, state, v, constructorMethod, constructorDescriptor, OwnerKind.IMPLEMENTATION);
|
FunctionCodegen.generateDefaultIfNeeded(constructorContext, state, v, constructorMethod.getAsmMethod(), constructorDescriptor, OwnerKind.IMPLEMENTATION);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateTraitMethods(ExpressionCodegen codegen) {
|
private void generateTraitMethods(ExpressionCodegen codegen) {
|
||||||
|
|||||||
@@ -650,8 +650,9 @@ public class JetTypeMapper {
|
|||||||
|
|
||||||
List<ValueParameterDescriptor> parameters = descriptor.getOriginal().getValueParameters();
|
List<ValueParameterDescriptor> parameters = descriptor.getOriginal().getValueParameters();
|
||||||
ClassDescriptor classDescriptor = descriptor.getContainingDeclaration();
|
ClassDescriptor classDescriptor = descriptor.getContainingDeclaration();
|
||||||
|
|
||||||
writeFormalTypeParameters(descriptor.getTypeParameters(), signatureWriter);
|
// constructor type parmeters are fake
|
||||||
|
writeFormalTypeParameters(Collections.<TypeParameterDescriptor>emptyList(), signatureWriter);
|
||||||
|
|
||||||
signatureWriter.writeParametersStart();
|
signatureWriter.writeParametersStart();
|
||||||
|
|
||||||
|
|||||||
@@ -12,4 +12,5 @@ public enum JvmMethodParameterKind {
|
|||||||
THIS0,
|
THIS0,
|
||||||
RECEIVER,
|
RECEIVER,
|
||||||
TYPE_INFO,
|
TYPE_INFO,
|
||||||
|
SHARED_VAR,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,19 @@ public class JvmMethodSignature {
|
|||||||
this.kotlinReturnType = "";
|
this.kotlinReturnType = "";
|
||||||
this.genericsAvailable = false;
|
this.genericsAvailable = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static JvmMethodSignature simple(@NotNull String methodName, @NotNull Type returnType, @NotNull List<JvmMethodParameterSignature> parameterSignatures) {
|
||||||
|
return new JvmMethodSignature(new Method(methodName, returnType, getTypes(parameterSignatures).toArray(new Type[0])), parameterSignatures);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static List<Type> getTypes(@NotNull List<JvmMethodParameterSignature> signatures) {
|
||||||
|
List<Type> r = new ArrayList<Type>(signatures.size());
|
||||||
|
for (JvmMethodParameterSignature signature : signatures) {
|
||||||
|
r.add(signature.getAsmType());
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
private void checkGenericsAvailable() {
|
private void checkGenericsAvailable() {
|
||||||
if (!genericsAvailable) {
|
if (!genericsAvailable) {
|
||||||
|
|||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
class ClassWithConstructorAndTypeParameter<P, Q>(q: Int)
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
class ClassWithConstructorAndTypeParameter<P, Q>(q: Q)
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
class Constructor0()
|
||||||
|
|
||||||
|
// method: Constructor0::<init>
|
||||||
|
// jvm signature: ()V
|
||||||
|
// generic signature: null
|
||||||
|
// kotlin signature: null
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
class ConstructorWithTypeParameter<P>()
|
||||||
|
|
||||||
|
// method: ConstructorWithTypeParameter::<init>
|
||||||
|
// jvm signature: (Ljet/TypeInfo;)V
|
||||||
|
// generic signature: null
|
||||||
|
// kotlin signature: null
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
class ConstructorWithTypeParameter<P>(p: P)
|
||||||
|
|
||||||
|
// method: ConstructorWithTypeParameter::<init>
|
||||||
|
// jvm signature: (Ljet/TypeInfo;Ljava/lang/Object;)V
|
||||||
|
// generic signature: (Ljet/TypeInfo;TP;)V
|
||||||
|
// kotlin signature: null
|
||||||
@@ -182,17 +182,33 @@ public class WriteSignatureTest extends TestCaseWithTmpdir {
|
|||||||
public AnnotationVisitor visitAnnotationDefault() {
|
public AnnotationVisitor visitAnnotationDefault() {
|
||||||
return new EmptyVisitor();
|
return new EmptyVisitor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private String makeKotlinSignature() {
|
private String makeKotlinSignature() {
|
||||||
|
boolean allNulls = true;
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append(typeParameters);
|
sb.append(typeParameters);
|
||||||
|
if (typeParameters != null && typeParameters.length() > 0) {
|
||||||
|
allNulls = false;
|
||||||
|
}
|
||||||
sb.append("(");
|
sb.append("(");
|
||||||
for (String parameterType : parameterTypes) {
|
for (String parameterType : parameterTypes) {
|
||||||
sb.append(parameterType);
|
sb.append(parameterType);
|
||||||
|
if (parameterType != null) {
|
||||||
|
allNulls = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
sb.append(")");
|
sb.append(")");
|
||||||
sb.append(returnType);
|
sb.append(returnType);
|
||||||
return sb.toString();
|
if (returnType != null) {
|
||||||
|
allNulls = false;
|
||||||
|
}
|
||||||
|
if (allNulls) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user