Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9d4b3c

Browse files
committed
Improve solution by avoiding false substitutions.
1 parent 0eeb65e commit c9d4b3c

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

byte-buddy-dep/src/main/java/net/bytebuddy/asm/MemberSubstitution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ public FieldDescription resolve(TypeDescription targetType, ByteCodeElement targ
10051005
} else if (parameters.get(0).isPrimitive() || parameters.get(0).isArray()) {
10061006
throw new IllegalStateException("Cannot access field on primitive or array type for " + target);
10071007
}
1008-
TypeDefinition current = instrumentedType;
1008+
TypeDefinition current = parameters.get(0).accept(new TypeDescription.Generic.Visitor.Substitutor.ForReplacement(instrumentedType));
10091009
do {
10101010
FieldList<?> fields = current.getDeclaredFields().filter(not(isStatic()).<FieldDescription>and(isVisibleTo(instrumentedType)).and(matcher));
10111011
if (fields.size() == 1) {
@@ -1226,7 +1226,7 @@ public MethodDescription resolve(TypeDescription targetType, ByteCodeElement tar
12261226
} else if (parameters.get(0).isPrimitive() || parameters.get(0).isArray()) {
12271227
throw new IllegalStateException("Cannot invoke method on primitive or array type for " + target);
12281228
}
1229-
TypeDefinition typeDefinition = instrumentedType;
1229+
TypeDefinition typeDefinition = parameters.get(0).accept(new TypeDescription.Generic.Visitor.Substitutor.ForReplacement(instrumentedType));
12301230
List<MethodDescription> candidates = CompoundList.<MethodDescription>of(methodGraphCompiler.compile(typeDefinition, instrumentedType)
12311231
.listNodes()
12321232
.asMethodList()

byte-buddy-dep/src/main/java/net/bytebuddy/description/type/TypeDescription.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,6 +2225,39 @@ public Generic onTypeVariable(Generic typeVariable) {
22252225
return new OfTypeVariable.Symbolic(typeVariable.getSymbol(), typeVariable);
22262226
}
22272227
}
2228+
2229+
/**
2230+
* A substitutor that replaces a type description with an equal type description.
2231+
*/
2232+
@HashCodeAndEqualsPlugin.Enhance
2233+
public static class ForReplacement extends Substitutor {
2234+
2235+
/**
2236+
* The type description to substitute.
2237+
*/
2238+
private final TypeDescription typeDescription;
2239+
2240+
/**
2241+
* Creates a new substitutor for a type description replacement.
2242+
*
2243+
* @param typeDescription The type description to substitute.
2244+
*/
2245+
public ForReplacement(TypeDescription typeDescription) {
2246+
this.typeDescription = typeDescription;
2247+
}
2248+
2249+
@Override
2250+
public Generic onTypeVariable(Generic typeVariable) {
2251+
return typeVariable;
2252+
}
2253+
2254+
@Override
2255+
protected Generic onSimpleType(Generic typeDescription) {
2256+
return typeDescription.asErasure().equals(this.typeDescription)
2257+
? new OfNonGenericType.Latent(this.typeDescription, typeDescription)
2258+
: typeDescription;
2259+
}
2260+
}
22282261
}
22292262

22302263
/**
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package net.bytebuddy.description.type;
2+
3+
import net.bytebuddy.description.annotation.AnnotationDescription;
4+
import net.bytebuddy.description.annotation.AnnotationList;
5+
import net.bytebuddy.description.annotation.AnnotationSource;
6+
import net.bytebuddy.dynamic.TargetType;
7+
import net.bytebuddy.test.utility.MockitoRule;
8+
import org.junit.Before;
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
import org.junit.rules.TestRule;
12+
import org.mockito.Mock;
13+
14+
import java.util.Collections;
15+
16+
import static org.hamcrest.CoreMatchers.is;
17+
import static org.hamcrest.MatcherAssert.assertThat;
18+
import static org.mockito.Mockito.mock;
19+
import static org.mockito.Mockito.when;
20+
21+
public class TypeDescriptionGenericVisitorSubstitutorForReplacementTest {
22+
23+
private static final String FOO = "foo";
24+
25+
@Rule
26+
public TestRule mockitoRule = new MockitoRule(this);
27+
28+
@Mock
29+
private TypeDescription target;
30+
31+
@Mock
32+
private TypeDescription.Generic generic;
33+
34+
@Mock
35+
private AnnotationDescription annotationDescription;
36+
37+
@Before
38+
public void setUp() throws Exception {
39+
when(generic.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(annotationDescription));
40+
}
41+
42+
@Test
43+
public void testReplacement() throws Exception {
44+
when(generic.asErasure()).thenReturn(target);
45+
TypeDescription.Generic typeDescription = new TypeDescription.Generic.Visitor.Substitutor.ForReplacement(target)
46+
.onSimpleType(new TypeDescription.Generic.OfNonGenericType.Latent(target, new AnnotationSource.Explicit(annotationDescription)));
47+
assertThat(typeDescription.asErasure(), is(target));
48+
assertThat(typeDescription.getDeclaredAnnotations(), is(Collections.singletonList(annotationDescription)));
49+
}
50+
51+
@Test
52+
public void testReplacementNoMatch() throws Exception {
53+
when(generic.asErasure()).thenReturn(mock(TypeDescription.class));
54+
TypeDescription.Generic typeDescription = new TypeDescription.Generic.Visitor.Substitutor.ForReplacement(target).onSimpleType(generic);
55+
assertThat(typeDescription, is(generic));
56+
}
57+
58+
@Test
59+
public void testTypeVariable() throws Exception {
60+
TypeDescription.Generic typeDescription = new TypeDescription.Generic.Visitor.Substitutor.ForReplacement(target).onTypeVariable(generic);
61+
assertThat(typeDescription, is(generic));
62+
}
63+
}

byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/scaffold/InstrumentedTypeDefaultTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ public void testWithAuxiliaryField() throws Exception {
213213
assertThat(instrumentedType.getLoadedTypeInitializer().isAlive(), is(true));
214214
}
215215

216+
@SuppressWarnings("unchecked")
216217
@Test(expected = IllegalStateException.class)
217218
public void testWithAuxiliaryFieldConflict() throws Exception {
218219
TypeDescription.Generic fieldType = mock(TypeDescription.Generic.class);

0 commit comments

Comments
 (0)