Using Roaster, how can I create an interface with a specific generic type (or types)?

I am currently using Roaster to create interfaces, but my interface has common types associated with it.

Here is what I tried to generate for starters:

String entityName = "SimpleEntity"; JavaInterfaceSource repository = Roaster.create(JavaInterfaceSource.class) .setName(entityName + "Repository"); JavaInterfaceSource jpaInterface = repository.addInterface(JpaRepository.class); jpaInterface.addTypeVariable(entityName); jpaInterface.addTypeVariable("String"); 

But the above results generate code that looks (something) like this:

 public interface SimpleEntityRepository<SimpleEntity> extends org.springframework.data.jpa.repository.JpaRepository { } 

In fact, I want the generic attribute to bind to a JpaRepository . How to do it?

+8
java roaster
source share
1 answer

JavaInterfaceSource#addInterface overloaded with a String signature. This means that you can create a generic type by doing some clever string concatenation. It also returns the same JavaInterfaceSource instance as in the jpaInterface == repository example above, so the operation is unnecessary and misleading.

Since it is overloaded with String , we just add the generics (read: angle brackets) that we want ourselves.

 repository.addInterface(JpaRepository.class.getSimpleName() + "<" + entityName + ", String>"); 

It may not be as elegant as the rest of the API, but it will ultimately generate the correct object.

 public interface SimpleEntityRepository extends JpaRepository<SimpleEntity, String> { } 
+3
source share

All Articles