[jboss-user] [Javassist user questions] - Re: java.lang.NoSuchMethodException when invoking a method

ssiddiqu do-not-reply at jboss.com
Wed Oct 10 17:46:50 EDT 2007


Can some more experienced users please help me on this?
Thanks.
--Sarim.
"ssiddiqu" wrote : I am getting  java.lang.NoSuchMethodException when running the following code. Any ideas how can I create a new class, add a method and invoke it?
  |     
  | 
  |    String aMethod="printSomething";
  |     	String src="public static void "+aMethod+"()" +
  | 		"{" +
  | 		"System.out.println(\"Kia Hoa\");" +
  | 		"}";
  |     	System.out.println(src);
  |         String className="mytest.MyTest";
  | 
  |         // we assume that called methods have no argument
  |         Class<?> params[] = {};
  |         Object paramsObj[] = {};
  |         try
  |         {
  |            	ClassPool pool = ClassPool.getDefault();
  |         	CtClass cc = pool.makeClass(className);        	
  |         	CtNewMethod.make(src,cc);
  |         	cc.stopPruning(true);
  |         	//cc.freeze();
  |         Loader cl = new Loader(pool);
  |         
  | //        Class<?> thisClass = Class.forName(className);
  |         Class thisClass = cl.loadClass(className);
  |         // get the method
  |         Method thisMethod = thisClass.getDeclaredMethod(aMethod);
  |         // call the method
  |         System.out.println
  |            (thisMethod.invoke(thisClass, paramsObj).toString());
  |         
  |         
  |         }
  |         catch(Exception e)
  |         {
  |         	e.printStackTrace();
  |         }
  |     	

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4093747#4093747

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4093747



More information about the jboss-user mailing list