use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression in project intellij-community by JetBrains.
the class GroovyConstructorUsagesSearcher method processGroovyConstructorUsages.
private static boolean processGroovyConstructorUsages(GrCodeReferenceElement element, final Processor<GrNewExpression> newExpressionProcessor, final LiteralConstructorSearcher literalProcessor) {
PsiElement parent = element.getParent();
if (parent instanceof GrAnonymousClassDefinition) {
parent = parent.getParent();
}
if (parent instanceof GrNewExpression) {
return newExpressionProcessor.process((GrNewExpression) parent);
}
if (parent instanceof GrTypeElement) {
final GrTypeElement typeElement = (GrTypeElement) parent;
final PsiElement grandpa = typeElement.getParent();
if (grandpa instanceof GrVariableDeclaration) {
final GrVariable[] vars = ((GrVariableDeclaration) grandpa).getVariables();
if (vars.length == 1) {
final GrVariable variable = vars[0];
if (!checkLiteralInstantiation(variable.getInitializerGroovy(), literalProcessor)) {
return false;
}
}
} else if (grandpa instanceof GrMethod) {
final GrMethod method = (GrMethod) grandpa;
if (typeElement == method.getReturnTypeElementGroovy()) {
ControlFlowUtils.visitAllExitPoints(method.getBlock(), new ControlFlowUtils.ExitPointVisitor() {
@Override
public boolean visitExitPoint(Instruction instruction, @Nullable GrExpression returnValue) {
if (!checkLiteralInstantiation(returnValue, literalProcessor)) {
return false;
}
return true;
}
});
}
} else if (grandpa instanceof GrTypeCastExpression) {
final GrTypeCastExpression cast = (GrTypeCastExpression) grandpa;
if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
return false;
}
} else if (grandpa instanceof GrSafeCastExpression) {
final GrSafeCastExpression cast = (GrSafeCastExpression) grandpa;
if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
return false;
}
}
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression in project intellij-community by JetBrains.
the class GroovyConstructorNamedArgumentProvider method getNamedArguments.
@Override
public void getNamedArguments(@NotNull GrCall call, @NotNull GroovyResolveResult resolveResult, @Nullable String argumentName, boolean forCompletion, @NotNull Map<String, NamedArgumentDescriptor> result) {
if (!(call instanceof GrNewExpression))
return;
PsiElement resolve = resolveResult.getElement();
if (resolve != null) {
if (!(resolve instanceof PsiMethod))
return;
PsiMethod method = (PsiMethod) resolve;
if (!method.isConstructor())
return;
}
GrNewExpression newCall = (GrNewExpression) call;
GrArgumentList argumentList = newCall.getArgumentList();
if (argumentList == null)
return;
GrExpression[] expressionArguments = argumentList.getExpressionArguments();
if (expressionArguments.length > 1 || (expressionArguments.length == 1 && !(expressionArguments[0] instanceof GrReferenceExpression))) {
return;
}
for (GroovyResolveResult newResult : newCall.multiResolveClass()) {
PsiElement element = newResult.getElement();
if (!(element instanceof PsiClass))
continue;
PsiClass aClass = (PsiClass) element;
if (!isClassHasConstructorWithMap(aClass))
continue;
PsiClassType classType = JavaPsiFacade.getElementFactory(aClass.getProject()).createType(aClass);
processClass(call, classType, argumentName, result);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression in project intellij-community by JetBrains.
the class GrClosureSignatureUtil method mapParametersToArguments.
@Nullable
public static ArgInfo<PsiElement>[] mapParametersToArguments(@NotNull GrClosureSignature signature, @NotNull GrNamedArgument[] namedArgs, @NotNull GrExpression[] expressionArgs, @NotNull GrClosableBlock[] closureArguments, @NotNull PsiElement context, boolean partial, boolean eraseArgs) {
List<InnerArg> innerArgs = new ArrayList<>();
boolean hasNamedArgs = namedArgs.length > 0;
GrClosureParameter[] params = signature.getParameters();
if (hasNamedArgs) {
if (params.length == 0)
return null;
PsiType type = params[0].getType();
if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP) || type == null || type.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) {
innerArgs.add(new InnerArg(GrMapType.create(context.getResolveScope()), namedArgs));
} else {
return null;
}
}
for (GrExpression expression : expressionArgs) {
PsiType type = expression.getType();
if (partial && expression instanceof GrNewExpression && com.intellij.psi.util.PsiUtil.resolveClassInType(type) == null) {
type = null;
}
if (eraseArgs) {
type = TypeConversionUtil.erasure(type);
}
innerArgs.add(new InnerArg(type, expression));
}
for (GrClosableBlock closureArgument : closureArguments) {
innerArgs.add(new InnerArg(TypeConversionUtil.erasure(closureArgument.getType()), closureArgument));
}
return mapParametersToArguments(signature, innerArgs, hasNamedArgs, partial, context);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression in project intellij-community by JetBrains.
the class NewInstanceOfSingletonInspection method buildVisitor.
@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
return new BaseInspectionVisitor() {
@Override
public void visitNewExpression(@NotNull GrNewExpression newExpression) {
if (newExpression.getArrayDeclaration() != null)
return;
GrCodeReferenceElement refElement = newExpression.getReferenceElement();
if (refElement == null)
return;
PsiElement resolved = refElement.resolve();
if (!(resolved instanceof GrTypeDefinition))
return;
PsiAnnotation annotation = AnnotationUtil.findAnnotation((GrTypeDefinition) resolved, GROOVY_LANG_SINGLETON);
if (annotation == null)
return;
registerError(newExpression, GroovyInspectionBundle.message("new.instance.of.singleton"), ContainerUtil.ar(new ReplaceWithInstanceAccessFix()), ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression in project intellij-community by JetBrains.
the class GrNewExpressionInfo method getElementToHighlight.
@NotNull
@Override
public PsiElement getElementToHighlight() {
GrNewExpression call = getCall();
GrArgumentList argList = call.getArgumentList();
if (argList != null)
return argList;
GrCodeReferenceElement ref = call.getReferenceElement();
if (ref != null)
return ref;
throw new IncorrectOperationException("reference of new expression should exist if it is a constructor call");
}
Aggregations