Java Code Examples for net.bytebuddy.implementation.Implementation#SpecialMethodInvocation

The following examples show how to use net.bytebuddy.implementation.Implementation#SpecialMethodInvocation . 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: TypeProxyCreationTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testAccessorIsValid() throws Exception {
    TypeProxy typeProxy = new TypeProxy(mock(TypeDescription.class),
            mock(Implementation.Target.class),
            mock(TypeProxy.InvocationFactory.class),
            false,
            false);
    TypeProxy.MethodCall methodCall = typeProxy.new MethodCall(mock(MethodAccessorFactory.class));
    TypeDescription instrumentedType = mock(TypeDescription.class);
    FieldList<FieldDescription.InDefinedShape> fieldList = mock(FieldList.class);
    when(fieldList.filter(any(ElementMatcher.class))).thenReturn(fieldList);
    when(fieldList.getOnly()).thenReturn(mock(FieldDescription.InDefinedShape.class));
    when(instrumentedType.getDeclaredFields()).thenReturn(fieldList);
    TypeProxy.MethodCall.Appender appender = methodCall.new Appender(instrumentedType);
    Implementation.SpecialMethodInvocation specialMethodInvocation = mock(Implementation.SpecialMethodInvocation.class);
    when(specialMethodInvocation.isValid()).thenReturn(true);
    StackManipulation stackManipulation = appender.new AccessorMethodInvocation(mock(MethodDescription.class), specialMethodInvocation);
    assertThat(stackManipulation.isValid(), is(true));
    verify(specialMethodInvocation).isValid();
    verifyNoMoreInteractions(specialMethodInvocation);
}
 
Example 2
Source File: RebaseImplementationTargetTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuperTypeMethodIsInvokable() throws Exception {
    when(invokableMethod.isSpecializableFor(rawSuperClass)).thenReturn(true);
    Implementation.SpecialMethodInvocation specialMethodInvocation = makeImplementationTarget().invokeSuper(invokableToken);
    assertThat(specialMethodInvocation.isValid(), is(true));
    assertThat(specialMethodInvocation.getMethodDescription(), is((MethodDescription) invokableMethod));
    assertThat(specialMethodInvocation.getTypeDescription(), is(rawSuperClass));
    MethodVisitor methodVisitor = mock(MethodVisitor.class);
    Implementation.Context implementationContext = mock(Implementation.Context.class);
    StackManipulation.Size size = specialMethodInvocation.apply(methodVisitor, implementationContext);
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESPECIAL, BAR, FOO, QUX, false);
    verifyNoMoreInteractions(methodVisitor);
    verifyZeroInteractions(implementationContext);
    assertThat(size.getSizeImpact(), is(0));
    assertThat(size.getMaximalSize(), is(0));
}
 
Example 3
Source File: RebaseImplementationTargetTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonRebasedMethodIsInvokable() throws Exception {
    when(invokableMethod.getDeclaringType()).thenReturn(instrumentedType);
    when(invokableMethod.isSpecializableFor(instrumentedType)).thenReturn(true);
    when(resolution.isRebased()).thenReturn(false);
    when(resolution.getResolvedMethod()).thenReturn(invokableMethod);
    Implementation.SpecialMethodInvocation specialMethodInvocation = makeImplementationTarget().invokeSuper(rebasedSignatureToken);
    assertThat(specialMethodInvocation.isValid(), is(true));
    assertThat(specialMethodInvocation.getMethodDescription(), is((MethodDescription) invokableMethod));
    assertThat(specialMethodInvocation.getTypeDescription(), is(instrumentedType));
    MethodVisitor methodVisitor = mock(MethodVisitor.class);
    Implementation.Context implementationContext = mock(Implementation.Context.class);
    StackManipulation.Size size = specialMethodInvocation.apply(methodVisitor, implementationContext);
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESPECIAL, BAZ, FOO, QUX, false);
    verifyNoMoreInteractions(methodVisitor);
    verifyZeroInteractions(implementationContext);
    assertThat(size.getSizeImpact(), is(0));
    assertThat(size.getMaximalSize(), is(0));
}
 
