net.bytebuddy.description.type.TypeList Java Examples

The following examples show how to use net.bytebuddy.description.type.TypeList. 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: DefaultMethodBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testBindExplicit() throws Exception {
    when(targetType.isAssignableFrom(Method.class)).thenReturn(true);
    when(source.isMethod()).thenReturn(true);
    when(implementationTarget.invokeDefault(token, TypeDescription.ForLoadedType.of(Runnable.class))).thenReturn(specialMethodInvocation);
    when(specialMethodInvocation.isValid()).thenReturn(true);
    when(annotation.targetType()).thenReturn((Class) Runnable.class);
    when(instrumentedType.getInterfaces()).thenReturn(new TypeList.Generic.Explicit(genericInterfaceType, genericInterfaceType));
    MethodDelegationBinder.ParameterBinding<?> binding = DefaultMethod.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(true));
}
 
Example #2
Source File: MethodAttributeAppenderForInstrumentedMethodOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReceiverTypeTypeAnnotationsRuntimeRetention() 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 Baz.Instance()));
    MethodAttributeAppender.ForInstrumentedMethod.INCLUDING_RECEIVER.apply(methodVisitor, methodDescription, annotationValueFilter);
    verify(methodVisitor).visitTypeAnnotation(TypeReference.newTypeReference(TypeReference.METHOD_RECEIVER).getValue(),
            null,
            Type.getDescriptor(AbstractAttributeAppenderTest.Baz.class),
            true);
    verifyNoMoreInteractions(methodVisitor);
    verify(methodDescription).getDeclaredAnnotations();
    verify(methodDescription).getParameters();
    verify(methodDescription).getReturnType();
    verify(methodDescription).getExceptionTypes();
    verify(methodDescription).getTypeVariables();
    verify(methodDescription).getReceiverType();
    verifyNoMoreInteractions(methodDescription);
}
 
Example #3
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 #4
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMethodParameterTypeTypeAnnotationClassFileRetention() 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.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(parameterDescription.getType()).thenReturn(simpleAnnotatedType);
    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());
    when(simpleAnnotatedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new QuxBaz.Instance()));
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verify(methodVisitor).visitTypeAnnotation(TypeReference.newFormalParameterReference(0).getValue(),
            null,
            Type.getDescriptor(QuxBaz.class),
            false);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #5
