com.dslplatform.json.runtime.EnumAnalyzer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dsl-json-java8 Show documentation
Show all versions of dsl-json-java8 Show documentation
DSL Platform compatible Java JSON library (https://dsl-platform.com)
The newest version!
package com.dslplatform.json.runtime;
import com.dslplatform.json.DslJson;
import com.dslplatform.json.Nullable;
import java.lang.reflect.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
public abstract class EnumAnalyzer {
public static final DslJson.ConverterFactory CONVERTER = new DslJson.ConverterFactory() {
@Nullable
@Override
public EnumDescription tryCreate(Type manifest, DslJson dslJson) {
if (manifest instanceof Class> && ((Class>) manifest).isEnum()) {
return analyze(manifest, (Class) manifest, dslJson);
}
if (manifest instanceof ParameterizedType) {
final ParameterizedType pt = (ParameterizedType) manifest;
if (pt.getActualTypeArguments().length == 1
&& ((Class>) pt.getRawType()).isEnum()) {
return analyze(manifest, (Class) pt.getRawType(), dslJson);
}
}
return null;
}
};
@Nullable
private static EnumDescription analyze(final Type manifest, final Class raw, final DslJson json) {
if (raw.isArray()
|| Collection.class.isAssignableFrom(raw)
|| (raw.getModifiers() & Modifier.ABSTRACT) != 0
|| (raw.getDeclaringClass() != null && (raw.getModifiers() & Modifier.STATIC) == 0)) {
return null;
}
final EnumDescription converter = new EnumDescription<>(raw, raw.getEnumConstants());
json.registerWriter(manifest, converter);
json.registerReader(manifest, converter);
return converter;
}
}