1 25 26 package net.yagga.util; 27 28 import java.lang.reflect.*; 29 import java.io.IOException ; 30 import java.util.jar.*; 31 import java.util.*; 32 import java.io.*; 33 34 64 public class MetaJarClassLoader extends ClassLoader 65 { 66 private MetaJarResources metaJarResources; 67 private String jarFile; 68 72 73 public MetaJarClassLoader(String jFile) 74 { 75 super(); 76 jarFile=jFile; 77 metaJarResources= new MetaJarResources(jarFile); 78 } 79 80 87 public String getActualJarName(){ 88 return metaJarResources.getActualJarName(); 89 } 90 91 92 97 public String getMainClassName() 98 { 99 Manifest mf=metaJarResources.getManifest(); 100 return mf != null ? mf.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS) : null; 101 } 102 103 113 public Object invokeMethod(String className, String method, Object [] args) 114 throws ClassNotFoundException , 115 NoSuchMethodException , 116 InvocationTargetException 117 { 118 Class c = loadClass(className); 119 120 Method m = c.getMethod(method, new Class [] { args.getClass() }); 122 m.setAccessible(true); 124 int mods = m.getModifiers(); 125 if (!Modifier.isStatic(mods) || 126 !Modifier.isPublic(mods)) { 127 throw new NoSuchMethodException (method); 128 } 129 Object ret=null; 130 try { 131 ret= m.invoke(null, new Object [] { args }); 132 } catch (IllegalAccessException e) { 133 } 135 return ret; 136 } 137 138 147 public void invokeClass(String name, String [] args) 148 throws ClassNotFoundException , 149 NoSuchMethodException , 150 InvocationTargetException 151 { 152 simpleInvokeMethod(name,"main",new Object []{args}); 153 171 } 172 173 189 190 public Object simpleInvokeMethod(String className, String method, Object [] args) 191 throws ClassNotFoundException , 192 NoSuchMethodException , 193 InvocationTargetException 194 { 195 Class cl=loadClass(className); 196 return MetaUt.simpleInvokeStaticMethod(cl,method,args); 197 } 198 199 207 protected Class findClass(String className) throws ClassNotFoundException 208 { 209 String urlName=className.replace('.', '/') + ".class"; 210 212 byte b1[]=metaJarResources.getBytes(urlName); 214 try{ 215 return defineClass(className,b1,0,b1.length); 216 }catch(ClassFormatError cfe){ 217 Ut.error("'"+className+"':"+cfe); 218 } 219 return null; 220 } 221 222 } 223 224 225 226 227 | Popular Tags |