Example 4
Source File: DefaultCall.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Implementation.SpecialMethodInvocation resolve(Implementation.Target implementationTarget, MethodDescription source) {
    if (!typeDescription.isInterface()) {
        throw new IllegalStateException(source + " method carries default method call parameter on non-interface type");
    }
    return implementationTarget.invokeDefault(source.asSignatureToken(), typeDescription);
}
 
Example 5
Source File: RebaseImplementationTargetTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonSpecializableSuperClassMethodIsNotInvokable() throws Exception {
    when(invokableMethod.isSpecializableFor(rawSuperClass)).thenReturn(false);
    when(resolution.isRebased()).thenReturn(false);
    when(resolution.getResolvedMethod()).thenReturn(invokableMethod);
    Implementation.SpecialMethodInvocation specialMethodInvocation = makeImplementationTarget().invokeSuper(invokableToken);
    assertThat(specialMethodInvocation.isValid(), is(false));
}
 
Example 6
Source File: SubclassImplementationTarget.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves a special method invocation for a non-constructor invocation.
 *
 * @param token A token describing the method to be invoked.
 * @return A special method invocation for a method representing the given method token, if available.
 */
private Implementation.SpecialMethodInvocation invokeMethod(MethodDescription.SignatureToken token) {
    MethodGraph.Node methodNode = methodGraph.getSuperClassGraph().locate(token);
    return methodNode.getSort().isUnique()
            ? Implementation.SpecialMethodInvocation.Simple.of(methodNode.getRepresentative(), instrumentedType.getSuperClass().asErasure())
            : Implementation.SpecialMethodInvocation.Illegal.INSTANCE;
}
 
Example 7
Source File: SubclassImplementationTarget.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves a special method invocation for a constructor invocation.
 *
 * @param token A token describing the constructor to be invoked.
 * @return A special method invocation for a constructor representing the given method token, if available.
 */
private Implementation.SpecialMethodInvocation invokeConstructor(MethodDescription.SignatureToken token) {
    TypeDescription.Generic superClass = instrumentedType.getSuperClass();
    MethodList<?> candidates = superClass == null
            ? new MethodList.Empty<MethodDescription.InGenericShape>()
            : superClass.getDeclaredMethods().filter(hasSignature(token).and(isVisibleTo(instrumentedType)));
    return candidates.size() == 1
            ? Implementation.SpecialMethodInvocation.Simple.of(candidates.getOnly(), instrumentedType.getSuperClass().asErasure())
            : Implementation.SpecialMethodInvocation.Illegal.INSTANCE;
}
 
Example 8
Source File: DefaultMethod.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Implementation.SpecialMethodInvocation resolve(Implementation.Target implementationTarget, MethodDescription source) {
    if (!typeDescription.isInterface()) {
        throw new IllegalStateException(source + " method carries default method call parameter on non-interface type");
    }
    return implementationTarget.invokeDefault(source.asSignatureToken(), TargetType.resolve(typeDescription, implementationTarget.getInstrumentedType()));
}
 
Example 9
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));
}
 
Example 10
Source File: DefaultMethod.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Implementation.SpecialMethodInvocation resolve(Implementation.Target implementationTarget, MethodDescription source) {
    return implementationTarget.invokeDefault(source.asSignatureToken());
}
 
Example 11
Source File: TypeProxy.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
public Implementation.SpecialMethodInvocation invoke(Implementation.Target implementationTarget,
                                                     TypeDescription proxiedType,
                                                     MethodDescription instrumentedMethod) {
    return implementationTarget.invokeDefault(instrumentedMethod.asSignatureToken(), proxiedType);
}
 
Example 12
Source File: SubclassImplementationTarget.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Implementation.SpecialMethodInvocation invokeSuper(MethodDescription.SignatureToken token) {
    return token.getName().equals(MethodDescription.CONSTRUCTOR_INTERNAL_NAME)
            ? invokeConstructor(token)
            : invokeMethod(token);
}
 
