diff --git a/changelog/@unreleased/pr-2264.v2.yml b/changelog/@unreleased/pr-2264.v2.yml new file mode 100644 index 000000000..6c2901696 --- /dev/null +++ b/changelog/@unreleased/pr-2264.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Expose a TypeMarker factory to wrap an arbitrary Type + links: + - https://github.com/palantir/conjure-java/pull/2264 diff --git a/conjure-undertow-lib/src/main/java/com/palantir/conjure/java/undertow/lib/TypeMarker.java b/conjure-undertow-lib/src/main/java/com/palantir/conjure/java/undertow/lib/TypeMarker.java index 0ce892bde..fe136bca9 100644 --- a/conjure-undertow-lib/src/main/java/com/palantir/conjure/java/undertow/lib/TypeMarker.java +++ b/conjure-undertow-lib/src/main/java/com/palantir/conjure/java/undertow/lib/TypeMarker.java @@ -21,6 +21,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; +import java.util.Objects; /** * Captures generic type information. @@ -49,6 +50,10 @@ protected TypeMarker() { SafeArg.of("typeVariable", type)); } + private TypeMarker(Type type) { + this.type = Preconditions.checkNotNull(type, "Type is required"); + } + public final Type getType() { return type; } @@ -57,4 +62,29 @@ public final Type getType() { public final String toString() { return "TypeMarker{type=" + type + '}'; } + + /** Create a new {@link TypeMarker} instance wrapping the provided {@link Type}. */ + public static TypeMarker of(Type type) { + return new WrappingTypeMarker(type); + } + + private static final class WrappingTypeMarker extends TypeMarker { + private WrappingTypeMarker(Type type) { + super(type); + } + + @Override + public int hashCode() { + return getType().hashCode(); + } + + @Override + public boolean equals(Object other) { + if (other instanceof WrappingTypeMarker) { + WrappingTypeMarker otherMarker = (WrappingTypeMarker) other; + return Objects.equals(getType(), otherMarker.getType()); + } + return false; + } + } }