@Nullable private static ElementFilter getClassReferenceFilter(PsiElement element) { //throw new foo if (AFTER_THROW_NEW.accepts(element)) { return THROWABLES_FILTER; } //throws list PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class); if (method != null && PsiTreeUtil.isAncestor(method.getThrowsList(), element, true)) { return THROWABLES_FILTER; } //new xxx.yyy if (psiElement().afterLeaf(psiElement().withText(".")).withSuperParent(2, psiElement(PsiNewExpression.class)).accepts(element)) { if (((PsiNewExpression)element.getParent().getParent()).getClassReference() == element.getParent()) { PsiType[] types = ExpectedTypesGetter.getExpectedTypes(element, false); return new OrFilter(ContainerUtil.map2Array(types, ElementFilter.class, new Function<PsiType, ElementFilter>() { @Override public ElementFilter fun(PsiType type) { return new AssignableFromFilter(type); } })); } } return null; }
@Nullable private static ElementFilter getClassReferenceFilter(final PsiElement element, final boolean inRefList) { //throw new foo if(AFTER_THROW_NEW.accepts(element)) { return THROWABLES_FILTER; } //new xxx.yyy if(psiElement().afterLeaf(psiElement().withText(".")).withSuperParent(2, psiElement(PsiNewExpression.class)).accepts(element)) { if(((PsiNewExpression) element.getParent().getParent()).getClassReference() == element.getParent()) { PsiType[] types = ExpectedTypesGetter.getExpectedTypes(element, false); return new OrFilter(ContainerUtil.map2Array(types, ElementFilter.class, (Function<PsiType, ElementFilter>) type -> new AssignableFromFilter(type))); } } // extends/implements/throws if(inRefList) { return new ElementExtractorFilter(new ElementFilter() { @Override public boolean isAcceptable(Object aClass, @Nullable PsiElement context) { return aClass instanceof PsiClass && ReferenceListWeigher.INSTANCE.getApplicability((PsiClass) aClass, element) != ReferenceListWeigher.ReferenceListApplicability.inapplicable; } @Override public boolean isClassAcceptable(Class hintClass) { return true; } }); } return null; }
private static Set<LookupElement> completeFinalReference(final PsiElement element, PsiJavaCodeReferenceElement reference, ElementFilter filter, final JavaSmartCompletionParameters parameters) { final Set<PsiField> used = parameters.getParameters().getInvocationCount() < 2 ? findConstantsUsedInSwitch(element) : Collections.<PsiField>emptySet(); final Set<LookupElement> elements = JavaSmartCompletionContributor.completeReference(element, reference, new AndFilter(filter, new ElementFilter() { @Override public boolean isAcceptable(Object o, PsiElement context) { if (o instanceof CandidateInfo) { final CandidateInfo info = (CandidateInfo)o; final PsiElement member = info.getElement(); final PsiType expectedType = parameters.getExpectedType(); if (expectedType.equals(PsiType.VOID)) { return member instanceof PsiMethod; } //noinspection SuspiciousMethodCalls if (member instanceof PsiEnumConstant && used.contains(CompletionUtil.getOriginalOrSelf(member))) { return false; } return AssignableFromFilter.isAcceptable(member, element, expectedType, info.getSubstitutor()); } return false; } @Override public boolean isClassAcceptable(Class hintClass) { return true; } }), false, true, parameters.getParameters(), PrefixMatcher.ALWAYS_TRUE); for (LookupElement lookupElement : elements) { if (lookupElement.getObject() instanceof PsiMethod) { final JavaMethodCallElement item = lookupElement.as(JavaMethodCallElement.CLASS_CONDITION_KEY); if (item != null) { final PsiMethod method = (PsiMethod)lookupElement.getObject(); if (SmartCompletionDecorator.hasUnboundTypeParams(method, parameters.getExpectedType())) { item.setInferenceSubstitutor(SmartCompletionDecorator.calculateMethodReturnTypeSubstitutor(method, parameters.getExpectedType()), element); } } } } return elements; }
private static Set<LookupElement> completeFinalReference(final PsiElement element, PsiJavaCodeReferenceElement reference, ElementFilter filter, final JavaSmartCompletionParameters parameters) { final Set<PsiField> used = parameters.getParameters().getInvocationCount() < 2 ? findConstantsUsedInSwitch(element) : Collections.<PsiField>emptySet(); final Set<LookupElement> elements = JavaSmartCompletionContributor.completeReference(element, reference, new AndFilter(filter, new ElementFilter() { @Override public boolean isAcceptable(Object o, PsiElement context) { if(o instanceof CandidateInfo) { final CandidateInfo info = (CandidateInfo) o; final PsiElement member = info.getElement(); final PsiType expectedType = parameters.getExpectedType(); if(expectedType.equals(PsiType.VOID)) { return member instanceof PsiMethod; } //noinspection SuspiciousMethodCalls if(member instanceof PsiEnumConstant && used.contains(CompletionUtil.getOriginalOrSelf(member))) { return false; } return AssignableFromFilter.isAcceptable(member, element, expectedType, info.getSubstitutor()); } return false; } @Override public boolean isClassAcceptable(Class hintClass) { return true; } }), false, true, parameters.getParameters(), PrefixMatcher.ALWAYS_TRUE); for(LookupElement lookupElement : elements) { if(lookupElement.getObject() instanceof PsiMethod) { final JavaMethodCallElement item = lookupElement.as(JavaMethodCallElement.CLASS_CONDITION_KEY); if(item != null) { final PsiMethod method = (PsiMethod) lookupElement.getObject(); if(SmartCompletionDecorator.hasUnboundTypeParams(method, parameters.getExpectedType())) { item.setInferenceSubstitutor(SmartCompletionDecorator.calculateMethodReturnTypeSubstitutor(method, parameters.getExpectedType()), element); } } } } return elements; }