Java Code Examples for net.bytebuddy.description.method.MethodDescription#isConstructor()

The following examples show how to use net.bytebuddy.description.method.MethodDescription#isConstructor() . 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: MethodCall.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation toStackManipulation(MethodDescription invokedMethod, Assigner assigner, Assigner.Typing typing) {
    if (instrumentedMethod.isStatic() && !invokedMethod.isStatic() && !invokedMethod.isConstructor()) {
        throw new IllegalStateException("Cannot invoke " + invokedMethod + " from " + instrumentedMethod);
    } else if (invokedMethod.isConstructor() && (!instrumentedMethod.isConstructor()
            || !instrumentedType.equals(invokedMethod.getDeclaringType().asErasure())
            && !instrumentedType.getSuperClass().asErasure().equals(invokedMethod.getDeclaringType().asErasure()))) {
        throw new IllegalStateException("Cannot invoke " + invokedMethod + " from " + instrumentedMethod + " in " + instrumentedType);
    }
    return new StackManipulation.Compound(
            invokedMethod.isStatic()
                    ? StackManipulation.Trivial.INSTANCE
                    : MethodVariableAccess.loadThis(),
            invokedMethod.isConstructor()
                    ? Duplication.SINGLE
                    : StackManipulation.Trivial.INSTANCE
    );
}
 
Example 2
Source File: MethodCall.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Invokes the given constructor in order to create an instance.
 *
 * @param methodDescription A description of the constructor to invoke.
 * @return A method call that invokes the given constructor without providing any arguments.
 */
public static MethodCall construct(MethodDescription methodDescription) {
    if (!methodDescription.isConstructor()) {
        throw new IllegalArgumentException("Not a constructor: " + methodDescription);
    }
    return new MethodCall(new MethodLocator.ForExplicitMethod(methodDescription),
            TargetHandler.ForConstructingInvocation.Factory.INSTANCE,
            Collections.<ArgumentLoader.Factory>emptyList(),
            MethodInvoker.ForContextualInvocation.Factory.INSTANCE,
            TerminationHandler.Simple.RETURNING,
            Assigner.DEFAULT,
            Assigner.Typing.STATIC);
}
 
Example 3
Source File: InliningImplementationMatcher.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a matcher where only overridable or declared methods are matched unless those are ignored. Methods that
 * are declared by the target type are only matched if they are not ignored. Declared methods that are not found on the
 * target type are always matched.
 *
 * @param ignoredMethods A method matcher that matches any ignored method.
 * @param originalType   The original type of the instrumentation before adding any user methods.
 * @return A latent method matcher that identifies any method to instrument for a rebasement or redefinition.
 */
protected static LatentMatcher<MethodDescription> of(LatentMatcher<? super MethodDescription> ignoredMethods, TypeDescription originalType) {
    ElementMatcher.Junction<MethodDescription> predefinedMethodSignatures = none();
    for (MethodDescription methodDescription : originalType.getDeclaredMethods()) {
        ElementMatcher.Junction<MethodDescription> signature = methodDescription.isConstructor()
                ? isConstructor()
                : ElementMatchers.<MethodDescription>named(methodDescription.getName());
        signature = signature.and(returns(methodDescription.getReturnType().asErasure()));
        signature = signature.and(takesArguments(methodDescription.getParameters().asTypeList().asErasures()));
        predefinedMethodSignatures = predefinedMethodSignatures.or(signature);
    }
    return new InliningImplementationMatcher(ignoredMethods, predefinedMethodSignatures);
}
 
Example 4
Source File: MethodSortMatcher.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean isSort(MethodDescription target) {
    return target.isConstructor();
}