markfuller1 / kittykam Goto Github PK
View Code? Open in Web Editor NEWLive Video Stream of My Cat Sleeping
Live Video Stream of My Cat Sleeping
public class TestObjectBuilder {
private static List<Type> recursionCheck = new ArrayList<>();
private static Random rand = new Random();
ObjectMapper mapper = new ObjectMapper();
public static Map<String, Object> objectToMap(Object o) {
Map<String, Object> converted = new LinkedHashMap<>();
Arrays.stream(o.getClass().getFields()).forEach(field -> {
field.setAccessible(true);
try {
converted.put(field.getName().toString(), field.get(o));
} catch (IllegalAccessException e) {
log.error(e.getMessage());
log.error(Arrays.toString(e.getStackTrace()));
}
});
return converted;
}
public static int recurse = 0;
private static <T> T getNullInstanceOf(Class<T> type) throws
InvocationTargetException, InstantiationException,
IllegalAccessException, NoSuchMethodException, ClassNotFoundException {
Class<?> clazz = Class.forName(type.getName());
Constructor<?> ctor = clazz.getConstructor();
ctor.setAccessible(true);
return (T) ctor.newInstance();
}
private static List<ImmutablePair<Class<?>, ?>> primitiveTypes = new ArrayList<>() {{
add(ImmutablePair.of(Integer.TYPE, 2));
add(ImmutablePair.of(Double.TYPE, 3.0));
add(ImmutablePair.of(Float.TYPE, 4.0f));
add(ImmutablePair.of(Long.TYPE, 5L));
add(ImmutablePair.of(Boolean.TYPE, true));
add(ImmutablePair.of(Integer.class, 2));
add(ImmutablePair.of(Double.class, 3.0));
add(ImmutablePair.of(Float.class, 4.0));
add(ImmutablePair.of(Long.class, 5L));
add(ImmutablePair.of(Boolean.class, true));
add(ImmutablePair.of(String.class, "string"));
}};
private static <T> boolean isPrimitive(Class<T> type) {
return primitiveTypes.stream().map(ImmutablePair::getLeft).anyMatch(val -> val.equals(type));
}
private static <T> Object generatePrimitive(Class<T> type) {
for (ImmutablePair<Class<?>, ?> primitive : primitiveTypes) {
if (type.equals(primitive.left)) {
return primitive.right;
}
}
return null;
}
public static boolean hasNoNulls(Object a) throws IllegalAccessException {
recurse++;
if (a == null) {
recurse--;
log.error("Is null:" + a + ":" + a.getClass());
return false;
}
if (isClassCollection(a.getClass())) {
for (Object e : (Collection) a) {
try {
boolean b = hasNoNulls(e);
if (!b) {
log.error("Has null:" + e + ":" + e.getClass());
return false;
}
} catch (IllegalAccessException illegalAccessException) {
log.error(Arrays.toString(illegalAccessException.getStackTrace()));
}
}
return true;
} else if (isMapCollection(a.getClass())) {
boolean b = hasNoNulls(((Map) a).values());
recurse--;
if (!b) {
log.error("Map Has null:" + ((Map) a).values());
}
return b;
}
for (Field f : a.getClass().getDeclaredFields()) {
f.setAccessible(true);
Object member = f.get(a);
if (member == null) {
recurse--;
log.error("Member was null:" + f + ":" + f.getType());
return false;
} else if (isWrapperType(member.getClass()) || member.getClass().isPrimitive() || isFinal(f) || f.getType().equals(SpecificData.class)) {
continue;
} else if (!f.getType().equals(a.getClass())) {
boolean b = hasNoNulls(member);
if (!b) {
recurse--;
log.error("Object has null:" + member + ":" + member.getClass());
return false;
}
}
}
recurse--;
return true;
}
public static boolean isFinal(Field f) {
if (Modifier.isFinal(f.getModifiers())) {
return true;
}
return false;
}
public static boolean isWrapperType(Class<?> clazz) {
return getWrapperTypes().contains(clazz);
}
private static Set<Class<?>> getWrapperTypes() {
Set<Class<?>> ret = new HashSet<Class<?>>();
ret.add(Boolean.class);
ret.add(Character.class);
ret.add(Byte.class);
ret.add(Short.class);
ret.add(Integer.class);
ret.add(Long.class);
ret.add(Float.class);
ret.add(Double.class);
ret.add(Void.class);
ret.add(String.class);
return ret;
}
public static <T> T object(Class<T> type) throws NoSuchMethodException, ClassNotFoundException,
InvocationTargetException, InstantiationException, IllegalAccessException {
if (recursionCheck.contains(type)) {
log.info("Infinite recursion blocked");
return null;
}
if (isPrimitive(type)) {
return (T) generatePrimitive(type);
}
// add current type to recursion check
recursionCheck.add(type);
T object;
try {
object = getNullInstanceOf(type);
} catch (NoSuchMethodException e) {
log.info("Can not call constructor for:{}", type);
recursionCheck.remove(type);
return null;
}
Field[] fields = object.getClass().getDeclaredFields();
for (Field f : fields) {
if (isFinal(f)) {
continue;
}
boolean numeric = false;
f.setAccessible(true);
if (f.getName().toLowerCase().contains("rate") ||
f.getName().toLowerCase().contains("hour") ||
f.getName().toLowerCase().contains("number")) {
numeric = true;
}
try {
if (f.getType().equals(CharSequence.class) || f.getType().equals(String.class)) {
f.set(object, f.getName());
if (numeric) {
f.set(object, "2.0");
numeric = false;
}
} else if (f.getType().equals(UUID.class)) {
f.set(object, UUID.randomUUID());
} else if (isClassCollection(f.getType()) && !recursionCheck.contains(f.getType())) {
ArrayList list = new ArrayList();
list.add(object(getGenericType(f)));
f.set(object, list);
} else if (isMapCollection(f.getType())) {
HashMap map = new HashMap();
f.set(object, map);
} else if (!recursionCheck.contains(f.getType())) {
// keep track of infinite recursion
f.set(object, object(f.getType()));
} else {
if (recursionCheck.contains(f.getType())) {
log.info("Infinite recursion detected:" + recursionCheck);
}
log.info("No implementation for: " + f.getName() + ":" + f.getType());
continue;
}
} catch (Exception e) {
log.error("Failed:" + e.getMessage() + ":" + f.getName() + ":" + f.getType());
continue;
}
}
recursionCheck.remove(type);
return object;
}
public static <T> T setIfBuiltIn(Class<T> type) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
if (type.equals(CharSequence.class) || type.equals(String.class) ||
type.equals(Integer.TYPE) || type.equals(Integer.class) ||
type.equals(Double.TYPE) || type.equals(Double.class) ||
type.equals(Long.TYPE) || type.equals(Long.class) ||
type.equals(Boolean.TYPE) || type.equals(Boolean.class) || type.equals(UUID.class)) {
return getNullInstanceOf(type);
}
return null;
}
public static Class<?> getGenericType(Field f) {
ParameterizedType pType = (ParameterizedType) f.getGenericType();
Class<?> type = (Class<?>) pType.getActualTypeArguments()[0];
return type;
}
public static boolean isClassCollection(Class<?> c) {
return Collection.class.isAssignableFrom(c);
}
public static boolean isMapCollection(Class<?> c) {
return Map.class.isAssignableFrom(c);
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.