Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getRequiredPropertyGenerator default method #846

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,40 @@

package com.navercorp.fixturemonkey.api.generator;

import javax.annotation.Nullable;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.PropertyGenerator;

@API(since = "0.4.0", status = Status.MAINTAINED)
@FunctionalInterface
public interface ArbitraryGenerator {
CombinableArbitrary<?> generate(ArbitraryGeneratorContext context);

/**
* Retrieves a {@link PropertyGenerator} that is required for the specified property.
* This default implementation returns {@code null}, indicating that no specific
* PropertyGenerator is required by default.
* Implementations can override this method to provide a non-null {@link PropertyGenerator} as needed.
*
* <p>
* When this method returns a non-null value, it serves as the default PropertyGenerator for the specified property.
* If a different PropertyGenerator is provided by an option, it should take precedence over the one
* returned by this method.
* </p>
*
* @param property The {@link Property} for which the PropertyGenerator is required.
* This parameter should not be null. Implementations can choose to
* throw an exception if null is passed.
* @return A {@link PropertyGenerator} that is associated with the specified property,
* or {@code null} if no such generator is required or available.
*/
@Nullable
default PropertyGenerator getRequiredPropertyGenerator(Property property) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@

package com.navercorp.fixturemonkey.api.generator;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary;
import com.navercorp.fixturemonkey.api.property.CompositePropertyGenerator;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.PropertyGenerator;

/**
* Generates a {@link CombinableArbitrary} by one or more {@link ArbitraryGenerator}.
Expand All @@ -48,4 +54,22 @@ public CombinableArbitrary<?> generate(ArbitraryGeneratorContext context) {
}
return generated;
}

@Nullable
@Override
public PropertyGenerator getRequiredPropertyGenerator(Property property) {
List<PropertyGenerator> propertyGenerators = new ArrayList<>();
for (ArbitraryGenerator arbitraryGenerator : arbitraryGenerators) {
PropertyGenerator propertyGenerator = arbitraryGenerator.getRequiredPropertyGenerator(property);
if (propertyGenerator != null) {
propertyGenerators.add(propertyGenerator);
}
}

if (propertyGenerators.isEmpty()) {
return null;
}

return new CompositePropertyGenerator(propertyGenerators);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@

import static com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary.NOT_GENERATED;

import javax.annotation.Nullable;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary;
import com.navercorp.fixturemonkey.api.arbitrary.TraceableCombinableArbitrary;
import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospector;
import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospectorResult;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.PropertyGenerator;

/**
* Generates a {@link CombinableArbitrary} by {@link ArbitraryIntrospector}.
Expand Down Expand Up @@ -69,4 +73,10 @@ public CombinableArbitrary<?> generate(ArbitraryGeneratorContext context) {

return NOT_GENERATED;
}

@Nullable
@Override
public PropertyGenerator getRequiredPropertyGenerator(Property property) {
return arbitraryIntrospector.getRequiredPropertyGenerator(property);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@

package com.navercorp.fixturemonkey.api.generator;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary;
import com.navercorp.fixturemonkey.api.property.CompositePropertyGenerator;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.PropertyGenerator;

/**
* Generates a {@link CombinableArbitrary} by a matched {@link ArbitraryGenerator}.
Expand Down Expand Up @@ -51,4 +57,23 @@ public CombinableArbitrary<?> generate(ArbitraryGeneratorContext context) {
}
return CombinableArbitrary.NOT_GENERATED;
}

@Nullable
@Override
public PropertyGenerator getRequiredPropertyGenerator(Property property) {
List<PropertyGenerator> propertyGenerators = new ArrayList<>();
for (ArbitraryGenerator arbitraryGenerator : arbitraryGenerators) {
PropertyGenerator propertyGenerator = arbitraryGenerator.getRequiredPropertyGenerator(property);

if (propertyGenerator != null) {
propertyGenerators.add(propertyGenerator);
}
}

if (propertyGenerators.isEmpty()) {
return null;
}

return new CompositePropertyGenerator(propertyGenerators);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,40 @@

package com.navercorp.fixturemonkey.api.introspector;

import javax.annotation.Nullable;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.PropertyGenerator;

@API(since = "0.4.0", status = Status.MAINTAINED)
@FunctionalInterface
public interface ArbitraryIntrospector {
ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context);

/**
* Retrieves a {@link PropertyGenerator} that is required for the specified property.
* This default implementation returns {@code null}, indicating that no specific
* PropertyGenerator is required by default.
* Implementations can override this method to provide a non-null {@link PropertyGenerator} as needed.
*
* <p>
* When this method returns a non-null value, it serves as the default PropertyGenerator for the specified property.
* If a different PropertyGenerator is provided by an option, it should take precedence over the one
* returned by this method.
* </p>
*
* @param property The {@link Property} for which the PropertyGenerator is required.
* This parameter should not be null. Implementations can choose to
* throw an exception if null is passed.
* @return A {@link PropertyGenerator} that is associated with the specified property,
* or {@code null} if no such generator is required or available.
*/
@Nullable
default PropertyGenerator getRequiredPropertyGenerator(Property property) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package com.navercorp.fixturemonkey.api.introspector;

import static com.navercorp.fixturemonkey.api.property.DefaultPropertyGenerator.FIELD_PROPERTY_GENERATOR;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -36,13 +38,15 @@
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryProperty;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.PropertyGenerator;
import com.navercorp.fixturemonkey.api.type.Reflections;
import com.navercorp.fixturemonkey.api.type.TypeCache;
import com.navercorp.fixturemonkey.api.type.Types;

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

private final Logger log = LoggerFactory.getLogger(this.getClass());

@Override
Expand Down Expand Up @@ -100,4 +104,9 @@ private Function<Map<ArbitraryProperty, Object>, Object> combine(
return object;
};
}

