use of java.lang.reflect.Type in project hadoop by apache.
the class TestEnumSetWritable method testAvroReflect.
@Test
public void testAvroReflect() throws Exception {
String schema = "{\"type\":\"array\",\"items\":{\"type\":\"enum\"," + "\"name\":\"TestEnumSet\"," + "\"namespace\":\"org.apache.hadoop.io.TestEnumSetWritable$\"," + "\"symbols\":[\"CREATE\",\"OVERWRITE\",\"APPEND\"]}," + "\"java-class\":\"org.apache.hadoop.io.EnumSetWritable\"}";
Type type = TestEnumSetWritable.class.getField("testField").getGenericType();
AvroTestUtil.testReflect(nonEmptyFlagWritable, type, schema);
}
use of java.lang.reflect.Type in project MinecraftForge by MinecraftForge.
the class ConfigManager method createConfig.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static void createConfig(String modid, String category, Configuration cfg, Class<?> ftype, Field f, Object instance) {
Property prop = null;
String comment = null;
Comment ca = f.getAnnotation(Comment.class);
if (ca != null)
comment = NEW_LINE.join(ca.value());
String langKey = modid + "." + category + "." + f.getName().toLowerCase(Locale.ENGLISH);
LangKey la = f.getAnnotation(LangKey.class);
if (la != null)
langKey = la.value();
ITypeAdapter adapter = ADAPTERS.get(ftype);
if (adapter != null) {
if (category.isEmpty())
throw new RuntimeException("Can not specify a primitive field when the category is empty: " + f.getDeclaringClass() + "/" + f.getName());
prop = adapter.getProp(cfg, category, f, instance, comment);
set(instance, f, adapter.getValue(prop));
} else if (ftype.getSuperclass() == Enum.class) {
if (category.isEmpty())
throw new RuntimeException("Can not specify a primitive field when the category is empty: " + f.getDeclaringClass() + "/" + f.getName());
Enum enu = (Enum) get(instance, f);
prop = cfg.get(category, getName(f), enu.name(), comment);
prop.setValidationPattern(makePattern((Class<? extends Enum>) ftype));
set(instance, f, Enum.valueOf((Class<? extends Enum>) ftype, prop.getString()));
} else if (ftype == Map.class) {
if (category.isEmpty())
throw new RuntimeException("Can not specify a primitive field when the category is empty: " + f.getDeclaringClass() + "/" + f.getName());
String sub = category + "." + getName(f).toLowerCase(Locale.ENGLISH);
Map<String, Object> m = (Map<String, Object>) get(instance, f);
ParameterizedType type = (ParameterizedType) f.getGenericType();
Type mtype = type.getActualTypeArguments()[1];
cfg.getCategory(sub).setComment(comment);
for (Entry<String, Object> e : m.entrySet()) {
ITypeAdapter.Map adpt = MAP_ADAPTERS.get(mtype);
if (adpt != null) {
prop = adpt.getProp(cfg, sub, e.getKey(), e.getValue());
} else if (mtype instanceof Class && ((Class<?>) mtype).getSuperclass() == Enum.class) {
prop = TypeAdapters.Str.getProp(cfg, sub, e.getKey(), ((Enum) e.getValue()).name());
prop.setValidationPattern(makePattern((Class<? extends Enum>) mtype));
} else
throw new RuntimeException("Unknown type in map! " + f.getDeclaringClass() + "/" + f.getName() + " " + mtype);
prop.setLanguageKey(langKey + "." + e.getKey().toLowerCase(Locale.ENGLISH));
}
prop = null;
} else if (//Only support classes that are one level below Object.
ftype.getSuperclass() == Object.class) {
String sub = (category.isEmpty() ? "" : category + ".") + getName(f).toLowerCase(Locale.ENGLISH);
cfg.getCategory(sub).setComment(comment);
Object sinst = get(instance, f);
for (Field sf : ftype.getDeclaredFields()) {
if (!Modifier.isPublic(sf.getModifiers()))
continue;
createConfig(modid, sub, cfg, sf.getType(), sf, sinst);
}
} else
throw new RuntimeException("Unknown type in config! " + f.getDeclaringClass() + "/" + f.getName() + " " + ftype);
if (prop != null) {
prop.setLanguageKey(langKey);
RangeInt ia = f.getAnnotation(RangeInt.class);
if (ia != null) {
prop.setMinValue(ia.min());
prop.setMaxValue(ia.max());
if (comment != null)
prop.setComment(NEW_LINE.join(new String[] { comment, "Min: " + ia.min(), "Max: " + ia.max() }));
else
prop.setComment(NEW_LINE.join(new String[] { "Min: " + ia.min(), "Max: " + ia.max() }));
}
RangeDouble da = f.getAnnotation(RangeDouble.class);
if (da != null) {
prop.setMinValue(da.min());
prop.setMaxValue(da.max());
if (comment != null)
prop.setComment(NEW_LINE.join(new String[] { comment, "Min: " + da.min(), "Max: " + da.max() }));
else
prop.setComment(NEW_LINE.join(new String[] { "Min: " + da.min(), "Max: " + da.max() }));
}
//TODO List length values
}
}
use of java.lang.reflect.Type in project qi4j-sdk by Qi4j.
the class CompositeAssemblyImpl method sideEffectDeclarations.
protected Iterable<Class<?>> sideEffectDeclarations(ArrayList<Type> allTypes) {
// Find all side-effects and flattern them into an iterable
Function<Type, Iterable<Class<?>>> function = new Function<Type, Iterable<Class<?>>>() {
@Override
public Iterable<Class<?>> map(Type type) {
SideEffects sideEffects = Annotations.annotationOn(type, SideEffects.class);
if (sideEffects == null) {
return empty();
} else {
return iterable(sideEffects.value());
}
}
};
Iterable<Class<?>> flatten = flattenIterables(map(function, allTypes));
return toList(flatten);
}
use of java.lang.reflect.Type in project qi4j-sdk by Qi4j.
the class CompositeAssemblyImpl method constraintDeclarations.
private Iterable<Class<? extends Constraint<?, ?>>> constraintDeclarations(ArrayList<Type> allTypes) {
// Find all constraints and flatten them into an iterable
Function<Type, Iterable<Class<? extends Constraint<?, ?>>>> function = new Function<Type, Iterable<Class<? extends Constraint<?, ?>>>>() {
@Override
public Iterable<Class<? extends Constraint<?, ?>>> map(Type type) {
Constraints constraints = Annotations.annotationOn(type, Constraints.class);
if (constraints == null) {
return empty();
} else {
return iterable(constraints.value());
}
}
};
Iterable<Class<? extends Constraint<?, ?>>> flatten = flattenIterables(map(function, allTypes));
return toList(flatten);
}
use of java.lang.reflect.Type in project qi4j-sdk by Qi4j.
the class NamedAssociationModel method bind.
@Override
public void bind(Resolution resolution) throws BindingException {
builderInfo = new AssociationInfo() {
@Override
public boolean isImmutable() {
return false;
}
@Override
public QualifiedName qualifiedName() {
return qualifiedName;
}
@Override
public Type type() {
return type;
}
@Override
public void checkConstraints(Object value) throws ConstraintViolationException {
NamedAssociationModel.this.checkConstraints(value);
}
};
if (type instanceof TypeVariable) {
Class mainType = first(resolution.model().types());
type = Classes.resolveTypeVariable((TypeVariable) type, ((Member) accessor).getDeclaringClass(), mainType);
}
}
Aggregations