introspectionmbean.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 840 行 · 第 1/2 页
JAVA
840 行
HashMap<String,MBeanAttributeInfo> attributes = new HashMap<String,MBeanAttributeInfo>(); ArrayList<MBeanConstructorInfo> constructors = new ArrayList<MBeanConstructorInfo>(); ArrayList<MBeanOperationInfo> operations = new ArrayList<MBeanOperationInfo>(); Method []methods = cl.getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getDeclaringClass() == Object.class) continue; if (Modifier.isStatic(method.getModifiers())) continue; String methodName = method.getName(); Class []args = method.getParameterTypes(); Class retType = method.getReturnType(); if (methodName.startsWith("get") && args.length == 0 && ! retType.equals(void.class)) { Method getter = method; String name = methodName.substring(3); Method setter = getSetter(methods, name, retType); String attributeName; if (isLowercaseAttributeNames) { StringBuilder builder = new StringBuilder(name); builder.setCharAt(0, Character.toLowerCase(builder.charAt(0))); attributeName = builder.toString(); } else attributeName = name; Class type = method.getReturnType(); MBeanAttributeInfo attr; attr = new MBeanAttributeInfo(attributeName, getDescription(method), getter, setter); /* Descriptor descriptor = attr.getDescriptor(); if (descriptor != null) { Object openType = getOpenType(type); if (openType != null) descriptor.setField("openType", openType); descriptor.setField("originalType", getTypeName(type)); attr.setDescriptor(descriptor); } */ if (attributes.get(attributeName) == null) attributes.put(attributeName, attr); } else if (methodName.startsWith("is") && args.length == 0 && (retType.equals(boolean.class) || retType.equals(Boolean.class))) { Method getter = method; String name = methodName.substring(2); Method setter = getSetter(methods, name, retType); String attributeName; if (isLowercaseAttributeNames) { StringBuilder builder = new StringBuilder(name); builder.setCharAt(0, Character.toLowerCase(builder.charAt(0))); attributeName = builder.toString(); } else attributeName = name; if (attributes.get(attributeName) == null) { attributes.put(attributeName, new MBeanAttributeInfo(attributeName, getDescription(method), getter, setter)); } } else if (methodName.startsWith("set") && args.length == 1) { Method setter = method; String name = methodName.substring(3); Method getter = getGetter(methods, name, args[0]); if (getter == null) { String attributeName; if (isLowercaseAttributeNames) { StringBuilder builder = new StringBuilder(name); builder.setCharAt(0, Character.toLowerCase(builder.charAt(0))); attributeName = builder.toString(); } else attributeName = name; if (attributes.get(attributeName) == null) { attributes.put(attributeName, new MBeanAttributeInfo(attributeName, getDescription(method), null, setter)); } } } else { operations.add(new MBeanOperationInfo(getName(method), getDescription(method), getSignature(method), method.getReturnType().getName(), MBeanOperationInfo.UNKNOWN)); } } ArrayList<MBeanNotificationInfo> notifications = new ArrayList<MBeanNotificationInfo>(); if (obj instanceof NotificationBroadcaster) { NotificationBroadcaster broadcaster; broadcaster = (NotificationBroadcaster) obj; MBeanNotificationInfo[] notifs = broadcaster.getNotificationInfo(); if (notifs != null) { for (int i = 0; i < notifs.length; i++) { MBeanNotificationInfo notif = notifs[i]; notifications.add((MBeanNotificationInfo) notifs[i].clone()); } } } Collections.sort(notifications, MBEAN_FEATURE_INFO_COMPARATOR); MBeanAttributeInfo []attrArray = new MBeanAttributeInfo[attributes.size()]; attributes.values().toArray(attrArray); Arrays.sort(attrArray, MBEAN_FEATURE_INFO_COMPARATOR); MBeanConstructorInfo []conArray = new MBeanConstructorInfo[constructors.size()]; constructors.toArray(conArray); MBeanOperationInfo []opArray = new MBeanOperationInfo[operations.size()]; operations.toArray(opArray); Arrays.sort(opArray, MBEAN_FEATURE_INFO_COMPARATOR); MBeanNotificationInfo []notifArray = new MBeanNotificationInfo[notifications.size()]; notifications.toArray(notifArray); Arrays.sort(notifArray, MBEAN_FEATURE_INFO_COMPARATOR); MBeanInfo modelInfo; modelInfo = new MBeanInfo(cl.getName(), getDescription(cl), attrArray, conArray, opArray, notifArray); /* Descriptor descriptor = modelInfo.getMBeanDescriptor(); if (descriptor != null) { descriptor.setField("mxbean", "true"); modelInfo.setMBeanDescriptor(descriptor); } */ info = modelInfo; _cachedInfo.put(cl, new SoftReference<MBeanInfo>(info)); return info; } catch (Exception e) { NotCompliantMBeanException exn; exn = new NotCompliantMBeanException(String.valueOf(e)); exn.initCause(e); throw exn; } } /** * Returns the matching setter. */ static Method getSetter(Method []methods, String property, Class type) { String name = "set" + property; for (int i = 0; i < methods.length; i++) { if (! methods[i].getName().equals(name)) continue; Class []args = methods[i].getParameterTypes(); if (args.length != 1 || ! args[0].equals(type)) continue; return methods[i]; } return null; } /** * Returns the matching getter. */ static Method getGetter(Method []methods, String property, Class type) { String getName = "get" + property; String isName = "is" + property; for (int i = 0; i < methods.length; i++) { if (! methods[i].getName().equals(getName) && ! methods[i].getName().equals(isName)) continue; Class []args = methods[i].getParameterTypes(); if (args.length != 0) continue; Class retType = methods[i].getReturnType(); if (! retType.equals(type)) continue; return methods[i]; } return null; } /** * Returns the class's description. */ static String getDescription(Class cl) { try { Description desc = (Description) cl.getAnnotation(_descriptionAnn); if (desc != null) return desc.value(); else return ""; } catch (Throwable e) { return ""; } } /** * Returns the method's description. */ static String getDescription(Method method) { try { Description desc = (Description) method.getAnnotation(_descriptionAnn); if (desc != null) return desc.value(); else return ""; } catch (Throwable e) { return ""; } } /** * Returns the method's name, the optional {@link Name} annotation overrides. */ static String getName(Method method) { try { Name name = (Name) method.getAnnotation(_nameAnn); if (name != null) return name.value(); else return method.getName(); } catch (Throwable e) { return method.getName(); } } private static MBeanParameterInfo[] getSignature(Method method) { Class[] params = method.getParameterTypes(); MBeanParameterInfo[] paramInfos = new MBeanParameterInfo[params.length]; for (int i = 0; i < params.length; i++) { Class cl = params[i]; String name = getName(method, i); String description = getDescription(method, i); paramInfos[i] = new MBeanParameterInfo(name, cl.getName(), description); } return paramInfos; } private static String getName(Method method, int i) { try { for (Annotation ann : method.getParameterAnnotations()[i]) { if (ann instanceof Name) return ((Name) ann).value(); } } catch (Throwable e) { log.log(Level.FINER, e.toString(), e); } return "p" + i; } private static String getDescription(Method method, int i) { try { for (Annotation ann : method.getParameterAnnotations()[i]) { if (ann instanceof Description) return ((Description) ann).value(); } } catch (Throwable e) { log.log(Level.FINER, e.toString(), e); } return ""; } private static String getTypeName(Class type) { if (type.isArray()) return getTypeName(type.getComponentType()) + "[]"; else return type.getName(); } private static OpenType getOpenType(Class type) { try { if (type.isArray()) { OpenType component = getOpenType(type.getComponentType()); if (component != null) return new ArrayType(1, component); else return null; } else if (type.getName().endsWith("MXBean") || type.getName().endsWith("MBean")) return SimpleType.OBJECTNAME; else if (void.class.equals(type)) return SimpleType.VOID; else if (boolean.class.equals(type) || Boolean.class.equals(type)) return SimpleType.BOOLEAN; else if (byte.class.equals(type) || Byte.class.equals(type)) return SimpleType.BYTE; else if (short.class.equals(type) || Short.class.equals(type)) return SimpleType.SHORT; else if (int.class.equals(type) || Integer.class.equals(type)) return SimpleType.INTEGER; else if (long.class.equals(type) || Long.class.equals(type)) return SimpleType.LONG; else if (float.class.equals(type) || Float.class.equals(type)) return SimpleType.FLOAT; else if (double.class.equals(type) || Double.class.equals(type)) return SimpleType.DOUBLE; else if (String.class.equals(type)) return SimpleType.STRING; else if (char.class.equals(type) || Character.class.equals(type)) return SimpleType.CHARACTER; else if (java.util.Date.class.equals(type)) return SimpleType.DATE; else if (java.util.Calendar.class.equals(type)) return SimpleType.DATE; else return null; // can't deal with more complex at the moment } catch (Exception e) { log.log(Level.FINER, e.toString(), e); return null; } } private static Class findClass(String name) { try { return Class.forName(name); } catch (Throwable e) { return null; } } private static final Comparator<MBeanFeatureInfo> MBEAN_FEATURE_INFO_COMPARATOR = new Comparator<MBeanFeatureInfo>() { public int compare(MBeanFeatureInfo o1, MBeanFeatureInfo o2) { return o1.getName().compareTo(o2.getName()); } }; static { _descriptionAnn = findClass("com.caucho.jmx.Description"); _nameAnn = findClass("com.caucho.jmx.Name"); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?