net.bytebuddy.description.method.ParameterDescription Java Examples

The following examples show how to use net.bytebuddy.description.method.ParameterDescription. 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: AbstractAnnotationDescriptionTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
private void assertValue(Annotation annotation, String methodName, Object unloadedValue, Object loadedValue) throws Exception {
    assertThat(describe(annotation).getValue(new MethodDescription.ForLoadedMethod(annotation.annotationType().getDeclaredMethod(methodName))).resolve(),
            is(unloadedValue));
    assertThat(describe(annotation).getValue(new MethodDescription.Latent(TypeDescription.ForLoadedType.of(annotation.annotationType()),
            methodName,
            Opcodes.ACC_PUBLIC,
            Collections.<TypeVariableToken>emptyList(),
            TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(annotation.annotationType().getDeclaredMethod(methodName).getReturnType()),
            Collections.<ParameterDescription.Token>emptyList(),
            Collections.<TypeDescription.Generic>emptyList(),
            Collections.<AnnotationDescription>emptyList(),
            AnnotationValue.UNDEFINED,
            TypeDescription.Generic.UNDEFINED)).resolve(), is(unloadedValue));
    assertThat(annotation.annotationType().getDeclaredMethod(methodName).invoke(describe(annotation).prepare(annotation.annotationType()).load()),
            is(loadedValue));
}
 
Example #2
Source File: MethodCall.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation toStackManipulation(ParameterDescription target, Assigner assigner, Assigner.Typing typing) {
    if (!fieldDescription.isStatic() && instrumentedMethod.isStatic()) {
        throw new IllegalStateException("Cannot access non-static " + fieldDescription + " from " + instrumentedMethod);
    }
    StackManipulation stackManipulation = new StackManipulation.Compound(
            fieldDescription.isStatic()
                    ? StackManipulation.Trivial.INSTANCE
                    : MethodVariableAccess.loadThis(),
            FieldAccess.forField(fieldDescription).read(),
            assigner.assign(fieldDescription.getType(), target.getType(), typing)
    );
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot assign " + fieldDescription + " to " + target);
    }
    return stackManipulation;
}
 
Example #3
Source File: MethodCall.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Resolves this appender to a stack manipulation.
 *
 * @param instrumentedMethod The instrumented method.
 * @param invokedMethod      The invoked method.
 * @param targetHandler      The resolved target handler to base the stack manipulation upon.
 * @return A stack manipulation that represents this method call.
 */
protected StackManipulation toStackManipulation(MethodDescription instrumentedMethod, MethodDescription invokedMethod, TargetHandler.Resolved targetHandler) {
    List<ArgumentLoader> argumentLoaders = new ArrayList<ArgumentLoader>();
    for (ArgumentLoader.ArgumentProvider argumentProvider : argumentProviders) {
        argumentLoaders.addAll(argumentProvider.resolve(instrumentedMethod, invokedMethod));
    }
    ParameterList<?> parameters = invokedMethod.getParameters();
    if (parameters.size() != argumentLoaders.size()) {
        throw new IllegalStateException(invokedMethod + " does not accept " + argumentLoaders.size() + " arguments");
    }
    Iterator<? extends ParameterDescription> parameterIterator = parameters.iterator();
    List<StackManipulation> argumentInstructions = new ArrayList<StackManipulation>(argumentLoaders.size());
    for (ArgumentLoader argumentLoader : argumentLoaders) {
        argumentInstructions.add(argumentLoader.toStackManipulation(parameterIterator.next(), assigner, typing));
    }
    return new StackManipulation.Compound(
            targetHandler.toStackManipulation(invokedMethod, assigner, typing),
            new StackManipulation.Compound(argumentInstructions),
            methodInvoker.toStackManipulation(invokedMethod, implementationTarget),
            terminationHandler.toStackManipulation(invokedMethod, instrumentedMethod, assigner, typing)
    );
}
 
