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;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Optional;
36 * scan and instantiate classes of given type in the class path
38 public class ClassScanner {
41 private ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
43 public <T> List<T> getAllClassesOfType(String basePackage, Class<T> ofType) {
44 Collection<File> allClassesInPackage = getAllClassesInPackage(basePackage);
45 List<T> loadedClasses = new ArrayList<>();
46 for (File clazzFile : allClassesInPackage) {
47 Optional<T> instance = loadAndInstantiateClass(getClassReference(clazzFile), ofType);
48 instance.ifPresent(loadedClasses::add);
53 private <T> Optional<T> loadAndInstantiateClass(String classReference, Class<T> ofType) {
55 return instantiateClassOfType(classReference, ofType);
56 }catch (ClassNotFoundException e) {
58 throw new MigrationException(String.format("could not find class %s of type %s. cause: %s", classReference, ofType.toGenericString(), e.getMessage()));
59 } catch (IllegalAccessException e1) {
61 throw new MigrationException(String.format("could not instantiate class %s of type %s. class is not accessible. cause: %s", classReference, ofType.toGenericString(), e1.getMessage()));
62 } catch (InstantiationException e2) {
64 throw new MigrationException(String.format("could not instantiate class %s of type %s. cause: %s", classReference, ofType.toGenericString(), e2.getMessage()));
68 private <T> Optional<T> instantiateClassOfType(String classReference, Class<T> ofType) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
69 String className = classReference.replaceAll(".class$", "").replaceAll(".class", "");
70 Class<?> aClass = classLoader.loadClass(className);
71 if (ofType.isAssignableFrom(aClass) && isInstantiateAbleClass(aClass)){
72 return Optional.of((T) aClass.newInstance());
74 return Optional.empty();
77 private boolean isInstantiateAbleClass(Class<?> clazz) {
78 return !Modifier.isAbstract(clazz.getModifiers()) && !clazz.isEnum() && !clazz.isAnonymousClass() && !clazz.isInterface();
81 private Collection<File> getAllClassesInPackage(String fromPackage) {
82 String path = fromPackage.replace(".", "/");
83 URL resource = classLoader.getResource(path);
84 if (noMigrationTasks(resource)) {
85 return Collections.emptyList();
87 return FileUtils.listFiles(new File(resource.getFile()), new String[]{"class"}, true);
90 private boolean noMigrationTasks(URL resource) {
91 return resource == null;
94 private String getClassReference(File classFile) {
95 String asPackage = classFile.getPath().replace(File.separator, ".");
96 String classes = "classes.";
97 return asPackage.substring(asPackage.indexOf(classes) + classes.length());