Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With AspectJ you can modify methods and fields with advises.</p> <p>My example is written with <code>@AspectJ</code> syntax which modifies the code at compile-time or load-time. If you want the modification at runtime, you can use Spring AOP which also supports this <code>@AspectJ</code> syntax.</p> <p>An example with a simple Person class and a stub repository. All information about which fields are updated is handled by an aspect called SetterAspect. It monitors which fields that are updated when the fields is written to. </p> <p>The other advice in this example is around the update method in the repository. This is to retrieve the data collected from the first aspect.</p> <p>The Person class:</p> <pre><code>public class Person { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public static void main(String[] args) { Person person = new Person(); person.setFirstName("James"); person.lastName = "Jameson"; DtoRepository&lt;Person&gt; personRepository = new DtoRepository&lt;Person&gt;(); personRepository.update(person); } } </code></pre> <p>The stub repository:</p> <pre><code>public class DtoRepository&lt;T&gt; { public void update(T t) { System.out.println(t.getClass().getSimpleName() + " updated.."); } public void updatePerson(T t, Set&lt;String&gt; updatedFields) { System.out.print("Updated the following fields on " + t.getClass().getSimpleName() + " in the repository: " + updatedFields); } } </code></pre> <p>The output for executing the <code>main()</code> method in the Person class with AspectJ:</p> <blockquote> <p>Updated the following fields on Person in the repository: [lastName, firstName]</p> </blockquote> <p>Important to note here is that the main() method calls on the <code>DtoRepository.update(T t)</code> but the <code>DtoRepository.update(T t, Set&lt;String&gt; updatedFields)</code> gets executed because of the around advice in the repository aspect.</p> <p>The aspect that monitors all writing to private fields in the demo package:</p> <pre><code>@Aspect public class SetterAspect { private UpdatableDtoManager updatableDtoManager = UpdatableDtoManager.INSTANCE; @Pointcut("set(private * demo.*.*)") public void setterMethod() {} @AfterReturning("setterMethod()") public void afterSetMethod(JoinPoint joinPoint) { String fieldName = joinPoint.getSignature().getName(); updatableDtoManager.updateObjectWithUpdatedField( fieldName, joinPoint.getTarget()); } } </code></pre> <p>The repository aspect:</p> <pre><code>@Aspect public class UpdatableDtoRepositoryAspect { private UpdatableDtoManager updatableDtoManager = UpdatableDtoManager.INSTANCE; @Pointcut("execution(void demo.DtoRepository.update(*)) " + "&amp;&amp; args(object)") public void updateMethodInRepository(Object object) {} @Around("updateMethodInRepository(object)") public void aroundUpdateMethodInRepository( ProceedingJoinPoint joinPoint, Object object) { Set&lt;String&gt; updatedFields = updatableDtoManager.getUpdatedFieldsForObject(object); if (updatedFields.size() &gt; 0) { ((DtoRepository&lt;Object&gt;)joinPoint.getTarget()). updatePerson(object, updatedFields); } else { // Returns without calling the repository. System.out.println("Nothing to update"); } } } </code></pre> <p>Finally, the two helper classes used by the aspects:</p> <pre><code>public enum UpdatableDtoManager { INSTANCE; private Map&lt;Object, UpdatedObject&gt; updatedObjects = new HashMap&lt;Object, UpdatedObject&gt;(); public void updateObjectWithUpdatedField( String fieldName, Object object) { if (!updatedObjects.containsKey(object)) { updatedObjects.put(object, new UpdatedObject()); } UpdatedObject updatedObject = updatedObjects.get(object); if (!updatedObject.containsField(fieldName)) { updatedObject.add(fieldName); } } public Set&lt;String&gt; getUpdatedFieldsForObject(Object object) { UpdatedObject updatedObject = updatedObjects.get(object); final Set&lt;String&gt; updatedFields; if (updatedObject != null) { updatedFields = updatedObject.getUpdatedFields(); } else { updatedFields = Collections.emptySet(); } return updatedFields; } } </code></pre> <p>and</p> <pre><code>public class UpdatedObject { private Map&lt;String, Object&gt; updatedFields = new HashMap&lt;String, Object&gt;(); public boolean containsField(String fieldName) { return updatedFields.containsKey(fieldName); } public void add(String fieldName) { updatedFields.put(fieldName, fieldName); } public Set&lt;String&gt; getUpdatedFields() { return Collections.unmodifiableSet( updatedFields.keySet()); } } </code></pre> <p>My example does all the update logic with aspects. If all the DTOs implemented an interface that returns a <code>Set&lt;String&gt;</code>, you could have avoided the last aspect.</p> <p>I hope this answered your question!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload