public static boolean isSpatial(final NumericDimensionField<?> d) { final Class<?> commonIndexType = GenericTypeResolver.resolveTypeArgument(d.getClass(), NumericDimensionField.class); return GeometryWrapper.class.isAssignableFrom(commonIndexType); }
@Override protected void setupMapper(final Job job) { job.setMapperClass(IntermediateKeyValueMapper.class); final Class<?>[] genericClasses = GenericTypeResolver.resolveTypeArguments(ingestPlugin.getClass(), IngestWithReducer.class); // set mapper output info job.setMapOutputKeyClass(genericClasses[1]); job.setMapOutputValueClass(genericClasses[2]); }
/** * Resolve the type arguments of the given generic interface against the given target class which * is assumed to implement the generic interface and possibly declare concrete types for its type * variables. * * @param clazz the target class to check against * @param genericIfc the generic interface or superclass to resolve the type argument from * @return the resolved type of each argument, with the array size matching the number of actual * type arguments, or <code>null</code> if not resolvable */ public static Class<?>[] resolveTypeArguments(Class<?> clazz, Class<?> genericIfc) { return doResolveTypeArguments(clazz, clazz, genericIfc); }
/** Extract a class instance from given Type. */ private static Class<?> extractClass(Class<?> ownerClass, Type arg) { if (arg instanceof ParameterizedType) { return extractClass(ownerClass, ((ParameterizedType) arg).getRawType()); } else if (arg instanceof GenericArrayType) { GenericArrayType gat = (GenericArrayType) arg; Type gt = gat.getGenericComponentType(); Class<?> componentClass = extractClass(ownerClass, gt); return Array.newInstance(componentClass, 0).getClass(); } else if (arg instanceof TypeVariable) { TypeVariable<?> tv = (TypeVariable<?>) arg; arg = getTypeVariableMap(ownerClass).get(tv); if (arg == null) { arg = extractBoundForTypeVariable(tv); } else { arg = extractClass(ownerClass, arg); } } return (arg instanceof Class ? (Class<?>) arg : Object.class); }
private static void extractTypeVariablesFromGenericInterfaces( Type[] genericInterfaces, Map<TypeVariable<?>, Type> typeVariableMap) { for (Type genericInterface : genericInterfaces) { if (genericInterface instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) genericInterface; populateTypeMapFromParameterizedType(pt, typeVariableMap); if (pt.getRawType() instanceof Class) { extractTypeVariablesFromGenericInterfaces( ((Class<?>) pt.getRawType()).getGenericInterfaces(), typeVariableMap); } } else if (genericInterface instanceof Class) { extractTypeVariablesFromGenericInterfaces( ((Class<?>) genericInterface).getGenericInterfaces(), typeVariableMap); } } }
private static Class<?>[] doResolveTypeArguments( Class<?> ownerClass, Type ifc, Class<?> genericIfc) { if (ifc instanceof ParameterizedType) { ParameterizedType paramIfc = (ParameterizedType) ifc; Type rawType = paramIfc.getRawType(); if (genericIfc.equals(rawType)) { Type[] typeArgs = paramIfc.getActualTypeArguments(); Class<?>[] result = new Class[typeArgs.length]; for (int i = 0; i < typeArgs.length; i++) { Type arg = typeArgs[i]; result[i] = extractClass(ownerClass, arg); } return result; } else if (genericIfc.isAssignableFrom((Class<?>) rawType)) { return doResolveTypeArguments(ownerClass, (Class<?>) rawType, genericIfc); } } else if (ifc != null && genericIfc.isAssignableFrom((Class<?>) ifc)) { return doResolveTypeArguments(ownerClass, (Class<?>) ifc, genericIfc); } return null; }
/** Extracts the bound <code>Type</code> for a given {@link TypeVariable}. */ static Type extractBoundForTypeVariable(TypeVariable<?> typeVariable) { Type[] bounds = typeVariable.getBounds(); if (bounds.length == 0) { return Object.class; } Type bound = bounds[0]; if (bound instanceof TypeVariable) { bound = extractBoundForTypeVariable((TypeVariable<?>) bound); } return bound; }
/** * Resolve the specified generic type against the given TypeVariable map. * * @param genericType the generic type to resolve * @param typeVariableMap the TypeVariable Map to resolved against * @return the type if it resolves to a Class, or <code>Object.class</code> otherwise */ public static Class<?> resolveType(Type genericType, Map<TypeVariable<?>, Type> typeVariableMap) { Type rawType = getRawType(genericType, typeVariableMap); return (rawType instanceof Class ? (Class<?>) rawType : Object.class); }
extractTypeVariablesFromGenericInterfaces(clazz.getGenericInterfaces(), typeVariableMap); if (genericType instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) genericType; populateTypeMapFromParameterizedType(pt, typeVariableMap); extractTypeVariablesFromGenericInterfaces(type.getGenericInterfaces(), typeVariableMap); genericType = type.getGenericSuperclass(); type = type.getSuperclass(); if (genericType instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) genericType; populateTypeMapFromParameterizedType(pt, typeVariableMap);
/** * Determine the raw type for the given generic parameter type. * * @param genericType the generic type to resolve * @param typeVariableMap the TypeVariable Map to resolved against * @return the resolved raw type */ static Type getRawType(Type genericType, Map<TypeVariable<?>, Type> typeVariableMap) { Type resolvedType = genericType; if (genericType instanceof TypeVariable) { TypeVariable<?> tv = (TypeVariable<?>) genericType; resolvedType = typeVariableMap.get(tv); if (resolvedType == null) { resolvedType = extractBoundForTypeVariable(tv); } } if (resolvedType instanceof ParameterizedType) { return ((ParameterizedType) resolvedType).getRawType(); } else { return resolvedType; } }
GenericTypeResolver.resolveTypeArgument(reader.getClass(), FieldReader.class), result.size()); return result.toArray(resultArray);
/** * Resolve the single type argument of the given generic interface against the given target class * which is assumed to implement the generic interface and possibly declare a concrete type for * its type variable. * * @param clazz the target class to check against * @param genericIfc the generic interface or superclass to resolve the type argument from * @return the resolved type of the argument, or <code>null</code> if not resolvable */ public static Class<?> resolveTypeArgument(Class<?> clazz, Class<?> genericIfc) { Class<?>[] typeArgs = resolveTypeArguments(clazz, genericIfc); if (typeArgs == null) { return null; } if (typeArgs.length != 1) { throw new IllegalArgumentException( "Expected 1 type argument on generic interface [" + genericIfc.getName() + "] but found " + typeArgs.length); } return typeArgs[0]; }
Type resolvedType = typeVariableMap.get(typeVariableArgument); if (resolvedType == null) { resolvedType = extractBoundForTypeVariable(typeVariableArgument);
private static Class<?>[] doResolveTypeArguments( Class<?> ownerClass, Class<?> classToIntrospect, Class<?> genericIfc) { while (classToIntrospect != null) { if (genericIfc.isInterface()) { Type[] ifcs = classToIntrospect.getGenericInterfaces(); for (Type ifc : ifcs) { Class<?>[] result = doResolveTypeArguments(ownerClass, ifc, genericIfc); if (result != null) { return result; } } } else { Class<?>[] result = doResolveTypeArguments( ownerClass, classToIntrospect.getGenericSuperclass(), genericIfc); if (result != null) { return result; } } classToIntrospect = classToIntrospect.getSuperclass(); } return null; }
@SuppressWarnings("unchecked") protected FieldType[] readVariableSizeField( final byte[] fieldData, final SerializationHelper<FieldType> serializationHelper) { if ((fieldData == null) || (fieldData.length == 0)) { return null; } final List<FieldType> result = new ArrayList<FieldType>(); final ByteBuffer buff = ByteBuffer.wrap(fieldData); // this would be bad if (buff.get() != Encoding.VARIABLE_SIZE_ENCODING.getByteEncoding()) { return null; } while (buff.remaining() >= 1) { final int size = serializationHelper.readUnsignedInt(buff); if (size > 0) { final byte[] bytes = new byte[size]; buff.get(bytes); result.add(serializationHelper.readField(reader, bytes)); } else { result.add(null); } } final FieldType[] resultArray = (FieldType[]) Array.newInstance( GenericTypeResolver.resolveTypeArgument(reader.getClass(), FieldReader.class), result.size()); return result.toArray(resultArray); }
GenericTypeResolver.resolveTypeArguments( indexHandler.getClass(), IndexFieldHandler.class)[1], GenericTypeResolver.resolveTypeArguments( defaultFieldHandler.getClass(), IndexFieldHandler.class)[1];
/** * Get index field handler for the provided dimension. * * @param dimension * @return field handler */ public IndexFieldHandler<T, ? extends CommonIndexValue, Object> getFieldHandler( final NumericDimensionField<? extends CommonIndexValue> dimension) { // first try explicit dimension matching IndexFieldHandler<T, ? extends CommonIndexValue, Object> fieldHandler = fieldNameMatchingFieldHandlers.get(dimension.getFieldName()); if (fieldHandler == null) { // if that fails, go for type matching fieldHandler = FieldUtils.getAssignableValueFromClassMap( GenericTypeResolver.resolveTypeArgument( dimension.getClass(), NumericDimensionField.class), typeMatchingFieldHandlers); fieldNameMatchingFieldHandlers.put(dimension.getFieldName(), fieldHandler); } return fieldHandler; }
if (serializationProvider != null) { final Class<?> type = GenericTypeResolver.resolveTypeArgument( serializationProvider.getClass(), FieldSerializationProviderSpi.class);