Package eu.stratosphere.api.java.functions

Examples of eu.stratosphere.api.java.functions.InvalidTypesException


    // class type e.g. for custom objects
    if (type instanceof Class<?> && ((Class<?>) type).isArray() && BasicTypeInfo.getInfoFor((Class<C>) type) == null) {
      Class<C> array = (Class<C>) type;
      return new ObjectArrayTypeInfo<T, C>(type, array.getComponentType());
    }
    throw new InvalidTypesException("The given type is not a valid object array.");
  }
View Full Code Here


  // --------------------------------------------------------------------------------------------

  @SuppressWarnings("unchecked")
  public static <X, C> BasicArrayTypeInfo<X, C> getInfoFor(Class<X> type) {
    if (!type.isArray()) {
      throw new InvalidTypesException("The given class is no array.");
    }

    // basic type arrays
    return (BasicArrayTypeInfo<X, C>) TYPES.get(type);
  }
View Full Code Here

    ArrayList<Type> typeHierarchy = new ArrayList<Type>();   
    try {
      validateInfo(typeHierarchy, getParameterType(baseClass, typeHierarchy, clazz, inputParamPos), inType);
    }
    catch(InvalidTypesException e) {
      throw new InvalidTypesException("Input mismatch: " + e.getMessage());
    }
  }
