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.
20 package com
.intellij
.codeInsight
.daemon
.impl
.quickfix
;
22 import com
.intellij
.codeInsight
.CodeInsightUtilBase
;
23 import com
.intellij
.codeInsight
.daemon
.QuickFixBundle
;
24 import com
.intellij
.codeInsight
.intention
.IntentionAction
;
25 import com
.intellij
.openapi
.editor
.Editor
;
26 import com
.intellij
.openapi
.project
.Project
;
27 import com
.intellij
.psi
.*;
28 import com
.intellij
.util
.IncorrectOperationException
;
29 import org
.jetbrains
.annotations
.NotNull
;
31 public class InsertNewFix
implements IntentionAction
{
32 private final PsiMethodCallExpression myMethodCall
;
33 private final PsiClass myClass
;
35 public InsertNewFix(PsiMethodCallExpression methodCall
, PsiClass aClass
) {
36 myMethodCall
= methodCall
;
41 public String
getText() {
42 return QuickFixBundle
.message("insert.new.fix");
46 public String
getFamilyName() {
50 public boolean isAvailable(@NotNull Project project
, Editor editor
, PsiFile file
) {
51 return myMethodCall
!= null
52 && myMethodCall
.isValid()
53 && myMethodCall
.getManager().isInProject(myMethodCall
);
56 public void invoke(@NotNull Project project
, Editor editor
, PsiFile file
) throws IncorrectOperationException
{
57 if (!CodeInsightUtilBase
.prepareFileForWrite(myMethodCall
.getContainingFile())) return;
58 PsiElementFactory factory
= JavaPsiFacade
.getInstance(myMethodCall
.getProject()).getElementFactory();
59 PsiNewExpression newExpression
= (PsiNewExpression
)factory
.createExpressionFromText("new X()",null);
61 PsiJavaCodeReferenceElement classReference
= newExpression
.getClassReference();
62 assert classReference
!= null;
63 classReference
.replace(factory
.createClassReferenceElement(myClass
));
64 PsiExpressionList argumentList
= newExpression
.getArgumentList();
65 assert argumentList
!= null;
66 argumentList
.replace(myMethodCall
.getArgumentList());
67 myMethodCall
.replace(newExpression
);
70 public boolean startInWriteAction() {