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
.daemon
.impl
.quickfix
;
18 import com
.intellij
.codeInsight
.CodeInsightUtilBase
;
19 import com
.intellij
.codeInsight
.daemon
.QuickFixBundle
;
20 import com
.intellij
.codeInsight
.intention
.IntentionAction
;
21 import com
.intellij
.openapi
.command
.undo
.UndoUtil
;
22 import com
.intellij
.openapi
.diagnostic
.Logger
;
23 import com
.intellij
.openapi
.editor
.Editor
;
24 import com
.intellij
.openapi
.project
.Project
;
25 import com
.intellij
.psi
.*;
26 import com
.intellij
.psi
.util
.PsiMatcherImpl
;
27 import com
.intellij
.psi
.util
.PsiMatchers
;
28 import com
.intellij
.util
.IncorrectOperationException
;
29 import org
.jetbrains
.annotations
.NotNull
;
31 public class InsertSuperFix
implements IntentionAction
{
32 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.InsertSuperFix");
34 private final PsiMethod myConstructor
;
36 public InsertSuperFix(PsiMethod constructor
) {
37 myConstructor
= constructor
;
41 public String
getText() {
42 return QuickFixBundle
.message("insert.super.constructor.call.text");
46 public String
getFamilyName() {
47 return QuickFixBundle
.message("insert.super.constructor.call.family");
50 public boolean isAvailable(@NotNull Project project
, Editor editor
, PsiFile file
) {
51 return myConstructor
.isValid()
52 && myConstructor
.getBody() != null
53 && myConstructor
.getBody().getLBrace() != null
54 && myConstructor
.getManager().isInProject(myConstructor
)
58 public void invoke(@NotNull Project project
, Editor editor
, PsiFile file
) {
59 if (!CodeInsightUtilBase
.prepareFileForWrite(myConstructor
.getContainingFile())) return;
61 PsiStatement superCall
=
62 JavaPsiFacade
.getInstance(myConstructor
.getProject()).getElementFactory().createStatementFromText("super();",null);
64 PsiCodeBlock body
= myConstructor
.getBody();
65 PsiJavaToken lBrace
= body
.getLBrace();
66 body
.addAfter(superCall
, lBrace
);
67 lBrace
= (PsiJavaToken
) new PsiMatcherImpl(body
)
68 .firstChild(PsiMatchers
.hasClass(PsiExpressionStatement
.class))
69 .firstChild(PsiMatchers
.hasClass(PsiMethodCallExpression
.class))
70 .firstChild(PsiMatchers
.hasClass(PsiExpressionList
.class))
71 .firstChild(PsiMatchers
.hasClass(PsiJavaToken
.class))
72 .dot(PsiMatchers
.hasText("("))
74 editor
.getCaretModel().moveToOffset(lBrace
.getTextOffset()+1);
75 UndoUtil
.markPsiFileForUndo(file
);
77 catch (IncorrectOperationException e
) {
82 public boolean startInWriteAction() {