Java源码示例:net.bytebuddy.description.annotation.AnnotationDescription

示例1
@Test
@JavaVersionRule.Enforce(8)
@SuppressWarnings("unchecked")
public void testAnnotationTypeOnInterfaceType() throws Exception {
    Class<? extends Annotation> typeAnnotationType = (Class<? extends Annotation>) Class.forName(TYPE_VARIABLE_NAME);
    MethodDescription.InDefinedShape value = TypeDescription.ForLoadedType.of(typeAnnotationType).getDeclaredMethods().filter(named(VALUE)).getOnly();
    Class<?> type = create(Class.forName(SIMPLE_TYPE_ANNOTATED))
            .merge(TypeManifestation.ABSTRACT)
            .implement(TypeDescription.Generic.Builder.rawType(Callable.class)
                    .build(AnnotationDescription.Builder.ofType(typeAnnotationType).define(VALUE, QUX * 3).build()))
            .make()
            .load(typeAnnotationType.getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST)
            .getLoaded();
    assertThat(type.getInterfaces().length, is(2));
    assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveInterfaceType(type, 0).asList().size(), is(1));
    assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveInterfaceType(type, 0).asList().ofType(typeAnnotationType)
            .getValue(value).resolve(Integer.class), is(QUX * 2));
    assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveInterfaceType(type, 1).asList().size(), is(1));
    assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveInterfaceType(type, 1).asList().ofType(typeAnnotationType)
            .getValue(value).resolve(Integer.class), is(QUX * 3));
}
 
示例2
@Test
@JavaVersionRule.Enforce(8)
@SuppressWarnings("unchecked")
public void testAnnotationTypeOnMethodParameterType() throws Exception {
    Class<? extends Annotation> typeAnnotationType = (Class<? extends Annotation>) Class.forName(TYPE_VARIABLE_NAME);
    MethodDescription.InDefinedShape value = TypeDescription.ForLoadedType.of(typeAnnotationType).getDeclaredMethods().filter(named(VALUE)).getOnly();
    Method method = createPlain()
            .merge(TypeManifestation.ABSTRACT)
            .defineMethod(FOO, void.class).withParameters(TypeDescription.Generic.Builder.rawType(Object.class)
                    .build(AnnotationDescription.Builder.ofType(typeAnnotationType).define(VALUE, INTEGER_VALUE).build()))
            .withoutCode()
            .make()
            .load(typeAnnotationType.getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST)
            .getLoaded()
            .getDeclaredMethod(FOO, Object.class);
    assertThat(method.getParameterTypes().length, is(1));
    assertThat(method.getParameterTypes()[0], is((Object) Object.class));
    assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveParameterType(method, 0).asList().size(), is(1));
    assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveParameterType(method, 0).asList().ofType(typeAnnotationType)
            .getValue(value).resolve(Integer.class), is(INTEGER_VALUE));
}
 
示例3
@Test(expected = IllegalStateException.class)
public void testMethodDuplicateAnnotation() 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(),
                    Arrays.asList(
                            AnnotationDescription.Builder.ofType(SampleAnnotation.class).build(),
                            AnnotationDescription.Builder.ofType(SampleAnnotation.class).build()
                    ), AnnotationValue.UNDEFINED,
                    TypeDescription.Generic.UNDEFINED))
            .validated();
}
 
示例4
@SuppressWarnings({"unchecked", "rawtypes"})
public static Annotation[] getAnnotations(Collection<AnnotationDescription> annotations)
{
    Collection<Annotation> col = new ArrayList<>(annotations.size());
    for (AnnotationDescription annotation : annotations)
    {
        TypeDescription annotationType = annotation.getAnnotationType();
        try
        {
            Class<?> forName = Class.forName(annotationType.getActualName());
            if (! forName.isAnnotation())
            {
                continue;
            }
            col.add(annotation.prepare((Class) forName).load());
        }
        catch (ClassNotFoundException ignored)
        {
        }
    }
    return col.toArray(new Annotation[col.size()]);
}
 
示例5
@Override
protected boolean isInjectElement(AnnotatedCodeElement element)
{
    for (AnnotationDescription annotation : AsmUtils.getAnnotationList(element))
    {
        TypeDescription annotationType = annotation.getAnnotationType();
        if (annotationType.equals(INJECT))
        {
            return true;
        }
        if (annotationType.getInheritedAnnotations().isAnnotationPresent(SHORTCUT_INJECT))
        {
            return true;
        }
    }
    return false;
}
 
