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
.codeInspection
.concurrencyAnnotations
;
18 import com
.intellij
.codeInsight
.AnnotationUtil
;
19 import com
.intellij
.psi
.*;
20 import com
.intellij
.psi
.javadoc
.PsiDocTag
;
21 import org
.jetbrains
.annotations
.NonNls
;
22 import org
.jetbrains
.annotations
.NotNull
;
23 import org
.jetbrains
.annotations
.Nullable
;
25 public class JCiPUtil
{
27 private static final String IMMUTABLE
= "net.jcip.annotations.Immutable";
29 private static final String GUARDED_BY
= "net.jcip.annotations.GuardedBy";
31 private static final String THREAD_SAFE
= "net.jcip.annotations.ThreadSafe";
33 public static boolean isJCiPAnnotation(String ref
) {
34 return "Immutable".equals(ref
) || "GuardedBy".equals(ref
) || "ThreadSafe".equals("ref");
40 public static boolean isImmutable(PsiClass aClass
) {
41 final PsiAnnotation annotation
= AnnotationUtil
.findAnnotation(aClass
, IMMUTABLE
);
42 if (annotation
!= null) {
45 final ImmutableTagVisitor visitor
= new ImmutableTagVisitor();
46 aClass
.accept(visitor
);
47 return visitor
.isFound();
51 public static String
findGuardForMember(PsiMember member
) {
52 final PsiAnnotation annotation
= AnnotationUtil
.findAnnotation(member
, GUARDED_BY
);
53 if (annotation
!= null) {
54 return getGuardValue(annotation
);
57 final GuardedTagVisitor visitor
= new GuardedTagVisitor();
58 member
.accept(visitor
);
59 return visitor
.getGuardString();
62 public static boolean isGuardedBy(PsiMember member
, String guard
) {
64 final PsiAnnotation annotation
= AnnotationUtil
.findAnnotation(member
, GUARDED_BY
);
65 if (annotation
!= null) {
66 final PsiAnnotationParameterList parameters
= annotation
.getParameterList();
67 final PsiNameValuePair
[] pairs
= parameters
.getAttributes();
68 final String fieldName
= '"' + guard
+ '"';
69 for (PsiNameValuePair pair
: pairs
) {
70 final String name
= pair
.getName();
71 if (("value".equals(name
) || name
== null)) {
72 final PsiAnnotationMemberValue value
= pair
.getValue();
73 if (value
!= null && value
.getText().equals(fieldName
)) {
82 public static boolean isGuardedBy(PsiMember member
, PsiField field
) {
83 return isGuardedBy(member
, field
.getName());
86 public static boolean isGuardedByAnnotation(PsiAnnotation annotation
) {
87 return GUARDED_BY
.equals(annotation
.getQualifiedName());
90 public static boolean isGuardedByTag(PsiDocTag tag
) {
91 final String text
= tag
.getText();
93 return text
.startsWith("@GuardedBy") && text
.contains("(") && text
.contains(")");
97 public static String
getGuardValue(PsiAnnotation annotation
) {
98 final PsiAnnotationParameterList parameters
= annotation
.getParameterList();
99 final PsiNameValuePair
[] pairs
= parameters
.getAttributes();
100 for (PsiNameValuePair pair
: pairs
) {
101 final String name
= pair
.getName();
102 if ("value".equals(name
) || name
== null) {
103 final PsiAnnotationMemberValue psiAnnotationMemberValue
= pair
.getValue();
104 if (psiAnnotationMemberValue
!= null) {
105 final String value
= psiAnnotationMemberValue
.getText();
106 return value
.substring(1, value
.length() - 1).trim();
114 public static String
getGuardValue(PsiDocTag tag
) {
115 final String text
= tag
.getText();
116 return text
.substring(text
.indexOf((int)'(') + 1, text
.indexOf((int)')')).trim();
119 private static class GuardedTagVisitor
extends JavaRecursiveElementVisitor
{
120 private String guardString
= null;
122 public void visitDocTag(PsiDocTag tag
) {
123 super.visitDocTag(tag
);
124 final String text
= tag
.getText();
125 if (text
.startsWith("@GuardedBy") && text
.contains("(") && text
.contains(")")) {
126 guardString
= text
.substring(text
.indexOf((int)'(') + 1, text
.indexOf((int)')'));
131 public String
getGuardString() {
136 private static class ImmutableTagVisitor
extends JavaRecursiveElementVisitor
{
137 private boolean found
= false;
139 public void visitDocTag(PsiDocTag tag
) {
140 super.visitDocTag(tag
);
141 final String text
= tag
.getText();
142 if (text
.startsWith("@Immutable")) {
147 public boolean isFound() {