Example #4
Source File: ConstructorStrategyDefaultTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testDefaultConstructorStrategyWithInheritedAnnotations() throws Exception {
    when(methodDescription.getParameters()).thenReturn(new ParameterList.Empty<ParameterDescription.InGenericShape>());
    ConstructorStrategy constructorStrategy = ConstructorStrategy.Default.DEFAULT_CONSTRUCTOR.withInheritedAnnotations();
    assertThat(constructorStrategy.extractConstructors(instrumentedType),
            is(Collections.singletonList(new MethodDescription.Token(Opcodes.ACC_PUBLIC))));
    assertThat(constructorStrategy.inject(instrumentedType, methodRegistry), is(methodRegistry));
    verify(methodRegistry).append(any(LatentMatcher.class),
            any(MethodRegistry.Handler.class),
            eq(MethodAttributeAppender.ForInstrumentedMethod.EXCLUDING_RECEIVER),
            eq(Transformer.NoOp.<MethodDescription>make()));
    verifyNoMoreInteractions(methodRegistry);
    verify(instrumentedType).getSuperClass();
    verifyNoMoreInteractions(instrumentedType);
}
 
Example #5
Source File: ConstructorStrategyDefaultTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testDefaultConstructorStrategyWithAttributeAppender() throws Exception {
    when(methodDescription.getParameters()).thenReturn(new ParameterList.Empty<ParameterDescription.InGenericShape>());
    MethodAttributeAppender.Factory methodAttributeAppenderFactory = mock(MethodAttributeAppender.Factory.class);
    ConstructorStrategy constructorStrategy = ConstructorStrategy.Default.DEFAULT_CONSTRUCTOR.with(methodAttributeAppenderFactory);
    assertThat(constructorStrategy.extractConstructors(instrumentedType),
            is(Collections.singletonList(new MethodDescription.Token(Opcodes.ACC_PUBLIC))));
    assertThat(constructorStrategy.inject(instrumentedType, methodRegistry), is(methodRegistry));
    verify(methodRegistry).append(any(LatentMatcher.class),
            any(MethodRegistry.Handler.class),
            eq(methodAttributeAppenderFactory),
            eq(Transformer.NoOp.<MethodDescription>make()));
    verifyNoMoreInteractions(methodRegistry);
    verify(instrumentedType).getSuperClass();
    verifyNoMoreInteractions(instrumentedType);
}
 
Example #6
Source File: MethodAttributeAppenderForInstrumentedMethodOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReceiverTypeTypeAnnotationsNoRetention() throws Exception {
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(methodDescription.getReceiverType()).thenReturn(simpleAnnotatedType);
    when(simpleAnnotatedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Qux.Instance()));
    MethodAttributeAppender.ForInstrumentedMethod.INCLUDING_RECEIVER.apply(methodVisitor, methodDescription, annotationValueFilter);
    verifyZeroInteractions(methodVisitor);
    verify(methodDescription).getDeclaredAnnotations();
    verify(methodDescription).getParameters();
    verify(methodDescription).getReturnType();
    verify(methodDescription).getExceptionTypes();
    verify(methodDescription).getTypeVariables();
    verify(methodDescription).getReceiverType();
    verifyNoMoreInteractions(methodDescription);
}
 
Example #7
Source File: FieldProxyBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetterForImplicitNamedFieldInNamedType() throws Exception {
    when(target.getType()).thenReturn(genericGetterType);
    doReturn(Foo.class).when(annotation).declaringType();
    when(instrumentedType.isAssignableTo(TypeDescription.ForLoadedType.of(Foo.class))).thenReturn(true);
    when(annotation.value()).thenReturn(FieldProxy.Binder.BEAN_PROPERTY);
    when(fieldDescription.getInternalName()).thenReturn(FOO);
    when(source.getReturnType()).thenReturn(genericFieldType);
    when(source.getParameters()).thenReturn(new ParameterList.Empty<ParameterDescription.InDefinedShape>());
    when(source.getName()).thenReturn("getFoo");
    when(source.getActualName()).thenReturn("getFoo");
    when(source.getInternalName()).thenReturn("getFoo");
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    MethodDelegationBinder.ParameterBinding<?> binding = new FieldProxy.Binder(getterMethod, setterMethod).bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(true));
}
 
Example #8
Source File: RebaseImplementationTargetTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    when(methodGraph.locate(Mockito.any(MethodDescription.SignatureToken.class))).thenReturn(MethodGraph.Node.Unresolved.INSTANCE);
    when(instrumentedType.getSuperClass()).thenReturn(superClass);
    when(superClass.asErasure()).thenReturn(rawSuperClass);
    when(rawSuperClass.getInternalName()).thenReturn(BAR);
    when(rebasedMethod.getInternalName()).thenReturn(QUX);
    when(rebasedMethod.asToken(ElementMatchers.is(instrumentedType))).thenReturn(rebasedToken);
    when(rebasedMethod.getDescriptor()).thenReturn(FOO);
    when(rebasedMethod.asDefined()).thenReturn(rebasedMethod);
    when(rebasedMethod.getReturnType()).thenReturn(genericReturnType);
    when(rebasedMethod.getParameters()).thenReturn(new ParameterList.Empty<ParameterDescription.InDefinedShape>());
    when(rebasedMethod.getDeclaringType()).thenReturn(instrumentedType);
    when(rebasedMethod.asSignatureToken()).thenReturn(rebasedSignatureToken);
    super.setUp();
}
 
Example #9
Source File: TypeWriterMethodPoolRecordTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    when(methodDescription.getInternalName()).thenReturn(FOO);
    when(methodDescription.getDescriptor()).thenReturn(BAR);
    when(methodDescription.getGenericSignature()).thenReturn(QUX);
    when(methodDescription.getExceptionTypes()).thenReturn(exceptionTypes);
    when(methodDescription.getActualModifiers(anyBoolean(), any(Visibility.class))).thenReturn(MODIFIERS);
    when(exceptionTypes.asErasures()).thenReturn(rawExceptionTypes);
    when(rawExceptionTypes.toInternalNames()).thenReturn(new String[]{BAZ});
    when(classVisitor.visitMethod(MODIFIERS, FOO, BAR, QUX, new String[]{BAZ})).thenReturn(methodVisitor);
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Explicit<ParameterDescription>(parameterDescription));
    when(parameterDescription.getName()).thenReturn(FOO);
    when(parameterDescription.getModifiers()).thenReturn(MODIFIERS);
    when(methodVisitor.visitAnnotationDefault()).thenReturn(annotationVisitor);
    when(byteCodeAppender.apply(methodVisitor, implementationContext, methodDescription))
            .thenReturn(new ByteCodeAppender.Size(ONE, TWO));
    when(otherAppender.apply(methodVisitor, implementationContext, methodDescription))
            .thenReturn(new ByteCodeAppender.Size(ONE * MULTIPLIER, TWO * MULTIPLIER));
    when(annotationValueFilterFactory.on(methodDescription)).thenReturn(annotationValueFilter);
    when(methodDescription.getVisibility()).thenReturn(Visibility.PUBLIC);
}
 
Example #10
Source File: FieldProxyBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetterForExplicitNamedFieldInHierarchy() throws Exception {
    when(target.getType()).thenReturn(genericGetterType);
    doReturn(void.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FOO);
    when(fieldDescription.getActualName()).thenReturn(FOO);
    when(source.getReturnType()).thenReturn(genericFieldType);
    when(source.getParameters()).thenReturn(new ParameterList.Empty<ParameterDescription.InDefinedShape>());
    when(source.getName()).thenReturn("getFoo");
    when(source.getInternalName()).thenReturn("getFoo");
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    MethodDelegationBinder.ParameterBinding<?> binding = new FieldProxy.Binder(getterMethod, setterMethod).bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(true));
}
 
Example #11
Source File: FixedValue.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
    if (instrumentedMethod.getParameters().size() <= index) {
        throw new IllegalStateException(instrumentedMethod + " does not define a parameter with index " + index);
    }
    ParameterDescription parameterDescription = instrumentedMethod.getParameters().get(index);
    StackManipulation stackManipulation = new StackManipulation.Compound(
            MethodVariableAccess.load(parameterDescription),
            assigner.assign(parameterDescription.getType(), instrumentedMethod.getReturnType(), typing),
            MethodReturn.of(instrumentedMethod.getReturnType())
    );
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot assign " + instrumentedMethod.getReturnType() + " to " + parameterDescription);
    }
    return new Size(stackManipulation.apply(methodVisitor, implementationContext).getMaximalSize(), instrumentedMethod.getStackSize());
}
 
