diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/description/method/MethodList.java b/byte-buddy-dep/src/main/java/net/bytebuddy/description/method/MethodList.java index 8bd9f568db9..6c104c31c1c 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/description/method/MethodList.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/description/method/MethodList.java @@ -24,6 +24,7 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; /** @@ -42,6 +43,22 @@ public interface MethodList extends FilterableList< */ ByteCodeElement.Token.TokenList asTokenList(ElementMatcher matcher); + /** + * Returns a list of signature tokens for this list of methods. + * + * @return A list of signature tokens for this list of methods. + */ + List asSignatureTokenList(); + + /** + * Returns a list of signature tokens for this list of methods. + * + * @param matcher A matcher for resolving methods to {@link net.bytebuddy.description.method.MethodDescription.Token}s. + * @param typeDescription The type description to resolve the {@link net.bytebuddy.description.method.MethodDescription.SignatureToken}s to. + * @return A list of signature tokens for this list of methods. + */ + List asSignatureTokenList(ElementMatcher matcher, TypeDescription typeDescription); + /** * Returns this list of these method descriptions resolved to their defined shape. * @@ -72,6 +89,28 @@ public ByteCodeElement.Token.TokenList asTokenList(Elem return new ByteCodeElement.Token.TokenList(tokens); } + /** + * {@inheritDoc} + */ + public List asSignatureTokenList() { + List tokens = new ArrayList(size()); + for (MethodDescription methodDescription : this) { + tokens.add(methodDescription.asSignatureToken()); + } + return tokens; + } + + /** + * {@inheritDoc} + */ + public List asSignatureTokenList(ElementMatcher matcher, TypeDescription typeDescription) { + List tokens = new ArrayList(size()); + for (MethodDescription methodDescription : this) { + tokens.add(methodDescription.asToken(matcher).asSignatureToken(typeDescription)); + } + return tokens; + } + /** * {@inheritDoc} */ @@ -311,6 +350,20 @@ public ByteCodeElement.Token.TokenList asTokenList(Elem return new ByteCodeElement.Token.TokenList(); } + /** + * {@inheritDoc} + */ + public List asSignatureTokenList() { + return Collections.emptyList(); + } + + /** + * {@inheritDoc} + */ + public List asSignatureTokenList(ElementMatcher matcher, TypeDescription typeDescription) { + return Collections.emptyList(); + } + /** * {@inheritDoc} */ diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/inline/MethodRebaseResolver.java b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/inline/MethodRebaseResolver.java index 0dd73aec8d9..da809bcf225 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/inline/MethodRebaseResolver.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/inline/MethodRebaseResolver.java @@ -33,8 +33,6 @@ import java.util.*; -import static net.bytebuddy.matcher.ElementMatchers.is; - /** * A method rebase resolver is responsible for mapping methods of an instrumented type to an alternative signature. * This way a method can exist in two versions within a class: @@ -507,21 +505,21 @@ protected Default(Map resolutions, * Creates a new method rebase resolver. * * @param instrumentedType The instrumented type. - * @param rebaseableMethodTokens Tokens describing all methods that can possibly be rebased. + * @param rebaseables Tokens describing all methods that can possibly be rebased. * @param classFileVersion The class file version for the instrumentation. * @param auxiliaryTypeNamingStrategy The naming strategy for naming a potential auxiliary type. * @param methodNameTransformer A transformer for method names. * @return A method rebase resolver that is capable of rebasing any of the provided methods. */ public static MethodRebaseResolver make(TypeDescription instrumentedType, - Set rebaseableMethodTokens, + Set rebaseables, ClassFileVersion classFileVersion, AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy, MethodNameTransformer methodNameTransformer) { DynamicType placeholderType = null; Map resolutions = new HashMap(); for (MethodDescription.InDefinedShape instrumentedMethod : instrumentedType.getDeclaredMethods()) { - if (rebaseableMethodTokens.contains(instrumentedMethod.asToken(is(instrumentedType)))) { + if (rebaseables.contains(instrumentedMethod.asSignatureToken())) { Resolution resolution; if (instrumentedMethod.isConstructor()) { if (placeholderType == null) { diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/inline/RebaseDynamicTypeBuilder.java b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/inline/RebaseDynamicTypeBuilder.java index 0547e87a614..732aefdd583 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/inline/RebaseDynamicTypeBuilder.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/inline/RebaseDynamicTypeBuilder.java @@ -19,7 +19,6 @@ import net.bytebuddy.asm.AsmVisitorWrapper; import net.bytebuddy.build.HashCodeAndEqualsPlugin; import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.description.method.MethodList; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.dynamic.ClassFileLocator; import net.bytebuddy.dynamic.DynamicType; @@ -31,14 +30,12 @@ import net.bytebuddy.implementation.attribute.AnnotationValueFilter; import net.bytebuddy.implementation.attribute.TypeAttributeAppender; import net.bytebuddy.implementation.auxiliary.AuxiliaryType; -import net.bytebuddy.matcher.ElementMatcher; import net.bytebuddy.matcher.LatentMatcher; import net.bytebuddy.pool.TypePool; import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Set; import static net.bytebuddy.matcher.ElementMatchers.is; @@ -226,10 +223,11 @@ public DynamicType.Unloaded make(TypeResolutionStrategy typeResolutionStrateg typeValidation, visibilityBridgeStrategy, InliningImplementationMatcher.of(ignoredMethods, originalType)); + HashSet rebaseables = new HashSet( + originalType.getDeclaredMethods().asSignatureTokenList(is(originalType), instrumentedType)); + rebaseables.retainAll(methodRegistry.getInstrumentedMethods().asSignatureTokenList()); MethodRebaseResolver methodRebaseResolver = MethodRebaseResolver.Default.make(methodRegistry.getInstrumentedType(), - new HashSet(originalType.getDeclaredMethods() - .asTokenList(is(originalType)) - .filter(RebaseableMatcher.of(methodRegistry.getInstrumentedType(), methodRegistry.getInstrumentedMethods()))), + rebaseables, classFileVersion, auxiliaryTypeNamingStrategy, methodNameTransformer); @@ -251,43 +249,4 @@ public DynamicType.Unloaded make(TypeResolutionStrategy typeResolutionStrateg classFileLocator, methodRebaseResolver).make(typeResolutionStrategy.resolve()); } - - /** - * A matcher that filters any method that should not be rebased, i.e. that is not already defined by the original type. - */ - @HashCodeAndEqualsPlugin.Enhance - protected static class RebaseableMatcher implements ElementMatcher { - - /** - * A set of method tokens representing all instrumented methods. - */ - private final Set tokens; - - /** - * Creates a new matcher for identifying rebasable methods. - * - * @param tokens A set of method tokens representing all instrumented methods. - */ - protected RebaseableMatcher(Set tokens) { - this.tokens = tokens; - } - - /** - * Returns a matcher that filters any method that should not be rebased. - * - * @param instrumentedType The instrumented type. - * @param instrumentedMethods All instrumented methods. - * @return A suitable matcher that filters all methods that should not be rebased. - */ - protected static ElementMatcher of(TypeDescription instrumentedType, MethodList instrumentedMethods) { - return new RebaseableMatcher(new HashSet(instrumentedMethods.asTokenList(is(instrumentedType)))); - } - - /** - * {@inheritDoc} - */ - public boolean matches(MethodDescription.Token target) { - return tokens.contains(target); - } - } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/scaffold/inline/MethodRebaseResolverDefaultTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/scaffold/inline/MethodRebaseResolverDefaultTest.java index 0d6382ca726..0b40539e28d 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/scaffold/inline/MethodRebaseResolverDefaultTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/scaffold/inline/MethodRebaseResolverDefaultTest.java @@ -102,7 +102,7 @@ public void testTokenMap() throws Exception { @Test public void testCreationWithoutConstructor() throws Exception { MethodRebaseResolver methodRebaseResolver = MethodRebaseResolver.Default.make(instrumentedType, - Collections.singleton(token), + Collections.singleton(signatureToken), classFileVersion, auxiliaryTypeNamingStrategy, methodNameTransformer); @@ -117,7 +117,7 @@ public void testCreationWithoutConstructor() throws Exception { public void testCreationWithConstructor() throws Exception { when(methodDescription.isConstructor()).thenReturn(true); MethodRebaseResolver methodRebaseResolver = MethodRebaseResolver.Default.make(instrumentedType, - Collections.singleton(token), + Collections.singleton(signatureToken), classFileVersion, auxiliaryTypeNamingStrategy, methodNameTransformer); diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/scaffold/inline/RebaseDynamicTypeBuilderRebaseableMatcherTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/scaffold/inline/RebaseDynamicTypeBuilderRebaseableMatcherTest.java deleted file mode 100644 index 97ef745ebcb..00000000000 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/scaffold/inline/RebaseDynamicTypeBuilderRebaseableMatcherTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package net.bytebuddy.dynamic.scaffold.inline; - -import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.test.utility.MockitoRule; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; -import org.mockito.Mock; - -import java.util.Collections; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.*; - -public class RebaseDynamicTypeBuilderRebaseableMatcherTest { - - @Rule - public TestRule mockitoRule = new MockitoRule(this); - - @Mock - private MethodDescription.Token targetToken, otherToken; - - @Test - public void testMatchToken() throws Exception { - assertThat(new RebaseDynamicTypeBuilder.RebaseableMatcher(Collections.singleton(targetToken)).matches(targetToken), is(true)); - } - - @Test - public void testNoMatchToken() throws Exception { - assertThat(new RebaseDynamicTypeBuilder.RebaseableMatcher(Collections.singleton(otherToken)).matches(targetToken), is(false)); - } -}