Java Code Examples for net.bytebuddy.description.method.MethodList#isEmpty()

The following examples show how to use net.bytebuddy.description.method.MethodList#isEmpty() . 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: PropertyAccessorCollector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation invocationOperation(
        AnnotatedMember annotatedMember, TypeDefinition beanClassDescription) {

    final String methodName = annotatedMember.getName();
    @SuppressWarnings("unchecked")
    final MethodList<MethodDescription> matchingMethods =
            (MethodList<MethodDescription>) beanClassDescription.getDeclaredMethods().filter(named(methodName));

    if (matchingMethods.size() == 1) { //method was declared on class
        return MethodInvocation.invoke(matchingMethods.getOnly());
    }
    if (matchingMethods.isEmpty()) { //method was not found on class, try super class
        return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
    }
    else { //should never happen
        throw new IllegalStateException("Could not find definition of method: " + methodName);
    }
}
 
Example 2
Source File: PropertyMutatorCollector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation invocationOperation(AnnotatedMember annotatedMember,
        TypeDefinition beanClassDescription) {

    final String methodName = annotatedMember.getName();
    @SuppressWarnings("unchecked")
    final MethodList<MethodDescription> matchingMethods =
            (MethodList<MethodDescription>) beanClassDescription.getDeclaredMethods().filter(named(methodName));

    if (matchingMethods.size() == 1) { //method was declared on class
        return MethodInvocation.invoke(matchingMethods.getOnly());
    }
    if (matchingMethods.isEmpty()) { //method was not found on class, try super class
        return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
    }
    else { //should never happen
        throw new IllegalStateException("Could not find definition of method: " + methodName);
    }

}
 
Example 3
Source File: HashCodeAndEqualsPlugin.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Override
protected EqualsMethod equalsMethod(TypeDescription instrumentedType) {
    TypeDefinition typeDefinition = instrumentedType.getSuperClass();
    while (typeDefinition != null && !typeDefinition.represents(Object.class)) {
        if (typeDefinition.asErasure().getDeclaredAnnotations().isAnnotationPresent(Enhance.class)) {
            return EqualsMethod.requiringSuperClassEquality();
        }
        MethodList<?> hashCode = typeDefinition.getDeclaredMethods().filter(isHashCode());
        if (!hashCode.isEmpty()) {
            return hashCode.getOnly().isAbstract()
                    ? EqualsMethod.isolated()
                    : EqualsMethod.requiringSuperClassEquality();
        }
        typeDefinition = typeDefinition.getSuperClass().asErasure();
    }
    return EqualsMethod.isolated();
}
 
Example 4
Source File: ConstructorStrategy.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public MethodRegistry inject(TypeDescription instrumentedType, MethodRegistry methodRegistry) {
    MethodList<?> candidates = instrumentedType.getSuperClass().getDeclaredMethods().filter(isConstructor().and(elementMatcher));
    if (candidates.isEmpty()) {
        throw new IllegalStateException("No possible candidate for super constructor invocation in " + instrumentedType.getSuperClass());
    } else if (!candidates.filter(takesArguments(0)).isEmpty()) {
        candidates = candidates.filter(takesArguments(0));
    } else if (candidates.size() > 1) {
        throw new IllegalStateException("More than one possible super constructor for constructor delegation: " + candidates);
    }
    MethodCall methodCall = MethodCall.invoke(candidates.getOnly());
    for (TypeDescription typeDescription : candidates.getOnly().getParameters().asTypeList().asErasures()) {
        methodCall = methodCall.with(typeDescription.getDefaultValue());
    }
    return methodRegistry.append(new LatentMatcher.Resolved<MethodDescription>(isConstructor().and(takesArguments(0))),
            new MethodRegistry.Handler.ForImplementation(methodCall),
            methodAttributeAppenderFactory,
            Transformer.NoOp.<MethodDescription>make());
}
 
Example 5
Source File: AnnotationDescription.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a builder with the additional, given property.
 *
 * @param property The name of the property to define.
 * @param value    An explicit description of the annotation value.
 * @return A builder with the additional, given property.
 */
public Builder define(String property, AnnotationValue<?, ?> value) {
    MethodList<MethodDescription.InDefinedShape> methodDescriptions = annotationType.getDeclaredMethods().filter(named(property));
    if (methodDescriptions.isEmpty()) {
        throw new IllegalArgumentException(annotationType + " does not define a property named " + property);
    }
    Map<String, AnnotationValue<?, ?>> annotationValues = new HashMap<String, AnnotationValue<?, ?>>(this.annotationValues);
    if (annotationValues.put(methodDescriptions.getOnly().getName(), value) != null) {
        throw new IllegalArgumentException("Property already defined: " + property);
    }
    return new Builder(annotationType, annotationValues);
}
 
