Java Class.isPrimitive Examples

Java Class.isPrimitive - 30 examples found. These are the top rated real world Java examples of Class.isPrimitive extracted from open source projects. You can rate examples to help us improve the quality of examples.
    private void prepareLeftOperand(
        BooleanOperator operation,
        Class<?> type,
        Class<?> leftType,
        Class<?> rightType,
        Label shortcutEvaluation) {
      if (leftType.isPrimitive()) {
        if (type != null) {
          castOrCoercePrimitive(LEFT_OPERAND, leftType, type);
        }
        return;
      }

      Label notNullLabel =
          jitLeftIsNull(
              type == null || leftType == type
                  ? jitNullSafeOperationStart()
                  : jitNullSafeCoercion(leftType, type));

      if (operation.isEquality() && !rightType.isPrimitive()) {
        // if (left == null) return right == null
        checkNullEquality();
      } else {
        // if (left == null) return false
        mv.visitInsn(ICONST_0);
      }

      mv.visitJumpInsn(GOTO, shortcutEvaluation);
      mv.visitLabel(notNullLabel);
    }
Example #2
0
 public static Object convertArrayElements(Class<?> arrayType, Object array) {
   Class<?> src = array.getClass().getComponentType();
   Class<?> dst = arrayType.getComponentType();
   if (src == null || dst == null) throw new IllegalArgumentException("not array type");
   Wrapper sw = (src.isPrimitive() ? Wrapper.forPrimitiveType(src) : null);
   Wrapper dw = (dst.isPrimitive() ? Wrapper.forPrimitiveType(dst) : null);
   int length;
   if (sw == null) {
     Object[] a = (Object[]) array;
     length = a.length;
     if (dw == null) return Arrays.copyOf(a, length, arrayType.asSubclass(Object[].class));
     Object res = dw.makeArray(length);
     dw.copyArrayUnboxing(a, 0, res, 0, length);
     return res;
   }
   length = j86.java.lang.reflect.Array.getLength(array);
   Object[] res;
   if (dw == null) {
     res = Arrays.copyOf(NO_ARGS_ARRAY, length, arrayType.asSubclass(Object[].class));
   } else {
     res = new Object[length];
   }
   sw.copyArrayBoxing(array, 0, res, 0, length);
   if (dw == null) return res;
   Object a = dw.makeArray(length);
   dw.copyArrayUnboxing(res, 0, a, 0, length);
   return a;
 }
Example #3
0
 /**
  * Returns whether first type is assignable from second one or not.
  *
  * @param type checked whether is assignable, always not null
  * @param from checked type, might be null
  * @return true if first type is assignable from second one, false otherwise
  */
 public static boolean isAssignable(final Class type, final Class from) {
   if (from == null) {
     return !type.isPrimitive();
   } else if (type.isAssignableFrom(from)) {
     return true;
   } else if (type.isPrimitive()) {
     if (type == boolean.class) {
       return Boolean.class.isAssignableFrom(from);
     } else if (type == int.class) {
       return Integer.class.isAssignableFrom(from);
     } else if (type == char.class) {
       return Character.class.isAssignableFrom(from);
     } else if (type == byte.class) {
       return Byte.class.isAssignableFrom(from);
     } else if (type == short.class) {
       return Short.class.isAssignableFrom(from);
     } else if (type == long.class) {
       return Long.class.isAssignableFrom(from);
     } else if (type == float.class) {
       return Float.class.isAssignableFrom(from);
     } else if (type == double.class) {
       return Double.class.isAssignableFrom(from);
     } else if (type == void.class) {
       return Void.class.isAssignableFrom(from);
     }
   }
   return false;
 }
 /**
  * @param parameterTypes
  * @param parameters
  * @return true when the signature fits
  */
 private static boolean signatureFits(Class[] parameterTypes, Object[] parameters) {
   if ((parameterTypes == null && parameters != null)
       || (parameterTypes != null && parameters == null)
       || (parameterTypes.length != parameters.length)) {
     return false;
   }
   for (int i = 0; i < parameters.length; i++) {
     Object parameter = parameters[i];
     Class parameterType = parameterTypes[i];
     if (parameter == null) {
       if (parameterType.isPrimitive()) {
         return false;
       }
       continue; // null fits any class
     }
     Class parameterClass = parameter.getClass();
     if (!(parameterType.isAssignableFrom(parameterClass)
         || (parameterType.isPrimitive() && isCompatible(parameterType, parameterClass)))) {
       //				System.out.println("- signature-mismatch: wanted=" + parameterType + ", have=" +
       // parameter.getClass() );
       return false;
       //			} else {
       //				System.out.println("+ signature-match: wanted=" + parameterType + ", have=" +
       // parameter.getClass() );
     }
   }
   return true;
 }
Example #5
0
  /**
   * @param cls
   * @param toClass
   * @param subtypeVarAssigns
   * @return
   */
  private static Map<TypeVariable<?>, Type> getTypeArguments(
      Class<?> cls, Class<?> toClass, Map<TypeVariable<?>, Type> subtypeVarAssigns) {
    // make sure they're assignable
    if (!isAssignable(cls, toClass)) {
      return null;
    }

    // can't work with primitives
    if (cls.isPrimitive()) {
      // both classes are primitives?
      if (toClass.isPrimitive()) {
        // dealing with widening here. No type arguments to be
        // harvested with these two types.
        return new HashMap<TypeVariable<?>, Type>();
      }

      // work with wrapper the wrapper class instead of the primitive
      cls = ClassUtils.primitiveToWrapper(cls);
    }

    // create a copy of the incoming map, or an empty one if it's null
    HashMap<TypeVariable<?>, Type> typeVarAssigns =
        subtypeVarAssigns == null
            ? new HashMap<TypeVariable<?>, Type>()
            : new HashMap<TypeVariable<?>, Type>(subtypeVarAssigns);

    // no arguments for the parameters, or target class has been reached
    if (cls.getTypeParameters().length > 0 || toClass.equals(cls)) {
      return typeVarAssigns;
    }

    // walk the inheritance hierarchy until the target class is reached
    return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns);
  }
Example #6
0
  /** Creates a {@link PluginPropertyField} based on the given field. */
  private PluginPropertyField createPluginProperty(Field field, TypeToken<?> resolvingType)
      throws UnsupportedTypeException {
    TypeToken<?> fieldType = resolvingType.resolveType(field.getGenericType());
    Class<?> rawType = fieldType.getRawType();

    Name nameAnnotation = field.getAnnotation(Name.class);
    Description descAnnotation = field.getAnnotation(Description.class);
    String name = nameAnnotation == null ? field.getName() : nameAnnotation.value();
    String description = descAnnotation == null ? "" : descAnnotation.value();

    if (rawType.isPrimitive()) {
      return new PluginPropertyField(name, description, rawType.getName(), true);
    }

    rawType = Primitives.unwrap(rawType);
    if (!rawType.isPrimitive() && !String.class.equals(rawType)) {
      throw new UnsupportedTypeException("Only primitive and String types are supported");
    }

    boolean required = true;
    for (Annotation annotation : field.getAnnotations()) {
      if (annotation.annotationType().getName().endsWith(".Nullable")) {
        required = false;
        break;
      }
    }

    return new PluginPropertyField(
        name, description, rawType.getSimpleName().toLowerCase(), required);
  }