use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitor method checkMethodApplicability.
private <T extends GroovyPsiElement> boolean checkMethodApplicability(@NotNull final GroovyResolveResult methodResolveResult, boolean checkUnknownArgs, @NotNull final CallInfo<T> info) {
final PsiElement element = methodResolveResult.getElement();
if (!(element instanceof PsiMethod))
return true;
if (element instanceof GrBuilderMethod)
return true;
final PsiMethod method = (PsiMethod) element;
if ("call".equals(method.getName()) && info.getInvokedExpression() instanceof GrReferenceExpression) {
final GrExpression qualifierExpression = ((GrReferenceExpression) info.getInvokedExpression()).getQualifierExpression();
if (qualifierExpression != null) {
final PsiType type = qualifierExpression.getType();
if (type instanceof GrClosureType) {
GrClosureSignatureUtil.ApplicabilityResult result = PsiUtil.isApplicableConcrete(info.getArgumentTypes(), (GrClosureType) type, info.getInvokedExpression());
switch(result) {
case inapplicable:
highlightInapplicableMethodUsage(methodResolveResult, info, method);
return false;
case //q(1,2)
canBeApplicable:
if (checkUnknownArgs) {
highlightUnknownArgs(info);
}
return !checkUnknownArgs;
default:
return true;
}
}
}
}
if (method instanceof GrGdkMethod && info.getInvokedExpression() instanceof GrReferenceExpression) {
final GrReferenceExpression invoked = (GrReferenceExpression) info.getInvokedExpression();
final GrExpression qualifier = PsiImplUtil.getRuntimeQualifier(invoked);
if (qualifier == null && method.getName().equals("call")) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(invoked.getProject());
final GrReferenceExpression callRef = factory.createReferenceExpressionFromText("qualifier.call", invoked);
callRef.setQualifier(invoked);
return checkMethodApplicability(methodResolveResult, checkUnknownArgs, new DelegatingCallInfo<T>(info) {
@Nullable
@Override
public GrExpression getInvokedExpression() {
return callRef;
}
@NotNull
@Override
public GroovyResolveResult advancedResolve() {
return methodResolveResult;
}
@NotNull
@Override
public GroovyResolveResult[] multiResolve() {
return new GroovyResolveResult[] { methodResolveResult };
}
@Nullable
@Override
public PsiType getQualifierInstanceType() {
return info.getInvokedExpression().getType();
}
});
}
final PsiMethod staticMethod = ((GrGdkMethod) method).getStaticMethod();
PsiType qualifierType = info.getQualifierInstanceType();
if (method.hasModifierProperty(PsiModifier.STATIC)) {
qualifierType = ResolveUtil.unwrapClassType(qualifierType);
}
//check methods processed by @Category(ClassWhichProcessMethod) annotation
if (qualifierType != null && !GdkMethodUtil.isCategoryMethod(staticMethod, qualifierType, qualifier, methodResolveResult.getSubstitutor()) && !checkCategoryQualifier(invoked, qualifier, staticMethod, methodResolveResult.getSubstitutor())) {
registerError(info.getHighlightElementForCategoryQualifier(), ProblemHighlightType.GENERIC_ERROR, GroovyInspectionBundle.message("category.method.0.cannot.be.applied.to.1", method.getName(), qualifierType.getCanonicalText()));
return false;
}
}
if (info.getArgumentTypes() == null)
return true;
GrClosureSignatureUtil.ApplicabilityResult applicable = PsiUtil.isApplicableConcrete(info.getArgumentTypes(), method, methodResolveResult.getSubstitutor(), info.getCall(), false);
switch(applicable) {
case inapplicable:
highlightInapplicableMethodUsage(methodResolveResult, info, method);
return false;
case canBeApplicable:
if (checkUnknownArgs) {
highlightUnknownArgs(info);
}
return !checkUnknownArgs;
default:
return true;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrCastFix method doCast.
static void doCast(@NotNull Project project, @NotNull PsiType type, @NotNull GrExpression expr) {
if (!type.isValid())
return;
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final GrSafeCastExpression cast = (GrSafeCastExpression) factory.createExpressionFromText("foo as String");
final GrTypeElement typeElement = factory.createTypeElement(type);
cast.getOperand().replaceWithExpression(expr, true);
cast.getCastTypeElement().replace(typeElement);
final GrExpression replaced = expr.replaceWithExpression(cast, true);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory 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.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyCodeFragment method createSingleImport.
@Nullable
private GrImportStatement createSingleImport(@NotNull String qname, @Nullable String importedName) {
final PsiClass aClass = JavaPsiFacade.getInstance(getProject()).findClass(qname, getResolveScope());
final boolean isStatic = aClass == null;
final String className = PsiNameHelper.getShortClassName(qname);
final String alias = importedName == null || className.equals(importedName) ? null : importedName;
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
try {
return factory.createImportStatement(qname, isStatic, false, alias, this);
} catch (IncorrectOperationException e) {
return null;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class JavaStylePropertiesUtil method genRefForGetter.
private static GrExpression genRefForGetter(GrMethodCall call, String accessorName) {
String name = GroovyPropertyUtils.getPropertyNameByGetterName(accessorName, true);
GrReferenceExpression refExpr = (GrReferenceExpression) call.getInvokedExpression();
String oldNameStr = refExpr.getReferenceNameElement().getText();
String newRefExpr = StringUtil.trimEnd(refExpr.getText(), oldNameStr) + name;
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(call.getProject());
return factory.createExpressionFromText(newRefExpr, call);
}
Aggregations