📄 descriptorclassloader.java
字号:
package piy;
import java.io.File;
import java.lang.reflect.*;
/**
* Loads in Descriptor classes, and the classes they are describing.
* @author David Vivash
* @version 1.0.1, 28/11/00
*/
public final class DescriptorClassLoader
{
/**
* Retrieves all of the classes from the supplied directory that can be cast
* to baseClass and secondBaseClass, If baseClass is Object, all of the classes in the directory
* will be returned. The classes should be accessible as "directoryName.className"
* or "directoryName.subDirectoryName.className" etc. That is, the classes in the
* directory specified must belong to a pckage with the directoryName being the
* root package name. It will also load all of the "Descriptor" files in the directory
* and map them to the classes loaded. If a loaded class has no descriptor, a
* StandardDescriptor class will be created for it.
* @param directory the (absolute) path to the directory containing the classes
* @param prefix the (classname) prefix representing the full package name up to the
* final directory which equals the specified directory
* @param baseClass the top level class from which all the returned classes can
* be cast to
* @param secondBaseClass another top level class to which the returned classes can be cast. Set to Object
* if only one class cast is needed and is represented in baseClass
* @param recurse set to true if the classloader should load from subdirectories as well
* @return an array containing all of the valid classes found
* @throws FileNotFoundException if directory is not found or does not represent a directory
*/
public static Descriptor[] getDescriptors(File directory, String prefix, Class baseClass, Class secondBaseClass, boolean recurse) throws java.io.FileNotFoundException
{
Class[] descriptors = PIYClassLoader.getClasses(directory, prefix, Descriptor.class, null, recurse);
Class[] classes = PIYClassLoader.getClasses(directory, prefix, baseClass, secondBaseClass, recurse);
Descriptor[] toReturn = new Descriptor[classes.length];
boolean found = false;
int j=0;
for (int i=0; i<classes.length; i++)
{
found = false;
j=0;
while(!found && j<descriptors.length) {
try{
Method getClass = descriptors[j].getMethod("getDescribedClass", new Class[0]);
if (getClass.invoke(descriptors[j].newInstance(), new Class[0]) == classes[i]) {
found = true;
toReturn[i] = (Descriptor)descriptors[j].newInstance();
}
} catch (Exception e) {
//ignore
}
j++;
}
if (!found) //create standard descriptor for the class
toReturn[i] = new StandardDescriptor(classes[i], classes[i].getName());
}
return toReturn;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -