原创

Java常用工具类代码整理——反射工具类

package com.jarvis.base.util;

/**
*   
*
* @Title: ReflectHelper.java
* @Description: 反射工具类
* @author 狂奔的程序猿 http://www.ysxbohui.com
* @date 202287
* @version V1.0   
*/
public class ReflectHelper {

/**
* 提指定的类载入以系统中
*
* @param name 类名称
* @return 类对象
* @throws ClassNotFoundException
*/
public static Class<?> classForName(String name) throws ClassNotFoundException {
try {
return Thread.currentThread().getContextClassLoader().loadClass(name);
}

catch (ClassNotFoundException e) {
e.printStackTrace();
System.err.println("[" + name + "]加载出错");
} catch (SecurityException e) {
e.printStackTrace();
System.err.println("[" + name + "]加载出错");
}
return Class.forName(name);
}

/**
* 根据名称生成指定的对象
*
* @param name 类名称
* @return 具体的对象,若发生异常,则返回null
*/
public static Object objectForName(String name) {
try {
return Class.forName(name).newInstance();
} catch (Exception ex) {
ex.printStackTrace();
System.err.println("[" + name + "]获取对象实例出错");
}
return null;
}
}

本文链接地址:http://www.ysxbohui.com/article/60

正文到此结束