java.lang.instrument.Instrumentation.isModifiableClass()方法的使用及代码示例

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

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

Instrumentation.isModifiableClass介绍

暂无

代码示例

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

@Override
public boolean mockable() {
  return INSTRUMENTATION.isModifiableClass(type) && !EXCLUDES.contains(type);
}

代码示例来源: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: stackoverflow.com

System.out.println(BACK_LINK.isModifiableClass(SelfAttacher.class));
vm.detach();

代码示例来源:origin: alibaba/jvm-sandbox

&& !inst.isModifiableClass(clazz)) {
logger.debug("remove from findForReTransform, because class:{} is unModifiable", clazz.getName());
continue;

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

if (c != null) {
  cc.get(c);
  if (inst.isModifiableClass(c) &&  isCandidate(c)) {
    debugPrint("candidate " + c + " added");
    list.add(c);

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

private static Set<Class<?>> getExistingModifiableSubClasses(
    Set<PointcutClassName> pointcutClassNames, Class<?>[] classes,
    Instrumentation instrumentation) {
  List<Class<?>> matchingClasses = Lists.newArrayList();
  Multimap<Class<?>, Class<?>> subClasses = ArrayListMultimap.create();
  for (Class<?> clazz : classes) {
    if (!instrumentation.isModifiableClass(clazz)) {
      continue;
    }
    Class<?> superclass = clazz.getSuperclass();
    if (superclass != null) {
      subClasses.put(superclass, clazz);
    }
    for (Class<?> iface : clazz.getInterfaces()) {
      subClasses.put(iface, clazz);
    }
    for (PointcutClassName pointcutClassName : pointcutClassNames) {
      if (pointcutClassName.appliesTo(clazz.getName())) {
        matchingClasses.add(clazz);
        break;
      }
    }
  }
  Set<Class<?>> matchingSubClasses = Sets.newHashSet();
  for (Class<?> matchingClass : matchingClasses) {
    addToMatchingSubClasses(matchingClass, matchingSubClasses, subClasses);
  }
  return matchingSubClasses;
}

代码示例来源: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: yushijinhun/authlib-injector

private static boolean canRetransformClass(Class<?> clazz) {
  if (!instrumentation.isModifiableClass(clazz)) {
    return false;
  }
  String name = clazz.getName();
  for (String prefix : nonTransformablePackages) {
    if (name.startsWith(prefix)) {
      return false;
    }
  }
  return true;
}

代码示例来源:origin: jbachorik/btrace2

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 (Exception e) {
    e.printStackTrace();
  }
}

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

public static void premain(String agentArgs, Instrumentation inst) {
 System.out.println(System.getProperty("java.version"));
 System.out.println(inst.isRedefineClassesSupported());
 int num=0;
 for(Class<?> clazz:inst.getAllLoadedClasses())
  if(!clazz.isArray() && !inst.isModifiableClass(clazz)) {
   System.out.println("not modifiable "+clazz);
   num++;
  }
 System.out.println((num==0? "all classes are": num+" classes are not")+" transformable");
}

代码示例来源:origin: jboss-fuse/fabric8

private boolean isInstrumentClass(Class c) {
  if (!instrumentation.isModifiableClass(c)) {
    LOG.trace("NO INSTRUMENT: Class " + c.getName() + " is not modifiable");
    return false;
  }
  if (!configuration.isAudit(c.getName())) {
    LOG.trace("NO INSTRUMENT: Class " + c.getName() + " is blacklisted");
    return false;
  }
  if (c.isArray() || c.isAnnotation() || c.isInterface() || c.isPrimitive() || c.isSynthetic() || c.isEnum()) {
    LOG.trace("NO INSTRUMENT: Class " + c.getName() + " is an array, primitive, annotation or enum etc");
    return false;
  }
  return true;
}

代码示例来源:origin: org.gridkit.3rd.btrace/runtime

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 (Exception e) {
    BTraceLogger.debugPrint(e);
  }
}

代码示例来源:origin: org.testifyproject.external/external-bytebuddy

@Override
  public void reset(Instrumentation instrumentation, ClassFileLocator classFileLocator, List<Class<?>> types) throws IOException, UnmodifiableClassException, ClassNotFoundException {
    for (Class<?> type : types) {
      if (!instrumentation.isModifiableClass(type)) {
        throw new IllegalArgumentException("Cannot modify type: " + type);
      }
    }
    instrumentation.addTransformer(ClassResettingTransformer.INSTANCE, true);
    try {
      instrumentation.retransformClasses(types.toArray(new Class<?>[types.size()]));
    } finally {
      instrumentation.removeTransformer(ClassResettingTransformer.INSTANCE);
    }
  }
};