示例6
@Override
public boolean isMatch(TypeDescription typeDescription) {
    for (MethodDescription.InDefinedShape methodDescription : typeDescription.getDeclaredMethods()) {
        List<String> annotationList = new ArrayList<String>(Arrays.asList(annotations));

        AnnotationList declaredAnnotations = methodDescription.getDeclaredAnnotations();
        for (AnnotationDescription annotation : declaredAnnotations) {
            annotationList.remove(annotation.getAnnotationType().getActualName());
        }
        if (annotationList.isEmpty()) {
            return true;
        }
    }

    return false;
}
 
示例7
/**
 * {@inheritDoc}
 */
public List<MethodDescription.Token> extractConstructors(TypeDescription instrumentedType) {
    List<ParameterDescription.Token> tokens = new ArrayList<ParameterDescription.Token>(instrumentedType.getRecordComponents().size());
    for (RecordComponentDescription.InDefinedShape recordComponent : instrumentedType.getRecordComponents()) {
        tokens.add(new ParameterDescription.Token(recordComponent.getType(),
                recordComponent.getDeclaredAnnotations().filter(targetsElement(ElementType.CONSTRUCTOR))));
    }
    return Collections.singletonList(new MethodDescription.Token(MethodDescription.CONSTRUCTOR_INTERNAL_NAME,
            Opcodes.ACC_PUBLIC,
            Collections.<TypeVariableToken>emptyList(),
            TypeDescription.Generic.VOID,
            tokens,
            Collections.<TypeDescription.Generic>emptyList(),
            Collections.<AnnotationDescription>emptyList(),
            AnnotationValue.UNDEFINED,
            TypeDescription.Generic.UNDEFINED));
}
 
示例8
@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);
}
 
示例9
@Test
@SuppressWarnings("unchecked")
public void testImitateSuperClassOpeningStrategy() throws Exception {
    assertThat(ConstructorStrategy.Default.IMITATE_SUPER_CLASS_OPENING.extractConstructors(instrumentedType), is(Collections.singletonList(new MethodDescription.Token(FOO,
            Opcodes.ACC_PUBLIC,
            Collections.<TypeVariableToken>emptyList(),
            typeDescription,
            Collections.<ParameterDescription.Token>emptyList(),
            Collections.<TypeDescription.Generic>emptyList(),
            Collections.<AnnotationDescription>emptyList(),
            defaultValue,
            TypeDescription.Generic.UNDEFINED))));
    assertThat(ConstructorStrategy.Default.IMITATE_SUPER_CLASS_OPENING.inject(instrumentedType, methodRegistry), is(methodRegistry));
    verify(methodRegistry).append(any(LatentMatcher.class),
            any(MethodRegistry.Handler.class),
            eq(MethodAttributeAppender.NoOp.INSTANCE),
            eq(Transformer.NoOp.<MethodDescription>make()));
    verifyNoMoreInteractions(methodRegistry);
    verify(instrumentedType, atLeastOnce()).getSuperClass();
    verifyNoMoreInteractions(instrumentedType);
}
 
示例10
@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();
}
 
示例11
/**
 * {@inheritDoc}
 */
public WithFlexibleName withAnnotations(List<? extends AnnotationDescription> annotationDescriptions) {
    return new Default(name,
            modifiers,
            superClass,
            typeVariables,
            interfaceTypes,
            fieldTokens,
            methodTokens,
            recordComponentTokens,
            CompoundList.of(this.annotationDescriptions, annotationDescriptions),
            typeInitializer,
            loadedTypeInitializer,
            declaringType,
            enclosingMethod,
            enclosingType,
            declaredTypes,
            anonymousClass,
            localClass,
            record,
            nestHost,
            nestMembers);
}
 
示例12
@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);
}
 
示例13
/**
 * Creates a new latent method description. All provided types are attached to this instance before they are returned.
 *
 * @param declaringType       The type that is declaring this method.
 * @param internalName        The internal name of this method.
 * @param modifiers           The modifiers of this method.
 * @param typeVariables       The type variables of the described method.
 * @param returnType          The return type of this method.
 * @param parameterTokens     The parameter tokens describing this method.
 * @param exceptionTypes      This method's exception types.
 * @param declaredAnnotations The annotations of this method.
 * @param defaultValue        The default value of this method or {@code null} if no default annotation value is defined.
 * @param receiverType        The receiver type of this method or {@code null} if the receiver type is defined implicitly.
 */
