OperationChain (@NotNull RefChainOperation operation, int weight, @NotNull ChainCompletionContext context) { if (operation instanceof MethodCall signature) { PsiClass qualifier = context.resolvePsiClass(signature.getQualifierDef()); if (qualifier == null || (!signature.isStatic() && InheritanceUtil.isInheritorOrSelf(context.getTarget().getTargetClass(), qualifier, true))) { return null; } PsiMethod[] methods = context.resolve(signature); if (methods.length == 0) return null; Set<PsiClass> classes = Arrays.stream(methods) .flatMap(m -> Arrays.stream(m.getParameterList().getParameters())) .map(p -> PsiUtil.resolveClassInType(p.getType())) .collect(Collectors.toSet()); PsiClass contextClass = context.getTarget().getTargetClass(); if (classes.contains(contextClass)) { return null; } classes.add(contextClass); return new OperationChain(qualifier, new ChainOperation[] {new ChainOperation.MethodCall(methods)}, signature, signature, weight); } else { TypeCast cast = (TypeCast)operation; PsiClass operand = context.resolvePsiClass(cast.getCompilerRef()); PsiClass castType = context.resolvePsiClass(cast.getCastTypeRef()); if (operand == null || castType == null) return null; return new OperationChain(operand, new ChainOperation[] {new ChainOperation.TypeCast(operand, castType)}, cast, null, weight); } }