-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from ergon/update_deps
Update dependencies and remove dependency on org.reflections
- Loading branch information
Showing
13 changed files
with
115 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
core/src/main/java/ch/ergon/adam/core/reflection/ReflectionHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package ch.ergon.adam.core.reflection; | ||
|
||
import com.google.common.reflect.ClassPath; | ||
|
||
import java.lang.reflect.Modifier; | ||
import java.util.*; | ||
|
||
import static java.util.stream.Collectors.toSet; | ||
|
||
public class ReflectionHelper { | ||
|
||
public final static Map<String, Set<Class<?>>> classesByPackageCache = new HashMap<>(); | ||
public final static Map<String, Set<String>> resourcesByPathCache = new HashMap<>(); | ||
|
||
|
||
private static Set<Class<?>> findAllClassesForPackage(String packageName) { | ||
if (!classesByPackageCache.containsKey(packageName)) { | ||
try { | ||
Set<Class<?>> classes = ClassPath.from(ClassLoader.getSystemClassLoader()).getAllClasses() | ||
.stream() | ||
.filter(c -> c.getPackageName().startsWith(packageName)) | ||
.map(ClassPath.ClassInfo::getName) | ||
.map(ReflectionHelper::getClass) | ||
.collect(toSet()); | ||
classesByPackageCache.put(packageName, classes); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
return classesByPackageCache.get(packageName); | ||
} | ||
|
||
public static Set<String> findAllRessourcesForPath(String path) { | ||
if (!resourcesByPathCache.containsKey(path)) { | ||
try { | ||
Set<String> resources = ClassPath.from(ClassLoader.getSystemClassLoader()).getResources() | ||
.stream() | ||
.filter(r -> r.getResourceName().startsWith(path)) | ||
.map(ClassPath.ResourceInfo::getResourceName) | ||
.collect(toSet()); | ||
resourcesByPathCache.put(path, resources); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
return resourcesByPathCache.get(path); | ||
} | ||
|
||
private static Class<?> getClass(String className) { | ||
try { | ||
return Class.forName(className); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static <T> Set<Class<? extends T>> findAllSubClasses(String packageName, Class<T> superClass) { | ||
return findAllClassesForPackage(packageName).stream() | ||
.filter(superClass::isAssignableFrom) | ||
.filter(c -> !c.isInterface() && !Modifier.isAbstract(c.getModifiers())) | ||
.map(c -> (Class<? extends T>)c) | ||
.collect(toSet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
5 | ||
4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters