ReferenceContributor from java to custom language

Hi,

I'm trying to write a plugin for a custom language which will resolve reference from java interface to a custom language implementation. So given interface:

@SomeAnnotation("classpath:testImplementation.sth")

 
public interface TestInterface {
    public Integer foo(int b);
}


And implementtion in testImplementation.sth

declare function foo($b) { 0 }

As you can notice from Java code I have all the information that I need to resolve a reference - a filename and a method name. The problem is that when I'm implementing ReferenceContributor I cannot attach it to any reasonable PsiElement. I thought that it should be as easy as

psiReferenceRegistrar.registerReferenceProvider(PsiJavaPatterns.psiElement(PsiMethod.class), new JavaToCustomReferenceProvider());


but it doesn't work. The code from JavaToCustomReferenceProvider doesn't get called. So what am I doing here wrong ? Or maybe should I use some other mechanism for resolving this reference ?

5 comments
Comment actions Permalink

ReferenceContributors work only for elements where they are intended to be used. In Java, those are string literals and comments. A PsiMethod is not a reference, it's a declaration, so it doesn't make any sense for it to support reference contributors.

In your case, since the code in your custom language is implementing a Java interface, you should actually support the "Goto Implementations" action for navigating from the Java interface to your custom language. You can do so by plugging into the definitionsScopedSearch extension point.

0
Comment actions Permalink

I've actually have done it but I'm having problem with retrieving the annotation value.

 
public class JavaToSthMethodDefinitionSearch extends QueryExecutorBase<PsiMethod, DefinitionsScopedSearch.SearchParameters> {

   @Override
   public void processQuery
(DefinitionsScopedSearch.SearchParameters searchParameters, Processor<PsiMethod> processor) {
      PsiMethod psiMethod = (PsiMethod) searchParameters.getElement();
      
System.out.println(psiMethod.getContainingClass().getModifierList());
   
}
}


Results in

[ 120775]  ERROR - plication.impl.ApplicationImpl - Read access is allowed from event dispatch thread or inside read-action only (see com.intellij.openapi.application.Application.runReadAction())

Any idea why accessing a parent class from within a method is not allowed ? Or how to access annotations on the containing class it in som other way ?

0
Comment actions Permalink

The exception message tells you exactly what you need to do to solve the problem.


0
Comment actions Permalink

You need to call the super class constructor with "true", so the read-lock related code is activated. E.g.

  public JavaToSthMethodDefinitionSearch() {
    super(true);
  }

Regards, Arjan

0

Please sign in to leave a comment.