io.github.rmuhamedgaliev.ClassFinder Maven / Gradle / Ivy
/*
* Copyright 2015 Rinat Muhamedgaliev
*
* 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 io.github.rmuhamedgaliev;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* Follow class provide ability retrieve classes from jar directory and find
* extended classes
*
* @author Rinat Muhamedgaliev
*/
public class ClassFinder {
// Path of get jars for build list files
private final static Path PLUGIN_PATH = Paths.get("plugins/");
private ArrayList pluginList = new ArrayList();
/**
* Find and build plugin list
* @param klass any {@link Class} for search extended classes
* @return {@link ArrayList} of classes extended of klass
* @throws Exception
*
* - When not found {@link ClassFinder#PLUGIN_PATH} in working directory
* - If can't access to classes from jar file
*
*/
public ArrayList loadPlugins(Class klass) throws Exception {
listFiles(PLUGIN_PATH).forEach(p -> {
try {
getPluginList(p.toFile(), klass);
} catch (Exception e) {
e.printStackTrace();
}
});
return pluginList;
}
/**
* Simple recursive retrieve directory
* @param path path of start get structure
* @return labda based {@link Stream} of {@link Path} objects
*/
private static Stream listFiles(Path path) {
if (Files.isDirectory(path)) {
try {
return Files.list(path).flatMap(ClassFinder::listFiles);
} catch (Exception e) {
return Stream.empty();
}
} else {
return Stream.of(path);
}
}
/**
* Follow method retrieve classes from jar file and find all extended classes
*
* @param file jar archived file with compiled classes
* @param klass pattern class for find extended classes
* @throws Exception
*
* - When not found {@link ClassFinder#PLUGIN_PATH} in working directory
* - If can't access to classes from jar file
*
*/
private void getPluginList(File file, Class klass) throws Exception {
if (file.isFile()) {
Class pluginClass;
ZipInputStream zip = new ZipInputStream(new FileInputStream(file));
for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
String className = entry.getName().replace('/', '.');
className = className.substring(0, className.length() - ".class".length());
URL[] urls = {new URL("jar:" + file.toURI().toURL() + "!/")};
URLClassLoader cl = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
pluginClass = cl.loadClass(className);
if (pluginClass != null && pluginClass.getSuperclass() != null && pluginClass.getSuperclass().equals(klass)) {
pluginList.add(pluginClass.newInstance());
}
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy