Hi,all,
I write a class named Bsafproxy which will change the method of the org.jdesktop.application.ResourceMap in the basf-1.9.jar; I don't know why it can not work, who will do me a faver, give me some idears, thanks.
BTW, BsafProxy.java is my class, and the bsaf-1.9.jar is witch i wanna to modifed.
package org.eric.javassist;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.Loader;
import javassist.NotFoundException;
/**
* @author apple
*
*/
public class BsafProxy {
/**
* @param args
*/
public static void main(String[] args) {
try {
ClassPool csPool = ClassPool.getDefault();
csPool.insertClassPath("/Users/apple/Documents/Eric/work/eclipse/workspace/HelloMac/bsaf-1.9.jar");
CtClass demoClass = csPool.get("org.jdesktop.application.ResourceMap");
CtMethod populateResourceMap = demoClass.getDeclaredMethod("populateResourceMap");
demoClass.removeMethod(populateResourceMap);
CtMethod newPopulate = CtNewMethod.make(getContent(), demoClass);
demoClass.addMethod(newPopulate);
// URL clazz = csPool.find("org.jdesktop.application.ResourceMap");
Loader loader = new Loader(csPool);
Class clazz = loader.loadClass("org.jdesktop.application.ResourceMap");
clazz.newInstance();
} catch (NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CannotCompileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static String getContent() {
// private void populateResourceMap(String bundleName, Map<String, Object> bundlesMap) {
// try {
// ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale, classLoader);
// Enumeration<String> keys = bundle.getKeys();
// while (keys.hasMoreElements()) {
// String key = keys.nextElement();
// bundlesMap.put(key, bundle.getObject(key));
// }
// } catch (MissingResourceException ignore) {
// /* bundleName is just a location to check, it's not
// * guaranteed to name a ResourceBundle
// */
// }
// }
StringBuffer content = new StringBuffer();
content.append("private void populateResourceMap(String bundleName, java.util.Map bundlesMap) {");
content.append("try {");
content.append(" java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle($1, locale, classLoader);");
content.append(" java.util.Enumeration keys = bundle.getKeys();");
content.append(" while (keys.hasMoreElements()) {");
content.append(" String key = keys.nextElement();");
content.append(" $2.put(key, bundle.getObject(key));");
content.append(" }");
content.append("} catch (java.util.MissingResourceException ignore) { }");
content.append("}");
return content.toString();
}
}
ouput:
Exception in thread "main" java.lang.VerifyError: (class: org/jdesktop/application/ResourceMap, method: populateResourceMap signature: (Ljava/lang/String;Ljava/util/Map;)V) Incompatible argument to function
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
at java.lang.Class.getConstructor0(Class.java:2699)
at java.lang.Class.newInstance0(Class.java:326)
at java.lang.Class.newInstance(Class.java:308)
at org.eric.javassist.BsafProxy.main(BsafProxy.java:38)