com.github.anhem.testpopulator.internal.carrier.ClassCarrier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of test-populator Show documentation
Show all versions of test-populator Show documentation
Populate java classes with fixed or random data using reflection. Facilitates the creation of objects in tests.
The newest version!
package com.github.anhem.testpopulator.internal.carrier;
import com.github.anhem.testpopulator.internal.object.ObjectFactory;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class ClassCarrier extends Carrier {
private final Class clazz;
public ClassCarrier(Class clazz, ObjectFactory objectFactory, List visited) {
super(objectFactory, visited);
this.clazz = clazz;
}
public Class getClazz() {
return clazz;
}
public ClassCarrier toClassCarrier(Class clazz) {
return new ClassCarrier<>(clazz, objectFactory, new ArrayList<>(visited));
}
@SuppressWarnings("unchecked")
public ClassCarrier toClassCarrier(Parameter parameter) {
return (ClassCarrier) new ClassCarrier<>(parameter.getType(), objectFactory, new ArrayList<>(visited));
}
public TypeCarrier toTypeCarrier(Type type) {
return new TypeCarrier(type, objectFactory, visited);
}
@SuppressWarnings("unchecked")
public CollectionCarrier toCollectionCarrier(Parameter parameter) {
return new CollectionCarrier<>((Class) parameter.getType(), parameter, objectFactory, visited);
}
public boolean addVisited() {
if (visited.contains(clazz.getName())) {
return false;
}
visited.add(clazz.getName());
return true;
}
}