Java Class.cast Examples

Java Class.cast - 30 examples found. These are the top rated real world Java examples of Class.cast extracted from open source projects. You can rate examples to help us improve the quality of examples.
  private <T extends View> T instanceView(Class<T> view) {
    if (view.isAssignableFrom(JSONSerialization.class)) {
      serialization =
          new GsonJSONSerialization(
              response, extractor, gsonBuilder, environment, reflectionProvider);
      return view.cast(serialization);
    }

    if (view.isAssignableFrom(XMLSerialization.class)) {
      serialization = new XStreamXMLSerialization(response, xstreambuilder, environment);
      return view.cast(serialization);
    }

    if (view.isAssignableFrom(RepresentationResult.class)) {
      serialization = new XStreamXMLSerialization(response, xstreambuilder, environment);
      return view.cast(
          new DefaultRepresentationResult(
              new FormatResolver() {
                @Override
                public String getAcceptFormat() {
                  return "xml";
                }
              },
              this,
              new MockInstanceImpl<>(this.serialization)));
    }

    return proxifier.proxify(view, returnOnFinalMethods(view));
  }
  /**
   * Method description
   *
   * @param ht_def_key
   * @param ht_dev_val
   * @param st_def_key
   * @param st_def_val
   * @param prop_key
   * @param prop_val_class
   * @param params
   * @param props
   * @param <T>
   */
  protected <T> void checkHighThroughputProperty(
      String ht_def_key,
      T ht_dev_val,
      String st_def_key,
      T st_def_val,
      String prop_key,
      Class<T> prop_val_class,
      Map<String, Object> params,
      Map<String, Object> props) {
    T tmp = st_def_val;
    String str_tmp = null;

    if (isHighThroughput()) {
      tmp = ht_dev_val;
      str_tmp = (String) params.get(ht_def_key);
    } else {
      tmp = st_def_val;
      str_tmp = (String) params.get(st_def_key);
    }
    if (prop_val_class.isAssignableFrom(Integer.class)) {
      tmp = prop_val_class.cast(DataTypes.parseNum(str_tmp, Integer.class, (Integer) tmp));
    }
    if (prop_val_class.isAssignableFrom(Long.class)) {
      tmp = prop_val_class.cast(DataTypes.parseNum(str_tmp, Long.class, (Long) tmp));
    }
    if (prop_val_class.isAssignableFrom(String.class)) {
      tmp = prop_val_class.cast(str_tmp);
    }
    props.put(prop_key, tmp);
  }
  /**
   * Adapts the source object to a view object.
   *
   * @param mapper An action that is invoked for each source object in the graph that is to be
   *     adapted. The action can influence how the source object is adapted via the provided {@link
   *     SourceObjectMapping}.
   */
  public <T, S> T adapt(
      Class<T> targetType, S sourceObject, Action<? super SourceObjectMapping> mapper) {
    if (sourceObject == null) {
      return null;
    }
    Class<? extends T> wrapperType = targetTypeProvider.getTargetType(targetType, sourceObject);
    DefaultSourceObjectMapping mapping =
        new DefaultSourceObjectMapping(sourceObject, targetType, wrapperType);
    mapper.execute(mapping);
    wrapperType = mapping.wrapperType.asSubclass(targetType);
    if (wrapperType.isInstance(sourceObject)) {
      return wrapperType.cast(sourceObject);
    }
    if (targetType.isEnum()) {
      return adaptToEnum(targetType, sourceObject);
    }

    MixInMethodInvoker mixInMethodInvoker = null;
    if (mapping.mixInType != null) {
      mixInMethodInvoker =
          new MixInMethodInvoker(
              mapping.mixInType, new AdaptingMethodInvoker(mapper, new ReflectionMethodInvoker()));
    }
    MethodInvoker overrideInvoker = chainInvokers(mixInMethodInvoker, mapping.overrideInvoker);
    Object proxy =
        Proxy.newProxyInstance(
            wrapperType.getClassLoader(),
            new Class<?>[] {wrapperType},
            new InvocationHandlerImpl(sourceObject, overrideInvoker, mapper));
    if (mixInMethodInvoker != null) {
      mixInMethodInvoker.setProxy(proxy);
    }
    return wrapperType.cast(proxy);
  }
