I have a bunch of standard MBeans which I need to convert to dynamic MBeans, (main purpose is to add user friendly descriptions to attributes, operations and operation parameters). I was wondering if there is any tool which can make my job easier by generating skeleton code for dynamic MBeans from the existing standard MBeans.
A:
I could not find anything. Here's a small utility method that generates the MBeanInfo for the operations only. Replace ThreadMXBean with your own interface. Warning : It does not take care of inheritance, non public methods, attributes etc. it is limited to operations. Use at your own risk.
import java.lang.management.ThreadMXBean;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class Gen {
public static void main(String[] args) {
List<Class> intfc = new ArrayList<Class>();
intfc.add(ThreadMXBean.class);
for(Class c:intfc)
System.out.println(gen(c));
}
private static String gen(Class c) {
StringBuilder sb = new StringBuilder();
sb.append("return new MBeanInfo(\"");
sb.append(c.getName());
sb.append("\",");
sb.append("\n\"Desc\",\r\n" +
" null,null, \r\n" +
" new MBeanOperationInfo[]{");
boolean first = true;
for(Method m:c.getDeclaredMethods()){
if(!first)
sb.append(",");
first = false;
sb.append("\nnew MBeanOperationInfo(");
sb.append("\""+m.getName()+"\",\""+m.getName()+"\", new MBeanParameterInfo[]{");
boolean firstparam = true;
for(Class param:m.getParameterTypes()){
if(!firstparam)
sb.append(",");
firstparam = false;
sb.append("new MBeanParameterInfo(\"ParamName\",\"");
sb.append(param.getName()).append("\",");
sb.append("\"Description\")");
}
sb.append("},\""+m.getReturnType().getName()+"\",0)");
sb.append("\n");
}
sb.append("}\n,null);");
return sb.toString();
}
}
saugata
2010-05-14 10:15:58