public void addMethod(@NotNull PsiMethod method, final PsiSubstitutor substitutor, boolean staticProblem) { final boolean isAccessible = JavaResolveUtil.isAccessible(method, getContainingClass(method), method.getModifierList(), myPlace, myAccessClass, myCurrentFileContext, myPlaceFile) && !isShadowed(method); if (isAccepted(method) && !(isInterfaceStaticMethodAccessibleThroughInheritance(method) && ImportsUtil.hasStaticImportOn(myPlace, method, true))) { if (!staticProblem && myAccessClass != null && method.hasModifierProperty(PsiModifier.STATIC)) { final PsiClass containingClass = method.getContainingClass(); if (containingClass != null && containingClass.isInterface() && !(myAccessClass instanceof PsiTypeParameter) && !containingClass.equals(myAccessClass)) { staticProblem = true; } } add(createCandidateInfo(method, substitutor, staticProblem, isAccessible, false)); if (acceptVarargs() && method.isVarArgs() && PsiUtil.isLanguageLevel8OrHigher(myPlace)) { add(createCandidateInfo(method, substitutor, staticProblem, isAccessible, true)); } myHasAccessibleStaticCorrectCandidate |= isAccessible && !staticProblem; } }
@NotNull @Override public ResolveResult[] resolve(@NotNull PsiJavaReference ref, @NotNull PsiFile containingFile, boolean incompleteCode) { PsiJavaCodeReferenceElementImpl referenceElement = (PsiJavaCodeReferenceElementImpl)ref; int kind = referenceElement.getKind(containingFile); JavaResolveResult[] result = referenceElement.resolve(kind, containingFile); if (incompleteCode && result.length == 0 && kind != CLASS_FQ_NAME_KIND && kind != CLASS_FQ_OR_PACKAGE_NAME_KIND) { VariableResolverProcessor processor = new VariableResolverProcessor(referenceElement, containingFile); PsiScopesUtil.resolveAndWalk(processor, referenceElement, null, true); result = processor.getResult(); if (result.length == 0 && kind == CLASS_NAME_KIND) { result = referenceElement.resolve(PACKAGE_NAME_KIND, containingFile); } } JavaResolveUtil.substituteResults(referenceElement, result); return result; }
@Nullable private PsiClass getScope(){ if (getFirstChildNode().getElementType() == JavaDocElementType.DOC_REFERENCE_HOLDER) { final PsiElement firstChildPsi = SourceTreeToPsiMap.treeElementToPsi(getFirstChildNode().getFirstChildNode()); if (firstChildPsi instanceof PsiJavaCodeReferenceElement) { PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement)firstChildPsi; final PsiElement referencedElement = referenceElement.resolve(); if (referencedElement instanceof PsiClass) return (PsiClass)referencedElement; return null; } else if (firstChildPsi instanceof PsiKeyword) { final PsiKeyword keyword = (PsiKeyword)firstChildPsi; if (keyword.getTokenType().equals(THIS_KEYWORD)) { return JavaResolveUtil.getContextClass(this); } else if (keyword.getTokenType().equals(SUPER_KEYWORD)) { final PsiClass contextClass = JavaResolveUtil.getContextClass(this); if (contextClass != null) return contextClass.getSuperClass(); return null; } } } return JavaResolveUtil.getContextClass(this); }
@Override public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) { ElementClassHint classHint = processor.getHint(ElementClassHint.KEY); if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.CLASS)) { final NameHint nameHint = processor.getHint(NameHint.KEY); final String name = nameHint != null ? nameHint.getName(state) : null; //"pseudo-imports" if (name != null) { PsiClass imported = myPseudoImports.get(name); if (imported != null) { if (!processor.execute(imported, state)) return false; } } else { for (PsiClass aClass : myPseudoImports.values()) { if (!processor.execute(aClass, state)) return false; } } if (myContext == null) { if (!JavaResolveUtil.processImplicitlyImportedPackages(processor, state, place, getManager())) return false; } } return true; }
private static void validate(@NotNull PsiElement reference, @Nullable PsiMember member, ProblemsHolder h) { if (member == null || !isAnnotatedAsTestOnly(member)) return; if (isInsideTestOnlyMethod(reference)) return; if (isInsideTestClass(reference)) return; if (isUnderTestSources(reference)) return; PsiAnnotation anno = findVisibleForTestingAnnotation(member); if (anno != null) { String modifier = getAccessModifierWithoutTesting(anno); if (modifier == null) { modifier = member.hasModifierProperty(PsiModifier.PUBLIC) ? PsiModifier.PROTECTED : member.hasModifierProperty(PsiModifier.PROTECTED) ? PsiModifier.PACKAGE_LOCAL : PsiModifier.PRIVATE; } LightModifierList modList = new LightModifierList(member.getManager(), JavaLanguage.INSTANCE, modifier); if (JavaResolveUtil.isAccessible(member, member.getContainingClass(), modList, reference, null, null)) { return; } } reportProblem(reference, member, h); }
@NotNull @Override public JavaResolveResult[] resolve(@NotNull PsiJavaReference ref, boolean incompleteCode) { PsiJavaCodeReferenceElementImpl referenceElement = (PsiJavaCodeReferenceElementImpl)ref; int kind = referenceElement.getKind(); PsiFile containingFile = referenceElement.getContainingFile(); JavaResolveResult[] result = referenceElement.resolve(kind, containingFile); if (incompleteCode && result.length == 0 && kind != CLASS_FQ_NAME_KIND && kind != CLASS_FQ_OR_PACKAGE_NAME_KIND) { VariableResolverProcessor processor = new VariableResolverProcessor(referenceElement, containingFile); PsiScopesUtil.resolveAndWalk(processor, referenceElement, null, incompleteCode); result = processor.getResult(); if (result.length == 0 && kind == CLASS_NAME_KIND) { result = referenceElement.resolve(PACKAGE_NAME_KIND, containingFile); } } JavaResolveUtil.substituteResults((PsiJavaCodeReferenceElement)ref, result); return result; }
@Nullable private PsiClass getScope(){ if (getFirstChildNode().getElementType() == ElementType.DOC_REFERENCE_HOLDER) { final PsiElement firstChildPsi = SourceTreeToPsiMap.treeElementToPsi(getFirstChildNode().getFirstChildNode()); if (firstChildPsi instanceof PsiJavaCodeReferenceElement) { PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement)firstChildPsi; final PsiElement referencedElement = referenceElement.resolve(); if (referencedElement instanceof PsiClass) return (PsiClass)referencedElement; return null; } else if (firstChildPsi instanceof PsiKeyword) { final PsiKeyword keyword = (PsiKeyword)firstChildPsi; if (keyword.getTokenType().equals(THIS_KEYWORD)) { return JavaResolveUtil.getContextClass(this); } else if (keyword.getTokenType().equals(SUPER_KEYWORD)) { final PsiClass contextClass = JavaResolveUtil.getContextClass(this); if (contextClass != null) return contextClass.getSuperClass(); return null; } } } return JavaResolveUtil.getContextClass(this); }
private static void validate(PsiCallExpression e, ProblemsHolder h) { PsiMethod method = e.resolveMethod(); if (method == null || !isAnnotatedAsTestOnly(method)) return; if (isInsideTestOnlyMethod(e)) return; if (isInsideTestClass(e)) return; if (isUnderTestSources(e)) return; PsiAnnotation anno = findVisibleForTestingAnnotation(method); if (anno != null) { LightModifierList modList = new LightModifierList(method.getManager(), JavaLanguage.INSTANCE, getAccessModifierWithoutTesting(anno)); if (JavaResolveUtil.isAccessible(method, method.getContainingClass(), modList, e, null, null)) { return; } } reportProblem(e, h); }
public void addMethod(@NotNull PsiMethod method, final PsiSubstitutor substitutor, boolean staticProblem) { final boolean isAccessible = JavaResolveUtil.isAccessible(method, getContainingClass(method), method.getModifierList(), myPlace, myAccessClass, myCurrentFileContext, myPlaceFile) && !isShadowed(method); if(isAccepted(method) && !(isInterfaceStaticMethodAccessibleThroughInheritance(method) && ImportsUtil.hasStaticImportOn(myPlace, method, true))) { if(!staticProblem && myAccessClass != null && method.hasModifierProperty(PsiModifier.STATIC)) { final PsiClass containingClass = method.getContainingClass(); if(containingClass != null && containingClass.isInterface() && !(myAccessClass instanceof PsiTypeParameter) && !containingClass.equals(myAccessClass)) { staticProblem = true; } } add(createCandidateInfo(method, substitutor, staticProblem, isAccessible, false)); if(acceptVarargs() && method.isVarArgs() && PsiUtil.isLanguageLevel8OrHigher(myPlace)) { add(createCandidateInfo(method, substitutor, staticProblem, isAccessible, true)); } myHasAccessibleStaticCorrectCandidate |= isAccessible && !staticProblem; } }
@NotNull @Override public ResolveResult[] resolve(@NotNull PsiJavaReference ref, @NotNull PsiFile containingFile, boolean incompleteCode) { PsiJavaCodeReferenceElementImpl referenceElement = (PsiJavaCodeReferenceElementImpl) ref; int kind = referenceElement.getKind(containingFile); JavaResolveResult[] result = referenceElement.resolve(kind, containingFile); if(incompleteCode && result.length == 0 && kind != CLASS_FQ_NAME_KIND && kind != CLASS_FQ_OR_PACKAGE_NAME_KIND) { VariableResolverProcessor processor = new VariableResolverProcessor(referenceElement, containingFile); PsiScopesUtil.resolveAndWalk(processor, referenceElement, null, true); result = processor.getResult(); if(result.length == 0 && kind == CLASS_NAME_KIND) { result = referenceElement.resolve(PACKAGE_NAME_KIND, containingFile); } } JavaResolveUtil.substituteResults(referenceElement, result); return result; }
@NotNull @Override public ResolveResult[] resolve(@NotNull PsiJavaReference ref, @NotNull PsiFile containingFile, boolean incompleteCode) { PsiReferenceExpressionImpl expression = (PsiReferenceExpressionImpl) ref; CompositeElement treeParent = expression.getTreeParent(); IElementType parentType = treeParent == null ? null : treeParent.getElementType(); List<ResolveResult[]> qualifiers = resolveAllQualifiers(expression, containingFile); JavaResolveResult[] result = expression.resolve(parentType, containingFile); if(result.length == 0 && incompleteCode && parentType != JavaElementType.REFERENCE_EXPRESSION) { result = expression.resolve(JavaElementType.REFERENCE_EXPRESSION, containingFile); } JavaResolveUtil.substituteResults(expression, result); qualifiers.clear(); // hold qualifier target list until this moment to avoid psi elements inside to GC return result; }
@Nullable private JavaResolveResult resolveOptimised(@NotNull PsiJavaCodeReferenceElement ref) { try { if(ref instanceof PsiReferenceExpressionImpl) { PsiReferenceExpressionImpl.OurGenericsResolver resolver = PsiReferenceExpressionImpl.OurGenericsResolver.INSTANCE; JavaResolveResult[] results = JavaResolveUtil.resolveWithContainingFile(ref, resolver, true, true, myFile); return results.length == 1 ? results[0] : JavaResolveResult.EMPTY; } else { return ref.advancedResolve(true); } } catch(IndexNotReadyException e) { return null; } }
@Nullable private JavaResolveResult[] resolveOptimised(@NotNull PsiReferenceExpression expression) { try { if(expression instanceof PsiReferenceExpressionImpl) { PsiReferenceExpressionImpl.OurGenericsResolver resolver = PsiReferenceExpressionImpl.OurGenericsResolver.INSTANCE; return JavaResolveUtil.resolveWithContainingFile(expression, resolver, true, true, myFile); } else { return expression.multiResolve(true); } } catch(IndexNotReadyException e) { return null; } }
@NotNull private JavaResolveResult resolveOptimised(@NotNull PsiJavaCodeReferenceElement ref) { JavaResolveResult result; if (ref instanceof PsiReferenceExpressionImpl) { JavaResolveResult[] results = JavaResolveUtil.resolveWithContainingFile(ref, PsiReferenceExpressionImpl.OurGenericsResolver.INSTANCE, true, true, myFile); result = results.length == 1 ? results[0] : JavaResolveResult.EMPTY; } else { result = ref.advancedResolve(true); } return result; }
@NotNull private JavaResolveResult[] resolveOptimised(@NotNull PsiReferenceExpression expression) { JavaResolveResult[] results; if (expression instanceof PsiReferenceExpressionImpl) { results = JavaResolveUtil.resolveWithContainingFile(expression, PsiReferenceExpressionImpl.OurGenericsResolver.INSTANCE, true, true, myFile); } else { results = expression.multiResolve(true); } return results; }
private static <T extends PsiElement> Collection<? extends PsiType> suggestFunctionalInterfaces(final @NotNull T element, final NullableFunction<PsiClass, PsiType> acceptanceChecker) { final Project project = element.getProject(); final Set<PsiType> types = new HashSet<PsiType>(); final Processor<PsiMember> consumer = new Processor<PsiMember>() { @Override public boolean process(PsiMember member) { if (member instanceof PsiClass && !Java15APIUsageInspectionBase.isForbiddenApiUsage(member, PsiUtil.getLanguageLevel(element))) { if (!JavaResolveUtil.isAccessible(member, null, member.getModifierList(), element, null, null)) { return true; } ContainerUtil.addIfNotNull(types, acceptanceChecker.fun((PsiClass)member)); } return true; } }; final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final GlobalSearchScope allScope = GlobalSearchScope.allScope(project); final PsiClass functionalInterfaceClass = psiFacade.findClass(CommonClassNames.JAVA_LANG_FUNCTIONAL_INTERFACE, allScope); if (functionalInterfaceClass != null) { AnnotatedMembersSearch.search(functionalInterfaceClass, element.getResolveScope()).forEach(consumer); } for (String functionalInterface : FUNCTIONAL_INTERFACES) { final PsiClass aClass = psiFacade.findClass(functionalInterface, allScope); if (aClass != null) { consumer.process(aClass); } } final ArrayList<PsiType> typesToSuggest = new ArrayList<PsiType>(types); Collections.sort(typesToSuggest, new Comparator<PsiType>() { @Override public int compare(PsiType o1, PsiType o2) { return o1.getCanonicalText().compareTo(o2.getCanonicalText()); } }); return typesToSuggest; }
protected boolean isAccessible(final PsiElement element) { if (element instanceof PsiMember) { final PsiMember member = (PsiMember)element; return JavaResolveUtil.isAccessible(member, member.getContainingClass(), member.getModifierList(), this, null, null); } return true; }
private void updateVisibilityPanel() { if (myTargetClass != null && myTargetClass.isInterface()) { myVPanel.disableAllButPublic(); } else { UIUtil.setEnabled(myVisibilityPanel, true, true); // exclude all modifiers not visible from all occurences final Set<String> visible = new THashSet<String>(); visible.add(PsiModifier.PRIVATE); visible.add(PsiModifier.PROTECTED); visible.add(PsiModifier.PACKAGE_LOCAL); visible.add(PsiModifier.PUBLIC); for (PsiExpression occurrence : myOccurrences) { final PsiManager psiManager = PsiManager.getInstance(myProject); for (Iterator<String> iterator = visible.iterator(); iterator.hasNext();) { String modifier = iterator.next(); try { final String modifierText = PsiModifier.PACKAGE_LOCAL.equals(modifier) ? "" : modifier + " "; final PsiField field = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createFieldFromText(modifierText + "int xxx;", myTargetClass); if (!JavaResolveUtil.isAccessible(field, myTargetClass, field.getModifierList(), occurrence, myTargetClass, null)) { iterator.remove(); } } catch (IncorrectOperationException e) { LOG.error(e); } } } if (!visible.contains(getFieldVisibility())) { if (visible.contains(PsiModifier.PUBLIC)) myVPanel.setVisibility(PsiModifier.PUBLIC); if (visible.contains(PsiModifier.PACKAGE_LOCAL)) myVPanel.setVisibility(PsiModifier.PACKAGE_LOCAL); if (visible.contains(PsiModifier.PROTECTED)) myVPanel.setVisibility(PsiModifier.PROTECTED); if (visible.contains(PsiModifier.PRIVATE)) myVPanel.setVisibility(PsiModifier.PRIVATE); } } }
@Override public void checkConflictsOnUsage(@NotNull MoveMembersProcessor.MoveMembersUsageInfo usageInfo, @Nullable String newVisibility, @Nullable PsiModifierList modifierListCopy, @NotNull PsiClass targetClass, @NotNull Set<PsiMember> membersToMove, @NotNull MultiMap<PsiElement, String> conflicts) { final PsiElement element = usageInfo.getElement(); if (element == null) return; final PsiMember member = usageInfo.member; if (element instanceof GrReferenceExpression) { GrExpression qualifier = ((GrReferenceExpression)element).getQualifier(); PsiClass accessObjectClass = null; if (qualifier != null) { accessObjectClass = (PsiClass)PsiUtil.getAccessObjectClass(qualifier).getElement(); } if (!JavaResolveUtil.isAccessible(member, targetClass, modifierListCopy, element, accessObjectClass, null)) { String visibility = newVisibility != null ? newVisibility : VisibilityUtil.getVisibilityStringToDisplay(member); String message = RefactoringBundle.message("0.with.1.visibility.is.not.accessible.from.2", RefactoringUIUtil.getDescription(member, false), visibility, RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(element), true)); conflicts.putValue(member, CommonRefactoringUtil.capitalize(message)); } } }
private void updateVisibilityPanel() { if (myTargetClass != null && myTargetClass.isInterface()) { myJavaVisibilityPanel.disableAllButPublic(); } else { UIUtil.setEnabled(myJavaVisibilityPanel, true, true); // exclude all modifiers not visible from all occurrences final Set<String> visible = new THashSet<String>(); visible.add(PsiModifier.PRIVATE); visible.add(PsiModifier.PROTECTED); visible.add(PsiModifier.PACKAGE_LOCAL); visible.add(PsiModifier.PUBLIC); for (PsiElement occurrence : myContext.getOccurrences()) { final PsiManager psiManager = PsiManager.getInstance(myContext.getProject()); for (Iterator<String> iterator = visible.iterator(); iterator.hasNext();) { String modifier = iterator.next(); try { final String modifierText = PsiModifier.PACKAGE_LOCAL.equals(modifier) ? "" : modifier + " "; final PsiField field = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createFieldFromText(modifierText + "int xxx;", myTargetClass); if (!JavaResolveUtil.isAccessible(field, myTargetClass, field.getModifierList(), occurrence, myTargetClass, null)) { iterator.remove(); } } catch (IncorrectOperationException e) { LOG.error(e); } } } if (!visible.contains(getVisibilityModifier())) { if (visible.contains(PsiModifier.PUBLIC)) myJavaVisibilityPanel.setVisibility(PsiModifier.PUBLIC); if (visible.contains(PsiModifier.PACKAGE_LOCAL)) myJavaVisibilityPanel.setVisibility(PsiModifier.PACKAGE_LOCAL); if (visible.contains(PsiModifier.PROTECTED)) myJavaVisibilityPanel.setVisibility(PsiModifier.PROTECTED); if (visible.contains(PsiModifier.PRIVATE)) myJavaVisibilityPanel.setVisibility(PsiModifier.PRIVATE); } } }
public void addMethod(final PsiMethod method, final PsiSubstitutor substitutor, final boolean staticProblem) { final boolean isAccessible = JavaResolveUtil.isAccessible(method, method.getContainingClass(), method.getModifierList(), myPlace, myAccessClass, myCurrentFileContext, myPlaceFile) && !isShadowed(method); if (isAccepted(method)) { add(createCandidateInfo(method, substitutor, staticProblem, isAccessible)); myHasAccessibleStaticCorrectCandidate |= isAccessible && !staticProblem; } }
private static List<LookupElement> collectVariantsByReceiver(boolean prioritize, PsiType functionalInterfaceType, PsiParameter[] params, PsiElement originalPosition, PsiSubstitutor substitutor, PsiType expectedReturnType) { List<LookupElement> result = new ArrayList<>(); final PsiType functionalInterfaceParamType = substitutor.substitute(params[0].getType()); final PsiClass paramClass = PsiUtil.resolveClassInClassTypeOnly(functionalInterfaceParamType); if(paramClass != null && !paramClass.hasTypeParameters()) { final Set<String> visited = new HashSet<>(); for(PsiMethod psiMethod : paramClass.getAllMethods()) { PsiClass containingClass = psiMethod.getContainingClass(); PsiClass qualifierClass = containingClass != null ? containingClass : paramClass; if(visited.add(psiMethod.getName()) && !psiMethod.hasModifierProperty(PsiModifier.STATIC) && hasAppropriateReturnType(expectedReturnType, psiMethod) && areParameterTypesAppropriate (psiMethod, params, substitutor, 1) && JavaResolveUtil.isAccessible(psiMethod, null, psiMethod.getModifierList(), originalPosition, null, null)) { LookupElement methodRefLookupElement = createMethodRefOnClass(functionalInterfaceType, psiMethod, qualifierClass); if(prioritize && containingClass == paramClass) { methodRefLookupElement = PrioritizedLookupElement.withExplicitProximity(methodRefLookupElement, 1); } result.add(methodRefLookupElement); } } } return result; }
@Nullable private static HighlightInfo checkReferenceToOurInstanceInsideThisOrSuper(@NotNull final PsiElement expression, @NotNull PsiClass referencedClass, final String resolvedName, @NotNull PsiFile containingFile) { if (PsiTreeUtil.getParentOfType(expression, PsiReferenceParameterList.class) != null) return null; PsiElement element = expression.getParent(); while (element != null) { // check if expression inside super()/this() call if (RefactoringChangeUtil.isSuperOrThisMethodCall(element)) { PsiElement parentClass = new PsiMatcherImpl(element) .parent(PsiMatchers.hasClass(PsiExpressionStatement.class)) .parent(PsiMatchers.hasClass(PsiCodeBlock.class)) .parent(PsiMatchers.hasClass(PsiMethod.class)) .dot(JavaMatchers.isConstructor(true)) .parent(PsiMatchers.hasClass(PsiClass.class)) .getElement(); if (parentClass == null) { return null; } // only this class/superclasses instance methods are not allowed to call PsiClass aClass = (PsiClass)parentClass; if (PsiUtil.isInnerClass(aClass) && referencedClass == aClass.getContainingClass()) return null; // field or method should be declared in this class or super if (!InheritanceUtil.isInheritorOrSelf(aClass, referencedClass, true)) return null; // and point to our instance if (expression instanceof PsiReferenceExpression && !thisOrSuperReference(((PsiReferenceExpression)expression).getQualifierExpression(), aClass)) { return null; } if (expression instanceof PsiJavaCodeReferenceElement && !aClass.equals(PsiTreeUtil.getParentOfType(expression, PsiClass.class)) && PsiTreeUtil.getParentOfType(expression, PsiTypeElement.class) != null) { return null; } if (expression instanceof PsiJavaCodeReferenceElement && PsiTreeUtil.getParentOfType(expression, PsiClassObjectAccessExpression.class) != null) { return null; } final HighlightInfo highlightInfo = createMemberReferencedError(resolvedName, expression.getTextRange()); if (expression instanceof PsiReferenceExpression && PsiUtil.isInnerClass(aClass)) { final String referenceName = ((PsiReferenceExpression)expression).getReferenceName(); final PsiClass containingClass = aClass.getContainingClass(); LOG.assertTrue(containingClass != null); final PsiField fieldInContainingClass = containingClass.findFieldByName(referenceName, true); if (fieldInContainingClass != null && ((PsiReferenceExpression)expression).getQualifierExpression() == null) { QuickFixAction.registerQuickFixAction(highlightInfo, new QualifyWithThisFix(containingClass, expression)); } } return highlightInfo; } if (element instanceof PsiReferenceExpression) { final PsiElement resolve; if (element instanceof PsiReferenceExpressionImpl) { PsiReferenceExpressionImpl referenceExpression = (PsiReferenceExpressionImpl)element; JavaResolveResult[] results = JavaResolveUtil .resolveWithContainingFile(referenceExpression, PsiReferenceExpressionImpl.OurGenericsResolver.INSTANCE, true, false, containingFile); resolve = results.length == 1 ? results[0].getElement() : null; } else { resolve = ((PsiReferenceExpression)element).resolve(); } if (resolve instanceof PsiField && ((PsiField)resolve).hasModifierProperty(PsiModifier.STATIC)) { return null; } } element = element.getParent(); if (element instanceof PsiClass && InheritanceUtil.isInheritorOrSelf((PsiClass)element, referencedClass, true)) return null; } return null; }
private static void collectVariantsByReceiver(boolean prioritize, PsiType functionalInterfaceType, PsiParameter[] params, PsiElement originalPosition, PsiSubstitutor substitutor, PsiType expectedReturnType, List<LookupElement> result) { final PsiType functionalInterfaceParamType = substitutor.substitute(params[0].getType()); final PsiClass paramClass = PsiUtil.resolveClassInClassTypeOnly(functionalInterfaceParamType); if (paramClass != null && !paramClass.hasTypeParameters()) { final Set<String> visited = new HashSet<String>(); for (PsiMethod psiMethod : paramClass.getAllMethods()) { final PsiType returnType = psiMethod.getReturnType(); if (visited.add(psiMethod.getName()) && isInstanceMethodWithAppropriateReturnType(expectedReturnType, psiMethod, returnType) && areParameterTypesAppropriate(psiMethod, params, substitutor, 1) && JavaResolveUtil.isAccessible(psiMethod, null, psiMethod.getModifierList(), originalPosition, null, null)) { LookupElement methodRefLookupElement = LookupElementBuilder .create(psiMethod) .withPresentableText(paramClass.getName() + "::" + psiMethod.getName()) .withInsertHandler(new InsertHandler<LookupElement>() { @Override public void handleInsert(InsertionContext context, LookupElement item) { final int startOffset = context.getStartOffset(); final Document document = context.getDocument(); final PsiFile file = context.getFile(); document.insertString(startOffset, "::"); JavaCompletionUtil.insertClassReference(paramClass, file, startOffset); } }) .withTypeText(functionalInterfaceType.getPresentableText()) .withIcon(AllIcons.Nodes.MethodReference) .withAutoCompletionPolicy(AutoCompletionPolicy.NEVER_AUTOCOMPLETE); if (prioritize && psiMethod.getContainingClass() == paramClass) { methodRefLookupElement = PrioritizedLookupElement.withPriority(methodRefLookupElement, 1); } result.add(methodRefLookupElement); } } } }
private boolean isVisibleFromOverridingMethod(PsiMethod method, PsiMethod overridingMethod) { final PsiModifierList modifierListCopy = (PsiModifierList)method.getModifierList().copy(); modifierListCopy.setModifierProperty(getModifier(), true); return JavaResolveUtil.isAccessible(method, method.getContainingClass(), modifierListCopy, overridingMethod, null, null); }
@Nullable private static HighlightInfo checkReferenceToOurInstanceInsideThisOrSuper(@NotNull final PsiElement expression, @NotNull PsiClass referencedClass, final String resolvedName, @NotNull PsiFile containingFile) { if (PsiTreeUtil.getParentOfType(expression, PsiReferenceParameterList.class) != null) return null; PsiElement element = expression.getParent(); while (element != null) { // check if expression inside super()/this() call if (RefactoringChangeUtil.isSuperOrThisMethodCall(element)) { PsiElement parentClass = new PsiMatcherImpl(element) .parent(PsiMatchers.hasClass(PsiExpressionStatement.class)) .parent(PsiMatchers.hasClass(PsiCodeBlock.class)) .parent(PsiMatchers.hasClass(PsiMethod.class)) .dot(JavaMatchers.isConstructor(true)) .parent(PsiMatchers.hasClass(PsiClass.class)) .getElement(); if (parentClass == null) { return null; } // only this class/superclasses instance methods are not allowed to call PsiClass aClass = (PsiClass)parentClass; if (PsiUtil.isInnerClass(aClass) && referencedClass == aClass.getContainingClass()) return null; // field or method should be declared in this class or super if (!InheritanceUtil.isInheritorOrSelf(aClass, referencedClass, true)) return null; // and point to our instance if (expression instanceof PsiReferenceExpression && !thisOrSuperReference(((PsiReferenceExpression)expression).getQualifierExpression(), aClass)) { return null; } if (expression instanceof PsiJavaCodeReferenceElement && !aClass.equals(PsiTreeUtil.getParentOfType(expression, PsiClass.class)) && PsiTreeUtil.getParentOfType(expression, PsiTypeElement.class) != null) { return null; } final HighlightInfo highlightInfo = createMemberReferencedError(resolvedName, expression.getTextRange()); if (expression instanceof PsiReferenceExpression && PsiUtil.isInnerClass(aClass)) { final String referenceName = ((PsiReferenceExpression)expression).getReferenceName(); final PsiClass containingClass = aClass.getContainingClass(); LOG.assertTrue(containingClass != null); final PsiField fieldInContainingClass = containingClass.findFieldByName(referenceName, true); if (fieldInContainingClass != null && ((PsiReferenceExpression)expression).getQualifierExpression() == null) { QuickFixAction.registerQuickFixAction(highlightInfo, new QualifyWithThisFix(containingClass, expression)); } } return highlightInfo; } if (element instanceof PsiReferenceExpression) { final PsiElement resolve; if (element instanceof PsiReferenceExpressionImpl) { PsiReferenceExpressionImpl referenceExpression = (PsiReferenceExpressionImpl)element; JavaResolveResult[] results = JavaResolveUtil .resolveWithContainingFile(referenceExpression, PsiReferenceExpressionImpl.OurGenericsResolver.INSTANCE, true, false, containingFile); resolve = results.length == 1 ? results[0].getElement() : null; } else { resolve = ((PsiReferenceExpression)element).resolve(); } if (resolve instanceof PsiField && ((PsiField)resolve).hasModifierProperty(PsiModifier.STATIC)) { return null; } } element = element.getParent(); if (element instanceof PsiClass && InheritanceUtil.isInheritorOrSelf((PsiClass)element, referencedClass, true)) return null; } return null; }
@Override public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) { ElementClassHint classHint = processor.getHint(ElementClassHint.KEY); if(classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.CLASS)) { final NameHint nameHint = processor.getHint(NameHint.KEY); final String name = nameHint != null ? nameHint.getName(state) : null; //"pseudo-imports" if(name != null) { PsiClass imported = myPseudoImports.get(name); if(imported != null) { if(!processor.execute(imported, state)) { return false; } } } else { for(PsiClass aClass : myPseudoImports.values()) { if(!processor.execute(aClass, state)) { return false; } } } if(getContext() == null) { if(!JavaResolveUtil.processImplicitlyImportedPackages(processor, state, place, getManager())) { return false; } } } return true; }
private static boolean shouldSuggestConstructor(@NotNull PsiClass psiClass, @NotNull PsiElement position, PsiMethod constructor) { return JavaResolveUtil.isAccessible(constructor, psiClass, constructor.getModifierList(), position, null, null) || willBeAccessibleInAnonymous(psiClass, constructor); }
private static <T extends PsiElement> Collection<? extends PsiType> suggestFunctionalInterfaces(final @NotNull T element, final NullableFunction<PsiClass, PsiType> acceptanceChecker) { final Project project = element.getProject(); final Set<PsiType> types = new HashSet<PsiType>(); final Processor<PsiMember> consumer = new Processor<PsiMember>() { @Override public boolean process(PsiMember member) { if(member instanceof PsiClass && !Java15APIUsageInspectionBase.isForbiddenApiUsage(member, PsiUtil.getLanguageLevel(element))) { if(!JavaResolveUtil.isAccessible(member, null, member.getModifierList(), element, null, null)) { return true; } ContainerUtil.addIfNotNull(types, acceptanceChecker.fun((PsiClass) member)); } return true; } }; final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final GlobalSearchScope allScope = GlobalSearchScope.allScope(project); final PsiClass functionalInterfaceClass = psiFacade.findClass(CommonClassNames.JAVA_LANG_FUNCTIONAL_INTERFACE, allScope); if(functionalInterfaceClass != null) { AnnotatedMembersSearch.search(functionalInterfaceClass, element.getResolveScope()).forEach(consumer); } for(String functionalInterface : FUNCTIONAL_INTERFACES) { final PsiClass aClass = psiFacade.findClass(functionalInterface, allScope); if(aClass != null) { consumer.process(aClass); } } final ArrayList<PsiType> typesToSuggest = new ArrayList<PsiType>(types); Collections.sort(typesToSuggest, new Comparator<PsiType>() { @Override public int compare(PsiType o1, PsiType o2) { return o1.getCanonicalText().compareTo(o2.getCanonicalText()); } }); return typesToSuggest; }