Example #12
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetterNameDiscovery() throws Exception {
    doReturn(void.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FieldValue.Binder.Delegate.BEAN_PROPERTY);
    when(instrumentedType.getDeclaredFields()).thenReturn(new FieldList.Explicit<FieldDescription.InDefinedShape>(fieldDescription));
    when(fieldDescription.getActualName()).thenReturn(FOO);
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(true);
    when(source.getInternalName()).thenReturn("getFoo");
    when(source.getActualName()).thenReturn("getFoo");
    when(source.getReturnType()).thenReturn(TypeDescription.Generic.OBJECT);
    when(source.getParameters()).thenReturn(new ParameterList.Empty<ParameterDescription.InDefinedShape>());
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(true));
}
 
Example #13
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMethodParameterAnnotationClassFileRetention() throws Exception {
    when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    ParameterDescription parameterDescription = mock(ParameterDescription.class);
    when(parameterDescription.getType()).thenReturn(TypeDescription.Generic.OBJECT);
    when(parameterDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new QuxBaz.Instance()));
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Explicit<ParameterDescription>(parameterDescription));
    when(methodDescription.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verify(methodVisitor).visitParameterAnnotation(0, Type.getDescriptor(QuxBaz.class), false);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #14
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testTypeVariableTypeAnnotationRuntimeRetention() throws Exception {
    when(annotatedTypeVariable.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    when(annotatedTypeVariableBound.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Explicit(annotatedTypeVariable));
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verify(methodVisitor).visitTypeAnnotation(TypeReference.newTypeParameterReference(TypeReference.METHOD_TYPE_PARAMETER, 0).getValue(),
            null,
            Type.getDescriptor(Baz.class),
            true);
    verify(methodVisitor).visitTypeAnnotation(TypeReference.newTypeParameterBoundReference(TypeReference.METHOD_TYPE_PARAMETER_BOUND, 0, 0).getValue(),
            null,
            Type.getDescriptor(Baz.class),
            true);
    verifyZeroInteractions(methodVisitor);
}
 
Example #15
Source File: InstrumentedTypeDefaultTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testMethodIllegalTypeVariableTypeAnnotation() throws Exception {
    makePlainInstrumentedType()
            .withMethod(new MethodDescription.Token(FOO,
                    ModifierContributor.EMPTY_MASK,
                    Collections.singletonList(new TypeVariableToken(FOO,
                            Collections.singletonList(TypeDescription.Generic.OBJECT),
                            Collections.singletonList(AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build()))),
                    TypeDescription.Generic.OBJECT,
                    Collections.<ParameterDescription.Token>emptyList(),
                    Collections.<TypeDescription.Generic>emptyList(),
                    Collections.<AnnotationDescription>emptyList(),
                    AnnotationValue.UNDEFINED,
                    TypeDescription.Generic.UNDEFINED))
            .validated();
}
 
Example #16
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMethodExceptionTypeTypeAnnotationClassFileRetention() throws Exception {
    when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Explicit(simpleAnnotatedType));
    when(simpleAnnotatedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new QuxBaz.Instance()));
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verify(methodVisitor).visitTypeAnnotation(TypeReference.newExceptionReference(0).getValue(),
            null,
            Type.getDescriptor(QuxBaz.class),
            false);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #17
Source File: ControllerMethodData.java    From Diorite with MIT License 6 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
protected ControllerMethodData(Controller controller, TypeDescription classType, InDefinedShape member, String name, int index)
{
    super(controller, classType, member, name, index);
    if (member.isStatic())
    {
        throw new IllegalStateException("Can't use injections on static methods! (Source: " + member.getDeclaringType().getCanonicalName() + "#" +
                                        member.getName() + " " + member.getDescriptor());
    }
    Map<Class<? extends Annotation>, ? extends Annotation> rawScopeAnnotations = controller.extractRawScopeAnnotations(member);
    Map<Class<? extends Annotation>, ? extends Annotation> rawQualifierAnnotations = controller.extractRawQualifierAnnotations(member);
    this.scopeAnnotations = controller.transformAll(this.classType, name, member, rawScopeAnnotations);
    this.qualifierAnnotations = controller.transformAll(this.classType, name, member, rawQualifierAnnotations);
    ParameterList<ParameterDescription.InDefinedShape> parameters = member.getParameters();
    org.diorite.inject.impl.data.InjectValueData<?, TypeDescription.ForLoadedType.Generic>[] values =
            new org.diorite.inject.impl.data.InjectValueData[parameters.size()];
    int i = 0;
    for (ParameterDescription.InDefinedShape param : parameters)
    {
        values[i] = controller.createValue(i, classType, param.getType(), param, Controller.fixName(param.getType(), param.getName()),
                                           rawScopeAnnotations, rawQualifierAnnotations);
        i++;
    }
    this.values = List.of(values);
}
 
Example #18
Source File: TargetMethodAnnotationDrivenBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static MethodDelegationBinder.ParameterBinding<?> prepareArgumentBinder(TargetMethodAnnotationDrivenBinder.ParameterBinder<?> parameterBinder,
                                                                                Class<? extends Annotation> annotationType,
                                                                                Object identificationToken) {
    doReturn(annotationType).when(parameterBinder).getHandledType();
    MethodDelegationBinder.ParameterBinding<?> parameterBinding = mock(MethodDelegationBinder.ParameterBinding.class);
    when(parameterBinding.isValid()).thenReturn(true);
    when(parameterBinding.apply(any(MethodVisitor.class), any(Implementation.Context.class))).thenReturn(new StackManipulation.Size(0, 0));
    when(parameterBinding.getIdentificationToken()).thenReturn(identificationToken);
    when(((TargetMethodAnnotationDrivenBinder.ParameterBinder) parameterBinder).bind(any(AnnotationDescription.Loadable.class),
            any(MethodDescription.class),
            any(ParameterDescription.class),
            any(Implementation.Target.class),
            any(Assigner.class),
            any(Assigner.Typing.class)))
            .thenReturn(parameterBinding);
    return parameterBinding;
}
 
Example #19
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMethodReturnTypeTypeAnnotationClassFileRetention() throws Exception {
    when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(simpleAnnotatedType);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    when(simpleAnnotatedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new QuxBaz.Instance()));
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verify(methodVisitor).visitTypeAnnotation(TypeReference.newTypeReference(TypeReference.METHOD_RETURN).getValue(),
            null,
            Type.getDescriptor(QuxBaz.class),
            false);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #20
Source File: InstrumentedTypeDefaultTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testInconsistentReceiverConstructor() throws Exception {
    makePlainInstrumentedType()
            .withMethod(new MethodDescription.Token(MethodDescription.CONSTRUCTOR_INTERNAL_NAME,
                    ModifierContributor.EMPTY_MASK,
                    Collections.<TypeVariableToken>emptyList(),
                    TypeDescription.Generic.OBJECT,
                    Collections.<ParameterDescription.Token>emptyList(),
                    Collections.<TypeDescription.Generic>emptyList(),
                    Collections.<AnnotationDescription>emptyList(),
                    AnnotationValue.UNDEFINED,
                    TypeDescription.Generic.OBJECT))
            .validated();
}
 
Example #21
Source File: ImplementationContextDefaultTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testFieldGetterRegistration() throws Exception {
    Implementation.Context.Default implementationContext = new Implementation.Context.Default(instrumentedType,
            classFileVersion,
            auxiliaryTypeNamingStrategy,
            typeInitializer,
            auxiliaryClassFileVersion);
    MethodDescription firstFieldGetter = implementationContext.registerGetterFor(firstField, MethodAccessorFactory.AccessType.DEFAULT);
    assertThat(firstFieldGetter.getParameters(), is((ParameterList) new ParameterList.Empty<ParameterDescription>()));
    assertThat(firstFieldGetter.getReturnType(), is(firstFieldType));
    assertThat(firstFieldGetter.getInternalName(), startsWith(FOO));
    assertThat(firstFieldGetter.getModifiers(), is(accessorMethodModifiers));
    assertThat(firstFieldGetter.getExceptionTypes(), is((TypeList.Generic) new TypeList.Generic.Empty()));
    assertThat(implementationContext.registerGetterFor(firstField, MethodAccessorFactory.AccessType.DEFAULT), is(firstFieldGetter));
    when(secondField.isStatic()).thenReturn(true);
    MethodDescription secondFieldGetter = implementationContext.registerGetterFor(secondField, MethodAccessorFactory.AccessType.DEFAULT);
    assertThat(secondFieldGetter.getParameters(), is((ParameterList) new ParameterList.Empty<ParameterDescription>()));
    assertThat(secondFieldGetter.getReturnType(), is(secondFieldType));
    assertThat(secondFieldGetter.getInternalName(), startsWith(BAR));
    assertThat(secondFieldGetter.getModifiers(), is(accessorMethodModifiers | Opcodes.ACC_STATIC));
    assertThat(secondFieldGetter.getExceptionTypes(), is((TypeList.Generic) new TypeList.Generic.Empty()));
    assertThat(implementationContext.registerGetterFor(firstField, MethodAccessorFactory.AccessType.DEFAULT), is(firstFieldGetter));
    assertThat(implementationContext.registerGetterFor(secondField, MethodAccessorFactory.AccessType.DEFAULT), is(secondFieldGetter));
    implementationContext.drain(drain, classVisitor, annotationValueFilterFactory);
    verify(classVisitor).visitMethod(eq(accessorMethodModifiers), Mockito.startsWith(FOO),
            eq("()" + BAR), Mockito.<String>isNull(), Mockito.<String[]>isNull());
    verify(classVisitor).visitMethod(eq(accessorMethodModifiers | Opcodes.ACC_STATIC), Mockito.startsWith(BAR),
            eq("()" + QUX), Mockito.<String>isNull(), Mockito.<String[]>isNull());
}
 
Example #22
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMethodAnnotationClassFileRetention() throws Exception {
    when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new QuxBaz.Instance()));
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verify(methodVisitor).visitAnnotation(Type.getDescriptor(QuxBaz.class), false);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #23
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMethodParameterAnnotationNoRetention() throws Exception {
    when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    ParameterDescription parameterDescription = mock(ParameterDescription.class);
    when(parameterDescription.getType()).thenReturn(TypeDescription.Generic.OBJECT);
    when(parameterDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Qux.Instance()));
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Explicit<ParameterDescription>(parameterDescription));
    when(methodDescription.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verifyZeroInteractions(methodVisitor);
}
 
