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

The following examples show how to use net.bytebuddy.description.method.MethodDescription#SignatureToken . 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: MethodGraphCompilerDefaultTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testReturnTypeClassMultipleEvolution() throws Exception {
    TypeDescription typeDescription = TypeDescription.ForLoadedType.of(ReturnTypeClassBase.Intermediate.Inner.class);
    MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(typeDescription);
    assertThat(methodGraph.listNodes().size(), is(TypeDescription.OBJECT.getDeclaredMethods().filter(isVirtual()).size() + 1));
    MethodDescription.SignatureToken token = typeDescription.getDeclaredMethods().filter(isMethod().and(ElementMatchers.not(isBridge()))).getOnly().asSignatureToken();
    MethodGraph.Node node = methodGraph.locate(token);
    MethodDescription.SignatureToken firstBridgeToken = typeDescription.getSuperClass().getDeclaredMethods()
            .filter(isMethod().and(ElementMatchers.not(isBridge()))).getOnly().asDefined().asSignatureToken();
    MethodDescription.SignatureToken secondBridgeToken = typeDescription.getSuperClass().getSuperClass().getDeclaredMethods()
            .filter(isMethod()).getOnly().asDefined().asSignatureToken();
    assertThat(node, is(methodGraph.locate(firstBridgeToken)));
    assertThat(node, is(methodGraph.locate(secondBridgeToken)));
    assertThat(node.getSort(), is(MethodGraph.Node.Sort.RESOLVED));
    assertThat(node.getMethodTypes().size(), is(3));
    assertThat(node.getMethodTypes().contains(token.asTypeToken()), is(true));
    assertThat(node.getMethodTypes().contains(firstBridgeToken.asTypeToken()), is(true));
    assertThat(node.getMethodTypes().contains(secondBridgeToken.asTypeToken()), is(true));
    assertThat(node.getVisibility(), is(Visibility.PUBLIC));
}
 
Example 2
Source File: MethodGraphCompilerDefaultTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testReturnTypeInterfaceMultipleEvolution() throws Exception {
    TypeDescription typeDescription = TypeDescription.ForLoadedType.of(ReturnTypeInterfaceBase.Intermediate.Inner.class);
    MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(typeDescription);
    assertThat(methodGraph.listNodes().size(), is(1));
    MethodDescription.SignatureToken token = typeDescription.getDeclaredMethods().filter(ElementMatchers.not(isBridge())).getOnly().asSignatureToken();
    MethodGraph.Node node = methodGraph.locate(token);
    MethodDescription.SignatureToken firstBridgeToken = typeDescription.getInterfaces().getOnly()
            .getDeclaredMethods().filter(ElementMatchers.not(isBridge())).getOnly().asSignatureToken();
    MethodDescription.SignatureToken secondBridgeToken = typeDescription.getInterfaces().getOnly().getInterfaces().getOnly()
            .getDeclaredMethods().getOnly().asSignatureToken();
    assertThat(node, is(methodGraph.locate(firstBridgeToken)));
    assertThat(node.getSort(), is(MethodGraph.Node.Sort.RESOLVED));
    assertThat(node.getMethodTypes().size(), is(3));
    assertThat(node.getMethodTypes().contains(token.asTypeToken()), is(true));
    assertThat(node.getMethodTypes().contains(firstBridgeToken.asTypeToken()), is(true));
    assertThat(node.getMethodTypes().contains(secondBridgeToken.asTypeToken()), is(true));
    assertThat(node.getVisibility(), is(Visibility.PUBLIC));
}
 
