mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
moved to java-psi
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.psi.PsiAnnotation;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class PsiAnnotationPattern extends PsiElementPattern<PsiAnnotation, PsiAnnotationPattern> {
|
||||
protected PsiAnnotationPattern() {
|
||||
super(PsiAnnotation.class);
|
||||
}
|
||||
|
||||
public PsiAnnotationPattern qName(final ElementPattern<String> pattern) {
|
||||
return with(new PatternCondition<PsiAnnotation>("qName") {
|
||||
public boolean accepts(@NotNull final PsiAnnotation psiAnnotation, final ProcessingContext context) {
|
||||
return pattern.getCondition().accepts(psiAnnotation.getQualifiedName(), context);
|
||||
}
|
||||
});
|
||||
}
|
||||
public PsiAnnotationPattern qName(@NonNls String qname) {
|
||||
return qName(StandardPatterns.string().equalTo(qname));
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.psi.PsiBinaryExpression;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class PsiBinaryExpressionPattern extends PsiExpressionPattern<PsiBinaryExpression, PsiBinaryExpressionPattern>{
|
||||
protected PsiBinaryExpressionPattern() {
|
||||
super(PsiBinaryExpression.class);
|
||||
}
|
||||
|
||||
public PsiBinaryExpressionPattern left(@NotNull final ElementPattern pattern) {
|
||||
return with(new PatternCondition<PsiBinaryExpression>("left") {
|
||||
public boolean accepts(@NotNull final PsiBinaryExpression psiBinaryExpression, final ProcessingContext context) {
|
||||
return pattern.getCondition().accepts(psiBinaryExpression.getLOperand(), context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public PsiBinaryExpressionPattern right(@NotNull final ElementPattern pattern) {
|
||||
return with(new PatternCondition<PsiBinaryExpression>("right") {
|
||||
public boolean accepts(@NotNull final PsiBinaryExpression psiBinaryExpression, final ProcessingContext context) {
|
||||
return pattern.getCondition().accepts(psiBinaryExpression.getROperand(), context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public PsiBinaryExpressionPattern operation(final ElementPattern pattern) {
|
||||
return with(new PatternCondition<PsiBinaryExpression>("operation") {
|
||||
public boolean accepts(@NotNull final PsiBinaryExpression psiBinaryExpression, final ProcessingContext context) {
|
||||
return pattern.getCondition().accepts(psiBinaryExpression.getOperationSign(), context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class PsiClassPattern extends PsiMemberPattern<PsiClass, PsiClassPattern>{
|
||||
protected PsiClassPattern() {
|
||||
super(PsiClass.class);
|
||||
}
|
||||
|
||||
public PsiClassPattern inheritorOf(final boolean strict, final PsiClassPattern pattern) {
|
||||
return with(new PatternCondition<PsiClass>("inheritorOf") {
|
||||
public boolean accepts(@NotNull PsiClass psiClass, final ProcessingContext context) {
|
||||
return isInheritor(psiClass, pattern, context, !strict);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static boolean isInheritor(PsiClass psiClass, ElementPattern pattern, final ProcessingContext matchingContext, boolean checkThisClass) {
|
||||
if (psiClass == null) return false;
|
||||
if (checkThisClass && pattern.getCondition().accepts(psiClass, matchingContext)) return true;
|
||||
if (isInheritor(psiClass.getSuperClass(), pattern, matchingContext, true)) return true;
|
||||
for (final PsiClass aClass : psiClass.getInterfaces()) {
|
||||
if (isInheritor(aClass, pattern, matchingContext, true)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public PsiClassPattern inheritorOf(final boolean strict, final String className) {
|
||||
return with(new PatternCondition<PsiClass>("inheritorOf") {
|
||||
public boolean accepts(@NotNull PsiClass psiClass, final ProcessingContext context) {
|
||||
return InheritanceUtil.isInheritor(psiClass, strict, className);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public PsiClassPattern isInterface() {
|
||||
return with(new PatternCondition<PsiClass>("isInterface") {
|
||||
public boolean accepts(@NotNull final PsiClass psiClass, final ProcessingContext context) {
|
||||
return psiClass.isInterface();
|
||||
}
|
||||
});}
|
||||
public PsiClassPattern isAnnotationType() {
|
||||
return with(new PatternCondition<PsiClass>("isAnnotationType") {
|
||||
public boolean accepts(@NotNull final PsiClass psiClass, final ProcessingContext context) {
|
||||
return psiClass.isAnnotationType();
|
||||
}
|
||||
});}
|
||||
|
||||
public PsiClassPattern withMethod(final boolean checkDeep, final ElementPattern<? extends PsiMethod> memberPattern) {
|
||||
return with(new PatternCondition<PsiClass>("withMethod") {
|
||||
public boolean accepts(@NotNull final PsiClass psiClass, final ProcessingContext context) {
|
||||
for (PsiMethod method : (checkDeep ? psiClass.getAllMethods() : psiClass.getMethods())) {
|
||||
if (memberPattern.accepts(method, context)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
public PsiClassPattern withField(final boolean checkDeep, final ElementPattern<? extends PsiField> memberPattern) {
|
||||
return with(new PatternCondition<PsiClass>("withField") {
|
||||
public boolean accepts(@NotNull final PsiClass psiClass, final ProcessingContext context) {
|
||||
for (PsiField field : (checkDeep ? psiClass.getAllFields() : psiClass.getFields())) {
|
||||
if (memberPattern.accepts(field, context)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public PsiClassPattern nonAnnotationType() {
|
||||
return with(new PatternCondition<PsiClass>("nonAnnotationType") {
|
||||
public boolean accepts(@NotNull final PsiClass psiClass, final ProcessingContext context) {
|
||||
return !psiClass.isAnnotationType();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public PsiClassPattern withQualifiedName(@NonNls @NotNull final String qname) {
|
||||
return with(new PatternCondition<PsiClass>("withQualifiedName") {
|
||||
public boolean accepts(@NotNull final PsiClass psiClass, final ProcessingContext context) {
|
||||
return qname.equals(psiClass.getQualifiedName());
|
||||
}
|
||||
});
|
||||
}
|
||||
public PsiClassPattern withQualifiedName(@NonNls @NotNull final ElementPattern<String> qname) {
|
||||
return with(new PatternCondition<PsiClass>("withQualifiedName") {
|
||||
public boolean accepts(@NotNull final PsiClass psiClass, final ProcessingContext context) {
|
||||
return qname.accepts(psiClass.getQualifiedName(), context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class PsiExpressionPattern<T extends PsiExpression, Self extends PsiExpressionPattern<T,Self>> extends PsiJavaElementPattern<T,Self> {
|
||||
protected PsiExpressionPattern(final Class<T> aClass) {
|
||||
super(aClass);
|
||||
}
|
||||
|
||||
public Self ofType(@NotNull final ElementPattern pattern) {
|
||||
return with(new PatternCondition<T>("ofType") {
|
||||
public boolean accepts(@NotNull final T t, final ProcessingContext context) {
|
||||
return pattern.getCondition().accepts(t.getType(), context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public PsiMethodCallPattern methodCall(final ElementPattern<? extends PsiMethod> method) {
|
||||
return new PsiMethodCallPattern().and(this).with(new PatternCondition<PsiMethodCallExpression>("methodCall") {
|
||||
public boolean accepts(@NotNull PsiMethodCallExpression callExpression, ProcessingContext context) {
|
||||
final JavaResolveResult[] results = callExpression.getMethodExpression().multiResolve(true);
|
||||
for (JavaResolveResult result : results) {
|
||||
if (method.getCondition().accepts(result.getElement(), context)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Self skipParentheses(final ElementPattern<? extends PsiExpression> expressionPattern) {
|
||||
return with(new PatternCondition<T>("skipParentheses") {
|
||||
@Override
|
||||
public boolean accepts(@NotNull T t, ProcessingContext context) {
|
||||
PsiExpression expression = t;
|
||||
while (expression instanceof PsiParenthesizedExpression) {
|
||||
expression = ((PsiParenthesizedExpression)expression).getExpression();
|
||||
}
|
||||
return expressionPattern.accepts(expression, context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static class Capture<T extends PsiExpression> extends PsiExpressionPattern<T, Capture<T>> {
|
||||
public Capture(final Class<T> aClass) {
|
||||
super(aClass);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class PsiFieldPattern extends PsiMemberPattern<PsiField, PsiFieldPattern>{
|
||||
public PsiFieldPattern() {
|
||||
super(new InitialPatternCondition<PsiField>(PsiField.class) {
|
||||
public boolean accepts(@Nullable final Object o, final ProcessingContext context) {
|
||||
return o instanceof PsiField;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class PsiJavaElementPattern<T extends PsiElement,Self extends PsiJavaElementPattern<T,Self>> extends PsiElementPattern<T,Self> {
|
||||
@NonNls private static final String VALUE = "value";
|
||||
|
||||
public PsiJavaElementPattern(final Class<T> aClass) {
|
||||
super(aClass);
|
||||
}
|
||||
|
||||
public PsiJavaElementPattern(@NotNull final InitialPatternCondition<T> condition) {
|
||||
super(condition);
|
||||
}
|
||||
|
||||
public Self annotationParam(@NonNls final String annotationQualifiedName, @NonNls final String parameterName) {
|
||||
return annotationParam(StandardPatterns.string().equalTo(annotationQualifiedName), parameterName);
|
||||
}
|
||||
|
||||
public Self annotationParam(@NonNls final String annotationQualifiedName) {
|
||||
return annotationParam(annotationQualifiedName, VALUE);
|
||||
}
|
||||
|
||||
public Self annotationParam(final ElementPattern<String> annotationQualifiedName, @NonNls final String parameterName) {
|
||||
return withParent(
|
||||
PsiJavaPatterns.psiNameValuePair().withName(parameterName).withParent(
|
||||
PlatformPatterns.psiElement(PsiAnnotationParameterList.class).withParent(
|
||||
PsiJavaPatterns.psiAnnotation().qName(annotationQualifiedName))));
|
||||
}
|
||||
|
||||
public Self insideAnnotationParam(final ElementPattern<String> annotationQualifiedName, @NonNls final String parameterName) {
|
||||
return inside(true,
|
||||
PsiJavaPatterns.psiNameValuePair().withName(parameterName).withParent(
|
||||
PlatformPatterns.psiElement(PsiAnnotationParameterList.class).withParent(
|
||||
PsiJavaPatterns.psiAnnotation().qName(annotationQualifiedName))));
|
||||
}
|
||||
|
||||
public Self insideAnnotationParam(final ElementPattern<String> annotationQualifiedName) {
|
||||
return insideAnnotationParam(annotationQualifiedName, VALUE);
|
||||
}
|
||||
|
||||
public Self nameIdentifierOf(final Class<? extends PsiMember> aClass) {
|
||||
return nameIdentifierOf(StandardPatterns.instanceOf(aClass));
|
||||
}
|
||||
|
||||
public Self nameIdentifierOf(final ElementPattern<? extends PsiMember> pattern) {
|
||||
return with(new PatternCondition<T>("nameIdentifierOf") {
|
||||
@Override
|
||||
public boolean accepts(@NotNull final T t, final ProcessingContext context) {
|
||||
if (!(t instanceof PsiIdentifier)) return false;
|
||||
|
||||
final PsiElement parent = t.getParent();
|
||||
if (parent instanceof PsiClass && t != ((PsiClass) parent).getNameIdentifier()) return false;
|
||||
if (parent instanceof PsiMethod && t != ((PsiMethod) parent).getNameIdentifier()) return false;
|
||||
if (parent instanceof PsiVariable && t != ((PsiVariable) parent).getNameIdentifier()) return false;
|
||||
|
||||
return pattern.getCondition().accepts(parent, context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Self methodCallParameter(final int index, final ElementPattern<? extends PsiMethod> methodPattern) {
|
||||
return with(new PatternCondition<T>("methodCallParameter") {
|
||||
@Override
|
||||
public boolean accepts(@NotNull final T literal, final ProcessingContext context) {
|
||||
final PsiElement parent = literal.getParent();
|
||||
if (parent instanceof PsiExpressionList) {
|
||||
final PsiExpressionList psiExpressionList = (PsiExpressionList)parent;
|
||||
final PsiExpression[] psiExpressions = psiExpressionList.getExpressions();
|
||||
if (!(psiExpressions.length > index && psiExpressions[index] == literal)) return false;
|
||||
|
||||
final PsiElement element = psiExpressionList.getParent();
|
||||
if (element instanceof PsiMethodCallExpression) {
|
||||
final JavaResolveResult[] results = ((PsiMethodCallExpression)element).getMethodExpression().multiResolve(false);
|
||||
for (JavaResolveResult result : results) {
|
||||
final PsiElement psiElement = result.getElement();
|
||||
if (methodPattern.getCondition().accepts(psiElement, context)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Self constructorParameter(final int index, final String... fqns) {
|
||||
return with(new PatternCondition<T>("methodCallParameter") {
|
||||
@Override
|
||||
public boolean accepts(@NotNull final T literal, final ProcessingContext context) {
|
||||
final PsiElement parent = literal.getParent();
|
||||
if (parent instanceof PsiExpressionList) {
|
||||
final PsiExpressionList psiExpressionList = (PsiExpressionList)parent;
|
||||
final PsiExpression[] psiExpressions = psiExpressionList.getExpressions();
|
||||
if (!(psiExpressions.length > index && psiExpressions[index] == literal)) return false;
|
||||
|
||||
final PsiElement element = psiExpressionList.getParent();
|
||||
if (element instanceof PsiNewExpression) {
|
||||
PsiJavaCodeReferenceElement reference = ((PsiNewExpression)element).getClassOrAnonymousClassReference();
|
||||
if (reference != null) {
|
||||
String qualifiedName = reference.getQualifiedName();
|
||||
for (String fqn : fqns) {
|
||||
if( fqn.equals(qualifiedName)) return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static class Capture<T extends PsiElement> extends PsiJavaElementPattern<T, Capture<T>> {
|
||||
public Capture(final Class<T> aClass) {
|
||||
super(aClass);
|
||||
}
|
||||
|
||||
public Capture(@NotNull final InitialPatternCondition<T> condition) {
|
||||
super(condition);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,181 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class PsiJavaPatterns extends StandardPatterns{
|
||||
|
||||
public static IElementTypePattern elementType() {
|
||||
return PlatformPatterns.elementType();
|
||||
}
|
||||
|
||||
public static VirtualFilePattern virtualFile() {
|
||||
return PlatformPatterns.virtualFile();
|
||||
}
|
||||
|
||||
public static PsiJavaElementPattern.Capture<PsiElement> psiElement() {
|
||||
return new PsiJavaElementPattern.Capture<PsiElement>(PsiElement.class);
|
||||
}
|
||||
|
||||
public static PsiJavaElementPattern.Capture<PsiElement> psiElement(IElementType type) {
|
||||
return psiElement().withElementType(type);
|
||||
}
|
||||
|
||||
public static <T extends PsiElement> PsiJavaElementPattern.Capture<T> psiElement(final Class<T> aClass) {
|
||||
return new PsiJavaElementPattern.Capture<T>(aClass);
|
||||
}
|
||||
|
||||
public static PsiJavaElementPattern.Capture<PsiElement> psiElement(final Class<? extends PsiElement>... classAlternatives) {
|
||||
return new PsiJavaElementPattern.Capture<PsiElement>(new InitialPatternCondition<PsiElement>(PsiElement.class) {
|
||||
@Override
|
||||
public boolean accepts(@Nullable Object o, ProcessingContext context) {
|
||||
for (Class<? extends PsiElement> classAlternative : classAlternatives) {
|
||||
if (classAlternative.isInstance(o)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static PsiJavaElementPattern.Capture<PsiLiteralExpression> literalExpression() {
|
||||
return literalExpression(null);
|
||||
}
|
||||
|
||||
public static PsiJavaElementPattern.Capture<PsiLiteral> psiLiteral() {
|
||||
return psiLiteral(null);
|
||||
}
|
||||
|
||||
public static PsiJavaElementPattern.Capture<PsiLiteral> psiLiteral(@Nullable final ElementPattern value) {
|
||||
return new PsiJavaElementPattern.Capture<PsiLiteral>(new InitialPatternConditionPlus<PsiLiteral>(PsiLiteral.class) {
|
||||
public boolean accepts(@Nullable final Object o, final ProcessingContext context) {
|
||||
return o instanceof PsiLiteral && (value == null || value.accepts(((PsiLiteral)o).getValue(), context));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ElementPattern<?>> getPatterns() {
|
||||
return Collections.<ElementPattern<?>>singletonList(value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static PsiJavaElementPattern.Capture<PsiNewExpression> psiNewExpression(@NotNull final String... fqns) {
|
||||
return new PsiJavaElementPattern.Capture<PsiNewExpression>(new InitialPatternCondition<PsiNewExpression>(PsiNewExpression.class) {
|
||||
public boolean accepts(@Nullable final Object o, final ProcessingContext context) {
|
||||
if(o instanceof PsiNewExpression) {
|
||||
PsiJavaCodeReferenceElement reference = ((PsiNewExpression)o).getClassOrAnonymousClassReference();
|
||||
if (reference != null) {
|
||||
for (String fqn : fqns) {
|
||||
if( fqn.equals(reference.getQualifiedName())) return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static PsiJavaElementPattern.Capture<PsiLiteralExpression> literalExpression(@Nullable final ElementPattern value) {
|
||||
return new PsiJavaElementPattern.Capture<PsiLiteralExpression>(new InitialPatternConditionPlus<PsiLiteralExpression>(PsiLiteralExpression.class) {
|
||||
public boolean accepts(@Nullable final Object o, final ProcessingContext context) {
|
||||
return o instanceof PsiLiteralExpression && (value == null || value.accepts(((PsiLiteralExpression)o).getValue(), context));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ElementPattern<?>> getPatterns() {
|
||||
return Collections.<ElementPattern<?>>singletonList(value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static PsiMemberPattern.Capture psiMember() {
|
||||
return new PsiMemberPattern.Capture();
|
||||
}
|
||||
|
||||
public static PsiMethodPattern psiMethod() {
|
||||
return new PsiMethodPattern();
|
||||
}
|
||||
|
||||
public static PsiParameterPattern psiParameter() {
|
||||
return new PsiParameterPattern();
|
||||
}
|
||||
|
||||
public static PsiModifierListOwnerPattern.Capture<PsiModifierListOwner> psiModifierListOwner() {
|
||||
return new PsiModifierListOwnerPattern.Capture<PsiModifierListOwner>(new InitialPatternCondition<PsiModifierListOwner>(PsiModifierListOwner.class) {
|
||||
@Override
|
||||
public boolean accepts(@Nullable Object o, ProcessingContext context) {
|
||||
return o instanceof PsiModifierListOwner;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public static PsiFieldPattern psiField() {
|
||||
return new PsiFieldPattern();
|
||||
}
|
||||
|
||||
public static PsiClassPattern psiClass() {
|
||||
return new PsiClassPattern();
|
||||
}
|
||||
|
||||
public static PsiAnnotationPattern psiAnnotation() {
|
||||
return new PsiAnnotationPattern();
|
||||
}
|
||||
|
||||
public static PsiNameValuePairPattern psiNameValuePair() {
|
||||
return new PsiNameValuePairPattern();
|
||||
}
|
||||
|
||||
public static PsiTypePattern psiType() {
|
||||
return new PsiTypePattern();
|
||||
}
|
||||
|
||||
public static PsiExpressionPattern.Capture<PsiExpression> psiExpression() {
|
||||
return new PsiExpressionPattern.Capture<PsiExpression>(PsiExpression.class);
|
||||
}
|
||||
|
||||
public static PsiBinaryExpressionPattern psiBinaryExpression() {
|
||||
return new PsiBinaryExpressionPattern();
|
||||
}
|
||||
|
||||
public static PsiTypeCastExpressionPattern psiTypeCastExpression() {
|
||||
return new PsiTypeCastExpressionPattern();
|
||||
}
|
||||
|
||||
public static PsiJavaElementPattern.Capture<PsiReferenceExpression> psiReferenceExpression() {
|
||||
return psiElement(PsiReferenceExpression.class);
|
||||
}
|
||||
|
||||
public static PsiStatementPattern.Capture<PsiExpressionStatement> psiExpressionStatement() {
|
||||
return new PsiStatementPattern.Capture<PsiExpressionStatement>(PsiExpressionStatement.class);
|
||||
}
|
||||
|
||||
public static PsiStatementPattern.Capture<PsiReturnStatement> psiReturnStatement() {
|
||||
return new PsiStatementPattern.Capture<PsiReturnStatement>(PsiReturnStatement.class);
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiMember;
|
||||
import com.intellij.util.PairProcessor;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class PsiMemberPattern<T extends PsiMember, Self extends PsiMemberPattern<T,Self>> extends PsiModifierListOwnerPattern<T,Self> {
|
||||
public PsiMemberPattern(@NotNull final InitialPatternCondition<T> condition) {
|
||||
super(condition);
|
||||
}
|
||||
|
||||
protected PsiMemberPattern(final Class<T> aClass) {
|
||||
super(aClass);
|
||||
}
|
||||
|
||||
public Self inClass(final @NonNls String qname) {
|
||||
return inClass(PsiJavaPatterns.psiClass().withQualifiedName(qname));
|
||||
}
|
||||
|
||||
public Self inClass(final ElementPattern pattern) {
|
||||
return with(new PatternConditionPlus<T, PsiClass>("inClass", pattern) {
|
||||
@Override
|
||||
public boolean processValues(T t, ProcessingContext context, PairProcessor<PsiClass, ProcessingContext> processor) {
|
||||
return processor.process(t.getContainingClass(), context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static class Capture extends PsiMemberPattern<PsiMember, Capture> {
|
||||
|
||||
protected Capture() {
|
||||
super(new InitialPatternCondition<PsiMember>(PsiMember.class) {
|
||||
public boolean accepts(@Nullable final Object o, final ProcessingContext context) {
|
||||
return o instanceof PsiMember;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.psi.PsiMethodCallExpression;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author nik
|
||||
*/
|
||||
public class PsiMethodCallPattern extends PsiExpressionPattern<PsiMethodCallExpression, PsiMethodCallPattern> {
|
||||
PsiMethodCallPattern() {
|
||||
super(PsiMethodCallExpression.class);
|
||||
}
|
||||
|
||||
public PsiMethodCallPattern withArguments(final ElementPattern<? extends PsiExpression>... arguments) {
|
||||
return with(new PatternCondition<PsiMethodCallExpression>("withArguments") {
|
||||
@Override
|
||||
public boolean accepts(@NotNull PsiMethodCallExpression callExpression, ProcessingContext context) {
|
||||
final PsiExpression[] actualArguments = callExpression.getArgumentList().getExpressions();
|
||||
if (arguments.length != actualArguments.length) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < actualArguments.length; i++) {
|
||||
if (!arguments[i].accepts(actualArguments[i], context)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public PsiMethodCallPattern withQualifier(final ElementPattern<? extends PsiExpression> qualifier) {
|
||||
return with(new PatternCondition<PsiMethodCallExpression>("withQualifier") {
|
||||
@Override
|
||||
public boolean accepts(@NotNull PsiMethodCallExpression psiMethodCallExpression, ProcessingContext context) {
|
||||
return qualifier.accepts(psiMethodCallExpression.getMethodExpression().getQualifierExpression(), context);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.searches.SuperMethodsSearch;
|
||||
import com.intellij.psi.util.MethodSignatureBackedByPsiMethod;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.PairProcessor;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import com.intellij.util.Processor;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class PsiMethodPattern extends PsiMemberPattern<PsiMethod,PsiMethodPattern> {
|
||||
public PsiMethodPattern() {
|
||||
super(PsiMethod.class);
|
||||
}
|
||||
|
||||
public PsiMethodPattern withParameterCount(@NonNls final int paramCount) {
|
||||
return with(new PatternCondition<PsiMethod>("withParameterCount") {
|
||||
@Override
|
||||
public boolean accepts(@NotNull final PsiMethod method, final ProcessingContext context) {
|
||||
return method.getParameterList().getParametersCount() == paramCount;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects the corrected method by argument types
|
||||
* @param inputTypes the array of FQN of the parameter types or wildcards.
|
||||
* The special values are:<bl><li>"?" - means any type</li><li>".." - instructs pattern to accept the rest of the arguments</li></bl>
|
||||
* @return
|
||||
*/
|
||||
public PsiMethodPattern withParameters(@NonNls final String... inputTypes) {
|
||||
final String[] types = inputTypes.length == 0 ? ArrayUtil.EMPTY_STRING_ARRAY : inputTypes;
|
||||
return with(new PatternCondition<PsiMethod>("withParameters") {
|
||||
@Override
|
||||
public boolean accepts(@NotNull final PsiMethod psiMethod, final ProcessingContext context) {
|
||||
final PsiParameterList parameterList = psiMethod.getParameterList();
|
||||
int dotsIndex = -1;
|
||||
while (++dotsIndex <types.length) {
|
||||
if (Comparing.equal("..", types[dotsIndex])) break;
|
||||
}
|
||||
|
||||
if (dotsIndex == types.length && parameterList.getParametersCount() != dotsIndex
|
||||
|| dotsIndex < types.length && parameterList.getParametersCount() < dotsIndex) {
|
||||
return false;
|
||||
}
|
||||
if (dotsIndex > 0) {
|
||||
final PsiParameter[] psiParameters = parameterList.getParameters();
|
||||
for (int i = 0; i < dotsIndex; i++) {
|
||||
if (!Comparing.equal("?", types[i]) && !typeEquivalent(psiParameters[i].getType(), types[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean typeEquivalent(PsiType type, String expectedText) {
|
||||
final PsiType erasure = TypeConversionUtil.erasure(type);
|
||||
final String text;
|
||||
if (erasure instanceof PsiEllipsisType && expectedText.endsWith("[]")) {
|
||||
text = ((PsiEllipsisType)erasure).getComponentType().getCanonicalText() + "[]";
|
||||
}
|
||||
else if (erasure instanceof PsiArrayType && expectedText.endsWith("...")) {
|
||||
text = ((PsiArrayType)erasure).getComponentType().getCanonicalText() +"...";
|
||||
}
|
||||
else {
|
||||
text = erasure.getCanonicalText();
|
||||
}
|
||||
return expectedText.equals(text);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public PsiMethodPattern definedInClass(@NonNls final String qname) {
|
||||
return definedInClass(PsiJavaPatterns.psiClass().withQualifiedName(qname));
|
||||
}
|
||||
|
||||
public PsiMethodPattern definedInClass(final ElementPattern<? extends PsiClass> pattern) {
|
||||
return with(new PatternConditionPlus<PsiMethod, PsiClass>("definedInClass", pattern) {
|
||||
|
||||
@Override
|
||||
public boolean processValues(PsiMethod t, final ProcessingContext context, final PairProcessor<PsiClass, ProcessingContext> processor) {
|
||||
if (!processor.process(t.getContainingClass(), context)) return false;
|
||||
final Ref<Boolean> result = Ref.create(Boolean.TRUE);
|
||||
SuperMethodsSearch.search(t, null, true, false).forEach(new Processor<MethodSignatureBackedByPsiMethod>() {
|
||||
@Override
|
||||
public boolean process(final MethodSignatureBackedByPsiMethod signature) {
|
||||
if (!processor.process(signature.getMethod().getContainingClass(), context)) {
|
||||
result.set(Boolean.FALSE);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
return result.get();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public PsiMethodPattern constructor(final boolean isConstructor) {
|
||||
return with(new PatternCondition<PsiMethod>("constructor") {
|
||||
@Override
|
||||
public boolean accepts(@NotNull final PsiMethod method, final ProcessingContext context) {
|
||||
return method.isConstructor() == isConstructor;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public PsiMethodPattern withThrowsList(final ElementPattern<?> pattern) {
|
||||
return with(new PatternCondition<PsiMethod>("withThrowsList") {
|
||||
@Override
|
||||
public boolean accepts(@NotNull final PsiMethod method, final ProcessingContext context) {
|
||||
return pattern.accepts(method.getThrowsList());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.psi.PsiModifierList;
|
||||
import com.intellij.psi.PsiModifierListOwner;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.codeInsight.AnnotationUtil;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class PsiModifierListOwnerPattern<T extends PsiModifierListOwner, Self extends PsiModifierListOwnerPattern<T,Self>> extends PsiElementPattern<T,Self> {
|
||||
public PsiModifierListOwnerPattern(@NotNull final InitialPatternCondition<T> condition) {
|
||||
super(condition);
|
||||
}
|
||||
|
||||
protected PsiModifierListOwnerPattern(final Class<T> aClass) {
|
||||
super(aClass);
|
||||
}
|
||||
|
||||
public Self withModifiers(final String... modifiers) {
|
||||
return with(new PatternCondition<T>("withModifiers") {
|
||||
public boolean accepts(@NotNull final T t, final ProcessingContext context) {
|
||||
return ContainerUtil.and(modifiers, new Condition<String>() {
|
||||
public boolean value(final String s) {
|
||||
return t.hasModifierProperty(s);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Self withoutModifiers(final String... modifiers) {
|
||||
return with(new PatternCondition<T>("withoutModifiers") {
|
||||
public boolean accepts(@NotNull final T t, final ProcessingContext context) {
|
||||
return ContainerUtil.and(modifiers, new Condition<String>() {
|
||||
public boolean value(final String s) {
|
||||
return !t.hasModifierProperty(s);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Self withAnnotation(@NonNls final String qualifiedName) {
|
||||
return with(new PatternCondition<T>("withAnnotation") {
|
||||
public boolean accepts(@NotNull final T t, final ProcessingContext context) {
|
||||
final PsiModifierList modifierList = t.getModifierList();
|
||||
return modifierList != null && modifierList.findAnnotation(qualifiedName) != null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Self withAnnotations(@NonNls final String... qualifiedNames) {
|
||||
return with(new PatternCondition<T>("withAnnotations") {
|
||||
public boolean accepts(@NotNull final T t, final ProcessingContext context) {
|
||||
return AnnotationUtil.findAnnotation(t, qualifiedNames) != null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static class Capture<T extends PsiModifierListOwner> extends PsiModifierListOwnerPattern<T, Capture<T>> {
|
||||
public Capture(@NotNull InitialPatternCondition<T> condition) {
|
||||
super(condition);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.psi.PsiNameValuePair;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class PsiNameValuePairPattern extends PsiElementPattern<PsiNameValuePair, PsiNameValuePairPattern> {
|
||||
protected PsiNameValuePairPattern() {
|
||||
super(PsiNameValuePair.class);
|
||||
}
|
||||
|
||||
public PsiNameValuePairPattern withName(@NotNull @NonNls final String requiredName) {
|
||||
return with(new PatternCondition<PsiNameValuePair>("withName") {
|
||||
public boolean accepts(@NotNull final PsiNameValuePair psiNameValuePair, final ProcessingContext context) {
|
||||
String actualName = psiNameValuePair.getName();
|
||||
return requiredName.equals(actualName) || actualName == null && "value".equals(requiredName);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiParameter;
|
||||
import com.intellij.util.PairProcessor;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author Gregory Shrago
|
||||
*/
|
||||
public class PsiParameterPattern extends PsiModifierListOwnerPattern<PsiParameter, PsiParameterPattern> {
|
||||
|
||||
protected PsiParameterPattern() {
|
||||
super(PsiParameter.class);
|
||||
}
|
||||
|
||||
public PsiParameterPattern ofMethod(final int index, final ElementPattern pattern) {
|
||||
return with(new PatternConditionPlus<PsiParameter, PsiMethod>("ofMethod", pattern) {
|
||||
@Override
|
||||
public boolean processValues(PsiParameter t,
|
||||
ProcessingContext context,
|
||||
PairProcessor<PsiMethod, ProcessingContext> processor) {
|
||||
PsiElement scope = t.getDeclarationScope();
|
||||
if (!(scope instanceof PsiMethod)) return true;
|
||||
return processor.process((PsiMethod)scope, context);
|
||||
}
|
||||
|
||||
public boolean accepts(@NotNull final PsiParameter t, final ProcessingContext context) {
|
||||
if (!super.accepts(t, context)) return false;
|
||||
final PsiMethod psiMethod = (PsiMethod)t.getDeclarationScope();
|
||||
|
||||
final PsiParameter[] parameters = psiMethod.getParameterList().getParameters();
|
||||
if (index < 0 || index >= parameters.length || !t.equals(parameters[index])) return false;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.psi.PsiMember;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiStatement;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
|
||||
/**
|
||||
* @author nik
|
||||
*/
|
||||
public class PsiStatementPattern<T extends PsiStatement, Self extends PsiStatementPattern<T, Self>> extends PsiJavaElementPattern<T, Self>{
|
||||
public PsiStatementPattern(final Class<T> aClass) {
|
||||
super(aClass);
|
||||
}
|
||||
|
||||
public Self insideMethod(final PsiMethodPattern pattern) {
|
||||
return with(new PatternCondition<T>("insideMethod") {
|
||||
public boolean accepts(@NotNull final T t, final ProcessingContext context) {
|
||||
PsiMethod method = PsiTreeUtil.getParentOfType(t, PsiMethod.class, false, PsiMember.class);
|
||||
return method != null && pattern.accepts(method, context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Self insideMethod(StringPattern methodName, String qualifiedClassName) {
|
||||
return insideMethod(PsiJavaPatterns.psiMethod().withName(methodName).definedInClass(qualifiedClassName));
|
||||
}
|
||||
|
||||
public Self insideMethod(@NotNull @NonNls String methodName, @NotNull @NonNls String qualifiedClassName) {
|
||||
return insideMethod(StandardPatterns.string().equalTo(methodName), qualifiedClassName);
|
||||
}
|
||||
|
||||
public static class Capture<T extends PsiStatement> extends PsiStatementPattern<T, Capture<T>> {
|
||||
public Capture(final Class<T> aClass) {
|
||||
super(aClass);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.PsiTypeCastExpression;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author nik
|
||||
*/
|
||||
public class PsiTypeCastExpressionPattern extends PsiExpressionPattern<PsiTypeCastExpression, PsiTypeCastExpressionPattern> {
|
||||
PsiTypeCastExpressionPattern() {
|
||||
super(PsiTypeCastExpression.class);
|
||||
}
|
||||
|
||||
public PsiTypeCastExpressionPattern withOperand(final ElementPattern<? extends PsiExpression> operand) {
|
||||
return with(new PatternCondition<PsiTypeCastExpression>("withOperand") {
|
||||
@Override
|
||||
public boolean accepts(@NotNull PsiTypeCastExpression psiTypeCastExpression, ProcessingContext context) {
|
||||
return operand.accepts(psiTypeCastExpression.getOperand(), context);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.patterns;
|
||||
|
||||
import com.intellij.psi.PsiArrayType;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiClassType;
|
||||
import com.intellij.psi.PsiType;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class PsiTypePattern extends ObjectPattern<PsiType,PsiTypePattern> {
|
||||
protected PsiTypePattern() {
|
||||
super(PsiType.class);
|
||||
}
|
||||
|
||||
public PsiTypePattern arrayOf(final ElementPattern pattern) {
|
||||
return with(new PatternCondition<PsiType>("arrayOf") {
|
||||
public boolean accepts(@NotNull final PsiType psiType, final ProcessingContext context) {
|
||||
return psiType instanceof PsiArrayType &&
|
||||
pattern.getCondition().accepts(((PsiArrayType)psiType).getComponentType(), context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public PsiTypePattern classType(final ElementPattern<? extends PsiClass> pattern) {
|
||||
return with(new PatternCondition<PsiType>("classType") {
|
||||
public boolean accepts(@NotNull final PsiType psiType, final ProcessingContext context) {
|
||||
return psiType instanceof PsiClassType &&
|
||||
pattern.getCondition().accepts(((PsiClassType)psiType).resolve(), context);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user