diff --git a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/AbstractElement.java b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/AbstractElement.java index 2c6509fe..0a0bece0 100644 --- a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/AbstractElement.java +++ b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/AbstractElement.java @@ -29,7 +29,7 @@ * @since 1.0 */ @Experimental -abstract sealed class AbstractElement permits ClassDef, EnumDef, FieldDef, InterfaceDef, MethodDef, ParameterDef, PropertyDef, RecordDef { +abstract sealed class AbstractElement permits ObjectDef, FieldDef, MethodDef, ParameterDef, PropertyDef { protected final String name; protected final Set modifiers; diff --git a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/AbstractElementBuilder.java b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/AbstractElementBuilder.java index 34696637..f08eac55 100644 --- a/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/AbstractElementBuilder.java +++ b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/AbstractElementBuilder.java @@ -32,13 +32,13 @@ * @since 1.0 */ @Experimental -public sealed class AbstractElementBuilder permits ClassDef.ClassDefBuilder, EnumDef.EnumDefBuilder, FieldDef.FieldDefBuilder, InterfaceDef.InterfaceDefBuilder, MethodDef.MethodDefBuilder, ParameterDef.ParameterDefBuilder, PropertyDef.PropertyDefBuilder, RecordDef.RecordDefBuilder { +public sealed class AbstractElementBuilder permits ObjectDefBuilder, FieldDef.FieldDefBuilder, MethodDef.MethodDefBuilder, ParameterDef.ParameterDefBuilder, PropertyDef.PropertyDefBuilder { protected final String name; protected EnumSet modifiers = EnumSet.noneOf(Modifier.class); protected List annotations = new ArrayList<>(); protected List javadoc = new ArrayList<>(); - private final ThisType thisInstance; + protected final ThisType thisInstance; protected AbstractElementBuilder(String name) { this.name = name; 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 e85f6b15..83fd7f53 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 @@ -33,13 +33,11 @@ * @since 1.0 */ @Experimental -public final class ClassDef extends AbstractElement implements ObjectDef { +public final class ClassDef extends ObjectDef { private final List fields; - private final List methods; private final List properties; private final List typeVariables; - private final List superinterfaces; private final ClassTypeDef superclass; private ClassDef(String name, @@ -52,12 +50,10 @@ private ClassDef(String name, List typeVariables, List superinterfaces, ClassTypeDef superclass) { - super(name, modifiers, annotations, javadoc); + super(name, modifiers, annotations, javadoc, methods, superinterfaces); this.fields = fields; - this.methods = methods; this.properties = properties; this.typeVariables = typeVariables; - this.superinterfaces = superinterfaces; this.superclass = superclass; } @@ -69,10 +65,6 @@ public List getFields() { return fields; } - public List getMethods() { - return methods; - } - public List getProperties() { return properties; } @@ -81,10 +73,6 @@ public List getTypeVariables() { return typeVariables; } - public List getSuperinterfaces() { - return superinterfaces; - } - @Nullable public ClassTypeDef getSuperclass() { return superclass; @@ -152,13 +140,11 @@ public String toString() { * @since 1.0 */ @Experimental - public static final class ClassDefBuilder extends AbstractElementBuilder { + public static final class ClassDefBuilder extends ObjectDefBuilder { private final List fields = new ArrayList<>(); - private final List methods = new ArrayList<>(); private final List properties = new ArrayList<>(); private final List typeVariables = new ArrayList<>(); - private final List superinterfaces = new ArrayList<>(); private ClassTypeDef superclass; private ClassDefBuilder(String name) { @@ -175,11 +161,6 @@ public ClassDefBuilder addField(FieldDef field) { return this; } - public ClassDefBuilder addMethod(MethodDef method) { - methods.add(method); - return this; - } - public ClassDefBuilder addProperty(PropertyDef property) { properties.add(property); return this; @@ -190,11 +171,6 @@ public ClassDefBuilder addTypeVariable(TypeDef.TypeVariable typeVariable) { return this; } - public ClassDefBuilder addSuperinterface(TypeDef superinterface) { - superinterfaces.add(superinterface); - return this; - } - public ClassDef build() { return new ClassDef(name, modifiers, fields, methods, properties, annotations, javadoc, typeVariables, superinterfaces, superclass); } 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 e2c1add0..84112a72 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 @@ -29,11 +29,9 @@ * @since 1.0 */ @Experimental -public final class EnumDef extends AbstractElement implements ObjectDef { +public final class EnumDef extends ObjectDef { private final List enumConstants; - private final List methods; - private final List superinterfaces; private EnumDef(String name, EnumSet modifiers, @@ -42,28 +40,18 @@ private EnumDef(String name, List javadoc, List enumConstants, List superinterfaces) { - super(name, modifiers, annotations, javadoc); - this.methods = methods; + super(name, modifiers, annotations, javadoc, methods, superinterfaces); this.enumConstants = enumConstants; - this.superinterfaces = superinterfaces; } public static EnumDefBuilder builder(String name) { return new EnumDefBuilder(name); } - public List getMethods() { - return methods; - } - public List getEnumConstants() { return enumConstants; } - public List getSuperinterfaces() { - return superinterfaces; - } - /** * The enum definition builder. * @@ -71,31 +59,19 @@ public List getSuperinterfaces() { * @since 1.0 */ @Experimental - public static final class EnumDefBuilder extends AbstractElementBuilder { + public static final class EnumDefBuilder extends ObjectDefBuilder { private final List enumConstants = new ArrayList<>(); - private final List methods = new ArrayList<>(); - private final List superinterfaces = new ArrayList<>(); private EnumDefBuilder(String name) { super(name); } - public EnumDefBuilder addMethod(MethodDef method) { - methods.add(method); - return this; - } - public EnumDefBuilder addEnumConstant(String name) { enumConstants.add(name); return this; } - public EnumDefBuilder addSuperinterface(TypeDef superinterface) { - superinterfaces.add(superinterface); - return this; - } - public EnumDef build() { return new EnumDef(name, modifiers, methods, annotations, javadoc, enumConstants, superinterfaces); } 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 bfe59129..ea8b08a9 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 @@ -29,12 +29,10 @@ * @since 1.0 */ @Experimental -public final class InterfaceDef extends AbstractElement implements ObjectDef { +public final class InterfaceDef extends ObjectDef { - private final List methods; private final List properties; private final List typeVariables; - private final List superinterfaces; private InterfaceDef(String name, EnumSet modifiers, @@ -44,21 +42,15 @@ private InterfaceDef(String name, List javadoc, List typeVariables, List superinterfaces) { - super(name, modifiers, annotations, javadoc); - this.methods = methods; + super(name, modifiers, annotations, javadoc, methods, superinterfaces); this.properties = properties; this.typeVariables = typeVariables; - this.superinterfaces = superinterfaces; } public static InterfaceDefBuilder builder(String name) { return new InterfaceDefBuilder(name); } - public List getMethods() { - return methods; - } - public List getProperties() { return properties; } @@ -67,10 +59,6 @@ public List getTypeVariables() { return typeVariables; } - public List getSuperinterfaces() { - return superinterfaces; - } - /** * The interface definition builder. * @@ -78,22 +66,15 @@ public List getSuperinterfaces() { * @since 1.0 */ @Experimental - public static final class InterfaceDefBuilder extends AbstractElementBuilder { + public static final class InterfaceDefBuilder extends ObjectDefBuilder { - private final List methods = new ArrayList<>(); private final List properties = new ArrayList<>(); private final List typeVariables = new ArrayList<>(); - private final List superinterfaces = new ArrayList<>(); private InterfaceDefBuilder(String name) { super(name); } - public InterfaceDefBuilder addMethod(MethodDef method) { - methods.add(method); - return this; - } - public InterfaceDefBuilder addProperty(PropertyDef property) { properties.add(property); return this; @@ -104,11 +85,6 @@ public InterfaceDefBuilder addTypeVariable(TypeDef.TypeVariable typeVariable) { return this; } - public InterfaceDefBuilder addSuperinterface(TypeDef superinterface) { - superinterfaces.add(superinterface); - return this; - } - public InterfaceDef build() { return new InterfaceDef(name, modifiers, methods, properties, annotations, javadoc, typeVariables, superinterfaces); } 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 4b0de732..a7340107 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 @@ -15,27 +15,56 @@ */ package io.micronaut.sourcegen.model; +import io.micronaut.core.annotation.Experimental; import io.micronaut.core.naming.NameUtils; +import javax.lang.model.element.Modifier; +import java.util.List; +import java.util.Set; + /** - * The interface defining the object type. + * The abstract class representing a type: class, enum, interface or record. * * @author Denis Stepanov * @since 1.0 */ -public interface ObjectDef { +@Experimental +public abstract sealed class ObjectDef extends AbstractElement permits ClassDef, EnumDef, InterfaceDef, RecordDef { + + private final List methods; + private final List superinterfaces; + + ObjectDef( + String name, Set modifiers, List annotations, + List javadoc, List methods, List superinterfaces + ) { + super(name, modifiers, annotations, javadoc); + this.methods = methods; + this.superinterfaces = superinterfaces; + } - String getName(); + public final List getMethods() { + return methods; + } + + public final List getSuperinterfaces() { + return superinterfaces; + } - default String getPackageName() { + public final String getPackageName() { return NameUtils.getPackageName(getName()); } - default String getSimpleName() { + public final String getSimpleName() { return NameUtils.getSimpleName(getName()); } - default ClassTypeDef asTypeDef() { + /** + * Get the type definition for this type. + * + * @return The type definition + */ + public ClassTypeDef asTypeDef() { return ClassTypeDef.of(getName()); } 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 new file mode 100644 index 00000000..ce3549ee --- /dev/null +++ b/sourcegen-model/src/main/java/io/micronaut/sourcegen/model/ObjectDefBuilder.java @@ -0,0 +1,53 @@ +/* + * Copyright 2017-2023 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.micronaut.sourcegen.model; + +import io.micronaut.core.annotation.Experimental; + +import java.util.ArrayList; +import java.util.List; + +/** + * The abstract builder that is used for specific types: interfaces, classes, records or enums. + * + * @param The type of this builder + * @author Andriy Dmytruk + * @since 1.3 + */ +@Experimental +public sealed class ObjectDefBuilder + extends AbstractElementBuilder + permits ClassDef.ClassDefBuilder, InterfaceDef.InterfaceDefBuilder, + RecordDef.RecordDefBuilder, EnumDef.EnumDefBuilder { + + protected final List methods = new ArrayList<>(); + protected final List superinterfaces = new ArrayList<>(); + + protected ObjectDefBuilder(String name) { + super(name); + } + + public final ThisType addMethod(MethodDef method) { + methods.add(method); + return thisInstance; + } + + public final ThisType addSuperinterface(TypeDef superinterface) { + superinterfaces.add(superinterface); + 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 1bfeca78..b85faa76 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 @@ -29,12 +29,10 @@ * @since 1.0 */ @Experimental -public final class RecordDef extends AbstractElement implements ObjectDef { +public final class RecordDef extends ObjectDef { - private final List methods; private final List properties; private final List typeVariables; - private final List superinterfaces; private RecordDef(String name, EnumSet modifiers, @@ -44,21 +42,15 @@ private RecordDef(String name, List javadoc, List typeVariables, List superinterfaces) { - super(name, modifiers, annotations, javadoc); - this.methods = methods; + super(name, modifiers, annotations, javadoc, methods, superinterfaces); this.properties = properties; this.typeVariables = typeVariables; - this.superinterfaces = superinterfaces; } public static RecordDefBuilder builder(String name) { return new RecordDefBuilder(name); } - public List getMethods() { - return methods; - } - public List getProperties() { return properties; } @@ -67,10 +59,6 @@ public List getTypeVariables() { return typeVariables; } - public List getSuperinterfaces() { - return superinterfaces; - } - /** * The record definition builder. * @@ -78,22 +66,15 @@ public List getSuperinterfaces() { * @since 1.0 */ @Experimental - public static final class RecordDefBuilder extends AbstractElementBuilder { + public static final class RecordDefBuilder extends ObjectDefBuilder { - private final List methods = new ArrayList<>(); private final List properties = new ArrayList<>(); private final List typeVariables = new ArrayList<>(); - private final List superinterfaces = new ArrayList<>(); private RecordDefBuilder(String name) { super(name); } - public RecordDefBuilder addMethod(MethodDef method) { - methods.add(method); - return this; - } - public RecordDefBuilder addProperty(PropertyDef property) { properties.add(property); return this; @@ -104,11 +85,6 @@ public RecordDefBuilder addTypeVariable(TypeDef.TypeVariable typeVariable) { return this; } - public RecordDefBuilder addSuperinterface(TypeDef superinterface) { - superinterfaces.add(superinterface); - return this; - } - public RecordDef build() { return new RecordDef(name, modifiers, methods, properties, annotations, javadoc, typeVariables, superinterfaces); }