本文整理匯總了Java中java.lang.instrument.ClassDefinition類的典型用法代碼示例。如果您正苦於以下問題:Java ClassDefinition類的具體用法?Java ClassDefinition怎麽用?Java ClassDefinition使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ClassDefinition類屬於java.lang.instrument包,在下文中一共展示了ClassDefinition類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: agentmain
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
public static void agentmain(String args,Instrumentation inst){
try{
System.err.println("傳進來的參數為"+args);
File f = new File(args);
byte[] targetClassFile = new byte[(int)f.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(f));
dis.readFully(targetClassFile);
dis.close();
DynamicClassLoader myLoader = new DynamicClassLoader();
Class targetClazz = myLoader.findClass(targetClassFile);
System.err.println("目標class類全路徑為"+targetClazz.getName());
ClassDefinition clazzDef = new ClassDefinition(Class.forName(targetClazz.getName()), targetClassFile);
inst.redefineClasses(clazzDef);
System.err.println("重新定義"+args+"完成!!");
}catch(Exception e){
e.printStackTrace();
}
}
示例2: redefineClasses
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
public void
redefineClasses(ClassDefinition... definitions)
throws ClassNotFoundException {
if (!isRedefineClassesSupported()) {
throw new UnsupportedOperationException("redefineClasses is not supported in this environment");
}
if (definitions == null) {
throw new NullPointerException("null passed as 'definitions' in redefineClasses");
}
for (int i = 0; i < definitions.length; ++i) {
if (definitions[i] == null) {
throw new NullPointerException("element of 'definitions' is null in redefineClasses");
}
}
if (definitions.length == 0) {
return; // short-circuit if there are no changes requested
}
redefineClasses0(mNativeAgent, definitions);
}
示例3: redefineClasses
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
public static boolean redefineClasses(ClassDefinition[] classDefinitions)
{
try
{
if (isRedefineClassesSupported())
{
getInstrumentation().redefineClasses(classDefinitions);
return true;
}
}
catch (Throwable throwable)
{
if (classDefinitions != null)
{
for (ClassDefinition classDefinition : classDefinitions)
{
logger.info(classDefinition.getDefinitionClass().toString());
}
}
logger.log(Level.INFO, throwable.getMessage(), throwable);
}
return false;
}
示例4: reload
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
private void reload(Entry e)
throws IOException, ClassNotFoundException, UnmodifiableClassException {
System.err.println(e.file);
cdefs.clear();
if (e.loaderRef != null) {
ClassLoader cl = e.loaderRef.get();
if (cl != null) {
request(e, cl);
if (e.children != null) {
for (Entry ce : e.children) {
request(ce, cl);
}
}
// System.err.println(cdefs);
Agent.inst.redefineClasses(cdefs.toArray(new ClassDefinition[0]));
} else {
e.loaderRef = null;
}
}
}
示例5: loadClass
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
@Override
public synchronized Class<?> loadClass(String name, byte[] barr) throws UnmodifiableClassException {
Class<?> clazz=null;
try {
clazz = loadClass(name,false,false); // we do not load existing class from disk
} catch (ClassNotFoundException cnf) {}
// if class already exists
if(clazz!=null) {
try {
InstrumentationFactory.getInstrumentation(config).redefineClasses(new ClassDefinition(clazz,barr));
}
catch (ClassNotFoundException e) {
// the documentation clearly sais that this exception only exists for backward compatibility and never happen
}
return clazz;
}
// class not exists yet
return _loadClass(name, barr);
}
示例6: loadClass
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
@Override
public synchronized Class<?> loadClass(String name, byte[] barr) throws UnmodifiableClassException {
Class<?> clazz=null;
try {
clazz = loadClass(name);
} catch (ClassNotFoundException cnf) {}
// if class already exists
if(clazz!=null) {
try {
InstrumentationFactory.getInstrumentation(config).redefineClasses(new ClassDefinition(clazz,barr));
}
catch (ClassNotFoundException e) {
// the documentation clearly sais that this exception only exists for backward compatibility and never happen
}
aprint.e("redefined:memory:"+clazz.getName());
return clazz;
}
// class not exists yet
return _loadClass(name, barr);
}
示例7: redefineClasses
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
public void
redefineClasses(ClassDefinition[] definitions)
throws ClassNotFoundException {
if (!isRedefineClassesSupported()) {
throw new UnsupportedOperationException("redefineClasses is not supported in this environment");
}
if (definitions == null) {
throw new NullPointerException("null passed as 'definitions' in redefineClasses");
}
for (int i = 0; i < definitions.length; ++i) {
if (definitions[i] == null) {
throw new NullPointerException("element of 'definitions' is null in redefineClasses");
}
}
if (definitions.length == 0) {
return; // short-circuit if there are no changes requested
}
redefineClasses0(mNativeAgent, definitions);
}
示例8: redefineClasses
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
public void redefineClasses(Map<Class<?>, byte[]> classes) throws ClassNotFoundException,
UnmodifiableClassException {
if (PluginManager.getInstance().getInstrumentation() == null) {
throw new IllegalStateException("Instrumentation agent is not properly installed!");
}
ClassDefinition[] definitions = new ClassDefinition[classes.size()];
int i = 0;
for (Map.Entry<Class<?>, byte[]> entry : classes.entrySet()) {
definitions[i++] = new ClassDefinition(entry.getKey(), entry.getValue());
URL classResource = getClassResource(entry.getKey());
try (OutputStream fileWriter = new FileOutputStream(new File(classResource.toURI()))) {
fileWriter.write(entry.getValue());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
PluginManager.getInstance().getInstrumentation().redefineClasses(definitions);
}
示例9: hotswap
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
/**
* Redefine the supplied set of classes using the supplied bytecode.
*
* This method operates on a set in order to allow interdependent changes to more than one class at the same time
* (a redefinition of class A can require a redefinition of class B).
*
* @param reloadMap class -> new bytecode
* @see java.lang.instrument.Instrumentation#redefineClasses(java.lang.instrument.ClassDefinition...)
*/
public void hotswap(Map<Class<?>, byte[]> reloadMap) {
if (instrumentation == null) {
throw new IllegalStateException("Plugin manager is not correctly initialized - no instrumentation available.");
}
synchronized (reloadMap) {
ClassDefinition[] definitions = new ClassDefinition[reloadMap.size()];
String[] classNames = new String[reloadMap.size()];
int i = 0;
for (Map.Entry<Class<?>, byte[]> entry : reloadMap.entrySet()) {
classNames[i] = entry.getKey().getName();
definitions[i++] = new ClassDefinition(entry.getKey(), entry.getValue());
}
try {
LOGGER.reload("Reloading classes {} (autoHotswap)", Arrays.toString(classNames));
synchronized (hotswapLock) {
instrumentation.redefineClasses(definitions);
}
LOGGER.debug("... reloaded classes {} (autoHotswap)", Arrays.toString(classNames));
} catch (Exception e) {
throw new IllegalStateException("Unable to redefine classes", e);
}
reloadMap.clear();
}
}
示例10: redefine
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
@Override
public void redefine(ClassBytecodes[] cbcs) throws ClassInstallException, NotImplementedException, EngineTerminationException {
int count = cbcs.length;
boolean[] status = new boolean[cbcs.length];
int success = 0;
NbRemoteLoader ldr = prepareClassLoader();
for (int i = 0; i < count; i++) {
String name = cbcs[i].name();
byte[] replaceBytecode = cbcs[i].bytecodes();
Long id = ldr.getClassId(name);
if (id != null) {
Class defined = ldr.getClassOfId(id);
try {
agent.getInstrumentation().redefineClasses(
new ClassDefinition(defined, replaceBytecode)
);
status[i] = true;
success++;
} catch (ClassNotFoundException | UnmodifiableClassException ex) {
Logger.getLogger(AgentWorker.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (success < count) {
throw new ClassInstallException("Could not redefine classes", status);
}
}
}
示例11: apply
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
/**
* Update edits made to classes in the current VM.
*/
public static void apply() {
try {
Recaf.INSTANCE.logging.info("Applying class redefinintions:");
ClassDefinition[] definitions = Marker.getDefinitions();
for (ClassDefinition define : definitions) {
Recaf.INSTANCE.logging.info(define.getDefinitionClass().getName(), 1);
}
instrument.redefineClasses(definitions);
Recaf.INSTANCE.logging.info("Done applying redefinitions.");
} catch (Exception e) {
Recaf.INSTANCE.logging.error(e);
}
}
示例12: getDefinitions
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
public static ClassDefinition[] getDefinitions() throws Exception {
Class[] classes = getClasses();
ClassDefinition[] definitions = new ClassDefinition[classes.length];
for (int i = 0; i < classes.length; i++) {
Class clazz = classes[i];
byte[] clazzBytes = Asm.toBytes(getNode(clazz));
definitions[i] = new ClassDefinition(clazz, clazzBytes);
}
return definitions;
}
示例13: doRedefine
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
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});
}
示例14: doRedefine
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
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.getAbsolutePath());
InputStream redefineStream = new FileInputStream(f);
byte[] redefineBuffer = NamedBuffer.loadBufferFromStream(redefineStream);
ClassDefinition redefineParamBlock = new ClassDefinition(
clazz, redefineBuffer);
RedefineMethodInBacktraceAgent.getInstrumentation().redefineClasses(
new ClassDefinition[] {redefineParamBlock});
}
示例15: redefine
import java.lang.instrument.ClassDefinition; //導入依賴的package包/類
static void redefine() {
if (instr == null) {
System.out.println("NOT REDEFINED");
return;
}
ClassDefinition cd = new ClassDefinition(CLS, getClassFile(1));
try {
instr.redefineClasses(cd);
} catch (Exception e) {
throw new Error(e);
}
System.out.println("REDEFINED");
}