Exemple #1
0
 public Object eject(Object obj) {
   try {
     return null == obj ? null : getter.invoke(obj);
   } catch (InvocationTargetException e) {
     throw new FailToGetValueException("getter=" + getter, e);
   } catch (Exception e) {
     if (log.isInfoEnabled()) log.info("Fail to value by getter", e);
     throw Lang.makeThrow(
         "Fail to invoke getter %s.'%s()' because [%s]: %s",
         getter.getDeclaringClass().getName(),
         getter.getName(),
         Lang.unwrapThrow(e),
         Lang.unwrapThrow(e).getMessage());
   }
 }
Exemple #2
0
 public void addCastor(Class<?> klass) {
   try {
     fillMap(klass, new HashMap<Class<?>, Method>());
   } catch (Throwable e) {
     throw Lang.wrapThrow(Lang.unwrapThrow(e));
   }
 }
 public void process(ActionContext ac) throws Throwable {
   Object module = ac.getModule();
   Method method = ac.getMethod();
   Object[] args = ac.getMethodArgs();
   try {
     Object re = method.invoke(module, args);
     ac.setMethodReturn(re);
     doNext(ac);
   } catch (IllegalAccessException e) {
     throw Lang.unwrapThrow(e);
   } catch (IllegalArgumentException e) {
     throw Lang.unwrapThrow(e);
   } catch (InvocationTargetException e) {
     throw Lang.unwrapThrow(e);
   }
 }
Exemple #4
0
 /**
  * 转换一个 POJO 从一个指定的类型到另外的类型
  *
  * @param src 源对象
  * @param fromType 源对象类型
  * @param toType 目标类型
  * @param args 转换时参数。有些 Castor 可能需要这个参数,比如 Array2Map
  * @return 目标对象
  * @throws FailToCastObjectException 如果没有找到转换器,或者转换失败
  */
 @SuppressWarnings({"unchecked", "rawtypes"})
 public <F, T> T cast(Object src, Class<F> fromType, Class<T> toType, String... args)
     throws FailToCastObjectException {
   if (null == src) {
     // 原生数据的默认值
     if (toType.isPrimitive()) {
       if (toType == int.class) return (T) Integer.valueOf(0);
       else if (toType == long.class) return (T) Long.valueOf(0L);
       else if (toType == byte.class) return (T) Byte.valueOf((byte) 0);
       else if (toType == short.class) return (T) Short.valueOf((short) 0);
       else if (toType == float.class) return (T) Float.valueOf(.0f);
       else if (toType == double.class) return (T) Double.valueOf(.0);
       else if (toType == boolean.class) return (T) Boolean.FALSE;
       else if (toType == char.class) return (T) Character.valueOf(' ');
       throw Lang.impossible();
     }
     // 是对象,直接返回 null
     return null;
   }
   if (fromType == toType || toType == null || fromType == null) return (T) src;
   if (fromType.getName().equals(toType.getName())) return (T) src;
   if (toType.isAssignableFrom(fromType)) return (T) src;
   Mirror<?> from = Mirror.me(fromType, extractor);
   if (from.canCastToDirectly(toType)) // Use language built-in cases
   return (T) src;
   Castor c = find(from, toType);
   if (null == c)
     throw new FailToCastObjectException(
         String.format(
             "Can not find castor for '%s'=>'%s' in (%d) because:\n%s",
             fromType.getName(), toType.getName(), map.size(), "Fail to find matched castor"));
   try {
     return (T) c.cast(src, toType, args);
   } catch (FailToCastObjectException e) {
     throw e;
   } catch (Exception e) {
     throw new FailToCastObjectException(
         String.format(
             "Fail to cast from <%s> to <%s> for {%s} because:\n%s:%s",
             fromType.getName(),
             toType.getName(),
             src,
             e.getClass().getSimpleName(),
             e.getMessage()),
         Lang.unwrapThrow(e));
   }
 }
Exemple #5
0
 public Object run(List<Object> param) {
   Object obj = fetchVar();
   Mirror<?> me = null;
   if (obj == null) throw new NullPointerException();
   if (obj instanceof Class) {
     // 也许是个静态方法
     me = Mirror.me(obj);
     try {
       return me.invoke(obj, right.toString(), param.toArray());
     } catch (InvokingException e) {
       throw e;
     } catch (Throwable e) {
       if (Lang.unwrapThrow(e) instanceof NoSuchMethodException) {
         me = Mirror.me(obj.getClass().getClass());
         return me.invoke(obj, right.toString(), param.toArray());
       }
       throw Lang.wrapThrow(e);
     }
   } else {
     me = Mirror.me(obj);
     return me.invoke(obj, right.toString(), param.toArray());
   }
 }