0

I'm lazy person... i just want the simplest way, now i'm learning android room. This might be answered here "Why is it not possible to extend annotations in Java?", but it seems like to try pass annotation to another annotation, right? So, Could i pass annotation to the child class for example, here is my parent class 'Model':

@Entity
public abstract class Model implements Parcelable{
    @Nullable
    @PrimaryKey
    private Object id;

    public String toJSON(){
        return new Gson().toJson(this);
    }

    public <T> T getId(Class<T>type) {
        if(type.equals(Integer.class)||type.equals(int.class)) {
            return (T) Integer.valueOf(new Double(Double.parseDouble(String.valueOf(id))).intValue());
        }
        else if(type.equals(Long.class)||type.equals(long.class)) {
            return (T) Long.valueOf(new Double(Double.parseDouble(String.valueOf(id))).longValue());
        }
        else if(type.equals(Double.class)||type.equals(double.class)) {
            return (T) Double.valueOf(id.toString());
        }else if(type.equals(UUID.class)){
            return (T) UUID.fromString(String.valueOf(id));
        }
        return type.cast(id);
    }

    public <T> void setId(T id) {
        this.id = id;
    }
}

I tried to pass @Entity to child class.

public class Movie extends Model {
    public static final String IDENTIFIER = Movie.class.getSimpleName();
    @SerializedName("popularity")
    private double popularity;
    @SerializedName("vote_count")
    private long voteCount;
    @SerializedName("video")
    private boolean video;
}

Because my data access object cannot detect my 'Movie' class, except i'm added Entity annotation

@Dao
public interface FavMovieDAO {
    @Query("SELECT * FROM Movie WHERE id = :movieId")
    public Movie find (int movieId);
}

If there is no way, i'll declare my entire model class as an Entity... :/. Thanks in advance.

  • I think you want to check out [Embedded](https://developer.android.com/reference/android/arch/persistence/room/Embedded) – MikeT Oct 14 '19 at 01:13
  • If not, maybe [@Inherited](https://stackoverflow.com/questions/23973107/how-to-use-inherited-annotation-in-java) – ChiefTwoPencils Oct 14 '19 at 01:15

0 Answers0