private void registerMvelHandler(Type targetType) {
   Class<?> targetClass = Generics.erase(targetType);
   SitebricksConversionHandler targetHandler = new SitebricksConversionHandler(targetType);
   DataConversion.addConversionHandler(targetClass, targetHandler);
   if (Primitives.isWrapperType(targetClass)) {
     DataConversion.addConversionHandler(Primitives.unwrap(targetClass), targetHandler);
   }
 }
Example #2
0
  public static Statement convert(final Context context, Object input, final MetaClass targetType) {
    try {
      if (input instanceof Statement) {
        if (input instanceof LiteralValue<?>) {
          input = ((LiteralValue<?>) input).getValue();
        } else {
          if ("null".equals(((Statement) input).generate(context))) {
            return (Statement) input;
          }

          assertAssignableTypes(context, ((Statement) input).getType(), targetType);
          return (Statement) input;
        }
      }

      if (input != null
          && MetaClassFactory.get(input.getClass())
              .getOuterComponentType()
              .getFullyQualifiedName()
              .equals(MetaClass.class.getName())) {
        return generate(context, input);
      }

      if (Object.class.getName().equals(targetType.getFullyQualifiedName())) {
        return generate(context, input);
      }

      Class<?> inputClass = input == null ? Object.class : input.getClass();

      if (MetaClass.class.isAssignableFrom(inputClass)) {
        inputClass = Class.class;
      }

      final Class<?> targetClass = targetType.asBoxed().asClass();
      if (NullType.class.getName().equals(targetClass.getName())) {
        return generate(context, input);
      }

      if (!targetClass.isAssignableFrom(inputClass)
          && DataConversion.canConvert(targetClass, inputClass)) {
        return generate(context, DataConversion.convert(input, targetClass));
      } else {
        return generate(context, input);
      }
    } catch (NumberFormatException nfe) {
      throw new InvalidTypeException(nfe);
    }
  }
Example #3
0
  @Override
  @SuppressWarnings("unchecked")
  public <T> T prompt(final String message, final Class<T> clazz, final T defaultIfEmpty) {
    Object result;
    String input;
    do {
      String query = " [" + defaultIfEmpty + "] ";

      if (defaultIfEmpty instanceof Boolean) {
        if (Boolean.TRUE.equals(defaultIfEmpty)) query = " [Y/n] ";
        else {
          query = " [y/N] ";
        }
      }

      input = prompt(message + query);
      if ((input == null) || "".equals(input.trim())) {
        result = defaultIfEmpty;
      } else {
        input = input.trim();
        try {
          result = DataConversion.convert(input, clazz);
        } catch (Exception e) {
          result = InvalidInput.INSTANCE;
        }
      }
    } while ((result instanceof InvalidInput));

    return (T) result;
  }
Example #4
0
  public static boolean canConvert(final MetaClass to, final MetaClass from) {
    try {
      final Class<?> fromClazz = from.asClass();
      final Class<?> toClass = to.asClass();

      return DataConversion.canConvert(toClass, fromClazz);
    } catch (Throwable t) {
      return false;
    }
  }
Example #5
0
  @Override
  @SuppressWarnings("unchecked")
  public <T> T prompt(final String message, final Class<T> clazz) {
    Object result;
    Object input;
    do {
      input = prompt(message);
      try {
        result = DataConversion.convert(input, clazz);
      } catch (Exception e) {
        result = InvalidInput.INSTANCE;
      }
    } while ((result instanceof InvalidInput));

    return (T) result;
  }
Example #6
0
  public void testDataConverterStrictMode() throws Exception {
    OptimizerFactory.setDefaultOptimizer("ASM");

    DataConversion.addConversionHandler(Date.class, new MVELDateCoercion());

    ParserContext ctx = new ParserContext();
    ctx.addImport("Cheese", Cheese.class);
    ctx.setStrongTyping(true);
    ctx.setStrictTypeEnforcement(true);

    Locale.setDefault(Locale.US);

    Cheese expectedCheese = new Cheese();
    expectedCheese.setUseBy(new SimpleDateFormat("dd-MMM-yyyy").parse("10-Jul-1974"));

    ExpressionCompiler compiler =
        new ExpressionCompiler("c = new Cheese(); c.useBy = '10-Jul-1974'; return c");
    Cheese actualCheese = (Cheese) executeExpression(compiler.compile(ctx), createTestMap());
    assertEquals(expectedCheese.getUseBy(), actualCheese.getUseBy());
  }