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
.patterns
;
18 import com
.intellij
.psi
.*;
19 import com
.intellij
.util
.ProcessingContext
;
20 import org
.jetbrains
.annotations
.NonNls
;
21 import org
.jetbrains
.annotations
.NotNull
;
26 public class PsiJavaElementPattern
<T
extends PsiElement
,Self
extends PsiJavaElementPattern
<T
,Self
>> extends PsiElementPattern
<T
,Self
> {
27 private static final @NonNls String VALUE
= "value";
29 public PsiJavaElementPattern(final Class
<T
> aClass
) {
33 public PsiJavaElementPattern(@NotNull final InitialPatternCondition
<T
> condition
) {
37 public Self
annotationParam(@NonNls final String annotationQualifiedName
, @NonNls final String parameterName
) {
38 return annotationParam(StandardPatterns
.string().equalTo(annotationQualifiedName
), parameterName
);
41 public Self
annotationParam(@NonNls final String annotationQualifiedName
) {
42 return annotationParam(annotationQualifiedName
, VALUE
);
45 public Self
annotationParam(final ElementPattern
<String
> annotationQualifiedName
, @NonNls final String parameterName
) {
47 PsiJavaPatterns
.psiNameValuePair().withName(parameterName
).withParent(
48 PlatformPatterns
.psiElement(PsiAnnotationParameterList
.class).withParent(
49 PsiJavaPatterns
.psiAnnotation().qName(annotationQualifiedName
))));
52 public Self
insideAnnotationParam(final ElementPattern
<String
> annotationQualifiedName
, @NonNls final String parameterName
) {
54 PsiJavaPatterns
.psiNameValuePair().withName(parameterName
).withParent(
55 PlatformPatterns
.psiElement(PsiAnnotationParameterList
.class).withParent(
56 PsiJavaPatterns
.psiAnnotation().qName(annotationQualifiedName
))));
59 public Self
insideAnnotationParam(final ElementPattern
<String
> annotationQualifiedName
) {
60 return insideAnnotationParam(annotationQualifiedName
, VALUE
);
63 public Self
nameIdentifierOf(final Class
<?
extends PsiMember
> aClass
) {
64 return nameIdentifierOf(PsiJavaPatterns
.instanceOf(aClass
));
67 public Self
nameIdentifierOf(final ElementPattern
<?
extends PsiMember
> pattern
) {
68 return with(new PatternCondition
<T
>("nameIdentifierOf") {
69 public boolean accepts(@NotNull final T t
, final ProcessingContext context
) {
70 if (!(t
instanceof PsiIdentifier
)) return false;
72 final PsiElement parent
= t
.getParent();
73 if (parent
instanceof PsiClass
&& t
!= ((PsiClass
) parent
).getNameIdentifier()) return false;
74 if (parent
instanceof PsiMethod
&& t
!= ((PsiMethod
) parent
).getNameIdentifier()) return false;
75 if (parent
instanceof PsiVariable
&& t
!= ((PsiVariable
) parent
).getNameIdentifier()) return false;
77 return pattern
.getCondition().accepts(parent
, context
);
82 public Self
methodCallParameter(final int index
, final ElementPattern
<?
extends PsiMethod
> methodPattern
) {
83 return with(new PatternCondition
<T
>("methodCallParameter") {
84 public boolean accepts(@NotNull final T literal
, final ProcessingContext context
) {
85 final PsiElement parent
= literal
.getParent();
86 if (parent
instanceof PsiExpressionList
) {
87 final PsiExpressionList psiExpressionList
= (PsiExpressionList
)parent
;
88 final PsiExpression
[] psiExpressions
= psiExpressionList
.getExpressions();
89 if (!(psiExpressions
.length
> index
&& psiExpressions
[index
] == literal
)) return false;
91 final PsiElement element
= psiExpressionList
.getParent();
92 if (element
instanceof PsiMethodCallExpression
) {
93 final JavaResolveResult
[] results
= ((PsiMethodCallExpression
)element
).getMethodExpression().multiResolve(false);
94 for (JavaResolveResult result
: results
) {
95 final PsiElement psiElement
= result
.getElement();
96 if (methodPattern
.getCondition().accepts(psiElement
, context
)) {
107 public static class Capture
<T
extends PsiElement
> extends PsiJavaElementPattern
<T
, Capture
<T
>> {
108 public Capture(final Class
<T
> aClass
) {
112 public Capture(@NotNull final InitialPatternCondition
<T
> condition
) {