Skip to content

Commit

Permalink
Fixed several todos.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Jan 4, 2016
1 parent 49315ed commit e8bc26a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
Expand Up @@ -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 +
Expand Down Expand Up @@ -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<TypeDescription, byte[]> auxiliaryTypes = dynamicType.getAuxiliaryTypes();
Map<TypeDescription, byte[]> independentTypes = new LinkedHashMap<TypeDescription, byte[]>(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<TypeDescription, LoadedTypeInitializer> loadedTypeInitializers = dynamicType.getLoadedTypeInitializers();
if (!independentTypes.isEmpty()) {
for (Map.Entry<TypeDescription, Class<?>> entry : classInjector.inject(independentTypes).entrySet()) {
loadedTypeInitializers.get(entry.getKey()).onLoad(entry.getValue());
}
for (Map.Entry<TypeDescription, Class<?>> entry : classInjector.inject(independentTypes).entrySet()) {
loadedTypeInitializers.get(entry.getKey()).onLoad(entry.getValue());
}
}
}
Expand Down
Expand Up @@ -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 {
Expand Down
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<TypeDescription, byte[]> map = new HashMap<TypeDescription, byte[]>();
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.<TypeDescription, Class<?>>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
Expand Down Expand Up @@ -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 */
}
}
Expand Up @@ -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;
Expand All @@ -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.<String, TypeList.Generic>singletonMap(QUX, new TypeList.Generic.Explicit(typeVariable)));
when(methodToken.getTypeVariables()).thenReturn(Collections.<String, TypeList.Generic>singletonMap(QUX, new TypeList.Generic.Explicit(typeVariableBound)));
when(methodToken.getExceptionTypes()).thenReturn(new TypeList.Generic.Explicit(exceptionType));
when(methodToken.getParameterTokens())
.thenReturn(new ByteCodeElement.Token.TokenList<ParameterDescription.Token>(parameterToken));
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit e8bc26a

Please sign in to comment.