Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

member substitution for newly defined elements and additional tests #1167

Merged
merged 1 commit into from Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1005,7 +1005,7 @@ public FieldDescription resolve(TypeDescription targetType, ByteCodeElement targ
} else if (parameters.get(0).isPrimitive() || parameters.get(0).isArray()) {
throw new IllegalStateException("Cannot access field on primitive or array type for " + target);
}
TypeDefinition current = parameters.get(0);
TypeDefinition current = instrumentedType;
do {
FieldList<?> fields = current.getDeclaredFields().filter(not(isStatic()).<FieldDescription>and(isVisibleTo(instrumentedType)).and(matcher));
if (fields.size() == 1) {
Expand Down Expand Up @@ -1226,7 +1226,7 @@ public MethodDescription resolve(TypeDescription targetType, ByteCodeElement tar
} else if (parameters.get(0).isPrimitive() || parameters.get(0).isArray()) {
throw new IllegalStateException("Cannot invoke method on primitive or array type for " + target);
}
TypeDefinition typeDefinition = parameters.get(0);
TypeDefinition typeDefinition = instrumentedType;
List<MethodDescription> candidates = CompoundList.<MethodDescription>of(methodGraphCompiler.compile(typeDefinition, instrumentedType)
.listNodes()
.asMethodList()
Expand Down
Expand Up @@ -2,11 +2,13 @@

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ByteArrayClassLoader;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.FieldAccessor;
import net.bytebuddy.implementation.bytecode.constant.NullConstant;
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.test.packaging.MemberSubstitutionTestHelper;
Expand Down Expand Up @@ -598,6 +600,53 @@ public void testMethodMatched() throws Exception {
assertThat(type.getDeclaredField(BAR).getInt(instance), is(1));
}

@Test
public void testDefinedFieldMatched() throws Exception {
Class<?> type = new ByteBuddy()
.redefine(MatcherSample.class)
.defineField(BAZ, int.class, Visibility.PUBLIC)
.visit(MemberSubstitution.strict().field(named(FOO)).replaceWithField(named(BAZ)).on(named(FOO)))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
Object instance = type.getConstructor().newInstance();
assertThat(type.getDeclaredMethod(FOO).invoke(instance), nullValue(Object.class));
assertThat(type.getDeclaredField(FOO).getInt(instance), is(0));
assertThat(type.getDeclaredField(BAZ).getInt(instance), is(1));
}

@Test
public void testDefinedPublicMethodMatched() throws Exception {
Class<?> type = new ByteBuddy()
.redefine(MatcherSample.class)
.defineField(BAZ, int.class, Visibility.PUBLIC)
.defineMethod(BAZ, void.class, Visibility.PUBLIC).withParameters(int.class).intercept(FieldAccessor.ofField(BAZ))
.visit(MemberSubstitution.strict().field(named(FOO)).replaceWithMethod(named(BAZ)).on(named(FOO)))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
Object instance = type.getConstructor().newInstance();
assertThat(type.getDeclaredMethod(FOO).invoke(instance), nullValue(Object.class));
assertThat(type.getDeclaredField(FOO).getInt(instance), is(0));
assertThat(type.getDeclaredField(BAZ).getInt(instance), is(1));
}

@Test
public void testDefinedPrivateMethodMatched() throws Exception {
Class<?> type = new ByteBuddy()
.redefine(MatcherSample.class)
.defineField(BAZ, int.class, Visibility.PUBLIC)
.defineMethod(BAZ, void.class, Visibility.PRIVATE).withParameters(int.class).intercept(FieldAccessor.ofField(BAZ))
.visit(MemberSubstitution.strict().field(named(FOO)).replaceWithMethod(named(BAZ)).on(named(FOO)))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
Object instance = type.getConstructor().newInstance();
assertThat(type.getDeclaredMethod(FOO).invoke(instance), nullValue(Object.class));
assertThat(type.getDeclaredField(FOO).getInt(instance), is(0));
assertThat(type.getDeclaredField(BAZ).getInt(instance), is(1));
}

@Test
public void testMethodSelfDelegationSample() throws Exception {
Class<?> type = new ByteBuddy()
Expand Down