代码示例来源:origin: foursquare/heapaudit

private static void instrument(HeapAudit agent,
                String args,
                Instrumentation instrumentation) throws FileNotFoundException, UnmodifiableClassException {
  if (!instrumentation.isRetransformClassesSupported()) {
    throw new UnmodifiableClassException();
  }
  instrumentation.addTransformer(agent,
                  true);
  ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
  for (Class<?> c: instrumentation.getAllLoadedClasses()) {
    if (instrumentation.isModifiableClass(c)) {
      classes.add(c);
    }
  }
  instrumentation.retransformClasses(classes.toArray(new Class<?>[classes.size()]));
}

代码示例来源:origin: com.github.fommil/java-allocation-instrumenter

private static void bootstrap(Instrumentation inst) {
  inst.addTransformer(new AllocationInstrumenter(),
      inst.isRetransformClassesSupported());
  if (!canRewriteBootstrap) {
    return;
  }
  // Get the set of already loaded classes that can be rewritten.
  Class<?>[] classes = inst.getAllLoadedClasses();
  ArrayList<Class<?>> classList = new ArrayList<Class<?>>();
  for (int i = 0; i < classes.length; i++) {
    if (inst.isModifiableClass(classes[i])) {
      classList.add(classes[i]);
    }
  }
  // Reload classes, if possible.
  Class<?>[] workaround = new Class<?>[classList.size()];
  try {
    inst.retransformClasses(classList.toArray(workaround));
  } catch (UnmodifiableClassException e) {
    System.err.println("AllocationInstrumenter was unable to "
        + "retransform early loaded classes.");
  }
}

代码示例来源:origin: EvoSuite/evosuite

@Test
public void testInstrumentation() throws Exception{

  try{
    InstrumentingAgent.activate();
    
    Instrumentation inst = InstrumentingAgent.getInstrumentation();
    Assert.assertNotNull(inst);
    ClassLoader loader = this.getClass().getClassLoader();
    Assert.assertTrue(inst.isModifiableClass(loader.loadClass(TimeA.class.getName())));
    Assert.assertTrue(inst.isModifiableClass(loader.loadClass(TimeB.class.getName())));
    Assert.assertTrue(inst.isModifiableClass(loader.loadClass(TimeC.class.getName())));
    Assert.assertTrue(inst.isModifiableClass(loader.loadClass(ExtendingTimeC.class.getName())));
    Assert.assertTrue(inst.isModifiableClass(loader.loadClass(ConcreteTime.class.getName())));
    Assert.assertTrue(inst.isModifiableClass(loader.loadClass(AbstractTime.class.getName())));
    
  } finally{
    InstrumentingAgent.deactivate();
  }
}

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

@Test
public void unmodifiableClass() throws UnmodifiableClassException {
  InstrumentationDefinition iDefinition = mock(InstrumentationDefinition.class);
  when(iDefinition.getClassName()).thenReturn("java.lang.Object");
  IAgentMessage<?> message = new UpdatedInstrumentationMessage();
  ((UpdatedInstrumentationMessage) message).getMessageContent().add(iDefinition);
  AgentMessagesReceivedEvent event = new AgentMessagesReceivedEvent(eventSource, Arrays.<IAgentMessage<?>> asList(message));
  when(instrumentation.getAllLoadedClasses()).thenReturn(new Class[] { Object.class, String.class });
  when(instrumentation.isModifiableClass(eq(Object.class))).thenReturn(false);
  when(agent.isUsingRetransformation()).thenReturn(true);
  retransformManager.onApplicationEvent(event);
  verify(classHashHelper).registerInstrumentationDefinition(eq("java.lang.Object"), eq(iDefinition));
  verify(instrumentation).getAllLoadedClasses();
  verify(instrumentation, times(2)).isModifiableClass(any(Class.class));
  verifyNoMoreInteractions(instrumentation, classHashHelper);
  verifyZeroInteractions(threadTransformHelper);
}

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

@Test
public void unknownInstrumentationClass() throws UnmodifiableClassException {
  InstrumentationDefinition iDefinition = mock(InstrumentationDefinition.class);
  when(iDefinition.getClassName()).thenReturn("unknown.Class");
  IAgentMessage<?> message = new UpdatedInstrumentationMessage();
  ((UpdatedInstrumentationMessage) message).getMessageContent().add(iDefinition);
  AgentMessagesReceivedEvent event = new AgentMessagesReceivedEvent(eventSource, Arrays.<IAgentMessage<?>> asList(message));
  when(instrumentation.getAllLoadedClasses()).thenReturn(new Class[] { Object.class, String.class });
  when(agent.isUsingRetransformation()).thenReturn(true);
  retransformManager.onApplicationEvent(event);
  verify(classHashHelper).registerInstrumentationDefinition(eq("unknown.Class"), eq(iDefinition));
  verify(instrumentation).getAllLoadedClasses();
  verify(instrumentation, times(2)).isModifiableClass(any(Class.class));
  verifyNoMoreInteractions(instrumentation, classHashHelper);
  verifyZeroInteractions(threadTransformHelper);
}

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

@Test
public void retransformationThrowsException() throws UnmodifiableClassException {
  InstrumentationDefinition iDefinition = mock(InstrumentationDefinition.class);
  when(iDefinition.getClassName()).thenReturn("java.lang.Object");
  IAgentMessage<?> message = new UpdatedInstrumentationMessage();
  ((UpdatedInstrumentationMessage) message).getMessageContent().add(iDefinition);
  AgentMessagesReceivedEvent event = new AgentMessagesReceivedEvent(eventSource, Arrays.<IAgentMessage<?>> asList(message));
  when(instrumentation.getAllLoadedClasses()).thenReturn(new Class[] { Object.class, String.class });
  doThrow(Exception.class).when(instrumentation).retransformClasses(any(Class.class));
  when(instrumentation.isModifiableClass(Matchers.<Class<?>> any())).thenReturn(true);
  when(agent.isUsingRetransformation()).thenReturn(true);
  retransformManager.onApplicationEvent(event);
  verify(classHashHelper).registerInstrumentationDefinition(eq("java.lang.Object"), eq(iDefinition));
  verify(instrumentation).getAllLoadedClasses();
  verify(instrumentation).retransformClasses(any(Class.class));
  verify(instrumentation, times(2)).isModifiableClass(any(Class.class));
  InOrder inOrder = inOrder(threadTransformHelper);
  inOrder.verify(threadTransformHelper).setThreadTransformDisabled(false);
  inOrder.verify(threadTransformHelper).setThreadTransformDisabled(true);
  verifyNoMoreInteractions(instrumentation, classHashHelper);
  verifyZeroInteractions(threadTransformHelper);
}

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

@Test
@SuppressWarnings("unchecked")
public void instrumentationThrowsOneException() throws UnmodifiableClassException {
  InstrumentationDefinition iDefinition = mock(InstrumentationDefinition.class);
  when(iDefinition.getClassName()).thenReturn("java.lang.Object");
  IAgentMessage<?> message = new UpdatedInstrumentationMessage();
  ((UpdatedInstrumentationMessage) message).getMessageContent().add(iDefinition);
  AgentMessagesReceivedEvent event = new AgentMessagesReceivedEvent(eventSource, Arrays.<IAgentMessage<?>> asList(message));
  when(instrumentation.getAllLoadedClasses()).thenReturn(new Class[] { Object.class, String.class });
  when(instrumentation.isModifiableClass(any(Class.class))).thenReturn(true).thenThrow(RuntimeException.class);
  when(agent.isUsingRetransformation()).thenReturn(true);
  retransformManager.onApplicationEvent(event);
  verify(classHashHelper).registerInstrumentationDefinition(eq("java.lang.Object"), eq(iDefinition));
  verify(instrumentation).getAllLoadedClasses();
  verify(instrumentation).retransformClasses(eq(Object.class));
  verify(instrumentation, times(2)).isModifiableClass(any(Class.class));
  InOrder inOrder = inOrder(threadTransformHelper);
  inOrder.verify(threadTransformHelper).setThreadTransformDisabled(false);
  inOrder.verify(threadTransformHelper).setThreadTransformDisabled(true);
  verifyNoMoreInteractions(instrumentation, classHashHelper, threadTransformHelper);
}

相关文章