Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Comment thread
AndreasIgel marked this conversation as resolved.
Resolved
throw new IllegalArgumentException(
"annotationType must be a plain class type; annotations can never be "
+ annotationType.getClass().getSimpleName());
}
this.annotationType = annotationType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}