publicstaticvoid doClassLoad(Class<?> c) { try { if (c.getName().equals("Transform")) {
Redefinition.addCommonTransformationResult("Transform", CLASS_BYTES, DEX_BYTES);
doRedefine.accept(c);
System.out.println("retransformClasses on an unprepared class succeeded");
}
} catch (Throwable e) {
System.out.println("Trying to redefine: " + c + ". " + "Caught error " + e.getClass() + ": " + e.getMessage());
}
}
publicstatic ClassLoader getClassLoaderFor(String location) throws Exception { try { Class<?> class_loader_class = Class.forName("dalvik.system.PathClassLoader");
Constructor<?> ctor = class_loader_class.getConstructor(String.class, ClassLoader.class); /* on Dalvik, this is a DexFile; otherwise, it's null */ return (ClassLoader)ctor.newInstance(location + "/" + TEST_NAME + "-ex.jar",
Main.class.getClassLoader());
} catch (ClassNotFoundException e) { // Running on RI. Use URLClassLoader. returnnew java.net.URLClassLoader( new java.net.URL[] { new java.net.URL("file://" + location + "/classes-ex/") });
}
}
publicstaticvoid testCurrentThread() throws Throwable {
System.out.println("Redefine in ClassLoad on current thread.");
doRedefine = (c) -> { Redefinition.doCommonClassRetransformation(c); };
ClassLoader new_loader = getClassLoaderFor(System.getenv("DEX_LOCATION")); Class<?> klass = (Class<?>)new_loader.loadClass("Transform"); if (klass == null) { thrownew AssertionError("loadClass failed");
}
Object o = klass.newInstance();
System.out.println("Object out is: " + o);
}
publicstaticvoid testRemoteThread() throws Throwable {
System.out.println("Redefine during ClassLoad on another thread."); finalClass[] loaded = newClass[] { null, }; final CountDownLatch gotClass = new CountDownLatch(1); final CountDownLatch wokeUp = new CountDownLatch(1); Thread redef_thread = newThread(() -> { try {
gotClass.await();
wokeUp.countDown(); // This will wait until the otehr thread returns so we need to wake up the other thread // first.
Redefinition.doCommonClassRetransformation(loaded[0]);
} catch (Exception e) { thrownew Error("Failed to do redef!", e);
}
});
redef_thread.start();
doRedefine = (c) -> { try {
loaded[0] = c;
gotClass.countDown();
wokeUp.await(); // Let the other thread do some stuff. Thread.sleep(5000);
} catch (Exception e) { thrownew Error("Failed to do redef!", e);
}
};
ClassLoader new_loader = getClassLoaderFor(System.getenv("DEX_LOCATION")); Class<?> klass = (Class<?>)new_loader.loadClass("Transform"); if (klass == null) { thrownew AssertionError("loadClass failed");
}
Object o = klass.newInstance();
System.out.println("Object out is: " + o);
redef_thread.join();
System.out.println("Redefinition thread finished.");
}
publicstaticvoid main(String[] args) { // make sure we can do the transform.
Redefinition.setTestConfiguration(Redefinition.Config.COMMON_RETRANSFORM);
Redefinition.setPopRetransformations(false);
Redefinition.enableCommonRetransformation(true);
setupClassLoadHook(Thread.currentThread()); try {
testCurrentThread();
testRemoteThread();
} catch (Throwable e) {
System.out.println(e.toString());
e.printStackTrace(System.out);
}
clearClassLoadHook(Thread.currentThread());
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.