use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitor method checkConstructorApplicability.
private <T extends GroovyPsiElement> boolean checkConstructorApplicability(@NotNull GroovyResolveResult constructorResolveResult, @NotNull CallInfo<T> info, boolean checkUnknownArgs) {
final PsiElement element = constructorResolveResult.getElement();
LOG.assertTrue(element instanceof PsiMethod && ((PsiMethod) element).isConstructor(), element);
final PsiMethod constructor = (PsiMethod) element;
final GrArgumentList argList = info.getArgumentList();
if (argList != null) {
final GrExpression[] exprArgs = argList.getExpressionArguments();
if (exprArgs.length == 0 && !PsiUtil.isConstructorHasRequiredParameters(constructor))
return true;
}
PsiType[] types = info.getArgumentTypes();
PsiClass containingClass = constructor.getContainingClass();
if (types != null && containingClass != null) {
final PsiType[] newTypes = GrInnerClassConstructorUtil.addEnclosingArgIfNeeded(types, info.getCall(), containingClass);
if (newTypes.length != types.length) {
return checkMethodApplicability(constructorResolveResult, checkUnknownArgs, new DelegatingCallInfo<T>(info) {
@Nullable
@Override
public PsiType[] getArgumentTypes() {
return newTypes;
}
});
}
}
return checkMethodApplicability(constructorResolveResult, checkUnknownArgs, info);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class GrChangeSignatureConflictSearcher method addMethodConflicts.
private void addMethodConflicts(MultiMap<PsiElement, String> conflicts) {
try {
GrMethod prototype;
final PsiMethod method = myChangeInfo.getMethod();
if (!(method instanceof GrMethod))
return;
PsiManager manager = method.getManager();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(manager.getProject());
final CanonicalTypes.Type returnType = myChangeInfo.getNewReturnType();
String newMethodName = myChangeInfo.getNewName();
if (method.isConstructor()) {
prototype = factory.createConstructorFromText("foo", ArrayUtil.EMPTY_STRING_ARRAY, ArrayUtil.EMPTY_STRING_ARRAY, "{}", method);
} else {
prototype = factory.createMethodFromText("", "foo", returnType != null ? returnType.getTypeText() : null, ArrayUtil.EMPTY_STRING_ARRAY, method);
}
prototype.setName(newMethodName);
JavaParameterInfo[] parameters = myChangeInfo.getNewParameters();
for (JavaParameterInfo info : parameters) {
GrParameter param;
if (info instanceof GrParameterInfo) {
param = factory.createParameter(info.getName(), info.getTypeText(), ((GrParameterInfo) info).getDefaultInitializer(), (GroovyPsiElement) method);
} else {
param = factory.createParameter(info.getName(), info.getTypeText(), (GroovyPsiElement) method);
}
prototype.getParameterList().add(param);
}
ConflictsUtil.checkMethodConflicts(method.getContainingClass(), method, prototype, conflicts);
GrMethodConflictUtil.checkMethodConflicts(method.getContainingClass(), prototype, ((GrMethod) method), conflicts, true);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class GrRefactoringConflictsUtil method analyzeModuleConflicts.
public static void analyzeModuleConflicts(final Project project, final Collection<? extends PsiElement> scopes, final UsageInfo[] usages, final PsiElement target, final MultiMap<PsiElement, String> conflicts) {
if (scopes == null)
return;
final VirtualFile vFile = PsiUtilCore.getVirtualFile(target);
if (vFile == null)
return;
List<GroovyPsiElement> groovyScopes = ContainerUtil.collect(scopes.iterator(), new FilteringIterator.InstanceOf<>(GroovyPsiElement.class));
analyzeModuleConflicts(project, groovyScopes, usages, vFile, conflicts);
scopes.removeAll(groovyScopes);
RefactoringConflictsUtil.analyzeModuleConflicts(project, scopes, usages, vFile, conflicts);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement 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 org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class GroovyChangeContextUtil method decodeContextInfo.
public static void decodeContextInfo(PsiElement element, @Nullable PsiClass thisClass, @Nullable GrExpression thisAccessExpr) {
if (!(element instanceof GroovyPsiElement))
return;
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
decodeContextInfo(child, thisClass, thisAccessExpr);
}
if (element.getCopyableUserData(KEY_ENCODED) != null) {
element.putCopyableUserData(KEY_ENCODED, null);
final PsiManager manager = element.getManager();
if (PsiUtil.isThisReference(element)) {
final PsiClass thisQualClass = element.getCopyableUserData(QUALIFIER_CLASS_KEY);
element.putCopyableUserData(QUALIFIER_CLASS_KEY, null);
if (thisAccessExpr != null && !manager.areElementsEquivalent(thisClass, thisQualClass)) {
element.replace(thisAccessExpr);
return;
}
} else if (element instanceof GrReferenceExpression) {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
final GrReferenceExpression refExpr = (GrReferenceExpression) element;
final PsiElement resolvedElement = refExpr.resolve();
final PsiMember memberRef = refExpr.getCopyableUserData(REF_TO_MEMBER);
refExpr.putCopyableUserData(REF_TO_MEMBER, null);
if (memberRef != null && memberRef.isValid()) {
final PsiClass memberClass = memberRef.getContainingClass();
if (memberClass != null) {
if (memberRef.hasModifierProperty(PsiModifier.STATIC)) {
if (!manager.areElementsEquivalent(memberRef, resolvedElement)) {
final PsiElement qualifier = refExpr.getQualifier();
if (!(qualifier instanceof GrReferenceExpression)) {
refExpr.setQualifier(factory.createReferenceExpressionFromText(memberClass.getQualifiedName()));
JavaCodeStyleManager.getInstance(manager.getProject()).shortenClassReferences(refExpr.getQualifier());
return;
}
}
} else if (thisAccessExpr instanceof GrReferenceExpression) {
final PsiElement qualifier = refExpr.getQualifier();
if (!(qualifier instanceof GrReferenceExpression)) {
refExpr.setQualifier(thisAccessExpr);
return;
}
}
}
}
}
PsiClass refClass = element.getCopyableUserData(REF_TO_CLASS);
element.putCopyableUserData(REF_TO_CLASS, null);
if (refClass != null && refClass.isValid()) {
final PsiReference ref = element.getReference();
if (ref != null) {
ref.bindToElement(refClass);
}
}
}
}
Aggregations