Example 3
Source File: MethodGraphCompilerDefaultTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericReturnTypeClassMultipleEvolution() throws Exception {
    TypeDescription typeDescription = TypeDescription.ForLoadedType.of(GenericReturnClassBase.Intermediate.Inner.class);
    MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(typeDescription);
    assertThat(methodGraph.listNodes().size(), is(TypeDescription.OBJECT.getDeclaredMethods().filter(isVirtual()).size() + 1));
    MethodDescription.SignatureToken token = typeDescription.getDeclaredMethods().filter(isMethod().and(ElementMatchers.not(isBridge()))).getOnly().asSignatureToken();
    MethodGraph.Node node = methodGraph.locate(token);
    MethodDescription.SignatureToken firstBridgeToken = typeDescription.getSuperClass().getDeclaredMethods()
            .filter(isMethod().and(ElementMatchers.not(isBridge()))).getOnly().asDefined().asSignatureToken();
    MethodDescription.SignatureToken secondBridgeToken = typeDescription.getSuperClass().getSuperClass().getDeclaredMethods()
            .filter(isMethod()).getOnly().asDefined().asSignatureToken();
    assertThat(node, is(methodGraph.locate(firstBridgeToken)));
    assertThat(node, is(methodGraph.locate(secondBridgeToken)));
    assertThat(node.getSort(), is(MethodGraph.Node.Sort.RESOLVED));
    assertThat(node.getMethodTypes().size(), is(3));
    assertThat(node.getMethodTypes().contains(token.asTypeToken()), is(true));
    assertThat(node.getMethodTypes().contains(firstBridgeToken.asTypeToken()), is(true));
    assertThat(node.getMethodTypes().contains(secondBridgeToken.asTypeToken()), is(true));
    assertThat(node.getVisibility(), is(Visibility.PUBLIC));
}
 
Example 4
Source File: MethodGraphCompilerDefaultTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericWithReturnTypeInterfaceMultipleEvolution() throws Exception {
    TypeDescription typeDescription = TypeDescription.ForLoadedType.of(GenericWithReturnTypeInterfaceBase.Intermediate.Inner.class);
    MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(typeDescription);
    assertThat(methodGraph.listNodes().size(), is(1));
    MethodDescription.SignatureToken token = typeDescription.getDeclaredMethods().filter(ElementMatchers.not(isBridge())).getOnly().asSignatureToken();
    MethodGraph.Node node = methodGraph.locate(token);
    MethodDescription.SignatureToken firstBridgeToken = typeDescription.getInterfaces().getOnly()
            .getDeclaredMethods().filter(ElementMatchers.not(isBridge())).getOnly().asDefined().asSignatureToken();
    MethodDescription.SignatureToken secondBridgeToken = typeDescription.getInterfaces().getOnly().getInterfaces().getOnly()
            .getDeclaredMethods().getOnly().asDefined().asSignatureToken();
    assertThat(node, is(methodGraph.locate(firstBridgeToken)));
    assertThat(node, is(methodGraph.locate(secondBridgeToken)));
    assertThat(node.getSort(), is(MethodGraph.Node.Sort.RESOLVED));
    assertThat(node.getMethodTypes().size(), is(3));
    assertThat(node.getMethodTypes().contains(token.asTypeToken()), is(true));
    assertThat(node.getMethodTypes().contains(firstBridgeToken.asTypeToken()), is(true));
    assertThat(node.getMethodTypes().contains(secondBridgeToken.asTypeToken()), is(true));
    assertThat(node.getVisibility(), is(Visibility.PUBLIC));
}
 
Example 5
Source File: MethodGraph.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Node locate(MethodDescription.SignatureToken token) {
    Node node = nodes.get(token);
    return node == null
            ? Node.Unresolved.INSTANCE
            : node;
}
 
Example 6
Source File: MethodGraphCompilerDefaultTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testReturnTypeInterfaceSingleEvolution() throws Exception {
    TypeDescription typeDescription = TypeDescription.ForLoadedType.of(ReturnTypeInterfaceBase.Inner.class);
    MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(typeDescription);
    assertThat(methodGraph.listNodes().size(), is(1));
    MethodDescription.SignatureToken token = typeDescription.getDeclaredMethods().filter(ElementMatchers.not(isBridge())).getOnly().asSignatureToken();
    MethodGraph.Node node = methodGraph.locate(token);
    MethodDescription.SignatureToken bridgeToken = typeDescription.getInterfaces().getOnly().getDeclaredMethods().getOnly().asSignatureToken();
    assertThat(node, is(methodGraph.locate(bridgeToken)));
    assertThat(node.getSort(), is(MethodGraph.Node.Sort.RESOLVED));
    assertThat(node.getMethodTypes().size(), is(2));
    assertThat(node.getMethodTypes().contains(token.asTypeToken()), is(true));
    assertThat(node.getMethodTypes().contains(bridgeToken.asTypeToken()), is(true));
    assertThat(node.getVisibility(), is(Visibility.PUBLIC));
}
 
