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.
21 package com
.intellij
.codeInspection
.concurrencyAnnotations
;
23 import com
.intellij
.codeInsight
.TargetElementUtil
;
24 import com
.intellij
.codeInsight
.daemon
.impl
.quickfix
.OrderEntryFix
;
25 import com
.intellij
.codeInsight
.intention
.IntentionAction
;
26 import com
.intellij
.openapi
.diagnostic
.Logger
;
27 import com
.intellij
.openapi
.editor
.Editor
;
28 import com
.intellij
.openapi
.module
.ModuleUtil
;
29 import com
.intellij
.openapi
.project
.Project
;
30 import com
.intellij
.openapi
.roots
.ProjectFileIndex
;
31 import com
.intellij
.openapi
.roots
.ProjectRootManager
;
32 import com
.intellij
.openapi
.vfs
.VirtualFile
;
33 import com
.intellij
.psi
.*;
34 import com
.intellij
.psi
.util
.PsiUtil
;
35 import com
.intellij
.util
.IncorrectOperationException
;
36 import com
.intellij
.util
.PathUtil
;
37 import net
.jcip
.annotations
.GuardedBy
;
38 import org
.jetbrains
.annotations
.NonNls
;
39 import org
.jetbrains
.annotations
.NotNull
;
41 public class JCiPOrderEntryFix
implements IntentionAction
{
42 private static final Logger LOG
= Logger
.getInstance("#" + JCiPOrderEntryFix
.class.getName());
45 public String
getText() {
46 return "Add jcip-annotations.jar to classpath";
50 public String
getFamilyName() {
54 public boolean isAvailable(@NotNull final Project project
, final Editor editor
, final PsiFile file
) {
55 if (!(file
instanceof PsiJavaFile
)) return false;
57 final PsiReference reference
= TargetElementUtil
.findReference(editor
);
58 if (!(reference
instanceof PsiJavaCodeReferenceElement
)) return false;
59 if (reference
.resolve() != null) return false;
60 @NonNls final String referenceName
= ((PsiJavaCodeReferenceElement
)reference
).getReferenceName();
61 if (referenceName
== null) return false;
62 final ProjectFileIndex fileIndex
= ProjectRootManager
.getInstance(project
).getFileIndex();
63 final VirtualFile virtualFile
= file
.getVirtualFile();
64 if (virtualFile
== null) return false;
65 if (fileIndex
.getModuleForFile(virtualFile
) == null) return false;
66 if (!(((PsiJavaCodeReferenceElement
)reference
).getParent() instanceof PsiAnnotation
&&
67 PsiUtil
.isLanguageLevel5OrHigher(((PsiJavaCodeReferenceElement
)reference
)))) return false;
68 if (!JCiPUtil
.isJCiPAnnotation(referenceName
)) return false;
72 public void invoke(@NotNull final Project project
, final Editor editor
, final PsiFile file
) throws IncorrectOperationException
{
73 final PsiJavaCodeReferenceElement reference
= (PsiJavaCodeReferenceElement
)TargetElementUtil
.findReference(editor
);
74 LOG
.assertTrue(reference
!= null);
75 String jarPath
= PathUtil
.getJarPathForClass(GuardedBy
.class);
76 final VirtualFile virtualFile
= file
.getVirtualFile();
77 LOG
.assertTrue(virtualFile
!= null);
78 OrderEntryFix
.addBundledJarToRoots(project
, editor
, ModuleUtil
.findModuleForFile(virtualFile
, project
), reference
,
79 "net.jcip.annotations." + reference
.getReferenceName(), jarPath
);
82 public boolean startInWriteAction() {