use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.
the class InlineToAnonymousClassProcessor method findUsages.
@NotNull
protected UsageInfo[] findUsages() {
if (myInlineThisOnly) {
return new UsageInfo[] { new UsageInfo(myCallToInline) };
}
Set<UsageInfo> usages = new HashSet<>();
final GlobalSearchScope searchScope = GlobalSearchScope.projectScope(myProject);
for (PsiReference reference : ReferencesSearch.search(myClass, searchScope)) {
usages.add(new UsageInfo(reference.getElement()));
}
final String qName = myClass.getQualifiedName();
if (qName != null) {
List<UsageInfo> nonCodeUsages = new ArrayList<>();
if (mySearchInComments) {
TextOccurrencesUtil.addUsagesInStringsAndComments(myClass, qName, nonCodeUsages, new NonCodeUsageInfoFactory(myClass, qName));
}
if (mySearchInNonJavaFiles) {
TextOccurrencesUtil.addTextOccurences(myClass, qName, searchScope, nonCodeUsages, new NonCodeUsageInfoFactory(myClass, qName));
}
usages.addAll(nonCodeUsages);
}
return usages.toArray(new UsageInfo[usages.size()]);
}
use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.
the class InlineToAnonymousClassProcessor method performRefactoring.
protected void performRefactoring(@NotNull UsageInfo[] usages) {
final PsiClassType superType = getSuperType(myClass);
LOG.assertTrue(superType != null);
List<PsiElement> elementsToDelete = new ArrayList<>();
List<PsiNewExpression> newExpressions = new ArrayList<>();
for (UsageInfo info : usages) {
final PsiElement element = info.getElement();
if (element instanceof PsiNewExpression) {
newExpressions.add((PsiNewExpression) element);
} else if (element.getParent() instanceof PsiNewExpression) {
newExpressions.add((PsiNewExpression) element.getParent());
} else {
PsiImportStatement statement = PsiTreeUtil.getParentOfType(element, PsiImportStatement.class);
if (statement != null && !myInlineThisOnly) {
elementsToDelete.add(statement);
} else {
PsiTypeElement typeElement = PsiTreeUtil.getParentOfType(element, PsiTypeElement.class);
if (typeElement != null) {
replaceWithSuperType(typeElement, superType);
}
}
}
}
Collections.sort(newExpressions, PsiUtil.BY_POSITION);
for (PsiNewExpression newExpression : newExpressions) {
replaceNewOrType(newExpression, superType);
}
for (PsiElement element : elementsToDelete) {
try {
if (element.isValid()) {
element.delete();
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
if (!myInlineThisOnly && myClass.getOriginalElement().isWritable()) {
try {
myClass.delete();
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}
use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.
the class InlineSuperCallUsageInfo method getConflictMessage.
@Override
public String getConflictMessage() {
final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
final PsiElement element = getElement();
if (element instanceof PsiMethodCallExpression) {
PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) element;
final PsiMethod superConstructor = methodCallExpression.resolveMethod();
if (superConstructor != null) {
InlineMethodProcessor.addInaccessibleMemberConflicts(superConstructor, new UsageInfo[] { new UsageInfo(methodCallExpression.getMethodExpression()) }, new ReferencedElementsCollector() {
@Override
protected void checkAddMember(@NotNull PsiMember member) {
if (!PsiTreeUtil.isAncestor(superConstructor.getContainingClass(), member, false)) {
super.checkAddMember(member);
}
}
}, conflicts);
if (InlineMethodProcessor.checkBadReturns(superConstructor) && !InlineUtil.allUsagesAreTailCalls(superConstructor)) {
conflicts.putValue(superConstructor, CommonRefactoringUtil.capitalize(RefactoringBundle.message("refactoring.is.not.supported.when.return.statement.interrupts.the.execution.flow", "") + " of super constructor"));
}
}
}
//todo
return conflicts.isEmpty() ? null : conflicts.values().iterator().next();
}
use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.
the class InlineMethodProcessor method getInaccessible.
/**
* Given a set of referencedElements, returns a map from containers (in a sense of ConflictsUtil.getContainer)
* to subsets of referencedElemens that are not accessible from that container
*
* @param referencedElements
* @param usages
* @param elementToInline
*/
private static Map<PsiMember, Set<PsiMember>> getInaccessible(HashSet<PsiMember> referencedElements, UsageInfo[] usages, PsiElement elementToInline) {
final Map<PsiMember, Set<PsiMember>> result = new HashMap<>();
final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(elementToInline.getProject()).getResolveHelper();
for (UsageInfo usage : usages) {
final PsiElement usageElement = usage.getElement();
if (usageElement == null)
continue;
final PsiElement container = ConflictsUtil.getContainer(usageElement);
// usage in import statement
if (!(container instanceof PsiMember))
continue;
PsiMember memberContainer = (PsiMember) container;
Set<PsiMember> inaccessibleReferenced = result.get(memberContainer);
if (inaccessibleReferenced == null) {
inaccessibleReferenced = new HashSet<>();
result.put(memberContainer, inaccessibleReferenced);
for (PsiMember member : referencedElements) {
if (PsiTreeUtil.isAncestor(elementToInline, member, false))
continue;
if (elementToInline instanceof PsiClass && InheritanceUtil.isInheritorOrSelf((PsiClass) elementToInline, member.getContainingClass(), true))
continue;
PsiElement resolveScope = usageElement instanceof PsiReferenceExpression ? ((PsiReferenceExpression) usageElement).advancedResolve(false).getCurrentFileResolveScope() : null;
if (!resolveHelper.isAccessible(member, member.getModifierList(), usageElement, null, resolveScope)) {
inaccessibleReferenced.add(member);
}
}
}
}
return result;
}
use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.
the class InlineParameterExpressionProcessor method performRefactoring.
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
final List<PsiClassType> thrownExceptions = ExceptionUtil.getThrownCheckedExceptions(myInitializer);
final Set<PsiVariable> varsUsedInInitializer = new HashSet<>();
final Set<PsiJavaCodeReferenceElement> paramRefsToInline = new HashSet<>();
final Map<PsiElement, PsiElement> replacements = new HashMap<>();
for (UsageInfo usage : usages) {
if (usage instanceof LocalReplacementUsageInfo) {
final LocalReplacementUsageInfo replacementUsageInfo = (LocalReplacementUsageInfo) usage;
final PsiElement element = replacementUsageInfo.getElement();
final PsiElement replacement = replacementUsageInfo.getReplacement();
if (element != null && replacement != null) {
replacements.put(element, replacement);
}
varsUsedInInitializer.add(replacementUsageInfo.getVariable());
} else {
LOG.assertTrue(!myCreateLocal);
paramRefsToInline.add((PsiJavaCodeReferenceElement) usage.getElement());
}
}
myInitializer = (PsiExpression) RefactoringUtil.replaceElementsWithMap(myInitializer, replacements);
if (myCreateLocal) {
final PsiElementFactory factory = JavaPsiFacade.getInstance(myMethod.getProject()).getElementFactory();
PsiDeclarationStatement localDeclaration = factory.createVariableDeclarationStatement(myParameter.getName(), myParameter.getType(), myInitializer);
final PsiLocalVariable declaredVar = (PsiLocalVariable) localDeclaration.getDeclaredElements()[0];
PsiUtil.setModifierProperty(declaredVar, PsiModifier.FINAL, myParameter.hasModifierProperty(PsiModifier.FINAL));
final PsiExpression localVarInitializer = InlineUtil.inlineVariable(myParameter, myInitializer, (PsiReferenceExpression) factory.createExpressionFromText(myParameter.getName(), myMethod));
final PsiExpression initializer = declaredVar.getInitializer();
LOG.assertTrue(initializer != null);
initializer.replace(localVarInitializer);
final PsiCodeBlock body = myMethod.getBody();
if (body != null) {
PsiElement anchor = findAnchorForLocalVariableDeclaration(body);
body.addAfter(localDeclaration, anchor);
}
} else {
for (PsiJavaCodeReferenceElement paramRef : paramRefsToInline) {
InlineUtil.inlineVariable(myParameter, myInitializer, paramRef);
}
}
//delete var if it becomes unused
for (PsiVariable variable : varsUsedInInitializer) {
if (variable != null && variable.isValid()) {
if (ReferencesSearch.search(variable).findFirst() == null) {
variable.delete();
}
}
}
ChangeSignatureProcessorBase.doChangeSignature(myChangeInfo, myChangeSignatureUsages);
if (!thrownExceptions.isEmpty()) {
for (PsiClassType exception : thrownExceptions) {
PsiClass exceptionClass = exception.resolve();
if (exceptionClass != null) {
PsiUtil.addException(myMethod, exceptionClass);
}
}
}
}
Aggregations