use of gnu.trove.THashMap in project intellij-community by JetBrains.
the class GenerateVisitorByHierarchyAction method generateVisitorClass.
private static void generateVisitorClass(final String visitorName, final PsiClass baseClass, final PsiDirectory directory, final GlobalSearchScope scope) {
final THashMap<PsiClass, Set<PsiClass>> classes = new THashMap<>();
for (PsiClass aClass : ClassInheritorsSearch.search(baseClass, scope, true).findAll()) {
if (aClass.hasModifierProperty(PsiModifier.ABSTRACT) == baseClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
final List<PsiClass> implementors = ContainerUtil.findAll(ClassInheritorsSearch.search(aClass).findAll(), psiClass -> !psiClass.hasModifierProperty(PsiModifier.ABSTRACT));
classes.put(aClass, new THashSet<>(implementors));
}
}
final THashMap<PsiClass, Set<PsiClass>> pathMap = new THashMap<>();
for (PsiClass aClass : classes.keySet()) {
final Set<PsiClass> superClasses = new LinkedHashSet<>();
for (PsiClass superClass : aClass.getSupers()) {
if (superClass.isInheritor(baseClass, true)) {
superClasses.add(superClass);
final Set<PsiClass> superImplementors = classes.get(superClass);
if (superImplementors != null) {
superImplementors.removeAll(classes.get(aClass));
}
}
}
if (superClasses.isEmpty()) {
superClasses.add(baseClass);
}
pathMap.put(aClass, superClasses);
}
pathMap.put(baseClass, Collections.<PsiClass>emptySet());
final ArrayList<PsiFile> psiFiles = new ArrayList<>();
for (Set<PsiClass> implementors : classes.values()) {
for (PsiClass psiClass : implementors) {
psiFiles.add(psiClass.getContainingFile());
}
}
final Project project = baseClass.getProject();
final PsiClass visitorClass = JavaPsiFacade.getInstance(project).findClass(visitorName, GlobalSearchScope.projectScope(project));
if (visitorClass != null) {
psiFiles.add(visitorClass.getContainingFile());
}
final int finalDetectedPrefix = detectClassPrefix(classes.keySet()).length();
new WriteCommandAction(project, PsiUtilCore.toPsiFileArray(psiFiles)) {
protected void run(@NotNull final Result result) throws Throwable {
if (visitorClass == null) {
final String shortClassName = PsiNameHelper.getShortClassName(visitorName);
if (directory != null) {
final PsiClass visitorClass = JavaDirectoryService.getInstance().createClass(directory, shortClassName);
generateVisitorClass(visitorClass, classes, pathMap, finalDetectedPrefix);
}
} else {
generateVisitorClass(visitorClass, classes, pathMap, finalDetectedPrefix);
}
}
@Override
protected boolean isGlobalUndoAction() {
return true;
}
}.execute();
}
use of gnu.trove.THashMap in project intellij-community by JetBrains.
the class PsiSuperMethodImplUtil method buildMethodHierarchy.
@NotNull
private static Map<MethodSignature, HierarchicalMethodSignature> buildMethodHierarchy(@NotNull PsiClass aClass, @Nullable String nameHint, @NotNull PsiSubstitutor substitutor, final boolean includePrivates, @NotNull final Set<PsiClass> visited, boolean isInRawContext, GlobalSearchScope resolveScope) {
ProgressManager.checkCanceled();
Map<MethodSignature, HierarchicalMethodSignature> result = new LinkedHashMap<>(new EqualityPolicy<MethodSignature>() {
@Override
public int getHashCode(MethodSignature object) {
return object.hashCode();
}
@Override
public boolean isEqual(MethodSignature o1, MethodSignature o2) {
if (o1.equals(o2)) {
final PsiMethod method1 = ((MethodSignatureBackedByPsiMethod) o1).getMethod();
final PsiType returnType1 = method1.getReturnType();
final PsiMethod method2 = ((MethodSignatureBackedByPsiMethod) o2).getMethod();
final PsiType returnType2 = method2.getReturnType();
if (method1.hasModifierProperty(PsiModifier.STATIC) || method2.hasModifierProperty(PsiModifier.STATIC)) {
return true;
}
if (MethodSignatureUtil.isReturnTypeSubstitutable(o1, o2, returnType1, returnType2)) {
return true;
}
final PsiClass containingClass1 = method1.getContainingClass();
final PsiClass containingClass2 = method2.getContainingClass();
if (containingClass1 != null && containingClass2 != null) {
return containingClass1.isAnnotationType() || containingClass2.isAnnotationType();
}
}
return false;
}
});
final Map<MethodSignature, List<PsiMethod>> sameParameterErasureMethods = new THashMap<>(MethodSignatureUtil.METHOD_PARAMETERS_ERASURE_EQUALITY);
Map<MethodSignature, HierarchicalMethodSignatureImpl> map = new THashMap<>(new TObjectHashingStrategy<MethodSignature>() {
@Override
public int computeHashCode(MethodSignature signature) {
return MethodSignatureUtil.METHOD_PARAMETERS_ERASURE_EQUALITY.computeHashCode(signature);
}
@Override
public boolean equals(MethodSignature o1, MethodSignature o2) {
if (!MethodSignatureUtil.METHOD_PARAMETERS_ERASURE_EQUALITY.equals(o1, o2))
return false;
List<PsiMethod> list = sameParameterErasureMethods.get(o1);
boolean toCheckReturnType = list != null && list.size() > 1;
if (!toCheckReturnType)
return true;
PsiType returnType1 = ((MethodSignatureBackedByPsiMethod) o1).getMethod().getReturnType();
PsiType returnType2 = ((MethodSignatureBackedByPsiMethod) o2).getMethod().getReturnType();
if (returnType1 == null && returnType2 == null)
return true;
if (returnType1 == null || returnType2 == null)
return false;
PsiType erasure1 = TypeConversionUtil.erasure(o1.getSubstitutor().substitute(returnType1));
PsiType erasure2 = TypeConversionUtil.erasure(o2.getSubstitutor().substitute(returnType2));
return erasure1.equals(erasure2);
}
});
PsiMethod[] methods = aClass.getMethods();
if ((nameHint == null || "values".equals(nameHint)) && aClass instanceof PsiClassImpl) {
final PsiMethod valuesMethod = ((PsiClassImpl) aClass).getValuesMethod();
if (valuesMethod != null) {
methods = ArrayUtil.append(methods, valuesMethod);
}
}
for (PsiMethod method : methods) {
if (!method.isValid()) {
throw new PsiInvalidElementAccessException(method, "class.valid=" + aClass.isValid() + "; name=" + method.getName());
}
if (nameHint != null && !nameHint.equals(method.getName()))
continue;
if (!includePrivates && method.hasModifierProperty(PsiModifier.PRIVATE))
continue;
final MethodSignatureBackedByPsiMethod signature = MethodSignatureBackedByPsiMethod.create(method, PsiSubstitutor.EMPTY, isInRawContext);
HierarchicalMethodSignatureImpl newH = new HierarchicalMethodSignatureImpl(MethodSignatureBackedByPsiMethod.create(method, substitutor, isInRawContext));
List<PsiMethod> list = sameParameterErasureMethods.get(signature);
if (list == null) {
list = new SmartList<>();
sameParameterErasureMethods.put(signature, list);
}
list.add(method);
LOG.assertTrue(newH.getMethod().isValid());
result.put(signature, newH);
map.put(signature, newH);
}
final List<PsiClassType.ClassResolveResult> superTypes = PsiClassImplUtil.getScopeCorrectedSuperTypes(aClass, resolveScope);
for (PsiClassType.ClassResolveResult superTypeResolveResult : superTypes) {
PsiClass superClass = superTypeResolveResult.getElement();
if (superClass == null)
continue;
// cyclic inheritance
if (!visited.add(superClass))
continue;
final PsiSubstitutor superSubstitutor = superTypeResolveResult.getSubstitutor();
PsiSubstitutor finalSubstitutor = PsiSuperMethodUtil.obtainFinalSubstitutor(superClass, superSubstitutor, substitutor, isInRawContext);
final boolean isInRawContextSuper = (isInRawContext || PsiUtil.isRawSubstitutor(superClass, superSubstitutor)) && superClass.getTypeParameters().length != 0;
Map<MethodSignature, HierarchicalMethodSignature> superResult = buildMethodHierarchy(superClass, nameHint, finalSubstitutor, false, visited, isInRawContextSuper, resolveScope);
visited.remove(superClass);
List<Pair<MethodSignature, HierarchicalMethodSignature>> flattened = new ArrayList<>();
for (Map.Entry<MethodSignature, HierarchicalMethodSignature> entry : superResult.entrySet()) {
HierarchicalMethodSignature hms = entry.getValue();
MethodSignature signature = MethodSignatureBackedByPsiMethod.create(hms.getMethod(), hms.getSubstitutor(), hms.isRaw());
PsiClass containingClass = hms.getMethod().getContainingClass();
List<HierarchicalMethodSignature> supers = new ArrayList<>(hms.getSuperSignatures());
for (HierarchicalMethodSignature aSuper : supers) {
PsiClass superContainingClass = aSuper.getMethod().getContainingClass();
if (containingClass != null && superContainingClass != null && !containingClass.isInheritor(superContainingClass, true)) {
// methods must be inherited from unrelated classes, so flatten hierarchy here
// class C implements SAM1, SAM2 { void methodimpl() {} }
//hms.getSuperSignatures().remove(aSuper);
flattened.add(Pair.create(signature, aSuper));
}
}
putInMap(aClass, result, map, hms, signature);
}
for (Pair<MethodSignature, HierarchicalMethodSignature> pair : flattened) {
putInMap(aClass, result, map, pair.second, pair.first);
}
}
for (Map.Entry<MethodSignature, HierarchicalMethodSignatureImpl> entry : map.entrySet()) {
HierarchicalMethodSignatureImpl hierarchicalMethodSignature = entry.getValue();
MethodSignature methodSignature = entry.getKey();
if (result.get(methodSignature) == null) {
LOG.assertTrue(hierarchicalMethodSignature.getMethod().isValid());
result.put(methodSignature, hierarchicalMethodSignature);
}
}
return result;
}
use of gnu.trove.THashMap in project intellij-community by JetBrains.
the class FileManagerImpl method removeInvalidFilesAndDirs.
void removeInvalidFilesAndDirs(boolean useFind) {
Map<VirtualFile, PsiDirectory> fileToPsiDirMap = new THashMap<>(myVFileToPsiDirMap);
if (useFind) {
myVFileToPsiDirMap.clear();
}
for (Iterator<VirtualFile> iterator = fileToPsiDirMap.keySet().iterator(); iterator.hasNext(); ) {
VirtualFile vFile = iterator.next();
if (!vFile.isValid()) {
iterator.remove();
} else {
PsiDirectory psiDir = findDirectory(vFile);
if (psiDir == null) {
iterator.remove();
}
}
}
myVFileToPsiDirMap.clear();
myVFileToPsiDirMap.putAll(fileToPsiDirMap);
// note: important to update directories map first - findFile uses findDirectory!
Map<VirtualFile, FileViewProvider> fileToPsiFileMap = new THashMap<>(myVFileToViewProviderMap);
Map<VirtualFile, FileViewProvider> originalFileToPsiFileMap = new THashMap<>(myVFileToViewProviderMap);
if (useFind) {
myVFileToViewProviderMap.clear();
}
for (Iterator<VirtualFile> iterator = fileToPsiFileMap.keySet().iterator(); iterator.hasNext(); ) {
VirtualFile vFile = iterator.next();
if (!vFile.isValid()) {
iterator.remove();
continue;
}
if (useFind) {
FileViewProvider view = fileToPsiFileMap.get(vFile);
if (view == null) {
// soft ref. collected
iterator.remove();
continue;
}
PsiFile psiFile1 = findFile(vFile);
if (psiFile1 == null) {
iterator.remove();
continue;
}
if (!areViewProvidersEquivalent(view, psiFile1.getViewProvider())) {
iterator.remove();
} else {
clearPsiCaches(view);
}
}
}
myVFileToViewProviderMap.clear();
myVFileToViewProviderMap.putAll(fileToPsiFileMap);
markInvalidations(originalFileToPsiFileMap);
}
use of gnu.trove.THashMap in project intellij-community by JetBrains.
the class MethodParameterPanel method resetImpl.
protected void resetImpl() {
setPsiClass(myOrigInjection.getClassName());
rebuildTreeModel();
final THashMap<String, MethodParameterInjection.MethodInfo> map = new THashMap<>();
for (PsiMethod method : myData.keySet()) {
final MethodParameterInjection.MethodInfo methodInfo = myData.get(method);
map.put(methodInfo.getMethodSignature(), methodInfo);
}
for (MethodParameterInjection.MethodInfo info : myOrigInjection.getMethodInfos()) {
final MethodParameterInjection.MethodInfo curInfo = map.get(info.getMethodSignature());
if (curInfo != null) {
System.arraycopy(info.getParamFlags(), 0, curInfo.getParamFlags(), 0, Math.min(info.getParamFlags().length, curInfo.getParamFlags().length));
curInfo.setReturnFlag(info.isReturnFlag());
} else {
final PsiMethod missingMethod = MethodParameterInjection.makeMethod(myProject, info.getMethodSignature());
myData.put(missingMethod, info.copy());
}
}
refreshTreeStructure();
final Enumeration enumeration = myRootNode.children();
while (enumeration.hasMoreElements()) {
PsiMethod method = (PsiMethod) ((DefaultMutableTreeNode) enumeration.nextElement()).getUserObject();
assert myData.containsKey(method);
}
}
use of gnu.trove.THashMap in project intellij-community by JetBrains.
the class ShuffleNamesAction method shuffleIds.
private static boolean shuffleIds(PsiFile file, Editor editor) {
final Map<String, String> map = new THashMap<>();
final StringBuilder sb = new StringBuilder();
final StringBuilder quote = new StringBuilder();
final ArrayList<String> split = new ArrayList<>(100);
file.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof LeafPsiElement) {
String type = ((LeafPsiElement) element).getElementType().toString();
String text = element.getText();
if (text.isEmpty())
return;
for (int i = 0, len = text.length(); i < len / 2; i++) {
char c = text.charAt(i);
if (c == text.charAt(len - i - 1) && !Character.isLetter(c)) {
quote.append(c);
} else
break;
}
boolean isQuoted = quote.length() > 0;
boolean isNumber = false;
if (isQuoted || type.equals("ID") || type.contains("IDENT") && !"ts".equals(text) || (isNumber = text.matches("[0-9]+"))) {
String replacement = map.get(text);
if (replacement == null) {
split.addAll(Arrays.asList((isQuoted ? text.substring(quote.length(), text.length() - quote.length()).replace("''", "") : text).split("")));
if (!isNumber) {
for (ListIterator<String> it = split.listIterator(); it.hasNext(); ) {
String s = it.next();
if (s.isEmpty()) {
it.remove();
continue;
}
int c = s.charAt(0);
int cap = c & 32;
c &= ~cap;
c = (char) ((c >= 'A') && (c <= 'Z') ? ((c - 'A' + 7) % 26 + 'A') : c) | cap;
it.set(String.valueOf((char) c));
}
}
Collections.shuffle(split);
if (isNumber && "0".equals(split.get(0))) {
split.set(0, "1");
}
replacement = StringUtil.join(split, "");
if (isQuoted) {
replacement = quote + replacement + quote.reverse();
}
map.put(text, replacement);
}
text = replacement;
}
sb.append(text);
quote.setLength(0);
split.clear();
}
super.visitElement(element);
}
});
editor.getDocument().setText(sb.toString());
return true;
}
Aggregations