View Full Code Here

 
  @SuppressWarnings("unchecked")
  private static void validateInfo(ArrayList<Type> typeHierarchy, Type type, TypeInformation<?> typeInfo) {
   
    if (type == null) {
      throw new InvalidTypesException("Unknown Error. Type is null.");
    }
   
    if (typeInfo == null) {
      throw new InvalidTypesException("Unknown Error. TypeInformation is null.");
    }
   
    if (!(type instanceof TypeVariable<?>)) {
      // check for basic type
      if (typeInfo.isBasicType()) {
       
        TypeInformation<?> actual = null;
        // check if basic type at all
        if (!(type instanceof Class<?>) || (actual = BasicTypeInfo.getInfoFor((Class<?>) type)) == null) {
          throw new InvalidTypesException("Basic type expected.");
        }
        // check if correct basic type
        if (!typeInfo.equals(actual)) {
          throw new InvalidTypesException("Basic type '" + typeInfo + "' expected but was '" + actual + "'.");
        }
       
      }
      // check for tuple
      else if (typeInfo.isTupleType()) {
        // check if tuple at all
        if (!(type instanceof Class<?> && Tuple.class.isAssignableFrom((Class<?>) type))
            && !(type instanceof ParameterizedType && Tuple.class.isAssignableFrom((Class<?>) ((ParameterizedType) type)
                .getRawType()))) {
          throw new InvalidTypesException("Tuple type expected.");
        }
       
        // do not allow usage of Tuple as type
        if (type instanceof Class<?> && ((Class<?>) type).equals(Tuple.class)) {
          throw new InvalidTypesException("Concrete subclass of Tuple expected.");
        }
       
        // go up the hierarchy until we reach immediate child of Tuple (with or without generics)
        while (!(type instanceof ParameterizedType && ((Class<?>) ((ParameterizedType) type).getRawType()).getSuperclass().equals(
            Tuple.class))
            && !(type instanceof Class<?> && ((Class<?>) type).getSuperclass().equals(Tuple.class))) {
          typeHierarchy.add(type);
          // parameterized type
          if (type instanceof ParameterizedType) {
            type = ((Class<?>) ((ParameterizedType) type).getRawType()).getGenericSuperclass();
          }
          // class
          else {
            type = ((Class<?>) type).getGenericSuperclass();
          }
        }
       
        // check if immediate child of Tuple has generics
        if (type instanceof Class<?>) {
          throw new InvalidTypesException("Parameterized Tuple type expected.");
        }
       
        TupleTypeInfo<?> tti = (TupleTypeInfo<?>) typeInfo;
       
        Type[] subTypes = ((ParameterizedType) type).getActualTypeArguments();
       
        if (subTypes.length != tti.getArity()) {
          throw new InvalidTypesException("Tuple arity '" + tti.getArity() + "' expected but was '"
              + subTypes.length + "'.");
        }
       
        for (int i = 0; i < subTypes.length; i++) {
          validateInfo(new ArrayList<Type>(typeHierarchy), subTypes[i], ((TupleTypeInfo<?>) typeInfo).getTypeAt(i));
        }
      }
      // check for Writable
      else if (typeInfo instanceof WritableTypeInfo<?>) {
        // check if writable at all
        if (!(type instanceof Class<?> && Writable.class.isAssignableFrom((Class<?>) type))) {
          throw new InvalidTypesException("Writable type expected.");
        }
       
        // check writable type contents
        Class<?> clazz = null;
        if (((WritableTypeInfo<?>) typeInfo).getTypeClass() != (clazz = (Class<?>) type)) {
          throw new InvalidTypesException("Writable type '"
              + ((WritableTypeInfo<?>) typeInfo).getTypeClass().getCanonicalName() + "' expected but was '"
              + clazz.getCanonicalName() + "'.");
        }
      }
      // check for basic array
      else if (typeInfo instanceof BasicArrayTypeInfo<?, ?>) {
        Type component = null;
        // check if array at all
        if (!(type instanceof Class<?> && ((Class<?>) type).isArray() && (component = ((Class<?>) type).getComponentType()) != null)
            && !(type instanceof GenericArrayType && (component = ((GenericArrayType) type).getGenericComponentType()) != null)) {
          throw new InvalidTypesException("Array type expected.");
        }
       
        if (component instanceof TypeVariable<?>) {
          component = materializeTypeVariable(typeHierarchy, (TypeVariable<?>) component);
          if (component == null) {
            return;
          }
        }
       
        validateInfo(typeHierarchy, component, ((BasicArrayTypeInfo<?, ?>) typeInfo).getComponentInfo());
       
      }
      // check for object array
      else if (typeInfo instanceof ObjectArrayTypeInfo<?, ?>) {
        // check if array at all
        if (!(type instanceof Class<?> && ((Class<?>) type).isArray()) && !(type instanceof GenericArrayType)) {
          throw new InvalidTypesException("Object array type expected.");
        }
       
        // check component
        Type component = null;
        if (type instanceof Class<?>) {
          component = ((Class<?>) type).getComponentType();
        } else {
          component = ((GenericArrayType) type).getGenericComponentType();
        }
       
        if (component instanceof TypeVariable<?>) {
          component = materializeTypeVariable(typeHierarchy, (TypeVariable<?>) component);
          if (component == null) {
            return;
          }
        }
       
        validateInfo(typeHierarchy, component, ((ObjectArrayTypeInfo<?, ?>) typeInfo).getComponentInfo());
      }
      // check for value
      else if (typeInfo instanceof ValueTypeInfo<?>) {
        // check if value at all
        if (!(type instanceof Class<?> && Value.class.isAssignableFrom((Class<?>) type))) {
          throw new InvalidTypesException("Value type expected.");
        }
       
        TypeInformation<?> actual = null;
        // check value type contents
        if (!((ValueTypeInfo<?>) typeInfo).equals(actual = ValueTypeInfo.getValueTypeInfo((Class<? extends Value>) type))) {
          throw new InvalidTypesException("Value type '" + typeInfo + "' expected but was '" + actual + "'.");
        }
      }
      // check for custom object
      else if (typeInfo instanceof GenericTypeInfo<?>) {
        Class<?> clazz = null;
        if (!(type instanceof Class<?> && ((GenericTypeInfo<?>) typeInfo).getTypeClass() == (clazz = (Class<?>) type))
            && !(type instanceof ParameterizedType && (clazz = (Class<?>) ((ParameterizedType) type).getRawType()) == ((GenericTypeInfo<?>) typeInfo)
                .getTypeClass())) {
          throw new InvalidTypesException("Generic object type '"
              + ((GenericTypeInfo<?>) typeInfo).getTypeClass().getCanonicalName() + "' expected but was '"
              + clazz.getCanonicalName() + "'.");
        }
      }
    } else {
View Full Code Here

      }
    }
   
    // check if immediate child of base class has generics
    if (curT instanceof Class<?>) {
      throw new InvalidTypesException("Function needs to be parameterized by using generics.");
    }
   
    if (typeHierarchy != null) {
      typeHierarchy.add(curT);
    }
