All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nosi.core.webapp.helpers.ReflectionHelper Maven / Gradle / Ivy

Go to download

IGRP Framework is a powerful and highly customizable platform developed by the Operational Nucleus for the Information Society (NOSi) to create web applications, it provides out of box, several modules to make easy to create stand-alone, production-grade web applications: authentication and access-control, business processes automation, reporting, page builder with automatic code generation and incorporation of the Once-Only-Principle, written in Java. IGRP Framework WAR - Contains some keys resources that give UI to IGRP Framework and others supports files.

There is a newer version: 2.0.0.240912-RCM
Show newest version
package nosi.core.webapp.helpers;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import nosi.core.webapp.Core;
import nosi.core.webapp.ReportKey;

/**
 * cvt00957
 * Sep 1, 2020
 */
public class ReflectionHelper {

	public static List> findClassesByInterface(Class interfaceClass, String fromPackage) {
		if (Core.isNull(interfaceClass) || Core.isNull(fromPackage)) {
			return null;
		}
		final List> rVal = new ArrayList>();
		try {
			final Class[] targets = getAllClassesFromPackage(fromPackage);
			if (targets != null) {
				for (Class aTarget : targets) {
					if (aTarget == null) {
						continue;
					} else if (aTarget.equals(interfaceClass)) {
						continue;
					} else if (!interfaceClass.isAssignableFrom(aTarget)) {
						continue;
					} else {
						rVal.add(aTarget);
					}
				}
			}
		} catch (ClassNotFoundException e) {
			return null;
		} catch (IOException e) {
			return null;
		}

		return rVal;
	}
	
	public static Class[] getAllClassesFromPackage(final String packageName)
			throws ClassNotFoundException, IOException {
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		assert classLoader != null;
		String path = packageName.replace('.', '/');
		Enumeration resources = classLoader.getResources(path);
		List dirs = new ArrayList();
		while (resources.hasMoreElements()) {
			URL resource = resources.nextElement();
			dirs.add(new File(resource.getFile()));
		}
		ArrayList classes = new ArrayList();
		for (File directory : dirs) {
			classes.addAll(findClasses(directory, packageName));
		}
		return classes.toArray(new Class[classes.size()]);
	}
	
	public static List> findClasses(File directory, String packageName) throws ClassNotFoundException {
		List> classes = new ArrayList>();
		if (!directory.exists()) {
			return classes;
		}
		File[] files = directory.listFiles();
		for (File file : files) {
			try {
				if (file.isDirectory()) {
					assert !file.getName().contains(".");
					classes.addAll(findClasses(file, packageName + "." + file.getName()));
				} else if (file.getName().endsWith(".class")) {
					classes.add(
							Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
				}
			}catch (NoClassDefFoundError e) {
				return classes;
			}
		}
		return classes;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy