From d16aee40137c59a215bddf5ed6d434e7c207c68f Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter <rafael.wth@gmail.com> Date: Fri, 1 Nov 2024 00:09:39 +0100 Subject: [PATCH] Add methods that allow that loading is avoided. --- .../net/bytebuddy/dynamic/DynamicType.java | 24 ++++++++++++++++--- .../dynamic/loading/ClassInjector.java | 19 --------------- .../dynamic/DynamicTypeDefaultTest.java | 18 ++++++++++++++ 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/DynamicType.java b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/DynamicType.java index 3455a4d8792..f464449e14d 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/DynamicType.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/DynamicType.java @@ -86,12 +86,19 @@ public interface DynamicType extends ClassFileLocator { */ byte[] getBytes(); + /** + * Returns a set of all auxiliary types that are represented by this dynamic type. + * + * @return A set of all auxiliary types. + */ + Set<TypeDescription> getAuxiliaryTypeDescriptions(); + /** * Returns a set of all types that are represented by this dynamic type. * * @return A set of all represented types. */ - Set<TypeDescription> getTypeDescriptions(); + Set<TypeDescription> getAllTypeDescriptions(); /** * <p> @@ -6133,11 +6140,22 @@ public void close() { /** * {@inheritDoc} */ - public Set<TypeDescription> getTypeDescriptions() { + public Set<TypeDescription> getAuxiliaryTypeDescriptions() { + Set<TypeDescription> types = new LinkedHashSet<TypeDescription>(); + for (DynamicType auxiliaryType : auxiliaryTypes) { + types.addAll(auxiliaryType.getAllTypeDescriptions()); + } + return types; + } + + /** + * {@inheritDoc} + */ + public Set<TypeDescription> getAllTypeDescriptions() { Set<TypeDescription> types = new LinkedHashSet<TypeDescription>(); types.add(typeDescription); for (DynamicType auxiliaryType : auxiliaryTypes) { - types.addAll(auxiliaryType.getTypeDescriptions()); + types.addAll(auxiliaryType.getAllTypeDescriptions()); } return types; } diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/loading/ClassInjector.java b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/loading/ClassInjector.java index 26c6c78194e..0071ab2a0c5 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/loading/ClassInjector.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/loading/ClassInjector.java @@ -97,8 +97,6 @@ public interface ClassInjector { */ Map<String, Class<?>> inject(Set<String> names, ClassFileLocator classFileLocator); - Map<TypeDescription, Class<?>> inject(List<? extends DynamicType> dynamicTypes); - /** * Injects the given types into the represented class loader. * @@ -120,23 +118,6 @@ public interface ClassInjector { */ abstract class AbstractBase implements ClassInjector { - /** - * {@inheritDoc} - */ - public Map<TypeDescription, Class<?>> inject(List<? extends DynamicType> dynamicTypes) { - Map<String, TypeDescription> types = new LinkedHashMap<String, TypeDescription>(); - for (DynamicType dynamicType : dynamicTypes) { - for (TypeDescription typeDescription : dynamicType.getTypeDescriptions()) { - types.put(typeDescription.getName(), typeDescription); - } - } - Map<TypeDescription, Class<?>> result = new HashMap<TypeDescription, Class<?>>(); - for (Map.Entry<String, Class<?>> entry : inject(types.keySet(), new ClassFileLocator.Compound(dynamicTypes)).entrySet()) { - result.put(types.get(entry.getKey()), entry.getValue()); - } - return result; - } - /** * {@inheritDoc} */ diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/DynamicTypeDefaultTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/DynamicTypeDefaultTest.java index 3380ed34960..5172d59a0c2 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/DynamicTypeDefaultTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/DynamicTypeDefaultTest.java @@ -327,6 +327,24 @@ public void testIterationOrder() throws Exception { assertThat(types.hasNext(), is(false)); } + @Test + public void testIterationOrderAllDescriptions() throws Exception { + Iterator<TypeDescription> types = dynamicType.getAllTypeDescriptions().iterator(); + assertThat(types.hasNext(), is(true)); + assertThat(types.next(), is(typeDescription)); + assertThat(types.hasNext(), is(true)); + assertThat(types.next(), is(auxiliaryTypeDescription)); + assertThat(types.hasNext(), is(false)); + } + + @Test + public void testIterationOrderAuxiliaryDescriptions() throws Exception { + Iterator<TypeDescription> types = dynamicType.getAuxiliaryTypeDescriptions().iterator(); + assertThat(types.hasNext(), is(true)); + assertThat(types.next(), is(auxiliaryTypeDescription)); + assertThat(types.hasNext(), is(false)); + } + @Test public void testClassFileLocator() throws Exception { assertThat(dynamicType.locate(FOOBAR.replace('/', '.')).isResolved(), is(true));