0

I am to new to Xtext. I need to extend an interface with interface. I need something like this:

import org.springframework.data.jpa.repository.JpaRepository;

@Repository
public interface PHRRepository extends
        JpaRepository<PlantHireRequest, Long> {

}

My grammar:

Repository:
  'repo' name=ValidID ':' type=JvmTypeReference
     body=XBlockExpression;

My JVMinferrer code:

 @Inject
  private TypesFactory typesFactory;

  @Inject
  private TypeReferences references;

  public static String REPOSITORY = "org.springframework.stereotype.Repository";
  public static String JPAREPOSITORY = "org.springframework.data.jpa.repository.JpaRepository";

   //repositories
   def dispatch void infer(Repository repo, 
                IJvmDeclaredTypeAcceptor acceptor, 
                boolean isPrelinkingPhase) {   
      acceptor.accept(repo.toInterface(repo.name, null)) [      
          documentation = repo.documentation
          annotations += annotationRef(REPOSITORY);
  superTypes += JPAREPOSITORY.typeRef(repo.type.cloneWithProxies,Long.typeRef); 
    ]
  }

which gives this:

@Repository
public interface PHRRepository {
}

Can anyone help me in this?

1 Answers1

0

the following works fine for me

    accept(entity.toInterface("I"+entity.name,[]))[
        superTypes += IEntity.typeRef
    ]

or

    accept(entity.toInterface("I"+entity.name,[]))[
         if (entity.superType != null) //asuming entity.supertype is modeled to be an interface
             superTypes += entity.superType.cloneWithProxies
    ]

if you want generics you can use

JpaRepository.typeRef(PlantHireRequest.typeRef, Long.typeRef) 

or

"x.y.Z".typeRef("a.b.C".typeRef(), "java.lang.Long".typeRef())

mixing currently does not work so as a workaround you can use

superTypes += "x.y.Z".typeRef(entity.superType.qualifiedName.typeRef(), "java.lang.Long".typeRef())
Christian Dietrich
  • 11,778
  • 4
  • 24
  • 32
  • Thanks for your reply.But I need to extend "JpaRepository". I am struck here. I have updated my question. – Vinod John Feb 07 '16 at 12:08
  • added hints to my answer as well – Christian Dietrich Feb 07 '16 at 20:07
  • Thanks. Your answer update extended JpaRepository. But not the arguments "". I have updated my question again. Also interfaces were not generated inside folder. It is generated inside root. How to make generate inside a folder. – Vinod John Feb 07 '16 at 22:21
  • Are you sure you have the class path right. And you should add one super type only once. And you have to use type ref for the Params as well The point about the folder/root I do not understand – Christian Dietrich Feb 08 '16 at 05:35
  • And you have to use clonewithproxies if the type ref comes from the model – Christian Dietrich Feb 08 '16 at 05:43
  • Changed as per your previous comment. But doesn't work. Updated code in question – Vinod John Feb 08 '16 at 08:19
  • can you file a bug for that and use as workaround ``` superTypes += "x.y.Z".typeRef(entity.superType.qualifiedName.typeRef(), "java.lang.Long".typeRef())``` – Christian Dietrich Feb 08 '16 at 11:27
  • Yes. Before that, should I inject anything additionally in order to get this? Currently I have injected JvmTypesBuilder and IQualifiedNameProvider. – Vinod John Feb 08 '16 at 11:53
  • no i have no additional injectons. (i use xtext 2.9.1). so once more the question: is your classpath fine? does it work if you use 3 strings? – Christian Dietrich Feb 08 '16 at 19:11
  • btw the following works perfectly for me in the domainmodel example ``` accept(entity.toInterface("I"+entity.name,[]))[ if (entity.superType != null) superTypes += "org.springframework.data.jpa.repository.JpaRepository".typeRef(entity.superType.qualifiedName.typeRef(), "java.lang.Long".typeRef()) ]``` – Christian Dietrich Feb 08 '16 at 19:23
  • The classpath is fine and works as well with 3 strings. But it is not generating the parameters only. – Vinod John Feb 08 '16 at 22:52
  • In my example I get it correctly generated. I don't know what is you actual problem. As said it won't work if you mix strings and non strings (imho a bug) – Christian Dietrich Feb 09 '16 at 05:59
  • Even I couldn't able to understand the actual problem here. I also tried with all as Strings without mixing up. Still it didn't work. – Vinod John Feb 10 '16 at 00:44
  • can you share some project that reproduces the problem? the dsl projects and the model project you use? – Christian Dietrich Feb 10 '16 at 05:10