Java Code Examples for com.intellij.psi.search.searches.ReferencesSearch#search()

The following examples show how to use com.intellij.psi.search.searches.ReferencesSearch#search() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileMoveHandler.java    From netbeans-mmd-plugin with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public List<UsageInfo> findUsages(final PsiFile psiFile, final PsiDirectory newParent, final boolean searchInComments, final boolean searchInNonJavaFiles) {
  Query<PsiReference> search = ReferencesSearch.search(psiFile);
  final List<PsiExtraFileReference> extraFileRefs = new ArrayList<PsiExtraFileReference>();
  search.forEach(new Processor<PsiReference>() {
    @Override
    public boolean process(PsiReference psiReference) {
      if (psiReference instanceof PsiExtraFileReference) {
        extraFileRefs.add((PsiExtraFileReference) psiReference);
      }
      return true;
    }
  });

  if (extraFileRefs.isEmpty()) {
    return null;
  } else {
    final List<UsageInfo> result = new ArrayList<UsageInfo>(extraFileRefs.size());
    for (final PsiExtraFileReference e : extraFileRefs) {
      result.add(new FileUsageInfo(e));
    }
    return result;
  }
}
 
Example 2
Source File: ChangeNamespaceProcessor.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
@RequiredReadAction
protected UsageInfo[] findUsages()
{
	List<UsageInfo> result = new ArrayList<>();
	Set<Couple<DotNetNamedElement>> children = CSharpMoveClassesUtil.findTypesAndNamespaces(myDeclaration);

	for(Couple<DotNetNamedElement> couple : children)
	{
		DotNetNamedElement second = couple.getSecond();

		for(PsiReference reference : ReferencesSearch.search(second, CSharpClassesMoveProcessor.mapScope(second)))
		{
			result.add(new CSharpClassesMoveProcessor.MyUsageInfo(reference.getElement(), couple, reference));
		}
	}

	return result.toArray(new UsageInfo[result.size()]);
}
 
Example 3
Source File: MoveFilesOrDirectoriesProcessor.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
protected UsageInfo[] findUsages() {
  ArrayList<UsageInfo> result = new ArrayList<>();
  for (int i = 0; i < myElementsToMove.length; i++) {
    PsiElement element = myElementsToMove[i];
    if (mySearchForReferences) {
      for (PsiReference reference : ReferencesSearch.search(element, GlobalSearchScope.projectScope(myProject))) {
        result.add(new MyUsageInfo(reference.getElement(), i, reference));
      }
    }
    findElementUsages(result, element);
  }

  return result.toArray(new UsageInfo[result.size()]);
}
 
Example 4
Source File: MicroProfileRestClientDiagnosticsParticipant.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
private static void validateInterfaceType(PsiClass interfaceType, List<Diagnostic> diagnostics,
		JavaDiagnosticsContext context) {
	boolean hasRegisterRestClient = AnnotationUtils.hasAnnotation(interfaceType, REGISTER_REST_CLIENT_ANNOTATION);
	if (hasRegisterRestClient) {
		return;
	}

	final AtomicInteger nbReferences = new AtomicInteger(0);
	Query<PsiReference> query = ReferencesSearch.search(interfaceType, createSearchScope(context.getJavaProject()));
	query.forEach(match -> {
		PsiElement o = PsiTreeUtil.getParentOfType(match.getElement(), PsiField.class);
		if (o instanceof PsiField) {
			PsiField field = (PsiField) o;
			boolean hasInjectAnnotation = AnnotationUtils.hasAnnotation(field, INJECT_ANNOTATION);
			boolean hasRestClientAnnotation = AnnotationUtils.hasAnnotation(field, REST_CLIENT_ANNOTATION);
			if (hasInjectAnnotation && hasRestClientAnnotation) {
				nbReferences.incrementAndGet();
			}
		}
	});

	if (nbReferences.get() > 0) {
		String uri = context.getUri();
		Range restInterfaceRange = PositionUtils.toNameRange(interfaceType, context.getUtils());
		Diagnostic d = context.createDiagnostic(uri,
				"The interface `" + interfaceType.getName()
						+ "` does not have the @RegisterRestClient annotation. The " + nbReferences.get()
						+ " fields references will not be injected as CDI beans.",
				restInterfaceRange, MicroProfileRestClientConstants.DIAGNOSTIC_SOURCE,
				MicroProfileRestClientErrorCode.RegisterRestClientAnnotationMissing);
		diagnostics.add(d);
	}
}
 
