use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ExpressionGenerator method visitIndexProperty.
@Override
public void visitIndexProperty(@NotNull GrIndexProperty expression) {
final GrExpression selectedExpression = expression.getInvokedExpression();
final PsiType thisType = selectedExpression.getType();
final GrArgumentList argList = expression.getArgumentList();
if (argList.getAllArguments().length == 0) {
// int[] or String[]
if (selectedExpression instanceof GrBuiltinTypeClassExpression) {
selectedExpression.accept(this);
return;
} else if (selectedExpression instanceof GrReferenceExpression) {
PsiElement resolved = ((GrReferenceExpression) selectedExpression).resolve();
if (resolved instanceof PsiClass) {
builder.append(((PsiClass) resolved).getQualifiedName());
builder.append("[].class");
return;
}
}
}
final PsiType[] argTypes = PsiUtil.getArgumentTypes(argList);
final GrExpression[] exprArgs = argList.getExpressionArguments();
final GrNamedArgument[] namedArgs = argList.getNamedArguments();
if (!PsiImplUtil.isSimpleArrayAccess(thisType, argTypes, expression, PsiUtil.isLValue(expression))) {
final GroovyResolveResult candidate = PsiImplUtil.extractUniqueResult(expression.multiResolve(false));
PsiElement element = candidate.getElement();
if (element != null || !PsiUtil.isLValue(expression)) {
//see the case of l-value in assignment expression
if (element instanceof GrGdkMethod && ((GrGdkMethod) element).getStaticMethod().getParameterList().getParameters()[0].getType().equalsToText("java.util.Map<K,V>")) {
PsiClass map = JavaPsiFacade.getInstance(context.project).findClass(CommonClassNames.JAVA_UTIL_MAP, expression.getResolveScope());
if (map != null) {
PsiMethod[] gets = map.findMethodsByName("get", false);
invokeMethodOn(gets[0], selectedExpression, exprArgs, namedArgs, GrClosableBlock.EMPTY_ARRAY, PsiSubstitutor.EMPTY, expression);
return;
}
} else if (element instanceof GrGdkMethod && ((GrGdkMethod) element).getStaticMethod().getParameterList().getParameters()[0].getType().equalsToText("java.util.List<T>")) {
PsiClass list = JavaPsiFacade.getInstance(context.project).findClass(CommonClassNames.JAVA_UTIL_LIST, expression.getResolveScope());
if (list != null) {
PsiMethod[] gets = list.findMethodsByName("get", false);
invokeMethodOn(gets[0], selectedExpression, exprArgs, namedArgs, GrClosableBlock.EMPTY_ARRAY, PsiSubstitutor.EMPTY, expression);
return;
}
}
GenerationUtil.invokeMethodByResolveResult(selectedExpression, candidate, "getAt", exprArgs, namedArgs, GrClosableBlock.EMPTY_ARRAY, this, expression);
return;
}
}
selectedExpression.accept(this);
builder.append('[');
final GrExpression arg = exprArgs[0];
arg.accept(this);
builder.append(']');
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GenerationUtil method writeCodeReferenceElement.
public static void writeCodeReferenceElement(StringBuilder builder, GrCodeReferenceElement referenceElement) {
final GroovyResolveResult resolveResult = referenceElement.advancedResolve();
final PsiElement resolved = resolveResult.getElement();
if (resolved == null) {
builder.append(referenceElement.getText());
return;
}
LOG.assertTrue(resolved instanceof PsiClass || resolved instanceof PsiPackage);
if (resolved instanceof PsiClass) {
builder.append(((PsiClass) resolved).getQualifiedName());
} else {
builder.append(((PsiPackage) resolved).getQualifiedName());
}
writeTypeParameters(builder, referenceElement.getTypeArguments(), referenceElement, new GeneratorClassNameProvider());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GenerationUtil method getDeclaredType.
@Nullable
public static PsiType getDeclaredType(@Nullable GrExpression expression, ExpressionContext context) {
if (expression instanceof GrReferenceExpression) {
final GroovyResolveResult resolveResult = ((GrReferenceExpression) expression).advancedResolve();
final PsiSubstitutor substitutor = resolveResult.getSubstitutor();
PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiVariable) {
return substitutor.substitute(context.typeProvider.getVarType((PsiVariable) resolved));
} else if (resolved instanceof PsiMethod) {
return getDeclaredType((PsiMethod) resolved, substitutor, context);
}
} else if (expression instanceof GrMethodCall) {
final GrExpression invokedExpression = ((GrMethodCall) expression).getInvokedExpression();
return getDeclaredType(invokedExpression, context);
} else if (expression instanceof GrBinaryExpression) {
final GroovyResolveResult result = PsiImplUtil.extractUniqueResult(((GrBinaryExpression) expression).multiResolve(false));
if (result.getElement() instanceof PsiMethod) {
return getDeclaredType((PsiMethod) result.getElement(), result.getSubstitutor(), context);
}
} else if (expression instanceof GrIndexProperty) {
final GroovyResolveResult result = ((GrIndexProperty) expression).advancedResolve();
if (result.getElement() instanceof PsiMethod) {
return getDeclaredType((PsiMethod) result.getElement(), result.getSubstitutor(), context);
}
} else if (expression instanceof GrAssignmentExpression) {
return getDeclaredType(((GrAssignmentExpression) expression).getRValue(), context);
} else if (expression instanceof GrConditionalExpression) {
return TypesUtil.getLeastUpperBoundNullable(getDeclaredType(((GrConditionalExpression) expression).getThenBranch(), context), getDeclaredType(((GrConditionalExpression) expression).getElseBranch(), context), expression.getManager());
} else if (expression instanceof GrParenthesizedExpression) {
return getDeclaredType(((GrParenthesizedExpression) expression).getOperand(), context);
} else if (expression == null) {
return null;
}
return expression.getType();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GenerationUtil method invokeMethodByName.
public static void invokeMethodByName(@Nullable GrExpression caller, @NotNull String methodName, @NotNull GrExpression[] exprs, @NotNull GrNamedArgument[] namedArgs, @NotNull GrClosableBlock[] closureArgs, @NotNull ExpressionGenerator expressionGenerator, @NotNull GroovyPsiElement psiContext) {
GroovyResolveResult call = resolveMethod(caller, methodName, exprs, namedArgs, closureArgs, psiContext);
invokeMethodByResolveResult(caller, call, methodName, exprs, namedArgs, closureArgs, expressionGenerator, psiContext);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GroovyPostHighlightingPass method doCollectInformation.
@Override
public void doCollectInformation(@NotNull final ProgressIndicator progress) {
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
VirtualFile virtualFile = myFile.getViewProvider().getVirtualFile();
if (!fileIndex.isInContent(virtualFile)) {
return;
}
final InspectionProfile profile = InspectionProjectProfileManager.getInstance(myProject).getCurrentProfile();
final HighlightDisplayKey unusedDefKey = HighlightDisplayKey.find(GroovyUnusedDeclarationInspection.SHORT_NAME);
final boolean deadCodeEnabled = profile.isToolEnabled(unusedDefKey, myFile);
final UnusedDeclarationInspectionBase deadCodeInspection = (UnusedDeclarationInspectionBase) profile.getUnwrappedTool(UnusedDeclarationInspectionBase.SHORT_NAME, myFile);
final GlobalUsageHelper usageHelper = new GlobalUsageHelper() {
@Override
public boolean isCurrentFileAlreadyChecked() {
return false;
}
@Override
public boolean isLocallyUsed(@NotNull PsiNamedElement member) {
return false;
}
@Override
public boolean shouldCheckUsages(@NotNull PsiMember member) {
return deadCodeInspection == null || !deadCodeInspection.isEntryPoint(member);
}
};
final List<HighlightInfo> unusedDeclarations = new ArrayList<>();
final Map<GrParameter, Boolean> usedParams = new HashMap<>();
myFile.accept(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof GrReferenceExpression && !((GrReferenceElement) element).isQualified()) {
GroovyResolveResult[] results = ((GrReferenceExpression) element).multiResolve(false);
if (results.length == 0) {
results = ((GrReferenceExpression) element).multiResolve(true);
}
for (GroovyResolveResult result : results) {
PsiElement resolved = result.getElement();
if (resolved instanceof GrParameter && resolved.getContainingFile() == myFile) {
usedParams.put((GrParameter) resolved, Boolean.TRUE);
}
}
}
if (deadCodeEnabled && element instanceof GrNamedElement && element instanceof PsiModifierListOwner && !UnusedSymbolUtil.isImplicitUsage(element.getProject(), (PsiModifierListOwner) element, progress) && !GroovySuppressableInspectionTool.isElementToolSuppressedIn(element, GroovyUnusedDeclarationInspection.SHORT_NAME)) {
PsiElement nameId = ((GrNamedElement) element).getNameIdentifierGroovy();
if (nameId.getNode().getElementType() == GroovyTokenTypes.mIDENT) {
String name = ((GrNamedElement) element).getName();
if (element instanceof GrTypeDefinition && !UnusedSymbolUtil.isClassUsed(myProject, element.getContainingFile(), (GrTypeDefinition) element, progress, usageHelper)) {
HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(nameId, "Class " + name + " is unused", HighlightInfoType.UNUSED_SYMBOL);
QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createSafeDeleteFix(element), unusedDefKey);
ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
} else if (element instanceof GrMethod) {
GrMethod method = (GrMethod) element;
if (!UnusedSymbolUtil.isMethodReferenced(method.getProject(), method.getContainingFile(), method, progress, usageHelper)) {
String message = (method.isConstructor() ? "Constructor" : "Method") + " " + name + " is unused";
HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(nameId, message, HighlightInfoType.UNUSED_SYMBOL);
QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createSafeDeleteFix(method), unusedDefKey);
ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
}
} else if (element instanceof GrField && isFieldUnused((GrField) element, progress, usageHelper)) {
HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(nameId, "Property " + name + " is unused", HighlightInfoType.UNUSED_SYMBOL);
QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createSafeDeleteFix(element), unusedDefKey);
ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
} else if (element instanceof GrParameter) {
if (!usedParams.containsKey(element)) {
usedParams.put((GrParameter) element, Boolean.FALSE);
}
}
}
}
super.visitElement(element);
}
});
final Set<GrImportStatement> unusedImports = new HashSet<>(PsiUtil.getValidImportStatements(myFile));
unusedImports.removeAll(GroovyImportUtil.findUsedImports(myFile));
myUnusedImports = unusedImports;
if (deadCodeEnabled) {
for (GrParameter parameter : usedParams.keySet()) {
if (usedParams.get(parameter))
continue;
PsiElement scope = parameter.getDeclarationScope();
if (scope instanceof GrMethod) {
GrMethod method = (GrMethod) scope;
if (methodMayHaveUnusedParameters(method)) {
PsiElement identifier = parameter.getNameIdentifierGroovy();
HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(identifier, "Parameter " + parameter.getName() + " is unused", HighlightInfoType.UNUSED_SYMBOL);
QuickFixAction.registerQuickFixAction(highlightInfo, GroovyQuickFixFactory.getInstance().createRemoveUnusedGrParameterFix(parameter), unusedDefKey);
ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
}
} else if (scope instanceof GrClosableBlock) {
//todo Max Medvedev
}
}
}
myUnusedDeclarations = unusedDeclarations;
}
Aggregations