View Full Code Here

     
      Type curT = t;
     
      // do not allow usage of Tuple as type
      if (curT instanceof Class<?> && ((Class<?>) curT).equals(Tuple.class)) {
        throw new InvalidTypesException(
            "Usage of class Tuple as a type is not allowed. Use a concrete subclass (e.g. Tuple1, Tuple2, etc.) instead.");
      }
     
      // go up the hierarchy until we reach immediate child of Tuple (with or without generics)
      // collect the types while moving up for a later top-down
      while (!(curT instanceof ParameterizedType && ((Class<?>) ((ParameterizedType) curT).getRawType()).getSuperclass().equals(
          Tuple.class))
          && !(curT instanceof Class<?> && ((Class<?>) curT).getSuperclass().equals(Tuple.class))) {
        typeHierarchy.add(curT);
       
        // parameterized type
        if (curT instanceof ParameterizedType) {
          curT = ((Class<?>) ((ParameterizedType) curT).getRawType()).getGenericSuperclass();
        }
        // class
        else {
          curT = ((Class<?>) curT).getGenericSuperclass();
        }
      }
     
      // check if immediate child of Tuple has generics
      if (curT instanceof Class<?>) {
        throw new InvalidTypesException("Tuple needs to be parameterized by using generics.");
      }
     
      ParameterizedType tupleChild = (ParameterizedType) curT;
     
      Type[] subtypes = new Type[tupleChild.getActualTypeArguments().length];
     
      // materialize possible type variables
      for (int i = 0; i < subtypes.length; i++) {
        // materialize immediate TypeVariables
        if (tupleChild.getActualTypeArguments()[i] instanceof TypeVariable<?>) {
          Type varContent = materializeTypeVariable(typeHierarchy, (TypeVariable<?>) tupleChild.getActualTypeArguments()[i]);
          // variable could not be materialized
          if (varContent == null) {
            // add the TypeVariable as subtype for step in next section
            subtypes[i] = tupleChild.getActualTypeArguments()[i];
          } else {
            // add class or parameterized type
            subtypes[i] = varContent;
          }
        }
        // class or parameterized type
        else {
          subtypes[i] = tupleChild.getActualTypeArguments()[i];
        }
      }
     
      TypeInformation<?>[] tupleSubTypes = new TypeInformation<?>[subtypes.length];
      for (int i = 0; i < subtypes.length; i++) {
        // sub type could not be determined with materializing
        // try to derive the type info of the TypeVariable from the immediate base child input as a last attempt
        if (subtypes[i] instanceof TypeVariable<?>) {
          ParameterizedType immediateBaseChild = (ParameterizedType) typeHierarchy.get(typeHierarchy.size() - 1);
          tupleSubTypes[i] = createTypeInfoWithImmediateBaseChildInput(immediateBaseChild, (TypeVariable<?>) subtypes[i],
              in1Type, in2Type);
         
          // variable could not be determined
          if (tupleSubTypes[i] == null) {
            throw new InvalidTypesException("Type of TypeVariable '" + ((TypeVariable<?>) subtypes[i]).getName() + "' in '"
                + ((TypeVariable<?>) subtypes[i]).getGenericDeclaration()
                + "' could not be determined. This is most likely a type erasure problem. "
                + "The type extraction currently supports types with generic variables only in cases where "
                + "all variables in the return type can be deduced from the input type(s).");
          }
        } else {
          tupleSubTypes[i] = createTypeInfoWithTypeHierarchy(new ArrayList<Type>(typeHierarchy), subtypes[i], in1Type, in2Type);
        }
      }
     
      // TODO: Check that type that extends Tuple does not have additional fields.
      // Right now, these fields are not be serialized by the TupleSerializer.
      // We might want to add an ExtendedTupleSerializer for that.
     
      if (t instanceof Class<?>) {
        return new TupleTypeInfo(((Class<? extends Tuple>) t), tupleSubTypes);
      } else if (t instanceof ParameterizedType) {
        return new TupleTypeInfo(((Class<? extends Tuple>) ((ParameterizedType) t).getRawType()), tupleSubTypes);
      }
    }
    // type depends on another type
    // e.g. class MyMapper<E> extends MapFunction<String, E>
    else if (t instanceof TypeVariable) {
      Type typeVar = materializeTypeVariable(typeHierarchy, (TypeVariable<?>) t);
     
      if (typeVar != null) {
        return createTypeInfoWithTypeHierarchy(typeHierarchy, typeVar, in1Type, in2Type);
      }
      // try to derive the type info of the TypeVariable from the immediate base child input as a last attempt
      else {
        ParameterizedType immediateBaseChild = (ParameterizedType) typeHierarchy.get(typeHierarchy.size() - 1);
        TypeInformation<OUT> typeInfo = (TypeInformation<OUT>) createTypeInfoWithImmediateBaseChildInput(immediateBaseChild,
            (TypeVariable<?>) t, in1Type, in2Type);
        if (typeInfo != null) {
          return typeInfo;
        } else {
          throw new InvalidTypesException("Type of TypeVariable '" + ((TypeVariable<?>) t).getName() + "' in '"
              + ((TypeVariable<?>) t).getGenericDeclaration() + "' could not be determined. This is most likely a type erasure problem. "
              + "The type extraction currently supports types with generic variables only in cases where "
              + "all variables in the return type can be deduced from the input type(s).");
        }
      }
    }
    // arrays with generics
    // (due to a Java 6 bug, it is possible that BasicArrayTypes also get classified as ObjectArrayTypes
    // since the JVM classifies e.g. String[] as GenericArrayType instead of Class)
    else if (t instanceof GenericArrayType) {
      GenericArrayType genericArray = (GenericArrayType) t;
     
      TypeInformation<?> componentInfo = createTypeInfoWithTypeHierarchy(typeHierarchy, genericArray.getGenericComponentType(),
          in1Type, in2Type);
      return ObjectArrayTypeInfo.getInfoFor(t, componentInfo);
    }
    // objects with generics are treated as raw type
    else if (t instanceof ParameterizedType) {
      return getForClass((Class<OUT>) ((ParameterizedType) t).getRawType());
    }
    // no tuple, no TypeVariable, no generic type
    else if (t instanceof Class) {
      return getForClass((Class<OUT>) t);
    }
   
    throw new InvalidTypesException("Type Information could not be created.");
  }
