Reg: Method invoke using Reflection for Array Parameters

Hi,

I have a doubt regarding the Method access using reflection.

I tried to invoke the "newInstance" method of URLClassLoader class.

The "newInstance" method accepts URL Array as parameter. So what should

be the parameters I have to specify in

getDeclaredMethod("newInstance", parameters);

I used the parameters as

private static final Class[] parameters = new Class[]{URL[].class};

but it is throwing IllegalArgumentException.

I have given the Class definition in which I am using Reflection

Can anybody please advice me a wayout for this problem.

Thanks and Regards,

Shamjith

//Class definition starts

class OpenListener implements Listener{

private static final Class[] parameters = new Class[]{URL[].class};

public OpenListener() {

// TODO Auto-generated constructor stub

}

public void handleEvent(Event arg0) {

// TODO Auto-generated method stub

}

/* the method used for replacing the a jar file

to the class path even if it already exists */

public static void replace(URL u) throws IOException {

int i=0;

URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();

Class sysclass = URLClassLoader.class;

URL url[]=sysloader.getURLs();

URL url_backup[]=url;

for(;i< url.length;i++){

System.out.println(i+" th url is "+url.toString());

String path=url.toString();

if(path.endsWith(".jar")){

try{

String jarPath=path.substring(6);

JarFile runjar= new JarFile(jarPath);

if(runjar.getJarEntry("sign.txt")!=null){

/* sign.txt is used for uniquely identifying

a particular jar

*/

url=u;

}

}catch(JarException je){

je.printStackTrace();

System.exit(-1);

}

}

}

System.out.println("After modification");

for(i=0;i< url.length;i++){

System.out.println(i+" th url is "+url.toString());

}

try {

Method method = sysclass.getDeclaredMethod("newInstance",parameters);

/* the "newInstance" method accepts URL[] as argument

*/

method.setAccessible(true);

sysloader=(URLClassLoader)method.invoke(sysloader,url);

} catch (Throwable t) {

t.printStackTrace();

System.exit(-1);

}

}

}

//Class definition ends

[2413 byte] By [Shamjitha] at [2007-10-3 4:02:50]
# 1
Please create and post an example class which demonstrates your problem. The code you've posted doesn't, because it doesn't compile. It also lacks a main method.
YAT_Archivista at 2007-7-14 22:02:12 > top of Java-index,Core,Core APIs...
# 2

> The "newInstance" method accepts URL Array as

> parameter. So what should

> be the parameters I have to specify in

> getDeclaredMethod("newInstance", parameters);

>

> I used the parameters as

> private static final Class[] parameters = new

> Class[]{URL[].class};

> but it is throwing IllegalArgumentException.

new Class[]{new URL[0].getClass()}

or

new Class[]{java.lang.reflect.Array.newInstance(URL.class,1).getClass()}

ejpa at 2007-7-14 22:02:12 > top of Java-index,Core,Core APIs...