Skip to content

Commit

Permalink
Fix maintain backward compatibility for raw type List or Set (#1120)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Dec 12, 2024
1 parent 74fc7d2 commit 1ecdea6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@
import com.navercorp.fixturemonkey.api.property.DefaultContainerElementProperty;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.TypeParameterProperty;
import com.navercorp.fixturemonkey.api.type.TypeReference;
import com.navercorp.fixturemonkey.api.type.Types;

@API(since = "0.4.3", status = Status.MAINTAINED)
public final class DefaultSingleContainerPropertyGenerator implements ContainerPropertyGenerator {
private static final TypeReference<String> DEFAULT_ELEMENT_TYPE =
new TypeReference<String>() {
};

public static final DefaultSingleContainerPropertyGenerator INSTANCE =
new DefaultSingleContainerPropertyGenerator();

Expand All @@ -55,7 +60,9 @@ public ContainerProperty generate(ContainerPropertyGeneratorContext context) {
List<Property> childProperties = new ArrayList<>();
for (int sequence = 0; sequence < size; sequence++) {
Integer elementIndex = sequence;
AnnotatedType elementType = elementTypes.get(0);
AnnotatedType elementType = elementTypes.isEmpty()
? DEFAULT_ELEMENT_TYPE.getAnnotatedType()
: elementTypes.get(0);

childProperties.add(
new DefaultContainerElementProperty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,31 @@
import com.navercorp.fixturemonkey.api.property.DefaultContainerElementProperty;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.TypeParameterProperty;
import com.navercorp.fixturemonkey.api.type.TypeReference;
import com.navercorp.fixturemonkey.api.type.Types;

@API(since = "0.4.0", status = Status.MAINTAINED)
public final class SetContainerPropertyGenerator implements ContainerPropertyGenerator {
public static final SetContainerPropertyGenerator INSTANCE = new SetContainerPropertyGenerator();

private static final Logger LOGGER = LoggerFactory.getLogger(SetContainerPropertyGenerator.class);
private static final TypeReference<String> DEFAULT_ELEMENT_TYPE =
new TypeReference<String>() {
};

public static final SetContainerPropertyGenerator INSTANCE = new SetContainerPropertyGenerator();

@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public ContainerProperty generate(ContainerPropertyGeneratorContext context) {
Property property = context.getProperty();

List<AnnotatedType> elementTypes = Types.getGenericsTypes(property.getAnnotatedType());
if (elementTypes.size() != 1) {
throw new IllegalArgumentException(
"Set elementsTypes must be have 1 generics type for element. "
+ "propertyType: " + property.getType()
+ ", elementTypes: " + elementTypes
);
AnnotatedType elementType;
if (elementTypes.size() == 1) {
elementType = elementTypes.get(0);
} else {
elementType = DEFAULT_ELEMENT_TYPE.getAnnotatedType();
}

AnnotatedType elementType = elementTypes.get(0);
ArbitraryContainerInfo containerInfo = context.getContainerInfo();
Class<?> actualElementType = Types.getActualType(elementType.getType());

Expand Down

0 comments on commit 1ecdea6

Please sign in to comment.