Example #4
0
  /**
   * Coerce the given object to targetType.
   *
   * @param value object to be coerced
   * @param targetType which should be object coerced into
   * @return the given value coerced to targetType
   */
  public static <T> T coerce(Object value, Class<T> targetType) {
    if (value == null) {
      return null;
    }

    if (targetType.isInstance(value)) {
      return targetType.cast(value);
    }

    if (value instanceof String) {
      PropertyEditor editor = PropertyEditorManager.findEditor(targetType);
      if (editor == null && Primitives.isWrapperType(targetType)) {
        editor = PropertyEditorManager.findEditor(Primitives.unwrap(targetType));
      }

      if (editor != null) {

        editor.setAsText((String) value);
        return targetType.cast(editor.getValue());
      } else if (targetType.isEnum()) {
        return targetType.cast(Enum.valueOf((Class<Enum>) targetType, (String) value));
      }
    }

    throw new IllegalArgumentException(
        MessageFormat.format(
            "Cannot convert {0} to object of {1} type", value, targetType.getName()));
  }
Example #5
0
    private static boolean isSetException(Score score)
        throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException,
            InvocationTargetException {
      Object craftScore = CRAFT_SCORE.cast(score);

      Object craftObjective = CRAFT_OBJECTIVE.cast(score.getObjective());
      Method craftHandle = CRAFT_OBJECTIVE.getDeclaredMethod("getHandle");
      craftHandle.setAccessible(true);
      Object craftObjectiveHandle = craftHandle.invoke(craftObjective);

      Field objective = CRAFT_SCORE.getDeclaredField("objective");
      objective.setAccessible(true);
      Object craftScoreboard = checkState(objective.get(craftScore));

      Field craftBoard = CRAFT_SCOREBOARD.getDeclaredField("board");
      craftBoard.setAccessible(true);
      Object scoreboard = craftBoard.get(craftScoreboard);
      Method playerObjectives = SCOREBOARD.getDeclaredMethod("getPlayerObjectives", String.class);
      playerObjectives.setAccessible(true);

      Field playerField = CRAFT_SCORE.getDeclaredField("entry");
      playerField.setAccessible(true);
      String playerName = (String) playerField.get(craftScore);
      Map map = (Map) playerObjectives.invoke(scoreboard, playerName);

      // return
      // objective.checkState().board.getPlayerObjectives(playerName).containsKey(objective.getHandle());
      return map.containsKey(craftObjectiveHandle);
    }
  public BuildComparisonSpec createSpec(Set<BuildOutcome> from, Set<BuildOutcome> to) {
    BuildComparisonSpecBuilder builder = new DefaultBuildComparisonSpecBuilder();

    Set<BuildOutcome> toCopy = new HashSet<BuildOutcome>(to);

    for (BuildOutcome fromBuildOutcome : from) {
      BuildOutcome toBuildOutcome = null;
      Class<? extends BuildOutcome> associationType = null;

      for (BuildOutcome buildOutcome : toCopy) {
        toBuildOutcome = buildOutcome;
        associationType = associator.findAssociationType(fromBuildOutcome, toBuildOutcome);

        if (associationType != null) {
          break;
        }
      }

      if (associationType == null) {
        builder.addUnassociatedFrom(fromBuildOutcome);
      } else {
        builder.associate(
            associationType.cast(fromBuildOutcome),
            associationType.cast(toBuildOutcome),
            (Class<BuildOutcome>) associationType);
        toCopy.remove(toBuildOutcome);
      }
    }

    for (BuildOutcome buildOutcome : toCopy) {
      builder.addUnassociatedTo(buildOutcome);
    }

    return builder.build();
  }