Skip to content

Commit 40ce311

Browse files
committed
Deprecate InterfaceCandidateConcretePropertyResolver
1 parent dcd98dd commit 40ce311

File tree

8 files changed

+119
-5
lines changed

8 files changed

+119
-5
lines changed

docs/content/v1.0.x-kor/release-notes/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ sectionStart
8585
### v.1.0.21
8686
Deprecate the ObjectPropertyGenerator that modify child properties listed below.
8787
For example, `InterfaceObjectPropertyGenerator`, `SealedTypeObjectPropertyGenerator`, `SingleValueObjectPropertyGenerator`
88-
Use `InterfaceCandidateConcretePropertyResolver`, `SealedTypeCandidateConcretePropertyResolver` instead.
88+
Use `ConcreteTypeCandidateConcretePropertyResolver`, `SealedTypeCandidateConcretePropertyResolver` instead.
8989

9090
Fix set `ZoneId` in Kotlin JDK21.
9191

docs/content/v1.0.x/release-notes/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ sectionStart
8585
### v.1.0.21
8686
Deprecate the ObjectPropertyGenerator that modify child properties listed below.
8787
For example, `InterfaceObjectPropertyGenerator`, `SealedTypeObjectPropertyGenerator`, `SingleValueObjectPropertyGenerator`
88-
Use `InterfaceCandidateConcretePropertyResolver`, `SealedTypeCandidateConcretePropertyResolver` instead.
88+
Use `ConcreteTypeCandidateConcretePropertyResolver`, `SealedTypeCandidateConcretePropertyResolver` instead.
8989

9090
Fix set `ZoneId` in Kotlin JDK21.
9191