Example #24
Source File: InstrumentedTypeDefaultTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testMethodParameterIllegalModifiers() throws Exception {
    makePlainInstrumentedType()
            .withMethod(new MethodDescription.Token(FOO,
                    ModifierContributor.EMPTY_MASK,
                    Collections.<TypeVariableToken>emptyList(),
                    TypeDescription.Generic.OBJECT,
                    Collections.singletonList(new ParameterDescription.Token(TypeDescription.Generic.OBJECT, FOO, -1)),
                    Collections.<TypeDescription.Generic>emptyList(),
                    Collections.<AnnotationDescription>emptyList(),
                    AnnotationValue.UNDEFINED,
                    TypeDescription.Generic.UNDEFINED))
            .validated();
}
 
Example #25
Source File: InstrumentedTypeDefaultTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testNonNullReceiverStaticMethod() throws Exception {
    makePlainInstrumentedType()
            .withMethod(new MethodDescription.Token(FOO,
                    Opcodes.ACC_STATIC,
                    Collections.<TypeVariableToken>emptyList(),
                    TypeDescription.Generic.OBJECT,
                    Collections.<ParameterDescription.Token>emptyList(),
                    Collections.<TypeDescription.Generic>emptyList(),
                    Collections.<AnnotationDescription>emptyList(),
                    AnnotationValue.UNDEFINED,
                    TypeDescription.Generic.OBJECT))
            .validated();
}
 
