public static void encryptFields ( Object object , CollectionMetaData cmd , ICipher cipher ) throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { for ( String secretAnnotatedFieldName : cmd . getSecretAnnotatedFieldNames ( ) ) { Method getterMethod = cmd . getGetterMethodForFieldName ( secretAnnotatedFieldName ) ; Method setterMethod = cmd . getSetterMethodForFieldName ( secretAnnotatedFieldName ) ; String value ; String encryptedValue = null ; try { value = ( String ) getterMethod . invoke ( object ) ; if ( null != value ) { encryptedValue = cipher . encrypt ( value ) ; setterMethod . invoke ( object , encryptedValue ) ; } } catch ( IllegalAccessException | IllegalArgumentException | InvocationTargetException e ) { logger . error ( "Error when invoking method for a @Secret annotated field" , e ) ; throw e ; } } }
A utility method to encrypt the value of field marked by the