use of com.intellij.psi.search.SearchScope in project intellij-community by JetBrains.
the class PsiAnnotationMethodReferencesSearcher method execute.
@Override
public boolean execute(@NotNull final ReferencesSearch.SearchParameters p, @NotNull final Processor<PsiReference> consumer) {
final PsiElement refElement = p.getElementToSearch();
boolean isAnnotation = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
@Override
public Boolean compute() {
return PsiUtil.isAnnotationMethod(refElement);
}
});
if (isAnnotation) {
final PsiMethod method = (PsiMethod) refElement;
PsiClass containingClass = ApplicationManager.getApplication().runReadAction(new Computable<PsiClass>() {
@Override
public PsiClass compute() {
boolean isValueMethod = PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME.equals(method.getName()) && method.getParameterList().getParametersCount() == 0;
return isValueMethod ? method.getContainingClass() : null;
}
});
if (containingClass != null) {
SearchScope scope = ApplicationManager.getApplication().runReadAction(new Computable<SearchScope>() {
@Override
public SearchScope compute() {
return p.getEffectiveSearchScope();
}
});
final Query<PsiReference> query = ReferencesSearch.search(containingClass, scope, p.isIgnoreAccessScope());
return query.forEach(createImplicitDefaultAnnotationMethodConsumer(consumer));
}
}
return true;
}
use of com.intellij.psi.search.SearchScope in project intellij-community by JetBrains.
the class ExtractMethodObjectProcessor method findUsages.
@NotNull
protected UsageInfo[] findUsages() {
final ArrayList<UsageInfo> result = new ArrayList<>();
final PsiClass containingClass = getMethod().getContainingClass();
final SearchScope scope = PsiUtilCore.getVirtualFile(containingClass) == null ? new LocalSearchScope(containingClass) : GlobalSearchScope.projectScope(myProject);
PsiReference[] refs = ReferencesSearch.search(getMethod(), scope, false).toArray(PsiReference.EMPTY_ARRAY);
for (PsiReference ref : refs) {
final PsiElement element = ref.getElement();
if (element != null && element.isValid()) {
result.add(new UsageInfo(element));
}
}
if (isCreateInnerClass()) {
final Set<PsiMethod> usedMethods = new LinkedHashSet<>();
getMethod().accept(new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
final PsiMethod method = expression.resolveMethod();
if (method != null) {
usedMethods.add(method);
}
}
});
for (PsiMethod usedMethod : usedMethods) {
if (usedMethod.hasModifierProperty(PsiModifier.PRIVATE) && (!usedMethod.hasModifierProperty(PsiModifier.STATIC) || myExtractProcessor.isStatic())) {
PsiMethod toMove = usedMethod;
for (PsiReference reference : ReferencesSearch.search(usedMethod)) {
if (!PsiTreeUtil.isAncestor(getMethod(), reference.getElement(), false)) {
toMove = null;
break;
}
}
if (toMove != null) {
myUsages.add(new MethodToMoveUsageInfo(toMove));
}
}
}
}
UsageInfo[] usageInfos = result.toArray(new UsageInfo[result.size()]);
return UsageViewUtil.removeDuplicatedUsages(usageInfos);
}
use of com.intellij.psi.search.SearchScope in project intellij-community by JetBrains.
the class CachesBasedRefSearcher method processQuery.
@Override
public void processQuery(@NotNull ReferencesSearch.SearchParameters p, @NotNull Processor<PsiReference> consumer) {
final PsiElement refElement = p.getElementToSearch();
boolean caseSensitive = refElement.getLanguage().isCaseSensitive();
String text = null;
if (refElement instanceof PsiFileSystemItem && !(refElement instanceof SyntheticFileSystemItem)) {
final VirtualFile vFile = ((PsiFileSystemItem) refElement).getVirtualFile();
if (vFile != null) {
text = vFile.getNameWithoutExtension();
}
// We must not look for file references with the file language's case-sensitivity,
// since case-sensitivity of the references themselves depends either on file system
// or on the rules of the language of reference
caseSensitive = false;
} else if (refElement instanceof PsiNamedElement) {
text = ((PsiNamedElement) refElement).getName();
if (refElement instanceof PsiMetaOwner) {
final PsiMetaData metaData = ((PsiMetaOwner) refElement).getMetaData();
if (metaData != null)
text = metaData.getName();
}
}
if (text == null && refElement instanceof PsiMetaOwner) {
final PsiMetaData metaData = ((PsiMetaOwner) refElement).getMetaData();
if (metaData != null)
text = metaData.getName();
}
if (StringUtil.isNotEmpty(text)) {
final SearchScope searchScope = p.getEffectiveSearchScope();
p.getOptimizer().searchWord(text, searchScope, caseSensitive, refElement);
}
}
use of com.intellij.psi.search.SearchScope in project intellij-community by JetBrains.
the class VariableAccessVisitor method buildReferenceMap.
private static HashMap<PsiMethod, Collection<PsiReference>> buildReferenceMap(Set<PsiMethod> privateMethods) {
final HashMap<PsiMethod, Collection<PsiReference>> referenceMap = new HashMap<>();
for (PsiMethod method : privateMethods) {
ProgressManager.checkCanceled();
final SearchScope scope = method.getUseScope();
final Collection<PsiReference> references = ReferencesSearch.search(method, scope).findAll();
referenceMap.put(method, references);
}
return referenceMap;
}
use of com.intellij.psi.search.SearchScope in project intellij-community by JetBrains.
the class IncrementDecrementUsedAsExpressionInspection method extractPrefixPostfixExpressionToSeparateStatement.
public static void extractPrefixPostfixExpressionToSeparateStatement(PsiElement element) {
final PsiExpression operand;
if (element instanceof PsiPostfixExpression) {
final PsiPostfixExpression postfixExpression = (PsiPostfixExpression) element;
operand = postfixExpression.getOperand();
} else if (element instanceof PsiPrefixExpression) {
final PsiPrefixExpression prefixExpression = (PsiPrefixExpression) element;
operand = prefixExpression.getOperand();
} else {
assert false;
return;
}
if (operand == null) {
return;
}
final PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiStatement.class);
if (statement == null) {
return;
}
final PsiElement parent = statement.getParent();
if (parent == null) {
return;
}
final Project project = element.getProject();
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
final String newStatementText = element.getText() + ';';
final String operandText = operand.getText();
if (parent instanceof PsiIfStatement || parent instanceof PsiLoopStatement) {
// need to add braces because
// in/decrement is inside braceless control statement body
final StringBuilder text = new StringBuilder();
text.append('{');
final String elementText = PsiReplacementUtil.getElementText(statement, element, operandText);
if (element instanceof PsiPostfixExpression) {
text.append(elementText);
text.append(newStatementText);
} else {
text.append(newStatementText);
text.append(elementText);
}
text.append('}');
final PsiCodeBlock codeBlock = factory.createCodeBlockFromText(text.toString(), parent);
statement.replace(codeBlock);
return;
}
final PsiStatement newStatement = factory.createStatementFromText(newStatementText, element);
if (statement instanceof PsiReturnStatement) {
if (element instanceof PsiPostfixExpression) {
// special handling of postfix expression in return statement
final PsiReturnStatement returnStatement = (PsiReturnStatement) statement;
final PsiExpression returnValue = returnStatement.getReturnValue();
if (returnValue == null) {
return;
}
final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
final String variableName = javaCodeStyleManager.suggestUniqueVariableName("result", returnValue, true);
final PsiType type = returnValue.getType();
if (type == null) {
return;
}
final String newReturnValueText = PsiReplacementUtil.getElementText(returnValue, element, operandText);
final String declarationStatementText = type.getCanonicalText() + ' ' + variableName + '=' + newReturnValueText + ';';
final PsiStatement declarationStatement = factory.createStatementFromText(declarationStatementText, returnStatement);
parent.addBefore(declarationStatement, statement);
parent.addBefore(newStatement, statement);
final PsiStatement newReturnStatement = factory.createStatementFromText("return " + variableName + ';', returnStatement);
returnStatement.replace(newReturnStatement);
return;
} else {
parent.addBefore(newStatement, statement);
}
} else if (statement instanceof PsiThrowStatement) {
if (element instanceof PsiPostfixExpression) {
// special handling of postfix expression in throw statement
final PsiThrowStatement returnStatement = (PsiThrowStatement) statement;
final PsiExpression exception = returnStatement.getException();
if (exception == null) {
return;
}
final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
final String variableName = javaCodeStyleManager.suggestUniqueVariableName("e", exception, true);
final PsiType type = exception.getType();
if (type == null) {
return;
}
final String newReturnValueText = PsiReplacementUtil.getElementText(exception, element, operandText);
final String declarationStatementText = type.getCanonicalText() + ' ' + variableName + '=' + newReturnValueText + ';';
final PsiStatement declarationStatement = factory.createStatementFromText(declarationStatementText, returnStatement);
parent.addBefore(declarationStatement, statement);
parent.addBefore(newStatement, statement);
final PsiStatement newReturnStatement = factory.createStatementFromText("throw " + variableName + ';', returnStatement);
returnStatement.replace(newReturnStatement);
return;
} else {
parent.addBefore(newStatement, statement);
}
} else if (!(statement instanceof PsiForStatement)) {
if (element instanceof PsiPostfixExpression) {
parent.addAfter(newStatement, statement);
} else {
parent.addBefore(newStatement, statement);
}
} else if (operand instanceof PsiReferenceExpression) {
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) operand;
final PsiElement target = referenceExpression.resolve();
if (target != null) {
final SearchScope useScope = target.getUseScope();
if (!new LocalSearchScope(statement).equals(useScope)) {
if (element instanceof PsiPostfixExpression) {
parent.addAfter(newStatement, statement);
} else {
parent.addBefore(newStatement, statement);
}
}
}
}
if (statement instanceof PsiLoopStatement) {
// in/decrement inside loop statement condition
final PsiLoopStatement loopStatement = (PsiLoopStatement) statement;
final PsiStatement body = loopStatement.getBody();
if (body instanceof PsiBlockStatement) {
final PsiBlockStatement blockStatement = (PsiBlockStatement) body;
final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
if (element instanceof PsiPostfixExpression) {
final PsiElement firstElement = codeBlock.getFirstBodyElement();
codeBlock.addBefore(newStatement, firstElement);
} else {
codeBlock.add(newStatement);
}
} else {
final StringBuilder blockText = new StringBuilder();
blockText.append('{');
if (element instanceof PsiPostfixExpression) {
blockText.append(newStatementText);
if (body != null) {
blockText.append(body.getText());
}
} else {
if (body != null) {
blockText.append(body.getText());
}
blockText.append(newStatementText);
}
blockText.append('}');
final PsiStatement blockStatement = factory.createStatementFromText(blockText.toString(), statement);
if (body == null) {
loopStatement.add(blockStatement);
} else {
body.replace(blockStatement);
}
}
}
PsiReplacementUtil.replaceExpression((PsiExpression) element, operandText);
}
Aggregations