Skip to content

Commit

Permalink
Add generating inner class (#807)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Nov 2, 2023
1 parent 6afcc30 commit dd3e282
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,29 @@ public List<Property> generateParameterProperties(ConstructorPropertyGeneratorCo
resolveParameterTypes(typeReferencesByConstructor, context.getInputParameterTypes());

Map<String, Field> fieldsByName = TypeCache.getFieldsByName(type);
int parameterSize = resolvedParameterNames.size();
boolean anyReceiverParameter = Arrays.stream(constructor.getAnnotatedParameterTypes())
.anyMatch(it -> constructor.getAnnotatedReceiverType() != null
&& it.getType().equals(constructor.getAnnotatedReceiverType().getType()));
int parameterSize = typeReferencesByConstructor.size();

Map<String, Property> constructorPropertiesByName = new LinkedHashMap<>();
for (int i = 0; i < parameterSize; i++) {
String parameterName = resolvedParameterNames.get(i);
if (anyReceiverParameter) {
Parameter receiverParameter = constructor.getParameters()[0];
constructorPropertiesByName.put(
receiverParameter.getName(),
new ConstructorProperty(
receiverParameter.getAnnotatedType(),
constructor,
receiverParameter.getName(),
null,
null
)
);
}
int parameterStartIndex = anyReceiverParameter ? 1 : 0;
for (int i = parameterStartIndex; i < parameterSize; i++) {
int parameterNameIndex = anyReceiverParameter ? i - 1 : i;
String parameterName = resolvedParameterNames.get(parameterNameIndex);
Field field = fieldsByName.get(parameterName);
Property fieldProperty = field != null
? new FieldProperty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,24 @@ private static String[] getParameterNames(Constructor<?> constructor) {
.map(Parameter::getName)
.toArray(String[]::new);
} else {
ConstructorProperties constructorPropertiesAnnotation =
constructor.getAnnotation(ConstructorProperties.class);
return constructorPropertiesAnnotation.value();
boolean anyReceiverParameter = Arrays.stream(constructor.getAnnotatedParameterTypes())
.anyMatch(it -> constructor.getAnnotatedReceiverType() != null
&& it.getType().equals(constructor.getAnnotatedReceiverType().getType()));

String[] constructorPropertiesNames = constructor.getAnnotation(ConstructorProperties.class).value();
if (!anyReceiverParameter) {
return constructorPropertiesNames;
}

String receiverParameterName = constructor.getParameters()[0].getName();
return concatArray(receiverParameterName, constructorPropertiesNames);
}
}

private static String[] concatArray(String single, String[] array) {
String[] concat = new String[1 + array.length];
concat[0] = single;
System.arraycopy(array, 0, concat, 1, array.length);
return concat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import com.navercorp.fixturemonkey.tests.java.ImmutableRecursiveTypeSpecs.SelfRecursiveListObject;
import com.navercorp.fixturemonkey.tests.java.ImmutableRecursiveTypeSpecs.SelfRecursiveMapObject;
import com.navercorp.fixturemonkey.tests.java.ImmutableRecursiveTypeSpecs.SelfRecursiveObject;
import com.navercorp.fixturemonkey.tests.java.NestedClassTestSpecs.Inner;

@SuppressWarnings("rawtypes")
class JavaTest {
Expand Down Expand Up @@ -1092,4 +1093,11 @@ void instantiateConstructorJavaBeansPropertyFilter() {
then(actual.getString()).isNull();
then(actual.getWrapperBoolean()).isNotNull();
}

@RepeatedTest(TEST_COUNT)
void nestedObject(){
Inner actual = SUT.giveMeOne(Inner.class);

then(actual).isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.navercorp.fixturemonkey.tests.java;

import lombok.Value;

class NestedClassTestSpecs {
@SuppressWarnings("InnerClassMayBeStatic")
@Value
public class Inner {
String value;
}
}

0 comments on commit dd3e282

Please sign in to comment.