Source File: TypeAttributeAppenderForInstrumentedTypeTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testTypeVariableTypeAnnotationRuntimeRetention() throws Exception {
    when(instrumentedType.getTypeVariables()).thenReturn(new TypeList.Generic.Explicit(annotatedTypeVariable));
    when(annotatedTypeVariable.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    when(annotatedTypeVariableBound.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    when(instrumentedType.getInterfaces()).thenReturn(new TypeList.Generic.Empty());
    when(instrumentedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    TypeAttributeAppender.ForInstrumentedType.INSTANCE.apply(classVisitor, instrumentedType, annotationValueFilter);
    verify(classVisitor).visitTypeAnnotation(TypeReference.newTypeParameterReference(TypeReference.CLASS_TYPE_PARAMETER, 0).getValue(),
            null,
            Type.getDescriptor(Baz.class),
            true);
    verify(classVisitor).visitTypeAnnotation(TypeReference.newTypeParameterBoundReference(TypeReference.CLASS_TYPE_PARAMETER_BOUND, 0, 0).getValue(),
            null,
            Type.getDescriptor(Baz.class),
            true);
    verifyNoMoreInteractions(classVisitor);
    verify(instrumentedType).getDeclaredAnnotations();
    verify(instrumentedType).getSuperClass();
    verify(instrumentedType).getInterfaces();
    verify(instrumentedType).getTypeVariables();
    verifyNoMoreInteractions(instrumentedType);
}
 
Example #6
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMethodParameterAnnotationRuntimeRetention() 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 Baz.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(Baz.class), true);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #7
Source File: MorphBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultMethodCallExplicitValid() throws Exception {
    when(source.asSignatureToken()).thenReturn(morphToken);
    when(instrumentedType.getInterfaces()).thenReturn(new TypeList.Generic.ForLoadedTypes(Foo.class));
    when(target.getType()).thenReturn(genericMorphType);
    when(morphMethod.getDeclaringType()).thenReturn(morphType);
    when(annotation.defaultMethod()).thenReturn(true);
    doReturn(Foo.class).when(annotation).defaultTarget();
    when(implementationTarget.invokeDefault(morphToken, TypeDescription.ForLoadedType.of(Foo.class)))
            .thenReturn(specialMethodInvocation);
    when(specialMethodInvocation.isValid()).thenReturn(true);
    MethodDelegationBinder.ParameterBinding<?> parameterBinding = new Morph.Binder(morphMethod)
            .bind(annotationDescription, source, target, implementationTarget, assigner, Assigner.Typing.STATIC);
    assertThat(parameterBinding.isValid(), is(true));
    verify(specialMethodInvocation).isValid();
}
 
Example #8
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 #9
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testTypeVariableTypeAnnotations() throws Exception {
    when(annotatedTypeVariable.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new QuxBaz.Instance()));
    when(annotatedTypeVariableBound.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.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(QuxBaz.class),
            false);
    verify(methodVisitor).visitTypeAnnotation(TypeReference.newTypeParameterBoundReference(TypeReference.METHOD_TYPE_PARAMETER_BOUND, 0, 0).getValue(),
            null,
            Type.getDescriptor(QuxBaz.class),
            false);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #10
Source File: MethodOverrideMatcherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    when(declaredTypeMethod.isVirtual()).thenReturn(true);
    when(superTypeMethod.isVirtual()).thenReturn(true);
    when(interfaceTypeMethod.isVirtual()).thenReturn(true);
    when(methodDescription.getDeclaringType()).thenReturn(declaringType);
    when(methodDescription.asSignatureToken()).thenReturn(token);
    when(declaringType.asGenericType()).thenReturn(declaringType);
    when(superType.asGenericType()).thenReturn(superType);
    when(interfaceType.asGenericType()).thenReturn(interfaceType);
    when(declaringType.asErasure()).thenReturn(rawDeclaringType);
    when(superType.asErasure()).thenReturn(rawSuperType);
    when(interfaceType.asErasure()).thenReturn(rawInterfaceType);
    when(declaringType.iterator()).thenReturn(Arrays.<TypeDefinition>asList(declaringType, superType).iterator());
    when(declaringType.getInterfaces()).thenReturn(new TypeList.Generic.Explicit(interfaceType));
    when(superType.getInterfaces()).thenReturn(new TypeList.Generic.Empty());
    when(interfaceType.getInterfaces()).thenReturn(new TypeList.Generic.Empty());
    when(declaringType.getDeclaredMethods()).thenReturn(new MethodList.Explicit<MethodDescription.InGenericShape>(declaredTypeMethod));
    when(superType.getDeclaredMethods()).thenReturn(new MethodList.Explicit<MethodDescription.InGenericShape>(superTypeMethod));
    when(interfaceType.getDeclaredMethods()).thenReturn(new MethodList.Explicit<MethodDescription.InGenericShape>(interfaceTypeMethod));
}
 
Example #11
Source File: MethodAttributeAppenderForInstrumentedMethodOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReceiverTypeTypeAnnotationsIgnored() 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 QuxBaz.Instance()));
    MethodAttributeAppender.ForInstrumentedMethod.EXCLUDING_RECEIVER.apply(methodVisitor, methodDescription, annotationValueFilter);
    verifyZeroInteractions(methodVisitor);
    verify(methodDescription).getDeclaredAnnotations();
    verify(methodDescription).getParameters();
    verify(methodDescription).getReturnType();
    verify(methodDescription).getExceptionTypes();
    verify(methodDescription).getTypeVariables();
    verifyNoMoreInteractions(methodDescription);
}
 
