use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.
the class BaseRefactoringProcessor method createPresentation.
@NotNull
private static UsageViewPresentation createPresentation(@NotNull UsageViewDescriptor descriptor, @NotNull Usage[] usages) {
UsageViewPresentation presentation = new UsageViewPresentation();
presentation.setTabText(RefactoringBundle.message("usageView.tabText"));
presentation.setTargetsNodeText(descriptor.getProcessedElementsHeader());
presentation.setShowReadOnlyStatusAsRed(true);
presentation.setShowCancelButton(true);
presentation.setUsagesString(RefactoringBundle.message("usageView.usagesText"));
int codeUsageCount = 0;
int nonCodeUsageCount = 0;
int dynamicUsagesCount = 0;
Set<PsiFile> codeFiles = new HashSet<>();
Set<PsiFile> nonCodeFiles = new HashSet<>();
Set<PsiFile> dynamicUsagesCodeFiles = new HashSet<>();
for (Usage usage : usages) {
if (usage instanceof PsiElementUsage) {
final PsiElementUsage elementUsage = (PsiElementUsage) usage;
final PsiElement element = elementUsage.getElement();
if (element == null)
continue;
final PsiFile containingFile = element.getContainingFile();
if (elementUsage.isNonCodeUsage()) {
nonCodeUsageCount++;
nonCodeFiles.add(containingFile);
} else {
codeUsageCount++;
codeFiles.add(containingFile);
}
if (usage instanceof UsageInfo2UsageAdapter) {
final UsageInfo usageInfo = ((UsageInfo2UsageAdapter) usage).getUsageInfo();
if (usageInfo instanceof MoveRenameUsageInfo && usageInfo.isDynamicUsage()) {
dynamicUsagesCount++;
dynamicUsagesCodeFiles.add(containingFile);
}
}
}
}
codeFiles.remove(null);
nonCodeFiles.remove(null);
dynamicUsagesCodeFiles.remove(null);
String codeReferencesText = descriptor.getCodeReferencesText(codeUsageCount, codeFiles.size());
presentation.setCodeUsagesString(codeReferencesText);
final String commentReferencesText = descriptor.getCommentReferencesText(nonCodeUsageCount, nonCodeFiles.size());
if (commentReferencesText != null) {
presentation.setNonCodeUsagesString(commentReferencesText);
}
presentation.setDynamicUsagesString("Dynamic " + StringUtil.decapitalize(descriptor.getCodeReferencesText(dynamicUsagesCount, dynamicUsagesCodeFiles.size())));
String generatedCodeString;
if (codeReferencesText.contains("in code")) {
generatedCodeString = StringUtil.replace(codeReferencesText, "in code", "in generated code");
} else {
generatedCodeString = codeReferencesText + " in generated code";
}
presentation.setUsagesInGeneratedCodeString(generatedCodeString);
return presentation;
}
use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.
the class JavaMethodsConflictResolver method checkSameSignatures.
protected void checkSameSignatures(@NotNull List<CandidateInfo> conflicts, FactoryMap<MethodCandidateInfo, PsiSubstitutor> map) {
// candidates should go in order of class hierarchy traversal
// in order for this to work
Map<MethodSignature, CandidateInfo> signatures = new THashMap<>(conflicts.size());
Set<PsiMethod> superMethods = new HashSet<>();
for (CandidateInfo conflict : conflicts) {
final PsiMethod method = ((MethodCandidateInfo) conflict).getElement();
final PsiClass containingClass = method.getContainingClass();
final boolean isInterface = containingClass != null && containingClass.isInterface();
for (HierarchicalMethodSignature methodSignature : method.getHierarchicalMethodSignature().getSuperSignatures()) {
final PsiMethod superMethod = methodSignature.getMethod();
if (!isInterface) {
superMethods.add(superMethod);
} else {
final PsiClass aClass = superMethod.getContainingClass();
if (aClass != null && !CommonClassNames.JAVA_LANG_OBJECT.equals(aClass.getQualifiedName())) {
superMethods.add(superMethod);
}
}
}
}
nextConflict: for (int i = 0; i < conflicts.size(); i++) {
ProgressManager.checkCanceled();
CandidateInfo info = conflicts.get(i);
PsiMethod method = (PsiMethod) info.getElement();
if (!method.hasModifierProperty(PsiModifier.STATIC) && superMethods.contains(method)) {
conflicts.remove(i);
i--;
continue;
}
PsiClass class1 = method.getContainingClass();
PsiSubstitutor infoSubstitutor = getSubstitutor((MethodCandidateInfo) info, map);
MethodSignature signature = method.getSignature(infoSubstitutor);
CandidateInfo existing = signatures.get(signature);
if (existing == null) {
signatures.put(signature, info);
continue;
}
PsiMethod existingMethod = (PsiMethod) existing.getElement();
PsiClass existingClass = existingMethod.getContainingClass();
if (class1 != null && existingClass != null) {
//prefer interface methods to methods from Object
if (class1.isInterface() && CommonClassNames.JAVA_LANG_OBJECT.equals(existingClass.getQualifiedName())) {
signatures.put(signature, info);
continue;
} else if (existingClass.isInterface() && CommonClassNames.JAVA_LANG_OBJECT.equals(class1.getQualifiedName())) {
conflicts.remove(info);
i--;
continue;
}
}
if (method == existingMethod) {
PsiElement scope1 = info.getCurrentFileResolveScope();
PsiElement scope2 = existing.getCurrentFileResolveScope();
if (scope1 instanceof PsiClass && scope2 instanceof PsiClass && PsiTreeUtil.isAncestor(scope1, scope2, true) && !existing.isAccessible()) {
//prefer methods from outer class to inaccessible base class methods
signatures.put(signature, info);
continue;
}
}
// filter out methods with incorrect inferred bounds (for unrelated methods only)
boolean existingTypeParamAgree = areTypeParametersAgree(existing);
boolean infoTypeParamAgree = areTypeParametersAgree(info);
if (existingTypeParamAgree && !infoTypeParamAgree && !PsiSuperMethodImplUtil.isSuperMethodSmart(method, existingMethod)) {
conflicts.remove(i);
i--;
continue;
}
if (!existingTypeParamAgree && infoTypeParamAgree && !PsiSuperMethodImplUtil.isSuperMethodSmart(existingMethod, method)) {
signatures.put(signature, info);
int index = conflicts.indexOf(existing);
conflicts.remove(index);
i--;
continue;
}
if (InheritanceUtil.isInheritorOrSelf(class1, existingClass, true) || InheritanceUtil.isInheritorOrSelf(existingClass, class1, true)) {
PsiParameter[] parameters = method.getParameterList().getParameters();
final PsiParameter[] existingParameters = existingMethod.getParameterList().getParameters();
for (int i1 = 0, parametersLength = parameters.length; i1 < parametersLength; i1++) {
if (parameters[i1].getType() instanceof PsiArrayType && !(existingParameters[i1].getType() instanceof PsiArrayType)) {
//prefer more specific type
signatures.put(signature, info);
continue nextConflict;
}
}
PsiType returnType1 = method.getReturnType();
PsiType returnType2 = existingMethod.getReturnType();
if (returnType1 != null && returnType2 != null) {
returnType1 = infoSubstitutor.substitute(returnType1);
returnType2 = getSubstitutor((MethodCandidateInfo) existing, map).substitute(returnType2);
if (!returnType1.equals(returnType2) && returnType1.isAssignableFrom(returnType2)) {
conflicts.remove(i);
i--;
continue;
}
}
// prefer derived class
signatures.put(signature, info);
} else {
final PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(myArgumentsList, PsiMethodCallExpression.class);
if (methodCallExpression != null) {
final PsiReferenceExpression expression = methodCallExpression.getMethodExpression();
final PsiExpression qualifierExpression = expression.getQualifierExpression();
PsiClass currentClass;
if (qualifierExpression != null) {
currentClass = PsiUtil.resolveClassInClassTypeOnly(qualifierExpression.getType());
} else {
currentClass = PsiTreeUtil.getParentOfType(expression, PsiClass.class);
}
if (currentClass != null && existingClass != null && class1 != null) {
final PsiSubstitutor eSubstitutor = TypeConversionUtil.getMaybeSuperClassSubstitutor(existingClass, currentClass, PsiSubstitutor.EMPTY, null);
final PsiSubstitutor cSubstitutor = TypeConversionUtil.getMaybeSuperClassSubstitutor(class1, currentClass, PsiSubstitutor.EMPTY, null);
if (eSubstitutor != null && cSubstitutor != null && MethodSignatureUtil.areSignaturesEqual(existingMethod.getSignature(eSubstitutor), method.getSignature(cSubstitutor))) {
final PsiType returnType = eSubstitutor.substitute(existingMethod.getReturnType());
final PsiType returnType1 = cSubstitutor.substitute(method.getReturnType());
if (returnType != null && returnType1 != null && !returnType1.equals(returnType)) {
if (TypeConversionUtil.isAssignable(returnType, returnType1, false)) {
if (class1.isInterface() && !existingClass.isInterface())
continue;
conflicts.remove(existing);
} else {
if (!TypeConversionUtil.isAssignable(returnType1, returnType, false))
continue;
conflicts.remove(i);
}
i--;
break;
}
}
}
}
}
}
}
use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.
the class ConfigurationRefactoringsTest method testRefactorTestMethod.
public void testRefactorTestMethod() throws IOException {
PsiClass psiClass = mySource.createClass("ATest", TEST_CODE);
assertNotNull(psiClass);
PsiMethod testMethod = psiClass.findMethodsByName("test", false)[0];
JUnitConfiguration configuration = createConfiguration(testMethod);
rename(testMethod, "test1");
checkMethodName("test1", configuration);
checkClassName("ATest", configuration);
assertEquals("ATest.test1", configuration.getName());
move(psiClass, "pkg");
checkClassName("pkg.ATest", configuration);
psiClass = configuration.getConfigurationModule().findClass(configuration.getPersistentData().getMainClassName());
rename(psiClass, "TestClassName");
assertEquals("TestClassName.test1", configuration.getName());
psiClass = configuration.getConfigurationModule().findClass(configuration.getPersistentData().getMainClassName());
PsiClass otherTest = mySource.createClass("ATest", TEST_CODE);
HashSet<PsiMember> members = new HashSet<>();
assertNotNull(psiClass);
members.add(psiClass.findMethodsByName("test1", false)[0]);
moveMembers(otherTest, members);
psiClass = configuration.getConfigurationModule().findClass(configuration.getPersistentData().getMainClassName());
checkMethodName("test1", configuration);
checkClassName("ATest", configuration);
assertEquals("ATest.test1", configuration.getName());
assertNotNull(psiClass);
PsiMethod otherMethod = psiClass.findMethodsByName("otherMethod", false)[0];
rename(otherMethod, "newName");
checkMethodName("test1", configuration);
}
use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.
the class LibraryRootsComponent method getNotExcludedRoots.
private Set<VirtualFile> getNotExcludedRoots() {
Set<VirtualFile> roots = new LinkedHashSet<>();
String[] excludedRootUrls = getLibraryEditor().getExcludedRootUrls();
Set<VirtualFile> excludedRoots = new HashSet<>();
for (String url : excludedRootUrls) {
ContainerUtil.addIfNotNull(excludedRoots, VirtualFileManager.getInstance().findFileByUrl(url));
}
for (PersistentOrderRootType type : OrderRootType.getAllPersistentTypes()) {
VirtualFile[] files = getLibraryEditor().getFiles(type);
for (VirtualFile file : files) {
if (!VfsUtilCore.isUnder(file, excludedRoots)) {
roots.add(PathUtil.getLocalFile(file));
}
}
}
return roots;
}
use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.
the class GotoCustomRegionAction method getCustomFoldingDescriptors.
@NotNull
private static Collection<FoldingDescriptor> getCustomFoldingDescriptors(@NotNull Editor editor, @NotNull Project project) {
Set<FoldingDescriptor> foldingDescriptors = new HashSet<>();
final Document document = editor.getDocument();
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
PsiFile file = documentManager != null ? documentManager.getPsiFile(document) : null;
if (file != null) {
final FileViewProvider viewProvider = file.getViewProvider();
for (final Language language : viewProvider.getLanguages()) {
final PsiFile psi = viewProvider.getPsi(language);
final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language);
if (psi != null) {
for (FoldingDescriptor descriptor : LanguageFolding.buildFoldingDescriptors(foldingBuilder, psi, document, false)) {
CustomFoldingBuilder customFoldingBuilder = getCustomFoldingBuilder(foldingBuilder, descriptor);
if (customFoldingBuilder != null) {
if (customFoldingBuilder.isCustomRegionStart(descriptor.getElement())) {
foldingDescriptors.add(descriptor);
}
}
}
}
}
}
return foldingDescriptors;
}
Aggregations