View Full Code Here

  public static <X> TypeInformation<X> getForClass(Class<X> clazz) {
    Validate.notNull(clazz);
   
    // check for abstract classes or interfaces
    if (Modifier.isInterface(clazz.getModifiers()) || (Modifier.isAbstract(clazz.getModifiers()) && !clazz.isArray())) {
      throw new InvalidTypesException("Interfaces and abstract classes are not valid types.");
    }
   
    // check for arrays
    if (clazz.isArray()) {

      // primitive arrays: int[], byte[], ...
      PrimitiveArrayTypeInfo<X> primitiveArrayInfo = PrimitiveArrayTypeInfo.getInfoFor(clazz);
      if (primitiveArrayInfo != null) {
        return primitiveArrayInfo;
      }
     
      // basic type arrays: String[], Integer[], Double[]
      BasicArrayTypeInfo<X, ?> basicArrayInfo = BasicArrayTypeInfo.getInfoFor(clazz);
      if (basicArrayInfo != null) {
        return basicArrayInfo;
      }
     
      // object arrays
      else {
        return ObjectArrayTypeInfo.getInfoFor(clazz);
      }
    }
   
    // check for writable types
    if(Writable.class.isAssignableFrom(clazz)) {
      return (TypeInformation<X>) WritableTypeInfo.getWritableTypeInfo((Class<? extends Writable>) clazz);
    }
   
    // check for basic types
    TypeInformation<X> basicTypeInfo = BasicTypeInfo.getInfoFor(clazz);
    if (basicTypeInfo != null) {
      return basicTypeInfo;
    }
   
    // check for subclasses of Value
    if (Value.class.isAssignableFrom(clazz)) {
      Class<? extends Value> valueClass = clazz.asSubclass(Value.class);
      return (TypeInformation<X>) ValueTypeInfo.getValueTypeInfo(valueClass);
    }
   
    // check for subclasses of Tuple
    if (Tuple.class.isAssignableFrom(clazz)) {
      throw new InvalidTypesException("Type information extraction for tuples cannot be done based on the class.");
    }
   
    // return a generic type
    return new GenericTypeInfo<X>(clazz);
  }
View Full Code Here

      TypeInformation<?>[] infos = new TypeInformation[numFields];
      for (int i = 0; i < numFields; i++) {
        Object field = t.getField(i);
       
        if (field == null) {
          throw new InvalidTypesException("Automatic type extraction is not possible on candidates with null values. "
              + "Please specify the types directly.");
        }
       
        infos[i] = getForObject(field);
      }
View Full Code Here

  static final <X extends Value> TypeInformation<X> getValueTypeInfo(Class<X> typeClass) {
    if (Value.class.isAssignableFrom(typeClass) && !typeClass.equals(Value.class)) {
      return new ValueTypeInfo<X>(typeClass);
    }
    else {
      throw new InvalidTypesException("The given class is no subclass of " + Value.class.getName());
    }
  }
View Full Code Here

  // --------------------------------------------------------------------------------------------

  @SuppressWarnings("unchecked")
  public static <X> PrimitiveArrayTypeInfo<X> getInfoFor(Class<X> type) {
    if (!type.isArray()) {
      throw new InvalidTypesException("The given class is no array.");
    }

    // basic type arrays
    return (PrimitiveArrayTypeInfo<X>) TYPES.get(type);
  }
View Full Code Here

TOP

Related Classes of eu.stratosphere.api.java.functions.InvalidTypesException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.