Example #12
Source File: TypeAttributeAppenderForInstrumentedTypeTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuperClassTypeAnnotationClassFileRetention() throws Exception {
    when(instrumentedType.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(instrumentedType.getInterfaces()).thenReturn(new TypeList.Generic.Empty());
    when(simpleAnnotatedType.getDeclaredAnnotations())
            .thenReturn(new AnnotationList.ForLoadedAnnotations(new QuxBaz.Instance()));
    when(instrumentedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(instrumentedType.getSuperClass()).thenReturn(simpleAnnotatedType);
    TypeAttributeAppender.ForInstrumentedType.INSTANCE.apply(classVisitor, instrumentedType, annotationValueFilter);
    verify(classVisitor).visitTypeAnnotation(TypeReference.newSuperTypeReference(-1).getValue(), null, Type.getDescriptor(QuxBaz.class), false);
    verifyNoMoreInteractions(classVisitor);
    verify(instrumentedType).getDeclaredAnnotations();
    verify(instrumentedType).getSuperClass();
    verify(instrumentedType).getInterfaces();
    verify(instrumentedType).getTypeVariables();
    verifyNoMoreInteractions(instrumentedType);
}
 
Example #13
Source File: JavaConstant.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a method handles representation of a loaded method handle which is analyzed using the given lookup context.
 * A method handle can only be analyzed on virtual machines that support the corresponding API (Java 7+). For virtual machines before Java 8+,
 * a method handle instance can only be analyzed by taking advantage of private APIs what might require a access context.
 *
 * @param methodHandle The loaded method handle to represent.
 * @param lookup       The lookup object to use for analyzing the method handle.
 * @return A representation of the loaded method handle
 */
public static MethodHandle ofLoaded(Object methodHandle, Object lookup) {
    if (!JavaType.METHOD_HANDLE.isInstance(methodHandle)) {
        throw new IllegalArgumentException("Expected method handle object: " + methodHandle);
    } else if (!JavaType.METHOD_HANDLES_LOOKUP.isInstance(lookup)) {
        throw new IllegalArgumentException("Expected method handle lookup object: " + lookup);
    }
    Dispatcher dispatcher = DISPATCHER.initialize();
    Object methodHandleInfo = dispatcher.reveal(lookup, methodHandle);
    Object methodType = dispatcher.getMethodType(methodHandleInfo);
    return new MethodHandle(HandleType.of(dispatcher.getReferenceKind(methodHandleInfo)),
            TypeDescription.ForLoadedType.of(dispatcher.getDeclaringClass(methodHandleInfo)),
            dispatcher.getName(methodHandleInfo),
            TypeDescription.ForLoadedType.of(dispatcher.returnType(methodType)),
            new TypeList.ForLoadedTypes(dispatcher.parameterArray(methodType)));
}
 
Example #14
Source File: TypeAttributeAppenderForInstrumentedTypeDifferentiatingTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testTypeVariableTypeAnnotationRuntimeRetention() throws Exception {
    when(instrumentedType.getTypeVariables()).thenReturn(new TypeList.Generic.Explicit(pseudoType, annotatedTypeVariable));
    when(annotatedTypeVariable.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    when(annotatedTypeVariableBound.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    when(instrumentedType.getInterfaces()).thenReturn(new TypeList.Generic.Empty());
    when(instrumentedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    new TypeAttributeAppender.ForInstrumentedType.Differentiating(0, 1, 0).apply(classVisitor, instrumentedType, annotationValueFilter);
    verify(classVisitor).visitTypeAnnotation(TypeReference.newTypeParameterReference(TypeReference.CLASS_TYPE_PARAMETER, 1).getValue(),
            null,
            Type.getDescriptor(Baz.class),
            true);
    verify(classVisitor).visitTypeAnnotation(TypeReference.newTypeParameterBoundReference(TypeReference.CLASS_TYPE_PARAMETER_BOUND, 1, 0).getValue(),
            null,
            Type.getDescriptor(Baz.class),
            true);
    verifyNoMoreInteractions(classVisitor);
    verify(instrumentedType).getDeclaredAnnotations();
    verify(instrumentedType).getInterfaces();
    verify(instrumentedType).getTypeVariables();
    verifyNoMoreInteractions(instrumentedType);
}
 
Example #15
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 #16
Source File: DefaultMethodBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testBindNoInterface() throws Exception {
    when(targetType.isAssignableFrom(Method.class)).thenReturn(true);
    when(source.isMethod()).thenReturn(true);
    when(instrumentedType.getInterfaces()).thenReturn(new TypeList.Generic.Empty());
    when(implementationTarget.invokeDefault(token)).thenReturn(specialMethodInvocation);
    when(annotation.targetType()).thenReturn((Class) void.class);
    MethodDelegationBinder.ParameterBinding<?> binding = DefaultMethod.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(false));
}
 
Example #17
Source File: MethodDescription.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if this method is a bootstrap method while expecting the supplied type as a type representation.
 *
 * @param selfType The type of the bootstrap method's type representation.
 * @return {@code true} if this method is a bootstrap method assuming the supplied type representation.
 */
private boolean isBootstrap(TypeDescription selfType) {
    TypeList parameterTypes = getParameters().asTypeList().asErasures();
    switch (parameterTypes.size()) {
        case 0:
            return false;
        case 1:
            return parameterTypes.getOnly().represents(Object[].class);
        case 2:
            return JavaType.METHOD_HANDLES_LOOKUP.getTypeStub().isAssignableTo(parameterTypes.get(0))
                    && parameterTypes.get(1).represents(Object[].class);
        case 3:
            return JavaType.METHOD_HANDLES_LOOKUP.getTypeStub().isAssignableTo(parameterTypes.get(0))
                    && (parameterTypes.get(1).represents(Object.class) || parameterTypes.get(1).represents(String.class))
                    && (parameterTypes.get(2).represents(Object[].class) || parameterTypes.get(2).isAssignableFrom(selfType));
        default:
            return JavaType.METHOD_HANDLES_LOOKUP.getTypeStub().isAssignableTo(parameterTypes.get(0))
                    && (parameterTypes.get(1).represents(Object.class) || parameterTypes.get(1).represents(String.class))
                    && parameterTypes.get(2).isAssignableFrom(selfType);
    }
}
 
Example #18
Source File: HierarchyMatch.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isMatch(TypeDescription typeDescription) {
    List<String> parentTypes = new ArrayList<String>(Arrays.asList(this.parentTypes));

    TypeList.Generic implInterfaces = typeDescription.getInterfaces();
    for (TypeDescription.Generic implInterface : implInterfaces) {
        matchHierarchyClass(implInterface, parentTypes);
    }

    if (typeDescription.getSuperClass() != null) {
        matchHierarchyClass(typeDescription.getSuperClass(), parentTypes);
    }

    return parentTypes.size() == 0;

}
 
Example #19
Source File: AbstractAttributeAppenderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    when(annotatedTypeVariable.accept(any(TypeDescription.Generic.Visitor.class))).thenCallRealMethod();
    when(annotatedTypeVariable.getUpperBounds()).thenReturn(new TypeList.Generic.Explicit(annotatedTypeVariableBound));
    when(annotatedTypeVariable.asGenericType()).thenReturn(annotatedTypeVariable);
    when(annotatedTypeVariableBound.accept(any(TypeDescription.Generic.Visitor.class))).thenCallRealMethod();
    when(annotatedTypeVariableBound.getSort()).thenReturn(TypeDefinition.Sort.VARIABLE);
    when(annotatedTypeVariableBound.asGenericType()).thenReturn(annotatedTypeVariableBound);
    when(simpleAnnotatedType.accept(any(TypeDescription.Generic.Visitor.class))).thenCallRealMethod();
    when(simpleAnnotatedType.asGenericType()).thenReturn(simpleAnnotatedType);
    when(simpleAnnotatedType.asErasure()).thenReturn(typeErasure);
    when(annotatedTypeVariable.asErasure()).thenReturn(typeErasure);
    when(annotatedTypeVariableBound.asErasure()).thenReturn(typeErasure);
}
 
Example #20
Source File: RebaseImplementationTarget.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new rebased method invocation.
 *
 * @param methodDescription   The method to invoke via a special method invocation.
 * @param instrumentedType    The instrumented type on which the method should be invoked on.
 * @param stackManipulation   The stack manipulation to execute in order to invoke the rebased method.
 * @param prependedParameters Any additional arguments that are to be provided to the rebased method.
 */
protected RebasedMethodInvocation(MethodDescription methodDescription,
                                  TypeDescription instrumentedType,
                                  StackManipulation stackManipulation,
                                  TypeList prependedParameters) {
    this.methodDescription = methodDescription;
    this.instrumentedType = instrumentedType;
    this.stackManipulation = stackManipulation;
    this.prependedParameters = prependedParameters;
}
 
Example #21
Source File: AnnotationAppenderForTypeAnnotationsTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testParameterized() throws Exception {
    when(erasure.getInnerClassCount()).thenReturn(1);
    when(typeDescription.getTypeArguments()).thenReturn(new TypeList.Generic.Explicit(second));
    when(typeDescription.getOwnerType()).thenReturn(third);
    assertThat(visitor.onParameterizedType(typeDescription), is(result));
    verify(annotationAppender).append(annotationDescription, annotationValueFilter, BAR, FOO + ".");
    verifyNoMoreInteractions(annotationAppender);
    verify(second).accept(matchesPrototype(new AnnotationAppender.ForTypeAnnotations(result, annotationValueFilter, BAR, FOO + ".0;")));
    verify(third).accept(matchesPrototype(new AnnotationAppender.ForTypeAnnotations(result, annotationValueFilter, BAR, FOO + "")));
}
 
Example #22
Source File: Transformer.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TypeDescription.Generic onTypeVariable(TypeDescription.Generic typeVariable) {
    TypeList.Generic candidates = getTypeVariables().filter(named(typeVariable.getSymbol()));
    TypeDescription.Generic attached = candidates.isEmpty()
            ? instrumentedType.findVariable(typeVariable.getSymbol())
            : candidates.getOnly();
    if (attached == null) {
        throw new IllegalArgumentException("Cannot attach undefined variable: " + typeVariable);
    } else {
        return new TypeDescription.Generic.OfTypeVariable.WithAnnotationOverlay(attached, typeVariable);
    }
}
 
Example #23
Source File: JavaConstantMethodHandleTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMethodHandleOfStaticMethod() throws Exception {
    JavaConstant.MethodHandle methodHandle = JavaConstant.MethodHandle.of(Foo.class.getDeclaredMethod(QUX, Void.class));
    assertThat(methodHandle.getHandleType(), is(JavaConstant.MethodHandle.HandleType.INVOKE_STATIC));
    assertThat(methodHandle.getName(), is(QUX));
    assertThat(methodHandle.getOwnerType(), is((TypeDescription) TypeDescription.ForLoadedType.of(Foo.class)));
    assertThat(methodHandle.getReturnType(), is(TypeDescription.VOID));
    assertThat(methodHandle.getParameterTypes(), is((List<TypeDescription>) new TypeList.ForLoadedTypes(Void.class)));
}
 
Example #24
Source File: JavaConstantMethodTypeTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMethodTypeOfMethod() throws Exception {
    JavaConstant.MethodType methodType = JavaConstant.MethodType.of(Foo.class.getDeclaredMethod(BAR, Void.class));
    assertThat(methodType.getReturnType(), is(TypeDescription.VOID));
    assertThat(methodType.getParameterTypes(), is((List<TypeDescription>) new TypeList.ForLoadedTypes(Void.class)));
    assertThat(methodType.getDescriptor(), is(new MethodDescription.ForLoadedMethod(Foo.class.getDeclaredMethod(BAR, Void.class)).getDescriptor()));
}
 
Example #25
Source File: TypeAttributeAppenderForInstrumentedTypeTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testInterfaceTypeAnnotations() throws Exception {
    when(instrumentedType.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(instrumentedType.getInterfaces()).thenReturn(new TypeList.Generic.Explicit(simpleAnnotatedType));
    when(simpleAnnotatedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new QuxBaz.Instance()));
    when(instrumentedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    TypeAttributeAppender.ForInstrumentedType.INSTANCE.apply(classVisitor, instrumentedType, annotationValueFilter);
    verify(classVisitor).visitTypeAnnotation(TypeReference.newSuperTypeReference(0).getValue(), null, Type.getDescriptor(QuxBaz.class), false);
    verifyNoMoreInteractions(classVisitor);
    verify(instrumentedType).getDeclaredAnnotations();
    verify(instrumentedType).getSuperClass();
    verify(instrumentedType).getInterfaces();
    verify(instrumentedType).getTypeVariables();
    verifyNoMoreInteractions(instrumentedType);
}
 
Example #26
Source File: AbstractMethodDescriptionTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericTypesOfMethodWithoutException() throws Exception {
    assertThat(describe(genericMethodWithRawException).getReturnType(),
            is(TypeDefinition.Sort.describe(genericMethodWithRawException.getGenericReturnType())));
    assertThat(describe(genericMethodWithRawException).getParameters().asTypeList(),
            is((TypeList.Generic) new TypeList.Generic.ForLoadedTypes(genericMethodWithRawException.getGenericParameterTypes())));
    assertThat(describe(genericMethodWithRawException).getExceptionTypes(),
            is((TypeList.Generic) new TypeList.Generic.ForLoadedTypes(genericMethodWithRawException.getGenericExceptionTypes())));
}
 
Example #27
Source File: MethodDescription.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TypeList.Generic getTypeVariables() {
    if (TypeDescription.AbstractBase.RAW_TYPES) {
        return new TypeList.Generic.Empty();
    }
    return TypeList.Generic.ForLoadedTypes.OfTypeVariables.of(method);
}
 
Example #28
Source File: MethodDescription.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TypeList.Generic getExceptionTypes() {
    if (TypeDescription.AbstractBase.RAW_TYPES) {
        return new TypeList.Generic.ForLoadedTypes(method.getExceptionTypes());
    }
    return new TypeList.Generic.OfMethodExceptionTypes(method);
}
 
Example #29
Source File: MethodInvocationOtherTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testIllegal() throws Exception {
    assertThat(MethodInvocation.IllegalInvocation.INSTANCE.isValid(), is(false));
    assertThat(MethodInvocation.IllegalInvocation.INSTANCE.special(mock(TypeDescription.class)),
            is((StackManipulation) StackManipulation.Illegal.INSTANCE));
    assertThat(MethodInvocation.IllegalInvocation.INSTANCE.virtual(mock(TypeDescription.class)),
            is((StackManipulation) StackManipulation.Illegal.INSTANCE));
    assertThat(MethodInvocation.IllegalInvocation.INSTANCE.dynamic(FOO, mock(TypeDescription.class), mock(TypeList.class), mock(List.class)),
            is((StackManipulation) StackManipulation.Illegal.INSTANCE));
    assertThat(MethodInvocation.IllegalInvocation.INSTANCE.onHandle(null),
            is((StackManipulation) StackManipulation.Illegal.INSTANCE));
    MethodInvocation.IllegalInvocation.INSTANCE.apply(mock(MethodVisitor.class), mock(Implementation.Context.class));
}
 
Example #30
Source File: RebaseImplementationTargetTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonSpecializableRebaseMethodIsNotInvokable() throws Exception {
    when(invokableMethod.getDeclaringType()).thenReturn(instrumentedType);
    when(resolution.isRebased()).thenReturn(true);
    when(resolution.getResolvedMethod()).thenReturn(rebasedMethod);
    when(resolution.getPrependedParameters()).thenReturn(new TypeList.Empty());
    when(rebasedMethod.isSpecializableFor(instrumentedType)).thenReturn(false);
    Implementation.SpecialMethodInvocation specialMethodInvocation = makeImplementationTarget().invokeSuper(rebasedSignatureToken);
    assertThat(specialMethodInvocation.isValid(), is(false));
}