use of com.intellij.codeInsight.intention.AddAnnotationFix in project intellij-community by JetBrains.
the class TestNGFramework method findOrCreateSetUpMethod.
@Override
protected PsiMethod findOrCreateSetUpMethod(PsiClass clazz) throws IncorrectOperationException {
PsiMethod method = findSetUpMethod(clazz);
if (method != null)
return method;
final PsiManager manager = clazz.getManager();
final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
String setUpName = "setUp";
PsiMethod patternMethod = createSetUpPatternMethod(factory);
PsiMethod inClass = clazz.findMethodBySignature(patternMethod, false);
if (inClass != null) {
if (AnnotationUtil.isAnnotated(inClass, SECONDARY_BEFORE_ANNOTATIONS, false)) {
return inClass;
}
int exit = ApplicationManager.getApplication().isUnitTestMode() ? Messages.YES : Messages.showYesNoDialog(manager.getProject(), "Method \'" + setUpName + "\' already exist but is not annotated as @BeforeMethod.", CommonBundle.getWarningTitle(), "Annotate", "Create new method", Messages.getWarningIcon());
if (exit == Messages.YES) {
new AddAnnotationFix(BeforeMethod.class.getName(), inClass).invoke(inClass.getProject(), null, inClass.getContainingFile());
return inClass;
} else if (exit == Messages.NO) {
inClass = null;
int i = 0;
while (clazz.findMethodBySignature(patternMethod, false) != null) {
patternMethod.setName(setUpName + (++i));
}
setUpName = patternMethod.getName();
}
}
final PsiClass superClass = clazz.getSuperClass();
if (superClass != null) {
final PsiMethod[] methods = superClass.findMethodsBySignature(patternMethod, false);
if (methods.length > 0) {
final PsiModifierList modifierList = methods[0].getModifierList();
if (!modifierList.hasModifierProperty(PsiModifier.PRIVATE)) {
//do not override private method
@NonNls String pattern = "@" + BeforeMethod.class.getName() + "\n";
if (modifierList.hasModifierProperty(PsiModifier.PROTECTED)) {
pattern += "protected ";
} else if (modifierList.hasModifierProperty(PsiModifier.PUBLIC)) {
pattern += "public ";
}
patternMethod = factory.createMethodFromText(pattern + "void " + setUpName + "() throws Exception {\nsuper." + setUpName + "();\n}", null);
}
}
}
final PsiMethod[] psiMethods = clazz.getMethods();
PsiMethod testMethod = null;
for (PsiMethod psiMethod : psiMethods) {
if (inClass == null && AnnotationUtil.isAnnotated(psiMethod, BeforeMethod.class.getName(), false)) {
inClass = psiMethod;
}
if (testMethod == null && AnnotationUtil.isAnnotated(psiMethod, Test.class.getName(), false) && !psiMethod.hasModifierProperty(PsiModifier.PRIVATE)) {
testMethod = psiMethod;
}
}
if (inClass == null) {
final PsiMethod psiMethod;
if (testMethod != null) {
psiMethod = (PsiMethod) clazz.addBefore(patternMethod, testMethod);
} else {
psiMethod = (PsiMethod) clazz.add(patternMethod);
}
JavaCodeStyleManager.getInstance(clazz.getProject()).shortenClassReferences(clazz);
return psiMethod;
} else if (inClass.getBody() == null) {
return (PsiMethod) inClass.replace(patternMethod);
}
return inClass;
}
use of com.intellij.codeInsight.intention.AddAnnotationFix in project intellij-community by JetBrains.
the class JUnit5Framework method findOrCreateSetUpMethod.
@Override
@Nullable
protected PsiMethod findOrCreateSetUpMethod(PsiClass clazz) throws IncorrectOperationException {
PsiMethod method = findSetUpMethod(clazz);
if (method != null)
return method;
PsiManager manager = clazz.getManager();
PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
method = createSetUpPatternMethod(factory);
PsiMethod existingMethod = clazz.findMethodBySignature(method, false);
if (existingMethod != null) {
if (AnnotationUtil.isAnnotated(existingMethod, JUnitUtil.BEFORE_ALL_ANNOTATION_NAME, false))
return existingMethod;
int exit = ApplicationManager.getApplication().isUnitTestMode() ? Messages.OK : Messages.showOkCancelDialog("Method setUp already exist but is not annotated as @BeforeEach. Annotate?", CommonBundle.getWarningTitle(), Messages.getWarningIcon());
if (exit == Messages.OK) {
new AddAnnotationFix(JUnitUtil.BEFORE_EACH_ANNOTATION_NAME, existingMethod).invoke(existingMethod.getProject(), null, existingMethod.getContainingFile());
return existingMethod;
}
}
final PsiMethod testMethod = JUnitUtil.findFirstTestMethod(clazz);
if (testMethod != null) {
method = (PsiMethod) clazz.addBefore(method, testMethod);
} else {
method = (PsiMethod) clazz.add(method);
}
JavaCodeStyleManager.getInstance(manager.getProject()).shortenClassReferences(method);
return method;
}
use of com.intellij.codeInsight.intention.AddAnnotationFix in project intellij-community by JetBrains.
the class AnnotateCapitalizationIntention method invoke.
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
final PsiModifierListOwner modifierListOwner = getElement(editor, file);
if (modifierListOwner == null)
throw new IncorrectOperationException();
BaseListPopupStep<Nls.Capitalization> step = new BaseListPopupStep<Nls.Capitalization>(null, Nls.Capitalization.Title, Nls.Capitalization.Sentence) {
@Override
public PopupStep onChosen(final Nls.Capitalization selectedValue, boolean finalChoice) {
new WriteCommandAction.Simple(project) {
@Override
protected void run() throws Throwable {
String nls = Nls.class.getName();
PsiAnnotation annotation = JavaPsiFacade.getInstance(project).getElementFactory().createAnnotationFromText("@" + nls + "(capitalization = " + nls + ".Capitalization." + selectedValue.toString() + ")", modifierListOwner);
new AddAnnotationFix(Nls.class.getName(), modifierListOwner, annotation.getParameterList().getAttributes()).applyFix();
}
}.execute();
return FINAL_CHOICE;
}
};
JBPopupFactory.getInstance().createListPopup(step).showInBestPositionFor(editor);
}
use of com.intellij.codeInsight.intention.AddAnnotationFix in project intellij-community by JetBrains.
the class JavaFxAnnotator method annotate.
@Override
public void annotate(@NotNull final PsiElement element, @NotNull AnnotationHolder holder) {
final PsiFile containingFile = element.getContainingFile();
if (!JavaFxFileTypeFactory.isFxml(containingFile))
return;
if (element instanceof XmlAttributeValue) {
final String value = ((XmlAttributeValue) element).getValue();
if (!JavaFxPsiUtil.isExpressionBinding(value) && !JavaFxPsiUtil.isIncorrectExpressionBinding(value)) {
final PsiReference[] references = element.getReferences();
for (PsiReference reference : references) {
if (reference instanceof JavaFxColorReference) {
attachColorIcon(element, holder, StringUtil.unquoteString(element.getText()));
continue;
}
final PsiElement resolve = reference.resolve();
if (resolve instanceof PsiMember) {
if (!JavaFxPsiUtil.isVisibleInFxml((PsiMember) resolve)) {
final String symbolPresentation = "'" + SymbolPresentationUtil.getSymbolPresentableText(resolve) + "'";
final Annotation annotation = holder.createErrorAnnotation(element, symbolPresentation + (resolve instanceof PsiClass ? " should be public" : " should be public or annotated with @FXML"));
if (!(resolve instanceof PsiClass)) {
annotation.registerUniversalFix(new AddAnnotationFix(JavaFxCommonNames.JAVAFX_FXML_ANNOTATION, (PsiMember) resolve, ArrayUtil.EMPTY_STRING_ARRAY), null, null);
}
}
}
}
}
} else if (element instanceof XmlAttribute) {
final XmlAttribute attribute = (XmlAttribute) element;
final String attributeName = attribute.getName();
if (!FxmlConstants.FX_BUILT_IN_ATTRIBUTES.contains(attributeName) && !attribute.isNamespaceDeclaration() && JavaFxPsiUtil.isReadOnly(attributeName, attribute.getParent())) {
holder.createErrorAnnotation(element.getNavigationElement(), "Property '" + attributeName + "' is read-only");
}
if (FxmlConstants.SOURCE.equals(attributeName)) {
final XmlAttributeValue valueElement = attribute.getValueElement();
if (valueElement != null) {
final XmlTag xmlTag = attribute.getParent();
if (xmlTag != null) {
final XmlTag referencedTag = JavaFxBuiltInTagDescriptor.getReferencedTag(xmlTag);
if (referencedTag != null) {
if (referencedTag.getTextOffset() > xmlTag.getTextOffset()) {
holder.createErrorAnnotation(valueElement.getValueTextRange(), valueElement.getValue() + " not found");
} else if (xmlTag.getParentTag() == referencedTag.getParentTag()) {
final Annotation annotation = holder.createErrorAnnotation(valueElement.getValueTextRange(), "Duplicate child added");
annotation.registerFix(new JavaFxWrapWithDefineIntention(referencedTag, valueElement.getValue()));
}
}
}
}
}
} else if (element instanceof XmlTag) {
if (FxmlConstants.FX_SCRIPT.equals(((XmlTag) element).getName())) {
final XmlTagValue tagValue = ((XmlTag) element).getValue();
if (!StringUtil.isEmptyOrSpaces(tagValue.getText())) {
final List<String> langs = JavaFxPsiUtil.parseInjectedLanguages((XmlFile) element.getContainingFile());
if (langs.isEmpty()) {
final ASTNode openTag = element.getNode().findChildByType(XmlTokenType.XML_NAME);
final Annotation annotation = holder.createErrorAnnotation(openTag != null ? openTag.getPsi() : element, "Page language not specified.");
annotation.registerFix(new JavaFxInjectPageLanguageIntention());
}
}
}
}
}
use of com.intellij.codeInsight.intention.AddAnnotationFix in project intellij-community by JetBrains.
the class AddAnnotationIntention method invoke.
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PsiModifierListOwner owner = AddAnnotationPsiFix.getContainer(file, editor.getCaretModel().getOffset());
if (owner == null || !owner.isValid())
return;
Pair<String, String[]> annotations = getAnnotations(project);
String toAdd = annotations.first;
String[] toRemove = annotations.second;
AddAnnotationFix fix = new AddAnnotationFix(toAdd, owner, toRemove);
fix.invoke(project, editor, file);
}
Aggregations