Search in sources :

Example 46 with Type

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);
}
Also used : Type(java.lang.reflect.Type) Test(org.junit.Test)

Example 47 with Type

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
    }
}
Also used : Comment(net.minecraftforge.common.config.Config.Comment) RangeDouble(net.minecraftforge.common.config.Config.RangeDouble) LangKey(net.minecraftforge.common.config.Config.LangKey) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Map(java.util.Map) RangeInt(net.minecraftforge.common.config.Config.RangeInt)

Example 48 with Type

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);
}
Also used : Function(org.qi4j.functional.Function) Annotations.isType(org.qi4j.api.util.Annotations.isType) Type(java.lang.reflect.Type) Classes.wrapperClass(org.qi4j.api.util.Classes.wrapperClass) SideEffects(org.qi4j.api.sideeffect.SideEffects)

Example 49 with Type

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);
}
Also used : Function(org.qi4j.functional.Function) Annotations.isType(org.qi4j.api.util.Annotations.isType) Type(java.lang.reflect.Type) Constraints(org.qi4j.api.constraint.Constraints) Constraint(org.qi4j.api.constraint.Constraint) Classes.wrapperClass(org.qi4j.api.util.Classes.wrapperClass)

Example 50 with Type

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);
    }
}
Also used : Type(java.lang.reflect.Type) GenericAssociationInfo(org.qi4j.api.association.GenericAssociationInfo) TypeVariable(java.lang.reflect.TypeVariable) QualifiedName(org.qi4j.api.common.QualifiedName) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) AccessibleObject(java.lang.reflect.AccessibleObject) Member(java.lang.reflect.Member)

Aggregations

Type (java.lang.reflect.Type)6423 ParameterizedType (java.lang.reflect.ParameterizedType)1761 ProgressRequestBody (io.kubernetes.client.ProgressRequestBody)722 ProgressResponseBody (io.kubernetes.client.ProgressResponseBody)722 GenericArrayType (java.lang.reflect.GenericArrayType)690 WildcardType (java.lang.reflect.WildcardType)571 Test (org.junit.Test)512 ArrayList (java.util.ArrayList)427 Method (java.lang.reflect.Method)416 TypeVariable (java.lang.reflect.TypeVariable)337 List (java.util.List)335 Map (java.util.Map)289 Gson (com.google.gson.Gson)231 V1Status (io.kubernetes.client.models.V1Status)228 V1Status (io.kubernetes.client.openapi.models.V1Status)224 HashMap (java.util.HashMap)204 Field (java.lang.reflect.Field)163 Annotation (java.lang.annotation.Annotation)160 IOException (java.io.IOException)140 Collection (java.util.Collection)111