@Override public PsiAnnotation getPsiElement() { PsiAnnotation annotation = SoftReference.dereference(myParsedFromRepository); if (annotation != null) { return annotation; } final String text = getText(); try { PsiJavaParserFacade facade = JavaPsiFacade.getInstance(getProject()).getParserFacade(); annotation = facade.createAnnotationFromText(text, getPsi()); myParsedFromRepository = new SoftReference<PsiAnnotation>(annotation); return annotation; } catch (IncorrectOperationException e) { LOG.error("Bad annotation in repository!", e); return null; } }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { PsiJavaModule module = (PsiJavaModule) startElement; PsiJavaParserFacade parserFacade = JavaPsiFacade.getInstance(project).getParserFacade(); PsiJavaModule tempModule = parserFacade.createModuleFromText("module " + module.getName() + " { requires " + myRequiredName + "; }"); Iterable<PsiRequiresStatement> tempModuleRequires = tempModule.getRequires(); PsiRequiresStatement requiresStatement = tempModuleRequires.iterator().next(); PsiElement addingPlace = findAddingPlace(module); if(addingPlace != null) { addingPlace.getParent().addAfter(requiresStatement, addingPlace); } }
@Nullable private PsiJavaCodeReferenceElement createReference() { final String refText = getImportReferenceText(); if (refText == null) return null; final PsiJavaParserFacade parserFacade = JavaPsiFacade.getInstance(getProject()).getParserFacade(); try { return parserFacade.createReferenceFromText(refText, getPsi()); } catch (IncorrectOperationException e) { return null; } }