use of net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.
the class InvokeDynamicTest method testChainedInvocation.
@Test
@JavaVersionRule.Enforce(7)
public void testChainedInvocation() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
DynamicType.Loaded<SimpleWithArgument> dynamicType = new ByteBuddy().subclass(SimpleWithArgument.class).method(isDeclaredBy(SimpleWithArgument.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(QUX, String.class).withArgument(0).andThen(FixedValue.value(BAZ))).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(0));
assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(FOO), is(BAZ));
}
use of net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapOfMethodsWithParametersWrapperReference.
@Test
@JavaVersionRule.Enforce(7)
public void testBootstrapOfMethodsWithParametersWrapperReference() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
Object value = new Object();
DynamicType.Loaded<Simple> dynamicType = new ByteBuddy().subclass(Simple.class).method(isDeclaredBy(Simple.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(BAR, String.class).withReference(BOOLEAN, BYTE, SHORT, CHARACTER, INTEGER, LONG, FLOAT, DOUBLE, FOO, CLASS, makeEnum(), makeMethodType(CLASS)).withReference(makeMethodHandle()).as(// avoid direct method handle
JavaType.METHOD_HANDLE.load()).withReference(value)).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(14));
assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(), is("" + BOOLEAN + BYTE + SHORT + CHARACTER + INTEGER + LONG + FLOAT + DOUBLE + FOO + CLASS + makeEnum() + makeMethodType(CLASS) + makeMethodHandle() + value));
}
use of net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapWithArrayArgumentsWithArguments.
@Test
@JavaVersionRule.Enforce(value = 7, hotSpot = 7)
public void testBootstrapWithArrayArgumentsWithArguments() throws Exception {
Class<?> type = Class.forName(PARAMETER_BOOTSTRAP);
Field field = type.getDeclaredField(ARGUMENTS_FIELD_NAME);
field.set(null, null);
TypeDescription typeDescription = new TypeDescription.ForLoadedType(type);
DynamicType.Loaded<Simple> dynamicType = new ByteBuddy().subclass(Simple.class).method(isDeclaredBy(Simple.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP_ARRAY_ARGUMENTS)).getOnly(), INTEGER, LONG, FLOAT, DOUBLE, FOO, CLASS, makeMethodType(CLASS), makeMethodHandle()).withoutArguments()).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(), is(FOO));
Object[] arguments = (Object[]) field.get(null);
assertThat(arguments.length, is(8));
assertThat(arguments[0], is((Object) INTEGER));
assertThat(arguments[1], is((Object) LONG));
assertThat(arguments[2], is((Object) FLOAT));
assertThat(arguments[3], is((Object) DOUBLE));
assertThat(arguments[4], is((Object) FOO));
assertThat(arguments[5], is((Object) CLASS));
assertThat(arguments[6], is(makeMethodType(CLASS)));
assertThat(JavaConstant.MethodHandle.ofLoaded(arguments[7]), is(JavaConstant.MethodHandle.ofLoaded(makeMethodHandle())));
}
use of net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapConstructor.
@Test
@JavaVersionRule.Enforce(7)
public void testBootstrapConstructor() throws Exception {
for (Constructor<?> constructor : Class.forName(STANDARD_ARGUMENT_BOOTSTRAP).getDeclaredConstructors()) {
DynamicType.Loaded<Simple> dynamicType = new ByteBuddy().subclass(Simple.class).method(isDeclaredBy(Simple.class)).intercept(InvokeDynamic.bootstrap(constructor).withoutArguments()).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(), is(FOO));
}
}
use of net.bytebuddy.dynamic.DynamicType 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);
}
Aggregations