Java Class.isArray Examples

Java Class.isArray - 30 examples found. These are the top rated real world Java examples of Class.isArray extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  /**
   * Return the correspondent type by checking the "assignability" of the class received as
   * parameter.
   *
   * @param iClass Class to check
   * @return OType instance if found, otherwise null
   */
  public static OType getTypeByClass(final Class<?> iClass) {
    if (iClass == null) return null;

    for (final OType type : TYPES)
      for (int i = 0; i < type.javaTypes.length; ++i) {
        if (type.javaTypes[i] == iClass) return type;
        if (type.javaTypes[i] == Array.class && iClass.isArray()) return type;
      }

    int priority = 0;
    boolean comparedAtLeastOnce;
    do {
      comparedAtLeastOnce = false;
      for (final OType type : TYPES) {
        if (type.allowAssignmentFrom.length > priority) {
          if (type.allowAssignmentFrom[priority].isAssignableFrom(iClass)) return type;
          if (type.allowAssignmentFrom[priority].isArray() && iClass.isArray()) return type;
          comparedAtLeastOnce = true;
        }
      }

      priority++;
    } while (comparedAtLeastOnce);

    return null;
  }
Example #2
0
 static String javaSignature(Class<?> type) {
   if (!type.isArray()) {
     return type.getName();
   } else {
     int arrayDimension = 0;
     do {
       ++arrayDimension;
       type = type.getComponentType();
     } while (type.isArray());
     String name = type.getName();
     String suffix = "[]";
     if (arrayDimension == 1) {
       return name.concat(suffix);
     } else {
       int length = name.length() + arrayDimension * suffix.length();
       StringBuffer sb = new StringBuffer(length);
       sb.append(name);
       while (arrayDimension != 0) {
         --arrayDimension;
         sb.append(suffix);
       }
       return sb.toString();
     }
   }
 }
Example #3
0
 /**
  * Appends the specified class name to the buffer. The class may represent a simple type, a
  * reference type or an array type.
  *
  * @param sb buffer
  * @param obj the class which name should be appended to the buffer
  * @throws NullPointerException if any of the arguments is null
  */
 void appendArrayType(StringBuilder sb, Class<?> obj) {
   if (!obj.isArray()) {
     sb.append(obj.getName());
     return;
   }
   int dimensions = 1;
   Class simplified = obj.getComponentType();
   obj = simplified;
   while (simplified.isArray()) {
     obj = simplified;
     dimensions++;
   }
   sb.append(obj.getName());
   switch (dimensions) {
     case 1:
       sb.append(DIMENSION_1);
       break;
     case 2:
       sb.append(DIMENSION_2);
       break;
     case 3:
       sb.append(DIMENSION_3);
       break;
     default:
       for (; dimensions > 0; dimensions--) {
         sb.append(DIMENSION_1);
       }
   }
 }
Example #4
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);
   }
 }
Example #5
0
 private boolean parametersMatchParameterList(Class<?>[] prmTypes, String[][] parameters) {
   int ndx = 0;
   while (ndx < prmTypes.length) {
     Class<?> prmType = prmTypes[ndx];
     if (ndx >= parameters.length) {
       return ndx == prmTypes.length - 1 && prmType.isArray();
     }
     if (ndx == prmTypes.length - 1
         && prmType.isArray()) { // Assume this only occurs for functions with var args
       Class<?> varArgClass = prmType.getComponentType();
       while (ndx < parameters.length) {
         Class<?> prmClass = resolver.resolveClass(parameters[ndx][0]);
         if (!varArgClass.isAssignableFrom(prmClass)) {
           return false;
         }
         ndx++;
       }
     } else {
       Class<?> prmClass = resolver.resolveClass(parameters[ndx][0]);
       if (!prmTypes[ndx].isAssignableFrom(prmClass)) {
         return false;
       }
     }
     ndx++;
   }
   return true;
 }
Example #6
0
 /**
  * Look up and return any registered {@link Converter} for the specified source and destination
  * class; if there is no registered Converter, return <code>null</code>.
  *
  * @param sourceType Class of the value being converted
  * @param targetType Class of the value to be converted to
  * @return The registered {@link Converter} or <code>null</code> if not found
  */
 public Converter lookup(final Class<?> sourceType, final Class<?> targetType) {
   if (targetType == null) {
     throw new IllegalArgumentException("Target type is missing");
   }
   if (sourceType == null) {
     return this.lookup(targetType);
   }
   Converter converter = null;
   // Convert --> String
   if (targetType == String.class) {
     converter = this.lookup(sourceType);
     if (converter == null
         && (sourceType.isArray() || Collection.class.isAssignableFrom(sourceType))) {
       converter = this.lookup(String[].class);
     }
     if (converter == null) {
       converter = this.lookup(String.class);
     }
     return converter;
   }
   // Convert --> String array
   if (targetType == String[].class) {
     if (sourceType.isArray() || Collection.class.isAssignableFrom(sourceType)) {
       converter = this.lookup(sourceType);
     }
     if (converter == null) {
       converter = this.lookup(String[].class);
     }
     return converter;
   }
   return this.lookup(targetType);
 }