Example #26
Source File: ConstructorStrategyDefaultTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    when(methodRegistry.append(any(LatentMatcher.class),
            any(MethodRegistry.Handler.class),
            any(MethodAttributeAppender.Factory.class),
            any(Transformer.class))).thenReturn(methodRegistry);
    when(instrumentedType.getSuperClass()).thenReturn(superClass);
    when(superClass.getDeclaredMethods()).thenReturn(new MethodList.Explicit<MethodDescription.InGenericShape>(methodDescription));
    when(methodDescription.isConstructor()).thenReturn(true);
    when(methodDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    when(methodDescription.asToken(matchesPrototype(ElementMatchers.is(instrumentedType)))).thenReturn(token);
    when(token.getName()).thenReturn(FOO);
    when(token.getModifiers()).thenReturn(MODIFIERS);
    when(token.getTypeVariableTokens()).thenReturn(new ByteCodeElement.Token.TokenList<TypeVariableToken>());
    when(token.getReturnType()).thenReturn(typeDescription);
    when(token.getParameterTokens()).thenReturn(new ByteCodeElement.Token.TokenList<ParameterDescription.Token>());
    when(token.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    when(token.getAnnotations()).thenReturn(new AnnotationList.Empty());
    when(token.getDefaultValue()).thenReturn((AnnotationValue) defaultValue);
    when(token.getReceiverType()).thenReturn(typeDescription);
    stripped = new MethodDescription.Token(FOO,
            MODIFIERS,
            Collections.<TypeVariableToken>emptyList(),
            typeDescription,
            Collections.<ParameterDescription.Token>emptyList(),
            Collections.<TypeDescription.Generic>emptyList(),
            Collections.<AnnotationDescription>emptyList(),
            defaultValue,
            TypeDescription.Generic.UNDEFINED);
}
 
Example #27
Source File: InstrumentedTypeDefaultTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testMethodInvisibleExceptionType() throws Exception {
    makePlainInstrumentedType()
            .withMethod(new MethodDescription.Token(FOO,
                    ModifierContributor.EMPTY_MASK,
                    Collections.<TypeVariableToken>emptyList(),
                    TypeDescription.Generic.OBJECT,
                    Collections.<ParameterDescription.Token>emptyList(),
                    Collections.singletonList(TypeDefinition.Sort.describe(PackagePrivateType.EXCEPTION_TYPE)),
                    Collections.<AnnotationDescription>emptyList(),
                    AnnotationValue.UNDEFINED,
                    TypeDescription.Generic.UNDEFINED))
            .validated();
}
 
Example #28
Source File: InstrumentedTypeDefaultTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testMethodTypeVariableDoubleClassBound() throws Exception {
    makePlainInstrumentedType()
            .withMethod(new MethodDescription.Token(FOO,
                    ModifierContributor.EMPTY_MASK,
                    Collections.singletonList(new TypeVariableToken(FOO, Arrays.asList(TypeDescription.Generic.OBJECT, TypeDefinition.Sort.describe(String.class)))),
                    TypeDescription.Generic.OBJECT,
                    Collections.<ParameterDescription.Token>emptyList(),
                    Collections.<TypeDescription.Generic>emptyList(),
                    Collections.<AnnotationDescription>emptyList(),
                    AnnotationValue.UNDEFINED,
                    TypeDescription.Generic.UNDEFINED))
            .validated();
}
 
Example #29
Source File: InstrumentedTypeDefaultTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testMethodIncompatibleAnnotation() throws Exception {
    makePlainInstrumentedType()
            .withMethod(new MethodDescription.Token(FOO,
                    ModifierContributor.EMPTY_MASK,
                    Collections.<TypeVariableToken>emptyList(),
                    TypeDescription.Generic.OBJECT,
                    Collections.<ParameterDescription.Token>emptyList(),
                    Collections.<TypeDescription.Generic>emptyList(),
                    Collections.singletonList(AnnotationDescription.Builder.ofType(IncompatibleAnnotation.class).build()),
                    AnnotationValue.UNDEFINED,
                    TypeDescription.Generic.UNDEFINED))
            .validated();
}
 
Example #30
Source File: InstrumentedTypeDefaultTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testInconsistentReceiverConstructorInnerClass() throws Exception {
    InstrumentedType.Factory.Default.MODIFIABLE.represent(TypeDescription.ForLoadedType.of(Foo.class))
            .withMethod(new MethodDescription.Token(MethodDescription.CONSTRUCTOR_INTERNAL_NAME,
                    ModifierContributor.EMPTY_MASK,
                    Collections.<TypeVariableToken>emptyList(),
                    TypeDescription.Generic.OBJECT,
                    Collections.<ParameterDescription.Token>emptyList(),
                    Collections.<TypeDescription.Generic>emptyList(),
                    Collections.<AnnotationDescription>emptyList(),
                    AnnotationValue.UNDEFINED,
                    TypeDefinition.Sort.describe(Foo.class)))
            .validated();
}