use of com.intellij.codeInsight.intention.AddAnnotationFix in project android by JetBrains.
the class AndroidLintAnimatorKeepInspection method getQuickFixes.
@NotNull
@Override
public AndroidLintQuickFix[] getQuickFixes(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull String message) {
return new AndroidLintQuickFix[] { new AndroidLintQuickFix() {
@Override
public void apply(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.Context context) {
if (!ObjectAnimatorDetector.isAddKeepErrorMessage(message, TextFormat.RAW)) {
return;
}
PsiModifierListOwner container = PsiTreeUtil.getParentOfType(startElement, PsiModifierListOwner.class);
if (container == null) {
return;
}
if (!FileModificationService.getInstance().preparePsiElementForWrite(container)) {
return;
}
final PsiModifierList modifierList = container.getModifierList();
if (modifierList != null) {
PsiAnnotation annotation = AnnotationUtil.findAnnotation(container, KEEP_ANNOTATION);
if (annotation == null) {
Project project = startElement.getProject();
new AddAnnotationFix(KEEP_ANNOTATION, container).invoke(project, null, container.getContainingFile());
}
}
}
@Override
public boolean isApplicable(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.ContextType contextType) {
return true;
}
@NotNull
@Override
public String getName() {
return "Annotate with @Keep";
}
} };
}
use of com.intellij.codeInsight.intention.AddAnnotationFix in project intellij-community by JetBrains.
the class OverrideImplementUtil method annotate.
public static void annotate(@NotNull PsiMethod result, String fqn, String... annosToRemove) throws IncorrectOperationException {
Project project = result.getProject();
AddAnnotationFix fix = new AddAnnotationFix(fqn, result, annosToRemove);
if (fix.isAvailable(project, null, result.getContainingFile())) {
fix.invoke(project, null, result.getContainingFile());
}
}
use of com.intellij.codeInsight.intention.AddAnnotationFix in project intellij-community by JetBrains.
the class GrPullUpHelper method doMoveMethod.
private void doMoveMethod(PsiSubstitutor substitutor, MemberInfo info) {
GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(myProject);
GrMethod method = (GrMethod) info.getMember();
PsiMethod sibling = method;
PsiMethod anchor = null;
while (sibling != null) {
sibling = PsiTreeUtil.getNextSiblingOfType(sibling, PsiMethod.class);
if (sibling != null) {
anchor = MethodSignatureUtil.findMethodInSuperClassBySignatureInDerived(method.getContainingClass(), myTargetSuperClass, sibling.getSignature(PsiSubstitutor.EMPTY), false);
if (anchor != null) {
break;
}
}
}
GrMethod methodCopy = (GrMethod) method.copy();
if (method.findSuperMethods(myTargetSuperClass).length == 0) {
deleteOverrideAnnotationIfFound(methodCopy);
}
final boolean isOriginalMethodAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT) || method.hasModifierProperty(PsiModifier.DEFAULT);
if (myTargetSuperClass.isInterface() || info.isToAbstract()) {
GroovyChangeContextUtil.clearContextInfo(method);
RefactoringUtil.makeMethodAbstract(myTargetSuperClass, methodCopy);
if (myTargetSuperClass.isInterface()) {
PsiUtil.setModifierProperty(methodCopy, PsiModifier.ABSTRACT, false);
}
replaceMovedMemberTypeParameters(methodCopy, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
final GrMethod movedElement = anchor != null ? (GrMethod) myTargetSuperClass.addBefore(methodCopy, anchor) : (GrMethod) myTargetSuperClass.add(methodCopy);
CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(method.getProject());
if (styleSettings.INSERT_OVERRIDE_ANNOTATION) {
if (PsiUtil.isLanguageLevel5OrHigher(mySourceClass) && !myTargetSuperClass.isInterface() || PsiUtil.isLanguageLevel6OrHigher(mySourceClass)) {
new AddAnnotationFix(CommonClassNames.JAVA_LANG_OVERRIDE, method).invoke(method.getProject(), null, mySourceClass.getContainingFile());
}
}
GrDocComment oldDoc = method.getDocComment();
if (oldDoc != null) {
GrDocCommentUtil.setDocComment(movedElement, oldDoc);
}
myDocCommentPolicy.processCopiedJavaDoc(methodCopy.getDocComment(), oldDoc, isOriginalMethodAbstract);
myMembersAfterMove.add(movedElement);
if (isOriginalMethodAbstract) {
deleteMemberWithDocComment(method);
}
} else {
if (isOriginalMethodAbstract) {
PsiUtil.setModifierProperty(myTargetSuperClass, PsiModifier.ABSTRACT, true);
}
fixReferencesToStatic(methodCopy);
replaceMovedMemberTypeParameters(methodCopy, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
final PsiMethod superClassMethod = myTargetSuperClass.findMethodBySignature(methodCopy, false);
Language language = myTargetSuperClass.getLanguage();
final PsiMethod movedElement;
if (superClassMethod != null && superClassMethod.hasModifierProperty(PsiModifier.ABSTRACT)) {
movedElement = (PsiMethod) superClassMethod.replace(convertMethodToLanguage(methodCopy, language));
} else {
movedElement = anchor != null ? (PsiMethod) myTargetSuperClass.addBefore(convertMethodToLanguage(methodCopy, language), anchor) : (PsiMethod) myTargetSuperClass.add(convertMethodToLanguage(methodCopy, language));
myMembersAfterMove.add(movedElement);
}
if (movedElement instanceof GrMethod) {
GrDocCommentUtil.setDocComment((GrDocCommentOwner) movedElement, method.getDocComment());
}
deleteMemberWithDocComment(method);
}
}
use of com.intellij.codeInsight.intention.AddAnnotationFix in project intellij-community by JetBrains.
the class JUnit4Framework method findOrCreateSetUpMethod.
private PsiMethod findOrCreateSetUpMethod(PsiClass clazz, String beforeClassAnnotationName, String beforeAnnotationName) {
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, beforeClassAnnotationName, false))
return existingMethod;
int exit = ApplicationManager.getApplication().isUnitTestMode() ? Messages.OK : Messages.showOkCancelDialog("Method setUp already exist but is not annotated as @Before. Annotate?", CommonBundle.getWarningTitle(), Messages.getWarningIcon());
if (exit == Messages.OK) {
new AddAnnotationFix(beforeAnnotationName, 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 android by JetBrains.
the class SuppressLintIntentionAction method addSuppressAnnotation.
// Based on the equivalent code in com.intellij.codeInsight.daemon.impl.actions.SuppressFix
// to add @SuppressWarnings annotations
private static void addSuppressAnnotation(final Project project, final PsiElement container, final PsiModifierListOwner modifierOwner, final String id) throws IncorrectOperationException {
PsiAnnotation annotation = AnnotationUtil.findAnnotation(modifierOwner, FQCN_SUPPRESS_LINT);
final PsiAnnotation newAnnotation = createNewAnnotation(project, container, annotation, id);
if (newAnnotation != null) {
if (annotation != null && annotation.isPhysical()) {
annotation.replace(newAnnotation);
} else {
final PsiNameValuePair[] attributes = newAnnotation.getParameterList().getAttributes();
//noinspection ConstantConditions
new AddAnnotationFix(FQCN_SUPPRESS_LINT, modifierOwner, attributes).invoke(project, null, /*editor*/
container.getContainingFile());
}
}
}
Aggregations