Java Class.isAssignableFrom Examples

Java Class.isAssignableFrom - 30 examples found. These are the top rated real world Java examples of Class.isAssignableFrom extracted from open source projects. You can rate examples to help us improve the quality of examples.
 private static boolean verifyReturn(Class columnClass, Class returnType) {
   if (columnClass.isAssignableFrom(returnType)) return true;
   if (returnType.isPrimitive()) {
     return (columnClass.isAssignableFrom(getWrapper(returnType)));
   }
   return false;
 }
  public static long[] getLongValues(Object value, long[] defaultValue) {
    Class<?> clazz = value.getClass();

    if (clazz.isArray()) {
      Class<?> componentType = clazz.getComponentType();

      if (componentType.isAssignableFrom(String.class)) {
        return getLongValues((String[]) value, defaultValue);
      } else if (componentType.isAssignableFrom(Long.class)) {
        return (long[]) value;
      } else if (Number.class.isAssignableFrom(componentType)) {
        Number[] numbers = (Number[]) value;

        long[] values = new long[numbers.length];

        for (int i = 0; i < values.length; i++) {
          values[i] = numbers[i].longValue();
        }

        return values;
      }
    }

    return defaultValue;
  }
  /**
   * Gets the producers for the closest super class of the target class
   *
   * @param targetClass The target class
   * @return The producer
   */
  protected ContentProducer getSuperClassContentProducer(Class targetClass, Class closestClass) {
    Map.Entry entry;
    Class entryClass;
    ContentProducer closestProducer = null;

    Iterator iter = fContentProducers.entrySet().iterator();
    while (iter.hasNext()) {
      entry = (Map.Entry) iter.next();
      entryClass = (Class) entry.getKey();
      if (entryClass.isAssignableFrom(targetClass)) {
        if (closestClass != null && closestClass.isAssignableFrom(entryClass)) {
          closestClass = entryClass;
          closestProducer = (ContentProducer) entry.getValue();
        }
      }
    }

    // whether we found one or not ask our parent to see if a more
    // exact producer is defined
    // unless we are autonomous (ie: we have no parent)
    if (!this.isAutonomous()) {
      ContentProducer parentProducer =
          getParent().getSuperClassContentProducer(targetClass, closestClass);
      if (parentProducer != null) {
        closestProducer = parentProducer;
      }
    }

    return closestProducer;
  }
Example #4
0
 /**
  * @param geoclass
  * @return
  */
 protected Feature createBasicFeature(Class<? extends Geometry> geoclass) {
   Feature f = new Feature();
   count.incrementAndGet();
   f.setName("feature" + count);
   f.setDescription("feature description " + count);
   if (geoclass.isAssignableFrom(Point.class)) {
     Point p = new Point(new Geodetic2DPoint(random));
     f.setGeometry(p);
   } else if (geoclass.isAssignableFrom(Line.class)) {
     List<Point> pts = new ArrayList<Point>();
     pts.add(new Point(new Geodetic2DPoint(random)));
     pts.add(new Point(new Geodetic2DPoint(random)));
     f.setGeometry(new Line(pts));
   } else if (geoclass.isAssignableFrom(LinearRing.class)) {
     List<Point> pts = new ArrayList<Point>();
     pts.add(new Point(new Geodetic2DPoint(random)));
     pts.add(new Point(new Geodetic2DPoint(random)));
     pts.add(new Point(new Geodetic2DPoint(random)));
     pts.add(new Point(new Geodetic2DPoint(random)));
     f.setGeometry(new LinearRing(pts));
   } else if (geoclass.isAssignableFrom(Polygon.class)) {
     List<Point> pts = new ArrayList<Point>();
     pts.add(new Point(new Geodetic2DPoint(random)));
     pts.add(new Point(new Geodetic2DPoint(random)));
     pts.add(new Point(new Geodetic2DPoint(random)));
     pts.add(new Point(new Geodetic2DPoint(random)));
     f.setGeometry(new Polygon(new LinearRing(pts)));
   }
   return f;
 }
Example #5
0
  /**
   * Returns the generic supertype for {@code supertype}. For example, given a class {@code
   * IntegerSet}, the result for when supertype is {@code Set.class} is {@code Set<Integer>} and the
   * result when the supertype is {@code Collection.class} is {@code Collection<Integer>}.
   */
  static Type getGenericSupertype(Type context, Class<?> rawType, Class<?> toResolve) {
    if (toResolve == rawType) {
      return context;
    }

    // we skip searching through interfaces if unknown is an interface
    if (toResolve.isInterface()) {
      Class<?>[] interfaces = rawType.getInterfaces();
      for (int i = 0, length = interfaces.length; i < length; i++) {
        if (interfaces[i] == toResolve) {
          return rawType.getGenericInterfaces()[i];
        } else if (toResolve.isAssignableFrom(interfaces[i])) {
          return getGenericSupertype(rawType.getGenericInterfaces()[i], interfaces[i], toResolve);
        }
      }
    }

    // check our supertypes
    if (!rawType.isInterface()) {
      while (rawType != Object.class) {
        Class<?> rawSupertype = rawType.getSuperclass();
        if (rawSupertype == toResolve) {
          return rawType.getGenericSuperclass();
        } else if (toResolve.isAssignableFrom(rawSupertype)) {
          return getGenericSupertype(rawType.getGenericSuperclass(), rawSupertype, toResolve);
        }
        rawType = rawSupertype;
      }
    }

    // we can't resolve this further
    return toResolve;
  }
  /**
   * Tests whether or not the left hand type is compatible with the right hand type in Groovy terms,
   * i.e. can the left type be assigned a value of the right hand type in Groovy.
   *
   * <p>This handles Java primitive type equivalence and uses isAssignableFrom for all other types,
   * with a bit of magic for native types and polymorphism i.e. Number assigned an int. If either
   * parameter is null an exception is thrown
   *
   * @param leftType The type of the left hand part of a notional assignment
   * @param rightType The type of the right hand part of a notional assignment
   * @return true if values of the right hand type can be assigned in Groovy to variables of the
   *     left hand type.
   */
  public static boolean isGroovyAssignableFrom(Class<?> leftType, Class<?> rightType) {
    if (leftType == null) {
      throw new NullPointerException("Left type is null!");
    }
    if (rightType == null) {
      throw new NullPointerException("Right type is null!");
    }
    if (leftType == Object.class) {
      return true;
    }
    if (leftType == rightType) {
      return true;
    }
    // check for primitive type equivalence
    Class<?> r = PRIMITIVE_TYPE_COMPATIBLE_CLASSES.get(leftType);
    boolean result = r == rightType;

    if (!result) {
      // If no primitive <-> wrapper match, it may still be assignable
      // from polymorphic primitives i.e. Number -> int (AKA Integer)
      if (rightType.isPrimitive()) {
        // see if incompatible
        r = PRIMITIVE_TYPE_COMPATIBLE_CLASSES.get(rightType);
        if (r != null) {
          result = leftType.isAssignableFrom(r);
        }
      } else {
        // Otherwise it may just be assignable using normal Java polymorphism
        result = leftType.isAssignableFrom(rightType);
      }
    }
    return result;
  }