From 520339da300b290b2791ea3c668aac682dcd9e64 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Tue, 27 Jul 2021 20:05:33 +0200 Subject: [PATCH] Add specific exception type to indicate unresolved type to allow easier catching of specific errors. --- .../java/net/bytebuddy/pool/TypePool.java | 38 ++++++++++++++++++- .../pool/TypePoolResolutionTest.java | 12 ++++-- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java b/byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java index 85eadb16c40..652edb3406e 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java @@ -89,6 +89,7 @@ interface Resolution { * of the returned type description. * * @return The type description that is represented by this resolution. + * @throws NoSuchTypeException If this resolution is unresolved. */ TypeDescription resolve(); @@ -158,7 +159,42 @@ public boolean isResolved() { * {@inheritDoc} */ public TypeDescription resolve() { - throw new IllegalStateException("Cannot resolve type description for " + name); + throw new NoSuchTypeException(name); + } + } + + /** + * An exception that indicates that a {@link TypePool} could not resolve a {@link TypeDescription} for a given name. + */ + class NoSuchTypeException extends IllegalStateException { + + /** + * The serial version UID. + */ + private static final long serialVersionUID = 1L; + + /** + * The name of the type that could not be resolved. + */ + private final String name; + + /** + * Creates a new exception to indicate an unresolved type. + * + * @param name The name of the type that could not be resolved. + */ + public NoSuchTypeException(String name) { + super("Cannot resolve type description for " + name); + this.name = name; + } + + /** + * Returns the name of the type that could not be resolved. + * + * @return The name of the type that could not be resolved. + */ + public String getName() { + return name; } } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/pool/TypePoolResolutionTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/pool/TypePoolResolutionTest.java index b4708b8acb9..f3afffc9b65 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/pool/TypePoolResolutionTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/pool/TypePoolResolutionTest.java @@ -3,10 +3,10 @@ import net.bytebuddy.description.type.TypeDescription; import org.junit.Test; +import static junit.framework.TestCase.fail; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -21,11 +21,15 @@ public void testSimpleResolution() throws Exception { assertThat(new TypePool.Resolution.Simple(typeDescription).resolve(), is(typeDescription)); } - @Test(expected = IllegalStateException.class) + @Test public void testIllegalResolution() throws Exception { assertThat(new TypePool.Resolution.Illegal(FOO).isResolved(), is(false)); - new TypePool.Resolution.Illegal(FOO).resolve(); - fail(); + try { + new TypePool.Resolution.Illegal(FOO).resolve(); + fail(); + } catch (TypePool.Resolution.NoSuchTypeException exception) { + assertThat(exception.getName(), is(FOO)); + } } @Test