/** * Creates the <code>ObjectIdentityImpl</code> based on the passed object instance. * The passed object must provide a <code>getId()</code> method, otherwise an * exception will be thrown. * <p> * The class name of the object passed will be considered the {@link #type}, so if * more control is required, a different constructor should be used. * * @param object the domain object instance to create an identity for. * * @throws IdentityUnavailableException if identity could not be extracted */ public ObjectIdentityImpl(Object object) throws IdentityUnavailableException { Assert.notNull(object, "object cannot be null"); Class<?> typeClass = ClassUtils.getUserClass(object.getClass()); type = typeClass.getName(); Object result; try { Method method = typeClass.getMethod("getId", new Class[] {}); result = method.invoke(object); } catch (Exception e) { throw new IdentityUnavailableException( "Could not extract identity from object " + object, e); } Assert.notNull(result, "getId() is required to return a non-null value"); Assert.isInstanceOf(Serializable.class, result, "Getter must provide a return value of type Serializable"); this.identifier = (Serializable) result; }
/** * Creates the <code>ObjectIdentityImpl</code> based on the passed object instance. * The passed object must provide a <code>getId()</code> method, otherwise an * exception will be thrown. * <p> * The class name of the object passed will be considered the {@link #type}, so if * more control is required, a different constructor should be used. * * @param object the domain object instance to create an identity for. * * @throws IdentityUnavailableException if identity could not be extracted */ public ObjectIdentityImpl(Object object) throws IdentityUnavailableException { Assert.notNull(object, "object cannot be null"); Class<?> typeClass = ClassUtils.getUserClass(object.getClass()); type = typeClass.getName(); Object result; try { Method method = typeClass.getMethod("getId", new Class[] {}); result = method.invoke(object); } catch (Exception e) { throw new IdentityUnavailableException( "Could not extract identity from object " + object, e); } Assert.notNull(result, "getId() is required to return a non-null value"); Assert.isInstanceOf(Serializable.class, result, "Getter must provide a return value of type Serializable"); this.identifier = (Serializable) result; }