Skip to content

Commit 53a1ab3

Browse files
committed
add extend by class and simplify wrapByFunction
1 parent 761c1c2 commit 53a1ab3

7 files changed

Lines changed: 106 additions & 23 deletions

File tree

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ From maven central <br>
1616
testImplementation 'ru.objectsfill:objects-fill-processor:x.x.x'
1717
testAnnotationProcessor 'ru.objectsfill:objects-fill-processor:x.x.x'
1818
```
19-
4. Read doc
2019
________________________________________________________________________
2120
This project can help with generation some random information to object.
2221

@@ -135,8 +134,7 @@ public void TestNext() {
135134
.valueLength(10)// change size of field
136135
.gen(),// construct object
137136
Extend
138-
.wrapByFunction()
139-
.addMutationFunction(t -> "You Can do this")
137+
.wrapByFunction(t -> "You Can do this")
140138
.gen()) // construct object
141139
.withGeneric("T", String.class) // if object with generic
142140
.collectionSize(10)// change global size for collections, stream, arrays
@@ -246,8 +244,7 @@ CollectionType collectionType = RandomValue.fill(Fill.object(CollectionType.clas
246244
```java
247245
Stream<String> collectionType = RandomValue.fillStream(Fill.object(String.class)
248246
.fieldParams(
249-
Extend.wrapByFunction()
250-
.addMutationFunction(t -> "You Can do this")
247+
Extend.wrapByFunction(t -> "You Can do this")
251248
.gen())
252249
.gen());
253250
```
@@ -265,8 +262,7 @@ Stream<String> collectionType = RandomValue.fillStream(Fill.object(String.class)
265262
.valueLength(10)// change size of field
266263
.gen(),// construct object
267264
Extend
268-
.wrapByFunction() //change behavior for all
269-
.addMutationFunction(t -> "You Can do this")
265+
.wrapByFunction(t -> "You Can do this") //change behavior for all
270266
.gen())
271267
.gen()
272268
```

build.gradle

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ tasks.register('javadocJar', Jar) {
3636
classifier = 'javadoc'
3737
from javadoc.destinationDir
3838
}
39+
//publishing {
40+
// publications {
41+
// mavenJava(MavenPublication) {
42+
// groupId = 'ru.objectsfill'
43+
// artifactId = 'objects-fill-processor'
44+
// version = "0.1.2"
45+
// from components.java
46+
// }
47+
// }
48+
// repositories {
49+
// mavenLocal()
50+
// }
51+
//}
3952

