Search in sources :

Example 6 with ClassDefinition

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;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UnmodifiableClassException(java.lang.instrument.UnmodifiableClassException) ClassDefinition(java.lang.instrument.ClassDefinition) TypeDescription(net.bytebuddy.description.type.TypeDescription) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 7 with ClassDefinition

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);
    }
}
Also used : ClassDefinition(java.lang.instrument.ClassDefinition) IOException(java.io.IOException)

Example 8 with ClassDefinition

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 });
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ClassDefinition(java.lang.instrument.ClassDefinition) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

ClassDefinition (java.lang.instrument.ClassDefinition)8 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 TypeDescription (net.bytebuddy.description.type.TypeDescription)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 Instrumentation (java.lang.instrument.Instrumentation)1 UnmodifiableClassException (java.lang.instrument.UnmodifiableClassException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ClassFileLocator (net.bytebuddy.dynamic.ClassFileLocator)1 BaseMatcher (org.hamcrest.BaseMatcher)1 Description (org.hamcrest.Description)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 VirtualFile (org.webpieces.util.file.VirtualFile)1