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

ssiddiqu do-not-reply at jboss.com
Tue Oct 9 02:28:53 EDT 2007


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=4092846#4092846

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



More information about the jboss-user mailing list