Example 5
Source File: BashFileRenameProcessor.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
/**
 * Returns references to the given element. If it is a BashPsiElement a special search scope is used to locate the elements referencing the file.
 *
 * @param element References to the given element
 * @return
 */
@NotNull
@Override
public Collection<PsiReference> findReferences(PsiElement element) {
    //fixme fix the custom scope
    SearchScope scope = (element instanceof BashPsiElement)
            ? BashElementSharedImpl.getElementUseScope((BashPsiElement) element, element.getProject())
            : GlobalSearchScope.projectScope(element.getProject());

    Query<PsiReference> search = ReferencesSearch.search(element, scope);
    return search.findAll();
}
 
Example 6
Source File: DeletionMarkingVisitor.java    From JHelper with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void removeIfNoReference(OCElement element) {
	for (PsiReference reference : ReferencesSearch.search(element, searchScope)) {
		PsiElement referenceElement = reference.getElement();
		if (!isParentFor(element, referenceElement)) {
			return;
		}
	}
	toDelete.add(element);
}
 
Example 7
Source File: CS0169.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Nullable
@Override
public HighlightInfoFactory checkImpl(@Nonnull CSharpLanguageVersion languageVersion, @Nonnull CSharpHighlightContext highlightContext, @Nonnull CSharpFieldDeclaration element)
{
	if(!element.hasModifier(CSharpModifier.PRIVATE))
	{
		return null;
	}

	PsiElement parent = element.getParent();
	if(!(parent instanceof CSharpTypeDeclaration))
	{
		return null;
	}

	PsiElement nameIdentifier = element.getNameIdentifier();
	if(nameIdentifier == null)
	{
		return null;
	}

	Query<PsiReference> search = ReferencesSearch.search(element, CSharpCompositeTypeDeclaration.createLocalScope((DotNetTypeDeclaration) parent));
	if(search.findFirst() == null)
	{
		return newBuilder(nameIdentifier, formatElement(element));
	}

	return null;
}
 
Example 8
Source File: CSharpClassesMoveProcessor.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
@RequiredReadAction
protected UsageInfo[] findUsages()
{
	List<UsageInfo> result = new ArrayList<>();
	List<Couple<DotNetNamedElement>> children = new ArrayList<>();
	for(PsiElement psiElement : myElementsToMove)
	{
		if(psiElement instanceof CSharpFile)
		{
			children.addAll(CSharpMoveClassesUtil.findTypesAndNamespaces((CSharpFile) psiElement));
		}
	}

	for(Couple<DotNetNamedElement> couple : children)
	{
		DotNetNamedElement second = couple.getSecond();

		for(PsiReference reference : ReferencesSearch.search(second, mapScope(second)))
		{
			result.add(new MyUsageInfo(reference.getElement(), couple, reference));
		}
	}

	return result.toArray(new UsageInfo[result.size()]);
}
 
Example 9
Source File: PullUpProcessor.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@Override
@NotNull
protected UsageInfo[] findUsages() {
  final List<UsageInfo> result = new ArrayList<UsageInfo>();
  for (MemberInfo memberInfo : myMembersToMove) {
    final PsiMember member = memberInfo.getMember();
    if (member.hasModifierProperty(PsiModifier.STATIC)) {
      for (PsiReference reference : ReferencesSearch.search(member)) {
        result.add(new UsageInfo(reference));
      }
    }
  }
  return result.isEmpty() ? UsageInfo.EMPTY_ARRAY : result.toArray(new UsageInfo[result.size()]);
}
 
Example 10
Source File: HaxePullUpHelper.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
private boolean willBeUsedInSubclass(PsiElement member, PsiClass superclass, PsiClass subclass) {
  for (PsiReference ref : ReferencesSearch.search(member, new LocalSearchScope(subclass), false)) {
    PsiElement element = ref.getElement();
    if (!RefactoringHierarchyUtil.willBeInTargetClass(element, myMembersToMove, superclass, false)) {
      return true;
    }
  }
  return false;
}
 
