Search in sources :

Example 1 with UnmodifiableClassException

use of java.lang.instrument.UnmodifiableClassException in project pinpoint by naver.

the class DynamicTransformServiceTest method testRetransform_Fail_memoryleak_prevent.

@Test()
public void testRetransform_Fail_memoryleak_prevent() throws Exception {
    final Instrumentation instrumentation = mock(Instrumentation.class);
    when(instrumentation.isModifiableClass(any(Class.class))).thenReturn(true);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            throw new UnmodifiableClassException();
        }
    }).when(instrumentation).retransformClasses(any(Class.class));
    DefaultDynamicTransformerRegistry listener = new DefaultDynamicTransformerRegistry();
    final ClassFileTransformer classFileTransformer = mock(ClassFileTransformer.class);
    DynamicTransformService dynamicTransformService = new DynamicTransformService(instrumentation, listener);
    try {
        dynamicTransformService.retransform(String.class, classFileTransformer);
        Assert.fail("expected retransform fail");
    } catch (Exception e) {
    }
    Assert.assertEquals(listener.size(), 0);
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) UnmodifiableClassException(java.lang.instrument.UnmodifiableClassException) Instrumentation(java.lang.instrument.Instrumentation) UnmodifiableClassException(java.lang.instrument.UnmodifiableClassException) Test(org.junit.Test)

Example 2 with UnmodifiableClassException

use of java.lang.instrument.UnmodifiableClassException in project mockito by mockito.

the class InlineBytecodeGenerator method triggerRetransformation.

private <T> void triggerRetransformation(MockFeatures<T> features) {
    Set<Class<?>> types = new HashSet<Class<?>>();
    Class<?> type = features.mockedType;
    do {
        if (mocked.add(type)) {
            types.add(type);
            addInterfaces(types, type.getInterfaces());
        }
        type = type.getSuperclass();
    } while (type != null);
    if (!types.isEmpty()) {
        try {
            instrumentation.retransformClasses(types.toArray(new Class<?>[types.size()]));
        } catch (UnmodifiableClassException exception) {
            for (Class<?> failed : types) {
                mocked.remove(failed);
            }
            throw new MockitoException("Could not modify all classes " + types, exception);
        }
    }
}
Also used : UnmodifiableClassException(java.lang.instrument.UnmodifiableClassException) MockitoException(org.mockito.exceptions.base.MockitoException) HashSet(java.util.HashSet)

Example 3 with UnmodifiableClassException

use of java.lang.instrument.UnmodifiableClassException 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 4 with UnmodifiableClassException

use of java.lang.instrument.UnmodifiableClassException in project openj9 by eclipse.

the class AgentMain method redefineClass.

public static void redefineClass(Class<?> theClass, byte[] classBytes) {
    try {
        logger.debug("Class<?> theClass is " + theClass);
        logger.debug("byte[] classBytes is " + classBytes);
        Assert.assertNotNull(theClass, "Class<?> theClass is null");
        Assert.assertNotNull(classBytes, "byte[] classBytes is null");
        ClassDefinition cdf = new ClassDefinition(theClass, classBytes);
        try {
            instrumentation.redefineClasses(cdf);
        } catch (UnmodifiableClassException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    } catch (java.lang.NullPointerException ex) {
        Assert.fail("Unexpected exception occored " + ex.getMessage(), ex);
    }
}
Also used : UnmodifiableClassException(java.lang.instrument.UnmodifiableClassException) ClassDefinition(java.lang.instrument.ClassDefinition)

Example 5 with UnmodifiableClassException

use of java.lang.instrument.UnmodifiableClassException in project fakereplace by fakereplace.

the class Fakereplace method premain.

public static void premain(java.lang.String s, java.lang.instrument.Instrumentation i) {
    AgentOptions.setup(s);
    inst = i;
    final Set<Extension> extension = getIntegrationInfo(ClassLoader.getSystemClassLoader());
    replaceableClassSelectors.add(DefaultReplaceableClassSelector.INSTANCE);
    // first we need to instrument the class loaders
    final Set<Class> cls = new HashSet<>();
    for (Class c : inst.getAllLoadedClasses()) {
        if (ClassLoader.class.isAssignableFrom(c)) {
            cls.add(c);
        }
    }
    final ClassLoaderTransformer classLoaderTransformer = new ClassLoaderTransformer();
    final MainTransformer mainTransformer = new MainTransformer();
    Fakereplace.mainTransformer = mainTransformer;
    inst.addTransformer(mainTransformer, true);
    mainTransformer.addTransformer(classLoaderTransformer);
    try {
        inst.retransformClasses(cls.toArray(EMPTY_CL_ARRAY));
    } catch (UnmodifiableClassException e) {
        e.printStackTrace();
    }
    mainTransformer.addTransformer(new IntegrationActivationTransformer(extension));
    mainTransformer.addTransformer(new AnnotationTransformer());
    mainTransformer.addTransformer(new FieldReplacementTransformer());
    mainTransformer.addTransformer(new MethodReplacementTransformer());
    mainTransformer.addTransformer(new Transformer());
    mainTransformer.addTransformer(new ClassInfoTransformer());
    mainTransformer.setRetransformationStarted(false);
    mainTransformer.setLogClassRetransformation(true);
}
Also used : FieldReplacementTransformer(org.fakereplace.replacement.FieldReplacementTransformer) MethodReplacementTransformer(org.fakereplace.replacement.MethodReplacementTransformer) AnnotationTransformer(org.fakereplace.replacement.AnnotationTransformer) UnmodifiableClassException(java.lang.instrument.UnmodifiableClassException) Extension(org.fakereplace.Extension) AnnotationTransformer(org.fakereplace.replacement.AnnotationTransformer) FieldReplacementTransformer(org.fakereplace.replacement.FieldReplacementTransformer) AddedClass(org.fakereplace.replacement.AddedClass) MethodReplacementTransformer(org.fakereplace.replacement.MethodReplacementTransformer) HashSet(java.util.HashSet)

Aggregations

UnmodifiableClassException (java.lang.instrument.UnmodifiableClassException)10 File (java.io.File)3 IOException (java.io.IOException)3 ClassDefinition (java.lang.instrument.ClassDefinition)3 HashSet (java.util.HashSet)3 FileOutputStream (java.io.FileOutputStream)2 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)2 Instrumentation (java.lang.instrument.Instrumentation)2 URL (java.net.URL)2 Test (org.junit.Test)2 MockitoException (org.mockito.exceptions.base.MockitoException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 OutputStream (java.io.OutputStream)1 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1