Search in sources :

Example 6 with GrNewExpression

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;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrTypeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with GrNewExpression

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);
    }
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 8 with GrNewExpression

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);
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with GrNewExpression

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);
        }
    };
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiAnnotation(com.intellij.psi.PsiAnnotation) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with GrNewExpression

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");
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) IncorrectOperationException(com.intellij.util.IncorrectOperationException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrNewExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression)14 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)9 GrAnonymousClassDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)5 PsiElement (com.intellij.psi.PsiElement)4 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)4 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)3 TextRange (com.intellij.openapi.util.TextRange)2 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)2 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)2 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)2 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)2 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)2 GrSafeCastExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression)2 GrTypeCastExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression)2