diff --git a/codegen-core/src/main/java/org/javahelpers/codegen/model/annotation/AnnotationDto.java b/codegen-core/src/main/java/org/javahelpers/codegen/model/annotation/AnnotationDto.java index 73bfc2a8..e91dbdd6 100644 --- a/codegen-core/src/main/java/org/javahelpers/codegen/model/annotation/AnnotationDto.java +++ b/codegen-core/src/main/java/org/javahelpers/codegen/model/annotation/AnnotationDto.java @@ -28,7 +28,7 @@ import java.util.Map; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; -import org.javahelpers.codegen.model.type.TypeName; +import org.javahelpers.codegen.model.type.*; /** * DTO representing an annotation to be generated in the output class. Contains the annotation type @@ -60,6 +60,15 @@ public TypeName getAnnotationType() { * @param annotationType the type of the annotation */ public void setAnnotationType(TypeName annotationType) { + if (annotationType instanceof TypeNameGeneric + || annotationType instanceof TypeNameArray + || annotationType instanceof TypeNameWildcard + || annotationType instanceof TypeNamePrimitive + || annotationType instanceof TypeNameVariable) { + throw new IllegalArgumentException( + "annotationType must be a plain class type; annotations can never be " + + annotationType.getClass().getSimpleName()); + } this.annotationType = annotationType; } diff --git a/codegen-core/src/main/java/org/javahelpers/codegen/model/imports/RegularImport.java b/codegen-core/src/main/java/org/javahelpers/codegen/model/imports/RegularImport.java index 2aa808f2..f3bd8f7d 100644 --- a/codegen-core/src/main/java/org/javahelpers/codegen/model/imports/RegularImport.java +++ b/codegen-core/src/main/java/org/javahelpers/codegen/model/imports/RegularImport.java @@ -41,6 +41,15 @@ public class RegularImport implements ImportStatement { private final TypeName type; + /** + * Creates a regular import from a class. + * + * @param clazz the class to import + */ + public RegularImport(Class clazz) { + this(TypeName.of(clazz)); + } + /** * Creates a regular import from a TypeName. * diff --git a/codegen-core/src/main/java/org/javahelpers/codegen/model/imports/StaticImport.java b/codegen-core/src/main/java/org/javahelpers/codegen/model/imports/StaticImport.java index dc989877..ee615834 100644 --- a/codegen-core/src/main/java/org/javahelpers/codegen/model/imports/StaticImport.java +++ b/codegen-core/src/main/java/org/javahelpers/codegen/model/imports/StaticImport.java @@ -40,6 +40,16 @@ public class StaticImport implements ImportStatement { private final TypeName type; private final String memberName; + /** + * Creates a static import from a class. + * + * @param clazz the class containing the static member + * @param memberName the name of the static member (method or field) + */ + public StaticImport(Class clazz, String memberName) { + this(TypeName.of(clazz), memberName); + } + /** * Creates a static import. * diff --git a/codegen-core/src/test/java/org/javahelpers/codegen/model/annotation/AnnotationDtoTest.java b/codegen-core/src/test/java/org/javahelpers/codegen/model/annotation/AnnotationDtoTest.java new file mode 100644 index 00000000..63815eb6 --- /dev/null +++ b/codegen-core/src/test/java/org/javahelpers/codegen/model/annotation/AnnotationDtoTest.java @@ -0,0 +1,66 @@ +/* + * MIT License + * + * Copyright (c) 2025-2026 Andreas Igel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.javahelpers.codegen.model.annotation; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.javahelpers.codegen.model.type.TypeName; +import org.javahelpers.codegen.model.type.TypeNameArray; +import org.javahelpers.codegen.model.type.TypeNameGeneric; +import org.javahelpers.codegen.model.type.TypeNameMap; +import org.javahelpers.codegen.model.type.TypeNamePrimitive; +import org.javahelpers.codegen.model.type.TypeNameSet; +import org.javahelpers.codegen.model.type.TypeNameVariable; +import org.javahelpers.codegen.model.type.TypeNameWildcard; +import org.junit.jupiter.api.Test; + +/** Tests for {@link AnnotationDto#setAnnotationType(TypeName)} validation. */ +class AnnotationDtoTest { + + @Test + void acceptsPlainClassType() { + AnnotationDto dto = new AnnotationDto(); + assertDoesNotThrow(() -> dto.setAnnotationType(new TypeName("java.lang", "Deprecated"))); + assertDoesNotThrow(() -> dto.setAnnotationType(TypeName.of(Deprecated.class))); + } + + @Test + void rejectsNonPlainTypeKinds() { + TypeName plain = new TypeName("java.lang", "Deprecated"); + assertRejects(TypeNameGeneric.of(java.util.List.class, String.class)); + assertRejects(TypeNameArray.of(String.class)); + assertRejects(TypeNameMap.of(String.class, Integer.class)); + assertRejects(TypeNameSet.of(String.class)); + assertRejects(TypeNameWildcard.extendsBound(plain)); + assertRejects(TypeNamePrimitive.INT); + assertRejects(new TypeNameVariable("T")); + } + + private static void assertRejects(TypeName type) { + AnnotationDto dto = new AnnotationDto(); + assertThrows(IllegalArgumentException.class, () -> dto.setAnnotationType(type)); + } +}