@Override
public boolean transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, ClassFile file, Set<Class<?>> classesToRetransform, ChangedClassImpl changedClass, Set<MethodInfo> modifiedMethods, boolean replaceable) throws IllegalClassFormatException, BadBytecode, DuplicateMemberException {
if (className.equals("com/sun/beans/introspect/ClassInfo")) {
file.addInterface(Runnable.class.getName());
MethodInfo run = new MethodInfo(file.getConstPool(), "run", "()V");
run.setAccessFlags(AccessFlag.PUBLIC);
Bytecode b = new Bytecode(file.getConstPool(), 1, 1);
b.addGetstatic(file.getName(), "CACHE", "Lcom/sun/beans/util/Cache;");
b.addInvokevirtual("com/sun/beans/util/Cache", "clear", "()V");
b.add(Opcode.RETURN);
run.setCodeAttribute(b.toCodeAttribute());
file.addMethod(run);
MethodInfo m = file.getMethod("<init>");
CodeIterator iterator = m.getCodeAttribute().iterator();
int pos = 0;
while (iterator.hasNext()) {
pos = iterator.next();
}
b = new Bytecode(file.getConstPool(), 1, 0);
b.add(Bytecode.ALOAD_0);
b.addPutstatic(ClassInfoTransformer.class.getName(), "clearAction", "Ljava/lang/Runnable;");
iterator.insert(pos, b.get());
m.getCodeAttribute().computeMaxStack();
return true;
}
return false;
}