📄 standardmetadataimpl.java
字号:
Object result = null; final Object value = attribute.getValue(); final String attname = attribute.getName(); // Query the metadata service to get the appropriate setter // of the object. Method meth = null; if (value == null) { meth = findSetter(mbeanClass, attname); } else { meth = findSetter(mbeanClass, attname, value.getClass()); } if (meth == null) { // Check whether the type is a primitive one Class primClass = findPrimForClass(value); if (primClass != null) { meth = findSetter(mbeanClass, attname, primClass); } } if (meth == null) { // Try to check if the attribute name does correspond to a // valid property meth= findSetter(mbeanClass, attname); if (meth == null) { if (isTraceOn()) { trace("setAttribute", "Cannot find setter for "+attribute+ " in class " + mbeanClass.getName()); } throw new AttributeNotFoundException( attname + " not accessible"); } else { final Object v = attribute.getValue(); if (v == null) { throw new InvalidAttributeValueException("attribute= " + attname + " value = null"); } else { throw new InvalidAttributeValueException("attribute= " + attname + " value = " + v); } } } // Invoke the setter if (isTraceOn()) { trace("setAttribute", "Invoking the set method for " + attname); } final Object[] values = new Object[1]; values[0] = value; try { result = meth.invoke(instance,values); } catch (IllegalAccessException e) { debugX("setAttribute",e); // Wrap the exception. throw new ReflectionException(e, "IllegalAccessException" + " occured trying to invoke the setter on the MBean"); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); debugX("setAttribute",t); if (t instanceof RuntimeException) { final String msg = "RuntimeException thrown in the setter for the attribute " + attribute; throw wrapRuntimeException((RuntimeException) t, msg); } else if (t instanceof Error) { throw new RuntimeErrorException((Error) t, "Error thrown in the MBean's setter"); } else { throw new MBeanException((Exception) t, "Exception thrown in the MBean's setter"); } } if (isTraceOn()) { trace("setAttribute", attname + "= " + value); } return value; } /** * Returns the MBeanNotificationInfo of the MBeans that implement * the NotificationBroadcaster interface. */ MBeanNotificationInfo[] findNotifications(Object moi) { if (moi instanceof javax.management.NotificationBroadcaster) { MBeanNotificationInfo[] mbn = ((NotificationBroadcaster)moi).getNotificationInfo(); if (mbn == null) { return new MBeanNotificationInfo[0]; } MBeanNotificationInfo[] result = new MBeanNotificationInfo[mbn.length]; for (int i = 0; i < mbn.length; i++) { result[i] = (MBeanNotificationInfo) mbn[i].clone(); } return result; } return new MBeanNotificationInfo[0]; } /** * Finds a specific method of an object. * Returns the method or null if not found */ public static Method findMethod(Class classObj, String name, Class parameterTypes[]) { Method method=null; try { method= classObj.getMethod(name, parameterTypes); } catch(Exception e) { // OK: will return null. } return method; } /** * Finds a specific method of an object without knowing the parameter * types. * Returns the method or null if not found */ public static Method findMethod(Class classObj, String name) { Method method = null ; try { Method[] methods=classObj.getMethods(); int i = 0; while ((i < methods.length) && !methods[i].getName().equals(name)) { i++; } if (i < methods.length) { method = methods[i]; } } catch(Exception e) { // OK: will return null. } return method; } /** * Finds a specific method of an object given the number of parameters. * Returns the method or null if not found */ public static Method findMethod(Class classObj, String name, int paramCount) { Method method = null; try { Method[] methods=classObj.getMethods(); int i = 0; boolean found = false; while ((i < methods.length) && !found) { found = methods[i].getName().equals(name); if (found) { // Now check if the number of parameters found = (methods[i].getParameterTypes().length == paramCount); } i++; } if (found) { method = methods[i-1] ; // Note i-1 ! } } catch(Exception e) { // OK: will return null; } return method; } /** * Finds the getter of a specific attribute in an object. * Returns the method for accessing the attributes, null otherwise */ public static Method findGetter(Class classObj, String attribute) { // Methods called "is" or "get" tout court are not getters if (attribute.length() == 0) return null; // Look for a method T getX(), where T is not void Method m = findMethod(classObj, "get" + attribute, null); if (m != null && m.getReturnType() != void.class) return m; // Look for a method boolean isX() // must not be any other type than "boolean", including not "Boolean" m = findMethod(classObj, "is" + attribute, null); if (m != null && m.getReturnType() == boolean.class) return m; return null; } /** * Finds the setter of a specific attribute in an object. * Returns the method for accessing the attribute, null otherwise */ public static Method findSetter(Class classObj, String attribute, Class type) { Method mth= findMethod(classObj, "set" + attribute, 1); if (mth != null) { Class[] pars = mth.getParameterTypes(); if (pars[0].isAssignableFrom(type)) { return mth; } } return null; } /** * Finds the setter of a specific attribute without knowing its type. * Returns the method for accessing the attribute, null otherwise */ public static Method findSetter(Class classObj, String attribute) { return findMethod(classObj, "set" + attribute, 1) ; } /** * Finds a specific constructor of a class * Returns the requested constructor or null if not found */ public static Constructor findConstructor(Class theClass, Class parameterTypes[]) { // Get the list of methods Constructor mth = null; try { mth = theClass.getConstructor(parameterTypes); } catch(Exception e) { return null; } return mth; } /** * Get the class of the constructed type * corresponding to the given primitive type */ public static Class findClassForPrim(String primName) { return (Class) primitiveClasses.get(primName); } /** * Get the class of the primitive type * corresponding to the given constructed object. */ public static Class findPrimForClass(Object value) { if (value instanceof Boolean) return Boolean.TYPE; else if (value instanceof Character) return Character.TYPE; else if (value instanceof Byte) return Byte.TYPE; else if (value instanceof Short) return Short.TYPE; else if (value instanceof Integer) return Integer.TYPE; else if (value instanceof Long) return Long.TYPE; else if (value instanceof Float) return Float.TYPE; else if (value instanceof Double) return Double.TYPE; return null; } /** * Converts the array of classes to an array of class signatures. */ static String[] findSignatures(Class[] clz) { String signers[] = new String[clz.length]; for (int i = 0; i < clz.length; i++) { signers[i] = findSignature(clz[i]); } return signers; } /** * Converts the class to a class signature. */ static String findSignature(Class clz) { return clz.getName(); } private RuntimeException wrapRuntimeException(RuntimeException re, String msg) { if (wrapRuntimeExceptions) return new RuntimeMBeanException(re, msg); else return re; } // TRACES & DEBUG //--------------- private static boolean isTraceOn() { return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER); } private static void trace(String clz, String func, String info) { Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info); } private static void trace(String func, String info) { trace(dbgTag, func, info); } private static boolean isDebugOn() { return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER); } private static void debug(String clz, String func, String info) { Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info); } private static void debug(String func, String info) { debug(dbgTag, func, info); } private static void debugX(String func,Throwable e) { if (isDebugOn()) { final StringWriter s = new StringWriter(); e.printStackTrace(new PrintWriter(s)); final String stack = s.toString(); debug(dbgTag,func,"Exception caught in "+ func+"(): "+e); debug(dbgTag,func,stack); // java.lang.System.err.println("**** Exception caught in "+ // func+"(): "+e); // java.lang.System.err.println(stack); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -