com.github.yoojia.next.lang.ClassLoader Maven / Gradle / Ivy
package com.github.yoojia.next.lang;
import com.github.yoojia.next.NextRequest;
import com.github.yoojia.next.NextResponse;
import java.lang.reflect.Method;
/**
* @author YOOJIA.CHEN ([email protected])
*/
public final class ClassLoader {
private final java.lang.ClassLoader mClassLoader;
public ClassLoader() {
mClassLoader = Thread.currentThread().getContextClassLoader();
}
public Class> load(String className){
try {
return mClassLoader.loadClass(className);
} catch (Exception e) {
return null;
}
}
public void checkConstructor(Class> clazz, String name){
try {
clazz.getConstructor(); // 在自动生成对象时,需要一个无参数构造方法
} catch (NoSuchMethodException e) {
throw new RuntimeException("A " + name + " MUST has a public NON-PARAMS constructor !", e);
}
}
public T newClassByName(String className){
// 为内建模块class缩写恢复原类名
if( ! className.contains(".")) {
className = "com.github.yoojia.next." + className;
}
try {
@SuppressWarnings("unchecked")
final Class clazz = (Class) mClassLoader.loadClass(className);
return clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException("Cannot instance class: " + className, e);
}
}
public boolean isSignatureMatched(Method method){
final Class>[] types = method.getParameterTypes();
if(types.length < 2){
return false;
}
boolean req = false, res = false;
for (Class> type : types) {
req |= NextRequest.class.equals(type);
res |= NextResponse.class.equals(type);
}
return req && res;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy