Java Code Examples for java.lang.instrument.Instrumentation#isModifiableClass()

The following examples show how to use java.lang.instrument.Instrumentation#isModifiableClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SnoopInstructionTransformer.java    From JQF with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void premain(String agentArgs, Instrumentation inst) throws ClassNotFoundException {

    preloadClasses();

    inst.addTransformer(new SnoopInstructionTransformer(), true);
    if (inst.isRetransformClassesSupported()) {
      for (Class clazz : inst.getAllLoadedClasses()) {
        try {
          String cname = clazz.getName().replace(".","/");
          if (shouldExclude(cname) == false) {
            if (inst.isModifiableClass(clazz)) {
              inst.retransformClasses(clazz);
            } else {
              println("[WARNING] Could not instrument " + clazz);
            }
          }
        } catch (Exception e){
          if (verbose) {
            println("[WARNING] Could not instrument " + clazz);
            e.printStackTrace();
          }
        }
      }
    }
  }
 
Example 2
Source File: TestLambdaFormRetransformation.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void premain(String args, Instrumentation instrumentation) {
    if (!instrumentation.isRetransformClassesSupported()) {
        System.out.println("Class retransformation is not supported.");
        return;
    }
    System.out.println("Calling lambda to ensure that lambda forms were created");

    Agent.lambda.run();

    System.out.println("Registering class file transformer");

    instrumentation.addTransformer(new Agent());

    for (Class c : instrumentation.getAllLoadedClasses()) {
        if (c.getName().contains("LambdaForm") &&
            instrumentation.isModifiableClass(c)) {
            System.out.format("We've found a modifiable lambda form: %s%n", c.getName());
            try {
                instrumentation.retransformClasses(c);
            } catch (UnmodifiableClassException e) {
                throw new AssertionError("Modification of modifiable class " +
                                         "caused UnmodifiableClassException", e);
            }
        }
    }
}
 
Example 3
Source File: AsyncAwaitInstrumentationAgent.java    From tascalate-async-await with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * JVM hook to dynamically load javaagent at runtime.
 * 
 * The agent class may have an agentmain method for use when the agent is
 * started after VM startup.
 * 
 * @param args arguments supplied to the agent
 * @param instrumentation {@link Instrumentation} object passed by JVM
 * @throws Exception thrown when agent is unable to start
 */
public static void agentmain(String args, Instrumentation instrumentation) throws Exception {
    setupInstrumentation(instrumentation);
    for (Class<?> clazz : instrumentation.getAllLoadedClasses()) {
        if (instrumentation.isModifiableClass(clazz) && 
            !AsyncAwaitClassFileTransformer.skipClassByName(clazz.getName())) {
            try {
                instrumentation.retransformClasses(clazz);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
    System.setProperty(AsyncAwaitInstrumentationAgent.class.getName(), "true");
    System.setProperty("org.apache.commons.javaflow.instrumentation.JavaFlowInstrumentationAgent", "true");
}
 
Example 4
Source File: LiveWeavingServiceImpl.java    From glowroot with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: AbstractInstrumentationAgent.java    From tascalate-javaflow with Apache License 2.0 4 votes vote down vote up
protected void retransformClasses(Instrumentation instrumentation, Set<String> ownPackages) {
    log.info("Re-transforming existing classes...");
    
    ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
    for (Class<?> clazz : instrumentation.getAllLoadedClasses()) {
        String className = clazz.getName();
        if (instrumentation.isModifiableClass(clazz)) {
            if (isClassLoaderParent(systemClassLoader, clazz.getClassLoader())) {
                if (log.isTraceEnabled()) {
                    log.trace("Skip re-transforming boot or extension/platform class: " + className);
                }
                continue;
            }
            
            boolean isOwnClass = false;
            for (String ownPackage : ownPackages) {
                if (className.startsWith(ownPackage)) {
                    isOwnClass = true;
                    break;
                }
            }
            
            if (isOwnClass) {
                if (log.isDebugEnabled()) {
                    log.debug("Skip re-transforming class (agent class): " + className);
                }
                continue;
            }
            
            if (log.isDebugEnabled()) {
                log.debug("Re-transforming class: " + className);
            }
            try {
                instrumentation.retransformClasses(clazz);
            } catch (Throwable e) {
                log.error("Error re-transofrming class "+ className, e);
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Non-modifiable class (re-transforming skipped): " + className);
            }                    
        }
    }
    log.info("Existing classes was re-transormed");
}