前言:
如今朋友们对“java调用静态方法”都比较讲究,小伙伴们都想要了解一些“java调用静态方法”的相关内容。那么小编在网络上网罗了一些有关“java调用静态方法””的相关资讯,希望朋友们能喜欢,兄弟们一起来学习一下吧!说明
参考内容:
1、反射机制,调用属性(静态&非静态);2、反射机制,调用构造方法(静态&非静态)。
getter/setter方法
定义一些方法来操作成员变量,每个方法都必须有一个有意义的名称,所以就有了getter-setter方法,getter-setter方法格式和写法都是固定的,所以只要有getter-setter方法就不用煞费心思的去想方法名称了,解决了起名字难问题。并且getter-setter方法还是程序员之间的一种规范,给属性赋值立刻就会想到getter-setter方法,降低了程序员之间的沟通成本。
代码案例
工具类:
import org.apache.commons.lang3.StringUtils;import java.lang.reflect.Method;import java.util.List;public class ReflectCallMethod { /** * @param entryClass * @param methodName * @param <T> * @return */ public static <T> T invokeForStatic(Class entryClass, String methodName) { if (entryClass == null) { return null; } T result = null; try { // 反射获取 Method method = entryClass.getDeclaredMethod(methodName); method.setAccessible(true); return (T) method.invoke(entryClass); } catch (Exception e) { e.printStackTrace(); } return result; } /** * @param entryClass * @param methodName * @param classArgs * @param valueArgs * @param <T> * @return */ public static <T> T invokeForStatic(Class entryClass, String methodName, Class[] classArgs, Object[] valueArgs) { if (entryClass == null) { return null; } T result = null; try { Method method = entryClass.getDeclaredMethod(methodName, classArgs); method.setAccessible(true); return (T) method.invoke(entryClass, valueArgs); } catch (Exception e) { e.printStackTrace(); } return result; } /** * @param entryClass * @param methodName * @param initArgs * @param <T> * @return */ public static <T> T invokeForStatic(Class entryClass, String methodName, List<Object> initArgs) { if (entryClass == null) { return null; } T result = null; try { Class[] classArgs = new Class[initArgs.size()]; Object[] valueArgs = new Object[initArgs.size()]; for (int i = 0; i < initArgs.size(); i++) { classArgs[i] = initArgs.get(i).getClass(); valueArgs[i] = initArgs.get(i); } Method method = entryClass.getDeclaredMethod(methodName, classArgs); method.setAccessible(true); return (T) method.invoke(entryClass, valueArgs); } catch (Exception e) { e.printStackTrace(); } return result; } /** * @param entryObject * @param methodName * @param <T> * @return */ public static <T> T invokeForInstance(Object entryObject, String methodName) { if (entryObject == null) { return null; } T result = null; try { // 反射获取 Method method = entryObject.getClass().getDeclaredMethod(methodName); method.setAccessible(true); return (T) method.invoke(entryObject); } catch (Exception e) { e.printStackTrace(); } return result; } /** * @param entryObject * @param methodName * @param classArgs * @param valueArgs * @param <T> * @return */ public static <T> T invokeForInstance(Object entryObject, String methodName, Class[] classArgs, Object[] valueArgs) { if (entryObject == null) { return null; } T result = null; try { Method method = entryObject.getClass().getDeclaredMethod(methodName, classArgs); method.setAccessible(true); return (T) method.invoke(entryObject, valueArgs); } catch (Exception e) { e.printStackTrace(); } return result; } /** * @param entryObject * @param methodName * @param initArgs * @param <T> * @return */ public static <T> T invokeForInstance(Object entryObject, String methodName, List<Object> initArgs) { if (entryObject == null) { return null; } T result = null; try { Class[] classArgs = new Class[initArgs.size()]; Object[] valueArgs = new Object[initArgs.size()]; for (int i = 0; i < initArgs.size(); i++) { classArgs[i] = initArgs.get(i).getClass(); valueArgs[i] = initArgs.get(i); } Method method = entryObject.getClass().getDeclaredMethod(methodName, classArgs); method.setAccessible(true); return (T) method.invoke(entryObject, valueArgs); } catch (Exception e) { e.printStackTrace(); } return result; } //=============================================================================================// // getter and setter //=============================================================================================// /** * @param entryObject * @param methodName * @param <E> * @return */ public static <E> E getInvoke(Object entryObject, String methodName) { if (entryObject == null || StringUtils.isEmpty(methodName)) { return null; } try { Method method = entryObject.getClass().getDeclaredMethod(methodName); method.setAccessible(true); Object returnValue = method.invoke(entryObject); return (E) returnValue; } catch (Exception ex) { ex.printStackTrace(); } return null; } /** * @param entryObject * @param fieldName * @param <E> * @return */ public static <E> E getInvokeByFieldName(Object entryObject, String fieldName) { if (entryObject == null || StringUtils.isEmpty(fieldName)) { return null; } try { String methodName = "get" + StringUtils.capitalize(fieldName); Method method = entryObject.getClass().getDeclaredMethod(methodName); method.setAccessible(true); Object returnValue = method.invoke(entryObject); return (E) returnValue; } catch (Exception ex) { ex.printStackTrace(); } return null; } /** * @param entryObject * @param methodName * @param <E> * @return */ public static <E> E setInvoke(Object entryObject, String methodName, Object object) { if (entryObject == null || StringUtils.isEmpty(methodName)) { return null; } try { Method method = entryObject.getClass().getDeclaredMethod(methodName, object.getClass()); method.setAccessible(true); Object returnValue = method.invoke(entryObject, object); return (E) returnValue; } catch (Exception ex) { ex.printStackTrace(); } return null; } /** * @param entryObject * @param fieldName * @param <E> * @return */ public static <E> E setInvokeByFieldName(Object entryObject, String fieldName, Object object) { if (entryObject == null || StringUtils.isEmpty(fieldName)) { return null; } try { String methodName = "set" + StringUtils.capitalize(fieldName); Method method = entryObject.getClass().getDeclaredMethod(methodName, object.getClass()); method.setAccessible(true); Object returnValue = method.invoke(entryObject, object); return (E) returnValue; } catch (Exception ex) { ex.printStackTrace(); } return null; } //=============================================================================================// // the end //=============================================================================================//}
测试类:
import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import java.io.Serializable;import java.util.Date;import java.util.HashMap;import java.util.Map;@Data@AllArgsConstructor@NoArgsConstructorpublic class User implements Serializable, Cloneable { private Integer id; private String name; private Date createTime; /** * 静态方法1 */ public static void testCall1() { System.out.println("testCall.1"); } /** * 静态方法2 * @param paramsMap * @return */ public static Map<String, Object> testCall2(Map<String, Object> paramsMap) { System.out.println("testCall.2," + paramsMap); return paramsMap; } /** * 静态方法 * @param paramsMap * @return */ public static Map<String, Object> testCall3(HashMap<String, Object> paramsMap) { System.out.println("testCall.3," + paramsMap); return paramsMap; } /** * 实例方法1 */ public void testCall11() { System.out.println("testCall.11"); } /** * 实例方法2 * @param paramsMap * @return */ public Map<String, Object> testCall12(Map<String, Object> paramsMap) { System.out.println("testCall.12," + paramsMap); return paramsMap; } /** * 实例方法3 * @param paramsMap * @return */ public Map<String, Object> testCall13(HashMap<String, Object> paramsMap) { System.out.println("testCall.13," + paramsMap); return paramsMap; }}
import java.util.Arrays;import java.util.Date;import java.util.HashMap;import java.util.Map;public class ReflectCallMethodDemo { public static void main(String[] args) { // 静态方法调用=====1 Object result1 = ReflectCallMethod.invokeForStatic(User.class, "testCall1"); System.out.println("result1 is null ," + result1); // 静态方法调用=====2 Map<String, Object> paramsMap1 = new HashMap<>(); paramsMap1.put("map", new Object()); Map<String, Object> result2 = ReflectCallMethod.invokeForStatic(User.class, "testCall2", new Class[]{Map.class}, new Object[]{paramsMap1}); System.out.println("result2 is map ," + result2); // 静态方法调用=====3 Map<String, Object> result3 = ReflectCallMethod.invokeForStatic(User.class, "testCall3", Arrays.asList(paramsMap1)); System.out.println("result3 is map ," + result3); // 实例方法调用1 User user = new User(); Object result11 = ReflectCallMethod.invokeForInstance(user, "testCall11"); System.out.println("result11 is null ," + result11); // 实例方法调用=====2 Map<String, Object> paramsMap21 = new HashMap<>(); paramsMap1.put("map", new Object()); Map<String, Object> result12 = ReflectCallMethod.invokeForInstance(user, "testCall12", new Class[]{Map.class}, new Object[]{paramsMap21}); System.out.println("result12 is map ," + result2); // 实例方法调用=====3 Map<String, Object> result13 = ReflectCallMethod.invokeForInstance(user, "testCall13", Arrays.asList(paramsMap21)); System.out.println("result13 is map ," + result13); // setter and getter User user2 = new User(2, "子贡", new Date()); String name = ReflectCallMethod.getInvoke(user2, "getName"); System.out.println("method name get ," + name); String name2 = ReflectCallMethod.getInvokeByFieldName(user2, "name"); System.out.println("field name get ," + name2); ReflectCallMethod.setInvoke(user2, "setName", "闵损"); ReflectCallMethod.setInvokeByFieldName(user2, "id", 200); int id = ReflectCallMethod.getInvoke(user2, "getId"); System.out.println("method name get ," + id); String name3 = ReflectCallMethod.getInvokeByFieldName(user2, "name"); System.out.println("field name get ," + name3); }}
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #java调用静态方法