use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector in project cdap by caskdata.
the class ObjectInspectorFactory method getReflectionObjectInspectorNoCache.
private static ObjectInspector getReflectionObjectInspectorNoCache(Type t) {
if (t instanceof GenericArrayType) {
GenericArrayType at = (GenericArrayType) t;
return getStandardListObjectInspector(getReflectionObjectInspector(at.getGenericComponentType()));
}
Map<TypeVariable, Type> genericTypes = null;
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
Type rawType = pt.getRawType();
// Collection?
if (Collection.class.isAssignableFrom((Class<?>) rawType)) {
return getStandardListObjectInspector(getReflectionObjectInspector(pt.getActualTypeArguments()[0]));
}
// Map?
if (Map.class.isAssignableFrom((Class<?>) rawType)) {
return getStandardMapObjectInspector(getReflectionObjectInspector(pt.getActualTypeArguments()[0]), getReflectionObjectInspector(pt.getActualTypeArguments()[1]));
}
// Otherwise convert t to RawType so we will fall into the following if block.
t = rawType;
ImmutableMap.Builder<TypeVariable, Type> builder = ImmutableMap.builder();
for (int i = 0; i < pt.getActualTypeArguments().length; i++) {
builder.put(((Class<?>) t).getTypeParameters()[i], pt.getActualTypeArguments()[i]);
}
genericTypes = builder.build();
}
// Must be a class.
if (!(t instanceof Class)) {
throw new RuntimeException(ObjectInspectorFactory.class.getName() + " internal error:" + t);
}
Class<?> c = (Class<?>) t;
// Java Primitive Type?
if (PrimitiveObjectInspectorUtils.isPrimitiveJavaType(c)) {
return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveJavaType(c).primitiveCategory);
}
// Java Primitive Class?
if (PrimitiveObjectInspectorUtils.isPrimitiveJavaClass(c)) {
return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveJavaClass(c).primitiveCategory);
}
// Primitive Writable class?
if (PrimitiveObjectInspectorUtils.isPrimitiveWritableClass(c)) {
return PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveWritableClass(c).primitiveCategory);
}
// Enum class?
if (Enum.class.isAssignableFrom(c)) {
return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.STRING);
}
// Array
if (c.isArray()) {
return getStandardListObjectInspector(getReflectionObjectInspector(c.getComponentType()));
}
// Must be struct because List and Map need to be ParameterizedType
Preconditions.checkState(!List.class.isAssignableFrom(c));
Preconditions.checkState(!Map.class.isAssignableFrom(c));
Preconditions.checkState(!c.isInterface(), "Cannot inspect an interface.");
ReflectionStructObjectInspector oi = new ReflectionStructObjectInspector();
// put it into the cache BEFORE it is initialized to make sure we can catch
// recursive types.
objectInspectorCache.put(t, oi);
Field[] fields = ObjectInspectorUtils.getDeclaredNonStaticFields(c);
List<ObjectInspector> structFieldObjectInspectors = new ArrayList<>(fields.length);
for (Field field : fields) {
// "this" pointer present in nested classes and that references the parent.
if (Modifier.isTransient(field.getModifiers()) || field.isSynthetic()) {
continue;
}
if (!oi.shouldIgnoreField(field.getName())) {
Type newType = field.getGenericType();
if (newType instanceof TypeVariable) {
Preconditions.checkNotNull(genericTypes, "Type was not recognized as a parameterized type.");
Preconditions.checkNotNull(genericTypes.get(newType), "Generic type " + newType + " not a parameter of class " + c);
newType = genericTypes.get(newType);
}
structFieldObjectInspectors.add(getReflectionObjectInspector(newType));
}
}
oi.init(c, structFieldObjectInspectors);
return oi;
}
Aggregations