Java Code Examples for net.bytebuddy.description.method.MethodDescription#InDefinedShape

The following examples show how to use net.bytebuddy.description.method.MethodDescription#InDefinedShape . 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: AbstractDynamicTypeBuilderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@JavaVersionRule.Enforce(8)
@SuppressWarnings("unchecked")
public void testAnnotationTypeOnParameterizedType() 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();
    Field field = createPlain()
            .defineField(FOO, TypeDescription.Generic.Builder.parameterizedType(TypeDescription.ForLoadedType.of(Collection.class),
                    TypeDescription.Generic.Builder.unboundWildcard(AnnotationDescription.Builder.ofType(typeAnnotationType)
                            .define(VALUE, INTEGER_VALUE).build()))
                    .build())
            .make()
            .load(typeAnnotationType.getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST)
            .getLoaded()
            .getDeclaredField(FOO);
    assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveFieldType(field).ofTypeArgument(0).asList().size(), is(1));
    assertThat(TypeDescription.Generic.AnnotationReader.DISPATCHER.resolveFieldType(field).ofTypeArgument(0).asList()
            .ofType(typeAnnotationType).getValue(value).resolve(Integer.class), is(INTEGER_VALUE));
}
 
Example 2
Source File: AnnotationDescription.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Override
public boolean equals(Object other) {
    if (this == other) {
        return true;
    } else if (!(other instanceof AnnotationDescription)) {
        return false;
    }
    AnnotationDescription annotationDescription = ((AnnotationDescription) other);
    TypeDescription annotationType = getAnnotationType();
    if (!annotationDescription.getAnnotationType().equals(annotationType)) {
        return false;
    }
    for (MethodDescription.InDefinedShape methodDescription : annotationType.getDeclaredMethods()) {
        if (!getValue(methodDescription).equals(annotationDescription.getValue(methodDescription))) {
            return false;
        }
    }
    return true;
}
 