4053
publishing {
4154
publications {
@@ -44,7 +57,7 @@ publishing {
4457
artifact javadocJar
4558
artifactId = "objects-fill-processor"
4659
groupId = "ru.objectsfill"
47-
version = "0.1.1"
60+
version = "0.1.2"
4861
from(components["java"])
4962
pom {
5063
packaging 'jar'

src/main/java/ru/objectsfill/core/RandomValueFieldSetterCallback.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.objectsfill.core;
22

3+
import ru.objectsfill.object_param.Extend;
34
import ru.objectsfill.object_param.Fill;
45
import ru.objectsfill.service.CollectionElementCreationService;
56
import ru.objectsfill.types.primitive_type.PrimitiveTypeName;
@@ -9,6 +10,7 @@
910
import java.lang.reflect.Modifier;
1011
import java.util.Optional;
1112
import java.util.function.Function;
13+
import java.util.function.Predicate;
1214

1315
import static ru.objectsfill.service.CollectionElementCreationService.getTypeClass;
1416
import static ru.objectsfill.types.primitive_type.PrimitiveTypeName.*;
@@ -99,7 +101,7 @@ private Optional<Fill> getExtendedParameterForField(Field field) {
99101

100102
return fill.getExtendedFieldParams()
101103
.stream()
102-
.filter(fillFieldParameter -> fillFieldParameter.getFieldName().equals(field.getName()))
104+
.filter(getExtendPredicate(field))
103105
.map(extendedFieldParameter -> Fill.object(fill.getObjectz())
104106
.excludeField(fill.getExcludedField())
105107
.fieldParams(fill.getExtendedFieldParams())
@@ -111,4 +113,21 @@ private Optional<Fill> getExtendedParameterForField(Field field) {
111113
)
112114
.findFirst();
113115
}
116+
117+
/**
118+
* get fields by class or name
119+
*
120+
* @param field The field to process.
121+
*/
122+
public static Predicate<Extend> getExtendPredicate(Field field) {
123+
return fillFieldParameter -> {
124+
if(fillFieldParameter.getFieldName() != null) {
125+
return fillFieldParameter.getFieldName().equals(field.getName());
126+
} else {
127+
return field.getType().isAssignableFrom(fillFieldParameter.getClazz());
128+
}
129+
};
130+
}
131+
132+
114133
}

src/main/java/ru/objectsfill/object_param/Extend.java

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
*/
88
public class Extend {
99

10+
/**
11+
* class type for extend
12+
*/
13+
private Class<?> clazz;
14+
1015
/**
1116
* name of the field
1217
*/
@@ -25,6 +30,14 @@ public class Extend {
2530
*/
2631
private UnaryOperator<Object> singleChangeFunction;
2732

33+
/**
34+
Gets class type for extend.
35+
@return class type for extend.
36+
*/
37+
public Class<?> getClazz() {
38+
return clazz;
39+
}
40+
2841
/**
2942
Gets mutation function.
3043
@return mutation function.
@@ -70,33 +83,49 @@ public static FillFieldParametersBuilder field(String fieldName) {
7083
}
7184

7285
/**
73-
start empty builder
86+
start builder with name
87+
@param clazz set class type
88+
@return fill field parameter builder.
89+
*/
90+
public static FillFieldParametersBuilder clazz(Class<?> clazz) {
91+
return new FillFieldParametersBuilder(clazz);
92+
}
93+
94+
/**
95+
start function builder
7496
@return fill field parameter builder.
7597
*/
76-
public static FillFieldParametersBuilder wrapByFunction() {
77-
return new FillFieldParametersBuilder();
98+
public static FillFieldParametersBuilder wrapByFunction(UnaryOperator<Object> singleChangeFunction) {
99+
return new FillFieldParametersBuilder(singleChangeFunction);
78100
}
79101

80102
/**
81103
* constructor for builder
82104
* @param fieldName field name
83105
* @param collectionSize size
84106
* @param valueLength length
107+
* @param clazz class type for extend
85108
* @param singleChangeFunction mutation function
86109
*/
87110
public Extend(String fieldName, Integer collectionSize, Integer valueLength,
88-
UnaryOperator<Object> singleChangeFunction) {
111+
UnaryOperator<Object> singleChangeFunction, Class<?> clazz) {
89112
this.fieldName = fieldName;
90113
this.collectionSize = collectionSize;
91114
this.valueLength = valueLength;
92115
this.singleChangeFunction = singleChangeFunction;
116+
this.clazz = clazz;
93117
}
94118

95119
/**
96120
* builder for extend class
97121
*/
98122
public static final class FillFieldParametersBuilder {
99123

124+
/**
125+
* class type for extend
126+
*/
127+
private Class<?> clazz;
128+
100129
/**
101130
* name of the field
102131
*/
@@ -125,12 +154,29 @@ public FillFieldParametersBuilder(String fieldName) {
125154
this.fieldName = fieldName;
126155
}
127156

157+
/**
158+
constructor with field class type
159+
@param clazz set field class type
160+
*/
161+
public FillFieldParametersBuilder(Class<?> clazz) {
162+
this.clazz = clazz;
163+
}
164+
165+
/**
166+
constructor with function
167+
@param singleChangeFunction with function
168+
*/
169+
public FillFieldParametersBuilder(UnaryOperator<Object> singleChangeFunction) {
170+
this.singleChangeFunction = singleChangeFunction;
171+
}
172+
128173
/**
129174
constructor
130175
*/
131176
public FillFieldParametersBuilder() {
132177
}
133178

179+
134180
/**
135181
Sets the collection size.
136182
@param collectionSize The collection size.
@@ -166,7 +212,7 @@ public FillFieldParametersBuilder addMutationFunction(UnaryOperator<Object> sing
166212
@return The created FillFieldParameters object.
167213
*/
168214
public Extend gen() {
169-
return new Extend(fieldName, collectionSize, valueLength, singleChangeFunction);
215+
return new Extend(fieldName, collectionSize, valueLength, singleChangeFunction, clazz);
170216
}
171217
}
172218
}

src/main/java/ru/objectsfill/service/ElementCreationService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ private Stream<?> generateCollectionByClassType(Field field, Fill fill, Class<?>
165165
@return An optional containing the value if found, or empty if not found.
166166
*/
167167
public static <T> Optional<T> findClassInContainer(Class<?> fieldType, Map<Class<?>, T> container) {
168-
Optional<Class<?>> clazzVal = container.keySet().stream().filter(types -> types.isAssignableFrom(fieldType)).findFirst();
168+
Optional<Class<?>> clazzVal = container.keySet()
169+
.stream()
170+
.filter(types -> types.isAssignableFrom(fieldType))
171+
.findFirst();
169172
if(container.containsKey(fieldType)) {
170173
return Optional.ofNullable(container.get(fieldType));
171174
}

src/main/java/ru/objectsfill/utils/FieldUtils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.util.function.UnaryOperator;
1414

1515
import static org.apache.commons.lang3.ArrayUtils.EMPTY_FIELD_ARRAY;
16+
import static ru.objectsfill.core.RandomValueFieldSetterCallback.getExtendPredicate;
17+
1618
/**
1719
* Utility class for working with fields in Java classes.
1820
*/
@@ -148,7 +150,10 @@ public static UnaryOperator<Object> getObjectUnaryOperator(Fill fill, Field fiel
148150
UnaryOperator<Object> mutationFunction = t -> t;
149151
if(field == null) {
150152
Optional<Extend> extend = getFirstSingleParamFunction(fill);
151-
if (extend.isPresent() && extend.get().getFieldName() == null && extend.get().getFieldChangeFunction() != null) {
153+
if (extend.isPresent() &&
154+
extend.get().getFieldName() == null &&
155+
extend.get().getFieldChangeFunction() != null &&
156+
extend.get().getClazz() == null) {
152157
mutationFunction = extend.get().getFieldChangeFunction();
153158
}
154159
} else {
@@ -181,7 +186,7 @@ private static Optional<Extend> getFirstSingleParamFunction(Fill fill) {
181186
public static Optional<Extend> getExtFillParam(Field field, Fill fill) {
182187
return fill.getExtendedFieldParams()
183188
.stream()
184-
.filter(fillFieldParameter -> fillFieldParameter.getFieldName().equals(field.getName()))
189+
.filter(getExtendPredicate(field))
185190
.findFirst();
186191
}
187192

src/main/java/ru/objectsfill/utils/ScanningForClassUtils.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ru.objectsfill.utils;
22

33
import org.reflections.Reflections;
4+
import ru.objectsfill.annotation_processor.exceptions.FillException;
45

56
import java.lang.annotation.Annotation;
67
import java.lang.reflect.InvocationTargetException;
@@ -35,12 +36,12 @@ public static <T extends Annotation, K> List<K> scanProjectForAnnotationAndCastT
3536
Set<Class<?>> annotated =
3637
reflections.get(SubTypes.of(TypesAnnotated.with(annotationClazz)).asClass());
3738
return annotated.stream()
38-
.map(ss -> {
39+
.map(castClass -> {
3940
try {
40-
return (K) ss.getConstructor().newInstance();
41+
return (K) castClass.getConstructor().newInstance();
4142
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
4243
NoSuchMethodException e) {
43-
throw new RuntimeException(e);
44+
throw new FillException(e.getMessage());
4445
}
4546
})
4647
.toList();
@@ -61,12 +62,12 @@ public static <T> List<T> scanClassImplInterface(Class<T> castInterface, String
6162
reflections.get(SubTypes.of(castInterface).asClass());
6263

6364
return subTypes.stream()
64-
.map(ss -> {
65+
.map(castClass -> {
6566
try {
66-
return (T) ss.getConstructor().newInstance();
67+
return (T) castClass.getConstructor().newInstance();
6768
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
6869
NoSuchMethodException e) {
69-
throw new RuntimeException(e);
70+
throw new FillException(e.getMessage());
7071
}
7172
})
7273
.toList();

0 commit comments

Comments
 (0)