From 26a96564bcf780f553d73dce678c60c0c5825b50 Mon Sep 17 00:00:00 2001 From: virustotalop Date: Sat, 16 Apr 2022 16:58:01 -0700 Subject: [PATCH] Don't use assignableFrom when checking super class 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. --- .../classscanner/JarClassScanner.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/ravenlab/classscanner/JarClassScanner.java b/src/main/java/com/github/ravenlab/classscanner/JarClassScanner.java index 080749e..e0ea14f 100644 --- a/src/main/java/com/github/ravenlab/classscanner/JarClassScanner.java +++ b/src/main/java/com/github/ravenlab/classscanner/JarClassScanner.java @@ -30,8 +30,9 @@ public Collection> 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 superClasses = getSuperClassNames(checkClass); + if (!Modifier.isAbstract(checkClass.getModifiers()) && + superClasses.contains(superClazz.getName())) { list.add((Class) checkClass); } } @@ -42,4 +43,18 @@ public Collection> collect(String packagePattern, } return list; } + + private Collection getSuperClassNames(Class clazz) { + Collection names = new ArrayList<>(); + return this.getSuperClassNamesRecur(clazz, names); + } + + private Collection getSuperClassNamesRecur(Class clazz, Collection 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); + } } \ No newline at end of file