public Latent(TypeDescription declaringType,
              String internalName,
              int modifiers,
              List<? extends TypeVariableToken> typeVariables,
              TypeDescription.Generic returnType,
              List<? extends ParameterDescription.Token> parameterTokens,
              List<? extends TypeDescription.Generic> exceptionTypes,
              List<? extends AnnotationDescription> declaredAnnotations,
              AnnotationValue<?, ?> defaultValue,
              TypeDescription.Generic receiverType) {
    this.declaringType = declaringType;
    this.internalName = internalName;
    this.modifiers = modifiers;
    this.typeVariables = typeVariables;
    this.returnType = returnType;
    this.parameterTokens = parameterTokens;
    this.exceptionTypes = exceptionTypes;
    this.declaredAnnotations = declaredAnnotations;
    this.defaultValue = defaultValue;
    this.receiverType = receiverType;
}
 
示例14
@Test
@JavaVersionRule.Enforce(8)
@SuppressWarnings("unchecked")
public void testAnnotationTypeOnTypeVariableType() throws Exception {
    Class<? extends Annotation> typeAnnotationType = (Class<? extends Annotation>) Class.forName(TYPE_VARIABLE_NAME);
    MethodDescription.InDefinedShape value = TypeDescription.ForLoadedType.of(typeAnnotationType).getDeclaredMethods().filter(named(VALUE)).getOnly();
    Class<?> type = create(Class.forName(SIMPLE_TYPE_ANNOTATED))
            .merge(TypeManifestation.ABSTRACT)
            .typeVariable(BAR, TypeDescription.Generic.Builder.rawType(Callable.class)
                    .build(AnnotationDescription.Builder.ofType(typeAnnotationType).define(VALUE, QUX * 4).build()))
            .annotateTypeVariable(AnnotationDescription.Builder.ofType(typeAnnotationType).define(VALUE, QUX * 3).build())
            .make()
            .load(typeAnnotationType.getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST)
            .getLoaded();
    assertThat(type.getTypeParameters().length, is(2));
    assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveTypeVariable(type.getTypeParameters()[0]).asList().size(), is(1));
    assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveTypeVariable(type.getTypeParameters()[0]).asList().ofType(typeAnnotationType)
            .getValue(value).resolve(Integer.class), is(QUX));
    assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveTypeVariable(type.getTypeParameters()[1]).asList().size(), is(1));
    assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveTypeVariable(type.getTypeParameters()[1]).asList().ofType(typeAnnotationType)
            .getValue(value).resolve(Integer.class), is(QUX * 3));
    assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveTypeVariable(type.getTypeParameters()[1]).ofTypeVariableBoundType(0)
            .asList().size(), is(1));
    assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveTypeVariable(type.getTypeParameters()[1]).ofTypeVariableBoundType(0)
            .asList().ofType(typeAnnotationType).getValue(value).resolve(Integer.class), is(QUX * 4));
}
 
示例15
@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);
}
 
示例16
/**
 * {@inheritDoc}
 */
public AnnotationAppender append(AnnotationDescription annotationDescription, AnnotationValueFilter annotationValueFilter, int typeReference, String typePath) {
    switch (annotationDescription.getRetention()) {
        case RUNTIME:
            doAppend(annotationDescription, true, annotationValueFilter, typeReference, typePath);
            break;
        case CLASS:
            doAppend(annotationDescription, false, annotationValueFilter, typeReference, typePath);
            break;
        case SOURCE:
            break;
        default:
            throw new IllegalStateException("Unexpected retention policy: " + annotationDescription.getRetention());
    }
    return this;
}
 
示例17
@Test
public void testPackageRebasement() throws Exception {
    Class<?> packageType = new ByteBuddy()
            .rebase(Sample.class.getPackage(), ClassFileLocator.ForClassLoader.of(getClass().getClassLoader()))
            .annotateType(AnnotationDescription.Builder.ofType(Baz.class).build())
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST)
            .getLoaded();
    assertThat(packageType.getSimpleName(), CoreMatchers.is(PackageDescription.PACKAGE_CLASS_NAME));
    assertThat(packageType.getName(), CoreMatchers.is(Sample.class.getPackage().getName() + "." + PackageDescription.PACKAGE_CLASS_NAME));
    assertThat(packageType.getModifiers(), CoreMatchers.is(PackageDescription.PACKAGE_MODIFIERS));
    assertThat(packageType.getDeclaredFields().length, CoreMatchers.is(0));
    assertThat(packageType.getDeclaredMethods().length, CoreMatchers.is(0));
    assertThat(packageType.getDeclaredAnnotations().length, CoreMatchers.is(2));
    assertThat(packageType.getAnnotation(PackageAnnotation.class), notNullValue(PackageAnnotation.class));
    assertThat(packageType.getAnnotation(Baz.class), notNullValue(Baz.class));
}
 
