final Map<Class,byte[]> classes) {
Log log = conf.getLog(OpenJPAConfiguration.LOG_ENHANCE);
if (classes == null || classes.size() == 0)
return;
Instrumentation inst = null;
ClassFileTransformer t = null;
try {
inst = InstrumentationFactory.getInstrumentation();
Class[] array = classes.keySet().toArray(new Class[classes.size()]);
if (JavaVersions.VERSION >= 6) {
log.trace(_loc.get("retransform-types", classes.keySet()));
t = new ClassFileTransformer() {
public byte[] transform(ClassLoader loader, String clsName,
Class<?> classBeingRedefined, ProtectionDomain pd,
byte[] classfileBuffer) {
return classes.get(classBeingRedefined);
}
};
// these are Java 6 methods, and we don't have a Java 6 build
// module yet. The cost of reflection here is negligible
// compared to the redefinition / enhancement costs in total,
// so this should not be a big problem.
Method meth = inst.getClass().getMethod("addTransformer",
new Class[] { ClassFileTransformer.class, boolean.class });
meth.invoke(inst, new Object[] { t, true });
meth = inst.getClass().getMethod("retransformClasses",
new Class[] { array.getClass() });
meth.invoke(inst, new Object[] { array });
} else {
log.trace(_loc.get("redefine-types", classes.keySet()));
// in a Java 5 context, we can use class redefinition instead
ClassDefinition[] defs = new ClassDefinition[array.length];
for (int i = 0; i < defs.length; i++)
defs[i] = new ClassDefinition(array[i],
classes.get(array[i]));
inst.redefineClasses(defs);
}
} catch (NoSuchMethodException e) {
throw new InternalException(e);
} catch (IllegalAccessException e) {
throw new InternalException(e);
} catch (InvocationTargetException e) {
throw new UserException(e.getCause());
} catch (IOException e) {
throw new InternalException(e);
} catch (ClassNotFoundException e) {
throw new InternalException(e);
} catch (UnmodifiableClassException e) {
throw new InternalException(e);
} finally {
if (inst != null && t != null)
inst.removeTransformer(t);
}
}