[SDC] rebase 1710 code
[sdc.git] / asdctool / src / test / java / org / openecomp / sdc / asdctool / migration / scanner / ClassScanner.java
1 package org.openecomp.sdc.asdctool.migration.scanner;
2
3 import org.apache.commons.io.FileUtils;
4 import org.openecomp.sdc.asdctool.migration.core.MigrationException;
5
6 import java.io.File;
7 import java.lang.reflect.Modifier;
8 import java.net.URL;
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;
14
15 /**
16  * scan and instantiate classes of given type in the class path
17  */
18 public class ClassScanner {
19
20
21     private ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
22
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);
29         }
30         return loadedClasses;
31     }
32
33     private <T> Optional<T> loadAndInstantiateClass(String classReference, Class<T> ofType)  {
34         try {
35             return instantiateClassOfType(classReference, ofType);
36         }catch (ClassNotFoundException e) {
37             //log
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) {
40             //log
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) {
43             //log
44             throw new MigrationException(String.format("could not instantiate class %s of type %s. cause: %s", classReference, ofType.toGenericString(), e2.getMessage()));
45         }
46     }
47
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());
53         }
54         return Optional.empty();
55     }
56
57     private boolean isInstantiateAbleClass(Class<?> clazz) {
58         return !Modifier.isAbstract(clazz.getModifiers()) && !clazz.isEnum() && !clazz.isAnonymousClass() && !clazz.isInterface();
59     }
60
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();
66         }
67         return FileUtils.listFiles(new File(resource.getFile()), new String[]{"class"}, true);
68     }
69
70     private boolean noMigrationTasks(URL resource) {
71         return resource == null;
72     }
73
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());
78     }
79
80
81 }