Skip to content

Commit

Permalink
Null check in findSuperClassParameterType
Browse files Browse the repository at this point in the history
  • Loading branch information
bbimber committed Mar 1, 2024
1 parent 12bdb58 commit 9594aef
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,21 @@ public Class<?> findSuperClassParameterType(Object instance)
}

// Based on: https://www.javacodegeeks.com/2013/12/advanced-java-generics-retreiving-generic-type-arguments.html
public static Class<?> findSuperClassParameterType(Object instance, int parameterIndex) {
public static Class<?> findSuperClassParameterType(Object instance, int parameterIndex)
{
Class<?> clazz = instance.getClass();
while (clazz != clazz.getSuperclass()) {
if (clazz == null)
{
throw new IllegalStateException("Class was null for instance: " + instance);
}

while (clazz != null && clazz != clazz.getSuperclass())
{
if (clazz.getGenericSuperclass() instanceof ParameterizedType pt)
{
return (Class<?>) pt.getActualTypeArguments()[parameterIndex];
}


clazz = clazz.getSuperclass();
}

Expand Down

0 comments on commit 9594aef

Please sign in to comment.