示例18
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    when(sourceDeclaringType.asErasure()).thenReturn(sourceDeclaringType);
    when(targetDeclaringType.asErasure()).thenReturn(targetDeclaringType);
    when(source.getDeclaringType()).thenReturn(sourceDeclaringType);
    annotation = mock(annotationType);
    doReturn(annotationType).when(annotation).annotationType();
    annotationDescription = AnnotationDescription.ForLoadedAnnotation.of(annotation);
    when(assigner.assign(any(TypeDescription.Generic.class), any(TypeDescription.Generic.class), any(Assigner.Typing.class))).thenReturn(stackManipulation);
    when(implementationTarget.getInstrumentedType()).thenReturn(instrumentedType);
    when(implementationTarget.getOriginType()).thenReturn(instrumentedType);
    when(instrumentedType.asErasure()).thenReturn(instrumentedType);
    when(instrumentedType.iterator()).then(new Answer<Iterator<TypeDefinition>>() {
        public Iterator<TypeDefinition> answer(InvocationOnMock invocationOnMock) throws Throwable {
            return Collections.<TypeDefinition>singleton(instrumentedType).iterator();
        }
    });
    when(source.asTypeToken()).thenReturn(sourceTypeToken);
}
 
示例19
private Class<?> makeTypeWithSuperClassAnnotation(Annotation annotation) throws Exception {
    when(valueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    ClassWriter classWriter = new ClassWriter(AsmVisitorWrapper.NO_FLAGS);
    classWriter.visit(ClassFileVersion.ofThisVm().getMinorMajorVersion(),
            Opcodes.ACC_PUBLIC,
            BAR.replace('.', '/'),
            null,
            Type.getInternalName(Object.class),
            null);
    AnnotationVisitor annotationVisitor = classWriter.visitTypeAnnotation(TypeReference.newSuperTypeReference(-1).getValue(),
            null,
            Type.getDescriptor(annotation.annotationType()),
            true);
    when(target.visit(any(String.class), anyBoolean())).thenReturn(annotationVisitor);
    AnnotationDescription annotationDescription = AnnotationDescription.ForLoadedAnnotation.of(annotation);
    annotationAppender.append(annotationDescription, valueFilter);
    classWriter.visitEnd();
    Class<?> bar = new ByteArrayClassLoader(getClass().getClassLoader(), Collections.singletonMap(BAR, classWriter.toByteArray())).loadClass(BAR);
    assertThat(bar.getName(), is(BAR));
    assertThat(bar.getSuperclass(), CoreMatchers.<Class<?>>is(Object.class));
    return bar;
}
 
示例20
/**
 * {@inheritDoc}
 */
public void apply(ClassVisitor classVisitor, TypeDescription instrumentedType, AnnotationValueFilter annotationValueFilter) {
    AnnotationAppender annotationAppender = new AnnotationAppender.Default(new AnnotationAppender.Target.OnType(classVisitor));
    AnnotationAppender.ForTypeAnnotations.ofTypeVariable(annotationAppender,
            annotationValueFilter,
            AnnotationAppender.ForTypeAnnotations.VARIABLE_ON_TYPE,
            typeVariableIndex,
            instrumentedType.getTypeVariables());
    TypeList.Generic interfaceTypes = instrumentedType.getInterfaces();
    int interfaceTypeIndex = this.interfaceTypeIndex;
    for (TypeDescription.Generic interfaceType : interfaceTypes.subList(this.interfaceTypeIndex, interfaceTypes.size())) {
        annotationAppender = interfaceType.accept(AnnotationAppender.ForTypeAnnotations.ofInterfaceType(annotationAppender,
                annotationValueFilter,
                interfaceTypeIndex++));
    }
    AnnotationList declaredAnnotations = instrumentedType.getDeclaredAnnotations();
    for (AnnotationDescription annotationDescription : declaredAnnotations.subList(annotationIndex, declaredAnnotations.size())) {
        annotationAppender = annotationAppender.append(annotationDescription, annotationValueFilter);
    }
}
 
示例21
@Test
@SuppressWarnings("unchecked")
public void testMethodExceptionTypeTypeAnnotationRuntimeRetention() 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 Baz.Instance()));
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verify(methodVisitor).visitTypeAnnotation(TypeReference.newExceptionReference(0).getValue(),
            null,
            Type.getDescriptor(Baz.class),
            true);
    verifyNoMoreInteractions(methodVisitor);
}
 
