[jboss-dev-forums] [Design of AOP on JBoss (Aspects/JBoss)] - Re: AOPConstructorJoinpoint and methodHasSubInstanceMetaData
kabir.khan@jboss.com
do-not-reply at jboss.com
Thu Jul 24 06:44:26 EDT 2008
BTW looking at ReflectionUtils.findMethod() it relies on exceptions:
| public static Method findMethod(Class<?> clazz, String name, Class<?>... parameterTypes)
| {
| if (clazz == null)
| return null;
|
| try
| {
| return clazz.getDeclaredMethod(name, parameterTypes);
| }
| catch(Exception ignored)
| {
| }
| return findMethod(clazz.getSuperclass(), name, parameterTypes);
| }
|
Since exceptions are expensive to create, I threw together a small benchmark
| public class TestFindMethods
| {
|
| public static void main(String[] args)
| {
| Class<?> clazz = ArrayList.class;
| //Use the name of one of the last methods from the base class, but with non-matching params
| //(The method is called java.util.AbstractCollection.retainAll(Collection<?>))
| String name = "retainAll";
| Class<?>[] params = new Class<?>[] {Integer.class};
|
| long get = 0;
| long iterate = 0;
| for (int i = 0 ; i < 10000000 ; i++)
| {
| long start1 = System.currentTimeMillis();
| Method m1 = getDeclaredMethod(clazz, name, params);
| get += (System.currentTimeMillis() - start1);
|
| long start2 = System.currentTimeMillis();
| Method m2 = iterateDeclaredMethods(clazz, name, params);
| iterate += (System.currentTimeMillis() - start2);
| }
|
| System.out.println("Get took " + get);
| System.out.println("Iterate took " + iterate);
| }
|
| private static Method getDeclaredMethod(Class clazz, String name, Class<?>[] params)
| {
| try
| {
| return clazz.getDeclaredMethod(name, params);
| }
| catch(Exception e)
| {
|
| }
| clazz = clazz.getSuperclass();
| if (clazz != null)
| {
| return getDeclaredMethod(clazz, name, params);
| }
| return null;
| }
|
| private static Method iterateDeclaredMethods(Class<?> clazz, String name, Class<?>[] params)
| {
| Method[] methods = clazz.getDeclaredMethods();
| for (int i = 0 ; i < methods.length ; i++)
| {
| if (name.equals(methods.getName()))
| {
| Class<?>[] myparams = methods.getParameterTypes();
| if (myparams.length == params.length)
| {
| for (int j = 0 ; j < myparams.length ; j++)
| {
| if (params[j] != myparams[j])
| {
| break;
| }
| return methods;
| }
| }
| }
| }
| clazz = clazz.getSuperclass();
| if (clazz != null)
| {
| return iterateDeclaredMethods(clazz, name, params);
| }
| return null;
| }
| }
|
Running it I get:
| Get took 179658
| Iterate took 112402
|
Of course if we get rid of the cause of not finding these methods then this becomes less relevant.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4166373#4166373
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4166373
More information about the jboss-dev-forums
mailing list