Example 13
Source File: SubclassImplementationTargetTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
@Test
public void testNonSpecializableSuperClassMethodIsNotInvokable() throws Exception {
    when(invokableMethod.isSpecializableFor(rawSuperClass)).thenReturn(false);
    Implementation.SpecialMethodInvocation specialMethodInvocation = makeImplementationTarget().invokeSuper(invokableToken);
    assertThat(specialMethodInvocation.isValid(), is(false));
}
 
Example 14
Source File: RebaseImplementationTargetSpecialMethodInvocationTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
protected Implementation.SpecialMethodInvocation make(MethodDescription methodDescription, TypeDescription typeDescription) {
    return new RebaseImplementationTarget.RebasedMethodInvocation(methodDescription,
            typeDescription,
            mock(StackManipulation.class),
            new TypeList.Empty());
}
 
Example 15
Source File: RebaseImplementationTarget.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a special method invocation for the given rebase resolution.
 *
 * @param resolution The resolution for which a special method invocation is to be created.
 * @return A special method invocation for the provided resolution.
 */
private Implementation.SpecialMethodInvocation invokeSuper(MethodRebaseResolver.Resolution resolution) {
    return resolution.isRebased()
            ? RebasedMethodInvocation.of(resolution.getResolvedMethod(), instrumentedType, resolution.getPrependedParameters())
            : Implementation.SpecialMethodInvocation.Simple.of(resolution.getResolvedMethod(), instrumentedType);
}
 
Example 16
Source File: SuperMethod.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new delegation method.
 *
 * @param specialMethodInvocation The special method invocation that represents the super method call.
 * @param cached                  {@code true} if the method constant should be cached.
 * @param privileged              {@code true} if this method should be looked up using an {@link java.security.AccessController}.
 */
protected DelegationMethod(Implementation.SpecialMethodInvocation specialMethodInvocation, boolean cached, boolean privileged) {
    this.specialMethodInvocation = specialMethodInvocation;
    this.cached = cached;
    this.privileged = privileged;
}
 
Example 17
Source File: DefaultMethod.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Resolves the special method invocation to this target.
 *
 * @param implementationTarget The implementation target.
 * @param source               The method being instrumented.
 * @return A special method invocation that represents the super call of this binding.
 */
Implementation.SpecialMethodInvocation resolve(Implementation.Target implementationTarget, MethodDescription source);
 
Example 18
Source File: DefaultMethod.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new delegation method.
 *
 * @param specialMethodInvocation The special method invocation that represents the super method call.
 * @param cached                  {@code true} if the method constant should be cached.
 * @param privileged              {@code true} if the method should be looked up using an {@link java.security.AccessController}.
 */
protected DelegationMethod(Implementation.SpecialMethodInvocation specialMethodInvocation, boolean cached, boolean privileged) {
    this.specialMethodInvocation = specialMethodInvocation;
    this.cached = cached;
    this.privileged = privileged;
}
 
Example 19
Source File: TypeProxy.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a special method invocation to implement for a given method.
 *
 * @param implementationTarget The implementation target the type proxy is created for.
 * @param proxiedType          The type for the type proxy to subclass or implement.
 * @param instrumentedMethod   The instrumented method that is to be invoked.
 * @return A special method invocation of the given method or an illegal invocation if the proxy should
 * throw an {@link java.lang.AbstractMethodError} when the instrumented method is invoked.
 */
Implementation.SpecialMethodInvocation invoke(Implementation.Target implementationTarget,
                                              TypeDescription proxiedType,
                                              MethodDescription instrumentedMethod);
 
Example 20
Source File: MethodCallProxy.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new method call proxy for a given method and uses a default assigner for assigning the method's return
 * value to either the {@link java.util.concurrent.Callable#call()} or {@link Runnable#run()} method returns.
 *
 * @param specialMethodInvocation The special method invocation which should be invoked by this method call proxy.
 * @param serializableProxy       Determines if the generated proxy should be serializableProxy.
 */
public MethodCallProxy(Implementation.SpecialMethodInvocation specialMethodInvocation, boolean serializableProxy) {
    this(specialMethodInvocation, serializableProxy, Assigner.DEFAULT);
}