网络字符串处理工具类
来源:互联网 发布:windows 10 一周年 编辑:程序博客网 时间:2024/06/15 20:58
/**
* Date:2017年10月15日下午5:09:57
* Copyright (c) 2017, czz1990520@163.com All Rights Reserved.
*
*/
/**
* @Description: TODO
* @author 程昭昭 czz1990520@163.com
* @date 2017年10月15日 下午5:09:57
*/
package com.czz.ehzc.tools;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
* ClassName:ReflectionUtils <br/>
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON. <br/>
* Date: 2017年10月15日 下午5:09:57 <br/>
* @author czz19
* @version
* @since JDK 1.8
* @see
*/
/**
* @Description: TODO
* @author czz1990520@163.com 程昭昭
* @date 2017年10月15日 下午5:09:57
*
*/
public class ReflectionUtils {
/**
* 通过反射, 获得定义 Class 时声明的父类的泛型参数的类型 如: public EmployeeDao extends
* BaseDao<Employee, String>
*
* @param clazz
* @param index
* @return
*/
@SuppressWarnings("rawtypes")
public static Class getSuperClassGenricType(Class clazz, int index) {
Type genType = clazz.getGenericSuperclass();
if (!(genType instanceof ParameterizedType)) {
return Object.class;
}
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
if (index >= params.length || index < 0) {
return Object.class;
}
if (!(params[index] instanceof Class)) {
return Object.class;
}
return (Class) params[index];
}
/**
* 通过反射, 获得 Class 定义中声明的父类的泛型参数类型 如: public EmployeeDao extends
* BaseDao<Employee, String>
*
* @param <T>
* @param clazz
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> Class<T> getSuperGenericType(Class clazz) {
return getSuperClassGenricType(clazz, 0);
}
/**
* 循环向上转型, 获取对象的 DeclaredMethod
*
* @param object
* @param methodName
* @param parameterTypes
* @return
*/
public static Method getDeclaredMethod(Object object, String methodName, Class<?>[] parameterTypes) {
for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass
.getSuperclass()) {
try {
// superClass.getMethod(methodName, parameterTypes);
return superClass.getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {
// Method 不在当前类定义, 继续向上转型
}
// ..
}
return null;
}
/**
* 使 filed 变为可访问
*
* @param field
*/
public static void makeAccessible(Field field) {
if (!Modifier.isPublic(field.getModifiers())) {
field.setAccessible(true);
}
}
/**
* 循环向上转型, 获取对象的 DeclaredField
*
* @param object
* @param filedName
* @return
*/
public static Field getDeclaredField(Object object, String filedName) {
for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass
.getSuperclass()) {
try {
Field field = null;
// makeAccessible(field);
field = superClass.getDeclaredField(filedName.toLowerCase());
return field;
} catch (NoSuchFieldException e) {
// Field 不在当前类定义, 继续向上转型
e.printStackTrace();
}
}
return null;
}
/**
* 直接调用对象方法, 而忽略修饰符(private, protected)
*
* @param object
* @param methodName
* @param parameterTypes
* @param parameters
* @return
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static Object invokeMethod(Object object, String methodName, Class<?>[] parameterTypes, Object[] parameters)
throws InvocationTargetException {
Method method = getDeclaredMethod(object, methodName, parameterTypes);
if (method == null) {
throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]");
}
method.setAccessible(true);
try {
return method.invoke(object, parameters);
} catch (IllegalAccessException e) {
System.out.println("不可能抛出的异常");
}
return null;
}
/**
* 直接设置对象属性值, 忽略 private/protected 修饰符, 也不经过 setter
*
* @param object
* @param fieldName
* @param value
*/
public static void setFieldValue(Object object, String fieldName, Object value) {
Field field = getDeclaredField(object, fieldName);
if (field == null) {
throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
}
makeAccessible(field);
try {
//System.out.println(value.getClass().toString());
if(value.getClass().toString().equals("class java.math.BigDecimal")) {
value = Integer.parseInt(value.toString());
}
field.set(object, value);
} catch (IllegalAccessException e) {
System.out.println("不可能抛出的异常");
}
}
/**
* 直接读取对象的属性值, 忽略 private/protected 修饰符, 也不经过 getter
*
* @param object
* @param fieldName
* @return
*/
public static Object getFieldValue(Object object, String fieldName) {
Field field = getDeclaredField(object, fieldName);
// @test
System.out.println(field);
if (field == null)
throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
makeAccessible(field);
Object result = null;
try {
result = field.get(object);
} catch (IllegalAccessException e) {
System.out.println("不可能抛出的异常");
}
return result;
}
}
* Date:2017年10月15日下午5:09:57
* Copyright (c) 2017, czz1990520@163.com All Rights Reserved.
*
*/
/**
* @Description: TODO
* @author 程昭昭 czz1990520@163.com
* @date 2017年10月15日 下午5:09:57
*/
package com.czz.ehzc.tools;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
* ClassName:ReflectionUtils <br/>
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON. <br/>
* Date: 2017年10月15日 下午5:09:57 <br/>
* @author czz19
* @version
* @since JDK 1.8
* @see
*/
/**
* @Description: TODO
* @author czz1990520@163.com 程昭昭
* @date 2017年10月15日 下午5:09:57
*
*/
public class ReflectionUtils {
/**
* 通过反射, 获得定义 Class 时声明的父类的泛型参数的类型 如: public EmployeeDao extends
* BaseDao<Employee, String>
*
* @param clazz
* @param index
* @return
*/
@SuppressWarnings("rawtypes")
public static Class getSuperClassGenricType(Class clazz, int index) {
Type genType = clazz.getGenericSuperclass();
if (!(genType instanceof ParameterizedType)) {
return Object.class;
}
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
if (index >= params.length || index < 0) {
return Object.class;
}
if (!(params[index] instanceof Class)) {
return Object.class;
}
return (Class) params[index];
}
/**
* 通过反射, 获得 Class 定义中声明的父类的泛型参数类型 如: public EmployeeDao extends
* BaseDao<Employee, String>
*
* @param <T>
* @param clazz
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> Class<T> getSuperGenericType(Class clazz) {
return getSuperClassGenricType(clazz, 0);
}
/**
* 循环向上转型, 获取对象的 DeclaredMethod
*
* @param object
* @param methodName
* @param parameterTypes
* @return
*/
public static Method getDeclaredMethod(Object object, String methodName, Class<?>[] parameterTypes) {
for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass
.getSuperclass()) {
try {
// superClass.getMethod(methodName, parameterTypes);
return superClass.getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {
// Method 不在当前类定义, 继续向上转型
}
// ..
}
return null;
}
/**
* 使 filed 变为可访问
*
* @param field
*/
public static void makeAccessible(Field field) {
if (!Modifier.isPublic(field.getModifiers())) {
field.setAccessible(true);
}
}
/**
* 循环向上转型, 获取对象的 DeclaredField
*
* @param object
* @param filedName
* @return
*/
public static Field getDeclaredField(Object object, String filedName) {
for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass
.getSuperclass()) {
try {
Field field = null;
// makeAccessible(field);
field = superClass.getDeclaredField(filedName.toLowerCase());
return field;
} catch (NoSuchFieldException e) {
// Field 不在当前类定义, 继续向上转型
e.printStackTrace();
}
}
return null;
}
/**
* 直接调用对象方法, 而忽略修饰符(private, protected)
*
* @param object
* @param methodName
* @param parameterTypes
* @param parameters
* @return
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static Object invokeMethod(Object object, String methodName, Class<?>[] parameterTypes, Object[] parameters)
throws InvocationTargetException {
Method method = getDeclaredMethod(object, methodName, parameterTypes);
if (method == null) {
throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]");
}
method.setAccessible(true);
try {
return method.invoke(object, parameters);
} catch (IllegalAccessException e) {
System.out.println("不可能抛出的异常");
}
return null;
}
/**
* 直接设置对象属性值, 忽略 private/protected 修饰符, 也不经过 setter
*
* @param object
* @param fieldName
* @param value
*/
public static void setFieldValue(Object object, String fieldName, Object value) {
Field field = getDeclaredField(object, fieldName);
if (field == null) {
throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
}
makeAccessible(field);
try {
//System.out.println(value.getClass().toString());
if(value.getClass().toString().equals("class java.math.BigDecimal")) {
value = Integer.parseInt(value.toString());
}
field.set(object, value);
} catch (IllegalAccessException e) {
System.out.println("不可能抛出的异常");
}
}
/**
* 直接读取对象的属性值, 忽略 private/protected 修饰符, 也不经过 getter
*
* @param object
* @param fieldName
* @return
*/
public static Object getFieldValue(Object object, String fieldName) {
Field field = getDeclaredField(object, fieldName);
// @test
System.out.println(field);
if (field == null)
throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
makeAccessible(field);
Object result = null;
try {
result = field.get(object);
} catch (IllegalAccessException e) {
System.out.println("不可能抛出的异常");
}
return result;
}
}
阅读全文
0 0
- 网络字符串处理工具类
- 工具类:字符串处理
- 字符串处理工具类
- 字符串处理工具类
- 工具类--字符串处理
- String字符串处理工具类
- JAVA字符串处理工具类
- 工具类:处理网络请求的回应
- java字符串日期处理工具类
- java工具类(处理字符串空)
- 字符串处理工具
- 常用字符串处理工具
- java字符串处理工具
- Java字符串处理工具
- 字符串处理高效工具(Java)
- HTTP工具类(网络中获取JSON字符串)
- Java工具类TextUtil(可以进行字符串处理的)
- Java敏感字符串处理工具类(河蟹专用)
- 定义一个圆类(Circle)
- Linux的SOCKET编程详解
- Qt的初步使用
- 【51Nod】1052 最大M子段和 DP
- HDU5536 Chip Factory(Trie树,二进制)
- 网络字符串处理工具类
- Android中Java代码与JavaScript交互
- 第八周项目4稀疏矩阵的三元组表示的实现及应用(2)
- Codeforces Round #442 (Div. 2) A. Alex and broken contest
- 在 python 字符串中,如何检测最后一个数字
- 从零开书学算法(导论)之归并排序
- nmap端口扫描+邮件告警
- JSP,Enum,switch( XX )
- md5工具类