public static < M extends MessageOrBuilder > M updateTimestamp ( final long time , final M messageOrBuilder , final TimeUnit timeUnit ) throws CouldNotPerformException { long milliseconds = TimeUnit . MILLISECONDS . convert ( time , timeUnit ) ; try { if ( messageOrBuilder == null ) { throw new NotAvailableException ( "messageOrBuilder" ) ; } try { // handle builder if ( messageOrBuilder . getClass ( ) . getSimpleName ( ) . equals ( "Builder" ) ) { messageOrBuilder . getClass ( ) . getMethod ( SET + TIMESTAMP_NAME , Timestamp . class ) . invoke ( messageOrBuilder , TimestampJavaTimeTransform . transform ( milliseconds ) ) ; return messageOrBuilder ; } //handle message final Object builder = messageOrBuilder . getClass ( ) . getMethod ( "toBuilder" ) . invoke ( messageOrBuilder ) ; builder . getClass ( ) . getMethod ( SET + TIMESTAMP_NAME , Timestamp . class ) . invoke ( builder , TimestampJavaTimeTransform . transform ( milliseconds ) ) ; return ( M ) builder . getClass ( ) . getMethod ( "build" ) . invoke ( builder ) ; } catch ( IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException ex ) { throw new NotSupportedException ( "Field[Timestamp]" , messageOrBuilder . getClass ( ) . getName ( ) , ex ) ; } } catch ( CouldNotPerformException ex ) { throw new CouldNotPerformException ( "Could not update timestemp! " , ex ) ; } }
Method updates the timestamp field of the given message with the given time in the given timeUnit .