Java tutorial
/* * Copyright (C) 2014 Artsem Semianenka (http://art4ul.com/) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.art4ul.loadinstead.bean; import com.art4ul.loadinstead.annotation.LoadInstead; import com.google.common.reflect.ClassPath; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; /** * Created by Artsem Semianenka * Website: http://art4ul.com * 11/25/14. */ class ClassMapper { private Map<Class<?>, Class<?>> classMap; private ClassMapper(List<String> packs) throws IOException { classMap = new HashMap<Class<?>, Class<?>>(); for (String pack : packs) { ClassPath classPath = ClassPath.from(getClass().getClassLoader()); Set<ClassPath.ClassInfo> classInfos = classPath.getTopLevelClasses(pack); for (ClassPath.ClassInfo info : classInfos) { processClass(info.load()); } } } public static ClassMapper load(List<String> packs) throws IOException { return new ClassMapper(packs); } private void processClass(Class<?> loadedClass) { LoadInstead loadInsteadAnnotation = loadedClass.getAnnotation(LoadInstead.class); if (loadInsteadAnnotation != null) { classMap.put(loadInsteadAnnotation.value(), loadedClass); } } public Class<?> getClassToReplace(Class<?> originalClass) { return classMap.get(originalClass); } }