public String calcGenerics(@NotNull PsiElement context, InsertionContext insertionContext) { if (insertionContext.getCompletionChar() == '<') { return ""; } assert context.isValid(); if (myDiamond) { return "<>"; } if (getObject() instanceof PsiClass) { PsiClass psiClass = (PsiClass) getObject(); PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(psiClass.getProject()).getResolveHelper(); PsiSubstitutor substitutor = getSubstitutor(); StringBuilder builder = new StringBuilder(); for (PsiTypeParameter parameter : psiClass.getTypeParameters()) { PsiType substitute = substitutor.substitute(parameter); if (substitute == null || (PsiUtil.resolveClassInType(substitute) == parameter && resolveHelper.resolveReferencedClass(parameter.getName(), context) != CompletionUtil.getOriginalOrSelf(parameter))) { return ""; } if (builder.length() > 0) { builder.append(", "); } builder.append(substitute.getCanonicalText()); } if (builder.length() > 0) { return "<" + builder + ">"; } } return ""; }
private static boolean shouldMarkRed(@NotNull Object object, @NotNull PsiElement place) { if (!(object instanceof PsiMember)) return false; if (Java15APIUsageInspectionBase.isForbiddenApiUsage( (PsiMember) object, PsiUtil.getLanguageLevel(place))) return true; if (object instanceof PsiEnumConstant) { return findConstantsUsedInSwitch(place) .contains(CompletionUtil.getOriginalOrSelf((PsiEnumConstant) object)); } return false; }
private static boolean shouldMarkRed(@NotNull Object object, @NotNull PsiElement place) { if (!(object instanceof PsiMember)) return false; if (Java15APIUsageInspectionBase.isForbiddenApiUsage( (PsiMember) object, PsiUtil.getLanguageLevel(place))) return true; if (object instanceof PsiEnumConstant) { return findConstantsUsedInSwitch(place) .contains(CompletionUtil.getOriginalOrSelf((PsiEnumConstant) object)); } if (object instanceof PsiClass && ReferenceListWeigher.INSTANCE.getApplicability((PsiClass) object, place) == inapplicable) { return true; } return false; }
private boolean shouldQualify(PsiField field, InsertionContext context) { if (myHelper != null && !myHelper.willBeImported()) { return true; } if (getAttribute(FORCE_QUALIFY) != null) { return true; } PsiReference reference = context.getFile().findReferenceAt(context.getStartOffset()); if (reference instanceof PsiReferenceExpression && !((PsiReferenceExpression) reference).isQualified()) { final PsiVariable target = JavaPsiFacade.getInstance(context.getProject()) .getResolveHelper() .resolveReferencedVariable(field.getName(), (PsiElement) reference); return !field .getManager() .areElementsEquivalent(target, CompletionUtil.getOriginalOrSelf(field)); } return false; }
@NotNull public static Set<PsiField> findConstantsUsedInSwitch(@Nullable PsiElement position) { if (IN_SWITCH_LABEL.accepts(position)) { Set<PsiField> used = ContainerUtil.newLinkedHashSet(); PsiSwitchStatement sw = PsiTreeUtil.getParentOfType(position, PsiSwitchStatement.class); assert sw != null; final PsiCodeBlock body = sw.getBody(); assert body != null; for (PsiStatement statement : body.getStatements()) { if (statement instanceof PsiSwitchLabelStatement) { final PsiExpression value = ((PsiSwitchLabelStatement) statement).getCaseValue(); if (value instanceof PsiReferenceExpression) { final PsiElement target = ((PsiReferenceExpression) value).resolve(); if (target instanceof PsiField) { used.add(CompletionUtil.getOriginalOrSelf((PsiField) target)); } } } } return used; } return Collections.emptySet(); }
public static Set<LookupElement> processJavaReference( PsiElement element, PsiJavaReference javaReference, ElementFilter elementFilter, JavaCompletionProcessor.Options options, final PrefixMatcher matcher, CompletionParameters parameters) { final Set<LookupElement> set = new LinkedHashSet<LookupElement>(); final Condition<String> nameCondition = new Condition<String>() { @Override public boolean value(String s) { return matcher.prefixMatches(s); } }; PsiMethodCallExpression call = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class); boolean checkInitialized = parameters.getInvocationCount() <= 1 && call != null && PsiKeyword.SUPER.equals(call.getMethodExpression().getText()); final JavaCompletionProcessor processor = new JavaCompletionProcessor( element, elementFilter, options.withInitialized(checkInitialized), nameCondition); final PsiType plainQualifier = processor.getQualifierType(); PsiType qualifierType = plainQualifier; PsiType runtimeQualifier = getQualifierCastType(javaReference, parameters); if (runtimeQualifier != null) { PsiType composite = qualifierType == null ? runtimeQualifier : PsiIntersectionType.createIntersection(qualifierType, runtimeQualifier); PsiElement ctx = createContextWithXxxVariable(element, composite); javaReference = (PsiReferenceExpression) JavaPsiFacade.getElementFactory(element.getProject()) .createExpressionFromText("xxx.xxx", ctx); qualifierType = runtimeQualifier; processor.setQualifierType(qualifierType); } javaReference.processVariants(processor); final PsiTypeLookupItem castItem = runtimeQualifier == null ? null : PsiTypeLookupItem.createLookupItem( runtimeQualifier, (PsiReferenceExpression) javaReference); final boolean pkgContext = inSomePackage(element); final Set<PsiMember> mentioned = new THashSet<PsiMember>(); for (CompletionElement completionElement : processor.getResults()) { for (LookupElement item : createLookupElements(completionElement, javaReference)) { item.putUserData(QUALIFIER_TYPE_ATTR, qualifierType); final Object o = item.getObject(); if (o instanceof PsiClass && !isSourceLevelAccessible(element, (PsiClass) o, pkgContext)) { continue; } if (o instanceof PsiMember) { if (isInExcludedPackage((PsiMember) o, true)) { continue; } mentioned.add(CompletionUtil.getOriginalOrSelf((PsiMember) o)); } set.add( highlightIfNeeded( qualifierType, castQualifier(item, castItem, plainQualifier, processor), o, element)); } } if (javaReference instanceof PsiJavaCodeReferenceElement && !((PsiJavaCodeReferenceElement) javaReference).isQualified()) { final StaticMemberProcessor memberProcessor = new JavaStaticMemberProcessor(parameters); memberProcessor.processMembersOfRegisteredClasses( matcher, new PairConsumer<PsiMember, PsiClass>() { @Override public void consume(PsiMember member, PsiClass psiClass) { if (!mentioned.contains(member) && processor.satisfies(member, ResolveState.initial())) { set.add(memberProcessor.createLookupElement(member, psiClass, true)); } } }); } return set; }