@Test public void testAttributeEntryGetters() throws Exception { assertEquals(key, attributeEntryClient.getKey()); assertEquals(description, attributeEntryClient.getDescription()); final Object value = attributeEntryClient.getValue(); assertNull(value); assertEquals(type, attributeEntryClient.getType()); assertEquals(boolValue, attributeEntryClient.isRw()); }
private Attribute getNamedAttribute(String name) { Attribute result = null; if (name.equals(ResourceDMBean.MBEAN_DESCRITION)) { result = new Attribute(ResourceDMBean.MBEAN_DESCRITION, this.description); } else { AttributeEntry entry = atts.get(name); if (entry != null) { try { result = new Attribute(name, entry.invoke(null)); } catch (Exception e) { log.warn("Exception while reading value of attribute " + name, e); } } else { log.warn("Did not find queried attribute with name " + name); } } return result; }
private boolean setNamedAttribute(Attribute attribute) { boolean result = false; AttributeEntry entry = atts.get(attribute.getName()); if (entry != null) { try { entry.invoke(attribute); result = true; } catch (Exception e) { log.warn("Exception while writing value for attribute " + attribute.getName(), e); } } else { log.warn( "Could not invoke set on attribute " + attribute.getName() + " with value " + attribute.getValue()); } return result; }
public ResourceDMBean(Object instance) { if (instance == null) throw new NullPointerException("Cannot make an MBean wrapper for null instance"); this.obj = instance; findDescription(); findFields(); findMethods(); attrInfo = new MBeanAttributeInfo[atts.size()]; int i = 0; MBeanAttributeInfo info = null; for (AttributeEntry entry : atts.values()) { info = entry.getInfo(); attrInfo[i++] = info; } opInfo = new MBeanOperationInfo[ops.size()]; ops.toArray(opInfo); }
private void processCodeAttribute(ClassMember member, AttributeEntry ae) throws IOException { ClassInput ci = new ClassInput(new java.io.ByteArrayInputStream(ae.infoIn)); DataInputUtil.skipFully(ci, 4); // puts us at code_length int len = ci.getU4(); DataInputUtil.skipFully(ci, len); // puts us at exception_table_length int count = ci.getU2(); if (count != 0) DataInputUtil.skipFully(ci, 8 * count); int nonAttrLength = 4 + 4 + len + 2 + (8 * count); // now at attributes count = ci.getU2(); if (count == 0) return; int newCount = count; for (int i = 0; i < count; i++) { int nameIndex = ci.getU2(); String name = nameIndexToString(nameIndex); if (name.equals("LineNumberTable") || name.equals("LocalVariableTable")) newCount--; else System.err.println("ERROR - Unknown code attribute " + name); len = ci.getU4(); DataInputUtil.skipFully(ci, len); } if (newCount != 0) { System.err.println("ERROR - expecting all code attributes to be removed"); System.exit(1); } // this is only coded for all attributes within a Code attribute being removed. byte[] newInfo = new byte[nonAttrLength + 2]; System.arraycopy(ae.infoIn, 0, newInfo, 0, nonAttrLength); // last two bytes are left at 0 which means 0 attributes ae.infoIn = newInfo; }
// remove all atttributes that are not essential public void removeAttributes() throws IOException { // Class level attributes if (attribute_info != null) { for (int i = attribute_info.size() - 1; i >= 0; i--) { AttributeEntry ae = (AttributeEntry) attribute_info.elementAt(i); String name = nameIndexToString(ae.getNameIndex()); if (name.equals("SourceFile")) attribute_info.removeElementAt(i); else if (name.equals("InnerClasses")) ; // leave in else System.err.println("WARNING - Unknown Class File attribute " + name); } if (attribute_info.size() == 0) attribute_info = null; } attribute_info = null; // fields for (Enumeration e = getFields(); e.hasMoreElements(); ) { ClassMember member = (ClassMember) e.nextElement(); Attributes attrs = member.attribute_info; if (attrs != null) { for (int i = attrs.size() - 1; i >= 0; i--) { AttributeEntry ae = (AttributeEntry) attrs.elementAt(i); String name = nameIndexToString(ae.getNameIndex()); if (name.equals("ConstantValue")) ; // leave in else if (name.equals("Synthetic")) ; // leave in else System.err.println("WARNING - Unknown Field attribute " + name); } if (attrs.size() == 0) member.attribute_info = null; } } // methods for (Enumeration e = getMethods(); e.hasMoreElements(); ) { ClassMember member = (ClassMember) e.nextElement(); Attributes attrs = member.attribute_info; if (attrs != null) { for (int i = attrs.size() - 1; i >= 0; i--) { AttributeEntry ae = (AttributeEntry) attrs.elementAt(i); String name = nameIndexToString(ae.getNameIndex()); if (name.equals("Code")) processCodeAttribute(member, ae); else if (name.equals("Exceptions")) ; // leave in else if (name.equals("Deprecated")) ; // leave in else if (name.equals("Synthetic")) ; // leave in else System.err.println("WARNING - Unknown method attribute " + name); } if (attrs.size() == 0) member.attribute_info = null; } } }
private void exposeManagedAttribute(Method method) { String methodName = method.getName(); if (!methodName.startsWith("get") && !methodName.startsWith("set") && !methodName.startsWith("is")) { if (log.isWarnEnabled()) log.warn( "method name " + methodName + " doesn't start with \"get\", \"set\", or \"is\"" + ", but is annotated with @ManagedAttribute: will be ignored"); return; } ManagedAttribute attr = method.getAnnotation(ManagedAttribute.class); Property prop = method.getAnnotation(Property.class); boolean expose_prop = prop != null && prop.exposeAsManagedAttribute(); boolean expose = attr != null || expose_prop; if (!expose) return; // Is name field of @ManagedAttributed used? String attributeName = attr != null ? attr.name() : null; if (attributeName != null && attributeName.trim().length() > 0) attributeName = attributeName.trim(); else attributeName = null; String descr = attr != null ? attr.description() : prop != null ? prop.description() : null; boolean writeAttribute = false; MBeanAttributeInfo info = null; if (isSetMethod(method)) { // setter attributeName = (attributeName == null) ? methodName.substring(3) : attributeName; info = new MBeanAttributeInfo( attributeName, method.getParameterTypes()[0].getCanonicalName(), descr, true, true, false); writeAttribute = true; } else { // getter if (method.getParameterTypes().length == 0 && method.getReturnType() != java.lang.Void.TYPE) { boolean hasSetter = atts.containsKey(attributeName); // we found is method if (methodName.startsWith("is")) { attributeName = (attributeName == null) ? methodName.substring(2) : attributeName; info = new MBeanAttributeInfo( attributeName, method.getReturnType().getCanonicalName(), descr, true, hasSetter, true); } else { // this has to be get attributeName = (attributeName == null) ? methodName.substring(3) : attributeName; info = new MBeanAttributeInfo( attributeName, method.getReturnType().getCanonicalName(), descr, true, hasSetter, false); } } else { if (log.isWarnEnabled()) { log.warn( "Method " + method.getName() + " must have a valid return type and zero parameters"); } // silently skip this method return; } } AttributeEntry ae = atts.get(attributeName); // is it a read method? if (!writeAttribute) { // we already have annotated field as read if (ae instanceof FieldAttributeEntry && ae.getInfo().isReadable()) { log.warn("not adding annotated method " + method + " since we already have read attribute"); } // we already have annotated set method else if (ae instanceof MethodAttributeEntry) { MethodAttributeEntry mae = (MethodAttributeEntry) ae; if (mae.hasSetMethod()) { atts.put( attributeName, new MethodAttributeEntry(mae.getInfo(), mae.getSetMethod(), method)); } } // we don't have such entry else { atts.put( attributeName, new MethodAttributeEntry(info, findSetter(obj.getClass(), attributeName), method)); } } // is it a set method? else { if (ae instanceof FieldAttributeEntry) { // we already have annotated field as write if (ae.getInfo().isWritable()) { log.warn( "Not adding annotated method " + methodName + " since we already have writable attribute"); } else { // we already have annotated field as read // lets make the field writable Field f = ((FieldAttributeEntry) ae).getField(); MBeanAttributeInfo i = new MBeanAttributeInfo( ae.getInfo().getName(), f.getType().getCanonicalName(), descr, true, !Modifier.isFinal(f.getModifiers()), false); atts.put(attributeName, new FieldAttributeEntry(i, f)); } } // we already have annotated getOrIs method else if (ae instanceof MethodAttributeEntry) { MethodAttributeEntry mae = (MethodAttributeEntry) ae; if (mae.hasIsOrGetMethod()) { atts.put(attributeName, new MethodAttributeEntry(info, method, mae.getIsOrGetMethod())); } } // we don't have such entry else { atts.put( attributeName, new MethodAttributeEntry(info, method, findGetter(obj.getClass(), attributeName))); } } }