static Type arrayType(Type type) { if (type instanceof Class) { Class clazz = (Class) type; // REVIEW: Is there a way to do this without creating an instance? // We just need the inverse of Class.getComponentType(). return Array.newInstance(clazz, 0).getClass(); } return new ArrayType(type); }
/** * Returns the element type of a {@link Collection}, {@link Iterable} * (including {@link org.apache.calcite.linq4j.Queryable Queryable} and * {@link org.apache.calcite.linq4j.Enumerable Enumerable}), {@link Iterator}, * {@link Enumerator}, or an array. * * <p>Returns null if the type is not one of these.</p> */ public static Type getElementType(Type type) { if (type instanceof ArrayType) { return ((ArrayType) type).getComponentType(); } if (type instanceof GenericArrayType) { return ((GenericArrayType) type).getGenericComponentType(); } Class clazz = toClass(type); if (clazz.isArray()) { return clazz.getComponentType(); } if (Collection.class.isAssignableFrom(clazz) || Iterable.class.isAssignableFrom(clazz) || Iterator.class.isAssignableFrom(clazz) || Enumerator.class.isAssignableFrom(clazz)) { if (type instanceof ParameterizedType) { return ((ParameterizedType) type).getActualTypeArguments()[0]; } return Object.class; } return null; }
final Types.ArrayType arrayType = (Types.ArrayType) type; final RelDataType componentRelType = createType(arrayType.getComponentType()); return createArrayType( createTypeWithNullability(componentRelType, arrayType.componentIsNullable()), arrayType.maximumCardinality());
final Types.ArrayType arrayType = (Types.ArrayType) type; final RelDataType componentRelType = createType(arrayType.getComponentType()); return createArrayType( createTypeWithNullability(componentRelType, arrayType.componentIsNullable()), arrayType.maximumCardinality());
static String className(Type type) { if (type instanceof ArrayType) { return className(((ArrayType) type).getComponentType()) + "[]"; } if (!(type instanceof Class)) { return type.toString(); } Class clazz = (Class) type; if (clazz.isArray()) { return className(clazz.getComponentType()) + "[]"; } String className = clazz.getName(); if (!clazz.isPrimitive() && clazz.getPackage() != null && clazz.getPackage().getName().equals("java.lang")) { return className.substring("java.lang.".length()); } return className.replace('$', '.'); }
/** Returns the type of a field. * * <p>Takes into account {@link org.apache.calcite.adapter.java.Array} * annotations if present. */ private Type fieldType(Field field) { final Class<?> klass = field.getType(); final org.apache.calcite.adapter.java.Array array = field.getAnnotation(org.apache.calcite.adapter.java.Array.class); if (array != null) { return new Types.ArrayType(array.component(), array.componentIsNullable(), array.maximumCardinality()); } final org.apache.calcite.adapter.java.Map map = field.getAnnotation(org.apache.calcite.adapter.java.Map.class); if (map != null) { return new Types.MapType(map.key(), map.keyIsNullable(), map.value(), map.valueIsNullable()); } return klass; }
/** * Returns the component type of an array. */ public static Type getComponentType(Type type) { if (type instanceof Class) { return ((Class) type).getComponentType(); } if (type instanceof ArrayType) { return ((ArrayType) type).getComponentType(); } if (type instanceof GenericArrayType) { return ((GenericArrayType) type).getGenericComponentType(); } if (type instanceof ParameterizedType) { return getComponentType(((ParameterizedType) type).getRawType()); } if (type instanceof TypeVariable) { TypeVariable typeVariable = (TypeVariable) type; return getComponentType(typeVariable.getBounds()[0]); } return null; // not an array type }
/** Returns the type of a field. * * <p>Takes into account {@link org.apache.calcite.adapter.java.Array} * annotations if present. */ private Type fieldType(Field field) { final Class<?> klass = field.getType(); final org.apache.calcite.adapter.java.Array array = field.getAnnotation(org.apache.calcite.adapter.java.Array.class); if (array != null) { return new Types.ArrayType(array.component(), array.componentIsNullable(), array.maximumCardinality()); } final org.apache.calcite.adapter.java.Map map = field.getAnnotation(org.apache.calcite.adapter.java.Map.class); if (map != null) { return new Types.MapType(map.key(), map.keyIsNullable(), map.value(), map.valueIsNullable()); } return klass; }
public static Type stripGenerics(Type type) { if (type instanceof GenericArrayType) { final Type componentType = ((GenericArrayType) type).getGenericComponentType(); return new ArrayType(stripGenerics(componentType)); } else if (type instanceof ParameterizedType) { return ((ParameterizedType) type).getRawType(); } else { return type; } }