I'm looking to instantiate and map all classes in my class path of a given type. I've done the scan using the Reflections library, and got a list of classes that were loaded. Then, when I'm mapping them, I'm checking for conflicts (the key is the simple class name). When that happens I would like to throw an exception, pointing out the fully qualified name of the conflicting classes, as well as the path they were loaded from (for instance in which JARs one might find each). I realize one may load a class from memory as well, but assuming I was loading it from a JAR on disk, I wonder if there's any way of getting that path... See code snippet below:
if (isConflictingFactoryRegistered(key, myFactory)) {
Class<? extends MyFactory> class1 = factoryMap.get(key).getClass();
Class<? extends MyFactory> class2 = myFactory.getClass();
try {
throw new RuntimeException("Found two conflicting factories for " + key
+ ": 1. " + class1 + " in " + getClass().getResource(class1.getSimpleName()+".class").toURI() +
" 2. " + class2 + " in " + getClass().getResource(class2.getSimpleName()+".class").toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
} else {
factoryMap.put(myFactory.getName(), myFactory);
}
Aucun commentaire:
Enregistrer un commentaire