Assertion fails when calling java.lang.reflect.Method.invoke()

[email protected] Fri, 18 Feb 2005 11:55:51 -0500
Newsgroups gmane.comp.java.vm.sablevm.devel
Message-ID <[email protected]>
Hi all,

While developing a Java application that starts another Java application, I met a strange 
SableVM 1.1.9 behaviour. An assertion fails when I put wrong parameters into 
Method.invoke() call. The error in the StartAppHelper.run() function:
  mainMethod.invoke(null, this.parameters); // parameters is String[]
should be
  mainMethod.invoke(null, this.parameterList); // parameterList is Object[]

This happens only when enabling SableVM debugging features (SableVM 1.1.9 compiled with 
options --enable-debugging-features --disable-errors-on-warnings).

Here is the command to compile it:
jikes -g -bootclasspath ~{...}/sablevm/sablevm-classpath/ Bridge.java

Here is the command to launch it:
/svm119/bin/sablevm -Y --classpath={...} -p 
java.library.path={...}--property=sablevm.bridge.start --property=java.compiler=NONE Bridge

The output with the right code or erroneous code without debugging features:
>JAVA : get the property
>start org.eclipseguide.hello.HelloWorld
>JAVA : helper creation
>JAVA : helper setup
>JAVA : helper start
>Hello, World! greetings from Sablevm
>Hello, World! greetings from Sablevm
>Hello, World! greetings from Sablevm

The output with the erroneous code and debugging features:
>JAVA : get the property
>start org.eclipseguide.hello.HelloWorld
>JAVA : helper creation
>JAVA : helper setup
>JAVA : helper start
>sablevm: util1.c:318: _svmf_get_reference_array_element: Assertion `indx >= 0 && indx < 
array->size' failed.
>Abandon

_______________________________________________
SableVM-devel mailing list
[email protected]
http://sablevm.org/lists/control/listinfo/sablevm-devel
HelloWorld.java (text/x-java, 794 B)
/*
 * Created on Jul 22, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package org.eclipseguide.hello;

/**
 * @author nizar
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class HelloWorld {

		public static void main(String[] args) {
				String message = "Hello, World! greetings from Sablevm";
			   	say(message);
		}
		
		public static void say(String msg) {
			   for (int i = 0; i < 3; i++) {
				   System.out.println(msg);				   
			  }
		}
		
		public static void hello() {
				   for (int j = 0; j < 3; j++) {
					   System.out.println("Hello from elsewhere");				   
				  }
		}
  }
Bridge.java (text/x-java, 2.1 KB)
    import java.util.*;
    import java.lang.reflect.*;

    class StartAppHelper extends Thread {

      private Class mainClass;
      private String className;
      private Method mainMethod;
      private String[] parameters;
      private Class[] parameterTypes;
      private Object[] parameterList;

      public StartAppHelper (String className, String[] parameters) {
        this.className = className;
        this.parameters = parameters;
	this.parameterTypes = new Class[1];
	this.parameterList = new Object[1];
	this.parameterList[0] = parameters;
      }

      boolean setup() {

	/* get the class whose name has been passed in parameters */
	try{
	    mainClass = Class.forName(this.className);
	}
	catch (Exception e){
	    System.out.println ("Error: exception thrown during class access!");
	    return false;
	}

	/* get the passed parameters type */
	this.parameterTypes[0] = this.parameters.getClass();

	/* get the main */
	try{
	    mainMethod = mainClass.getMethod("main", parameterTypes);
	}
	catch (Exception e){
	    System.out.println ("Error: exception thrown during method access!");
	    return false;
	}
	return true;
      }

      public void run()
      {
	/* call the main method */
	/* as main is static, one can invoke it from the Class not the instance */
	try{
	    mainMethod.invoke(null, this.parameters);
	}
	catch (Exception e){
	    System.out.println ("Error: exception thrown running method !");
	    System.exit (0);
        }       	
      }
    }

    class Bridge {
	static StartAppHelper helper;
  
	/* main */
    	public static void main(String[] args) {
            Bridge bridge = new Bridge();
            String start;

	    System.out.println("JAVA : get the property");
	    start = System.getProperty("sablevm.bridge.start");
	    System.out.println("start "+start);


	    System.out.println("JAVA : helper creation");
	    helper = new StartAppHelper (start, args);

	    System.out.println("JAVA : helper setup");
	    if (helper.setup() == false){
		System.out.println ("Error: an error occured during class setup!");
		System.exit (0);
	    }
   
	    System.out.println("JAVA : helper start");
	    helper.start();
   	}
    }