Example 7
Source File: MethodGraphCompilerDefaultTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testOrphanedBridge() throws Exception {
    MethodDescription.SignatureToken bridgeMethod = new MethodDescription.SignatureToken("foo",
            TypeDescription.VOID,
            Collections.<TypeDescription>emptyList());
    TypeDescription typeDescription = new InstrumentedType.Default("foo",
            Opcodes.ACC_PUBLIC,
            TypeDescription.Generic.OBJECT,
            Collections.<TypeVariableToken>emptyList(),
            Collections.<TypeDescription.Generic>emptyList(),
            Collections.<FieldDescription.Token>emptyList(),
            Collections.singletonList(new MethodDescription.Token("foo",
                    Opcodes.ACC_BRIDGE,
                    TypeDescription.Generic.VOID,
                    Collections.<TypeDescription.Generic>emptyList())),
            Collections.<RecordComponentDescription.Token>emptyList(),
            Collections.<AnnotationDescription>emptyList(),
            TypeInitializer.None.INSTANCE,
            LoadedTypeInitializer.NoOp.INSTANCE,
            TypeDescription.UNDEFINED,
            MethodDescription.UNDEFINED,
            TypeDescription.UNDEFINED,
            Collections.<TypeDescription>emptyList(),
            false,
            false,
            false,
            TargetType.DESCRIPTION,
            Collections.<TypeDescription>emptyList());
    MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(typeDescription);
    assertThat(methodGraph.listNodes().size(), is(1 + TypeDescription.OBJECT.getDeclaredMethods().filter(ElementMatchers.isVirtual()).size()));
    MethodGraph.Node node = methodGraph.locate(bridgeMethod);
    assertThat(node.getSort(), is(MethodGraph.Node.Sort.RESOLVED));
    assertThat(node.getRepresentative().asSignatureToken(), is(bridgeMethod));
    assertThat(node.getMethodTypes().size(), is(1));
    assertThat(node.getMethodTypes(), hasItem(bridgeMethod.asTypeToken()));
    assertThat(node.getVisibility(), is(Visibility.PACKAGE_PRIVATE));
}
 
Example 8
Source File: MethodGraph.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Linked compile(TypeDefinition typeDefinition, TypeDescription viewPoint) {
    LinkedHashMap<MethodDescription.SignatureToken, Node> nodes = new LinkedHashMap<MethodDescription.SignatureToken, Node>();
    for (MethodDescription methodDescription : typeDefinition.getDeclaredMethods().filter(isVirtual().and(not(isBridge())).and(isVisibleTo(viewPoint)))) {
        nodes.put(methodDescription.asSignatureToken(), new Node.Simple(methodDescription));
    }
    return new Linked.Delegation(new MethodGraph.Simple(nodes), Empty.INSTANCE, Collections.<TypeDescription, MethodGraph>emptyMap());
}
 
Example 9
Source File: MethodCallProxy.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the precomputed method graph.
 */
@SuppressFBWarnings(value = "SE_BAD_FIELD_STORE", justification = "Precomputed method graph is not intended for serialization")
PrecomputedMethodGraph() {
    LinkedHashMap<MethodDescription.SignatureToken, MethodGraph.Node> nodes = new LinkedHashMap<MethodDescription.SignatureToken, MethodGraph.Node>();
    MethodDescription callMethod = new MethodDescription.Latent(TypeDescription.ForLoadedType.of(Callable.class),
            "call",
            Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT,
            Collections.<TypeVariableToken>emptyList(),
            TypeDescription.Generic.OBJECT,
            Collections.<ParameterDescription.Token>emptyList(),
            Collections.singletonList(TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(Exception.class)),
            Collections.<AnnotationDescription>emptyList(),
            AnnotationValue.UNDEFINED,
            TypeDescription.Generic.UNDEFINED);
    nodes.put(callMethod.asSignatureToken(), new MethodGraph.Node.Simple(callMethod));
    MethodDescription runMethod = new MethodDescription.Latent(TypeDescription.ForLoadedType.of(Runnable.class),
            "run",
            Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT,
            Collections.<TypeVariableToken>emptyList(),
            TypeDescription.Generic.VOID,
            Collections.<ParameterDescription.Token>emptyList(),
            Collections.<TypeDescription.Generic>emptyList(),
            Collections.<AnnotationDescription>emptyList(),
            AnnotationValue.UNDEFINED,
            TypeDescription.Generic.UNDEFINED);
    nodes.put(runMethod.asSignatureToken(), new MethodGraph.Node.Simple(runMethod));
    MethodGraph methodGraph = new MethodGraph.Simple(nodes);
    this.methodGraph = new MethodGraph.Linked.Delegation(methodGraph, methodGraph, Collections.<TypeDescription, MethodGraph>emptyMap());
}
 
