All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ng-cli.0.121.0.source-code.init-agent.qute Maven / Gradle / Ivy

The newest version!
///usr/bin/env jbang "$0" "$@" ; exit $?
{#for dep in dependencies.orEmpty}
//DEPS {dep}
{/for}
{#if dependencies.isEmpty()}// //DEPS  {/if}
//JAVAAGENT Can-Redefine-Classes Can-Retransform-Classes Can-Set-Native-Method-Prefix

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.ProtectionDomain;


public class {baseName} {

    public static void premain(String agentArgs, Instrumentation instrumentation) {
        System.out.println("jbang agent {baseName} loaded. Will dump all loaded classes into `classes/`");

        ClassLogger transformer = new ClassLogger();
        instrumentation.addTransformer(transformer);
    }

    public static void main(String[] args) {
        System.out.println("This is a (jbang) javaagent.\n" +
                           "Usage: \n" +
                           "   jbang --javaagent={baseName}.java yourApp.java");
    }

    static public class ClassLogger implements ClassFileTransformer {
      @Override
      public byte[] transform(ClassLoader loader,
                              String className,
                              Class classBeingRedefined,
                              ProtectionDomain protectionDomain,
                              byte[] classfileBuffer) throws IllegalClassFormatException {
        try {
          Path path = Paths.get("classes/" + className + ".class");
          Files.createDirectories(path.getParent());
          Files.write(path, classfileBuffer);
        } catch (Throwable ignored) {
            System.err.println(ignored);
        } finally { return classfileBuffer; }
      }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy