Search in sources :

Example 91 with MethodDescription

use of net.bytebuddy.description.method.MethodDescription in project byte-buddy by raphw.

the class MethodGraphCompilerForDeclaredMethodsTest method testCompilation.

@Test
public void testCompilation() throws Exception {
    TypeDescription typeDescription = mock(TypeDescription.class);
    MethodDescription.InDefinedShape methodDescription = mock(MethodDescription.InDefinedShape.class);
    MethodDescription.SignatureToken token = mock(MethodDescription.SignatureToken.class);
    when(methodDescription.asSignatureToken()).thenReturn(token);
    when(typeDescription.getDeclaredMethods()).thenReturn(new MethodList.Explicit<MethodDescription.InDefinedShape>(methodDescription));
    when(methodDescription.isVirtual()).thenReturn(true);
    when(methodDescription.isBridge()).thenReturn(false);
    when(methodDescription.isVisibleTo(typeDescription)).thenReturn(true);
    MethodGraph.Linked methodGraph = MethodGraph.Compiler.ForDeclaredMethods.INSTANCE.compile(typeDescription);
    assertThat(methodGraph.listNodes().size(), is(1));
    assertThat(methodGraph.listNodes().getOnly().getRepresentative(), is((MethodDescription) methodDescription));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) TypeDescription(net.bytebuddy.description.type.TypeDescription) MethodList(net.bytebuddy.description.method.MethodList) Test(org.junit.Test)

Example 92 with MethodDescription

use of net.bytebuddy.description.method.MethodDescription in project byte-buddy by raphw.

the class RebaseImplementationTargetTest method testRebasedMethodIsInvokable.

@Test
public void testRebasedMethodIsInvokable() throws Exception {
    when(invokableMethod.getDeclaringType()).thenReturn(instrumentedType);
    when(resolution.isRebased()).thenReturn(true);
    when(resolution.getResolvedMethod()).thenReturn(rebasedMethod);
    when(resolution.getAdditionalArguments()).thenReturn(StackManipulation.Trivial.INSTANCE);
    when(rebasedMethod.isSpecializableFor(instrumentedType)).thenReturn(true);
    Implementation.SpecialMethodInvocation specialMethodInvocation = makeImplementationTarget().invokeSuper(rebasedSignatureToken);
    assertThat(specialMethodInvocation.isValid(), is(true));
    assertThat(specialMethodInvocation.getMethodDescription(), is((MethodDescription) rebasedMethod));
    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, QUX, FOO, false);
    verifyNoMoreInteractions(methodVisitor);
    verifyZeroInteractions(implementationContext);
    assertThat(size.getSizeImpact(), is(0));
    assertThat(size.getMaximalSize(), is(0));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Implementation(net.bytebuddy.implementation.Implementation) MethodVisitor(org.objectweb.asm.MethodVisitor) AbstractImplementationTargetTest(net.bytebuddy.implementation.AbstractImplementationTargetTest) Test(org.junit.Test)

Example 93 with MethodDescription

use of net.bytebuddy.description.method.MethodDescription in project byte-buddy by raphw.

the class RebaseImplementationTargetTest method testRebasedConstructorIsInvokable.

@Test
public void testRebasedConstructorIsInvokable() throws Exception {
    when(rebasedMethod.isConstructor()).thenReturn(true);
    when(invokableMethod.getDeclaringType()).thenReturn(instrumentedType);
    when(resolution.isRebased()).thenReturn(true);
    when(resolution.getResolvedMethod()).thenReturn(rebasedMethod);
    when(resolution.getAdditionalArguments()).thenReturn(NullConstant.INSTANCE);
    when(rebasedMethod.isSpecializableFor(instrumentedType)).thenReturn(true);
    Implementation.SpecialMethodInvocation specialMethodInvocation = makeImplementationTarget().invokeSuper(rebasedSignatureToken);
    assertThat(specialMethodInvocation.isValid(), is(true));
    assertThat(specialMethodInvocation.getMethodDescription(), is((MethodDescription) rebasedMethod));
    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).visitInsn(Opcodes.ACONST_NULL);
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESPECIAL, BAZ, QUX, FOO, false);
    verifyNoMoreInteractions(methodVisitor);
    verifyZeroInteractions(implementationContext);
    assertThat(size.getSizeImpact(), is(1));
    assertThat(size.getMaximalSize(), is(1));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Implementation(net.bytebuddy.implementation.Implementation) MethodVisitor(org.objectweb.asm.MethodVisitor) AbstractImplementationTargetTest(net.bytebuddy.implementation.AbstractImplementationTargetTest) Test(org.junit.Test)

