I am currently working on an Android project that stores data using Google Datastore and is accessible via the Cloud Endpoints with the Objectify library. The objectify library does a decent job explaining polymorphism using the "@Subclass" annotation however the problem I'm having is that when I generate the client library for use in the Android code the sub-classes are not included. Is there a way to force the compiler to include these classes?
Here is a basic example of what I'm doing. Lets take the polymorphism example from the Objectify documentation:
@Entity
public class Animal {
@Id Long id;
String name;
}
@Subclass(index=true)
public class Mammal extends Animal {
boolean longHair;
}
@Subclass(index=true)
public class Cat extends Mammal {
boolean hypoallergenic;
}
It's worth mentioning that all three of these classes have been registered with "ObjectifyService.factory().register()." Using the classes above I have a method in my api that looks similar to this:
@ApiMethod(
name = "getAnimals", path = "zoo/getAnimals", httpMethod = ApiMethod.HttpMethod.GET
)
public Collection<Animal> getAnimals(final User user, @Named("IdList") Collection<Long> idList)
throws UnauthorizedException {
// If the user is not logged in, throw an UnauthorizedException
if (user == null) {
throw new UnauthorizedException("Authorization required");
}
Map<Long,Animal> animalMap = ofy().load().type(Animal.class).ids(idList);
ArrayList<Animal> animals= new ArrayList<>();
for (Long animalId:idList) {
animals.add(animalMap.get(animalId));
}
return animals;
}
As you can see the final ArrayList "animals" could contain Animals, Mammals, or Cats however the generated library only gives me access to the Animal class. I need to make sure that the sub-classes are available to the Android code. How does the compiler know which classes to include and is there a way to force a class to be added? Does the polymorphism even carry over to the client side or is that only supported server side?
This is not a great answer but you could expose a no-op API which returns those types (in reality just return null;). This should force the class generation. If there's a more formal way to do this, feel free to correct me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.