use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class UnnecessaryQualifiedReferenceInspection method canBeSimplified.
private static boolean canBeSimplified(PsiElement element) {
if (PsiTreeUtil.getParentOfType(element, PsiComment.class) != null)
return false;
if (element instanceof GrCodeReferenceElement) {
if (PsiTreeUtil.getParentOfType(element, GrImportStatement.class, GrPackageDefinition.class) != null)
return false;
} else if (element instanceof GrReferenceExpression) {
if (!PsiImplUtil.seemsToBeQualifiedClassName((GrReferenceExpression) element))
return false;
} else {
return false;
}
final GrReferenceElement ref = (GrReferenceElement) element;
if (ref.getQualifier() == null)
return false;
if (!(ref.getContainingFile() instanceof GroovyFileBase))
return false;
final PsiElement resolved = ref.resolve();
if (!(resolved instanceof PsiClass))
return false;
final String name = ((PsiClass) resolved).getName();
if (name == null)
return false;
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
final GrReferenceExpression shortedRef = factory.createReferenceExpressionFromText(name, element);
final GroovyResolveResult resolveResult = shortedRef.advancedResolve();
if (element.getManager().areElementsEquivalent(resolved, resolveResult.getElement())) {
return true;
}
final PsiClass containingClass = ((PsiClass) resolved).getContainingClass();
if (containingClass != null && !GroovyCodeStyleSettingsFacade.getInstance(containingClass.getProject()).insertInnerClassImports()) {
return false;
}
return resolveResult.getElement() == null || !resolveResult.isAccessible() || !resolveResult.isStaticsOK();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GroovyNamedArgumentProvider method getNamedArgumentsFromAllProviders.
@Nullable
public static Map<String, NamedArgumentDescriptor> getNamedArgumentsFromAllProviders(@NotNull GrCall call, @Nullable String argumentName, boolean forCompletion) {
Map<String, NamedArgumentDescriptor> namedArguments = new HashMap<String, NamedArgumentDescriptor>() {
@Override
public NamedArgumentDescriptor put(String key, NamedArgumentDescriptor value) {
NamedArgumentDescriptor oldValue = super.put(key, value);
if (oldValue != null) {
super.put(key, oldValue);
}
//noinspection ConstantConditions
return oldValue;
}
};
GroovyResolveResult[] callVariants = call.getCallVariants(null);
if (callVariants.length == 0 || PsiUtil.isSingleBindingVariant(callVariants)) {
for (GroovyNamedArgumentProvider namedArgumentProvider : EP_NAME.getExtensions()) {
namedArgumentProvider.getNamedArguments(call, GroovyResolveResult.EMPTY_RESULT, argumentName, forCompletion, namedArguments);
}
} else {
boolean mapExpected = false;
for (GroovyResolveResult result : callVariants) {
PsiElement element = result.getElement();
if (element instanceof GrAccessorMethod)
continue;
if (element instanceof PsiMethod) {
PsiMethod method = (PsiMethod) element;
PsiParameter[] parameters = method.getParameterList().getParameters();
if (!method.isConstructor() && !(parameters.length > 0 && canBeMap(parameters[0])))
continue;
mapExpected = true;
for (GroovyMethodInfo methodInfo : GroovyMethodInfo.getInfos(method)) {
if (methodInfo.getNamedArguments() != null || methodInfo.isNamedArgumentProviderDefined()) {
if (methodInfo.isApplicable(method)) {
if (methodInfo.isNamedArgumentProviderDefined()) {
methodInfo.getNamedArgProvider().getNamedArguments(call, result, argumentName, forCompletion, namedArguments);
}
if (methodInfo.getNamedArguments() != null) {
namedArguments.putAll(methodInfo.getNamedArguments());
}
}
}
}
}
for (GroovyNamedArgumentProvider namedArgumentProvider : EP_NAME.getExtensions()) {
namedArgumentProvider.getNamedArguments(call, result, argumentName, forCompletion, namedArguments);
}
if (element instanceof GrVariable && InheritanceUtil.isInheritor(((GrVariable) element).getTypeGroovy(), GroovyCommonClassNames.GROOVY_LANG_CLOSURE)) {
mapExpected = true;
}
}
if (!mapExpected && namedArguments.isEmpty()) {
return null;
}
}
return namedArguments;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GDKSuperMethodSearcher method execute.
@Override
public boolean execute(@NotNull SuperMethodsSearch.SearchParameters queryParameters, @NotNull Processor<MethodSignatureBackedByPsiMethod> consumer) {
final PsiMethod method = queryParameters.getMethod();
if (!(method instanceof GrMethod)) {
return true;
}
if (method.hasModifierProperty(PsiModifier.STATIC))
return true;
final PsiClass psiClass = method.getContainingClass();
if (psiClass == null)
return true;
final HierarchicalMethodSignature hierarchicalSignature = method.getHierarchicalMethodSignature();
if (!hierarchicalSignature.getSuperSignatures().isEmpty())
return true;
final Project project = method.getProject();
final String name = method.getName();
final MethodResolverProcessor processor = new MethodResolverProcessor(name, ((GrMethod) method), false, null, null, PsiType.EMPTY_ARRAY);
ResolveUtil.processNonCodeMembers(JavaPsiFacade.getElementFactory(project).createType(psiClass), processor, (GrMethod) method, ResolveState.initial());
final GroovyResolveResult[] candidates = processor.getCandidates();
final PsiManager psiManager = PsiManager.getInstance(project);
final MethodSignature signature = method.getHierarchicalMethodSignature();
List<PsiMethod> goodSupers = new ArrayList<>();
for (GroovyResolveResult candidate : candidates) {
final PsiElement element = candidate.getElement();
if (element instanceof PsiMethod) {
final PsiMethod m = (PsiMethod) element;
if (!isTheSameMethod(method, psiManager, m) && PsiImplUtil.isExtendsSignature(m.getHierarchicalMethodSignature(), signature)) {
goodSupers.add(m);
}
}
}
if (goodSupers.isEmpty())
return true;
List<PsiMethod> result = new ArrayList<>(goodSupers.size());
result.add(goodSupers.get(0));
final Comparator<PsiMethod> comparator = (o1, o2) -> {
//compare by first parameter type
final PsiType type1 = getRealType(o1);
final PsiType type2 = getRealType(o2);
if (TypesUtil.isAssignableByMethodCallConversion(type1, type2, o1)) {
return -1;
} else if (TypesUtil.isAssignableByMethodCallConversion(type2, type1, o1)) {
return 1;
}
return 0;
};
Outer: for (PsiMethod current : goodSupers) {
for (Iterator<PsiMethod> i = result.iterator(); i.hasNext(); ) {
PsiMethod m = i.next();
final int res = comparator.compare(m, current);
if (res > 0) {
continue Outer;
} else if (res < 0) {
i.remove();
}
}
result.add(current);
}
for (PsiMethod psiMethod : result) {
if (!consumer.process(getRealMethod(psiMethod).getHierarchicalMethodSignature())) {
return false;
}
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GrUnresolvedAccessChecker method checkCodeRefInner.
@Nullable
private HighlightInfo checkCodeRefInner(GrCodeReferenceElement refElement) {
if (PsiTreeUtil.getParentOfType(refElement, GroovyDocPsiElement.class) != null)
return null;
PsiElement nameElement = refElement.getReferenceNameElement();
if (nameElement == null)
return null;
if (isResolvedStaticImport(refElement))
return null;
GroovyResolveResult resolveResult = refElement.advancedResolve();
final PsiElement resolved = resolveResult.getElement();
if (!(refElement.getParent() instanceof GrPackageDefinition) && resolved == null) {
String message = GroovyBundle.message("cannot.resolve", refElement.getReferenceName());
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(nameElement).descriptionAndTooltip(message).create();
// todo implement for nested classes
registerCreateClassByTypeFix(refElement, info, myDisplayKey);
registerAddImportFixes(refElement, info, myDisplayKey);
UnresolvedReferenceQuickFixProvider.registerReferenceFixes(refElement, new QuickFixActionRegistrarAdapter(info, myDisplayKey));
QuickFixFactory.getInstance().registerOrderEntryFixes(new QuickFixActionRegistrarAdapter(info, myDisplayKey), refElement);
return info;
}
if (refElement.getParent() instanceof GrNewExpression) {
boolean inStaticContext = GrStaticChecker.isInStaticContext(refElement);
if (!inStaticContext && GrUnresolvedAccessInspection.isSuppressed(refElement))
return null;
if (!inStaticContext) {
if (!myInspectionEnabled)
return null;
assert myInspection != null;
if (!myInspection.myHighlightInnerClasses)
return null;
}
GrNewExpression newExpression = (GrNewExpression) refElement.getParent();
if (resolved instanceof PsiClass) {
PsiClass clazz = (PsiClass) resolved;
final PsiClass outerClass = clazz.getContainingClass();
if (com.intellij.psi.util.PsiUtil.isInnerClass(clazz) && outerClass != null && newExpression.getArgumentList() != null && !PsiUtil.hasEnclosingInstanceInScope(outerClass, newExpression, true) && !hasEnclosingInstanceInArgList(newExpression.getArgumentList(), outerClass)) {
String qname = clazz.getQualifiedName();
LOG.assertTrue(qname != null, clazz.getText());
return createAnnotationForRef(refElement, inStaticContext, GroovyBundle.message("cannot.reference.non.static", qname));
}
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GroovyUntypedAccessInspection method buildVisitor.
@Override
@NotNull
protected BaseInspectionVisitor buildVisitor() {
return new BaseInspectionVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression refExpr) {
super.visitReferenceExpression(refExpr);
if (PsiUtil.isThisOrSuperRef(refExpr))
return;
GroovyResolveResult resolveResult = refExpr.advancedResolve();
PsiElement resolved = resolveResult.getElement();
if (resolved != null) {
if (GrHighlightUtil.isDeclarationAssignment(refExpr) || resolved instanceof PsiPackage)
return;
} else {
GrExpression qualifier = refExpr.getQualifierExpression();
if (qualifier == null && GrHighlightUtil.isDeclarationAssignment(refExpr))
return;
}
final PsiType refExprType = refExpr.getType();
if (refExprType == null) {
if (resolved != null) {
registerError(refExpr);
}
} else if (refExprType instanceof PsiClassType && ((PsiClassType) refExprType).resolve() == null) {
registerError(refExpr);
}
}
};
}
Aggregations