上一章节说了反射中学习到的知识,现在通过实践来写一个动态创建类,以及获取动态设值的方法
public class ReflectProUtils { /** * 获取泛型中的类型 * 例如: List<String> 获取出的类型为 [String.class] * 说明: * 需要获取泛型的,必须通过继承等方式重新构建一个新类,传递泛型的类型 * * 可查考 * @throws ClassNotFoundException * @see BaseMapper * */ @SuppressWarnings("rawtypes") public static List<Class> getGenericsClass(Class thisClass) throws ClassNotFoundException{ //返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的直接超类的 Type。 Type genType = thisClass.getGenericSuperclass(); List<Class> list= new ArrayList<Class>(); if (!(genType instanceof ParameterizedType)) { list.add(Object.class); return list; } //返回表示此类型实际类型参数的Type对象的数组 Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); for(Type type : params){ list.add( type2Class(type) ); } return list; } /** * Type类型转换为Class类型 * */ public static Class type2Class(Type type) throws ClassNotFoundException{ String clsStr = type.toString(); String classStrPath = clsStr.substring(clsStr.indexOf(" ")+1 , clsStr.length()); if(int.class.getName().equals(classStrPath)) return int.class; else if(Constants.BYTE_NAME.equals(classStrPath)) return byte.class; else if(Constants.SHORT_NAME.equals(classStrPath)) return short.class; else if(Constants.LONG_NAME.equals(classStrPath)) return long.class; else if(Constants.FLOAT_NAME.equals(classStrPath)) return float.class; else if(Constants.DOUBLE_NAME.endsWith(classStrPath)) return double.class; else if(Constants.CHAR_NAME.equals(classStrPath)) return char.class; else if(Constants.BOOLEAN_NAME.equals(classStrPath)) return boolean.class; return Class.forName(classStrPath); } /** * 获取类中的所有字段,包括私有字段 * @param Class * @return {@link Field} * */ @SuppressWarnings("rawtypes") public static Field[] getAllFields(Class cls){ return cls.getDeclaredFields(); } /** * 只获取类中的公共方法,被public修饰的方法 * @param Class * @return {@link Field} * */ @SuppressWarnings("rawtypes") public static Field[] getPublicFields(Class cls){ return cls.getFields(); } /** * 获取当前类下的所有get/set方法 * @param Class * @return {@link Map} <方法名,方法> * */ @SuppressWarnings("rawtypes") public static Map<String,Method> getAllGSMethods(Class cls){ Map<String,Method> getSetMethodMap = new HashMap<String,Method>(); Method[] methods = cls.getDeclaredMethods(); for(Method method : methods){ String name = method.getName(); if(name.contains("get") || name.contains("set")) getSetMethodMap.put(name, method); } return getSetMethodMap; } /** * 获取所有的方法 * @param cls * @return {@link Method} * */ @SuppressWarnings("rawtypes") public static Method[] getAllMethods(Class cls){ return cls.getDeclaredMethods(); } /** * 只获取公开的方法 * @param cls * @return {@link Method} * */ @SuppressWarnings("rawtypes") public static Method[] getPublicMethods(Class cls){ return cls.getMethods(); } /** * 为对象执行set方法,进行赋值 * set_T_Properties_value * * @param fields 对象中的字段 * @param getSetMap get/set方法 * @param T 需要赋值的对象 * @param args 参数 * @throws Exception * */ public static void setTProVal(Field field ,Object T,Object arg) throws Exception{ String name = field.getName(); try { Method method = T.getClass().getMethod( "set" + StringUtil.toUpperCaseFirstOne(name), arg.getClass()); if(method != null){ method.invoke(T, arg); } } catch (NoSuchMethodException e) { System.out.println("找不到对应参数类型的方法..请核对方法名和类型"); } } public static <T>void setTProVal(T bean,Map<String,T> param) throws Exception{ Field[] fields = getAllFields(bean.getClass()); for(Field field : fields){ field.setAccessible(true); String name = field.getName(); Object val = param.get(name); if(val != null){ setTProVal(field, bean, val); } } } /** * 获取一个对象下面的字段对应的数据 * @param Field 字段 * @param obj 对象 * @throws Exception * */ public static <T>Object getTProVal(Field field,T obj ) throws Exception{ String name = field.getName(); try { Method method = obj.getClass().getMethod("get"+StringUtil.toUpperCaseFirstOne(name)); if(method != null){ return method.invoke(obj, null); } } catch (NoSuchMethodException e) { System.out.println("找不到对应参数类型的方法..请核对方法名和类型"); } return null; } /** * 获取Field的类型 * @return * @throws ClassNotFoundException * */ public static Class getFieldType(Field field) throws ClassNotFoundException{ return type2Class(field.getType()); } /** * 判断某个类下是否有某个方法存在 * @param Class * @param 方法名 * */ public static boolean hasMethod(Class cls , String methodName){ Map<String,Method> methodMap = getAllGSMethods(cls); if(methodMap == null) return false; else{ Method method = methodMap.get(methodName); return method != null ? true :false; } } public static void main(String[] args) throws ClassNotFoundException { System.out.println(int.class.getName()); } }
相关推荐
IDEA插件之GET/SET自动生成带模版注释插件,很方便的插件。 使用方法在下面 https://blog.csdn.net/qq_41961113/article/details/80079185
uc_pm_blackls_set uc_pm_blackls_add uc_pm_blackls_delete //好友 uc_friend_add uc_friend_delete uc_friend_totalnum uc_friend_ls //积分 uc_credit_exchange_request //头像 uc_avatar uc_check_avatar ...
void sha256_get(uint8_t hash[32], const uint8_t *message, int length);/*此函数用于对消息计算摘要值,输入任意大小消息,输出32字节摘要值*/ void hmac_sha256_get(uint8_t digest[32], uint8_t *message, int...
java版的SNMP_get,SNMP_walk,SNMP_getbulk,详细举例代码完整版!!
lr_set_transaction_instance_status/设置事务实例的状态 lr_set_transaction_status/设置打开事务的状态 lr_set_transaction_status_by_name/设置事务的状态 lr_start_sub_transaction/标记子事务的开始 lr_start_...
一种基于FPGA的UPP接口代码实现connect_debug_port u_ila_0/probe45 [get_nets [list tx2_lock]] create_debug_port u_ila_0 probe set_property PROBE_TYPE DATA_AND_TRIGGER [get_debug_ports u_ila_0/probe46] ...
把下载的jar包放在 Maven仓库的 org\mybatis\generator\mybatis-generator-core\1.3.5 下面即可,然后在generatorConfig配置文件中加上 <property name="hasLombok" value="true"/> </plugin> 即可
eclipse 的GET/SET对字段的注释自动添加
smnp get/set方法 target PDU.GETBULK request.add(new VariableBinding(new OID("1.3.6.1.2.1.10000.3.1"))); ResponseEvent responseEvent = snmp.send(request, target); 相应的注释 请参照 注释 修改...
博文Unity-Get/Set属性访问器详解源码!!!!!!!!!!
51单片机实现的485通讯程序:#ifndef __485_C__ #define __485_C__ #include #include #define unsigned char uchar #define unsigned int uint /* 通信命令 */ #define __ACTIVE_ 0x01 // 主机询问从机是否...
Idea中生成带注释的get/set方法插件,配置超级超级超级超级超级超级超级超级简单方便,亲测有效好用
NULL 博文链接:https://radiumxie.iteye.com/blog/1198825
编写pojo类时,eclipse 自动生成get/set方法会自动加上字段注释的内容,本文件基于eclipse4.7开发,其他版本未测试
public static String REQUEST_METHOD_GET = "GET"; // 媒体类型 public static String MIME_TYPE_FORM = "application/x-www-form-urlencoded;charset=utf-8"; public static String MIME_TYPE_XML = ...
Java 反射创建get set方法及反射方法的调用
function get_tags_arr($title) { require(APP_ROOT.'/pscws4.class.php'); $pscws = new PSCWS4(); $pscws->set_dict(APP_ROOT.'/scws/dict.utf8.xdb'); $pscws->set_rule(APP_ROOT.'/scws/rules.utf8...
主要介绍了探讨:JavaScript ECAMScript5 新特性之get/set访问器 的相关资料,需要的朋友可以参考下