Commit 56d7935c by chenxi

Merge remote-tracking branch 'origin/master'

2 parents 75f2629d ba34f04c
/*
package cn.com.glsx.charging.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class VOHelpler {
private static AtomicInteger rec = new AtomicInteger();
@SuppressWarnings("unchecked")
public static <T, D> T random(Class<T> t, Class<D> d) {
T o = null;
try {
if (StringUtils.startsWithAny(t.getName(), "org.glsx", "cn.com.glsx.charging")) {
o = initSwaggerExample(t.newInstance());
} else if (t == Integer.class || t == int.class) {
o = (T) Integer.valueOf(RandomUtils.nextInt(10, 3000) + 1);
} else if (t == List.class) {
o = (T) Lists.newArrayList();
for (int i = 0; i < 3; i++) {
if (d != null) {
((List) o).add(random(d, null));
} else {
((List) o).add(initSwaggerExample(ClassUtils.getGenricType(t, 0)));
}
}
} else if (t == String.class) {
o = (T) "test_str";
} else if (t == Date.class) {
o = (T) DateUtils.addDay(DateUtils.today(), -rec.getAndIncrement());
if (rec.get() >= 7) {
rec.set(0);
}
} else if (t == BigDecimal.class) {
o = (T) BigDecimal.valueOf(RandomUtils.nextDouble()).setScale(4, RoundingMode.HALF_UP);
} else {
log.info("not init type:{}", t.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
return o;
}
public static <O> O initSwaggerExample(O o) {
if (o == null) {
return o;
}
List<Field> fields = BeanUtils.getFields(o.getClass());
for (Field f : fields) {
try {
f.setAccessible(true);
if (StringUtils.startsWithAny(f.getType().getName(), "org.glsx", "cn.com.glsx.charging")) {
if (f.get(o) == null && !Modifier.isAbstract(f.getType().getModifiers())) {
// 直接初始化
Object v = initSwaggerExample(f.getType().newInstance());
f.set(o, v);
} else {
initSwaggerExample(f.get(o));
}
} else if (StringUtils.startsWith(f.getType().getName(), "java")) {
ApiModelProperty annon = f.getAnnotation(ApiModelProperty.class);
if (annon != null && StringUtils.isNotEmpty(annon.example())) {
if (StringUtils.isNotEmpty(annon.dataType())) {
f.set(o, TypeConvertUtil.convert(annon.example(), f.getType().getName(), annon.dataType()));
} else {
f.set(o, TypeConvertUtil.convert(annon.example(), f.getType().getName(), null));
}
} else {
// 直接初始化
f.setAccessible(true);
f.set(o, random(f.getType(), ClassUtils.getFieldGenericType(f, 0)));
}
}
} catch (Exception e) {
log.warn("field:{}", f, e);
}
}
return o;
}
}
*/
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!