Can I create class using reflection?
bornmw Dec 21, 2007 12:00 PMHi!
I assume my question is about design of JBossAOP since users couldn't help me.
My question is simple.
I have a class that is post processed by aopc.
Does JBossAOP design let me I instantiate it using reflection?
When I try doing it I get this:
java -cp .:/projects/lib/jboss-aop_1.5.5.GA/lib-50/jboss-aop-jdk50.jar:/usr/local/pluto-1.1.4/shared /lib/portlet-api-1.0.jar:/usr/local/pluto-1.1.4/webapps/AOPPortlet/WEB-INF/lib/concurrent.jar:/usr/l ocal/pluto-1.1.4/webapps/AOPPortlet/WEB-INF/lib/javassist-snapshot.jar:/usr/local/pluto-1.1.4/webapp s/AOPPortlet/WEB-INF/lib/jboss-common.jar:/usr/local/pluto-1.1.4/webapps/AOPPortlet/WEB-INF/lib/trov e.jar ConstrTest constructor = public Test() Exception in thread "main" java.lang.VerifyError: (class: org/jboss/aop/advice/ViewAspect0, method: invoke signature: (Lorg/jb oss/aop/joinpoint/Invocation;)Ljava/lang/Object;) Unable to pop operand off an empty stack at java.lang.Class.getDeclaredConstructors0(Native Method) at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389) at java.lang.Class.getConstructor0(Class.java:2699) at java.lang.Class.newInstance0(Class.java:326) at java.lang.Class.newInstance(Class.java:308) at org.jboss.aop.advice.PerVmAdvice.generateInterceptor(PerVmAdvice.java:132) at org.jboss.aop.advice.PerVmAdvice.generateOptimized(PerVmAdvice.java:56) at org.jboss.aop.advice.AdviceFactory.create(AdviceFactory.java:68) at org.jboss.aop.Advisor.createInterceptorChain(Advisor.java:618) at org.jboss.aop.Advisor.pointcutResolved(Advisor.java:888) at org.jboss.aop.Advisor.resolveMethodPointcut(Advisor.java:650) at org.jboss.aop.ClassAdvisor.createInterceptorChains(ClassAdvisor.java:607) at org.jboss.aop.ClassAdvisor.access$300(ClassAdvisor.java:82) at org.jboss.aop.ClassAdvisor$1.run(ClassAdvisor.java:299) at java.security.AccessController.doPrivileged(Native Method) at org.jboss.aop.ClassAdvisor.attachClass(ClassAdvisor.java:271) at org.jboss.aop.AspectManager.initialiseClassAdvisor(AspectManager.java:591) at org.jboss.aop.AspectManager.getAdvisor(AspectManager.java:579) at Test.<clinit>(Test.java) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java: 39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorIm pl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at java.lang.Class.newInstance0(Class.java:355) at java.lang.Class.newInstance(Class.java:308) at ConstrTest.main(ConstrTest.java:16)
My code is:
ClassLoader loader = Thread.currentThread().getContextClassLoader(); Class clazz = loader.loadClass("Test"); for (int j = 0; j < clazz.getConstructors().length; j++) { Constructor constructor = clazz.getConstructors()[j]; System.out.println("constructor = " + constructor); } Test t = (Test) clazz.newInstance(); System.out.println("t = " + t);
Test is a simple class with default constructor and one method that has aspect attached to it.
What am I doing wrong? Or is it not possible to construct aspectized classes using reflection at all?
Many thanks.