gosu-lang/Gosu-IntelliJ-Plugin — Issue #1: Gosu plugin does not return annotation parameters
Author: chrisgow
Created: 2012-02-25 05:35:51+00:00
State: open

Hi:

I'm trying to get an existing IJ plugin to work with the Gosu IJ plugin (2.0.3) and am running into a problem. The plugin I'm trying to get to work is the IntellijBehave plugin and it appears that when traversing annotations (at least at the function level) the parameterList() object of the annotation from a Gosu class does not contain any values. Below is a simple IJ action that will traverse all files in your project and print to the console parameters for an annotation. If you create a sample project and add an annotation that takes a parameter to a Gosu file you will see that the parametter is not displayed:

public class ReadAnnotationsFromGosu extends AnAction {
public ReadAnnotationsFromGosu() {
super("Dump Annotations from Gosu");
}

@Override
public void actionPerformed(AnActionEvent anActionEvent) {
Project project = anActionEvent.getData(PlatformDataKeys.PROJECT);
traverseProject(project, project.getBaseDir().getChildren());
}

private void traverseProject(Project project, VirtualFile[] files) {
for (VirtualFile file : files) {
if (file.isDirectory()) {
traverseProject(project, file.getChildren());
} else {
scanForAnnotations(project, file);
}
}
}

private void scanForAnnotations(Project project, VirtualFile file) {
PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
if (!(psiFile instanceof PsiClassOwner)) return;
PsiClass[] psiClasses = ((PsiClassOwner)psiFile).getClasses();

for (PsiClass psiClass : psiClasses) {
PsiMethod[] methods = psiClass.getMethods();

for (PsiMethod method : methods) {
processMethodAnnotations(method.getModifierList().getApplicableAnnotations());
}
}
}

private void processMethodAnnotations(PsiAnnotation[] annotations) {
for (PsiAnnotation annotation : annotations) {

PsiAnnotationParameterList parameterList = annotation.getParameterList();
PsiNameValuePair[] attrs = parameterList.getAttributes();

if (attrs.length > 0) {
System.out.println(annotation.getQualifiedName() + ":" + attrs[0].getValue().getText());
} else {
System.out.println("No attributes for annotation: " + annotation.getQualifiedName());
}
}
}
}