I've been trying to get this to work for a few days and have been failing miserably at it. I cannot seem to get my class cast properly to an Introduced interface. I'm using Spring 3.0.5.
Does anyone have a complete working example of a project that uses @DeclareParents? Or the XML equivalent? I have found a bunch of snippets on the web, but haven't had success in getting those to work either.
This is my first time trying Introductions, but am having a lot of difficulty getting this to work. I've read through the Spring docs and the AspectJ docs as well as well as the forums but still cannot get this to work. I thought created the classes/interfaces correctly, but when I try to cast the Advised object to the interface, I get a cast error.
Target:
@Entity @Table(name = "item") public class Item implements java.io.Serializable { private long id; private Integer mainType; private Integer subType; private String brandName; private String itemName; private Date releaseDate; private Date retireDate; private String fileName; private String thumbnailName; private String dimension; private boolean active; private boolean unlocked; private Integer unlockCost; private boolean owned; public Item() { } ... // bunch of getters and setters ... }
Interface:
public interface AbstractBaseEntity { /** * @return the customAttributes */ public Object getCustomAttribute(String name); /** * @param customAttributes the customAttributes to set */ public void setCustomAttribute(String name, Object value); }
Implementation:
public class AbstractBaseEntityImpl implements AbstractBaseEntity { /** * Map of custom attributes that can be mapped for the specific entity */ @Transient protected Map customAttributes = new ConcurrentHashMap(); /** * @return the customAttributes */ public Object getCustomAttribute( String name ) { return customAttributes.get(name); } /** * @param customAttributes the customAttributes to set */ public void setCustomAttribute(String name, Object value) { this.customAttributes.put(name, value); } }
Aspect:
@DeclareParents(value="com.fwl.domain.model.*+", defaultImpl=AbstractBaseEntityImpl.class) public AbstractBaseEntity mixin;
However, when I pass my introduced object to a method as an Object parameter, checking if it is an instanceof AbstractBaseEntity, it returns false.
public void localize(Object entity, Locale locale) { List cachedFields; if (entity == null) // do nothing return; // check if the entity is already localized if( entity instanceof AbstractBaseEntity) // already localized so nothing to do return; ... ... }
Is there anyway to ensure that the introduction is being done properly? Anyway to determine why I cannot cast it as an AbstractBaseEntity?
Any help would be greatly appreciated.
Thanks,
Eric