protected Serializable getIdValue(final Object domainObject) { final Class<?> clazz = domainObject.getClass(); // 直接用getId try { final AclManaged aclManaged = AnnotationUtils.findAnnotation(clazz, AclManaged.class); final Method getIdMethod = clazz.getMethod(aclManaged.getIdMethodName()); if (getIdMethod != null) { final Object id = ReflectionUtils.invokeMethod(getIdMethod, domainObject); if (id == null) { logger.warn("此对象id为空:" + domainObject); return null; } if (!(id instanceof Serializable)) { logger.warn("此对象id不能序列化:" + domainObject + " ,id:" + id); // 来到这里就扯淡了吧? return null; } return (Serializable) id; } } catch (final NoSuchMethodException e) { // 没有getId方法就不用它呗 } catch (final SecurityException e) { logger.debug(domainObject.getClass().getName() + "getId方法访问不能访问。", e); } // 没有getId方法 // 用标注了@Id注解的字段 final Field[] fields = clazz.getDeclaredFields(); Serializable idValueStr = concatIdValues(domainObject, fields); if (idValueStr == null) { idValueStr = concatIdValues(domainObject, ReflectionUtils.getAllDeclaredMethods(clazz)); } if (idValueStr == null) { logger.warn(domainObject.getClass().getName() + "没有id字段。"); return null; } if (String.valueOf(idValueStr).isEmpty()) { logger.warn("此对象的id字段值都是空字符串:" + domainObject); return null; } return idValueStr; }
@Override public ObjectIdentity getObjectIdentity(final Object domainObject) { final AclManaged aclManaged = AnnotationUtils.findAnnotation(domainObject.getClass(), AclManaged.class); // acl的所谓的class_id建议用AclClassId注解指定! final String typeName; if (aclManaged == null) { typeName = domainObject.getClass().getName(); } else { if (aclManaged.value().isEmpty()) { logger.warn(domainObject.getClass() + "的AclClassId值为空"); typeName = domainObject.getClass().getName(); } else { typeName = aclManaged.value(); } } final Serializable idValue = getIdValue(domainObject); if (idValue == null) { return null; } return new ObjectIdentityImpl(typeName, idValue); }