net.bytebuddy.dynamic.loading.ByteArrayClassLoader Java Examples
The following examples show how to use
net.bytebuddy.dynamic.loading.ByteArrayClassLoader.
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: AbstractDynamicTypeBuilderForInliningTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testEnabledAnnotationRetention() throws Exception { Class<?> type = create(Annotated.class) .field(ElementMatchers.any()).annotateField(new Annotation[0]) .method(ElementMatchers.any()).intercept(StubMethod.INSTANCE) .make() .load(new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileLocator.ForClassLoader.readToNames(SampleAnnotation.class)), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); @SuppressWarnings("unchecked") Class<? extends Annotation> sampleAnnotation = (Class<? extends Annotation>) type.getClassLoader().loadClass(SampleAnnotation.class.getName()); assertThat(type.isAnnotationPresent(sampleAnnotation), is(true)); assertThat(type.getDeclaredField(FOO).isAnnotationPresent(sampleAnnotation), is(true)); assertThat(type.getDeclaredMethod(FOO, Void.class).isAnnotationPresent(sampleAnnotation), is(true)); assertThat(type.getDeclaredMethod(FOO, Void.class).getParameterAnnotations()[0].length, is(1)); assertThat(type.getDeclaredMethod(FOO, Void.class).getParameterAnnotations()[0][0].annotationType(), is((Object) sampleAnnotation)); }
Example #2
Source File: PluginEngineDefaultTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testSimpleTransformation() throws Exception { Plugin.Engine.Listener listener = mock(Plugin.Engine.Listener.class); Plugin plugin = eager ? new SimplePlugin() : new PreprocessingPlugin(new SimplePlugin()); Plugin.Engine.Source source = Plugin.Engine.Source.InMemory.ofTypes(Sample.class); Plugin.Engine.Target.InMemory target = new Plugin.Engine.Target.InMemory(); Plugin.Engine.Summary summary = new Plugin.Engine.Default() .with(listener) .with(ClassFileLocator.ForClassLoader.of(SimplePlugin.class.getClassLoader())) .with(dispatcherFactory) .apply(source, target, new Plugin.Factory.Simple(plugin)); ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, target.toTypeMap()); Class<?> type = classLoader.loadClass(Sample.class.getName()); assertThat(type.getDeclaredField(FOO).getType(), is((Object) Void.class)); assertThat(summary.getTransformed(), hasItems(TypeDescription.ForLoadedType.of(Sample.class))); assertThat(summary.getFailed().size(), is(0)); assertThat(summary.getUnresolved().size(), is(0)); verify(listener).onManifest(Plugin.Engine.Source.Origin.NO_MANIFEST); verify(listener).onDiscovery(Sample.class.getName()); verify(listener).onTransformation(TypeDescription.ForLoadedType.of(Sample.class), plugin); verify(listener).onTransformation(TypeDescription.ForLoadedType.of(Sample.class), Collections.singletonList(plugin)); verify(listener).onComplete(TypeDescription.ForLoadedType.of(Sample.class)); verifyNoMoreInteractions(listener); }
Example #3
Source File: PluginEngineDefaultTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testSimpleTransformationIgnoredByPlugin() throws Exception { Plugin.Engine.Listener listener = mock(Plugin.Engine.Listener.class); Plugin plugin = eager ? new IgnoringPlugin() : new PreprocessingPlugin(new IgnoringPlugin()); Plugin.Engine.Source source = Plugin.Engine.Source.InMemory.ofTypes(Sample.class); Plugin.Engine.Target.InMemory target = new Plugin.Engine.Target.InMemory(); Plugin.Engine.Summary summary = new Plugin.Engine.Default() .with(listener) .with(ClassFileLocator.ForClassLoader.of(IgnoringPlugin.class.getClassLoader())) .with(dispatcherFactory) .apply(source, target, new Plugin.Factory.Simple(plugin)); ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, target.toTypeMap()); Class<?> type = classLoader.loadClass(Sample.class.getName()); assertThat(type.getDeclaredFields().length, is(0)); assertThat(summary.getTransformed().size(), is(0)); assertThat(summary.getFailed().size(), is(0)); assertThat(summary.getUnresolved().size(), is(0)); verify(listener).onManifest(Plugin.Engine.Source.Origin.NO_MANIFEST); verify(listener).onDiscovery(Sample.class.getName()); verify(listener).onIgnored(TypeDescription.ForLoadedType.of(Sample.class), plugin); verify(listener).onIgnored(TypeDescription.ForLoadedType.of(Sample.class), Collections.singletonList(plugin)); verify(listener).onComplete(TypeDescription.ForLoadedType.of(Sample.class)); verifyNoMoreInteractions(listener); }
Example #4
Source File: ContainerResourceMonitoringTracerTest.java From garmadon with Apache License 2.0 | 6 votes |
@Before public void setUp() throws IOException, ClassNotFoundException { classLoader = new ByteArrayClassLoader.ChildFirst(getClass().getClassLoader(), ClassFileExtraction.of( Tracer.class, MethodTracer.class, ContainerResourceMonitoringTracer.class, ContainerResourceMonitoringTracer.VcoreUsageTracer.class, Class.forName(ContainerResourceMonitoringTracer.VcoreUsageTracer.class.getName() + "$SingletonHolder"), ContainersMonitorImpl.class, Class.forName(ContainersMonitorImpl.class.getName() + "$MonitoringThread"), ContainerMetrics.class, Class.forName(ContainerMetrics.class.getName() + "$1") ), ByteArrayClassLoader.PersistenceHandler.MANIFEST); }
Example #5
Source File: PluginEngineDefaultTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testSimpleTransformationIgnoredByMatcher() throws Exception { Plugin.Engine.Listener listener = mock(Plugin.Engine.Listener.class); Plugin plugin = eager ? new SimplePlugin() : new PreprocessingPlugin(new SimplePlugin()); Plugin.Engine.Source source = Plugin.Engine.Source.InMemory.ofTypes(Sample.class); Plugin.Engine.Target.InMemory target = new Plugin.Engine.Target.InMemory(); Plugin.Engine.Summary summary = new Plugin.Engine.Default() .with(listener) .with(ClassFileLocator.ForClassLoader.of(SimplePlugin.class.getClassLoader())) .with(dispatcherFactory) .ignore(ElementMatchers.is(Sample.class)) .apply(source, target, new Plugin.Factory.Simple(plugin)); ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, target.toTypeMap()); Class<?> type = classLoader.loadClass(Sample.class.getName()); assertThat(type.getDeclaredFields().length, is(0)); assertThat(summary.getTransformed().size(), is(0)); assertThat(summary.getFailed().size(), is(0)); assertThat(summary.getUnresolved().size(), is(0)); verify(listener).onManifest(Plugin.Engine.Source.Origin.NO_MANIFEST); verify(listener).onDiscovery(Sample.class.getName()); verify(listener).onIgnored(TypeDescription.ForLoadedType.of(Sample.class), Collections.singletonList(plugin)); verify(listener).onComplete(TypeDescription.ForLoadedType.of(Sample.class)); verifyNoMoreInteractions(listener); }
Example #6
Source File: AbstractTypeDescriptionTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testNonEnclosedAnonymousType() throws Exception { ClassWriter classWriter = new ClassWriter(0); classWriter.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, "foo/Bar", null, "java/lang/Object", null); classWriter.visitInnerClass("foo/Bar", null, null, Opcodes.ACC_PUBLIC); classWriter.visitEnd(); ClassLoader classLoader = new ByteArrayClassLoader(null, Collections.singletonMap("foo.Bar", classWriter.toByteArray()), ByteArrayClassLoader.PersistenceHandler.MANIFEST); Class<?> type = classLoader.loadClass("foo.Bar"); assertThat(describe(type).isAnonymousType(), is(type.isAnonymousClass())); assertThat(describe(type).isLocalType(), is(type.isLocalClass())); assertThat(describe(type).isMemberType(), is(type.isMemberClass())); }
Example #7
Source File: PluginEngineDefaultTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testLiveInitializer() throws Exception { Plugin.Engine.Listener listener = mock(Plugin.Engine.Listener.class); Plugin plugin = new LiveInitializerPlugin(); Plugin.Engine.Source source = Plugin.Engine.Source.InMemory.ofTypes(Sample.class); Plugin.Engine.Target.InMemory target = new Plugin.Engine.Target.InMemory(); Plugin.Engine.Summary summary = new Plugin.Engine.Default() .with(listener) .withoutErrorHandlers() .with(ClassFileLocator.ForClassLoader.of(SimplePlugin.class.getClassLoader())) .with(dispatcherFactory) .apply(source, target, new Plugin.Factory.Simple(plugin)); ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, target.toTypeMap()); Class<?> type = classLoader.loadClass(Sample.class.getName()); assertThat(type.getDeclaredField(FOO).getType(), is((Object) Void.class)); assertThat(summary.getTransformed(), hasItems(TypeDescription.ForLoadedType.of(Sample.class))); assertThat(summary.getFailed().size(), is(0)); assertThat(summary.getUnresolved().size(), is(0)); verify(listener).onManifest(Plugin.Engine.Source.Origin.NO_MANIFEST); verify(listener).onDiscovery(Sample.class.getName()); verify(listener).onTransformation(TypeDescription.ForLoadedType.of(Sample.class), plugin); verify(listener).onTransformation(TypeDescription.ForLoadedType.of(Sample.class), Collections.singletonList(plugin)); verify(listener).onLiveInitializer(TypeDescription.ForLoadedType.of(Sample.class), TypeDescription.ForLoadedType.of(Sample.class)); verify(listener).onComplete(TypeDescription.ForLoadedType.of(Sample.class)); verifyNoMoreInteractions(listener); }
Example #8
Source File: AnnotationAppenderDefaultTest.java From byte-buddy with Apache License 2.0 | 6 votes |
private Class<?> makeTypeWithAnnotation(Annotation annotation) throws Exception { when(valueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true); ClassWriter classWriter = new ClassWriter(AsmVisitorWrapper.NO_FLAGS); classWriter.visit(ClassFileVersion.ofThisVm().getMinorMajorVersion(), Opcodes.ACC_PUBLIC, BAR.replace('.', '/'), null, Type.getInternalName(Object.class), null); AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(Type.getDescriptor(annotation.annotationType()), true); when(target.visit(any(String.class), anyBoolean())).thenReturn(annotationVisitor); AnnotationDescription annotationDescription = AnnotationDescription.ForLoadedAnnotation.of(annotation); annotationAppender.append(annotationDescription, valueFilter); classWriter.visitEnd(); Class<?> bar = new ByteArrayClassLoader(getClass().getClassLoader(), Collections.singletonMap(BAR, classWriter.toByteArray())).loadClass(BAR); assertThat(bar.getName(), is(BAR)); assertThat(bar.getSuperclass(), CoreMatchers.<Class<?>>is(Object.class)); return bar; }
Example #9
Source File: AnnotationAppenderDefaultTest.java From byte-buddy with Apache License 2.0 | 6 votes |
private Class<?> makeTypeWithSuperClassAnnotation(Annotation annotation) throws Exception { when(valueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true); ClassWriter classWriter = new ClassWriter(AsmVisitorWrapper.NO_FLAGS); classWriter.visit(ClassFileVersion.ofThisVm().getMinorMajorVersion(), Opcodes.ACC_PUBLIC, BAR.replace('.', '/'), null, Type.getInternalName(Object.class), null); AnnotationVisitor annotationVisitor = classWriter.visitTypeAnnotation(TypeReference.newSuperTypeReference(-1).getValue(), null, Type.getDescriptor(annotation.annotationType()), true); when(target.visit(any(String.class), anyBoolean())).thenReturn(annotationVisitor); AnnotationDescription annotationDescription = AnnotationDescription.ForLoadedAnnotation.of(annotation); annotationAppender.append(annotationDescription, valueFilter); classWriter.visitEnd(); Class<?> bar = new ByteArrayClassLoader(getClass().getClassLoader(), Collections.singletonMap(BAR, classWriter.toByteArray())).loadClass(BAR); assertThat(bar.getName(), is(BAR)); assertThat(bar.getSuperclass(), CoreMatchers.<Class<?>>is(Object.class)); return bar; }
Example #10
Source File: AbstractDynamicTypeBuilderTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testPreparedField() throws Exception { ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileLocator.ForClassLoader.readToNames(SampleAnnotation.class)); Class<?> type = createPlain() .defineMethod(BAR, String.class, Visibility.PUBLIC) .intercept(new PreparedField()) .make() .load(classLoader, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(type.getDeclaredFields().length, is(1)); assertThat(type.getDeclaredField(FOO).getName(), is(FOO)); assertThat(type.getDeclaredField(FOO).getType(), CoreMatchers.<Class<?>>is(Object.class)); assertThat(type.getDeclaredField(FOO).getModifiers(), is(MODIFIERS)); assertThat(type.getDeclaredField(FOO).getAnnotations().length, is(1)); Annotation annotation = type.getDeclaredField(FOO).getAnnotations()[0]; assertThat(annotation.annotationType().getName(), is(SampleAnnotation.class.getName())); Method foo = annotation.annotationType().getDeclaredMethod(FOO); assertThat(foo.invoke(annotation), is((Object) BAR)); }
Example #11
Source File: AbstractDynamicTypeBuilderTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testPreparedMethod() throws Exception { ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileLocator.ForClassLoader.readToNames(SampleAnnotation.class)); Class<?> type = createPlain() .defineMethod(BAR, String.class, Visibility.PUBLIC) .intercept(new PreparedMethod()) .make() .load(classLoader, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(type.getDeclaredMethods().length, is(2)); assertThat(type.getDeclaredMethod(FOO, Object.class).getName(), is(FOO)); assertThat(type.getDeclaredMethod(FOO, Object.class).getReturnType(), CoreMatchers.<Class<?>>is(Object.class)); assertThat(type.getDeclaredMethod(FOO, Object.class).getParameterTypes().length, is(1)); assertThat(type.getDeclaredMethod(FOO, Object.class).getParameterTypes()[0], CoreMatchers.<Class<?>>is(Object.class)); assertThat(type.getDeclaredMethod(FOO, Object.class).getModifiers(), is(MODIFIERS)); assertThat(type.getDeclaredMethod(FOO, Object.class).getAnnotations().length, is(1)); Annotation methodAnnotation = type.getDeclaredMethod(FOO, Object.class).getAnnotations()[0]; assertThat(methodAnnotation.annotationType().getName(), is(SampleAnnotation.class.getName())); Method methodMethod = methodAnnotation.annotationType().getDeclaredMethod(FOO); assertThat(methodMethod.invoke(methodAnnotation), is((Object) BAR)); assertThat(type.getDeclaredMethod(FOO, Object.class).getParameterAnnotations()[0].length, is(1)); Annotation parameterAnnotation = type.getDeclaredMethod(FOO, Object.class).getParameterAnnotations()[0][0]; assertThat(parameterAnnotation.annotationType().getName(), is(SampleAnnotation.class.getName())); Method parameterMethod = parameterAnnotation.annotationType().getDeclaredMethod(FOO); assertThat(parameterMethod.invoke(parameterAnnotation), is((Object) QUX)); }
Example #12
Source File: AbstractDynamicTypeBuilderForInliningTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testDisabledAnnotationRetention() throws Exception { Class<?> type = createDisabledRetention(Annotated.class) .field(ElementMatchers.any()).annotateField(new Annotation[0]) .method(ElementMatchers.any()).intercept(StubMethod.INSTANCE) .make() .load(new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileLocator.ForClassLoader.readToNames(SampleAnnotation.class)), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); @SuppressWarnings("unchecked") Class<? extends Annotation> sampleAnnotation = (Class<? extends Annotation>) type.getClassLoader().loadClass(SampleAnnotation.class.getName()); assertThat(type.isAnnotationPresent(sampleAnnotation), is(true)); assertThat(type.getDeclaredField(FOO).isAnnotationPresent(sampleAnnotation), is(false)); assertThat(type.getDeclaredMethod(FOO, Void.class).isAnnotationPresent(sampleAnnotation), is(false)); assertThat(type.getDeclaredMethod(FOO, Void.class).getParameterAnnotations()[0].length, is(0)); }
Example #13
Source File: MapRedInputFormatTracerTest.java From garmadon with Apache License 2.0 | 6 votes |
@Before public void setUp() throws IOException { eventHandler = mock(BiConsumer.class); argument = ArgumentCaptor.forClass(DataAccessEventProtos.PathEvent.class); MapReduceTracer.initEventHandler(eventHandler); inputSplit = mock(InputSplit.class); jobConf = mock(JobConf.class); reporter = mock(Reporter.class); when(jobConf.getJobName()).thenReturn("Application"); when(jobConf.getUser()).thenReturn("user"); classLoader = new ByteArrayClassLoader.ChildFirst(getClass().getClassLoader(), ClassFileExtraction.of( MapRedInputFormatTestClasses.OneLevelHierarchy.class, MapRedInputFormatTestClasses.AbstractInputFormat.class, MapRedInputFormatTestClasses.RealInputFormat.class, MapRedInputFormatTestClasses.Level1.class, MapRedInputFormatTestClasses.Level2CallingSuper.class, MapRedInputFormatTestClasses.Level3NotCallingSuper.class), ByteArrayClassLoader.PersistenceHandler.MANIFEST); }
Example #14
Source File: MapRedOutputFormatTracerTest.java From garmadon with Apache License 2.0 | 6 votes |
@Before public void setUp() throws IOException { eventHandler = mock(BiConsumer.class); argument = ArgumentCaptor.forClass(DataAccessEventProtos.PathEvent.class); MapReduceTracer.initEventHandler(eventHandler); jobConf = mock(JobConf.class); when(jobConf.getJobName()) .thenReturn("Application"); when(jobConf.getUser()) .thenReturn("user"); classLoader = new ByteArrayClassLoader.ChildFirst(getClass().getClassLoader(), ClassFileExtraction.of( MapRedOutputFormatTestClasses.OneLevelHierarchy.class ), ByteArrayClassLoader.PersistenceHandler.MANIFEST); }
Example #15
Source File: SerializerTest.java From subzero with Apache License 2.0 | 6 votes |
private void testAddedFields() throws Exception { String classname = "some.pckage.SyntheticPerson"; String v1Field = "firstname"; String[] v2Fields = {v1Field, "lastname"}; String expectedFirstname = "somename"; //v1 class has just a single field: firstname ByteArrayClassLoader v1classLoader = TestUtils.createClass(classname, v1Field); Serializer<Object> serializerV1 = new Serializer<Object>((Class<Object>) v1classLoader.loadClass(classname)); serializerV1.setHazelcastInstance(newMockHazelcastInstance(v1classLoader)); //v2 class has two fields - the default Kryo serializer is not deserialize it ByteArrayClassLoader v2classLoader = TestUtils.createClass(classname, v2Fields); Serializer<Object> serializerV2 = new Serializer<Object>((Class<Object>) v2classLoader.loadClass(classname)); serializerV2.setHazelcastInstance(newMockHazelcastInstance(v2classLoader)); Object v1Instance = v1classLoader.loadClass(classname).newInstance(); v1Instance.getClass().getField(v1Field).set(v1Instance, expectedFirstname); byte[] blob = TestUtils.serialize(serializerV1, v1Instance); Object v2Instance = TestUtils.deserialize(serializerV2, blob); String actualFirstname = (String) v2Instance.getClass().getField(v1Field).get(v2Instance); assertEquals(expectedFirstname, actualFirstname); }
Example #16
Source File: AbstractDynamicTypeBuilderForInliningTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testVisibilityBridge() throws Exception { InjectionClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, false, ClassFileLocator.ForClassLoader.readToNames(VisibilityBridge.class, FooBar.class)); Class<?> type = create(PackagePrivateVisibilityBridgeExtension.class) .modifiers(Opcodes.ACC_PUBLIC) .make() .load(classLoader, InjectionClassLoader.Strategy.INSTANCE) .getLoaded(); assertThat(type.getDeclaredConstructors().length, is(1)); Constructor<?> constructor = type.getDeclaredConstructor(); constructor.setAccessible(true); assertThat(type.getDeclaredMethods().length, is(2)); Method foo = type.getDeclaredMethod(FOO, String.class); foo.setAccessible(true); assertThat(foo.isBridge(), is(true)); assertThat(foo.getDeclaredAnnotations().length, is(1)); assertThat(foo.getDeclaredAnnotations()[0].annotationType().getName(), is(FooBar.class.getName())); assertThat(foo.invoke(constructor.newInstance(), BAR), is((Object) (FOO + BAR))); assertThat(foo.getParameterAnnotations()[0].length, is(1)); assertThat(foo.getParameterAnnotations()[0][0].annotationType().getName(), is(FooBar.class.getName())); assertThat(foo.invoke(constructor.newInstance(), BAR), is((Object) (FOO + BAR))); Method bar = type.getDeclaredMethod(BAR, List.class); bar.setAccessible(true); assertThat(bar.isBridge(), is(true)); assertThat(bar.getDeclaredAnnotations().length, is(0)); List<?> list = new ArrayList<Object>(); assertThat(bar.invoke(constructor.newInstance(), list), sameInstance((Object) list)); assertThat(bar.getGenericReturnType(), instanceOf(Class.class)); assertThat(bar.getGenericParameterTypes()[0], instanceOf(Class.class)); assertThat(bar.getGenericExceptionTypes()[0], instanceOf(Class.class)); }
Example #17
Source File: AbstractTypeDescriptionGenericTest.java From byte-buddy with Apache License 2.0 | 5 votes |
public static Field make() throws IOException, ClassNotFoundException, NoSuchFieldException { ClassReader classReader = new ClassReader(InconsistentGenerics.class.getName()); ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS); classReader.accept(new GenericDisintegrator(classWriter), 0); return new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, Collections.singletonMap(InconsistentGenerics.class.getName(), classWriter.toByteArray()), ByteArrayClassLoader.PersistenceHandler.MANIFEST) .loadClass(InconsistentGenerics.class.getName()).getDeclaredField(FOO); }
Example #18
Source File: AbstractDynamicTypeBuilderForInliningTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testNoVisibilityBridgeForAbstractMethod() throws Exception { InjectionClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, false, ClassFileLocator.ForClassLoader.readToNames(PackagePrivateVisibilityBridgeExtensionAbstractMethod.class, VisibilityBridgeAbstractMethod.class)); Class<?> type = create(PackagePrivateVisibilityBridgeExtensionAbstractMethod.class) .modifiers(Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT) .make() .load(classLoader, InjectionClassLoader.Strategy.INSTANCE) .getLoaded(); assertThat(type.getDeclaredConstructors().length, is(1)); assertThat(type.getDeclaredMethods().length, is(0)); }
Example #19
Source File: AbstractDynamicTypeBuilderForInliningTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testNoVisibilityBridgeForInheritedType() throws Exception { InjectionClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, false, ClassFileLocator.ForClassLoader.readToNames(PublicVisibilityBridgeExtension.class, VisibilityBridge.class, FooBar.class)); Class<?> type = new ByteBuddy().subclass(PublicVisibilityBridgeExtension.class) .modifiers(Opcodes.ACC_PUBLIC) .make() .load(classLoader, InjectionClassLoader.Strategy.INSTANCE) .getLoaded(); assertThat(type.getDeclaredConstructors().length, is(1)); assertThat(type.getDeclaredMethods().length, is(0)); }
Example #20
Source File: PluginEngineDefaultTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testSimpleTransformationErrorIgnored() throws Exception { Plugin.Engine.Listener listener = mock(Plugin.Engine.Listener.class); RuntimeException exception = new RuntimeException(); Plugin plugin = eager ? new FailingPlugin(exception) : new PreprocessingPlugin(new FailingPlugin(exception)); Plugin.Engine.Source source = Plugin.Engine.Source.InMemory.ofTypes(Sample.class); Plugin.Engine.Target.InMemory target = new Plugin.Engine.Target.InMemory(); Plugin.Engine.Summary summary = new Plugin.Engine.Default() .with(listener) .withoutErrorHandlers() .with(ClassFileLocator.ForClassLoader.of(FailingPlugin.class.getClassLoader())) .with(dispatcherFactory) .apply(source, target, new Plugin.Factory.Simple(plugin)); assertThat(summary.getTransformed().size(), is(0)); assertThat(summary.getFailed().size(), is(1)); assertThat(summary.getFailed().containsKey(TypeDescription.ForLoadedType.of(Sample.class)), is(true)); assertThat(summary.getUnresolved().size(), is(0)); assertThat(target.getStorage().size(), is(1)); ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, target.toTypeMap()); Class<?> type = classLoader.loadClass(Sample.class.getName()); assertThat(type.getDeclaredFields().length, is(0)); verify(listener).onManifest(Plugin.Engine.Source.Origin.NO_MANIFEST); verify(listener).onDiscovery(Sample.class.getName()); verify(listener).onError(TypeDescription.ForLoadedType.of(Sample.class), plugin, exception); verify(listener).onError(TypeDescription.ForLoadedType.of(Sample.class), Collections.<Throwable>singletonList(exception)); verify(listener).onComplete(TypeDescription.ForLoadedType.of(Sample.class)); verify(listener).onError(Collections.singletonMap(TypeDescription.ForLoadedType.of(Sample.class), Collections.<Throwable>singletonList(exception))); verify(listener).onError(plugin, exception); verifyNoMoreInteractions(listener); }
Example #21
Source File: PluginEngineDefaultTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testUnresolved() throws Exception { Plugin.Engine.Listener listener = mock(Plugin.Engine.Listener.class); Plugin plugin = eager ? new SimplePlugin() : new PreprocessingPlugin(new SimplePlugin()); Plugin.Engine.Source source = new Plugin.Engine.Source.InMemory(Collections.singletonMap( Sample.class.getName().replace('.', '/') + ".class", ClassFileLocator.ForClassLoader.read(Sample.class))) { @Override public ClassFileLocator getClassFileLocator() { return ClassFileLocator.NoOp.INSTANCE; } }; Plugin.Engine.Target.InMemory target = new Plugin.Engine.Target.InMemory(); Plugin.Engine.Summary summary = new Plugin.Engine.Default() .with(listener) .withoutErrorHandlers() .with(dispatcherFactory) .apply(source, target, new Plugin.Factory.Simple(plugin)); ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, target.toTypeMap()); Class<?> type = classLoader.loadClass(Sample.class.getName()); assertThat(type.getDeclaredFields().length, is(0)); assertThat(summary.getTransformed().size(), is(0)); assertThat(summary.getFailed().size(), is(0)); assertThat(summary.getUnresolved().size(), is(1)); assertThat(summary.getUnresolved().contains(Sample.class.getName()), is(true)); verify(listener).onManifest(Plugin.Engine.Source.Origin.NO_MANIFEST); verify(listener).onDiscovery(Sample.class.getName()); verify(listener).onUnresolved(Sample.class.getName()); verifyNoMoreInteractions(listener); }
Example #22
Source File: AbstractDynamicTypeBuilderForInliningTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testNoVisibilityBridgeForNonPublicType() throws Exception { InjectionClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, false, ClassFileLocator.ForClassLoader.readToNames(PackagePrivateVisibilityBridgeExtension.class, VisibilityBridge.class, FooBar.class)); Class<?> type = create(PackagePrivateVisibilityBridgeExtension.class) .modifiers(0) .make() .load(classLoader, InjectionClassLoader.Strategy.INSTANCE) .getLoaded(); assertThat(type.getDeclaredConstructors().length, is(1)); assertThat(type.getDeclaredMethods().length, is(0)); }
Example #23
Source File: MethodDelegationSuperTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testFinalType() throws Exception { InjectionClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, false, ClassFileLocator.ForClassLoader.readToNames(SimpleInterceptor.class)); Class<?> type = new ByteBuddy() .rebase(FinalType.class) .modifiers(TypeManifestation.PLAIN, Visibility.PUBLIC) .method(named(FOO)).intercept(ExceptionMethod.throwing(RuntimeException.class)) .method(named(BAR)).intercept(MethodDelegation.to(SimpleInterceptor.class)) .make() .load(classLoader, InjectionClassLoader.Strategy.INSTANCE) .getLoaded(); assertThat(type.getDeclaredMethod(BAR).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO)); }
Example #24
Source File: AbstractDynamicTypeBuilderTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testTypeInitializer() throws Exception { ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileLocator.ForClassLoader.readToNames(Bar.class)); Class<?> type = createPlain() .invokable(isTypeInitializer()).intercept(MethodCall.invoke(Bar.class.getDeclaredMethod("invoke"))) .make() .load(classLoader, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(type.getDeclaredConstructor().newInstance(), notNullValue(Object.class)); Class<?> foo = classLoader.loadClass(Bar.class.getName()); assertThat(foo.getDeclaredField(FOO).get(null), is((Object) FOO)); }
Example #25
Source File: SubclassDynamicTypeBuilderTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testDoesNotOverridePrivateMethod() throws Exception { Class<?> type = new ByteBuddy() .subclass(PrivateMethod.class) .method(isDeclaredBy(PrivateMethod.class)) .intercept(StubMethod.INSTANCE) .make() .load(new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileLocator.ForClassLoader.readToNames(PrivateMethod.class)), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(type.getDeclaredMethods().length, is(0)); }
Example #26
Source File: SubclassDynamicTypeBuilderTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testDoesNotOverrideMethodWithPackagePrivateArgumentType() throws Exception { Class<?> type = new ByteBuddy() .subclass(PackagePrivateArgumentType.class) .name("net.bytebuddy.test.generated." + FOO) .method(isDeclaredBy(PackagePrivateArgumentType.class)) .intercept(StubMethod.INSTANCE) .make() .load(new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileLocator.ForClassLoader.readToNames(PackagePrivateArgumentType.class, PackagePrivateArgumentType.Argument.class)), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(type.getDeclaredMethods().length, is(0)); }
Example #27
Source File: NexusTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testNexusAccessorNonActive() throws Exception { ClassLoader classLoader = new ByteArrayClassLoader.ChildFirst(getClass().getClassLoader(), ClassFileLocator.ForClassLoader.readToNames(Nexus.class, NexusAccessor.class, NexusAccessor.Dispatcher.class, NexusAccessor.Dispatcher.CreationAction.class, NexusAccessor.Dispatcher.Available.class, NexusAccessor.Dispatcher.Unavailable.class), null, ByteArrayClassLoader.PersistenceHandler.MANIFEST, PackageDefinitionStrategy.NoOp.INSTANCE); Field duplicateInitializers = classLoader.loadClass(Nexus.class.getName()).getDeclaredField("TYPE_INITIALIZERS"); duplicateInitializers.setAccessible(true); assertThat(((Map<?, ?>) duplicateInitializers.get(null)).size(), is(0)); Field actualInitializers = Nexus.class.getDeclaredField("TYPE_INITIALIZERS"); actualInitializers.setAccessible(true); assertThat(((Map<?, ?>) actualInitializers.get(null)).size(), is(0)); Class<?> accessor = classLoader.loadClass(NexusAccessor.class.getName()); ClassLoader qux = mock(ClassLoader.class); when(loadedTypeInitializer.isAlive()).thenReturn(false); assertThat(accessor .getDeclaredMethod("register", String.class, ClassLoader.class, int.class, LoadedTypeInitializer.class) .invoke(accessor.getDeclaredConstructor().newInstance(), FOO, qux, BAR, loadedTypeInitializer), nullValue(Object.class)); try { assertThat(((Map<?, ?>) duplicateInitializers.get(null)).size(), is(0)); assertThat(((Map<?, ?>) actualInitializers.get(null)).size(), is(0)); } finally { Constructor<Nexus> constructor = Nexus.class.getDeclaredConstructor(String.class, ClassLoader.class, ReferenceQueue.class, int.class); constructor.setAccessible(true); Object value = ((Map<?, ?>) actualInitializers.get(null)).remove(constructor.newInstance(FOO, qux, null, BAR)); assertThat(value, nullValue()); } }
Example #28
Source File: NexusTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testNexusAccessorClassLoaderBoundary() throws Exception { ClassLoader classLoader = new ByteArrayClassLoader.ChildFirst(getClass().getClassLoader(), ClassFileLocator.ForClassLoader.readToNames(Nexus.class, NexusAccessor.class, NexusAccessor.Dispatcher.class, NexusAccessor.Dispatcher.CreationAction.class, NexusAccessor.Dispatcher.Available.class, NexusAccessor.Dispatcher.Unavailable.class), null, ByteArrayClassLoader.PersistenceHandler.MANIFEST, PackageDefinitionStrategy.NoOp.INSTANCE); Field duplicateInitializers = classLoader.loadClass(Nexus.class.getName()).getDeclaredField("TYPE_INITIALIZERS"); duplicateInitializers.setAccessible(true); assertThat(((Map<?, ?>) duplicateInitializers.get(null)).size(), is(0)); Field actualInitializers = Nexus.class.getDeclaredField("TYPE_INITIALIZERS"); actualInitializers.setAccessible(true); assertThat(((Map<?, ?>) actualInitializers.get(null)).size(), is(0)); Class<?> accessor = classLoader.loadClass(NexusAccessor.class.getName()); ClassLoader qux = mock(ClassLoader.class); when(loadedTypeInitializer.isAlive()).thenReturn(true); assertThat(accessor .getDeclaredMethod("register", String.class, ClassLoader.class, int.class, LoadedTypeInitializer.class) .invoke(accessor.getDeclaredConstructor().newInstance(), FOO, qux, BAR, loadedTypeInitializer), nullValue(Object.class)); try { assertThat(((Map<?, ?>) duplicateInitializers.get(null)).size(), is(0)); assertThat(((Map<?, ?>) actualInitializers.get(null)).size(), is(1)); } finally { Constructor<Nexus> constructor = Nexus.class.getDeclaredConstructor(String.class, ClassLoader.class, ReferenceQueue.class, int.class); constructor.setAccessible(true); Object value = ((Map<?, ?>) actualInitializers.get(null)).remove(constructor.newInstance(FOO, qux, null, BAR)); assertThat(value, is((Object) loadedTypeInitializer)); } }
Example #29
Source File: NexusTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testNexusAccessorClassLoaderNoResource() throws Exception { ClassLoader classLoader = new ByteArrayClassLoader.ChildFirst(getClass().getClassLoader(), ClassFileLocator.ForClassLoader.readToNames(Nexus.class, NexusAccessor.class, NexusAccessor.Dispatcher.class, NexusAccessor.Dispatcher.CreationAction.class, NexusAccessor.Dispatcher.Available.class, NexusAccessor.Dispatcher.Unavailable.class), null, ByteArrayClassLoader.PersistenceHandler.LATENT, PackageDefinitionStrategy.NoOp.INSTANCE); Field duplicateInitializers = classLoader.loadClass(Nexus.class.getName()).getDeclaredField("TYPE_INITIALIZERS"); duplicateInitializers.setAccessible(true); assertThat(((Map<?, ?>) duplicateInitializers.get(null)).size(), is(0)); Field actualInitializers = Nexus.class.getDeclaredField("TYPE_INITIALIZERS"); actualInitializers.setAccessible(true); assertThat(((Map<?, ?>) actualInitializers.get(null)).size(), is(0)); Class<?> accessor = classLoader.loadClass(NexusAccessor.class.getName()); ClassLoader qux = mock(ClassLoader.class); when(loadedTypeInitializer.isAlive()).thenReturn(true); assertThat(accessor .getDeclaredMethod("register", String.class, ClassLoader.class, int.class, LoadedTypeInitializer.class) .invoke(accessor.getDeclaredConstructor().newInstance(), FOO, qux, BAR, loadedTypeInitializer), nullValue(Object.class)); try { assertThat(((Map<?, ?>) duplicateInitializers.get(null)).size(), is(0)); assertThat(((Map<?, ?>) actualInitializers.get(null)).size(), is(1)); } finally { Constructor<Nexus> constructor = Nexus.class.getDeclaredConstructor(String.class, ClassLoader.class, ReferenceQueue.class, int.class); constructor.setAccessible(true); Object value = ((Map<?, ?>) actualInitializers.get(null)).remove(constructor.newInstance(FOO, qux, null, BAR)); assertThat(value, is((Object) loadedTypeInitializer)); } }
Example #30
Source File: SubclassDynamicTypeBuilderTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testDoesNotOverrideMethodWithPackagePrivateReturnType() throws Exception { Class<?> type = new ByteBuddy() .subclass(PackagePrivateReturnType.class) .name("net.bytebuddy.test.generated." + FOO) .method(isDeclaredBy(PackagePrivateReturnType.class)) .intercept(StubMethod.INSTANCE) .make() .load(new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileLocator.ForClassLoader.readToNames(PackagePrivateReturnType.class, PackagePrivateReturnType.Argument.class)), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(type.getDeclaredMethods().length, is(0)); }