java.lang.instrument.Instrumentation类的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(403)

本文整理了Java中java.lang.instrument.Instrumentation类的一些代码示例,展示了Instrumentation类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Instrumentation类的具体详情如下:
包路径:java.lang.instrument.Instrumentation
类名称:Instrumentation

Instrumentation介绍

暂无

代码示例

代码示例来源:origin: oldmanpushcart/greys-anatomy

inst.addTransformer(getClassByteArrayFileTransformer, true);
  final int size = classes.size();
  final Class<?>[] classArray = new Class<?>[size];
  arraycopy(classes.toArray(), 0, classArray, 0, size);
  inst.retransformClasses(classArray);
} finally {
  inst.removeTransformer(getClassByteArrayFileTransformer);

代码示例来源:origin: stackoverflow.com

import java.lang.instrument.Instrumentation;

public class ObjectSizeFetcher {
  private static Instrumentation instrumentation;

  public static void premain(String args, Instrumentation inst) {
    instrumentation = inst;
  }

  public static long getObjectSize(Object o) {
    return instrumentation.getObjectSize(o);
  }
}

代码示例来源:origin: redisson/redisson

/**
   * {@inheritDoc}
   */
  public Iterable<Iterable<Class<?>>> resolve(Instrumentation instrumentation) {
    return Collections.<Iterable<Class<?>>>singleton(Arrays.<Class<?>>asList(instrumentation.getAllLoadedClasses()));
  }
}

代码示例来源:origin: btraceio/btrace

public static void retransform(String runtimeName, Class<?> clazz) {
  try {
    BTraceRuntime rt = runtimes.get(runtimeName);
    if (rt != null && rt.instrumentation.isModifiableClass(clazz)) {
      rt.instrumentation.retransformClasses(clazz);
    }
  } catch (Throwable e) {
    warning(e);
  }
}

代码示例来源:origin: redisson/redisson

/**
   * {@inheritDoc}
   */
  public void addTransformer(Instrumentation instrumentation, ClassFileTransformer classFileTransformer, boolean canRetransform) {
    if (canRetransform) {
      throw new UnsupportedOperationException("The current VM does not support retransformation");
    }
    instrumentation.addTransformer(classFileTransformer);
  }
}

代码示例来源:origin: electronicarts/ea-async

public static void agentmain(String agentArgs, Instrumentation inst)
  {
    Transformer transformer = new Transformer();
    inst.addTransformer(transformer, true);
    f1:
    for (Class<?> clazz : inst.getAllLoadedClasses())
    {
      if (inst.isModifiableClass(clazz)
          && !clazz.getName().startsWith("java.")
          && !clazz.getName().startsWith("javax.")
          && !clazz.getName().startsWith("sun."))
      {
        try
        {
          if (transformer.needsInstrumentation(clazz))
          {
            inst.retransformClasses(clazz);
          }
        }
        catch (Exception | Error e)
        {
          e.printStackTrace();
        }
      }
    }
    System.setProperty(Transformer.EA_ASYNC_RUNNING, "true");
  }
}

代码示例来源:origin: glowroot/glowroot

