Search in sources :

Example 96 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project neo4j by neo4j.

the class PluginPointFactoryImpl method parameterExtractor.

private static ParameterExtractor parameterExtractor(Type type, Parameter parameter, Description description) {
    if (type instanceof ParameterizedType) {
        ParameterizedType paramType = (ParameterizedType) type;
        Class<?> raw = (Class<?>) paramType.getRawType();
        Type componentType = paramType.getActualTypeArguments()[0];
        Class<?> component = null;
        if (componentType instanceof Class<?>) {
            component = (Class<?>) componentType;
        }
        if (Set.class == raw) {
            TypeCaster caster = TYPES.get(component);
            if (caster != null) {
                return new ListParameterExtractor(caster, component, parameter, description) {

                    @Override
                    Object convert(Object[] result) {
                        return new HashSet<Object>(Arrays.asList(result));
                    }
                };
            }
        } else if (List.class == raw || Collection.class == raw || Iterable.class == raw) {
            TypeCaster caster = TYPES.get(component);
            if (caster != null) {
                return new ListParameterExtractor(caster, component, parameter, description) {

                    @Override
                    Object convert(Object[] result) {
                        return Arrays.asList(result);
                    }
                };
            }
        }
    } else if (type instanceof Class<?>) {
        Class<?> raw = (Class<?>) type;
        if (raw.isArray()) {
            TypeCaster caster = TYPES.get(raw.getComponentType());
            if (caster != null) {
                return new ListParameterExtractor(caster, raw.getComponentType(), parameter, description) {

                    @Override
                    Object convert(Object[] result) {
                        return result;
                    }
                };
            }
        } else {
            TypeCaster caster = TYPES.get(raw);
            if (caster != null) {
                return new ParameterExtractor(caster, raw, parameter, description);
            }
        }
    } else if (type instanceof GenericArrayType) {
        GenericArrayType array = (GenericArrayType) type;
        Type component = array.getGenericComponentType();
        if (component instanceof Class<?>) {
            TypeCaster caster = TYPES.get(component);
            if (caster != null) {
                return new ListParameterExtractor(caster, (Class<?>) component, parameter, description) {

                    @Override
                    Object convert(Object[] result) {
                        return result;
                    }
                };
            }
        }
    }
    throw new IllegalStateException("Unsupported parameter type: " + type);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) RelationshipType(org.neo4j.graphdb.RelationshipType) Collection(java.util.Collection) List(java.util.List) HashSet(java.util.HashSet)

Example 97 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project jodd by oblac.

the class ReflectUtil method typeToString.

private static void typeToString(StringBuilder sb, Type type, Set<Type> visited) {
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        final Class<?> rawType = (Class<?>) parameterizedType.getRawType();
        sb.append(rawType.getName());
        boolean first = true;
        for (Type typeArg : parameterizedType.getActualTypeArguments()) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }
            sb.append('<');
            typeToString(sb, typeArg, visited);
            sb.append('>');
        }
    } else if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        sb.append('?');
        // According to JLS(http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.5.1):
        // - Lower and upper can't coexist: (for instance, this is not allowed: <? extends List<String> & super MyInterface>)
        // - Multiple bounds are not supported (for instance, this is not allowed: <? extends List<String> & MyInterface>)
        final Type bound;
        if (wildcardType.getLowerBounds().length != 0) {
            sb.append(" super ");
            bound = wildcardType.getLowerBounds()[0];
        } else {
            sb.append(" extends ");
            bound = wildcardType.getUpperBounds()[0];
        }
        typeToString(sb, bound, visited);
    } else if (type instanceof TypeVariable<?>) {
        TypeVariable<?> typeVariable = (TypeVariable<?>) type;
        sb.append(typeVariable.getName());
        if (!visited.contains(type)) {
            visited.add(type);
            sb.append(" extends ");
            boolean first = true;
            for (Type bound : typeVariable.getBounds()) {
                if (first) {
                    first = false;
                } else {
                    sb.append(" & ");
                }
                typeToString(sb, bound, visited);
            }
            visited.remove(type);
        }
    } else if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = (GenericArrayType) type;
        typeToString(genericArrayType.getGenericComponentType());
        sb.append(genericArrayType.getGenericComponentType());
        sb.append("[]");
    } else if (type instanceof Class) {
        Class<?> typeClass = (Class<?>) type;
        sb.append(typeClass.getName());
    } else {
        throw new IllegalArgumentException("Unsupported type: " + type);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 98 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project jodd by oblac.

the class ReflectUtil method getRawType.

/**
	 * Returns raw class for given <code>type</code> when implementation class is known
	 * and it makes difference.
	 * @see #resolveVariable(java.lang.reflect.TypeVariable, Class)
	 */
public static Class<?> getRawType(Type type, Class implClass) {
    if (type instanceof Class) {
        return (Class) type;
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) type;
        return getRawType(pType.getRawType(), implClass);
    }
    if (type instanceof WildcardType) {
        WildcardType wType = (WildcardType) type;
        Type[] lowerTypes = wType.getLowerBounds();
        if (lowerTypes.length > 0) {
            return getRawType(lowerTypes[0], implClass);
        }
        Type[] upperTypes = wType.getUpperBounds();
        if (upperTypes.length != 0) {
            return getRawType(upperTypes[0], implClass);
        }
        return Object.class;
    }
    if (type instanceof GenericArrayType) {
        Type genericComponentType = ((GenericArrayType) type).getGenericComponentType();
        Class<?> rawType = getRawType(genericComponentType, implClass);
        // this is sort of stupid, but there seems no other way (consider don't creating new instances each time)...
        return Array.newInstance(rawType, 0).getClass();
    }
    if (type instanceof TypeVariable) {
        TypeVariable<?> varType = (TypeVariable<?>) type;
        if (implClass != null) {
            Type resolvedType = resolveVariable(varType, implClass);
            if (resolvedType != null) {
                return getRawType(resolvedType, null);
            }
        }
        Type[] boundsTypes = varType.getBounds();
        if (boundsTypes.length == 0) {
            return Object.class;
        }
        return getRawType(boundsTypes[0], implClass);
    }
    return null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) AccessibleObject(java.lang.reflect.AccessibleObject) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 99 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project jodd by oblac.