Example 11
Source File: InplaceRefactoring.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected Collection<PsiReference> collectRefs(SearchScope referencesSearchScope) {
  final Query<PsiReference> search = ReferencesSearch.search(myElementToRename, referencesSearchScope, false);

  final CommonProcessors.CollectProcessor<PsiReference> processor = new CommonProcessors.CollectProcessor<PsiReference>() {
    @Override
    protected boolean accept(PsiReference reference) {
      return acceptReference(reference);
    }
  };

  search.forEach(processor);
  return processor.getResults();
}
 
Example 12
Source File: CallerChooserBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected Collection<PsiElement> findElementsToHighlight(M caller, PsiElement callee) {
  Query<PsiReference> references = ReferencesSearch.search(callee, new LocalSearchScope(caller), false);
  return ContainerUtil.mapNotNull(references, new Function<PsiReference, PsiElement>() {
    @Override
    public PsiElement fun(PsiReference psiReference) {
      return psiReference.getElement();
    }
  });
}
 
Example 13
Source File: AbstractInplaceIntroducer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void addReferenceAtCaret(Collection<PsiReference> refs) {
  final V variable = getLocalVariable();
  if (variable != null) {
    for (PsiReference reference : ReferencesSearch.search(variable)) {
      refs.add(reference);
    }
  } else {
    refs.clear();
  }
}
 
Example 14
Source File: PushDownConflicts.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
public void checkTargetClassConflicts(final PsiClass targetClass, final boolean checkStatic, final PsiElement context) {
  if (targetClass != null) {
    for (final PsiMember movedMember : myMovedMembers) {
      checkMemberPlacementInTargetClassConflict(targetClass, movedMember);
      movedMember.accept(new JavaRecursiveElementWalkingVisitor() {
        @Override
        public void visitMethodCallExpression(PsiMethodCallExpression expression) {
          super.visitMethodCallExpression(expression);
          if (expression.getMethodExpression().getQualifierExpression() instanceof PsiSuperExpression) {
            final PsiMethod resolvedMethod = expression.resolveMethod();
            if (resolvedMethod != null) {
              final PsiClass resolvedClass = resolvedMethod.getContainingClass();
              if (resolvedClass != null) {
                if (myClass.isInheritor(resolvedClass, true)) {
                  final PsiMethod methodBySignature = myClass.findMethodBySignature(resolvedMethod, false);
                  if (methodBySignature != null && !myMovedMembers.contains(methodBySignature)) {
                    myConflicts.putValue(expression, "Super method call will resolve to another method");
                  }
                }
              }
            }
          }
        }
      });
    }
  }
  Members:
  for (PsiMember member : myMovedMembers) {
    for (PsiReference ref : ReferencesSearch.search(member, member.getResolveScope(), false)) {
      final PsiElement element = ref.getElement();
      if (element instanceof PsiReferenceExpression) {
        final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)element;
        final PsiExpression qualifier = referenceExpression.getQualifierExpression();
        if (qualifier != null) {
          final PsiType qualifierType = qualifier.getType();
          PsiClass aClass = null;
          if (qualifierType instanceof PsiClassType) {
            aClass = ((PsiClassType)qualifierType).resolve();
          }
          else {
            if (!checkStatic) continue;
            if (qualifier instanceof PsiReferenceExpression) {
              final PsiElement resolved = ((PsiReferenceExpression)qualifier).resolve();
              if (resolved instanceof PsiClass) {
                aClass = (PsiClass)resolved;
              }
            }
          }

          if (!InheritanceUtil.isInheritorOrSelf(aClass, targetClass, true)) {
            myConflicts.putValue(referenceExpression, RefactoringBundle.message("pushed.members.will.not.be.visible.from.certain.call.sites"));
            break Members;
          }
        }
      }
    }
  }
  //RefactoringConflictsUtil.analyzeAccessibilityConflicts(myMovedMembers, targetClass, myConflicts, null, context, myAbstractMembers);

}