1 package org.openecomp.sdc.asdctool.migration.scanner;
3 import org.apache.commons.io.FileUtils;
4 import org.openecomp.sdc.asdctool.migration.core.MigrationException;
7 import java.lang.reflect.Modifier;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Optional;
16 * scan and instantiate classes of given type in the class path
18 public class ClassScanner {
21 private ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
23 public <T> List<T> getAllClassesOfType(String basePackage, Class<T> ofType) {
24 Collection<File> allClassesInPackage = getAllClassesInPackage(basePackage);
25 List<T> loadedClasses = new ArrayList<>();
26 for (File clazzFile : allClassesInPackage) {
27 Optional<T> instance = loadAndInstantiateClass(getClassReference(clazzFile), ofType);
28 instance.ifPresent(loadedClasses::add);
33 private <T> Optional<T> loadAndInstantiateClass(String classReference, Class<T> ofType) {
35 return instantiateClassOfType(classReference, ofType);
36 }catch (ClassNotFoundException e) {
38 throw new MigrationException(String.format("could not find class %s of type %s. cause: %s", classReference, ofType.toGenericString(), e.getMessage()));
39 } catch (IllegalAccessException e1) {
41 throw new MigrationException(String.format("could not instantiate class %s of type %s. class is not accessible. cause: %s", classReference, ofType.toGenericString(), e1.getMessage()));
42 } catch (InstantiationException e2) {
44 throw new MigrationException(String.format("could not instantiate class %s of type %s. cause: %s", classReference, ofType.toGenericString(), e2.getMessage()));
48 private <T> Optional<T> instantiateClassOfType(String classReference, Class<T> ofType) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
49 String className = classReference.replaceAll(".class$", "").replaceAll(".class", "");
50 Class<?> aClass = classLoader.loadClass(className);
51 if (ofType.isAssignableFrom(aClass) && isInstantiateAbleClass(aClass)){
52 return Optional.of((T) aClass.newInstance());
54 return Optional.empty();
57 private boolean isInstantiateAbleClass(Class<?> clazz) {
58 return !Modifier.isAbstract(clazz.getModifiers()) && !clazz.isEnum() && !clazz.isAnonymousClass() && !clazz.isInterface();
61 private Collection<File> getAllClassesInPackage(String fromPackage) {
62 String path = fromPackage.replace(".", "/");
63 URL resource = classLoader.getResource(path);
64 if (noMigrationTasks(resource)) {
65 return Collections.emptyList();
67 return FileUtils.listFiles(new File(resource.getFile()), new String[]{"class"}, true);
70 private boolean noMigrationTasks(URL resource) {
71 return resource == null;
74 private String getClassReference(File classFile) {
75 String asPackage = classFile.getPath().replace(File.separator, ".");
76 String classes = "classes.";
77 return asPackage.substring(asPackage.indexOf(classes) + classes.length());