use of com.intellij.psi.impl.light.LightElement in project intellij-community by JetBrains.
the class RefactoringConflictsUtil method analyzeModuleConflicts.
public static void analyzeModuleConflicts(final Project project, final Collection<? extends PsiElement> scopes, final UsageInfo[] usages, final VirtualFile vFile, final MultiMap<PsiElement, String> conflicts) {
if (scopes == null)
return;
for (final PsiElement scope : scopes) {
if (scope instanceof PsiPackage)
return;
}
final Module targetModule = ModuleUtilCore.findModuleForFile(vFile, project);
if (targetModule == null)
return;
final GlobalSearchScope resolveScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(targetModule);
final HashSet<PsiElement> reported = new HashSet<>();
for (final PsiElement scope : scopes) {
scope.accept(new JavaRecursiveElementVisitor() {
@Override
public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
super.visitReferenceElement(reference);
final PsiElement resolved = reference.resolve();
if (resolved != null && !reported.contains(resolved) && !CommonRefactoringUtil.isAncestor(resolved, scopes) && !(resolved instanceof LightElement) && !haveElementInScope(resolved)) {
if (resolved instanceof PsiMethod) {
for (PsiMethod superMethod : ((PsiMethod) resolved).findDeepestSuperMethods()) {
if (haveElementInScope(superMethod))
return;
}
}
final String scopeDescription = RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(reference), true);
final String message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.in.module.2", RefactoringUIUtil.getDescription(resolved, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(targetModule.getName()));
conflicts.putValue(resolved, CommonRefactoringUtil.capitalize(message));
reported.add(resolved);
}
}
private boolean haveElementInScope(PsiElement resolved) {
if (PsiSearchScopeUtil.isInScope(resolveScope, resolved)) {
return true;
}
if (!resolved.getManager().isInProject(resolved)) {
if (resolved instanceof PsiMember) {
final PsiClass containingClass = ((PsiMember) resolved).getContainingClass();
if (containingClass != null) {
final String fqn = containingClass.getQualifiedName();
if (fqn != null) {
final PsiClass classFromTarget = JavaPsiFacade.getInstance(project).findClass(fqn, resolveScope);
if (classFromTarget != null) {
if (resolved instanceof PsiMethod) {
return classFromTarget.findMethodsBySignature((PsiMethod) resolved, true).length > 0;
}
if (resolved instanceof PsiField) {
return classFromTarget.findFieldByName(((PsiField) resolved).getName(), false) != null;
}
if (resolved instanceof PsiClass) {
return classFromTarget.findInnerClassByName(((PsiClass) resolved).getName(), false) != null;
}
}
}
}
}
if (resolved instanceof PsiClass) {
final String fqn = ((PsiClass) resolved).getQualifiedName();
return fqn != null && JavaPsiFacade.getInstance(project).findClass(fqn, resolveScope) != null;
}
}
return false;
}
});
}
boolean isInTestSources = ModuleRootManager.getInstance(targetModule).getFileIndex().isInTestSourceContent(vFile);
NextUsage: for (UsageInfo usage : usages) {
final PsiElement element = usage.getElement();
if (element != null && PsiTreeUtil.getParentOfType(element, PsiImportStatement.class, false) == null) {
for (PsiElement scope : scopes) {
if (PsiTreeUtil.isAncestor(scope, element, false))
continue NextUsage;
}
final GlobalSearchScope resolveScope1 = element.getResolveScope();
if (!resolveScope1.isSearchInModuleContent(targetModule, isInTestSources)) {
final PsiFile usageFile = element.getContainingFile();
PsiElement container;
if (usageFile instanceof PsiJavaFile) {
container = ConflictsUtil.getContainer(element);
} else {
container = usageFile;
}
final String scopeDescription = RefactoringUIUtil.getDescription(container, true);
final VirtualFile usageVFile = usageFile.getVirtualFile();
if (usageVFile != null) {
Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(usageVFile);
if (module != null) {
final String message;
final PsiElement referencedElement;
if (usage instanceof MoveRenameUsageInfo) {
referencedElement = ((MoveRenameUsageInfo) usage).getReferencedElement();
} else {
referencedElement = usage.getElement();
}
assert referencedElement != null : usage;
if (module == targetModule && isInTestSources) {
message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.production.of.module.2", RefactoringUIUtil.getDescription(referencedElement, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(module.getName()));
} else {
message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.module.2", RefactoringUIUtil.getDescription(referencedElement, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(module.getName()));
}
conflicts.putValue(referencedElement, CommonRefactoringUtil.capitalize(message));
}
}
}
}
}
}
use of com.intellij.psi.impl.light.LightElement in project intellij-community by JetBrains.
the class RenameProcessor method classifyUsages.
public static MultiMap<PsiElement, UsageInfo> classifyUsages(Collection<? extends PsiElement> elements, UsageInfo[] usages) {
final MultiMap<PsiElement, UsageInfo> result = new MultiMap<>();
for (UsageInfo usage : usages) {
LOG.assertTrue(usage instanceof MoveRenameUsageInfo);
if (usage.getReference() instanceof LightElement) {
//filter out implicit references (e.g. from derived class to super class' default constructor)
continue;
}
MoveRenameUsageInfo usageInfo = (MoveRenameUsageInfo) usage;
if (usage instanceof RelatedUsageInfo) {
final PsiElement relatedElement = ((RelatedUsageInfo) usage).getRelatedElement();
if (elements.contains(relatedElement)) {
result.putValue(relatedElement, usage);
}
} else {
PsiElement referenced = usageInfo.getReferencedElement();
if (elements.contains(referenced)) {
result.putValue(referenced, usage);
} else if (referenced != null) {
PsiElement indirect = referenced.getNavigationElement();
if (elements.contains(indirect)) {
result.putValue(indirect, usage);
}
}
}
}
return result;
}
use of com.intellij.psi.impl.light.LightElement in project intellij-community by JetBrains.
the class GrRefactoringConflictsUtil method analyzeModuleConflicts.
public static void analyzeModuleConflicts(final Project project, final Collection<? extends GroovyPsiElement> scopes, final UsageInfo[] usages, final VirtualFile vFile, final MultiMap<PsiElement, String> conflicts) {
if (scopes == null)
return;
for (final PsiElement scope : scopes) {
if (scope instanceof PsiPackage)
return;
}
final Module targetModule = ModuleUtilCore.findModuleForFile(vFile, project);
if (targetModule == null)
return;
final GlobalSearchScope resolveScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(targetModule);
final HashSet<PsiElement> reported = new HashSet<>();
for (final GroovyPsiElement scope : scopes) {
scope.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
super.visitCodeReferenceElement(refElement);
visit(refElement);
}
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression reference) {
super.visitReferenceExpression(reference);
visit(reference);
}
private void visit(GrReferenceElement<? extends GroovyPsiElement> reference) {
final PsiElement resolved = reference.resolve();
if (resolved != null && !reported.contains(resolved) && !CommonRefactoringUtil.isAncestor(resolved, scopes) && !PsiSearchScopeUtil.isInScope(resolveScope, resolved) && !(resolved instanceof LightElement)) {
final String scopeDescription = RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(reference), true);
final String message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.in.module.2", RefactoringUIUtil.getDescription(resolved, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(targetModule.getName()));
conflicts.putValue(resolved, CommonRefactoringUtil.capitalize(message));
reported.add(resolved);
}
}
});
}
boolean isInTestSources = ModuleRootManager.getInstance(targetModule).getFileIndex().isInTestSourceContent(vFile);
NextUsage: for (UsageInfo usage : usages) {
final PsiElement element = usage.getElement();
if (element != null && PsiTreeUtil.getParentOfType(element, GrImportStatement.class, false) == null) {
for (PsiElement scope : scopes) {
if (PsiTreeUtil.isAncestor(scope, element, false))
continue NextUsage;
}
final GlobalSearchScope resolveScope1 = element.getResolveScope();
if (!resolveScope1.isSearchInModuleContent(targetModule, isInTestSources)) {
final PsiFile usageFile = element.getContainingFile();
PsiElement container;
if (usageFile instanceof PsiJavaFile) {
container = ConflictsUtil.getContainer(element);
} else {
container = usageFile;
}
final String scopeDescription = RefactoringUIUtil.getDescription(container, true);
final VirtualFile usageVFile = usageFile.getVirtualFile();
if (usageVFile != null) {
Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(usageVFile);
if (module != null) {
final String message;
final PsiElement referencedElement;
if (usage instanceof MoveRenameUsageInfo) {
referencedElement = ((MoveRenameUsageInfo) usage).getReferencedElement();
} else {
referencedElement = usage.getElement();
}
assert referencedElement != null : usage;
if (module == targetModule && isInTestSources) {
message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.production.of.module.2", RefactoringUIUtil.getDescription(referencedElement, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(module.getName()));
} else {
message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.from.module.2", RefactoringUIUtil.getDescription(referencedElement, true), scopeDescription, CommonRefactoringUtil.htmlEmphasize(module.getName()));
}
conflicts.putValue(referencedElement, CommonRefactoringUtil.capitalize(message));
}
}
}
}
}
}
use of com.intellij.psi.impl.light.LightElement in project intellij-community by JetBrains.
the class StubGenerator method writeConstructor.
@Override
public void writeConstructor(final StringBuilder text, PsiMethod constructor, boolean isEnum) {
LOG.assertTrue(constructor.isConstructor());
if (!isEnum) {
text.append("public ");
//writeModifiers(text, constructor.getModifierList(), JAVA_MODIFIERS);
}
/************* name **********/
//append constructor name
text.append(constructor.getName());
/************* parameters **********/
GenerationUtil.writeParameterList(text, constructor.getParameterList().getParameters(), classNameProvider, null);
final Set<String> throwsTypes = collectThrowsTypes(constructor, new THashSet<>());
if (!throwsTypes.isEmpty()) {
text.append("throws ").append(StringUtil.join(throwsTypes, ", ")).append(' ');
}
/************* body **********/
text.append("{\n");
if (constructor instanceof GrReflectedMethod) {
constructor = ((GrReflectedMethod) constructor).getBaseMethod();
}
if (constructor instanceof GrMethod) {
final GrConstructorInvocation invocation = PsiImplUtil.getChainingConstructorInvocation((GrMethod) constructor);
if (invocation != null) {
final GroovyResolveResult resolveResult = resolveChainingConstructor((GrMethod) constructor);
if (resolveResult != null) {
text.append(invocation.isSuperCall() ? "super(" : "this(");
writeStubConstructorInvocation(text, (PsiMethod) resolveResult.getElement(), resolveResult.getSubstitutor(), invocation);
text.append(");");
}
} else if (constructor instanceof LightElement) {
writeStubConstructorInvocation(constructor, text);
}
}
text.append("\n}\n");
}
use of com.intellij.psi.impl.light.LightElement in project intellij-community by JetBrains.
the class ResolveResultList method poke.
// Allows to add non-null elements and discard nulls in a hassle-free way.
public boolean poke(final PsiElement what, final int rate) {
PyPsiUtils.assertValid(what);
if (what == null)
return false;
if (!(what instanceof LightElement) && !what.isValid()) {
throw new PsiInvalidElementAccessException(what, "Trying to resolve a reference to an invalid element");
}
super.add(new RatedResolveResult(rate, what));
return true;
}
Aggregations