net.bytebuddy.dynamic.loading.PackageDefinitionStrategy Java Examples
The following examples show how to use
net.bytebuddy.dynamic.loading.PackageDefinitionStrategy.
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: HelperClassManager.java From apm-agent-java with Apache License 2.0 | 6 votes |
static Class injectClass(@Nullable ClassLoader targetClassLoader, @Nullable ProtectionDomain pd, String className, boolean isBootstrapClass) throws IOException, ClassNotFoundException { if (targetClassLoader == null) { if (isBootstrapClass) { return Class.forName(className, false, null); } else { throw new UnsupportedOperationException("Cannot load non-bootstrap class from bootstrap class loader"); } } ClassInjector classInjector; if (targetClassLoader == ClassLoader.getSystemClassLoader()) { classInjector = ClassInjector.UsingReflection.ofSystemClassLoader(); } else { classInjector = new ClassInjector.UsingReflection(targetClassLoader, pd, PackageDefinitionStrategy.NoOp.INSTANCE, true); } final byte[] classBytes = getAgentClassBytes(className); final TypeDescription typeDesc = new TypeDescription.Latent(className, 0, null, Collections.<TypeDescription.Generic>emptyList()); Map<TypeDescription, byte[]> typeMap = new HashMap<>(); typeMap.put(typeDesc, classBytes); return classInjector.inject(typeMap).values().iterator().next(); }
Example #2
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 #3
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 #4
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 #5
Source File: AbstractTypeDescriptionTest.java From byte-buddy with Apache License 2.0 | 4 votes |
@SuppressWarnings({"unchecked", "deprecation"}) protected AbstractTypeDescriptionTest() { standardTypes = Arrays.asList( Object.class, Object[].class, SampleClass.class, SampleClass[].class, SampleInterface.class, SampleInterface[].class, SampleAnnotation.class, SampleAnnotation[].class, void.class, byte.class, byte[].class, short.class, short[].class, char.class, char[].class, int.class, int[].class, long.class, long[].class, float.class, float[].class, double.class, double[].class, new EnclosingType().localMethod, Array.newInstance(new EnclosingType().localConstructor, 1).getClass(), new EnclosingType().anonymousMethod, Array.newInstance(new EnclosingType().anonymousMethod, 1).getClass(), new EnclosingType().localConstructor, Array.newInstance(new EnclosingType().localConstructor, 1).getClass(), new EnclosingType().anonymousConstructor, Array.newInstance(new EnclosingType().anonymousConstructor, 1).getClass(), EnclosingType.LOCAL_INITIALIZER, Array.newInstance(EnclosingType.LOCAL_INITIALIZER.getClass(), 1).getClass(), EnclosingType.ANONYMOUS_INITIALIZER, Array.newInstance(EnclosingType.ANONYMOUS_INITIALIZER, 1).getClass(), EnclosingType.LOCAL_METHOD, Array.newInstance(EnclosingType.LOCAL_METHOD.getClass(), 1).getClass(), EnclosingType.ANONYMOUS_METHOD, Array.newInstance(EnclosingType.ANONYMOUS_METHOD, 1).getClass(), EnclosingType.INNER, Array.newInstance(EnclosingType.INNER, 1).getClass(), EnclosingType.NESTED, Array.newInstance(EnclosingType.NESTED, 1).getClass(), EnclosingType.PRIVATE_INNER, Array.newInstance(EnclosingType.PRIVATE_INNER, 1).getClass(), EnclosingType.PRIVATE_NESTED, Array.newInstance(EnclosingType.PRIVATE_NESTED, 1).getClass(), EnclosingType.PROTECTED_INNER, Array.newInstance(EnclosingType.PROTECTED_INNER, 1).getClass(), EnclosingType.PROTECTED_NESTED, Array.newInstance(EnclosingType.PROTECTED_NESTED, 1).getClass(), EnclosingType.PACKAGE_INNER, Array.newInstance(EnclosingType.PACKAGE_INNER, 1).getClass(), EnclosingType.PACKAGE_NESTED, Array.newInstance(EnclosingType.PACKAGE_NESTED, 1).getClass(), EnclosingType.FINAL_NESTED, Array.newInstance(EnclosingType.FINAL_NESTED, 1).getClass(), EnclosingType.FINAL_INNER, Array.newInstance(EnclosingType.FINAL_INNER, 1).getClass(), EnclosingType.DEPRECATED, Array.newInstance(EnclosingType.DEPRECATED, 1).getClass(), Type$With$Dollar.class, new ByteBuddy() .subclass(Object.class) .name("sample.WithoutDefinedPackage") .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT.with(PackageDefinitionStrategy.NoOp.INSTANCE)) .getLoaded(), new ByteBuddy() .subclass(Object.class) .name("WithoutPackage") .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT) .getLoaded()); }
Example #6
Source File: ModelClassLoader.java From activejpa with Apache License 2.0 | 4 votes |
/** * @param loader */ public ModelClassLoader(ClassLoader loader) { super(loader, new HashMap<>(), ClassLoadingStrategy.NO_PROTECTION_DOMAIN, PersistenceHandler.MANIFEST, PackageDefinitionStrategy.Trivial.INSTANCE, new ModelClassEnhancer().getTransformer()); }