package com.googlecode.netburstjbehaveidea;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.searches.AnnotatedElementsSearch;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.Query;
import java.util.Collection;
/**
* @author artem.stasuk@gmail.com
*/
class StepImplementationSearcher {
public static IdeaAnnotation find(
final Project project, final String line, final JBehaveStepType stepType) {
if (stepType == null) return null;
final String annotationName = stepType.qualifiedAnnotationName();
final GlobalSearchScope globalSearchScope = GlobalSearchScope.allScope(project);
final PsiClass psiAnnotationClass = JavaPsiFacade.getInstance(project).findClass(
annotationName, globalSearchScope);
if (psiAnnotationClass == null) return null;
final Query<PsiMethod> psiMethodQuery = AnnotatedElementsSearch.searchPsiMethods(
psiAnnotationClass, globalSearchScope);
final Collection<PsiMethod> annotatedPsiMethods = psiMethodQuery.findAll();
for (final PsiMethod annotatedPsiMethod : annotatedPsiMethods) {
final PsiAnnotation psiAnnotation = annotatedPsiMethod.getModifierList().findAnnotation(annotationName);
IdeaAnnotation ideaAnnotation = new IdeaAnnotation(psiAnnotation, annotatedPsiMethod);
if (ideaAnnotation.matches(line.toLowerCase())) {
return ideaAnnotation;
}
}
return null;
}
public static JBehaveStepType detectType(final PsiElement psiElement) {
final IElementType elementType = psiElement.getNode().getElementType();
if (elementType instanceof StoryElementType) {
final StoryElementType storyElementType = (StoryElementType) elementType;
return storyElementType.getStepType();
} else {
return null;
}
}
}