diff --git a/sourcegen-bytecode-writer/src/main/java/io/micronaut/sourcegen/bytecode/expression/CastExpressionWriter.java b/sourcegen-bytecode-writer/src/main/java/io/micronaut/sourcegen/bytecode/expression/CastExpressionWriter.java index 14b9912c..baa2e8e7 100644 --- a/sourcegen-bytecode-writer/src/main/java/io/micronaut/sourcegen/bytecode/expression/CastExpressionWriter.java +++ b/sourcegen-bytecode-writer/src/main/java/io/micronaut/sourcegen/bytecode/expression/CastExpressionWriter.java @@ -18,9 +18,12 @@ import io.micronaut.inject.ast.ClassElement; import io.micronaut.sourcegen.bytecode.MethodContext; import io.micronaut.sourcegen.bytecode.TypeUtils; +import io.micronaut.sourcegen.model.ClassDef; import io.micronaut.sourcegen.model.ClassTypeDef; +import io.micronaut.sourcegen.model.EnumDef; import io.micronaut.sourcegen.model.ExpressionDef; import io.micronaut.sourcegen.model.ObjectDef; +import io.micronaut.sourcegen.model.RecordDef; import io.micronaut.sourcegen.model.TypeDef; import org.objectweb.asm.commons.GeneratorAdapter; @@ -61,34 +64,71 @@ private static void cast(GeneratorAdapter generatorAdapter, MethodContext contex if (!from.isPrimitive() && to.isPrimitive()) { unbox(generatorAdapter, context, to); } - } else if (!from.makeNullable().equals(to.makeNullable())) { - if (from instanceof ClassTypeDef.ClassElementType fromElement) { - ClassElement fromClassElement = fromElement.classElement(); - if (to instanceof ClassTypeDef.ClassElementType toElement) { - if (!fromClassElement.isAssignable(toElement.classElement())) { - checkCast(generatorAdapter, context, from, to); - } - } else if (to instanceof ClassTypeDef.JavaClass toClass) { - if (!fromClassElement.isAssignable(toClass.type())) { - checkCast(generatorAdapter, context, from, to); - } - } else if (to instanceof ClassTypeDef.ClassName toClassName) { - if (!fromClassElement.isAssignable(toClassName.className())) { - checkCast(generatorAdapter, context, from, to); - } - } else { - checkCast(generatorAdapter, context, from, to); - } - } else if (from instanceof ClassTypeDef.JavaClass fromClass && to instanceof ClassTypeDef.JavaClass toClass) { - if (!toClass.type().isAssignableFrom(fromClass.type())) { - checkCast(generatorAdapter, context, from, to); - } - } else { - checkCast(generatorAdapter, context, from, to); + } else if (needsCast(from, to)) { + checkCast(generatorAdapter, context, from, to); + } + } + + private static boolean needsCast(TypeDef from, TypeDef to) { + if (from.makeNullable().equals(to.makeNullable())) { + return false; + } + if (from instanceof ClassTypeDef.Parameterized parameterized) { + return needsCast(parameterized.rawType(), to); + } + if (to instanceof ClassTypeDef.Parameterized parameterized) { + return needsCast(from, parameterized.rawType()); + } + if (from instanceof ClassTypeDef.ClassElementType fromElement) { + return needsCast(fromElement.classElement(), to); + } + if (from instanceof ClassTypeDef.JavaClass fromClass) { + if (to instanceof ClassTypeDef.JavaClass toClass) { + return !toClass.type().isAssignableFrom(fromClass.type()); + } + } + if (from instanceof ClassTypeDef.ClassDefType fromClassDef) { + ClassTypeDef fromSuperclass = getSuperclass(fromClassDef.objectDef()); + if (fromSuperclass != null) { + return needsCast(fromSuperclass, to); + } + } + return true; + } + + private static boolean needsCast(ClassElement from, TypeDef to) { + if (to instanceof ClassTypeDef.ClassElementType toElement) { + return !from.isAssignable(toElement.classElement()); + } + if (to instanceof ClassTypeDef.JavaClass toClass) { + return !from.isAssignable(toClass.type()); + } + if (to instanceof ClassTypeDef.ClassName toClassName) { + return !from.isAssignable(toClassName.name()); + } + if (to instanceof ClassTypeDef.ClassDefType toClassDefType) { + if (from.isAssignable(toClassDefType.getName())) { + return false; } + return !from.isAssignable(toClassDefType.getName()); } + return true; } + private static ClassTypeDef getSuperclass(ObjectDef objectDef) { + if (objectDef instanceof ClassDef classDef) { + return classDef.getSuperclass(); + } + if (objectDef instanceof EnumDef) { + return ClassTypeDef.of(Enum.class); + } + if (objectDef instanceof RecordDef) { + return ClassTypeDef.of(Record.class); + } + return null; + } + + private static void checkCast(GeneratorAdapter generatorAdapter, MethodContext context, TypeDef from, TypeDef to) { TypeDef toType = ObjectDef.getContextualType(context.objectDef(), to); if (!toType.makeNullable().equals(from.makeNullable())) { diff --git a/sourcegen-bytecode-writer/src/test/java/io/micronaut/sourcegen/bytecode/ByteCodeWriterTest.java b/sourcegen-bytecode-writer/src/test/java/io/micronaut/sourcegen/bytecode/ByteCodeWriterTest.java index 1f7b20cd..4edcece8 100644 --- a/sourcegen-bytecode-writer/src/test/java/io/micronaut/sourcegen/bytecode/ByteCodeWriterTest.java +++ b/sourcegen-bytecode-writer/src/test/java/io/micronaut/sourcegen/bytecode/ByteCodeWriterTest.java @@ -23,6 +23,8 @@ import java.io.PrintStream; import java.io.PrintWriter; import java.io.StringWriter; +import java.util.AbstractList; +import java.util.List; import java.util.Map; import static io.micronaut.sourcegen.bytecode.DecompilerUtils.decompileToJava; @@ -1519,6 +1521,169 @@ Object invoke() { """, decompileToJava(bytes)); } + @Test + void testCastingClassDefWithSuperclass() { + + ClassDef myList = ClassDef.builder("example.MyList") + .superclass(ClassTypeDef.of(AbstractList.class)) + .build(); + + ClassDef classDef = ClassDef.builder("example.Test") + .addMethod(MethodDef.builder("load") + .returns(ClassTypeDef.of(List.class)) + .build((aThis, methodParameters) -> myList.asTypeDef() + .instantiate().returning()) + ) + .build(); + + StringWriter bytecodeWriter = new StringWriter(); + byte[] bytes = generateFile(classDef, bytecodeWriter); + + String bytecode = bytecodeWriter.toString(); + + Assertions.assertEquals(""" +// class version 61.0 (61) +// access flags 0x0 +// signature Ljava/lang/Object; +// declaration: example/Test +class example/Test { + + + // access flags 0x0 + ()V + ALOAD 0 + INVOKESPECIAL java/lang/Object. ()V + RETURN + + // access flags 0x0 + load()Ljava/util/List; + NEW example/MyList + DUP + INVOKESPECIAL example/MyList. ()V + ARETURN +} +""", bytecode); + + Assertions.assertEquals(""" +package example; + +import java.util.List; + +class Test { + List load() { + return new MyList(); + } +} +""", decompileToJava(bytes)); + } + + @Test + void testCastingThisClassDefWithSuperclass() { + + ClassDef classDef = ClassDef.builder("example.Test") + .superclass(TypeDef.parameterized(AbstractList.class, Number.class)) + .addMethod(MethodDef.builder("load") + .returns(ClassTypeDef.of(List.class)) + .build((aThis, methodParameters) -> aThis.type().instantiate().returning()) + ) + .build(); + + StringWriter bytecodeWriter = new StringWriter(); + byte[] bytes = generateFile(classDef, bytecodeWriter); + + String bytecode = bytecodeWriter.toString(); + + Assertions.assertEquals(""" +// class version 61.0 (61) +// access flags 0x0 +// signature Ljava/util/AbstractList; +// declaration: example/Test extends java.util.AbstractList +class example/Test extends java/util/AbstractList { + + + // access flags 0x0 + ()V + ALOAD 0 + INVOKESPECIAL java/util/AbstractList. ()V + RETURN + + // access flags 0x0 + load()Ljava/util/List; + NEW example/Test + DUP + INVOKESPECIAL example/Test. ()V + ARETURN +} +""", bytecode); + + Assertions.assertEquals(""" +package example; + +import java.util.AbstractList; +import java.util.List; + +class Test extends AbstractList { + List load() { + return new Test(); + } +} +""", decompileToJava(bytes)); + } + + @Test + void testCastingEnum() { + + EnumDef enumDef = EnumDef.builder("example.MyEnum") + .addEnumConstant("A") + .addEnumConstant("B") + .build(); + + ClassDef classDef = ClassDef.builder("example.Test") + .addMethod(MethodDef.builder("load") + .returns(Enum.class) + .build((aThis, methodParameters) -> enumDef.asTypeDef() + .getStaticField(enumDef.getField("A")) + .returning()) + ) + .build(); + + StringWriter bytecodeWriter = new StringWriter(); + byte[] bytes = generateFile(classDef, bytecodeWriter); + + String bytecode = bytecodeWriter.toString(); + + Assertions.assertEquals(""" +// class version 61.0 (61) +// access flags 0x0 +// signature Ljava/lang/Object; +// declaration: example/Test +class example/Test { + + + // access flags 0x0 + ()V + ALOAD 0 + INVOKESPECIAL java/lang/Object. ()V + RETURN + + // access flags 0x0 + load()Ljava/lang/Enum; + GETSTATIC example/MyEnum.A : Lexample/MyEnum; + ARETURN +} +""", bytecode); + + Assertions.assertEquals(""" +package example; + +class Test { + Enum load() { + return MyEnum.A; + } +} +""", decompileToJava(bytes)); + } + private String toBytecode(ObjectDef objectDef) { StringWriter stringWriter = new StringWriter(); generateFile(objectDef, stringWriter); diff --git a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ClassDef.java b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ClassDef.java index de62f2e0..29da2a3e 100644 --- a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ClassDef.java +++ b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ClassDef.java @@ -40,7 +40,7 @@ public final class ClassDef extends ObjectDef { private final ClassTypeDef superclass; private final StatementDef staticInitializer; - private ClassDef(ClassTypeDef type, + private ClassDef(ClassTypeDef.ClassName className, EnumSet modifiers, List fields, List methods, @@ -52,7 +52,8 @@ private ClassDef(ClassTypeDef type, ClassTypeDef superclass, List innerTypes, StatementDef staticInitializer) { - super(type, modifiers, annotations, javadoc, methods, properties, superinterfaces, innerTypes); + super(className, modifiers, annotations, javadoc, methods, properties, superinterfaces, innerTypes); + ClassTypeDef.of(this); this.fields = fields; this.typeVariables = typeVariables; this.superclass = superclass; @@ -60,8 +61,8 @@ private ClassDef(ClassTypeDef type, } @Override - public ClassDef withType(ClassTypeDef type) { - return new ClassDef(type, modifiers, fields, methods, properties, annotations, javadoc, typeVariables, superinterfaces, superclass, innerTypes, staticInitializer); + public ClassDef withClassName(ClassTypeDef.ClassName className) { + return new ClassDef(className, modifiers, fields, methods, properties, annotations, javadoc, typeVariables, superinterfaces, superclass, innerTypes, staticInitializer); } @Override @@ -108,7 +109,7 @@ public FieldDef findField(String name) { public FieldDef getField(String name) { FieldDef field = findField(name); if (field == null) { - throw new IllegalStateException("Class: " + this.name + " doesn't have a field: " + name); + throw new IllegalStateException("Class: " + this.className + " doesn't have a field: " + name); } return null; } @@ -122,8 +123,11 @@ public boolean hasField(String name) { if (superclass instanceof ClassTypeDef.ClassElementType classElementType) { return classElementType.classElement().findField(name).isPresent(); } - if (superclass instanceof ClassTypeDef.ClassDefType classDefType) { - return classDefType.classDef().hasField(name); + if (superclass instanceof ClassTypeDef.ClassDefType classDefType && classDefType.objectDef() instanceof ClassDef classDef) { + return classDef.hasField(name); + } + if (superclass instanceof ClassTypeDef.ClassDefType classDefType && classDefType.objectDef() instanceof EnumDef enumDef) { + return enumDef.hasField(name); } if (superclass instanceof ClassTypeDef.JavaClass javaClass) { try { @@ -146,7 +150,7 @@ public StatementDef getStaticInitializer() { @Override public String toString() { - return "ClassDef{" + "name='" + name + '\'' + '}'; + return "ClassDef{" + "name='" + className + '\'' + '}'; } /** @@ -199,7 +203,7 @@ public ClassDefBuilder addStaticInitializer(StatementDef staticInitializer) { } public ClassDef build() { - return new ClassDef(ClassTypeDef.of(name), modifiers, fields, methods, properties, annotations, javadoc, typeVariables, superinterfaces, superclass, innerTypes, staticInitializer); + return new ClassDef(new ClassTypeDef.ClassName(name), modifiers, fields, methods, properties, annotations, javadoc, typeVariables, superinterfaces, superclass, innerTypes, staticInitializer); } /** diff --git a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ClassTypeDef.java b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ClassTypeDef.java index f422eb71..09f60f35 100644 --- a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ClassTypeDef.java +++ b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ClassTypeDef.java @@ -44,10 +44,15 @@ public sealed interface ClassTypeDef extends TypeDef { String getName(); /** - * @return The type name + * @return The canonical type name * @since 1.5 */ - String getCanonicalName(); + default String getCanonicalName() { + if (isInner()) { + return getName().replace("$", "."); + } + return getName(); + } /** * @return The simple name @@ -418,11 +423,11 @@ static ClassTypeDef of(ClassElement classElement) { /** * Create a new type definition. * - * @param classDef The class definition + * @param objectDef The object definition * @return type definition */ - static ClassTypeDef of(ClassDef classDef) { - return new ClassDefType(classDef, false); + static ClassTypeDef of(ObjectDef objectDef) { + return new ClassDefType(objectDef, false); } /** @@ -504,26 +509,26 @@ public boolean isInner() { /** * The class name type. * - * @param className The class name + * @param name The class name * @param isInner Is inner * @param nullable Is nullable * @author Denis Stepanov * @since 1.0 */ @Experimental - record ClassName(String className, boolean isInner, boolean nullable) implements ClassTypeDef { + record ClassName(String name, boolean isInner, boolean nullable) implements ClassTypeDef { - @Override - public String getName() { - return className; + public ClassName(String name) { + this(name, false); + } + + public ClassName(String name, boolean isInner) { + this(name, isInner, false); } @Override - public String getCanonicalName() { - if (isInner) { - return className.replace("$", "."); - } - return className; + public String getName() { + return name; } @Override @@ -538,7 +543,7 @@ public boolean isNullable() { @Override public ClassTypeDef makeNullable() { - return new ClassName(className, isInner, true); + return new ClassName(name, isInner, true); } } @@ -598,27 +603,27 @@ public boolean isInner() { /** * The class def element type. * - * @param classDef The class def + * @param objectDef The object def * @param nullable Is nullable * @author Denis Stepanov * @since 1.2 */ @Experimental - record ClassDefType(ClassDef classDef, boolean nullable) implements ClassTypeDef { + record ClassDefType(ObjectDef objectDef, boolean nullable) implements ClassTypeDef { @Override public String getName() { - return classDef.getName(); + return objectDef.className.name; } @Override - public String getSimpleName() { - return classDef.getSimpleName(); + public boolean isInner() { + return objectDef.className.isInner; } @Override - public String getCanonicalName() { - return classDef.getName().replace("$", "."); + public boolean isInterface() { + return objectDef instanceof InterfaceDef; } @Override @@ -628,7 +633,7 @@ public boolean isNullable() { @Override public ClassTypeDef makeNullable() { - return new ClassDefType(classDef, true); + return new ClassDefType(objectDef, true); } } diff --git a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/EnumDef.java b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/EnumDef.java index 346e4a00..cf0ec43f 100644 --- a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/EnumDef.java +++ b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/EnumDef.java @@ -45,7 +45,7 @@ public final class EnumDef extends ObjectDef { private final List fields; private final LinkedHashMap> enumConstants; - private EnumDef(ClassTypeDef type, + private EnumDef(ClassTypeDef.ClassName className, EnumSet modifiers, List fields, List methods, @@ -55,14 +55,14 @@ private EnumDef(ClassTypeDef type, LinkedHashMap> enumConstants, List superinterfaces, List innerTypes) { - super(type, modifiers, annotations, javadoc, methods, properties, superinterfaces, innerTypes); + super(className, modifiers, annotations, javadoc, methods, properties, superinterfaces, innerTypes); this.fields = fields; this.enumConstants = enumConstants; } @Override - public EnumDef withType(ClassTypeDef type) { - return new EnumDef(type, modifiers, fields, methods, properties, annotations, javadoc, enumConstants, superinterfaces, innerTypes); + public EnumDef withClassName(ClassTypeDef.ClassName className) { + return new EnumDef(className, modifiers, fields, methods, properties, annotations, javadoc, enumConstants, superinterfaces, innerTypes); } public static EnumDefBuilder builder(String name) { @@ -94,9 +94,12 @@ public FieldDef findField(String name) { @NonNull public FieldDef getField(String name) { + if (enumConstants.containsKey(name)) { + return FieldDef.builder(name, asTypeDef()).build(); + } FieldDef field = findField(name); if (field == null) { - throw new IllegalStateException("Enum: " + this.name + " doesn't have a field: " + name); + throw new IllegalStateException("Enum: " + this.className + " doesn't have a field: " + name); } return null; } @@ -169,7 +172,7 @@ public EnumDef build() { } } } - return new EnumDef(ClassTypeDef.of(name), modifiers, fields, methods, properties, annotations, javadoc, enumConstants, superinterfaces, innerTypes); + return new EnumDef(new ClassTypeDef.ClassName(name), modifiers, fields, methods, properties, annotations, javadoc, enumConstants, superinterfaces, innerTypes); } /** diff --git a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ExpressionDef.java b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ExpressionDef.java index 32ed5925..edce2828 100644 --- a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ExpressionDef.java +++ b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ExpressionDef.java @@ -449,7 +449,7 @@ default VariableDef.Field field(String fieldName, TypeDef typeDef) { * @since 1.2 */ default VariableDef.Field field(FieldDef fieldDef) { - return new VariableDef.Field(this, fieldDef.name, fieldDef.getType()); + return new VariableDef.Field(this, fieldDef.getName(), fieldDef.getType()); } /** diff --git a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/InterfaceDef.java b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/InterfaceDef.java index 7353f689..527fbc59 100644 --- a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/InterfaceDef.java +++ b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/InterfaceDef.java @@ -33,7 +33,7 @@ public final class InterfaceDef extends ObjectDef { private final List typeVariables; - private InterfaceDef(ClassTypeDef type, + private InterfaceDef(ClassTypeDef.ClassName className, EnumSet modifiers, List methods, List properties, @@ -42,13 +42,13 @@ private InterfaceDef(ClassTypeDef type, List typeVariables, List superinterfaces, List innerTypes) { - super(type, modifiers, annotations, javadoc, methods, properties, superinterfaces, innerTypes); + super(className, modifiers, annotations, javadoc, methods, properties, superinterfaces, innerTypes); this.typeVariables = typeVariables; } @Override - public InterfaceDef withType(ClassTypeDef type) { - return new InterfaceDef(type, modifiers, methods, properties, annotations, javadoc, typeVariables, superinterfaces, innerTypes); + public InterfaceDef withClassName(ClassTypeDef.ClassName className) { + return new InterfaceDef(className, modifiers, methods, properties, annotations, javadoc, typeVariables, superinterfaces, innerTypes); } @Override @@ -88,7 +88,7 @@ public InterfaceDefBuilder addTypeVariable(TypeDef.TypeVariable typeVariable) { } public InterfaceDef build() { - return new InterfaceDef(ClassTypeDef.of(name), modifiers, methods, properties, annotations, javadoc, typeVariables, superinterfaces, innerTypes); + return new InterfaceDef(new ClassTypeDef.ClassName(name), modifiers, methods, properties, annotations, javadoc, typeVariables, superinterfaces, innerTypes); } } diff --git a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ObjectDef.java b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ObjectDef.java index 9fe6c747..5b084b76 100644 --- a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ObjectDef.java +++ b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ObjectDef.java @@ -32,20 +32,24 @@ @Experimental public abstract sealed class ObjectDef extends AbstractElement permits ClassDef, EnumDef, InterfaceDef, RecordDef { - protected final ClassTypeDef type; + protected final ClassTypeDef.ClassName className; protected final List methods; protected final List properties; protected final List superinterfaces; protected final List innerTypes; ObjectDef( - ClassTypeDef type, EnumSet modifiers, List annotations, - List javadoc, List methods, List properties, + ClassTypeDef.ClassName className, + EnumSet modifiers, + List annotations, + List javadoc, + List methods, + List properties, List superinterfaces, List innerTypes ) { - super(type.getName(), modifiers, annotations, javadoc); - this.type = type; + super(className.getName(), modifiers, annotations, javadoc); + this.className = className; this.methods = methods; this.properties = properties; this.superinterfaces = superinterfaces; @@ -65,11 +69,11 @@ public final List getSuperinterfaces() { } public final String getPackageName() { - return type.getPackageName(); + return className.getPackageName(); } public final String getSimpleName() { - return type.getSimpleName(); + return className.getSimpleName(); } public final List getInnerTypes() { @@ -77,12 +81,13 @@ public final List getInnerTypes() { } /** - * Creates a copy of this definition with a specific type. - * @param type The type - * @return the copy of this object definition with a new name + * Creates a copy of this definition with a new class name. + * + * @param className The class name + * @return the copy of this object definition with a new class name * @since 1.5 */ - public abstract ObjectDef withType(ClassTypeDef type); + public abstract ObjectDef withClassName(ClassTypeDef.ClassName className); /** * Get the type definition for this type. @@ -90,7 +95,7 @@ public final List getInnerTypes() { * @return The type definition */ public ClassTypeDef asTypeDef() { - return type; + return ClassTypeDef.of(this); } /** diff --git a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ObjectDefBuilder.java b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ObjectDefBuilder.java index 07064421..c9f92941 100644 --- a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ObjectDefBuilder.java +++ b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ObjectDefBuilder.java @@ -113,11 +113,8 @@ public final ThisType addSuperinterfaces(@NonNull Collection superinter @NonNull public final ThisType addInnerType(@NonNull ObjectDef innerDef) { ClassTypeDef innerType = innerDef.asTypeDef(); - ClassTypeDef newType = ClassTypeDef.of( - ClassTypeDef.of(name).getCanonicalName() + "$" + innerType.getSimpleName(), - true - ); - innerTypes.add(innerDef.withType(newType)); + String newName = ClassTypeDef.of(name).getCanonicalName() + "$" + innerType.getSimpleName(); + innerTypes.add(innerDef.withClassName(new ClassTypeDef.ClassName(newName, true))); return thisInstance; } diff --git a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/RecordDef.java b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/RecordDef.java index 52f0796d..52cc1957 100644 --- a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/RecordDef.java +++ b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/RecordDef.java @@ -33,7 +33,7 @@ public final class RecordDef extends ObjectDef { private final List typeVariables; - private RecordDef(ClassTypeDef type, + private RecordDef(ClassTypeDef.ClassName className, EnumSet modifiers, List methods, List properties, @@ -42,13 +42,13 @@ private RecordDef(ClassTypeDef type, List typeVariables, List superinterfaces, List innerTypes) { - super(type, modifiers, annotations, javadoc, methods, properties, superinterfaces, innerTypes); + super(className, modifiers, annotations, javadoc, methods, properties, superinterfaces, innerTypes); this.typeVariables = typeVariables; } @Override - public RecordDef withType(ClassTypeDef type) { - return new RecordDef(type, modifiers, methods, properties, annotations, javadoc, typeVariables, superinterfaces, innerTypes); + public RecordDef withClassName(ClassTypeDef.ClassName className) { + return new RecordDef(className, modifiers, methods, properties, annotations, javadoc, typeVariables, superinterfaces, innerTypes); } public static RecordDefBuilder builder(String name) { @@ -80,7 +80,7 @@ public RecordDefBuilder addTypeVariable(TypeDef.TypeVariable typeVariable) { } public RecordDef build() { - return new RecordDef(ClassTypeDef.of(name), modifiers, methods, properties, annotations, javadoc, typeVariables, superinterfaces, innerTypes); + return new RecordDef(new ClassTypeDef.ClassName(name), modifiers, methods, properties, annotations, javadoc, typeVariables, superinterfaces, innerTypes); } } diff --git a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/VariableDef.java b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/VariableDef.java index 5b97d475..8b1af659 100644 --- a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/VariableDef.java +++ b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/VariableDef.java @@ -43,7 +43,7 @@ default StatementDef assign(ExpressionDef expression) { * @return The statement */ default StatementDef assign(ParameterDef parameterDef) { - return assign(new MethodParameter(parameterDef.name, parameterDef.getType())); + return assign(new MethodParameter(parameterDef.getName(), parameterDef.getType())); } /** @@ -87,7 +87,7 @@ public StatementDef.DefineAndAssign defineAndAssign(ExpressionDef expression) { record MethodParameter(String name, TypeDef type) implements VariableDef { public MethodParameter(ParameterDef parameterDef) { - this(parameterDef.name, parameterDef.getType()); + this(parameterDef.getName(), parameterDef.getType()); } }