Skip to content

Commit

Permalink
Merge pull request #1167 from Gingerbreadz/master
Browse files Browse the repository at this point in the history
member substitution for newly defined elements and additional tests
  • Loading branch information
raphw authored Dec 1, 2021
2 parents b681d87 + 54808bb commit 0eeb65e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 0eeb65e

Please sign in to comment.