Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
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.TypeNameWildcard;

/**
* Represents a field declaration in the generated class.
Expand Down Expand Up @@ -129,7 +130,9 @@ public void addImport(Class<?> clazz) {
* @param typeName the type to import
*/
public void addImport(TypeName typeName) {
if (typeName instanceof TypeNameGeneric generic) {
if (typeName instanceof TypeNameWildcard wildcard) {
wildcard.getBound().ifPresent(this::addImport);
} else if (typeName instanceof TypeNameGeneric generic) {
addImport(generic.getRawType());
generic.getInnerTypeArguments().forEach(this::addImport);
} else if (typeName instanceof TypeNameArray array) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
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.TypeNameWildcard;

/**
* Generic representation of a class to generate.
Expand Down Expand Up @@ -171,7 +172,9 @@ public void addImport(Class<?> clazz) {
* @param typeName the type to import
*/
public void addImport(TypeName typeName) {
if (typeName instanceof TypeNameGeneric generic) {
if (typeName instanceof TypeNameWildcard wildcard) {
wildcard.getBound().ifPresent(this::addImport);
} else if (typeName instanceof TypeNameGeneric generic) {
addImport(generic.getRawType());
generic.getInnerTypeArguments().forEach(this::addImport);
} else if (typeName instanceof TypeNameArray array) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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.TypeNameWildcard;

/**
* Base DTO for holding code template information with placeholders.
Expand Down Expand Up @@ -131,7 +132,9 @@ public Set<ImportStatement> getCodeBlockImports() {
* @param typeName type to import
*/
public void addCodeBlockImport(TypeName typeName) {
if (typeName instanceof TypeNameGeneric generic) {
if (typeName instanceof TypeNameWildcard wildcard) {
wildcard.getBound().ifPresent(this::addCodeBlockImport);
} else if (typeName instanceof TypeNameGeneric generic) {
addCodeBlockImport(generic.getRawType());
generic.getInnerTypeArguments().forEach(this::addCodeBlockImport);
} else if (typeName instanceof TypeNameArray array) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ public String getClassName() {
return className;
}

/**
* Checks whether any immediate type argument is a wildcard.
*
* @return false for types without generic arguments
*/
public boolean hasWildcardArguments() {
return false;
}

/**
* Returns the simple name with generic type arguments (if any), without package prefixes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
* way.
*/
public class TypeNameGeneric extends TypeName {
/**
* Checks whether any immediate type argument is a wildcard, rather than a writable element type.
*
* @return true if an immediate argument is a wildcard
*/
@Override
public boolean hasWildcardArguments() {
return innerTypeArguments.stream().anyMatch(TypeNameWildcard.class::isInstance);
}

private final List<TypeName> innerTypeArguments;

/** The raw (outer) type before generic parameterization; preserves nested-type structure. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* 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.type;

import java.util.Objects;
import java.util.Optional;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

/** A wildcard type argument, optionally bounded above or below by another type. */
public final class TypeNameWildcard extends TypeName {
public enum BoundKind {
UNBOUNDED,
EXTENDS,
SUPER
}

private final BoundKind boundKind;
private final TypeName bound;

/** Creates an unbounded wildcard. */
public TypeNameWildcard() {
super("", "?");
boundKind = BoundKind.UNBOUNDED;
bound = null;
}

private TypeNameWildcard(BoundKind boundKind, TypeName bound) {
super("", "?");
this.boundKind = boundKind;
this.bound = Objects.requireNonNull(bound, "bound");
}

/**
* Creates an upper-bounded wildcard.
*
* @param bound the non-null upper bound
* @return the wildcard
*/
public static TypeNameWildcard extendsBound(TypeName bound) {
return new TypeNameWildcard(BoundKind.EXTENDS, bound);
}

/**
* Creates a lower-bounded wildcard.
*
* @param bound the non-null lower bound
* @return the wildcard
*/
public static TypeNameWildcard superBound(TypeName bound) {
return new TypeNameWildcard(BoundKind.SUPER, bound);
}

public BoundKind getBoundKind() {
return boundKind;
}

public Optional<TypeName> getBound() {
return Optional.ofNullable(bound);
}

@Override
public String getFullQualifiedName() {
return switch (boundKind) {
case UNBOUNDED -> "?";
case EXTENDS -> "? extends " + bound.getFullQualifiedName();
case SUPER -> "? super " + bound.getFullQualifiedName();
};
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

TypeNameWildcard that = (TypeNameWildcard) o;

return new EqualsBuilder()
.appendSuper(super.equals(o))
.append(boundKind, that.boundKind)
.append(bound, that.bound)
.isEquals();
}

@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.appendSuper(super.hashCode())
.append(boundKind)
.append(bound)
.toHashCode();
}
Comment thread
AndreasIgel marked this conversation as resolved.
Resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.javahelpers.codegen.model.type.TypeNameGeneric;
import org.javahelpers.codegen.model.type.TypeNamePrimitive;
import org.javahelpers.codegen.model.type.TypeNameVariable;
import org.javahelpers.codegen.model.type.TypeNameWildcard;

/**
* Utility class for collecting, deduplicating, and sorting imports for generated classes.
Expand Down Expand Up @@ -291,6 +292,11 @@ private void addImport(TypeName type) {
return;
}

if (type instanceof TypeNameWildcard wildcard) {
wildcard.getBound().ifPresent(this::addTypeImports);
return;
}

if (type instanceof TypeNameArray arrayType) {
addImport(arrayType.getTypeOfArray());
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@
/** Unit tests for {@link TypeName}. */
class TypeNameTest {

@Test
void hasWildcardArgumentsDefaultsToFalse() {
assertFalse(TypeName.of(String.class).hasWildcardArguments());
}

@Test
void hasWildcardArgumentsChecksImmediateGenericArguments() {
TypeName unbounded = new TypeNameGeneric(TypeName.of(List.class), new TypeNameWildcard());
TypeName upperBounded =
new TypeNameGeneric(
TypeName.of(List.class), TypeNameWildcard.extendsBound(TypeName.of(Number.class)));
TypeName lowerBounded =
new TypeNameGeneric(
TypeName.of(List.class), TypeNameWildcard.superBound(TypeName.of(Integer.class)));
TypeName concrete = new TypeNameGeneric(List.class, String.class);
TypeName empty = new TypeNameGeneric(TypeName.of(List.class), List.of());
TypeName nested = new TypeNameGeneric(TypeName.of(List.class), unbounded);

assertTrue(unbounded.hasWildcardArguments());
assertTrue(upperBounded.hasWildcardArguments());
assertTrue(lowerBounded.hasWildcardArguments());
assertFalse(concrete.hasWildcardArguments());
assertFalse(empty.hasWildcardArguments());
assertFalse(nested.hasWildcardArguments());
}

@Test
void is_qualifiedName_matchesExactClass() {
assertTrue(new TypeName("java.lang", "String").is(String.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.type;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import org.junit.jupiter.api.Test;

class TypeNameWildcardTest {
@Test
void modelsBoundsAndQualifiedNames() {
TypeNameWildcard unbounded = new TypeNameWildcard();
assertEquals("?", unbounded.getFullQualifiedName());
assertEquals(TypeNameWildcard.BoundKind.UNBOUNDED, unbounded.getBoundKind());
assertTrue(unbounded.getBound().isEmpty());
TypeNameWildcard upper = TypeNameWildcard.extendsBound(TypeName.of(Number.class));
assertEquals("? extends java.lang.Number", upper.getFullQualifiedName());
assertEquals(TypeName.of(Number.class), upper.getBound().orElseThrow());
TypeNameWildcard lower =
TypeNameWildcard.superBound(new TypeNameGeneric(List.class, String.class));
assertEquals("? super java.util.List<java.lang.String>", lower.getFullQualifiedName());
assertEquals(TypeNameWildcard.BoundKind.SUPER, lower.getBoundKind());
}

@Test
void equalityIncludesDirectionAndBound() {
TypeNameWildcard upper = TypeNameWildcard.extendsBound(TypeName.of(Number.class));
TypeNameWildcard same = TypeNameWildcard.extendsBound(TypeName.of(Number.class));
assertEquals(upper, same);
assertEquals(upper.hashCode(), same.hashCode());
assertEquals(new TypeNameWildcard(), new TypeNameWildcard());
assertNotEquals(upper, new TypeNameWildcard());
assertNotEquals(upper, TypeNameWildcard.superBound(TypeName.of(Number.class)));
assertNotEquals(upper, TypeNameWildcard.extendsBound(TypeName.of(Integer.class)));
assertNotEquals(upper, TypeName.of(Number.class));
assertNotEquals(upper, null);
}

@Test
void rejectsMissingBound() {
assertThrows(NullPointerException.class, () -> TypeNameWildcard.extendsBound(null));
assertThrows(NullPointerException.class, () -> TypeNameWildcard.superBound(null));
}
}
Loading