update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / intention / impl / AddOverrideAnnotationAction.java
blob13e6e6f4bababbdaaace252819023e6c0eebb316
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInsight.intention.impl;
18 import com.intellij.codeInsight.CodeInsightBundle;
19 import com.intellij.codeInsight.intention.AddAnnotationFix;
20 import com.intellij.codeInsight.intention.IntentionAction;
21 import com.intellij.openapi.editor.Editor;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.pom.java.LanguageLevel;
24 import com.intellij.psi.*;
25 import com.intellij.psi.util.PsiTreeUtil;
26 import com.intellij.psi.util.PsiUtil;
27 import com.intellij.util.IncorrectOperationException;
28 import org.jetbrains.annotations.NotNull;
30 /**
31 * @author ven
33 public class AddOverrideAnnotationAction implements IntentionAction {
34 private static final String JAVA_LANG_OVERRIDE = "java.lang.Override";
36 @NotNull
37 public String getText() {
38 return CodeInsightBundle.message("intention.add.override.annotation");
41 @NotNull
42 public String getFamilyName() {
43 return CodeInsightBundle.message("intention.add.override.annotation.family");
46 public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
47 if (!PsiUtil.isLanguageLevel5OrHigher(file)) return false;
48 PsiMethod method = findMethod(file, editor.getCaretModel().getOffset());
49 if (method == null) return false;
50 if (method.getModifierList().findAnnotation(JAVA_LANG_OVERRIDE) != null) return false;
51 PsiMethod[] superMethods = method.findSuperMethods();
52 for (PsiMethod superMethod : superMethods) {
53 if (!superMethod.hasModifierProperty(PsiModifier.ABSTRACT)
54 && new AddAnnotationFix(JAVA_LANG_OVERRIDE, method).isAvailable(project, editor, file)) {
55 return true;
59 return false;
62 public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
63 PsiMethod method = findMethod(file, editor.getCaretModel().getOffset());
64 new AddAnnotationFix(JAVA_LANG_OVERRIDE, method).invoke(project, editor, file);
67 private static PsiMethod findMethod(PsiFile file, int offset) {
68 PsiElement element = file.findElementAt(offset);
69 PsiMethod res = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
70 if (res == null) return null;
72 //Not available in method's body
73 PsiCodeBlock body = res.getBody();
74 if (body == null) return null;
75 if (body.getTextRange().getStartOffset() <= offset) return null;
77 return res;
80 public boolean startInWriteAction() {
81 return true;