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);
}
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);
}
}
}
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;
}
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);
}
}
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);
}
Aggregations