2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.asdctool.migration.scanner;
23 import org.apache.commons.io.FileUtils;
24 import org.openecomp.sdc.asdctool.migration.core.MigrationException;
27 import java.lang.reflect.Modifier;
32 * scan and instantiate classes of given type in the class path
34 public class ClassScanner {
37 private ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
39 public <T> List<T> getAllClassesOfType(String basePackage, Class<T> ofType) {
40 Collection<File> allClassesInPackage = getAllClassesInPackage(basePackage);
41 List<T> loadedClasses = new ArrayList<>();
42 for (File clazzFile : allClassesInPackage) {
43 Optional<T> instance = loadAndInstantiateClass(getClassReference(clazzFile), ofType);
44 instance.ifPresent(loadedClasses::add);
49 private <T> Optional<T> loadAndInstantiateClass(String classReference, Class<T> ofType) {
51 return instantiateClassOfType(classReference, ofType);
52 }catch (ClassNotFoundException e) {
54 throw new MigrationException(String.format("could not find class %s of type %s. cause: %s", classReference, ofType.toGenericString(), e.getMessage()));
55 } catch (IllegalAccessException e1) {
57 throw new MigrationException(String.format("could not instantiate class %s of type %s. class is not accessible. cause: %s", classReference, ofType.toGenericString(), e1.getMessage()));
58 } catch (InstantiationException e2) {
60 throw new MigrationException(String.format("could not instantiate class %s of type %s. cause: %s", classReference, ofType.toGenericString(), e2.getMessage()));
64 private <T> Optional<T> instantiateClassOfType(String classReference, Class<T> ofType) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
65 String className = classReference.replaceAll(".class$", "").replaceAll(".class", "");
66 Class<?> aClass = classLoader.loadClass(className);
67 if (ofType.isAssignableFrom(aClass) && isInstantiateAbleClass(aClass)){
68 return Optional.of((T) aClass.newInstance());
70 return Optional.empty();
73 private boolean isInstantiateAbleClass(Class<?> clazz) {
74 return !Modifier.isAbstract(clazz.getModifiers()) && !clazz.isEnum() && !clazz.isAnonymousClass() && !clazz.isInterface();
77 private Collection<File> getAllClassesInPackage(String fromPackage) {
78 String path = fromPackage.replace(".", "/");
79 URL resource = classLoader.getResource(path);
80 if (noMigrationTasks(resource)) {
81 return Collections.emptyList();
83 return FileUtils.listFiles(new File(resource.getFile()), new String[]{"class"}, true);
86 private boolean noMigrationTasks(URL resource) {
87 return resource == null;
90 private String getClassReference(File classFile) {
91 String asPackage = classFile.getPath().replace(File.separator, ".");
92 String classes = "classes.";
93 return asPackage.substring(asPackage.indexOf(classes) + classes.length());