@Override
  public Object invokeMethod(String name, Object obj) {
    Object[] args = obj.getClass().isArray() ? (Object[]) obj : new Object[] {obj};

    if (isCriteriaConstructionMethod(name, args)) {

      initializeQuery();

      uniqueResult = false;

      invokeClosureNode(args[0]);

      Object result;
      if (!uniqueResult) {
        result = query.list();
      } else {
        result = query.singleResult();
      }
      query = null;
      return result;
    }

    MetaMethod metaMethod = getMetaClass().getMetaMethod(name, args);
    if (metaMethod != null) {
      return metaMethod.invoke(this, args);
    }

    metaMethod = queryMetaClass.getMetaMethod(name, args);
    if (metaMethod != null) {
      return metaMethod.invoke(query, args);
    }

    if (args.length == 1 && args[0] instanceof Closure) {

      final PersistentProperty property = persistentEntity.getPropertyByName(name);
      if (property instanceof Association) {
        Association association = (Association) property;
        Query previousQuery = query;
        PersistentEntity previousEntity = persistentEntity;

        Query associationQuery = null;
        try {
          associationQuery = query.createQuery(property.getName());
          if (associationQuery instanceof AssociationQuery) {
            previousQuery.add((Query.Criterion) associationQuery);
          }
          query = associationQuery;
          persistentEntity = association.getAssociatedEntity();
          invokeClosureNode(args[0]);
          return query;
        } finally {

          persistentEntity = previousEntity;
          query = previousQuery;
        }
      }
    }

    throw new MissingMethodException(name, getClass(), args);
  }
 @SuppressWarnings("unchecked")
 public boolean onPreUpdate(PreUpdateEvent event) {
   Object entity = event.getEntity();
   boolean evict = false;
   if (preUpdateEventListener != null) {
     evict = preUpdateEventListener.call(entity);
     synchronizePersisterState(entity, event.getPersister(), event.getState());
   }
   if (lastUpdatedProperty != null && shouldTimestamp) {
     Object now =
         DefaultGroovyMethods.newInstance(
             lastUpdatedProperty.getType(), new Object[] {System.currentTimeMillis()});
     event
             .getState()[
             ArrayUtils.indexOf(
                 event.getPersister().getPropertyNames(),
                 GrailsDomainClassProperty.LAST_UPDATED)] =
         now;
     lastUpdatedProperty.setProperty(entity, now);
   }
   if (!AbstractSavePersistentMethod.isAutoValidationDisabled(entity)
       && !DefaultTypeTransformation.castToBoolean(
           validateMethod.invoke(entity, new Object[] {validateParams}))) {
     evict = true;
     if (failOnErrorEnabled) {
       Errors errors = (Errors) errorsProperty.getProperty(entity);
       throw new ValidationException(
           "Validation error whilst flushing entity [" + entity.getClass().getName() + "]",
           errors);
     }
   }
   return evict;
 }
 @Override
 public boolean call(Object entity) {
   Object retval = method.invoke(entity, EMPTY_OBJECT_ARRAY);
   return resolveReturnValue(retval);
 }
示例#4
0
 public Object invoke(Object object, Object[] arguments) {
   // make sure parameterTypes gets set
   method.getParameterTypes();
   return method.invoke(
       mixinInMetaClass.getMixinInstance(object), method.correctArguments(arguments));
 }