Skip to content

Commit

Permalink
Don't use assignableFrom when checking super class
Browse files Browse the repository at this point in the history
Instead get all the super class names and output them to a collection. Then we use the output of the super classes to check to see if the super class exists in the collection.
  • Loading branch information
virustotalop committed Apr 16, 2022
1 parent b47edc2 commit 26a9656
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public <T> Collection<Class<? extends T>> collect(String packagePattern,
if (className.startsWith(packagePattern) && className.endsWith(".class")) {
String loadedName = className.replace(".class", "");
Class<?> checkClass = Class.forName(loadedName, false, source.getClassLoader());
if (superClazz.isAssignableFrom(checkClass)
&& !Modifier.isAbstract(checkClass.getModifiers())) {
Collection<String> superClasses = getSuperClassNames(checkClass);
if (!Modifier.isAbstract(checkClass.getModifiers()) &&
superClasses.contains(superClazz.getName())) {
list.add((Class<? extends T>) checkClass);
}
}
Expand All @@ -42,4 +43,18 @@ public <T> Collection<Class<? extends T>> collect(String packagePattern,
}
return list;
}

private Collection<String> getSuperClassNames(Class<?> clazz) {
Collection<String> names = new ArrayList<>();
return this.getSuperClassNamesRecur(clazz, names);
}

private Collection<String> getSuperClassNamesRecur(Class<?> clazz, Collection<String> names) {
Class<?> superClass = clazz.getSuperclass();
if (superClass == null || superClass.getName().equals(Object.class.getName())) {
return names;
}
names.add(superClass.getName());
return this.getSuperClassNamesRecur(superClass, names);
}
}

0 comments on commit 26a9656

Please sign in to comment.