public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
   if (Object.class.equals(method.getDeclaringClass())) {
     try {
       return method.invoke(this, args);
     } catch (Throwable t) {
       throw ExceptionUtil.unwrapThrowable(t);
     }
   }
   final MapperMethod mapperMethod = cachedMapperMethod(method);
   return mapperMethod.execute(sqlSession, args);
 }
Ejemplo n.º 2
0
 public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy)
     throws Throwable {
   final String methodName = method.getName();
   try {
     synchronized (lazyLoader) {
       if (WRITE_REPLACE_METHOD.equals(methodName)) {
         Object original = null;
         if (constructorArgTypes.isEmpty()) {
           original = objectFactory.create(type);
         } else {
           original = objectFactory.create(type, constructorArgTypes, constructorArgs);
         }
         PropertyCopier.copyBeanProperties(type, enhanced, original);
         if (lazyLoader.size() > 0) {
           return new CglibSerialStateHolder(
               original,
               lazyLoader.getProperties(),
               objectFactory,
               constructorArgTypes,
               constructorArgs);
         } else {
           return original;
         }
       } else {
         if (lazyLoader.size() > 0 && !FINALIZE_METHOD.equals(methodName)) {
           if (aggressive || lazyLoadTriggerMethods.contains(methodName)) {
             lazyLoader.loadAll();
           } else if (PropertyNamer.isProperty(methodName)) {
             final String property = PropertyNamer.methodToProperty(methodName);
             if (lazyLoader.hasLoader(property)) {
               lazyLoader.load(property);
             }
           }
         }
       }
     }
     return methodProxy.invokeSuper(enhanced, args);
   } catch (Throwable t) {
     throw ExceptionUtil.unwrapThrowable(t);
   }
 }
Ejemplo n.º 3
0
  public static <T> List<ColumnModel> getColumnModelListForUpdate(
      Configuration configuration,
      Class<?> entityClass,
      T entity,
      boolean isMapUnderscoreToCamelCase,
      Map<String, Object> parameterMap) {
    MetaClass metaClass = MetaClass.forClass(entityClass, configuration.getReflectorFactory());
    String[] readablePropertyNames = metaClass.getGetterNames();

    List<ColumnModel> columnModelList = new ArrayList<ColumnModel>();
    ColumnModel columnModel = null;
    for (String propertyName : readablePropertyNames) {
      Method getterMethod = findGetterMethod(propertyName, entityClass);
      if (getterMethod == null) {
        continue;
      }

      if (AnnotationUtils.findAnnotation(getterMethod, Transient.class) != null
          || AnnotationUtils.findAnnotation(getterMethod, CreatedDate.class) != null) {
        continue;
      }

      columnModel = new ColumnModel();
      try {
        Invoker method = metaClass.getGetInvoker(propertyName);
        try {
          Object value = method.invoke(entity, null);
          String columnName = getColumnName(propertyName, isMapUnderscoreToCamelCase);

          // Handle PK
          if (AnnotationUtils.findAnnotation(getterMethod, Id.class) != null) {
            Assert.notNull(value, propertyName + " is required");
            parameterMap.put(columnName, value);
            continue;
          }

          // Handle the property that annotated by ${#link ModifiedDate}
          if (AnnotationUtils.findAnnotation(getterMethod, ModifiedDate.class) != null) {
            if (methodReturnTypeCheck(getterMethod, Date.class)) {
              value = new Date();
            }
          }

          if (value != null
              || AnnotationUtils.findAnnotation(getterMethod, NullableForUpdate.class) != null) {
            // Handle the property that annotated by {#link Enumerated}
            Enumerated enumrated = AnnotationUtils.findAnnotation(getterMethod, Enumerated.class);
            if (enumrated != null) {
              if (enumrated.value() == EnumType.ORDINAL) {
                Class<?> returnType = getterMethod.getReturnType();
                value =
                    Enum.valueOf(returnType.asSubclass(Enum.class), String.valueOf(value))
                        .ordinal();
              }
            }

            // Handle the property that annotated by {#link Association}
            if (AnnotationUtils.findAnnotation(getterMethod, Association.class) != null) {
              Association association =
                  AnnotationUtils.findAnnotation(getterMethod, Association.class);
              String joinColumn = association.joinColumn();
              if (joinColumn == null || "".equals(joinColumn.trim())) {
                columnName = columnName + "_id";
              } else {
                columnName = association.joinColumn();
              }

              Class<?> returnType = getterMethod.getReturnType();
              MetaClass associationMetaClass =
                  MetaClass.forClass(returnType, configuration.getReflectorFactory());
              Invoker m =
                  associationMetaClass.getGetInvoker(
                      "id"); // TODO, now just support the primary key that named "id"
              value = m.invoke(value, null);
            }

            columnModel.setName(columnName);
            columnModel.setValue(value);
            columnModelList.add(columnModel);
          }

        } catch (Throwable t) {
          throw ExceptionUtil.unwrapThrowable(t);
        }
      } catch (Throwable t) {
        throw new ReflectionException(
            "Could not get the value of property '"
                + propertyName
                + "' of '"
                + entityClass
                + "' Cause: "
                + t.toString(),
            t);
      }
    }

    return columnModelList;
  }
Ejemplo n.º 4
0
  public static <T> T populate(
      Configuration configuration,
      Class<?> entityClass,
      T entity,
      Map<String, Object> resultMap,
      boolean isMapUnderscoreToCamelCase) {
    MetaClass metaClass = MetaClass.forClass(entityClass, configuration.getReflectorFactory());

    for (String key : resultMap.keySet()) {
      String property = metaClass.findProperty(key, isMapUnderscoreToCamelCase);
      if (property == null) {
        property =
            metaClass.findProperty(
                key.replace("_id", ""),
                isMapUnderscoreToCamelCase); // just for association entity property
        if (property == null) {
          property = getPropertyNameForAssociationEntity(entityClass, metaClass, key);
        }
      }

      if (property != null && metaClass.hasSetter(property)) {
        Object value = resultMap.get(key);
        try {
          Method getterMethod = findGetterMethod(property, entityClass);

          // Handle the property that annotated by {#link Enumerated}.
          Enumerated enumrated = AnnotationUtils.findAnnotation(getterMethod, Enumerated.class);
          if (enumrated != null) {
            Class<?> returnType = getterMethod.getReturnType();
            if (enumrated.value() == EnumType.STRING) {
              value = Enum.valueOf(returnType.asSubclass(Enum.class), String.valueOf(value));
            } else {
              value = returnType.asSubclass(Enum.class).getEnumConstants()[(int) value];
            }
          }

          // Handler the property that annotated by {#link Association}
          Association association = AnnotationUtils.findAnnotation(getterMethod, Association.class);
          if (association != null) {
            Class<?> returnType = getterMethod.getReturnType();
            MetaClass associationMetaClass =
                MetaClass.forClass(returnType, configuration.getReflectorFactory());
            Invoker m = associationMetaClass.getSetInvoker("id");
            Object associationEntity = returnType.newInstance();
            Object[] params = {value};
            m.invoke(associationEntity, params);
            value = associationEntity;
          }

          Invoker method = metaClass.getSetInvoker(property);
          Object[] params = {value};
          try {
            method.invoke(entity, params);
          } catch (Throwable t) {
            throw ExceptionUtil.unwrapThrowable(t);
          }
        } catch (Throwable t) {
          throw new ReflectionException(
              "Could not set property '"
                  + property
                  + "' of '"
                  + entityClass
                  + "' with value '"
                  + value
                  + "' Cause: "
                  + t.toString(),
              t);
        }
      }
    }

    return entity;
  }