docs/content/v1.1.x-kor/docs/generating-objects/generating-interface.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,5 +271,50 @@ FixtureMonkey fixture = FixtureMonkey.builder()
271271
SealedDefaultStringSupplier stringSupplier = (SealedDefaultStringSupplier)fixture.giveMeOne(SealedStringSupplier.class);
272272
```
273273

274+
### For advanced users
275+
만약 인터페이스의 구현체가 너무 많다면 프로그래밍 방식으로 (progammatic) 인터페이스 구현체를 추가할 수 있습니다.
276+
`CandidateConcretePropertyResolver` 인터페이스를 구현한 클래스를 만들어서 `InterfacePlugin`에 추가하면 됩니다.
277+
278+
```java
279+
class YourCustomCandidateConcretePropertyResolver implements CandidateConcretePropertyResolver {
280+
@Override
281+
public List<Property> resolveCandidateConcreteProperties(Property property) {
282+
// 구현체를 추가하는 로직을 작성하세요.
283+
return List.of(...);
284+
}
285+
}
286+
```
287+
288+
만약 `List<Property>` 를 만들기 어렵다면 `Property` 생성 로직을 `ConcreteTypeCandidateConcretePropertyResolver` 에게 위임할 수 있습니다.
289+
`ConcreteTypeCandidateConcretePropertyResolver``CandidateConcretePropertyResolver`를 구현한 클래스로 생성자로 제공된 타입들과 Property 정보를 사용해 `List<Property>`로 변환해줍니다.
290+
Property 정보는 타입 파라미터를 추론할 때 사용됩니다.
291+
292+
아래와 같이 선언한 FixtureMonkey 인스턴스를 사용해서 `Collection<String>`을 생성하면 `List<String>`, `Set<String>` 중 하나로 타입이 결정됩니다.
293+
추가 옵션을 사용해서 구현체를 직접 결정할 수도 있고, 픽스쳐 몽키에게 위임할 수도 있습니다.
294+
픽스쳐 몽키의 기본 설정은 `List<String>`을 구현체 `ArrayList<String>`로 결정하고 `Set<String>``HashSet<String>`으로 결정합니다.
295+
296+
{{< alert icon="💡" title="notice">}}
297+
298+
첫 번째 파라미터로 옵션을 적용할 타입 조건은 주의해서 설정해야 합니다.
299+
예를 들어, 아래 예시에서 `AssignableTypeMatcher`를 사용하면 구현체들도 조건을 만족하므로 무한 루프에 걸립니다.
300+
301+
{{</ alert>}}
302+
303+
```java
304+
FixtureMonkey sut = FixtureMonkey.builder()
305+
.plugin(new InterfacePlugin()
306+
.interfaceImplements(
307+
new ExactTypeMatcher(Collection.class),
308+
new ConcreteTypeCandidateConcretePropertyResolver<>(List.of(List.class, Set.class))
309+
)
310+
)
311+
.build();
312+
313+
Collection<String> actual = sut.giveMeOne(new TypeReference<>() {
314+
});
315+
316+
then(actual).isInstanceOfAny(List.class, Set.class);
317+
```
318+
274319
이번 장에서는 인터페이스 타입을 생성하는 방법을 간단한 예제를 보며 배웠습니다. 인터페이스를 생성하는 데 문제가 있다면 `InterfacePlugin` 옵션들을 살펴보세요.
275320
그래도 문제가 해결되지 않는다면 GitHub에 재현 가능한 예제를 포함한 이슈를 올려주세요.

docs/content/v1.1.x-kor/release-notes/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ sectionStart
147147
### v.1.0.21
148148
Deprecate the ObjectPropertyGenerator that modify child properties listed below.
149149
For example, `InterfaceObjectPropertyGenerator`, `SealedTypeObjectPropertyGenerator`, `SingleValueObjectPropertyGenerator`
150-
Use `InterfaceCandidateConcretePropertyResolver`, `SealedTypeCandidateConcretePropertyResolver` instead.
150+
Use `ConcreteTypeCandidateConcretePropertyResolver`, `SealedTypeCandidateConcretePropertyResolver` instead.
151151

152152
Fix set `ZoneId` in Kotlin JDK21.
153153

docs/content/v1.1.x/docs/generating-objects/generating-interface.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,5 +273,53 @@ FixtureMonkey fixture = FixtureMonkey.builder()
273273
SealedDefaultStringSupplier stringSupplier = (SealedDefaultStringSupplier)fixture.giveMeOne(SealedStringSupplier.class);
274274
```
275275

