Search in sources :

Example 1 with JavaModule

use of net.bytebuddy.utility.JavaModule in project byte-buddy by raphw.

the class AgentBuilderListenerTest method testReadEdgeAddingListenerNamedDuplexCannotRead.

@Test
public void testReadEdgeAddingListenerNamedDuplexCannotRead() throws Exception {
    Instrumentation instrumentation = mock(Instrumentation.class);
    JavaModule source = mock(JavaModule.class), target = mock(JavaModule.class);
    when(source.isNamed()).thenReturn(true);
    when(source.canRead(target)).thenReturn(false);
    when(target.canRead(source)).thenReturn(false);
    AgentBuilder.Listener listener = new AgentBuilder.Listener.ModuleReadEdgeCompleting(instrumentation, true, Collections.singleton(target));
    listener.onTransformation(mock(TypeDescription.class), mock(ClassLoader.class), source, LOADED, mock(DynamicType.class));
    verify(source).isNamed();
    verify(source).canRead(target);
    verify(source).addReads(instrumentation, target);
    verifyNoMoreInteractions(source);
    verify(target).canRead(source);
    verify(target).addReads(instrumentation, source);
    verifyNoMoreInteractions(target);
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) Instrumentation(java.lang.instrument.Instrumentation) TypeDescription(net.bytebuddy.description.type.TypeDescription) JavaModule(net.bytebuddy.utility.JavaModule) Test(org.junit.Test)

Example 2 with JavaModule

use of net.bytebuddy.utility.JavaModule in project byte-buddy by raphw.

the class AgentBuilderListenerTest method testReadEdgeAddingListenerNamedCannotRead.

@Test
public void testReadEdgeAddingListenerNamedCannotRead() throws Exception {
    Instrumentation instrumentation = mock(Instrumentation.class);
    JavaModule source = mock(JavaModule.class), target = mock(JavaModule.class);
    when(source.isNamed()).thenReturn(true);
    when(source.canRead(target)).thenReturn(false);
    AgentBuilder.Listener listener = new AgentBuilder.Listener.ModuleReadEdgeCompleting(instrumentation, false, Collections.singleton(target));
    listener.onTransformation(mock(TypeDescription.class), mock(ClassLoader.class), source, LOADED, mock(DynamicType.class));
    verify(source).isNamed();
    verify(source).canRead(target);
    verify(source).addReads(instrumentation, target);
    verifyNoMoreInteractions(source);
    verifyZeroInteractions(target);
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) Instrumentation(java.lang.instrument.Instrumentation) TypeDescription(net.bytebuddy.description.type.TypeDescription) JavaModule(net.bytebuddy.utility.JavaModule) Test(org.junit.Test)

Example 3 with JavaModule

use of net.bytebuddy.utility.JavaModule in project byte-buddy by raphw.

the class AgentBuilderListenerTest method testReadEdgeAddingListenerCanRead.

@Test
public void testReadEdgeAddingListenerCanRead() throws Exception {
    Instrumentation instrumentation = mock(Instrumentation.class);
    JavaModule source = mock(JavaModule.class), target = mock(JavaModule.class);
    when(source.isNamed()).thenReturn(true);
    when(source.canRead(target)).thenReturn(true);
    AgentBuilder.Listener listener = new AgentBuilder.Listener.ModuleReadEdgeCompleting(instrumentation, false, Collections.singleton(target));
    listener.onTransformation(mock(TypeDescription.class), mock(ClassLoader.class), source, LOADED, mock(DynamicType.class));
    verify(source).isNamed();
    verify(source).canRead(target);
    verifyNoMoreInteractions(source);
    verifyZeroInteractions(target);
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) Instrumentation(java.lang.instrument.Instrumentation) TypeDescription(net.bytebuddy.description.type.TypeDescription) JavaModule(net.bytebuddy.utility.JavaModule) Test(org.junit.Test)

Example 4 with JavaModule

use of net.bytebuddy.utility.JavaModule in project byte-buddy by raphw.

the class AgentBuilderListenerTest method testReadEdgeAddingListenerUnnamed.

@Test
public void testReadEdgeAddingListenerUnnamed() throws Exception {
    Instrumentation instrumentation = mock(Instrumentation.class);
    JavaModule source = mock(JavaModule.class), target = mock(JavaModule.class);
    AgentBuilder.Listener listener = new AgentBuilder.Listener.ModuleReadEdgeCompleting(instrumentation, false, Collections.singleton(target));
    listener.onTransformation(mock(TypeDescription.class), mock(ClassLoader.class), source, LOADED, mock(DynamicType.class));
    verify(source).isNamed();
    verifyNoMoreInteractions(source);
    verifyZeroInteractions(target);
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) Instrumentation(java.lang.instrument.Instrumentation) TypeDescription(net.bytebuddy.description.type.TypeDescription) JavaModule(net.bytebuddy.utility.JavaModule) Test(org.junit.Test)

