Skip to content

Commit

Permalink
Reintroduce base name resolver in suffixing random strategy and depre…
Browse files Browse the repository at this point in the history
…cate it to provide binary compatibility.
  • Loading branch information
raphw committed Nov 9, 2021
1 parent 3ea315d commit b6d899c
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 6 deletions.
118 changes: 116 additions & 2 deletions byte-buddy-dep/src/main/java/net/bytebuddy/NamingStrategy.java
Expand Up @@ -346,7 +346,7 @@ class SuffixingRandom extends Suffixing {
* @param suffix The suffix for the generated class.
*/
public SuffixingRandom(String suffix) {
this(suffix, BaseNameResolver.ForUnnamedType.INSTANCE);
this(suffix, Suffixing.BaseNameResolver.ForUnnamedType.INSTANCE);
}

/**
Expand All @@ -359,7 +359,7 @@ public SuffixingRandom(String suffix) {
* no prefix is added.
*/
public SuffixingRandom(String suffix, String javaLangPackagePrefix) {
this(suffix, BaseNameResolver.ForUnnamedType.INSTANCE, javaLangPackagePrefix);
this(suffix, Suffixing.BaseNameResolver.ForUnnamedType.INSTANCE, javaLangPackagePrefix);
}

/**
Expand All @@ -368,8 +368,21 @@ public SuffixingRandom(String suffix, String javaLangPackagePrefix) {
*
* @param suffix The suffix for the generated class.
* @param baseNameResolver The base name resolver that is queried for locating the base name.
* @deprecated Use {@link SuffixingRandom#SuffixingRandom(String, Suffixing.BaseNameResolver)}.
*/
@Deprecated
public SuffixingRandom(String suffix, BaseNameResolver baseNameResolver) {
this(suffix, (Suffixing.BaseNameResolver) baseNameResolver);
}

/**
* Creates an immutable naming strategy with a given suffix but moves types that subclass types within
* the {@code java.lang} package into Byte Buddy's package namespace.
*
* @param suffix The suffix for the generated class.
* @param baseNameResolver The base name resolver that is queried for locating the base name.
*/
public SuffixingRandom(String suffix, Suffixing.BaseNameResolver baseNameResolver) {
this(suffix, baseNameResolver, BYTE_BUDDY_RENAME_PACKAGE);
}

Expand All @@ -382,8 +395,24 @@ public SuffixingRandom(String suffix, BaseNameResolver baseNameResolver) {
* @param javaLangPackagePrefix The fallback namespace for type's that subclass types within the
* {@code java.*} namespace. If The prefix is set to the empty string,
* no prefix is added.
* @deprecated Use {@link SuffixingRandom#SuffixingRandom(String, Suffixing.BaseNameResolver, String)}.
*/
@Deprecated
public SuffixingRandom(String suffix, BaseNameResolver baseNameResolver, String javaLangPackagePrefix) {
this(suffix, (Suffixing.BaseNameResolver) baseNameResolver, javaLangPackagePrefix);
}

/**
* Creates an immutable naming strategy with a given suffix but moves types that subclass types within
* the {@code java.lang} package into a given namespace.
*
* @param suffix The suffix for the generated class.
* @param baseNameResolver The base name resolver that is queried for locating the base name.
* @param javaLangPackagePrefix The fallback namespace for type's that subclass types within the
* {@code java.*} namespace. If The prefix is set to the empty string,
* no prefix is added.
*/
public SuffixingRandom(String suffix, Suffixing.BaseNameResolver baseNameResolver, String javaLangPackagePrefix) {
this(suffix, baseNameResolver, javaLangPackagePrefix, new RandomString());
}

Expand All @@ -397,8 +426,25 @@ public SuffixingRandom(String suffix, BaseNameResolver baseNameResolver, String
* {@code java.*} namespace. If The prefix is set to the empty string,
* no prefix is added.
* @param randomString The random string instance to use.
* @deprecated Use {@link SuffixingRandom#SuffixingRandom(String, Suffixing.BaseNameResolver, String, RandomString)}.
*/
@Deprecated
public SuffixingRandom(String suffix, BaseNameResolver baseNameResolver, String javaLangPackagePrefix, RandomString randomString) {
this(suffix, (Suffixing.BaseNameResolver) baseNameResolver, javaLangPackagePrefix, randomString);
}

/**
* Creates an immutable naming strategy with a given suffix but moves types that subclass types within
* the {@code java.lang} package into a given namespace.
*
* @param suffix The suffix for the generated class.
* @param baseNameResolver The base name resolver that is queried for locating the base name.
* @param javaLangPackagePrefix The fallback namespace for type's that subclass types within the
* {@code java.*} namespace. If The prefix is set to the empty string,
* no prefix is added.
* @param randomString The random string instance to use.
*/
public SuffixingRandom(String suffix, Suffixing.BaseNameResolver baseNameResolver, String javaLangPackagePrefix, RandomString randomString) {
super(suffix, baseNameResolver, javaLangPackagePrefix);
this.randomString = randomString;
}
Expand All @@ -407,6 +453,74 @@ public SuffixingRandom(String suffix, BaseNameResolver baseNameResolver, String
protected String name(TypeDescription superClass) {
return super.name(superClass) + "$" + randomString.nextString();
}

/**
* A base name resolver is responsible for resolving a name onto which the suffix is appended.
*
* @deprecated Use {@link Suffixing.BaseNameResolver}.
*/
@Deprecated
public interface BaseNameResolver extends Suffixing.BaseNameResolver {

/**
* Uses the unnamed type's super type's name as the resolved name.
*
* @deprecated Use {@link Suffixing.BaseNameResolver.ForUnnamedType}.
*/
@Deprecated
enum ForUnnamedType implements BaseNameResolver {

/**
* The singleton instance.
*/
INSTANCE;

/**
* {@inheritDoc}
*/
public String resolve(TypeDescription typeDescription) {
return typeDescription.getName();
}
}

/**
* Uses a specific type's name as the resolved name.
*
* @deprecated Use {@link Suffixing.BaseNameResolver.ForGivenType}.
*/
@Deprecated
@HashCodeAndEqualsPlugin.Enhance
class ForGivenType extends Suffixing.BaseNameResolver.ForGivenType implements BaseNameResolver {

/**
* Creates a new base name resolver that resolves a using the name of a given type.
*
* @param typeDescription The type description which represents the resolved name.
*/
public ForGivenType(TypeDescription typeDescription) {
super(typeDescription);
}
}

/**
* A base name resolver that simply returns a fixed value.
*
* @deprecated Use {@link Suffixing.BaseNameResolver.ForFixedValue}.
*/
@Deprecated
@HashCodeAndEqualsPlugin.Enhance
class ForFixedValue extends Suffixing.BaseNameResolver.ForFixedValue implements BaseNameResolver {

/**
* Creates a new base name resolver for a fixed name.
*
* @param name The fixed name
*/
public ForFixedValue(String name) {
super(name);
}
}
}
}

/**
Expand Down
Expand Up @@ -22,7 +22,7 @@ public class NamingStrategyTest {
public TestRule mockitoRule = new MockitoRule(this);

@Mock
private NamingStrategy.SuffixingRandom.BaseNameResolver baseNameResolver;
private NamingStrategy.Suffixing.BaseNameResolver baseNameResolver;

@Mock
private TypeDescription.Generic typeDescription;
Expand Down Expand Up @@ -143,10 +143,10 @@ public void testSuffixingRandomRedefine() throws Exception {

@Test
public void testBaseNameResolvers() throws Exception {
assertThat(new NamingStrategy.SuffixingRandom.BaseNameResolver.ForFixedValue(FOO).resolve(rawTypeDescription), is(FOO));
assertThat(new NamingStrategy.Suffixing.BaseNameResolver.ForFixedValue(FOO).resolve(rawTypeDescription), is(FOO));
when(rawTypeDescription.getName()).thenReturn(FOO);
assertThat(new NamingStrategy.SuffixingRandom.BaseNameResolver.ForGivenType(rawTypeDescription).resolve(rawTypeDescription), is(FOO));
assertThat(NamingStrategy.SuffixingRandom.BaseNameResolver.ForUnnamedType.INSTANCE.resolve(rawTypeDescription), is(FOO));
assertThat(new NamingStrategy.Suffixing.BaseNameResolver.ForGivenType(rawTypeDescription).resolve(rawTypeDescription), is(FOO));
assertThat(NamingStrategy.Suffixing.BaseNameResolver.ForUnnamedType.INSTANCE.resolve(rawTypeDescription), is(FOO));
}

@Test
Expand Down

0 comments on commit b6d899c

Please sign in to comment.