From e8bc26a94aa74a03118c86e5fd2f7f77afb9e0c7 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Mon, 4 Jan 2016 18:43:57 +0100 Subject: [PATCH] Fixed several todos. --- .../bytebuddy/agent/builder/AgentBuilder.java | 13 +++--- ...onStrategySelfInjectionDispatcherTest.java | 1 + ...gentBuilderInitializationStrategyTest.java | 40 ++++++++++++++++--- .../dynamic/MethodTransformerSimpleTest.java | 15 +++---- 4 files changed, 48 insertions(+), 21 deletions(-) diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/agent/builder/AgentBuilder.java b/byte-buddy-dep/src/main/java/net/bytebuddy/agent/builder/AgentBuilder.java index 2cb75384c87..ccb76dfbf8c 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/agent/builder/AgentBuilder.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/agent/builder/AgentBuilder.java @@ -1181,7 +1181,7 @@ public int hashCode() { @Override public String toString() { - return "AgentBuilder.InitializationStrategy.SelfInjection.Dispatcher.InjectingInitializer{" + // TODO: object properties + return "AgentBuilder.InitializationStrategy.SelfInjection.Dispatcher.InjectingInitializer{" + "instrumentedType=" + instrumentedType + ", rawAuxiliaryTypes=" + rawAuxiliaryTypes + ", loadedTypeInitializers=" + loadedTypeInitializers + @@ -1495,21 +1495,18 @@ public DynamicType.Builder apply(DynamicType.Builder builder) { @Override public void register(DynamicType dynamicType, ClassLoader classLoader, InjectorFactory injectorFactory) { - TypeDescription instrumentedType = dynamicType.getTypeDescription(); Map auxiliaryTypes = dynamicType.getAuxiliaryTypes(); Map independentTypes = new LinkedHashMap(auxiliaryTypes); for (TypeDescription auxiliaryType : auxiliaryTypes.keySet()) { - if (auxiliaryType.isAssignableTo(instrumentedType)) { + if (!auxiliaryType.getDeclaredAnnotations().isAnnotationPresent(AuxiliaryType.Eager.class)) { independentTypes.remove(auxiliaryType); } } - if (!auxiliaryTypes.isEmpty()) { + if (!independentTypes.isEmpty()) { ClassInjector classInjector = injectorFactory.resolve(); Map loadedTypeInitializers = dynamicType.getLoadedTypeInitializers(); - if (!independentTypes.isEmpty()) { - for (Map.Entry> entry : classInjector.inject(independentTypes).entrySet()) { - loadedTypeInitializers.get(entry.getKey()).onLoad(entry.getValue()); - } + for (Map.Entry> entry : classInjector.inject(independentTypes).entrySet()) { + loadedTypeInitializers.get(entry.getKey()).onLoad(entry.getValue()); } } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/agent/builder/AgentBuilderInitializationStrategySelfInjectionDispatcherTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/agent/builder/AgentBuilderInitializationStrategySelfInjectionDispatcherTest.java index a5e9e6abcc5..77f6a816df7 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/agent/builder/AgentBuilderInitializationStrategySelfInjectionDispatcherTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/agent/builder/AgentBuilderInitializationStrategySelfInjectionDispatcherTest.java @@ -197,6 +197,7 @@ public void testObjectProperties() throws Exception { ObjectPropertyAssertion.of(AgentBuilder.InitializationStrategy.SelfInjection.Dispatcher.Lazy.class).apply(); ObjectPropertyAssertion.of(AgentBuilder.InitializationStrategy.SelfInjection.Dispatcher.Eager.class).apply(); ObjectPropertyAssertion.of(AgentBuilder.InitializationStrategy.SelfInjection.Dispatcher.Split.class).apply(); + ObjectPropertyAssertion.of(AgentBuilder.InitializationStrategy.SelfInjection.Dispatcher.InjectingInitializer.class).apply(); } private static class Foo { diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/agent/builder/AgentBuilderInitializationStrategyTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/agent/builder/AgentBuilderInitializationStrategyTest.java index f782eeff353..8aeb91659eb 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/agent/builder/AgentBuilderInitializationStrategyTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/agent/builder/AgentBuilderInitializationStrategyTest.java @@ -1,9 +1,13 @@ package net.bytebuddy.agent.builder; +import net.bytebuddy.description.annotation.AnnotationList; +import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.dynamic.DynamicType; import net.bytebuddy.dynamic.loading.ByteArrayClassLoader; +import net.bytebuddy.dynamic.loading.ClassInjector; import net.bytebuddy.dynamic.loading.PackageDefinitionStrategy; import net.bytebuddy.implementation.LoadedTypeInitializer; +import net.bytebuddy.implementation.auxiliary.AuxiliaryType; import net.bytebuddy.test.utility.ClassFileExtraction; import net.bytebuddy.test.utility.MockitoRule; import net.bytebuddy.test.utility.ObjectPropertyAssertion; @@ -12,25 +16,25 @@ import org.junit.rules.TestRule; import org.mockito.Mock; +import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.security.AccessController; -import java.util.Arrays; -import java.util.Iterator; -import java.util.Map; +import java.util.*; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.*; public class AgentBuilderInitializationStrategyTest { private static final String FOO = "foo"; + private static final byte[] QUX = new byte[]{1, 2, 3}, BAZ = new byte[]{4, 5, 6}; + private static final int BAR = 42; @Rule @@ -82,8 +86,28 @@ public void testPrematureApplication() throws Exception { } @Test + @SuppressWarnings("unchecked") public void testPrematureRegistration() throws Exception { - AgentBuilder.InitializationStrategy.Minimal.INSTANCE.register(dynamicType, classLoader, injectorFactory); // TODO + Annotation eagerAnnotation = mock(AuxiliaryType.Eager.class); + when(eagerAnnotation.annotationType()).thenReturn((Class) AuxiliaryType.Eager.class); + TypeDescription independent = mock(TypeDescription.class), dependent = mock(TypeDescription.class); + when(independent.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotation(eagerAnnotation)); + when(dependent.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty()); + Map map = new HashMap(); + map.put(independent, QUX); + map.put(dependent, BAZ); + when(dynamicType.getAuxiliaryTypes()).thenReturn(map); + ClassInjector classInjector = mock(ClassInjector.class); + when(injectorFactory.resolve()).thenReturn(classInjector); + when(classInjector.inject(Collections.singletonMap(independent, QUX))) + .thenReturn(Collections.>singletonMap(independent, Foo.class)); + LoadedTypeInitializer loadedTypeInitializer = mock(LoadedTypeInitializer.class); + when(dynamicType.getLoadedTypeInitializers()).thenReturn(Collections.singletonMap(independent, loadedTypeInitializer)); + AgentBuilder.InitializationStrategy.Minimal.INSTANCE.register(dynamicType, classLoader, injectorFactory); + verify(classInjector).inject(Collections.singletonMap(independent, QUX)); + verifyNoMoreInteractions(classInjector); + verify(loadedTypeInitializer).onLoad(Foo.class); + verifyNoMoreInteractions(loadedTypeInitializer); } @Test @@ -199,4 +223,8 @@ public Method create() { ObjectPropertyAssertion.of(AgentBuilder.InitializationStrategy.SelfInjection.NexusAccessor.InitializationAppender.class).apply(); ObjectPropertyAssertion.of(AgentBuilder.InitializationStrategy.Minimal.class).apply(); } + + private static class Foo { + /* empty */ + } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/MethodTransformerSimpleTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/MethodTransformerSimpleTest.java index a9ec8dba1bb..e10b59548c6 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/MethodTransformerSimpleTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/MethodTransformerSimpleTest.java @@ -56,7 +56,7 @@ public class MethodTransformerSimpleTest { private ParameterDescription.InDefinedShape definedParameter; @Mock - private TypeDescription.Generic returnType, typeVariable, parameterType, exceptionType, declaringType; + private TypeDescription.Generic returnType, typeVariableBound, parameterType, exceptionType, declaringType; @Mock private AnnotationDescription methodAnnotation, parameterAnnotation; @@ -68,19 +68,19 @@ public class MethodTransformerSimpleTest { @SuppressWarnings("unchecked") public void setUp() throws Exception { when(returnType.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(returnType); - when(typeVariable.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(typeVariable); + when(typeVariableBound.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(typeVariableBound); when(parameterType.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(parameterType); when(exceptionType.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(exceptionType); - when(typeVariable.getSymbol()).thenReturn(QUX); - when(typeVariable.getSort()).thenReturn(TypeDefinition.Sort.VARIABLE); - when(typeVariable.asGenericType()).thenReturn(typeVariable); + when(typeVariableBound.getSymbol()).thenReturn(QUX); + when(typeVariableBound.getSort()).thenReturn(TypeDefinition.Sort.VARIABLE); + when(typeVariableBound.asGenericType()).thenReturn(typeVariableBound); when(methodDescription.asToken()).thenReturn(methodToken); when(methodDescription.getDeclaringType()).thenReturn(declaringType); when(methodDescription.asDefined()).thenReturn(definedMethod); when(methodToken.getName()).thenReturn(FOO); when(methodToken.getModifiers()).thenReturn(MODIFIERS); when(methodToken.getReturnType()).thenReturn(returnType); - when(methodToken.getTypeVariables()).thenReturn(Collections.singletonMap(QUX, new TypeList.Generic.Explicit(typeVariable))); + when(methodToken.getTypeVariables()).thenReturn(Collections.singletonMap(QUX, new TypeList.Generic.Explicit(typeVariableBound))); when(methodToken.getExceptionTypes()).thenReturn(new TypeList.Generic.Explicit(exceptionType)); when(methodToken.getParameterTokens()) .thenReturn(new ByteCodeElement.Token.TokenList(parameterToken)); @@ -131,7 +131,8 @@ public void testModifierTransformation() throws Exception { assertThat(transformed.getReturnType(), is(returnType)); assertThat(transformed.getTypeVariables().size(), is(1)); assertThat(transformed.getTypeVariables().containsKey(QUX), is(true)); - assertThat(transformed.getTypeVariables().get(QUX).size(), is(1)); // TODO + assertThat(transformed.getTypeVariables().get(QUX).size(), is(1)); + assertThat(transformed.getTypeVariables().get(QUX).contains(typeVariableBound), is(true)); assertThat(transformed.getExceptionTypes().size(), is(1)); assertThat(transformed.getExceptionTypes().getOnly(), is(exceptionType)); assertThat(transformed.getParameterTokens().size(), is(1));