Skip to content

Commit

Permalink
Add specific exception type to indicate unresolved type to allow easi…
Browse files Browse the repository at this point in the history
…er catching of specific errors.
  • Loading branch information
raphw committed Jul 27, 2021
1 parent f82339e commit 520339d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
38 changes: 37 additions & 1 deletion byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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

Expand All @@ -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
Expand Down

0 comments on commit 520339d

Please sign in to comment.