Example 5 with JavaModule

use of net.bytebuddy.utility.JavaModule in project incubator-skywalking by apache.

the class SkyWalkingAgent method premain.

/**
 * Main entrance.
 * Use byte-buddy transform to enhance all classes, which define in plugins.
 *
 * @param agentArgs
 * @param instrumentation
 * @throws PluginException
 */
public static void premain(String agentArgs, Instrumentation instrumentation) throws PluginException {
    final PluginFinder pluginFinder;
    try {
        SnifferConfigInitializer.initialize();
        pluginFinder = new PluginFinder(new PluginBootstrap().loadPlugins());
        ServiceManager.INSTANCE.boot();
    } catch (Exception e) {
        logger.error(e, "Skywalking agent initialized failure. Shutting down.");
        return;
    }
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        @Override
        public void run() {
            ServiceManager.INSTANCE.shutdown();
        }
    }, "skywalking service shutdown thread"));
    new AgentBuilder.Default().type(pluginFinder.buildMatch()).transform(new AgentBuilder.Transformer() {

        @Override
        public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule module) {
            List<AbstractClassEnhancePluginDefine> pluginDefines = pluginFinder.find(typeDescription, classLoader);
            if (pluginDefines.size() > 0) {
                DynamicType.Builder<?> newBuilder = builder;
                EnhanceContext context = new EnhanceContext();
                for (AbstractClassEnhancePluginDefine define : pluginDefines) {
                    DynamicType.Builder<?> possibleNewBuilder = define.define(typeDescription.getTypeName(), newBuilder, classLoader, context);
                    if (possibleNewBuilder != null) {
                        newBuilder = possibleNewBuilder;
                    }
                }
                if (context.isEnhanced()) {
                    logger.debug("Finish the prepare stage for {}.", typeDescription.getName());
                }
                return newBuilder;
            }
            logger.debug("Matched class {}, but ignore by finding mechanism.", typeDescription.getTypeName());
            return builder;
        }
    }).with(new AgentBuilder.Listener() {

        @Override
        public void onDiscovery(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded) {
        }

        @Override
        public void onTransformation(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module, boolean loaded, DynamicType dynamicType) {
            if (logger.isDebugEnable()) {
                logger.debug("On Transformation class {}.", typeDescription.getName());
            }
            InstrumentDebuggingClass.INSTANCE.log(typeDescription, dynamicType);
        }

        @Override
        public void onIgnored(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module, boolean loaded) {
        }

        @Override
        public void onError(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded, Throwable throwable) {
            logger.error("Enhance class " + typeName + " error.", throwable);
        }

        @Override
        public void onComplete(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded) {
        }
    }).installOn(instrumentation);
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) AgentBuilder(net.bytebuddy.agent.builder.AgentBuilder) PluginBootstrap(org.apache.skywalking.apm.agent.core.plugin.PluginBootstrap) PluginException(org.apache.skywalking.apm.agent.core.plugin.PluginException) JavaModule(net.bytebuddy.utility.JavaModule) EnhanceContext(org.apache.skywalking.apm.agent.core.plugin.EnhanceContext) AgentBuilder(net.bytebuddy.agent.builder.AgentBuilder) PluginFinder(org.apache.skywalking.apm.agent.core.plugin.PluginFinder) AbstractClassEnhancePluginDefine(org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine) TypeDescription(net.bytebuddy.description.type.TypeDescription) List(java.util.List)

Aggregations

JavaModule (net.bytebuddy.utility.JavaModule)8 TypeDescription (net.bytebuddy.description.type.TypeDescription)7 DynamicType (net.bytebuddy.dynamic.DynamicType)7 Test (org.junit.Test)7 Instrumentation (java.lang.instrument.Instrumentation)6 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 List (java.util.List)1 AgentBuilder (net.bytebuddy.agent.builder.AgentBuilder)1 AbstractClassEnhancePluginDefine (org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine)1 EnhanceContext (org.apache.skywalking.apm.agent.core.plugin.EnhanceContext)1 PluginBootstrap (org.apache.skywalking.apm.agent.core.plugin.PluginBootstrap)1 PluginException (org.apache.skywalking.apm.agent.core.plugin.PluginException)1 PluginFinder (org.apache.skywalking.apm.agent.core.plugin.PluginFinder)1