if (instrumentation != null) {
  for (File pluginJar : pluginCache.pluginJars()) {
    instrumentation.appendToBootstrapClassLoaderSearch(new JarFile(pluginJar));
  instrumentation.addTransformer(pointcutClassFileTransformer);
  checkNotNull(instrumentation).removeTransformer(pointcutClassFileTransformer);
  WeavingClassFileTransformer transformer =
      new WeavingClassFileTransformer(weaver, instrumentation);
  if (instrumentation.isRetransformClassesSupported()) {
    instrumentation.addTransformer(transformer, true);
    jvmRetransformClassesSupported = true;
  } else {
    instrumentation.addTransformer(transformer);
    jvmRetransformClassesSupported = false;
          + " applied to it: {}", entry.getKey(), entry.getValue());
    instrumentation.removeTransformer(preCheckClassFileTransformer);
  Class<?>[] initialLoadedClasses = instrumentation.getAllLoadedClasses();
  adviceCache.initialReweave(initialLoadedClasses);
  logAnyImportantClassLoadedPriorToWeavingInit(initialLoadedClasses, glowrootJarFile,

代码示例来源:origin: net.orfjackal.retrolambda/retrolambda

public static void premain(String agentArgs, Instrumentation inst) throws Exception {
  // Append the agent JAR to the bootstrap search path so that the instrumented InnerClassLambdaMetaFactory
  // could refer to Agent.
  inst.appendToBootstrapClassLoaderSearch(new JarFile(getAgentJarFile()));
  inst.addTransformer(new InnerClassLambdaMetafactoryTransformer(), true);
  inst.retransformClasses(Class.forName("java.lang.invoke.InnerClassLambdaMetafactory"));
}

代码示例来源:origin: scouter-project/scouter

private void redefine(StringSet clazzes) {
  Class[] loadedClasses = JavaAgent.getInstrumentation().getAllLoadedClasses();
  ArrayList<ClassDefinition> definitionList = new ArrayList<ClassDefinition>();
  boolean allSuccess = true;
  for (int i = 0; i < loadedClasses.length; i++) {
    if (clazzes.hasKey(loadedClasses[i].getName())) {
      try {
        byte[] buff = ClassUtil.getByteCode(loadedClasses[i]);
        if (buff == null) {
          continue;
        }
        definitionList.add(new ClassDefinition(loadedClasses[i], buff));
      } catch (Exception e) {
        allSuccess = false;
        break;
      }
    }
  }
  if (definitionList.size() > 0 && allSuccess) {
    try {
      JavaAgent.getInstrumentation().redefineClasses(definitionList.toArray(new ClassDefinition[definitionList.size()]));
    } catch (Throwable th) {
      Logger.println("A183", "redefine error : " + loadedClasses);
    }
  }
}

代码示例来源:origin: org.jmockit/jmockit-coverage

public static void resetConfiguration()
{
 Startup.instrumentation().removeTransformer(instance);
 CoverageData.instance().clear();
 Startup.instrumentation().addTransformer(create(false));
 instance.outputPendingForShutdown = false;
}

代码示例来源:origin: btraceio/btrace

debugPrint("filtering loaded classes");
ClassCache cc = ClassCache.getInstance();
for (Class c : inst.getAllLoadedClasses()) {
  if (c != null) {
    cc.get(c);
    if (inst.isModifiableClass(c) &&  isCandidate(c)) {
      debugPrint("candidate " + c + " added");
      list.add(c);
      try {
        debugPrint("Attempting to retransform class: " + c.getName());
        inst.retransformClasses(c);
      } catch (VerifyError e) {
        debugPrint("verification error: " + c.getName());
    inst.retransformClasses(classes);

代码示例来源:origin: spring-projects/spring-framework

/**
 * Remove all registered transformers, in inverse order of registration.
 */
public void removeTransformers() {
  synchronized (this.transformers) {
    if (this.instrumentation != null && !this.transformers.isEmpty()) {
      for (int i = this.transformers.size() - 1; i >= 0; i--) {
        this.instrumentation.removeTransformer(this.transformers.get(i));
      }
      this.transformers.clear();
    }
  }
}

代码示例来源:origin: redisson/redisson

@Override
protected void apply(Instrumentation instrumentation, Map<Class<?>, ClassDefinition> classDefinitions)
    throws UnmodifiableClassException, ClassNotFoundException {
  instrumentation.redefineClasses(classDefinitions.values().toArray(new ClassDefinition[classDefinitions.size()]));
}

代码示例来源:origin: glowroot/glowroot

pointcutClassNames, instrumentation.getAllLoadedClasses(), instrumentation);
  return 0;
instrumentation.retransformClasses(Iterables.toArray(classes, Class.class));
List<Class<?>> updatedReweavableClasses =
    analyzedWorld.getClassesWithReweavableAdvice(false);

代码示例来源:origin: org.mockito/mockito-core

try {
  assureCanReadMockito(types);
  instrumentation.retransformClasses(types.toArray(new Class<?>[types.size()]));
  Throwable throwable = lastException;
  if (throwable != null) {

代码示例来源:origin: glowroot/glowroot

public static void initialReweave(Set<PointcutClassName> pointcutClassNames,
    Class<?>[] initialLoadedClasses, Instrumentation instrumentation) {
  if (!instrumentation.isRetransformClassesSupported()) {
    return;
  }
  Set<Class<?>> classes = getExistingModifiableSubClasses(pointcutClassNames,
      initialLoadedClasses, instrumentation);
  for (Class<?> clazz : classes) {
    if (clazz.isInterface()) {
      continue;
    }
    try {
      instrumentation.retransformClasses(clazz);
    } catch (UnmodifiableClassException e) {
      // IBM JDK 6 throws UnmodifiableClassException even though call to
      // isModifiableClass() in getExistingModifiableSubClasses() returns true
      logger.debug(e.getMessage(), e);
    }
  }
}

代码示例来源:origin: redisson/redisson

/**
 * {@inheritDoc}
 */
public void addTransformer(Instrumentation instrumentation, ClassFileTransformer classFileTransformer, boolean canRetransform) {
  if (canRetransform) {
    throw new UnsupportedOperationException("Cannot apply retransformation on legacy VM");
  } else {
    instrumentation.addTransformer(classFileTransformer);
  }
}

代码示例来源:origin: scouter-project/scouter

private void redefine(StringSet clazzes) {
  Class[] loadedClasses = JavaAgent.getInstrumentation().getAllLoadedClasses();
  ArrayList<ClassDefinition> definitionList = new ArrayList<ClassDefinition>();
  boolean allSuccess = true;
  for (int i = 0; i < loadedClasses.length; i++) {
    if (clazzes.hasKey(loadedClasses[i].getName())) {
      try {
        byte[] buff = ClassUtil.getByteCode(loadedClasses[i]);
        if (buff == null) {
          continue;
        }
        definitionList.add(new ClassDefinition(loadedClasses[i], buff));
      } catch (Exception e) {
        allSuccess = false;
        break;
      }
    }
  }
  if (definitionList.size() > 0 && allSuccess) {
    try {
      JavaAgent.getInstrumentation().redefineClasses(definitionList.toArray(new ClassDefinition[definitionList.size()]));
    } catch (Throwable th) {
      Logger.println("A183", "redefine error : " + loadedClasses);
    }
  }
}

代码示例来源:origin: org.springframework/spring-context

/**
 * Remove all registered transformers, in inverse order of registration.
 */
public void removeTransformers() {
  synchronized (this.transformers) {
    if (this.instrumentation != null && !this.transformers.isEmpty()) {
      for (int i = this.transformers.size() - 1; i >= 0; i--) {
        this.instrumentation.removeTransformer(this.transformers.get(i));
      }
      this.transformers.clear();
    }
  }
}

代码示例来源:origin: dcevm/dcevm

public void redefineClasses(Map<Class<?>, byte[]> classes) throws ClassNotFoundException, UnmodifiableClassException {
 Instrumentation instrumentation = InstrumentationAgent.INSTRUMENTATION;
 if (instrumentation == 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());
 }
 instrumentation.redefineClasses(definitions);
}

相关文章