Example 94 with MethodDescription

use of net.bytebuddy.description.method.MethodDescription in project byte-buddy by raphw.

the class ImplementationSpecialMethodInvocationSimpleTest method testHashCode.

@Test
public void testHashCode() throws Exception {
    MethodDescription firstMethod = mock(MethodDescription.class), secondMethod = mock(MethodDescription.class);
    MethodDescription.SignatureToken firstToken = mock(MethodDescription.SignatureToken.class), secondToken = mock(MethodDescription.SignatureToken.class);
    when(firstMethod.asSignatureToken()).thenReturn(firstToken);
    when(secondMethod.asSignatureToken()).thenReturn(secondToken);
    TypeDescription firstType = mock(TypeDescription.class), secondType = mock(TypeDescription.class);
    assertThat(new Implementation.SpecialMethodInvocation.Simple(firstMethod, firstType, mock(StackManipulation.class)).hashCode(), is(new Implementation.SpecialMethodInvocation.Simple(firstMethod, firstType, mock(StackManipulation.class)).hashCode()));
    assertThat(new Implementation.SpecialMethodInvocation.Simple(firstMethod, firstType, mock(StackManipulation.class)).hashCode(), not(new Implementation.SpecialMethodInvocation.Simple(secondMethod, firstType, mock(StackManipulation.class)).hashCode()));
    assertThat(new Implementation.SpecialMethodInvocation.Simple(firstMethod, firstType, mock(StackManipulation.class)).hashCode(), not(new Implementation.SpecialMethodInvocation.Simple(firstMethod, secondType, mock(StackManipulation.class)).hashCode()));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 95 with MethodDescription

use of net.bytebuddy.description.method.MethodDescription in project byte-buddy by raphw.

the class InliningImplementationMatcher method of.

/**
     * 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);
}
Also used : ElementMatcher(net.bytebuddy.matcher.ElementMatcher) MethodDescription(net.bytebuddy.description.method.MethodDescription)

Aggregations

MethodDescription (net.bytebuddy.description.method.MethodDescription)105 Test (org.junit.Test)102 TypeDescription (net.bytebuddy.description.type.TypeDescription)62 MethodVisitor (org.objectweb.asm.MethodVisitor)15 StackManipulation (net.bytebuddy.implementation.bytecode.StackManipulation)12 MethodList (net.bytebuddy.description.method.MethodList)9 Implementation (net.bytebuddy.implementation.Implementation)8 AbstractImplementationTargetTest (net.bytebuddy.implementation.AbstractImplementationTargetTest)6 Serializable (java.io.Serializable)4 TypeVariableSource (net.bytebuddy.description.TypeVariableSource)4 FieldList (net.bytebuddy.description.field.FieldList)4 TypePool (net.bytebuddy.pool.TypePool)4 ClassVisitor (org.objectweb.asm.ClassVisitor)4 ByteBuddy (net.bytebuddy.ByteBuddy)3 DynamicType (net.bytebuddy.dynamic.DynamicType)3 AnnotationDescription (net.bytebuddy.description.annotation.AnnotationDescription)2 LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)2 ByteCodeAppender (net.bytebuddy.implementation.bytecode.ByteCodeAppender)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1