use of com.intellij.psi.util.MethodSignature in project intellij-community by JetBrains.
the class GroovyOverrideImplementExploreUtil method getMapToOverrideImplement.
@NotNull
public static Map<MethodSignature, CandidateInfo> getMapToOverrideImplement(GrTypeDefinition aClass, boolean toImplement, boolean skipImplemented) {
Map<MethodSignature, PsiMethod> abstracts = ContainerUtil.newLinkedHashMap();
Map<MethodSignature, PsiMethod> finals = ContainerUtil.newLinkedHashMap();
Map<MethodSignature, PsiMethod> concretes = ContainerUtil.newLinkedHashMap();
PsiUtilCore.ensureValid(aClass);
Collection<HierarchicalMethodSignature> allMethodSignatures = aClass.getVisibleSignatures();
PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(aClass.getProject()).getResolveHelper();
for (HierarchicalMethodSignature signature : allMethodSignatures) {
PsiMethod method = signature.getMethod();
if (method instanceof GrTraitMethod) {
for (HierarchicalMethodSignature superSignature : signature.getSuperSignatures()) {
processMethod(aClass, skipImplemented, abstracts, finals, concretes, resolveHelper, superSignature, superSignature.getMethod());
}
} else {
processMethod(aClass, skipImplemented, abstracts, finals, concretes, resolveHelper, signature, method);
}
}
final Map<MethodSignature, CandidateInfo> result = new TreeMap<>(new OverrideImplementExploreUtil.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 = OverrideImplementExploreUtil.correctSubstitutor(concrete, signature.getSubstitutor());
CandidateInfo info = new CandidateInfo(concrete, subst);
result.put(signature, info);
}
}
}
}
return result;
}
use of com.intellij.psi.util.MethodSignature in project intellij-community by JetBrains.
the class ConvertClosureToMethodIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrField field;
if (element.getParent() instanceof GrField) {
field = (GrField) element.getParent();
} else {
final PsiReference ref = element.getReference();
LOG.assertTrue(ref != null);
PsiElement resolved = ref.resolve();
if (resolved instanceof GrAccessorMethod) {
resolved = ((GrAccessorMethod) resolved).getProperty();
}
LOG.assertTrue(resolved instanceof GrField);
field = (GrField) resolved;
}
final HashSet<PsiReference> usages = new HashSet<>();
usages.addAll(ReferencesSearch.search(field).findAll());
final GrAccessorMethod[] getters = field.getGetters();
for (GrAccessorMethod getter : getters) {
usages.addAll(MethodReferencesSearch.search(getter).findAll());
}
final GrAccessorMethod setter = field.getSetter();
if (setter != null) {
usages.addAll(MethodReferencesSearch.search(setter).findAll());
}
final String fieldName = field.getName();
LOG.assertTrue(fieldName != null);
final Collection<PsiElement> fieldUsages = new HashSet<>();
MultiMap<PsiElement, String> conflicts = new MultiMap<>();
for (PsiReference usage : usages) {
final PsiElement psiElement = usage.getElement();
if (PsiUtil.isMethodUsage(psiElement))
continue;
if (!GroovyLanguage.INSTANCE.equals(psiElement.getLanguage())) {
conflicts.putValue(psiElement, GroovyIntentionsBundle.message("closure.is.accessed.outside.of.groovy", fieldName));
} else {
if (psiElement instanceof GrReferenceExpression) {
fieldUsages.add(psiElement);
if (PsiUtil.isAccessedForWriting((GrExpression) psiElement)) {
conflicts.putValue(psiElement, GroovyIntentionsBundle.message("write.access.to.closure.variable", fieldName));
}
} else if (psiElement instanceof GrArgumentLabel) {
conflicts.putValue(psiElement, GroovyIntentionsBundle.message("field.is.used.in.argument.label", fieldName));
}
}
}
final PsiClass containingClass = field.getContainingClass();
final GrExpression initializer = field.getInitializerGroovy();
LOG.assertTrue(initializer != null);
final PsiType type = initializer.getType();
LOG.assertTrue(type instanceof GrClosureType);
final GrSignature signature = ((GrClosureType) type).getSignature();
final List<MethodSignature> signatures = GrClosureSignatureUtil.generateAllMethodSignaturesBySignature(fieldName, signature);
for (MethodSignature s : signatures) {
final PsiMethod method = MethodSignatureUtil.findMethodBySignature(containingClass, s, true);
if (method != null) {
conflicts.putValue(method, GroovyIntentionsBundle.message("method.with.signature.already.exists", GroovyPresentationUtil.getSignaturePresentation(s)));
}
}
if (!conflicts.isEmpty()) {
final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts, () -> execute(field, fieldUsages));
conflictsDialog.show();
if (conflictsDialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
return;
}
execute(field, fieldUsages);
}
use of com.intellij.psi.util.MethodSignature in project intellij-community by JetBrains.
the class I18nInspection method isInNonNlsEquals.
private static boolean isInNonNlsEquals(PsiExpression expression, final Set<PsiModifierListOwner> nonNlsTargets) {
if (!(expression.getParent().getParent() instanceof PsiMethodCallExpression)) {
return false;
}
final PsiMethodCallExpression call = (PsiMethodCallExpression) expression.getParent().getParent();
final PsiReferenceExpression methodExpression = call.getMethodExpression();
final PsiExpression qualifier = methodExpression.getQualifierExpression();
if (qualifier != expression) {
return false;
}
if (!"equals".equals(methodExpression.getReferenceName())) {
return false;
}
final PsiElement resolved = methodExpression.resolve();
if (!(resolved instanceof PsiMethod)) {
return false;
}
PsiType objectType = PsiType.getJavaLangObject(resolved.getManager(), resolved.getResolveScope());
MethodSignature equalsSignature = MethodSignatureUtil.createMethodSignature("equals", new PsiType[] { objectType }, PsiTypeParameter.EMPTY_ARRAY, PsiSubstitutor.EMPTY);
if (!equalsSignature.equals(((PsiMethod) resolved).getSignature(PsiSubstitutor.EMPTY))) {
return false;
}
final PsiExpression[] expressions = call.getArgumentList().getExpressions();
if (expressions.length != 1) {
return false;
}
final PsiExpression arg = expressions[0];
PsiReferenceExpression ref = null;
if (arg instanceof PsiReferenceExpression) {
ref = (PsiReferenceExpression) arg;
} else if (arg instanceof PsiMethodCallExpression)
ref = ((PsiMethodCallExpression) arg).getMethodExpression();
if (ref != null) {
final PsiElement resolvedEntity = ref.resolve();
if (resolvedEntity instanceof PsiModifierListOwner) {
PsiModifierListOwner modifierListOwner = (PsiModifierListOwner) resolvedEntity;
if (annotatedAsNonNls(modifierListOwner)) {
return true;
}
nonNlsTargets.add(modifierListOwner);
}
}
return false;
}
use of com.intellij.psi.util.MethodSignature in project intellij-community by JetBrains.
the class JavaOverridingMethodsSearcher method findOverridingMethod.
@Nullable
static PsiMethod findOverridingMethod(@NotNull Project project, @NotNull PsiClass inheritor, @NotNull PsiMethod method, @NotNull PsiClass methodContainingClass) {
String name = method.getName();
if (inheritor.findMethodsByName(name, false).length > 0) {
PsiMethod found = MethodSignatureUtil.findMethodBySuperSignature(inheritor, getSuperSignature(inheritor, methodContainingClass, method), false);
if (found != null && isAcceptable(project, found, inheritor, method, methodContainingClass)) {
return found;
}
}
if (methodContainingClass.isInterface() && !inheritor.isInterface()) {
//check for sibling implementation
final PsiClass superClass = inheritor.getSuperClass();
if (superClass != null && !superClass.isInheritor(methodContainingClass, true) && superClass.findMethodsByName(name, true).length > 0) {
MethodSignature signature = getSuperSignature(inheritor, methodContainingClass, method);
PsiMethod derived = MethodSignatureUtil.findMethodInSuperClassBySignatureInDerived(inheritor, superClass, signature, true);
if (derived != null && isAcceptable(project, derived, inheritor, method, methodContainingClass)) {
return derived;
}
}
}
return null;
}
use of com.intellij.psi.util.MethodSignature in project intellij-community by JetBrains.
the class SuspiciousCollectionsMethodCallsInspection method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
final List<PsiMethod> patternMethods = new ArrayList<>();
final IntArrayList indices = new IntArrayList();
return new JavaElementVisitor() {
@Override
public void visitMethodCallExpression(PsiMethodCallExpression methodCall) {
final String message = getSuspiciousMethodCallMessage(methodCall, REPORT_CONVERTIBLE_METHOD_CALLS, patternMethods, indices);
if (message != null) {
holder.registerProblem(methodCall.getArgumentList().getExpressions()[0], message);
}
}
@Override
public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) {
final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType();
final PsiClassType.ClassResolveResult functionalInterfaceResolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType);
if (interfaceMethod != null && interfaceMethod.getParameterList().getParametersCount() == 1) {
final PsiSubstitutor psiSubstitutor = LambdaUtil.getSubstitutor(interfaceMethod, functionalInterfaceResolveResult);
final MethodSignature signature = interfaceMethod.getSignature(psiSubstitutor);
String message = SuspiciousMethodCallUtil.getSuspiciousMethodCallMessage(expression, signature.getParameterTypes()[0], REPORT_CONVERTIBLE_METHOD_CALLS, patternMethods, indices);
if (message != null) {
holder.registerProblem(ObjectUtils.notNull(expression.getReferenceNameElement(), expression), message);
}
}
}
};
}
Aggregations