use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class OverrideImplementExploreUtil method getMapToOverrideImplement.
@NotNull
public static Map<MethodSignature, CandidateInfo> getMapToOverrideImplement(PsiClass aClass, boolean toImplement, boolean skipImplemented) {
Map<MethodSignature, PsiMethod> abstracts = new LinkedHashMap<>();
Map<MethodSignature, PsiMethod> finals = new LinkedHashMap<>();
Map<MethodSignature, PsiMethod> concretes = new LinkedHashMap<>();
PsiUtilCore.ensureValid(aClass);
Collection<HierarchicalMethodSignature> allMethodSigs = aClass.getVisibleSignatures();
PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(aClass.getProject()).getResolveHelper();
for (HierarchicalMethodSignature signature : allMethodSigs) {
PsiMethod method = signature.getMethod();
PsiUtilCore.ensureValid(method);
if (method.hasModifierProperty(PsiModifier.STATIC) || !resolveHelper.isAccessible(method, aClass, aClass))
continue;
for (HierarchicalMethodSignature superMethodSignature : signature.getSuperSignatures()) {
final PsiMethod superMethod = superMethodSignature.getMethod();
if (PsiUtil.getAccessLevel(superMethod.getModifierList()) > PsiUtil.getAccessLevel(method.getModifierList())) {
method = superMethod;
break;
}
}
PsiClass hisClass = method.getContainingClass();
if (hisClass == null)
continue;
// filter non-immediate super constructors
if (method.isConstructor() && (!aClass.isInheritor(hisClass, false) || aClass instanceof PsiAnonymousClass || aClass.isEnum())) {
continue;
}
// filter already implemented
if (skipImplemented && MethodSignatureUtil.findMethodBySignature(aClass, signature, false) != null) {
continue;
}
if (method.hasModifierProperty(PsiModifier.FINAL)) {
finals.put(signature, method);
continue;
}
Map<MethodSignature, PsiMethod> map = hisClass.isInterface() || method.hasModifierProperty(PsiModifier.ABSTRACT) ? abstracts : concretes;
fillMap(signature, method, map);
if (isDefaultMethod(aClass, method)) {
fillMap(signature, method, concretes);
}
}
final Map<MethodSignature, CandidateInfo> result = new TreeMap<>(new MethodSignatureComparator());
if (toImplement || aClass.isInterface()) {
collectMethodsToImplement(aClass, abstracts, finals, concretes, result);
} else {
for (Map.Entry<MethodSignature, PsiMethod> entry : concretes.entrySet()) {
MethodSignature signature = entry.getKey();
PsiMethod concrete = entry.getValue();
if (finals.get(signature) == null) {
PsiMethod abstractOne = abstracts.get(signature);
if (abstractOne == null || !abstractOne.getContainingClass().isInheritor(concrete.getContainingClass(), true) || CommonClassNames.JAVA_LANG_OBJECT.equals(concrete.getContainingClass().getQualifiedName())) {
PsiSubstitutor subst = correctSubstitutor(concrete, signature.getSubstitutor());
CandidateInfo info = new CandidateInfo(concrete, subst);
result.put(signature, info);
}
}
}
}
return result;
}
use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class ChangeParameterClassFix method invoke.
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
final PsiClass myClass = (PsiClass) startElement;
if (!FileModificationService.getInstance().prepareFileForWrite(file))
return;
ApplicationManager.getApplication().runWriteAction(() -> invokeImpl(myClass));
final Editor editor1 = CodeInsightUtil.positionCursorAtLBrace(project, myClass.getContainingFile(), myClass);
if (editor1 == null)
return;
final Collection<CandidateInfo> toImplement = OverrideImplementExploreUtil.getMethodsToOverrideImplement(myClass, true);
if (!toImplement.isEmpty()) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
ApplicationManager.getApplication().runWriteAction(() -> {
Collection<PsiMethodMember> members = ContainerUtil.map2List(toImplement, s -> new PsiMethodMember(s));
OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(editor1, myClass, members, false);
});
} else {
//SCR 12599
editor1.getCaretModel().moveToOffset(myClass.getTextRange().getStartOffset());
OverrideImplementUtil.chooseAndImplementMethods(project, editor1, myClass);
}
}
UndoUtil.markPsiFileForUndo(file);
}
use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class GrClassImplUtil method getInnerClassesForResolve.
@NotNull
private static List<PsiClass> getInnerClassesForResolve(@NotNull final GrTypeDefinition grType, @Nullable final PsiElement lastParent, @NotNull final PsiElement place) {
if (lastParent instanceof GrReferenceList || PsiTreeUtil.getParentOfType(place, GrReferenceList.class) != null) {
return Arrays.asList(grType.getCodeInnerClasses());
}
boolean includeSynthetic = !PsiTreeUtil.isContextAncestor(grType, place, true);
Object key = Trinity.create(grType, lastParent, place);
List<PsiClass> classes = RecursionManager.doPreventingRecursion(key, false, () -> {
List<PsiClass> result = new ArrayList<>();
for (CandidateInfo info : CollectClassMembersUtil.getAllInnerClasses(grType, includeSynthetic).values()) {
final PsiClass inner = (PsiClass) info.getElement();
final PsiClass containingClass = inner.getContainingClass();
assert containingClass != null;
if (lastParent == null || !containingClass.isInterface() || PsiTreeUtil.isAncestor(containingClass, place, false)) {
ContainerUtil.addIfNotNull(result, inner);
}
}
return result;
});
if (classes == null) {
return Arrays.asList(grType.getCodeInnerClasses());
}
return classes;
}
use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class GrClassImplUtil method findInnerClassByName.
@Nullable
public static PsiClass findInnerClassByName(GrTypeDefinition grType, String name, boolean checkBases) {
if (!checkBases) {
for (PsiClass inner : grType.getInnerClasses()) {
if (name.equals(inner.getName()))
return inner;
}
return null;
} else {
Map<String, CandidateInfo> innerClasses = CollectClassMembersUtil.getAllInnerClasses(grType, true);
final CandidateInfo info = innerClasses.get(name);
return info == null ? null : (PsiClass) info.getElement();
}
}
use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class CollectClassMembersUtil method addMethod.
private static void addMethod(@NotNull Map<String, List<CandidateInfo>> allMethods, @NotNull PsiMethod method, @NotNull PsiSubstitutor substitutor) {
String name = method.getName();
List<CandidateInfo> methods = allMethods.get(name);
if (methods == null) {
methods = new ArrayList<>();
allMethods.put(name, methods);
}
methods.add(new CandidateInfo(method, substitutor));
}
Aggregations