I encountered a problem like this in my work recently:
I made a test tool for all interfaces in a certain project. Using java Swing technology, the project has different versions. Not all interfaces in the version are the same, and the tools I made need to be compatible with all versions.
So this problem was introduced:
If some interfaces do not exist in some versions, an error will be reported when performing this operation through the interface. Therefore, in order to be compatible with all versions, you must consider whether the method exists before calling the method. At the same time, in order not to throw exceptions during compilation, when calling the method
It also needs to be called through reflection, and the specific implementation is as follows:
1. Use reflection to determine whether the method exists
/** * Determine whether the method exists* * @param obj JObjectService instance* @param methodName Method name* @param paraTypes Method form parameter type array* @return */ public static boolean judgeMethodIsExist(Object obj, String methodName, Class[] paraTypes) { boolean result = true; try { if (null != obj) { Method method = obj.getClass().getMethod(methodName, paraTypes); } } catch (NoSuchMethodException ex) { showWarnInfo(ex.toString()); log.error("Exception occurs at: " + MainJFrame.hostName + "; details are: " + ex.getMessage()); result = false; } return result; }Parameter description:
(1) obj: an object that calls a method
(2) methodName: The name of the method to be called
(3) paraTypes: The parameter type required by the method (multiple is an array)
Through this method, you can determine whether the methodName (parameter type) method you call through the obj object exists. If it does not exist, it will run a NoSuchMethodException exception
2. Call the method through reflection to avoid compile-time exceptions
sysUser = MainJFrame.getSysUser(); Class[] paramObj = {String.class, long.class, String.class, String.class, int.class}; //Judge whether the method exists boolean isExist = CommonUtil.judgeMethodIsExist(sysUser, "createBucket", paramObj); if (isExist) { try { //Calling method Class clazz = sysUser.getClass(); //Method name--The type of parameter in the method (in parameter order) Method createBucket = clazz.getDeclaredMethod("createBucket",String.class, long.class, String.class, String.class, int.class); int create = (int) createBucket.invoke(sysUser,bucketName, Long.parseLong(bucketSize), bucketLoc, bucketAcl, Integer.parseInt(StringUtil.emptyToZero(bucketCycle))); if (create == 1) { CommonUtil.showInfo("Bucket created successfully"); log.info("Bucket created successfully"); } else { CommonUtil.showWarnInfo("Bucket creation failed, server internal error!"); log.info("Bucket creation failed, server internal error"); } } catch (Exception ex) { CommonUtil.showWarnInfo(ex.getMessage()); log.error("Exception occurs at: " + MainJFrame.hostName + "; the details are: " + ex.getMessage()); } }In the above code:
Line 1: Object sysUser that calls the method createBucket method
Line 2: Array of parameter types in the method, used when determining whether the method exists
Line 3: Determine whether the method exists by array of object, method name and parameter type
Through the above three lines, the following tasks will be executed when the method exists. If it does not exist, exception information will be prompted.
Line 6: Get the Class of the object
Line 7: Get the method method object, the parameter is the type corresponding to the method name and parameter
Line 8: Call the createBucket method through Method object reflection, the parameters are the sysUser object and the required parameters (values) respectively.
Through the above method, no compile-time exception occurs even if an interface in the object does not exist.
To be honest, this is the first time I have encountered a problem with Java reflection in my project, so it is necessary to record it!
Summarize
The above is the entire content of this article’s application notes on Java reflection in actual work. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Java Reflection Tutorial
Things you need to know about Java reflection mechanism
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!