uPhyca / gradle-android-aspectj-plugin

A Gradle plugin which enables AspectJ for Android builds.
Apache License 2.0
279 stars 55 forks source link

@DeclareMixin does not work #21

Closed RafaelKa closed 9 years ago

RafaelKa commented 9 years ago

Hello,

i tried to introduce Mixins in my models,

PersistenceMagicAspect.java:

package de.thm.sagprojekt.Persistence.Aspect;

import org.aspectj.lang.annotation.*;
import de.thm.sagprojekt.Persistence.Aspect.Mixins.EntityImplementation;
import de.thm.sagprojekt.Persistence.Aspect.Mixins.EntityInterface;
import de.thm.sagprojekt.Persistence.Annotation.AggregateRoot;

@Aspect
public class PersistenceMagicAspect {

    @DeclareParents(value = "isEntity()", defaultImpl = EntityImplementation.class)
    public EntityInterface mixin;

    @Pointcut("within(@de.thm.sagprojekt.Persistence.Annotation.Entity *) || within(@de.thm.sagprojekt.Persistence.Annotation.AggregateRoot *)")
    public void isEntity() {}

    @Pointcut("execution(public * set*(..))")
    public void isPublicSetMethod() {}

    @Pointcut("isEntity() && isPublicSetMethod()")
    public void entitySetterEntryPoint() {
    }

    @DeclareMixin("@AggregateRoot *") // tried "isEntity() *" etc.
    public EntityInterface entityMixin(){
        return new EntityImplementation();
    }
}

AggregateRoot.java (Annotation)

package de.thm.sagprojekt.Persistence.Annotation;

import java.lang.annotation.*;

@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface AggregateRoot {
}

EntityInterface.java

package de.thm.sagprojekt.Persistence.Aspect.Mixins;

public interface EntityInterface {
    public Boolean isPersistent();
    public void setPersistent(Boolean persistent);
}

EntityImplementation.java

package de.thm.sagprojekt.Persistence.Aspect.Mixins;

import android.util.Log;

import de.thm.sagprojekt.Persistence.Annotation.Transient;

public class EntityImplementation implements EntityInterface, Cloneable {
    @Transient
    protected Boolean mPersistent = false;

    @Override
    public Boolean isPersistent() {
        Log.d("TESTMAPPER", "isPersistent()");
        return mPersistent;
    }

    @Override
    public void setPersistent(Boolean persistent) {
        Log.d("TESTMAPPER", "setPersistent()");
        this.mPersistent = persistent;
    }
}

but it seems to be wrong. If i use following code my app crashes with XYZ cannot be cast to de.thm.sagprojekt.Persistence.Aspect.Mixins.EntityImplementation

PersistenceManager.java

package de.thm.sagprojekt.Persistence;

import de.thm.sagprojekt.Persistence.Aspect.Mixins.EntityImplementation;
import de.thm.sagprojekt.Persistence.Aspect.Mixins.EntityInterface;

public class PersistenceManager {

    public static boolean isEntityPersistent(Object entity) {
        return ((EntityImplementation)entity).isPersistent();
    }
}

has somebody working example?

PS: i used Api19