示例22
@Test
public void testAnnotationComparatorEqualsLeftBiggerAnnotations() {
    Comparator<FieldDescription.InDefinedShape> comparator = HashCodeAndEqualsPlugin.AnnotationOrderComparator.INSTANCE;
    FieldDescription.InDefinedShape left = mock(FieldDescription.InDefinedShape.class), right = mock(FieldDescription.InDefinedShape.class);
    when(left.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(AnnotationDescription.Builder.ofType(HashCodeAndEqualsPlugin.Sorted.class)
            .define("value", 42)
            .build()));
    when(right.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(AnnotationDescription.Builder.ofType(HashCodeAndEqualsPlugin.Sorted.class)
            .define("value", 0)
            .build()));
    assertThat(comparator.compare(left, right), is(-1));
}
 
示例23
@Override
public Advice.OffsetMapping make(ParameterDescription.InDefinedShape target, AnnotationDescription.Loadable<JaxRsPath> annotation, AdviceType adviceType) {
    return new Advice.OffsetMapping() {
        @Override
        public Target resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner, Advice.ArgumentHandler argumentHandler, Sort sort) {
            Object value = null;
            if (useAnnotationValueForTransactionName) {
                value = getTransactionAnnotationValueFromAnnotations(instrumentedMethod, instrumentedType);
            }
            return Target.ForStackManipulation.of(value);
        }
    };
}
 
示例24
@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();
}
 
示例25
private void assertAnnotations(Class<?> type) {
    assertThat(describe(type).getDeclaredAnnotations(),
            hasItems(new AnnotationList.ForLoadedAnnotations(type.getDeclaredAnnotations())
                    .toArray(new AnnotationDescription[0])));
    assertThat(describe(type).getDeclaredAnnotations().size(), is(type.getDeclaredAnnotations().length));
    assertThat(describe(type).getInheritedAnnotations(),
            hasItems(new AnnotationList.ForLoadedAnnotations(type.getAnnotations())
                    .toArray(new AnnotationDescription[0])));
    assertThat(describe(type).getInheritedAnnotations().size(), is(type.getAnnotations().length));
}
 
示例26
@Test(expected = IllegalStateException.class)
public void testMethodParameterIllegalType() throws Exception {
    makePlainInstrumentedType()
            .withMethod(new MethodDescription.Token(FOO,
                    ModifierContributor.EMPTY_MASK,
                    Collections.<TypeVariableToken>emptyList(),
                    TypeDescription.Generic.OBJECT,
                    Collections.singletonList(new ParameterDescription.Token(TypeDescription.Generic.VOID)),
                    Collections.<TypeDescription.Generic>emptyList(),
                    Collections.<AnnotationDescription>emptyList(),
                    AnnotationValue.UNDEFINED,
                    TypeDescription.Generic.UNDEFINED))
            .validated();
}
 
示例27
@Override
public boolean isMatch(TypeDescription typeDescription) {
    List<String> annotationList = new ArrayList<String>(Arrays.asList(annotations));
    AnnotationList declaredAnnotations = typeDescription.getDeclaredAnnotations();
    for (AnnotationDescription annotation : declaredAnnotations) {
        annotationList.remove(annotation.getAnnotationType().getActualName());
    }
    return annotationList.isEmpty();
}
 
示例28
/**
 * Creates a new bound handler.
 *
 * @param target          The target parameter being handled.
 * @param parameterBinder The parameter binder that is actually responsible for binding the parameter.
 * @param annotation      The annotation value that lead to the binding of this handler.
 * @param typing          The typing to apply.
 */
protected Bound(ParameterDescription target,
                ParameterBinder<T> parameterBinder,
                AnnotationDescription.Loadable<T> annotation,
                Assigner.Typing typing) {
    this.target = target;
    this.parameterBinder = parameterBinder;
    this.annotation = annotation;
    this.typing = typing;
}
 
示例29
@Test(expected = IllegalStateException.class)
public void testAnnotationOnMethodPreJava5TypeAssertion() throws Exception {
    new ByteBuddy(ClassFileVersion.JAVA_V4)
            .subclass(Object.class)
            .defineMethod(FOO, void.class)
            .intercept(StubMethod.INSTANCE)
            .annotateMethod(AnnotationDescription.Builder.ofType(Foo.class).build())
            .make();
}
 
示例30
@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);
}