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

