本文整理汇总了Java中net.bytebuddy.implementation.FixedValue类的典型用法代码示例。如果您正苦于以下问题:Java FixedValue类的具体用法?Java FixedValue怎么用?Java FixedValue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FixedValue类属于net.bytebuddy.implementation包,在下文中一共展示了FixedValue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRedefinitionReloadingStrategy
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
@Test
@AgentAttachmentRule.Enforce(redefinesClasses = true)
public void testRedefinitionReloadingStrategy() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
Foo foo = new Foo();
assertThat(foo.foo(), is(FOO));
ClassReloadingStrategy classReloadingStrategy = new ClassReloadingStrategy(ByteBuddyAgent.getInstrumentation(), ClassReloadingStrategy.Strategy.REDEFINITION);
new ByteBuddy()
.redefine(Foo.class)
.method(named(FOO))
.intercept(FixedValue.value(BAR))
.make()
.load(Foo.class.getClassLoader(), classReloadingStrategy);
try {
assertThat(foo.foo(), is(BAR));
} finally {
classReloadingStrategy.reset(Foo.class);
assertThat(foo.foo(), is(FOO));
}
}
示例2: testFromAgentClassReloadingStrategy
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
@Test
@AgentAttachmentRule.Enforce(redefinesClasses = true)
public void testFromAgentClassReloadingStrategy() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
Foo foo = new Foo();
assertThat(foo.foo(), is(FOO));
ClassReloadingStrategy classReloadingStrategy = ClassReloadingStrategy.fromInstalledAgent();
new ByteBuddy()
.redefine(Foo.class)
.method(named(FOO))
.intercept(FixedValue.value(BAR))
.make()
.load(Foo.class.getClassLoader(), classReloadingStrategy);
try {
assertThat(foo.foo(), is(BAR));
} finally {
classReloadingStrategy.reset(Foo.class);
assertThat(foo.foo(), is(FOO));
}
}
示例3: testFromAgentClassWithAuxiliaryReloadingStrategy
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
@Test
@AgentAttachmentRule.Enforce(redefinesClasses = true)
public void testFromAgentClassWithAuxiliaryReloadingStrategy() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
Foo foo = new Foo();
assertThat(foo.foo(), is(FOO));
ClassReloadingStrategy classReloadingStrategy = ClassReloadingStrategy.fromInstalledAgent();
String randomName = FOO + RandomString.make();
new ByteBuddy()
.redefine(Foo.class)
.method(named(FOO))
.intercept(FixedValue.value(BAR))
.make()
.include(new ByteBuddy().subclass(Object.class).name(randomName).make())
.load(Foo.class.getClassLoader(), classReloadingStrategy);
try {
assertThat(foo.foo(), is(BAR));
} finally {
classReloadingStrategy.reset(Foo.class);
assertThat(foo.foo(), is(FOO));
}
assertThat(Class.forName(randomName), notNullValue(Class.class));
}
示例4: testRetransformationReloadingStrategy
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
@Test
@AgentAttachmentRule.Enforce(retransformsClasses = true)
public void testRetransformationReloadingStrategy() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
Foo foo = new Foo();
assertThat(foo.foo(), is(FOO));
ClassReloadingStrategy classReloadingStrategy = new ClassReloadingStrategy(ByteBuddyAgent.getInstrumentation(), ClassReloadingStrategy.Strategy.RETRANSFORMATION);
new ByteBuddy()
.redefine(Foo.class)
.method(named(FOO))
.intercept(FixedValue.value(BAR))
.make()
.load(Foo.class.getClassLoader(), classReloadingStrategy);
try {
assertThat(foo.foo(), is(BAR));
} finally {
classReloadingStrategy.reset(Foo.class);
assertThat(foo.foo(), is(FOO));
}
}
示例5: testAnonymousType
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
@Test
@JavaVersionRule.Enforce(value = 8, atMost = 8)
@AgentAttachmentRule.Enforce(retransformsClasses = true)
public void testAnonymousType() throws Exception {
ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER,
ClassFileExtraction.of(Class.forName(LAMBDA_SAMPLE_FACTORY)),
ByteArrayClassLoader.PersistenceHandler.MANIFEST);
Instrumentation instrumentation = ByteBuddyAgent.install();
Class<?> factory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY);
@SuppressWarnings("unchecked")
Callable<String> instance = (Callable<String>) factory.getDeclaredMethod("nonCapturing").invoke(factory.getDeclaredConstructor().newInstance());
// Anonymous types can only be reset to their original format, if a retransformation is applied.
ClassReloadingStrategy classReloadingStrategy = new ClassReloadingStrategy(instrumentation,
ClassReloadingStrategy.Strategy.RETRANSFORMATION).preregistered(instance.getClass());
ClassFileLocator classFileLocator = ClassFileLocator.AgentBased.of(instrumentation, instance.getClass());
try {
assertThat(instance.call(), is(FOO));
new ByteBuddy()
.redefine(instance.getClass(), classFileLocator)
.method(named("call"))
.intercept(FixedValue.value(BAR))
.make()
.load(instance.getClass().getClassLoader(), classReloadingStrategy);
assertThat(instance.call(), is(BAR));
} finally {
classReloadingStrategy.reset(classFileLocator, instance.getClass());
assertThat(instance.call(), is(FOO));
}
}
示例6: testVisibilityBridgeForDefaultMethod
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
@Test
@JavaVersionRule.Enforce(8)
public void testVisibilityBridgeForDefaultMethod() throws Exception {
Class<?> defaultInterface = new ByteBuddy()
.makeInterface()
.merge(Visibility.PACKAGE_PRIVATE)
.defineMethod(FOO, String.class, Visibility.PUBLIC)
.intercept(FixedValue.value(BAR))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER)
.getLoaded();
Class<?> type = new ByteBuddy()
.subclass(defaultInterface)
.modifiers(Visibility.PUBLIC)
.make()
.load(defaultInterface.getClassLoader(), ClassLoadingStrategy.Default.INJECTION)
.getLoaded();
assertThat(type.getDeclaredConstructors().length, is(1));
assertThat(type.getDeclaredMethods().length, is(1));
Method foo = type.getDeclaredMethod(FOO);
assertThat(foo.isBridge(), is(true));
assertThat(foo.invoke(type.getDeclaredConstructor().newInstance()), is((Object) (BAR)));
}
示例7: hello
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
public void hello() throws IllegalAccessException, InstantiationException {
Class<?> dynamicType = new ByteBuddy()
.subclass(Bingoo.class)
// .name(namingStrategy)
.method(named("toString")).intercept(FixedValue.value("Hello World!"))
.make()
.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
Object instance = dynamicType.newInstance();
System.out.println(instance.toString()); // "Hello World!"
System.out.println(dynamicType.getName());
//assertThat(dynamicType.newInstance().toString(), is("Hello World!"));
}
示例8: delegateOrNoop
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
/** Delegates to the given method if available, or does nothing. */
private static Implementation delegateOrNoop(
TypeDescription doFnType, DoFnSignature.DoFnMethod method) {
return (method == null)
? FixedValue.originType()
: new DoFnMethodDelegation(doFnType, method.targetMethod());
}
示例9: addClass
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
private Collection<File> addClass(String name) throws IOException {
return new ByteBuddy()
.subclass(Object.class)
.name(name)
.defineMethod(FOO, String.class, Visibility.PUBLIC).intercept(FixedValue.value(FOO))
.defineMethod(BAR, String.class, Visibility.PUBLIC).intercept(FixedValue.value(BAR))
.make()
.saveIn(target)
.values();
}
示例10: addClass
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
private Collection<File> addClass(String name) throws IOException {
return new ByteBuddy()
.subclass(Object.class)
.name(name)
.defineMethod(FOO, String.class, Visibility.PUBLIC).intercept(FixedValue.value(FOO))
.defineMethod(BAR, String.class, Visibility.PUBLIC).intercept(FixedValue.value(BAR))
.make()
.saveIn(project)
.values();
}
示例11: transform
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
@Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder,
TypeDescription typeDescription,
ClassLoader classLoader,
JavaModule module) {
return builder
.method(isDeclaredBy(typeDescription).and(named(FOO))).intercept(FixedValue.value(FOO))
.method(isDeclaredBy(typeDescription).and(named(BAR))).intercept(FixedValue.value(BAR));
}
示例12: transform
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
@Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder,
TypeDescription typeDescription,
ClassLoader classLoader,
JavaModule module) {
return builder.method(named(FOO)).intercept(FixedValue.value(BAR));
}
示例13: testAdviceContainsDeadCode
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
@Test
public void testAdviceContainsDeadCode() throws Exception {
Class<?> advice = new ByteBuddy(classFileVersion)
.subclass(Object.class)
.defineMethod(FOO, void.class, Ownership.STATIC)
.intercept(new DeadVoidAppender())
.annotateMethod(AnnotationDescription.Builder.ofType(Advice.OnMethodEnter.class).define("suppress", RuntimeException.class).build())
.annotateMethod(AnnotationDescription.Builder.ofType(Advice.OnMethodExit.class).define("suppress", RuntimeException.class).build())
.make()
.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER_PERSISTENT)
.getLoaded();
Class<?> foo = new ByteBuddy(classFileVersion)
.subclass(Object.class)
.defineMethod("foo", String.class, Visibility.PUBLIC)
.intercept(FixedValue.value(FOO))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT)
.getLoaded();
Class<?> redefined = new ByteBuddy()
.redefine(foo)
.visit(Advice.to(advice).on(named(FOO)))
.make()
.load(null, ClassLoadingStrategy.Default.CHILD_FIRST)
.getLoaded();
assertThat(redefined, not(sameInstance((Object) foo)));
assertThat(redefined.getDeclaredMethod(FOO).invoke(redefined.getDeclaredConstructor().newInstance()), is((Object) FOO));
}
示例14: testInconsistentStackSizeAdvice
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
@Test
public void testInconsistentStackSizeAdvice() throws Exception {
Class<?> advice = new ByteBuddy()
.subclass(Object.class)
.defineMethod(FOO, type, Ownership.STATIC)
.intercept(new InconsistentSizeAppender())
.annotateMethod(AnnotationDescription.Builder.ofType(Advice.OnMethodEnter.class).define("suppress", RuntimeException.class).build())
.annotateMethod(AnnotationDescription.Builder.ofType(Advice.OnMethodExit.class).define("suppress", RuntimeException.class).build())
.make()
.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER_PERSISTENT)
.getLoaded();
Class<?> foo = new ByteBuddy()
.subclass(Object.class)
.defineMethod("foo", String.class, Visibility.PUBLIC)
.intercept(FixedValue.value(FOO))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT)
.getLoaded();
Class<?> redefined = new ByteBuddy()
.redefine(foo)
.visit(Advice.to(advice).on(named(FOO)))
.make()
.load(null, ClassLoadingStrategy.Default.CHILD_FIRST)
.getLoaded();
assertThat(redefined, not(sameInstance((Object) foo)));
assertThat(redefined.getDeclaredMethod(FOO).invoke(redefined.getDeclaredConstructor().newInstance()), is((Object) FOO));
}
示例15: testGenericMethod
import net.bytebuddy.implementation.FixedValue; //导入依赖的package包/类
@Test
public void testGenericMethod() throws Exception {
DynamicType.Unloaded<?> unloaded = new ByteBuddy()
.redefine(GenericMethod.class)
.method(named(FOO))
.intercept(FixedValue.nullValue())
.make();
Class<?> type = unloaded.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
MethodDescription createdMethod = new MethodDescription.ForLoadedMethod(type.getDeclaredMethod(FOO, Exception.class));
MethodDescription originalMethod = new MethodDescription.ForLoadedMethod(GenericMethod.class.getDeclaredMethod(FOO, Exception.class));
assertThat(createdMethod.getTypeVariables(), is(originalMethod.getTypeVariables()));
assertThat(createdMethod.getReturnType(), is(originalMethod.getReturnType()));
assertThat(createdMethod.getParameters().getOnly().getType(), is(originalMethod.getParameters().getOnly().getType()));
assertThat(createdMethod.getExceptionTypes().getOnly(), is(originalMethod.getExceptionTypes().getOnly()));
}