net.bytebuddy.implementation.FixedValue Java Examples
The following examples show how to use
net.bytebuddy.implementation.FixedValue.
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: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(11) public void testDynamicConstantFactoryNested() throws Exception { Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.DynamicConstantBootstrap"); Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.bootstrap(FOO, bootstrap.getMethod("bootstrap", Class.forName("java.lang.invoke.MethodHandles$Lookup"), String.class, Class.class, bootstrap), JavaConstant.Dynamic.bootstrap(BAR, bootstrap.getMethod("bootstrap", Class.forName("java.lang.invoke.MethodHandles$Lookup"), Object[].class))))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(bootstrap)); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), sameInstance(baz.getDeclaredMethod(FOO).invoke(foo))); }
Example #2
Source File: ClassReloadingStrategyTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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 = ClassReloadingStrategy.fromInstalledAgent(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)); } }
Example #3
Source File: ClassReloadingStrategyTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@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 = ClassReloadingStrategy.fromInstalledAgent(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)); } }
Example #4
Source File: ClassReloadingStrategyTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @AgentAttachmentRule.Enforce(retransformsClasses = true) @JavaVersionRule.Enforce(atMost = 10) // Wait for mechanism in sun.misc.Unsafe to define class. 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)); }
Example #5
Source File: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(11) public void testInvoke() throws Exception { Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.ofInvocation(SampleClass.class.getMethod("make")))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(SampleClass.class)); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), sameInstance(baz.getDeclaredMethod(FOO).invoke(foo))); }
Example #6
Source File: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(11) public void testDynamicConstantConstructorNested() throws Exception { Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.DynamicConstantBootstrap"); Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.bootstrap(FOO, bootstrap.getConstructor( Class.forName("java.lang.invoke.MethodHandles$Lookup"), String.class, Class.class, bootstrap), JavaConstant.Dynamic.bootstrap(BAR, bootstrap.getConstructor( Class.forName("java.lang.invoke.MethodHandles$Lookup"), Object[].class))))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(bootstrap)); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), sameInstance(baz.getDeclaredMethod(FOO).invoke(foo))); }
Example #7
Source File: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(11) public void testDynamicConstantConstructorVarargs() throws Exception { Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.DynamicConstantBootstrap"); Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.bootstrap(FOO, bootstrap.getConstructor( Class.forName("java.lang.invoke.MethodHandles$Lookup"), String.class, Class.class, Object[].class)))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(bootstrap)); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), sameInstance(baz.getDeclaredMethod(FOO).invoke(foo))); }
Example #8
Source File: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(11) public void testDynamicConstantConstructorNoVarargs() throws Exception { Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.DynamicConstantBootstrap"); Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.bootstrap(FOO, bootstrap.getConstructor( Class.forName("java.lang.invoke.MethodHandles$Lookup"), String.class, Class.class)))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(bootstrap)); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), sameInstance(baz.getDeclaredMethod(FOO).invoke(foo))); }
Example #9
Source File: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(11) public void testDynamicConstantConstructorLookupAndStringOnly() throws Exception { Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.DynamicConstantBootstrap"); Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.bootstrap(FOO, bootstrap.getConstructor( Class.forName("java.lang.invoke.MethodHandles$Lookup"), String.class, Object[].class)))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(bootstrap)); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), sameInstance(baz.getDeclaredMethod(FOO).invoke(foo))); }
Example #10
Source File: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(11) public void testDynamicConstantConstructorLookupOnly() throws Exception { Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.DynamicConstantBootstrap"); Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.bootstrap(FOO, bootstrap.getConstructor( Class.forName("java.lang.invoke.MethodHandles$Lookup"), Object[].class)))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(bootstrap)); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), sameInstance(baz.getDeclaredMethod(FOO).invoke(foo))); }
Example #11
Source File: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(11) public void testConstruct() throws Exception { Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.ofInvocation(SampleClass.class.getConstructor()))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(SampleClass.class)); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), sameInstance(baz.getDeclaredMethod(FOO).invoke(foo))); }
Example #12
Source File: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(11) public void testDynamicConstantFactoryVarargs() throws Exception { Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.DynamicConstantBootstrap"); Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.bootstrap(FOO, bootstrap.getMethod("bootstrap", Class.forName("java.lang.invoke.MethodHandles$Lookup"), String.class, Class.class, Object[].class)))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(bootstrap)); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), sameInstance(baz.getDeclaredMethod(FOO).invoke(foo))); }
Example #13
Source File: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(11) public void testDynamicConstantFactoryNoVarargs() throws Exception { Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.DynamicConstantBootstrap"); Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.bootstrap(FOO, bootstrap.getMethod("bootstrap", Class.forName("java.lang.invoke.MethodHandles$Lookup"), String.class, Class.class)))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(bootstrap)); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), sameInstance(baz.getDeclaredMethod(FOO).invoke(foo))); }
Example #14
Source File: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(11) public void testDynamicConstantFactoryLookupAndStringOnly() throws Exception { Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.DynamicConstantBootstrap"); Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.bootstrap(FOO, bootstrap.getMethod("bootstrap", Class.forName("java.lang.invoke.MethodHandles$Lookup"), String.class, Object[].class)))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(bootstrap)); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), sameInstance(baz.getDeclaredMethod(FOO).invoke(foo))); }
Example #15
Source File: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(11) public void testDynamicConstantFactoryLookupOnly() throws Exception { Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.DynamicConstantBootstrap"); Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.bootstrap(FOO, bootstrap.getMethod("bootstrap", Class.forName("java.lang.invoke.MethodHandles$Lookup"), Object[].class)))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(bootstrap)); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), sameInstance(baz.getDeclaredMethod(FOO).invoke(foo))); }
Example #16
Source File: ClassReloadingStrategyTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @AgentAttachmentRule.Enforce(retransformsClasses = 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)); } }
Example #17
Source File: TypeReferenceAdjustmentTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testConstantType() { new ByteBuddy() .subclass(Object.class) .modifiers(TypeManifestation.ABSTRACT) .defineMethod(FOO, void.class) .intercept(FixedValue.value(Foo.class)) .visit(new AssertionVisitorWrapper(false, Foo.class)) .visit(TypeReferenceAdjustment.strict()) .make(); }
Example #18
Source File: GenericSignatureResolutionTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testGenericMethodWithoutGenericExceptionTypes() throws Exception { DynamicType.Unloaded<?> unloaded = new ByteBuddy() .redefine(GenericMethod.class) .method(named(BAR)) .intercept(FixedValue.nullValue()) .make(); Class<?> type = unloaded.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded(); MethodDescription createdMethod = new MethodDescription.ForLoadedMethod(type.getDeclaredMethod(BAR, Object.class)); MethodDescription originalMethod = new MethodDescription.ForLoadedMethod(GenericMethod.class.getDeclaredMethod(BAR, Object.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())); }
Example #19
Source File: GenericSignatureResolutionTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@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())); }
Example #20
Source File: AdviceInconsistentStackSizeTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@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)); }
Example #21
Source File: AdviceDeadCodeTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@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)); }
Example #22
Source File: JavaConstantDynamicTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test @JavaVersionRule.Enforce(11) public void testStaticFieldVarHandle() throws Exception { Class<? extends Foo> baz = new ByteBuddy() .subclass(Foo.class) .method(isDeclaredBy(Foo.class)) .intercept(FixedValue.value(JavaConstant.Dynamic.ofVarHandle(SampleClass.class.getDeclaredField("FOO")))) .make() .load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(baz.getDeclaredFields().length, is(0)); assertThat(baz.getDeclaredMethods().length, is(1)); Foo foo = baz.getDeclaredConstructor().newInstance(); assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(Class.forName("java.lang.invoke.VarHandle"))); }
Example #23
Source File: ClassReloadingStrategyTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test @JavaVersionRule.Enforce(value = 8, atMost = 8, j9 = false) @AgentAttachmentRule.Enforce(retransformsClasses = true) public void testAnonymousType() throws Exception { ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileLocator.ForClassLoader.readToNames(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)); } }
Example #24
Source File: TypeWriterDefaultTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testTypeInLegacyConstantPoolRemapped() throws Exception { Class<?> dynamicType = new ByteBuddy(ClassFileVersion.JAVA_V4) .with(TypeValidation.DISABLED) .subclass(Object.class) .defineMethod(FOO, Object.class, Visibility.PUBLIC) .intercept(FixedValue.value(Object.class)) .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(dynamicType.getDeclaredMethod(FOO).invoke(dynamicType.getDeclaredConstructor().newInstance()), is((Object) Object.class)); }
Example #25
Source File: TypeWriterDefaultTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testArrayTypeInLegacyConstantPoolRemapped() throws Exception { Class<?> dynamicType = new ByteBuddy(ClassFileVersion.JAVA_V4) .with(TypeValidation.DISABLED) .subclass(Object.class) .defineMethod(FOO, Object.class, Visibility.PUBLIC) .intercept(FixedValue.value(Object[].class)) .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(dynamicType.getDeclaredMethod(FOO).invoke(dynamicType.getDeclaredConstructor().newInstance()), is((Object) Object[].class)); }
Example #26
Source File: TypeWriterDefaultTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testPrimitiveTypeInLegacyConstantPoolRemapped() throws Exception { Class<?> dynamicType = new ByteBuddy(ClassFileVersion.JAVA_V4) .with(TypeValidation.DISABLED) .subclass(Object.class) .defineMethod(FOO, Object.class, Visibility.PUBLIC) .intercept(FixedValue.value(int.class)) .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(dynamicType.getDeclaredMethod(FOO).invoke(dynamicType.getDeclaredConstructor().newInstance()), is((Object) int.class)); }
Example #27
Source File: TypeWriterDefaultTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testLegacyTypeRedefinitionIsDiscovered() throws Exception { Class<?> dynamicType = new ByteBuddy() .with(TypeValidation.DISABLED) .redefine(Class.forName("net.bytebuddy.test.precompiled.TypeConstantSample")) .method(named(BAR)) .intercept(FixedValue.value(int.class)) .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(dynamicType.getDeclaredMethod(BAR).invoke(null), is((Object) int.class)); }
Example #28
Source File: TypeWriterDefaultTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test(expected = IllegalStateException.class) public void testMethodTypeInLegacyConstantPool() throws Exception { new ByteBuddy(ClassFileVersion.JAVA_V4) .subclass(Object.class) .defineMethod(FOO, Object.class) .intercept(FixedValue.value(JavaConstant.MethodType.of(Object.class, Object.class))) .make(); }
Example #29
Source File: TypeWriterDefaultTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test(expected = IllegalStateException.class) public void testDynamicConstantInPre11ConstantPool() throws Exception { new ByteBuddy(ClassFileVersion.JAVA_V10) .subclass(Object.class) .defineMethod(FOO, Object.class) .intercept(FixedValue.value(JavaConstant.Dynamic.ofNullConstant())) .make(); }
Example #30
Source File: TypeWriterDefaultTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test(expected = IllegalStateException.class) public void testMethodHandleInLegacyConstantPool() throws Exception { new ByteBuddy(ClassFileVersion.JAVA_V4) .subclass(Object.class) .defineMethod(FOO, Object.class) .intercept(FixedValue.value(JavaConstant.MethodHandle.of(new MethodDescription.ForLoadedMethod(Object.class.getDeclaredMethod("toString"))))) .make(); }