Java Class.getComponentType Examples

Java Class.getComponentType - 30 examples found. These are the top rated real world Java examples of Class.getComponentType extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
 private Object newValue(Class<?> type, String name) {
   try {
     if (type.isArray()) {
       Class<?> componentType = type.getComponentType();
       // TODO - only handles 2-dimensional arrays
       if (componentType.isArray()) {
         Object array = Array.newInstance(componentType, 1);
         Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0));
         return array;
       } else {
         return Array.newInstance(componentType, 0);
       }
     } else if (Collection.class.isAssignableFrom(type)) {
       return CollectionFactory.createCollection(type, 16);
     } else if (Map.class.isAssignableFrom(type)) {
       return CollectionFactory.createMap(type, 16);
     } else {
       return type.newInstance();
     }
   } catch (Exception ex) {
     // TODO Root cause exception context is lost here... should we throw another exception type
     // that preserves context instead?
     throw new NullValueInNestedPathException(
         getRootClass(),
         this.nestedPath + name,
         "Could not instantiate property type ["
             + type.getName()
             + "] to auto-grow nested property path: "
             + ex);
   }
 }
  private PropertyDescriptor findExistingPropertyDescriptor(
      String propertyName, Class<?> propertyType) {

    for (PropertyDescriptor pd : this.propertyDescriptors) {
      final Class<?> candidateType;
      final String candidateName = pd.getName();
      if (pd instanceof IndexedPropertyDescriptor) {
        IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
        candidateType = ipd.getIndexedPropertyType();
        if (candidateName.equals(propertyName)
            && (candidateType.equals(propertyType)
                || candidateType.equals(propertyType.getComponentType()))) {
          return pd;
        }
      } else {
        candidateType = pd.getPropertyType();
        if (candidateName.equals(propertyName)
            && (candidateType.equals(propertyType)
                || propertyType.equals(candidateType.getComponentType()))) {
          return pd;
        }
      }
    }
    return null;
  }
Example #3
0
  /**
   * Creates the type, it's private so you must use fromType.
   *
   * @param type
   */
  private JavaType(Type type) {

    // the type
    this.type = type;

    // parameterized?
    parameterizedType =
        ParameterizedType.class.isInstance(type) ? ParameterizedType.class.cast(type) : null;

    // generic info
    genericInfo = parameterizedType != null;

    // get class
    if (Class.class.isInstance(type)) {
      clazz = Class.class.cast(type);
    } else if (parameterizedType != null) {
      clazz = Class.class.cast(parameterizedType.getRawType());
    } else {
      clazz = null;
    }

    // component type
    if (clazz != null && clazz.getComponentType() != null) {
      componentType = clazz.getComponentType();
    }

    // instantiable
    instantiable = clazz != null;

    // typeParameters
    typeParameters = parameterizedType != null ? parameterizedType.getActualTypeArguments() : null;

    // genericInfo
    genericInfo = typeParameters != null;
  }
  private static AnnotationPropertyVal rebuild(
      String key, Class annotationClass, String valueStr, TypeResolver resolver)
      throws NoSuchMethodException {
    Method prop = annotationClass.getMethod(key);
    Class returnType = prop.getReturnType();
    Object val = decode(returnType, valueStr, resolver);
    AnnotationPropertyVal.ValType valType;
    if (returnType.isPrimitive()) {
      valType = AnnotationPropertyVal.ValType.PRIMITIVE;
    } else if (returnType.isEnum()) {
      valType = AnnotationPropertyVal.ValType.ENUMERATION;
    } else if (returnType.isArray()) {

      if (returnType.getComponentType().isEnum()) {
        valType = AnnotationPropertyVal.ValType.ENUMARRAY;
      } else if (returnType.getComponentType().isPrimitive()) {
        valType = AnnotationPropertyVal.ValType.PRIMARRAY;
      } else if (String.class.equals(returnType.getComponentType())) {
        valType = AnnotationPropertyVal.ValType.STRINGARRAY;
      } else {
        valType = AnnotationPropertyVal.ValType.CLASSARRAY;
      }

    } else if (String.class.equals(returnType)) {
      valType = AnnotationPropertyVal.ValType.STRING;
    } else {
      valType = AnnotationPropertyVal.ValType.KLASS;
    }

    AnnotationPropertyVal pv = new AnnotationPropertyVal(key, returnType, val, valType);
    return pv;
  }
Example #5
0
  /**
   * Create an array from a String
   *
   * <p>Unfortunately, this feature is very poorly documented and true UDT support by the PostGreSQL
   * JDBC driver has been postponed for a long time.
   *
   * @param string A String representation of an array
   * @return The converted array
   */
  private static Object[] pgNewArray(Class<?> type, String string) throws SQLException {
    if (string == null) {
      return null;
    }

    try {
      Class<?> component = type.getComponentType();
      String values = string.replaceAll("^\\{(.*)\\}$", "$1");

      if ("".equals(values)) {
        return (Object[]) java.lang.reflect.Array.newInstance(component, 0);
      } else {
        String[] split = values.split(",");
        Object[] result = (Object[]) java.lang.reflect.Array.newInstance(component, split.length);

        for (int i = 0; i < split.length; i++) {
          result[i] = pgFromString(type.getComponentType(), split[i]);
        }

        return result;
      }
    } catch (Exception e) {
      throw new SQLException(e);
    }
  }
Example #6
0
  /**
   * Retrieve an editor object for a given class. This method seems unable to retrieve a primitive
   * editor for obscure reasons. So better use the one based on PropertyDescriptor if possible.
   */
  public static PropertyEditor findEditor(Class<?> cls) {
    PropertyEditor editor = PropertyEditorManager.findEditor(cls);

    // Try to unwrap primitives
    if (editor == null && Primitives.isWrapperType(cls)) {
      editor = PropertyEditorManager.findEditor(Primitives.unwrap(cls));
    }

    if ((editor == null) && useDefaultGOE) {
      if (cls.isArray()) {
        Class<?> unwrapped =
            Primitives.isWrapperType(cls.getComponentType())
                ? Primitives.unwrap(cls.getComponentType())
                : cls;
        if (unwrapped.isPrimitive()) {
          editor = new ArrayEditor();
        } else {
          editor = new ObjectArrayEditor<>(unwrapped.getComponentType());
        }
      } else if (cls.isEnum()) {
        editor = new EnumEditor();
      } else {
        editor = new GenericObjectEditor();
        ((GenericObjectEditor) editor).setClassType(cls);
      }
    }
    return editor;
  }