276+
### For advanced users
277+
If there are too many implementations of an interface, you can add interface implementations programmatically.
278+
All you have to do is create a class that implements the `CandidateConcretePropertyResolver` interface and add it to the `InterfacePlugin`.
279+
280+
```java
281+
class YourCustomCandidateConcretePropertyResolver implements CandidateConcretePropertyResolver {
282+
@Override
283+
public List<Property> resolveCandidateConcreteProperties(Property property) {
284+
// resolve your implementations
285+
return List.of(...);
286+
}
287+
}
288+
```
289+
290+
If you have a trouble creating `List<Property>`, you can delegate the creation logic to `ConcreteTypeCandidateConcretePropertyResolver`.
291+
292+
`ConcreteTypeCandidateConcretePropertyResolver` is a class that implements the `CandidateConcretePropertyResolver` interface.
293+
It converts the types and property information provided in the constructor to `List<Property>`.
294+
The property information is used when inferring type parameters.
295+
296+
In the case below, the `ConcreteTypeCandidateConcretePropertyResolver` is used to resolve the implementations of `List` and `Set`.
297+
`Collection<String>` is resolved as either `List<String>` or `Set<String>`.
298+
You can resolve the actual implementations programmatically or delegate the creation logic to Fixture Monkey.
299+
By default, Fixture Monkey resolves `List<String>` as `ArrayList<String>` and `Set<String>` as `HashSet<String>`.
300+
301+
{{< alert icon="💡" title="notice">}}
302+
303+
You should be careful when setting the type condition to apply the options as the first parameter.
304+
For example, using `AssignableTypeMatcher` in the example below will cause an infinite loop because the implementations also satisfy the condition.
305+
306+
{{</ alert>}}
307+
308+
```java
309+
FixtureMonkey sut = FixtureMonkey.builder()
310+
.plugin(new InterfacePlugin()
311+
.interfaceImplements(
312+
new ExactTypeMatcher(Collection.class),
313+
new ConcreteTypeCandidateConcretePropertyResolver<>(List.of(List.class, Set.class))
314+
)
315+
)
316+
.build();
317+
318+
Collection<String> actual = sut.giveMeOne(new TypeReference<>() {
319+
});
320+
321+
then(actual).isInstanceOfAny(List.class, Set.class);
322+
```
323+
276324
This chapter illustrates how to create an interface type. If you get stuck, all you need to remember is the `InterfacePlugin' plugin.
277325
If the plugin doesn't solve your problem, please post a bug with a reproducible example.

docs/content/v1.1.x/release-notes/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ sectionStart
147147
### v.1.0.21
148148
Deprecate the ObjectPropertyGenerator that modify child properties listed below.
149149
For example, `InterfaceObjectPropertyGenerator`, `SealedTypeObjectPropertyGenerator`, `SingleValueObjectPropertyGenerator`
150-
Use `InterfaceCandidateConcretePropertyResolver`, `SealedTypeCandidateConcretePropertyResolver` instead.
150+
Use `ConcreteTypeCandidateConcretePropertyResolver`, `SealedTypeCandidateConcretePropertyResolver` instead.
151151

152152
Fix set `ZoneId` in Kotlin JDK21.
153153

fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/property/ConcreteTypeCandidateConcretePropertyResolver.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
import com.navercorp.fixturemonkey.api.type.GenericType;
3131
import com.navercorp.fixturemonkey.api.type.Types;
3232

33+
/**
34+
* This class is used to resolve more concrete types for a given interface.
35+
* The concrete types could be an interface or a class that implements the interface.
36+
*
37+
* @param <T> the type parameter of the interface
38+
*/
3339
@API(since = "1.0.16", status = Status.EXPERIMENTAL)
3440
public final class ConcreteTypeCandidateConcretePropertyResolver<T> implements CandidateConcretePropertyResolver {
3541
private final List<Class<? extends T>> concreteTypes;
@@ -38,6 +44,16 @@ public ConcreteTypeCandidateConcretePropertyResolver(List<Class<? extends T>> co
3844
this.concreteTypes = concreteTypes;
3945
}
4046

47+
/**
48+
* Resolves more concrete types for a given interface.
49+
* The concrete types could be an interface or a class that implements the interface.
50+
* The provided property could be a property of concrete type or an abstract class or interface.
51+
* The type parameter of the interface is used to generate properties of concrete types.
52+
* It returns a list of properties of concrete types that implement the interface.
53+
*
54+
* @param property it could be a property of concrete type or an abstract class or interface.
55+
* @return a list of properties of concrete types that implement the interface
56+
*/
4157
@Override
4258
public List<Property> resolve(Property property) {
4359
List<AnnotatedType> genericsTypes = Types.getGenericsTypes(property.getAnnotatedType());

fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/property/InterfaceCandidateConcretePropertyResolver.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626
import org.apiguardian.api.API;
2727
import org.apiguardian.api.API.Status;
2828

29-
@API(since = "1.0.21", status = Status.EXPERIMENTAL)
29+
/**
30+
* It is deprecated.
31+
* Use {@link ConcreteTypeCandidateConcretePropertyResolver} instead.
32+
*/
33+
@API(since = "1.0.21", status = Status.DEPRECATED)
34+
@Deprecated
3035
public final class InterfaceCandidateConcretePropertyResolver<T> implements CandidateConcretePropertyResolver {
3136
private final List<Class<? extends T>> implementations;
3237

0 commit comments

Comments
 (0)