public class test {
public static void main(String[] args){
ClassPool pool = ClassPool.getDefault();
try {
CtClass cc=null;
cc = pool.get("com.test');
CtMethod m= cc.getDeclaredMethod("b") ;
m.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
try {
cc.writeFile();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (CannotCompileException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
} catch (NotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
此博客展示了一段Java代码,通过ClassPool获取类,修改类中方法的修饰符为public static,最后尝试将修改后的类写入文件。代码中处理了可能出现的NotFoundException、IOException和CannotCompileException等异常。
1193

被折叠的 条评论
为什么被折叠?



