Search in sources :

Example 26 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method processPropertyTypeForQNames.

private void processPropertyTypeForQNames(PropertyDescriptor pType, Map<QualifiedName, QNameInfo> qNameInfos, Set<QualifiedName> newQNames, List<CompositeDescriptorInfo> vDescriptors, Set<String> enumValues, Boolean setQNameTableNameToNull) {
    QualifiedName qName = pType.qualifiedName();
    if (!newQNames.contains(qName) && !qName.name().equals(Identity.class.getName())) {
        newQNames.add(qName);
        // System.out.println("QName: " + qName + ", hc: " + qName.hashCode());
        QNameInfo info = qNameInfos.get(qName);
        if (info == null) {
            info = QNameInfo.fromProperty(//
            qName, setQNameTableNameToNull ? null : (QNAME_TABLE_NAME_PREFIX + qNameInfos.size()), //
            pType);
            qNameInfos.put(qName, info);
        }
        Type vType = info.getFinalType();
        while (vType instanceof ParameterizedType) {
            vType = ((ParameterizedType) vType).getRawType();
        }
        if (//
        vType instanceof Class<?>) {
            final Class<?> vTypeClass = (Class<?>) vType;
            if (((Class<?>) vType).isInterface()) {
                for (CompositeDescriptorInfo descInfo : vDescriptors) {
                    CompositeDescriptor desc = descInfo.composite;
                    if (desc instanceof ValueDescriptor) {
                        ValueDescriptor vDesc = (ValueDescriptor) desc;
                        // other Serializable
                        if (Iterables.matchesAny(new Specification<Class<?>>() {

                            @Override
                            public boolean satisfiedBy(Class<?> item) {
                                return vTypeClass.isAssignableFrom(item);
                            }
                        }, vDesc.types())) {
                            for (PropertyDescriptor subPDesc : vDesc.state().properties()) {
                                //
                                this.processPropertyTypeForQNames(//
                                subPDesc, //
                                qNameInfos, //
                                newQNames, //
                                vDescriptors, //
                                enumValues, //
                                setQNameTableNameToNull);
                            }
                        }
                    }
                }
            } else if (Enum.class.isAssignableFrom((Class<?>) vType)) {
                for (Object value : ((Class<?>) vType).getEnumConstants()) {
                    enumValues.add(QualifiedName.fromClass((Class<?>) vType, value.toString()).toString());
                }
            }
        }
    }
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) QualifiedName(org.qi4j.api.common.QualifiedName) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) ParameterizedType(java.lang.reflect.ParameterizedType) QNameType(org.qi4j.index.sql.support.common.QNameInfo.QNameType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ObjectType(org.sql.generation.api.grammar.manipulation.ObjectType) SQLDataType(org.sql.generation.api.grammar.common.datatypes.SQLDataType) CompositeDescriptor(org.qi4j.api.composite.CompositeDescriptor) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo) Identity(org.qi4j.api.entity.Identity)

Example 27 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project qi4j-sdk by Qi4j.

the class ValueCompositeCxfType method isMap.

private boolean isMap(Type type) {
    if (isMapClass(type)) {
        return true;
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType param = (ParameterizedType) type;
        Type rawType = param.getRawType();
        if (isCollectionClass(rawType)) {
            return true;
        }
    }
    return false;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) CollectionType(org.apache.cxf.aegis.type.collection.CollectionType) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) MapType(org.apache.cxf.aegis.type.collection.MapType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) AegisType(org.apache.cxf.aegis.type.AegisType)

Example 28 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project qi4j-sdk by Qi4j.

the class ValueCompositeCxfType method isCollection.

private boolean isCollection(Type type) {
    if (isCollectionClass(type)) {
        return true;
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType param = (ParameterizedType) type;
        Type rawType = param.getRawType();
        if (isCollectionClass(rawType)) {
            return true;
        }
    }
    return false;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) CollectionType(org.apache.cxf.aegis.type.collection.CollectionType) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) MapType(org.apache.cxf.aegis.type.collection.MapType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) AegisType(org.apache.cxf.aegis.type.AegisType)

Example 29 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project MVCHelper by LuckyJayce.

the class JsonParser method getSuperclassTypeParameter.

private static Type getSuperclassTypeParameter(Class<?> subclass) {
    Type superclass = subclass.getGenericSuperclass();
    if (superclass instanceof Class) {
        throw new RuntimeException("Missing type parameter.");
    }
    ParameterizedType parameterized = (ParameterizedType) superclass;
    return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 30 with ParameterizedType

use of java.lang.reflect.ParameterizedType 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)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)605 Type (java.lang.reflect.Type)456 GenericArrayType (java.lang.reflect.GenericArrayType)203 WildcardType (java.lang.reflect.WildcardType)162 TypeVariable (java.lang.reflect.TypeVariable)126 ArrayList (java.util.ArrayList)79 Method (java.lang.reflect.Method)60 Test (org.junit.Test)55 List (java.util.List)45 Field (java.lang.reflect.Field)43 Map (java.util.Map)39 HashMap (java.util.HashMap)32 Collection (java.util.Collection)18 Annotation (java.lang.annotation.Annotation)13 ImmutableList (com.google.common.collect.ImmutableList)12 TypeLiteral (com.google.inject.TypeLiteral)12 HashSet (java.util.HashSet)11 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 GenericType (javax.ws.rs.core.GenericType)10 MediaType (javax.ws.rs.core.MediaType)10