use of java.lang.instrument.ClassDefinition in project byte-buddy by raphw.
the class ClassReloadingStrategy method load.
@Override
public Map<TypeDescription, Class<?>> load(ClassLoader classLoader, Map<TypeDescription, byte[]> types) {
Map<String, Class<?>> availableTypes = new HashMap<String, Class<?>>(preregisteredTypes);
for (Class<?> type : instrumentation.getInitiatedClasses(classLoader)) {
availableTypes.put(TypeDescription.ForLoadedType.getName(type), type);
}
Map<Class<?>, ClassDefinition> classDefinitions = new ConcurrentHashMap<Class<?>, ClassDefinition>();
Map<TypeDescription, Class<?>> loadedClasses = new HashMap<TypeDescription, Class<?>>();
Map<TypeDescription, byte[]> unloadedClasses = new LinkedHashMap<TypeDescription, byte[]>();
for (Map.Entry<TypeDescription, byte[]> entry : types.entrySet()) {
Class<?> type = availableTypes.get(entry.getKey().getName());
if (type != null) {
classDefinitions.put(type, new ClassDefinition(type, entry.getValue()));
loadedClasses.put(entry.getKey(), type);
} else {
unloadedClasses.put(entry.getKey(), entry.getValue());
}
}
try {
strategy.apply(instrumentation, classDefinitions);
if (!unloadedClasses.isEmpty()) {
loadedClasses.putAll((classLoader == null ? bootstrapInjection.make(instrumentation) : new ClassInjector.UsingReflection(classLoader)).inject(unloadedClasses));
}
} catch (ClassNotFoundException exception) {
throw new IllegalArgumentException("Could not locate classes for redefinition", exception);
} catch (UnmodifiableClassException exception) {
throw new IllegalStateException("Cannot redefine specified class", exception);
}
return loadedClasses;
}
use of java.lang.instrument.ClassDefinition in project jdk8u_jdk by JetBrains.
the class RedefineAgent method testRedefine.
// test transform and redefine for an attached agent
public static void testRedefine(Instrumentation inst) throws Exception {
Class[] classes = inst.getAllLoadedClasses();
for (Class k : classes) {
if (k.getName().equals(targetName)) {
throw new Exception("RedefineAgent Test error: class " + targetName + " has already been loaded.");
}
}
inst.addTransformer(new RedefineAgent());
ClassLoader.getSystemClassLoader().loadClass(targetName);
classes = inst.getAllLoadedClasses();
Class targetClass = null;
for (Class k : classes) {
if (k.getName().equals(targetName)) {
targetClass = k;
break;
}
}
if (targetClass == null) {
throw new Exception("RedefineAgent Test error: class " + targetName + " not loaded.");
}
if (classfilebytes == null) {
throw new Exception("RedefineAgent Error(6439234): no transform call for class " + targetName);
}
ClassDefinition cd = new ClassDefinition(targetClass, classfilebytes);
inst.redefineClasses(cd);
System.out.println("RedefineAgent did redefine.");
if (gotRedefineTransform) {
System.out.println("RedefineAgent got redefine transform.");
} else {
throw new Exception("RedefineAgent Error(6439234): no transform call for redefine " + targetName);
}
}
use of java.lang.instrument.ClassDefinition in project jdk8u_jdk by JetBrains.
the class RedefineMethodWithAnnotationsApp method doRedefine.
private static void doRedefine(Class<?> clazz) throws Exception {
// Load the second version of this class.
File f = new File(clazz.getName() + ".class");
System.out.println("Reading test class from " + f);
InputStream redefineStream = new FileInputStream(f);
byte[] redefineBuffer = NamedBuffer.loadBufferFromStream(redefineStream);
ClassDefinition redefineParamBlock = new ClassDefinition(clazz, redefineBuffer);
RedefineMethodWithAnnotationsAgent.getInstrumentation().redefineClasses(new ClassDefinition[] { redefineParamBlock });
}
Aggregations