public boolean isMap() { Class<S> type = getType(); for (Class<?> mapType : MAP_TYPES) { if (mapType.isAssignableFrom(type)) { return true; } } return false; }
public TypeInformation<?> getMapValueType() { if (isMap()) { return getTypeArgument(getType(), Map.class, 1); } List<TypeInformation<?>> arguments = getTypeArguments(); if (arguments.size() > 1) { return arguments.get(1); } return null; }
@Override @SuppressWarnings("unchecked") public TypeInformation<? extends S> specialize(ClassTypeInformation<?> type) { Assert.isTrue(getType().isAssignableFrom(type.getType()), String.format("%s must be assignable from %s", getType(), type.getType())); List<TypeInformation<?>> arguments = getTypeArguments(); return (TypeInformation<? extends S>) (arguments.isEmpty() ? type : createInfo(new SyntheticParamterizedType(type, arguments))); }
public TypeInformation<?> getComponentType() { Class<S> rawType = getType(); if (rawType.isArray()) { return createInfo(rawType.getComponentType()); } if (isMap()) { return getTypeArgument(rawType, Map.class, 0); } if (Iterable.class.isAssignableFrom(rawType)) { return getTypeArgument(rawType, Iterable.class, 0); } List<TypeInformation<?>> arguments = getTypeArguments(); if (arguments.size() > 0) { return arguments.get(0); } return null; }
public boolean isMap() { return Map.class.isAssignableFrom(getType()); }
@Nullable protected TypeInformation<?> doGetComponentType() { Class<S> rawType = getType(); if (rawType.isArray()) { return createInfo(rawType.getComponentType()); } if (isMap()) { return getTypeArgument(getBaseType(MAP_TYPES), 0); } if (Iterable.class.isAssignableFrom(rawType)) { return getTypeArgument(Iterable.class, 0); } List<TypeInformation<?>> arguments = getTypeArguments(); return arguments.size() > 0 ? arguments.get(0) : null; }
public boolean isCollectionLike() { Class<?> rawType = getType(); if (rawType.isArray() || Iterable.class.equals(rawType)) { return true; } return Collection.class.isAssignableFrom(rawType); }
private Class<?> getBaseType(Class<?>[] candidates) { Class<S> type = getType(); for (Class<?> candidate : candidates) { if (candidate.isAssignableFrom(type)) { return candidate; } } throw new IllegalArgumentException(String.format("Type %s not contained in candidates %s!", type, candidates)); }
public boolean isCollectionLike() { Class<?> rawType = getType(); return rawType.isArray() // || Iterable.class.equals(rawType) // || Collection.class.isAssignableFrom(rawType) // || Streamable.class.equals(rawType); }
@Override protected TypeInformation<?> createInfo(Type fieldType) { if (parent.getType().equals(fieldType)) { return parent; } return super.createInfo(fieldType); }
public boolean isAssignableFrom(TypeInformation<?> target) { TypeInformation<?> superTypeInformation = target.getSuperTypeInformation(getType()); return superTypeInformation == null ? false : superTypeInformation.equals(this); }
@Override protected TypeInformation<?> createInfo(Type fieldType) { if (parent.getType().equals(fieldType)) { return parent; } return super.createInfo(fieldType); }
public boolean isAssignableFrom(TypeInformation<?> target) { return target.getSuperTypeInformation(getType()).equals(this); }
@Nullable public TypeInformation<?> getSuperTypeInformation(Class<?> superType) { Class<?> rawType = getType(); if (!superType.isAssignableFrom(rawType)) { return null; } if (getType().equals(superType)) { return this; } List<Type> candidates = new ArrayList<>(); Type genericSuperclass = rawType.getGenericSuperclass(); if (genericSuperclass != null) { candidates.add(genericSuperclass); } candidates.addAll(Arrays.asList(rawType.getGenericInterfaces())); for (Type candidate : candidates) { TypeInformation<?> candidateInfo = createInfo(candidate); if (superType.equals(candidateInfo.getType())) { return candidateInfo; } else { TypeInformation<?> nestedSuperType = candidateInfo.getSuperTypeInformation(superType); if (nestedSuperType != null) { return nestedSuperType; } } } return null; }
public TypeInformation<?> getSuperTypeInformation(Class<?> superType) { Class<?> type = getType(); if (!superType.isAssignableFrom(type)) { return null; } if (getType().equals(superType)) { return this; } List<Type> candidates = new ArrayList<Type>(); Type genericSuperclass = type.getGenericSuperclass(); if (genericSuperclass != null) { candidates.add(genericSuperclass); } candidates.addAll(Arrays.asList(type.getGenericInterfaces())); for (Type candidate : candidates) { TypeInformation<?> candidateInfo = createInfo(candidate); if (superType.equals(candidateInfo.getType())) { return candidateInfo; } else { TypeInformation<?> nestedSuperType = candidateInfo.getSuperTypeInformation(superType); if (nestedSuperType != null) { return nestedSuperType; } } } return null; }
@Override public ClassTypeInformation<?> getRawTypeInformation() { return ClassTypeInformation.from(getType()).getRawTypeInformation(); }
@Nullable private TypeInformation<?> getTypeArgument(Class<?> bound, int index) { Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(getType(), bound); if (arguments == null) { return getSuperTypeInformation(bound) instanceof ParameterizedTypeInformation ? ClassTypeInformation.OBJECT : null; } return createInfo(arguments[index]); }
/** * Returns the {@link TypeInformation} for the given atomic field. Will inspect fields first and return the type of a * field if available. Otherwise it will fall back to a {@link PropertyDescriptor}. * * @see #getGenericType(PropertyDescriptor) * @param fieldname * @return */ @SuppressWarnings("null") private Optional<TypeInformation<?>> getPropertyInformation(String fieldname) { Class<?> rawType = getType(); Field field = ReflectionUtils.findField(rawType, fieldname); if (field != null) { return Optional.of(createInfo(field.getGenericType())); } return findPropertyDescriptor(rawType, fieldname).map(it -> createInfo(getGenericType(it))); }
/** * Returns the {@link TypeInformation} for the given atomic field. Will inspect fields first and return the type of a * field if available. Otherwise it will fall back to a {@link PropertyDescriptor}. * * @see #getGenericType(PropertyDescriptor) * @param fieldname * @return */ private TypeInformation<?> getPropertyInformation(String fieldname) { Class<?> type = getType(); Field field = ReflectionUtils.findField(type, fieldname); if (field != null) { return createInfo(field.getGenericType()); } PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(type, fieldname); return descriptor == null ? null : createInfo(getGenericType(descriptor)); }