@Override
public PropertyGenerator getRequiredPropertyGenerator(Property property) {
return FIELD_PROPERTY_GENERATOR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,27 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryProperty;
import com.navercorp.fixturemonkey.api.property.ConstructorParameterPropertyGenerator;
import com.navercorp.fixturemonkey.api.property.ConstructorPropertyGeneratorContext;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.PropertyGenerator;
import com.navercorp.fixturemonkey.api.type.Reflections;
import com.navercorp.fixturemonkey.api.type.TypeReference;
import com.navercorp.fixturemonkey.api.type.Types;

@API(since = "0.6.12", status = Status.MAINTAINED)
Expand All @@ -48,22 +55,55 @@ public ConstructorArbitraryIntrospector(ConstructorWithParameterNames<?> constru
public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context) {
Property property = context.getResolvedProperty();
Class<?> type = Types.getActualType(property.getType());

if (Modifier.isAbstract(type.getModifiers())) {
return ArbitraryIntrospectorResult.NOT_INTROSPECTED;
}

List<String> parameterNames = constructorWithParamNames.getParameterNames().isEmpty()
? Arrays.stream(constructorWithParamNames.getConstructor().getParameters())
.map(Parameter::getName)
.collect(Collectors.toList())
: constructorWithParamNames.getParameterNames();

return new ArbitraryIntrospectorResult(
CombinableArbitrary.objectBuilder()
.properties(context.getCombinableArbitrariesByArbitraryProperty())
.build(
combine(
constructorWithParamNames.getConstructor(),
constructorWithParamNames.getParameterNames()
parameterNames
)
)
);
}

@Override
public PropertyGenerator getRequiredPropertyGenerator(Property property) {
ConstructorParameterPropertyGenerator propertyGenerator = new ConstructorParameterPropertyGenerator(
it -> it.equals(constructorWithParamNames.constructor),
it -> true
);

if (constructorWithParamNames.getParameterNames().isEmpty()) {
return propertyGenerator;
}

List<TypeReference<?>> parameterTypes =
Arrays.stream(constructorWithParamNames.constructor.getGenericParameterTypes())
.map(it -> Types.toTypeReference(Types.generateAnnotatedTypeWithoutAnnotation(it)))
.collect(Collectors.toList());

return p -> propertyGenerator.generateParameterProperties(
new ConstructorPropertyGeneratorContext(
p,
constructorWithParamNames.constructor,
parameterTypes,
constructorWithParamNames.parameterNames
)
);
}

private static Function<Map<ArbitraryProperty, Object>, Object> combine(
Constructor<?> constructor,
List<String> parameterNames
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

package com.navercorp.fixturemonkey.api.introspector;

import java.beans.ConstructorProperties;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -33,7 +36,9 @@
import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryProperty;
import com.navercorp.fixturemonkey.api.property.ConstructorParameterPropertyGenerator;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.PropertyGenerator;
import com.navercorp.fixturemonkey.api.type.Reflections;
import com.navercorp.fixturemonkey.api.type.TypeCache;
import com.navercorp.fixturemonkey.api.type.Types;
Expand All @@ -42,6 +47,12 @@
public final class ConstructorPropertiesArbitraryIntrospector implements ArbitraryIntrospector {
public static final ConstructorPropertiesArbitraryIntrospector INSTANCE =
new ConstructorPropertiesArbitraryIntrospector();
public static final PropertyGenerator PROPERTY_GENERATOR = new ConstructorParameterPropertyGenerator(
it -> it.getAnnotation(ConstructorProperties.class) != null
|| Arrays.stream(it.getParameters()).anyMatch(Parameter::isNamePresent)
|| it.getParameters().length == 0,
it -> true
);

@Override
public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context) {
Expand Down Expand Up @@ -95,4 +106,9 @@ private static Function<Map<ArbitraryProperty, Object>, Object> combine(
return Reflections.newInstance(primaryConstructor, list.toArray());
};
}

@Override
public PropertyGenerator getRequiredPropertyGenerator(Property property) {
return PROPERTY_GENERATOR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.navercorp.fixturemonkey.api.introspector;

import static com.navercorp.fixturemonkey.api.property.DefaultPropertyGenerator.FIELD_PROPERTY_GENERATOR;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;
Expand All @@ -33,6 +35,7 @@
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryProperty;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.PropertyGenerator;
import com.navercorp.fixturemonkey.api.type.Reflections;
import com.navercorp.fixturemonkey.api.type.TypeCache;
import com.navercorp.fixturemonkey.api.type.Types;
Expand Down Expand Up @@ -105,4 +108,9 @@ private Function<Map<ArbitraryProperty, Object>, Object> combine(
return object;
};
}

@Override
public PropertyGenerator getRequiredPropertyGenerator(Property property) {
return FIELD_PROPERTY_GENERATOR;
}
}
Loading
Loading