Example 6
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitMethodInsn(int opcode, String owner, String internalName, String descriptor, boolean isInterface) {
    TypePool.Resolution resolution = typePool.describe(owner.replace('/', '.'));
    if (resolution.isResolved()) {
        MethodList<?> candidates;
        if (opcode == Opcodes.INVOKESPECIAL && internalName.equals(MethodDescription.CONSTRUCTOR_INTERNAL_NAME)) {
            candidates = resolution.resolve().getDeclaredMethods().filter(strict
                    ? ElementMatchers.<MethodDescription>isConstructor().and(hasDescriptor(descriptor))
                    : ElementMatchers.<MethodDescription>failSafe(isConstructor().and(hasDescriptor(descriptor))));
        } else if (opcode == Opcodes.INVOKESTATIC || opcode == Opcodes.INVOKESPECIAL) {
            candidates = resolution.resolve().getDeclaredMethods().filter(strict
                    ? ElementMatchers.<MethodDescription>named(internalName).and(hasDescriptor(descriptor))
                    : ElementMatchers.<MethodDescription>failSafe(named(internalName).and(hasDescriptor(descriptor))));
        } else if (virtualPrivateCalls) {
            candidates = resolution.resolve().getDeclaredMethods().filter(strict
                    ? ElementMatchers.<MethodDescription>isPrivate().and(not(isStatic())).and(named(internalName).and(hasDescriptor(descriptor)))
                    : ElementMatchers.<MethodDescription>failSafe(isPrivate().<MethodDescription>and(not(isStatic())).and(named(internalName).and(hasDescriptor(descriptor)))));
            if (candidates.isEmpty()) {
                candidates = methodGraphCompiler.compile(resolution.resolve(), instrumentedType).listNodes().asMethodList().filter(strict
                        ? ElementMatchers.<MethodDescription>named(internalName).and(hasDescriptor(descriptor))
                        : ElementMatchers.<MethodDescription>failSafe(named(internalName).and(hasDescriptor(descriptor))));
            }
        } else {
            candidates = methodGraphCompiler.compile(resolution.resolve(), instrumentedType).listNodes().asMethodList().filter(strict
                    ? ElementMatchers.<MethodDescription>named(internalName).and(hasDescriptor(descriptor))
                    : ElementMatchers.<MethodDescription>failSafe(named(internalName).and(hasDescriptor(descriptor))));
        }
        if (!candidates.isEmpty()) {
            Replacement.Binding binding = replacement.bind(instrumentedType,
                    instrumentedMethod,
                    resolution.resolve(),
                    candidates.getOnly(),
                    Replacement.InvocationType.of(opcode, candidates.getOnly()));
            if (binding.isBound()) {
                stackSizeBuffer = Math.max(stackSizeBuffer, binding.make(
                        candidates.getOnly().isStatic() || candidates.getOnly().isConstructor()
                                ? candidates.getOnly().getParameters().asTypeList()
                                : new TypeList.Generic.Explicit(CompoundList.of(resolution.resolve(), candidates.getOnly().getParameters().asTypeList())),
                        candidates.getOnly().isConstructor()
                                ? candidates.getOnly().getDeclaringType().asGenericType()
                                : candidates.getOnly().getReturnType(),
                        getFreeOffset())
                        .apply(new LocalVariableTracingMethodVisitor(mv), implementationContext).getMaximalSize() - (candidates.getOnly().isConstructor()
                        ? StackSize.SINGLE
                        : candidates.getOnly().getReturnType().getStackSize()).getSize());
                if (candidates.getOnly().isConstructor()) {
                    stackSizeBuffer = Math.max(stackSizeBuffer, new StackManipulation.Compound(
                            Duplication.SINGLE.flipOver(TypeDescription.OBJECT),
                            Removal.SINGLE,
                            Removal.SINGLE,
                            Duplication.SINGLE.flipOver(TypeDescription.OBJECT),
                            Removal.SINGLE,
                            Removal.SINGLE
                    ).apply(mv, implementationContext).getMaximalSize() + StackSize.SINGLE.getSize());
                }
                return;
            }
        } else if (strict) {
            throw new IllegalStateException("Could not resolve " + owner.replace('/', '.')
                    + "." + internalName + descriptor + " using " + typePool);
        }
    } else if (strict) {
        throw new IllegalStateException("Could not resolve " + owner.replace('/', '.') + " using " + typePool);
    }
    super.visitMethodInsn(opcode, owner, internalName, descriptor, isInterface);
}