Example 10
Source File: ImplementationSpecialMethodInvocationSimpleTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@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()));
}
 
Example 11
Source File: Implementation.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public SpecialMethodInvocation invokeDefault(MethodDescription.SignatureToken token) {
    SpecialMethodInvocation specialMethodInvocation = SpecialMethodInvocation.Illegal.INSTANCE;
    for (TypeDescription interfaceType : instrumentedType.getInterfaces().asErasures()) {
        SpecialMethodInvocation invocation = invokeDefault(token, interfaceType).withCheckedCompatibilityTo(token.asTypeToken());
        if (invocation.isValid()) {
            if (specialMethodInvocation.isValid()) {
                return SpecialMethodInvocation.Illegal.INSTANCE;
            } else {
                specialMethodInvocation = invocation;
            }
        }
    }
    return specialMethodInvocation;
}
 
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: MethodGraph.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Node locate(MethodDescription.SignatureToken token) {
    return Node.Unresolved.INSTANCE;
}
 
Example 14
Source File: MethodGraph.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Node locate(MethodDescription.SignatureToken token) {
    return methodGraph.locate(token);
}
 
Example 15
Source File: AbstractImplementationTargetTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
@Test
public void testIllegalSuperConstructor() throws Exception {
    MethodDescription.SignatureToken token = mock(MethodDescription.SignatureToken.class);
    when(token.getName()).thenReturn(MethodDescription.CONSTRUCTOR_INTERNAL_NAME);
    assertThat(makeImplementationTarget().invokeSuper(token).isValid(), is(false));
}
 
Example 16
Source File: AbstractImplementationTargetTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
@Test
public void testIllegalSuperMethod() throws Exception {
    MethodDescription.SignatureToken token = mock(MethodDescription.SignatureToken.class);
    when(token.getName()).thenReturn(FOO);
    assertThat(makeImplementationTarget().invokeSuper(token).isValid(), is(false));
}
 
Example 17
Source File: Implementation.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a special method invocation for invoking a default method.
 *
 * @param targetType The interface on which the default method is to be invoked.
 * @param token      A token that uniquely describes the method to invoke.
 * @return The corresponding special method invocation which might be illegal if the requested invocation is
 * not legal.
 */
SpecialMethodInvocation invokeDefault(MethodDescription.SignatureToken token, TypeDescription targetType);
 
Example 18
Source File: MethodGraph.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new simple method graph.
 *
 * @param nodes The nodes represented by this method graph.
 */
public Simple(LinkedHashMap<MethodDescription.SignatureToken, Node> nodes) {
    this.nodes = nodes;
}
 
Example 19
Source File: Implementation.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a special method invocation for invoking a default method with the given token. The default method call must
 * not be ambiguous or an illegal special method invocation is returned.
 *
 * @param token A token of the method that is to be invoked as a default method.
 * @return The corresponding default method invocation which might be illegal if the requested invocation is not legal or ambiguous.
 */
SpecialMethodInvocation invokeDefault(MethodDescription.SignatureToken token);
 
Example 20
Source File: ElementMatchers.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Only matches method descriptions that yield the provided signature token.
 *
 * @param token The signature token to match against.
 * @param <T>   The type of the matched object.
 * @return A matcher for a method with the provided signature token.
 */
public static <T extends MethodDescription> ElementMatcher.Junction<T> hasSignature(MethodDescription.SignatureToken token) {
    return new SignatureTokenMatcher<T>(is(token));
}