Example 3
Source File: AbstractTypeDescriptionGenericTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
@JavaVersionRule.Enforce(8)
@SuppressWarnings("unchecked")
public void testTypeAnnotationsSuperType() throws Exception {
    Class<? extends Annotation> typeAnnotation = (Class<? extends Annotation>) Class.forName(TYPE_ANNOTATION);
    MethodDescription.InDefinedShape value = TypeDescription.ForLoadedType.of(typeAnnotation).getDeclaredMethods().getOnly();
    Class<?> samples = Class.forName(TYPE_ANNOTATION_SAMPLES);
    TypeDescription.Generic superClass = describeSuperClass(samples);
    assertThat(superClass.getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(superClass.getDeclaredAnnotations().size(), is(1));
    assertThat(superClass.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
    assertThat(superClass.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value).resolve(Integer.class), is(18));
}
 
Example 4
Source File: AnnotationAppender.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Handles the writing of a single annotation to an annotation visitor.
 *
 * @param annotationVisitor     The annotation visitor the write process is to be applied on.
 * @param annotation            The annotation to be written.
 * @param annotationValueFilter The value filter to apply for discovering which values of an annotation should be written.
 */
private static void handle(AnnotationVisitor annotationVisitor, AnnotationDescription annotation, AnnotationValueFilter annotationValueFilter) {
    for (MethodDescription.InDefinedShape methodDescription : annotation.getAnnotationType().getDeclaredMethods()) {
        if (annotationValueFilter.isRelevant(annotation, methodDescription)) {
            apply(annotationVisitor, methodDescription.getReturnType().asErasure(), methodDescription.getName(), annotation.getValue(methodDescription).resolve());
        }
    }
    annotationVisitor.visitEnd();
}
 
Example 5
Source File: Implementation.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public MethodDescription.InDefinedShape registerSetterFor(FieldDescription fieldDescription, AccessType accessType) {
    DelegationRecord record = registeredSetters.get(fieldDescription);
    record = record == null
            ? new FieldSetterDelegation(instrumentedType, suffix, accessType, fieldDescription)
            : record.with(accessType);
    registeredSetters.put(fieldDescription, record);
    return record.getMethod();
}
 
Example 6
Source File: JavaConstantMethodHandleTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testAbstractMethodNotSpecial() throws Exception {
    MethodDescription.InDefinedShape methodDescription = mock(MethodDescription.InDefinedShape.class);
    TypeDescription typeDescription = mock(TypeDescription.class);
    when(methodDescription.isAbstract()).thenReturn(true);
    when(methodDescription.isSpecializableFor(typeDescription)).thenReturn(true);
    JavaConstant.MethodHandle.ofSpecial(methodDescription, typeDescription);
}
 
Example 7
Source File: JavaConstantMethodHandleTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testMethodNotSpecializable() throws Exception {
    MethodDescription.InDefinedShape methodDescription = mock(MethodDescription.InDefinedShape.class);
    TypeDescription typeDescription = mock(TypeDescription.class);
    when(methodDescription.isSpecializableFor(typeDescription)).thenReturn(false);
    JavaConstant.MethodHandle.ofSpecial(methodDescription, typeDescription);
}
 
Example 8
Source File: MethodConstant.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a stack manipulation that loads a method constant onto the operand stack.
 *
 * @param methodDescription The method to be loaded onto the stack.
 * @return A stack manipulation that assigns a method constant for the given method description.
 */
public static CanCache of(MethodDescription.InDefinedShape methodDescription) {
    if (methodDescription.isTypeInitializer()) {
        return CanCacheIllegal.INSTANCE;
    } else if (methodDescription.isConstructor()) {
        return new ForConstructor(methodDescription);
    } else {
        return new ForMethod(methodDescription);
    }
}
 
Example 9
Source File: InstrumentedTypeDefaultTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testWithMethod() throws Exception {
    TypeDescription.Generic returnType = mock(TypeDescription.Generic.class);
    when(returnType.accept(Mockito.any(TypeDescription.Generic.Visitor.class))).thenReturn(returnType);
    TypeDescription rawReturnType = mock(TypeDescription.class);
    when(returnType.asErasure()).thenReturn(rawReturnType);
    when(rawReturnType.getName()).thenReturn(FOO);
    TypeDescription.Generic parameterType = mock(TypeDescription.Generic.class);
    when(parameterType.accept(Mockito.any(TypeDescription.Generic.Visitor.class))).thenReturn(parameterType);
    when(parameterType.asGenericType()).thenReturn(parameterType);
    TypeDescription rawParameterType = mock(TypeDescription.class);
    when(parameterType.asErasure()).thenReturn(rawParameterType);
    when(rawParameterType.getName()).thenReturn(QUX);
    when(rawParameterType.getStackSize()).thenReturn(StackSize.ZERO);
    InstrumentedType instrumentedType = makePlainInstrumentedType();
    assertThat(instrumentedType.getDeclaredFields().size(), is(0));
    instrumentedType = instrumentedType.withMethod(new MethodDescription.Token(BAR,
            Opcodes.ACC_PUBLIC,
            returnType,
            Collections.singletonList(parameterType)));
    assertThat(instrumentedType.getDeclaredMethods().size(), is(1));
    MethodDescription.InDefinedShape methodDescription = instrumentedType.getDeclaredMethods().get(0);
    assertThat(methodDescription.getReturnType(), is(returnType));
    assertThat(methodDescription.getParameters().size(), is(1));
    assertThat(methodDescription.getParameters().asTypeList(), is(Collections.singletonList(parameterType)));
    assertThat(methodDescription.getExceptionTypes().size(), is(0));
    assertThat(methodDescription.getModifiers(), is(Opcodes.ACC_PUBLIC));
    assertThat(methodDescription.getName(), is(BAR));
    assertThat(methodDescription.getDeclaringType(), sameInstance((TypeDescription) instrumentedType));
}
 
Example 10
Source File: MethodGraphCompilerForDeclaredMethodsTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompilationNonBridge() throws Exception {
    TypeDescription typeDescription = mock(TypeDescription.class);
    MethodDescription.InDefinedShape methodDescription = mock(MethodDescription.InDefinedShape.class);
    when(typeDescription.getDeclaredMethods()).thenReturn(new MethodList.Explicit<MethodDescription.InDefinedShape>(methodDescription));
    when(methodDescription.isVirtual()).thenReturn(true);
    when(methodDescription.getModifiers()).thenReturn(Opcodes.ACC_BRIDGE);
    when(methodDescription.isVisibleTo(typeDescription)).thenReturn(true);
    MethodGraph.Linked methodGraph = MethodGraph.Compiler.ForDeclaredMethods.INSTANCE.compile(typeDescription);
    assertThat(methodGraph.listNodes().size(), is(0));
}
 
Example 11
Source File: AbstractTypeDescriptionGenericTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
@JavaVersionRule.Enforce(8)
@SuppressWarnings("unchecked")
public void testTypeAnnotationsMethodReturnType() throws Exception {
    Class<? extends Annotation> typeAnnotation = (Class<? extends Annotation>) Class.forName(TYPE_ANNOTATION);
    MethodDescription.InDefinedShape value = TypeDescription.ForLoadedType.of(typeAnnotation).getDeclaredMethods().getOnly();
    Class<?> samples = Class.forName(TYPE_ANNOTATION_SAMPLES);
    TypeDescription.Generic returnType = describeReturnType(samples.getDeclaredMethod(FOO, Exception[][].class));
    assertThat(returnType.getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(returnType.getDeclaredAnnotations().size(), is(1));
    assertThat(returnType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
    assertThat(returnType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value).resolve(Integer.class), is(28));
}
 
Example 12
Source File: AnnotationValue.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public AnnotationValue<U, V> filter(MethodDescription.InDefinedShape property, TypeDefinition typeDefinition) {
    return this;
}
 
Example 13
Source File: InstrumentedType.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public MethodList<MethodDescription.InDefinedShape> getDeclaredMethods() {
    return typeDescription.getDeclaredMethods();
}
 
Example 14
Source File: JavaConstant.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * Represents a constant that is resolved by invoking a {@code static} factory method or a constructor.
 *
 * @param methodDescription The method or constructor to invoke to create the represented constant value.
 * @param constants         The constant values passed to the bootstrap method. Values can be represented either
 *                          as {@link TypeDescription}, as {@link JavaConstant}, as {@link String} or a primitive
 *                          {@code int}, {@code long}, {@code float} or {@code double} represented as wrapper type.
 * @return A dynamic constant that is resolved by the supplied factory method or constructor.
 */
public static Dynamic ofInvocation(MethodDescription.InDefinedShape methodDescription, List<?> constants) {
    if (!methodDescription.isConstructor() && methodDescription.getReturnType().represents(void.class)) {
        throw new IllegalArgumentException("Bootstrap method is no constructor or non-void static factory: " + methodDescription);
    } else if (methodDescription.getParameters().size() + (methodDescription.isStatic() || methodDescription.isConstructor() ? 0 : 1) != constants.size()) {
        throw new IllegalArgumentException("Cannot assign " + constants + " to " + methodDescription);
    }
    List<Object> arguments = new ArrayList<Object>(constants.size());
    arguments.add(new Handle(methodDescription.isConstructor() ? Opcodes.H_NEWINVOKESPECIAL : Opcodes.H_INVOKESTATIC,
            methodDescription.getDeclaringType().getInternalName(),
            methodDescription.getInternalName(),
            methodDescription.getDescriptor(),
            false));
    Iterator<TypeDescription> iterator = (methodDescription.isStatic() || methodDescription.isConstructor()
            ? methodDescription.getParameters().asTypeList().asErasures()
            : CompoundList.of(methodDescription.getDeclaringType(), methodDescription.getParameters().asTypeList().asErasures())).iterator();
    for (Object constant : constants) {
        TypeDescription typeDescription;
        if (constant instanceof JavaConstant) {
            arguments.add(((JavaConstant) constant).asConstantPoolValue());
            typeDescription = ((JavaConstant) constant).getType();
        } else if (constant instanceof TypeDescription) {
            arguments.add(Type.getType(((TypeDescription) constant).getDescriptor()));
            typeDescription = TypeDescription.CLASS;
        } else {
            arguments.add(constant);
            typeDescription = TypeDescription.ForLoadedType.of(constant.getClass()).asUnboxed();
            if (JavaType.METHOD_TYPE.isInstance(constant) || JavaType.METHOD_HANDLE.isInstance(constant)) {
                throw new IllegalArgumentException("Must be represented as a JavaConstant instance: " + constant);
            } else if (constant instanceof Class<?>) {
                throw new IllegalArgumentException("Must be represented as a TypeDescription instance: " + constant);
            } else if (!typeDescription.isCompileTimeConstant()) {
                throw new IllegalArgumentException("Not a compile-time constant: " + constant);
            }
        }
        if (!typeDescription.isAssignableTo(iterator.next())) {
            throw new IllegalArgumentException("Cannot assign " + constants + " to " + methodDescription);
        }
    }
    return new Dynamic(new ConstantDynamic("invoke",
            (methodDescription.isConstructor()
                    ? methodDescription.getDeclaringType()
                    : methodDescription.getReturnType().asErasure()).getDescriptor(),
            new Handle(Opcodes.H_INVOKESTATIC,
                    CONSTANT_BOOTSTRAPS,
                    "invoke",
                    "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/invoke/MethodHandle;[Ljava/lang/Object;)Ljava/lang/Object;",
                    false),
            arguments.toArray()), methodDescription.isConstructor() ? methodDescription.getDeclaringType() : methodDescription.getReturnType().asErasure());
}
 
Example 15
Source File: TypePoolDefaultMethodDescriptionTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
protected MethodDescription.InDefinedShape describe(Constructor<?> constructor) {
    return typePool.describe(constructor.getDeclaringClass().getName())
            .resolve()
            .getDeclaredMethods().filter(is(constructor)).getOnly();
}
 
Example 16
Source File: TypePoolDefaultLazyMethodListTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
protected MethodDescription.InDefinedShape asElement(Method element) {
    return new MethodDescription.ForLoadedMethod(element);
}
 
Example 17
Source File: AbstractTypeDescriptionGenericVariableDefiningTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
@Test
@JavaVersionRule.Enforce(8)
@SuppressWarnings("unchecked")
public void testTypeVariableV() throws Exception {
    Class<? extends Annotation> typeAnnotation = (Class<? extends Annotation>) Class.forName(TYPE_ANNOTATION);
    MethodDescription.InDefinedShape value = TypeDescription.ForLoadedType.of(typeAnnotation).getDeclaredMethods().getOnly();
    TypeDescription typeDescription = describe(Class.forName(TYPE_ANNOTATION_SAMPLES));
    TypeDescription.Generic v = typeDescription.getTypeVariables().filter(named(V)).getOnly();
    assertThat(v.getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(v.getDeclaredAnnotations().size(), is(1));
    assertThat(v.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
    assertThat(v.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value).resolve(Integer.class), is(7));
    assertThat(v.getUpperBounds().get(0).getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(v.getUpperBounds().get(0).getDeclaredAnnotations().size(), is(0));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(0).getSort(), is(TypeDefinition.Sort.WILDCARD));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(0).getDeclaredAnnotations().size(), is(1));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(0).getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(0).getDeclaredAnnotations().ofType(typeAnnotation).getValue(value).resolve(Integer.class), is(8));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(0).getUpperBounds().getOnly().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(0).getUpperBounds().getOnly().getDeclaredAnnotations().size(), is(1));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(0).getUpperBounds().getOnly().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(0).getUpperBounds().getOnly().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value).resolve(Integer.class), is(9));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getDeclaredAnnotations().size(), is(1));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getDeclaredAnnotations().ofType(typeAnnotation).getValue(value).resolve(Integer.class), is(10));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getTypeArguments().getOnly().getSort(), is(TypeDefinition.Sort.WILDCARD));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getTypeArguments().getOnly().getDeclaredAnnotations().size(), is(1));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getTypeArguments().getOnly().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getTypeArguments().getOnly().getDeclaredAnnotations().ofType(typeAnnotation)
            .getValue(value).resolve(Integer.class), is(11));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getTypeArguments().getOnly().getLowerBounds().getOnly().getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getTypeArguments().getOnly().getLowerBounds().getOnly().getDeclaredAnnotations().size(), is(1));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getTypeArguments().getOnly().getLowerBounds().getOnly().getDeclaredAnnotations()
            .isAnnotationPresent(typeAnnotation), is(true));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getTypeArguments().getOnly().getLowerBounds().getOnly().getDeclaredAnnotations()
            .ofType(typeAnnotation).getValue(value).resolve(Integer.class), is(12));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getTypeArguments().getOnly().getLowerBounds().getOnly().getUpperBounds().get(0)
            .getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getTypeArguments().getOnly().getLowerBounds().getOnly().getUpperBounds().get(0)
            .getDeclaredAnnotations().size(), is(0));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getTypeArguments().getOnly().getLowerBounds().getOnly().getUpperBounds().get(1)
            .getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getTypeArguments().getOnly().getLowerBounds().getOnly().getUpperBounds().get(1)
            .getDeclaredAnnotations().size(), is(1));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getTypeArguments().getOnly().getLowerBounds().getOnly().getUpperBounds().get(1)
            .getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
    assertThat(v.getUpperBounds().get(0).getTypeArguments().get(1).getTypeArguments().getOnly().getLowerBounds().getOnly().getUpperBounds().get(1)
            .getDeclaredAnnotations().getOnly().prepare(typeAnnotation).getValue(value).resolve(Integer.class), is(3));
}
 
Example 18
Source File: Implementation.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public MethodDescription.InDefinedShape registerGetterFor(FieldDescription fieldDescription, AccessType accessType) {
    throw new IllegalStateException("Registration of field accessor was disabled: " + fieldDescription);
}
 
Example 19
Source File: MethodInvocation.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an invocation of a given method on a given invocation target type.
 *
 * @param methodDescription The method to be invoked.
 * @param typeDescription   The type on which this method is to be invoked.
 */
protected Invocation(MethodDescription.InDefinedShape methodDescription, TypeDescription typeDescription) {
    this.typeDescription = typeDescription;
    this.methodDescription = methodDescription;
}
 
Example 20
Source File: MethodRebaseResolver.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Resolves a rebasement for the provided method.
 *
 * @param instrumentedType      The instrumented type.
 * @param methodDescription     The method to be rebased.
 * @param methodNameTransformer The transformer to use for renaming the method.
 * @return A resolution for rebasing the provided method.
 */
public static Resolution of(TypeDescription instrumentedType,
                            MethodDescription.InDefinedShape methodDescription,
                            MethodNameTransformer methodNameTransformer) {
    return new ForRebasedMethod(new RebasedMethod(instrumentedType, methodDescription, methodNameTransformer));
}