the class ReflectUtil method getComponentTypes.

/**
	 * Returns all component types of the given type.
	 * For example the following types all have the
	 * component-type MyClass:
	 * <ul>
	 * <li>MyClass[]</li>
	 * <li>List&lt;MyClass&gt;</li>
	 * <li>Foo&lt;? extends MyClass&gt;</li>
	 * <li>Bar&lt;? super MyClass&gt;</li>
	 * <li>&lt;T extends MyClass&gt; T[]</li>
	 * </ul>
	 */
public static Class[] getComponentTypes(Type type, Class implClass) {
    if (type instanceof Class) {
        Class clazz = (Class) type;
        if (clazz.isArray()) {
            return new Class[] { clazz.getComponentType() };
        }
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        Type[] generics = pt.getActualTypeArguments();
        if (generics.length == 0) {
            return null;
        }
        Class[] types = new Class[generics.length];
        for (int i = 0; i < generics.length; i++) {
            types[i] = getRawType(generics[i], implClass);
        }
        return types;
    } else if (type instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) type;
        Class rawType = getRawType(gat.getGenericComponentType(), implClass);
        if (rawType == null) {
            return null;
        }
        return new Class[] { rawType };
    }
    return null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 100 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project orientdb by orientechnologies.

the class OObjectSerializerHelper method convertInObject.

public static Object convertInObject(final Object iPojo, final String iField, final Object iValue, final Class<?> parameterType) {
    // New conversion method working with OLazyObjectList
    if (!(iValue instanceof OObjectLazyList<?>))
        return OType.convert(iValue, parameterType);
    List<Object> aSubList = null;
    try {
        final Field aField = OObjectSerializerHelper.getField(iPojo, iField);
        final Class<?> listClass = aField.getType();
        final ParameterizedType aType = (ParameterizedType) aField.getGenericType();
        final Class<?> objectClass = (Class<?>) aType.getActualTypeArguments()[0];
        final OObjectLazyList<?> aList = (OObjectLazyList<?>) iValue;
        // Instantiation of the list
        if (listClass.isInterface()) {
            aSubList = new ArrayList<Object>();
        } else {
            aSubList = (List<Object>) listClass.newInstance();
        }
        for (final Object value : aList) {
            if (value instanceof ODocument) {
                final ODocument aDocument = (ODocument) value;
                aSubList.add(OObjectSerializerHelper.convertDocumentInType(aDocument, objectClass));
            } else {
                aSubList.add(value);
            }
        }
    } catch (Exception e) {
        OLogManager.instance().error(null, "Error on convertInObject()", e);
    }
    return aSubList;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) OObjectLazyList(com.orientechnologies.orient.object.db.OObjectLazyList) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OException(com.orientechnologies.common.exception.OException) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) OObjectNotDetachedException(com.orientechnologies.orient.object.db.OObjectNotDetachedException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

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