Thanks Andy, That's what I was thinking as well. However, one thing that I haven't been able to find is how to get the type of obj that needs to be instantiated. Currently, it is a Validity object. However, if I wanted to do this more generically, I would want to do something like:
value = thisJoinPoint.getSignature().fieldType.newInstance() However, I can't seem to find any way to get "fieldType". Is that information not accessible anywhere? If not, is there a way to get the expected return type of the get() pointcut? Tx, Eric On Tue, Jul 8, 2014 at 5:26 AM, Andy Clement <andrew.clem...@gmail.com> wrote: > I only had a couple of mins to play but if you went with around advice you > could make the reflection only run if initialization is required: > > Object *around*(): embeddedGetter() { > > Object value = *proceed*(); > > *if* (value == *null*) { > > String fieldName = *thisJoinPoint*.getSignature().getName(); > > Object obj = *thisJoinPoint*.getThis(); > > *try*{ > > Field field = obj.getClass().getDeclaredField(fieldName); > > field.setAccessible(*true*); > > field.set(obj, value = *new* Validity() ); > > } > > *catch*( IllegalAccessException | NoSuchFieldException > e){e.printStackTrace();} > > } > > *return* value; > > } > > > Andy > > > On 8 July 2014 04:34, Eric B <ebenza...@gmail.com> wrote: > >> I've got the following issue that I am trying to solve with AspectJ. >> >> Given an entity class with a null @Embedded field, when trying to access >> it with a getter, instantiate it first if it is null. >> >> For example: >> >> @Entity >> public class MyClass { >> >> @Id >> private long id; >> >> @Embedded >> private Validity validity; >> } >> >> >> And Validity: >> >> @Embeddable >> public class Validity{ >> private long from; >> private long to; >> } >> >> I'm having trouble figuring out how to best write the before() advice >> however. Ideally, I'm trying to avoid using reflection for fear of slowing >> things down, but so far, the best I have been able to come up with is the >> following: >> >> // define a pointcut for any getter method of a field with @Embedded of >> type Validity with any name in com.ia.domain package >> pointcut embeddedGetter() : get( @javax.persistence.Embedded >> com.ia.domain.Validity com.ia.domain..* ); >> before() : embeddedGetter(){ >> String fieldName = thisJoinPoint.getSignature().getName(); >> Object obj = thisJoinPoint.getThis(); >> // check to see if the obj has the field already defined or is null >> try{ >> Field field = obj.getClass().getDeclaredField(fieldName); >> field.setAccessible(true); >> if( field.get(obj) == null ) >> field.set(obj, new com.ia.domain.Validity() ); >> } >> catch( IllegalAccessException | NoSuchFieldException e){} >> } >> >> >> Is there a better way? >> >> Thanks, >> >> Eric >> >> >> >> >> _______________________________________________ >> aspectj-users mailing list >> aspectj-users@eclipse.org >> To change your delivery options, retrieve your password, or unsubscribe >> from this list, visit >> https://dev.eclipse.org/mailman/listinfo/aspectj-users >> > > > _______________________________________________ > aspectj-users mailing list > aspectj-users@eclipse.org > To change your delivery options, retrieve your password, or unsubscribe > from this list, visit > https://dev.eclipse.org/mailman/listinfo/aspectj-users >
_______________________________________________ aspectj-users mailing list aspectj-users@eclipse.org To change your delivery options, retrieve your password, or unsubscribe from this list, visit https://dev.eclipse.org/mailman/listinfo/aspectj-users