Search in sources :

Example 1 with GrSafeCastExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression 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);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 2 with GrSafeCastExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression 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 3 with GrSafeCastExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression in project intellij-community by JetBrains.

the class ClosureAsAnonymousParameterEnhancer method getClosureParameterType.

@Nullable
@Override
protected PsiType getClosureParameterType(GrClosableBlock closure, int index) {
    List<PsiType> expectedTypes;
    if (closure.getParent() instanceof GrSafeCastExpression) {
        GrSafeCastExpression safeCastExpression = (GrSafeCastExpression) closure.getParent();
        GrTypeElement typeElement = safeCastExpression.getCastTypeElement();
        if (typeElement != null) {
            PsiType castType = typeElement.getType();
            expectedTypes = ContainerUtil.newArrayList(GroovyExpectedTypesProvider.getDefaultExpectedTypes(safeCastExpression));
            for (Iterator<PsiType> iterator = expectedTypes.iterator(); iterator.hasNext(); ) {
                if (!TypesUtil.isAssignable(iterator.next(), castType, closure)) {
                    iterator.remove();
                }
            }
            if (expectedTypes.isEmpty())
                expectedTypes.add(castType);
        } else {
            expectedTypes = GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure);
        }
    } else {
        expectedTypes = GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure);
    }
    for (PsiType constraint : expectedTypes) {
        final PsiType suggestion = GppClosureParameterTypeProvider.getSingleMethodParameterType(constraint, index, closure);
        if (suggestion != null) {
            if (GroovyConfigUtils.getInstance().isVersionAtLeast(closure, GroovyConfigUtils.GROOVY2_3)) {
                if (suggestion instanceof PsiWildcardType && ((PsiWildcardType) suggestion).isSuper()) {
                    return ((PsiWildcardType) suggestion).getBound();
                }
            }
            return TypesUtil.substituteAndNormalizeType(suggestion, PsiSubstitutor.EMPTY, null, closure);
        }
    }
    return null;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) PsiWildcardType(com.intellij.psi.PsiWildcardType) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with GrSafeCastExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression in project intellij-community by JetBrains.

the class GrChangeSignatureUsageProcessor method processMethodUsage.

private static void processMethodUsage(PsiElement element, JavaChangeInfo changeInfo, boolean toChangeArguments, boolean toCatchExceptions, GrClosureSignatureUtil.ArgInfo<PsiElement>[] map, PsiSubstitutor substitutor) {
    if (map == null)
        return;
    if (changeInfo.isNameChanged()) {
        if (element instanceof GrReferenceElement) {
            element = ((GrReferenceElement) element).handleElementRename(changeInfo.getNewName());
        }
    }
    if (toChangeArguments) {
        JavaParameterInfo[] parameters = changeInfo.getNewParameters();
        GrArgumentList argumentList = PsiUtil.getArgumentsList(element);
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
        if (argumentList == null) {
            if (element instanceof GrEnumConstant) {
                argumentList = factory.createArgumentList();
                argumentList = (GrArgumentList) element.add(argumentList);
            } else {
                return;
            }
        }
        Set<PsiElement> argsToDelete = new HashSet<>(map.length * 2);
        for (GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo : map) {
            argsToDelete.addAll(argInfo.args);
        }
        GrExpression[] values = new GrExpression[parameters.length];
        for (int i = 0; i < parameters.length; i++) {
            JavaParameterInfo parameter = parameters[i];
            int index = parameter.getOldIndex();
            if (index >= 0) {
                argsToDelete.removeAll(map[index].args);
            } else {
                values[i] = createDefaultValue(factory, changeInfo, parameter, argumentList, substitutor);
            }
        }
        for (PsiElement arg : argsToDelete) {
            arg.delete();
        }
        boolean skipOptionals = false;
        //PsiTreeUtil.getChildOfAnyType(argumentList, GrExpression.class, GrNamedArgument.class);
        PsiElement anchor = null;
        for (int i = 0; i < parameters.length; i++) {
            JavaParameterInfo parameter = parameters[i];
            int index = parameter.getOldIndex();
            if (index >= 0) {
                GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = map[index];
                List<PsiElement> arguments = argInfo.args;
                if (argInfo.isMultiArg) {
                    //arguments for Map and varArg
                    if ((i != 0 || !(!arguments.isEmpty() && arguments.iterator().next() instanceof GrNamedArgument)) && (i != parameters.length - 1 || !parameter.isVarargType())) {
                        final PsiType type = parameter.createType(changeInfo.getMethod().getParameterList(), argumentList.getManager());
                        final GrExpression arg = GroovyRefactoringUtil.generateArgFromMultiArg(substitutor, arguments, type, element.getProject());
                        for (PsiElement argument : arguments) {
                            argument.delete();
                        }
                        anchor = argumentList.addAfter(arg, anchor);
                        JavaCodeStyleManager.getInstance(anchor.getProject()).shortenClassReferences(anchor);
                    }
                } else {
                    //arguments for simple parameters
                    if (arguments.size() == 1) {
                        //arg exists
                        PsiElement arg = arguments.iterator().next();
                        if (i == parameters.length - 1 && parameter.isVarargType()) {
                            if (arg instanceof GrSafeCastExpression) {
                                PsiElement expr = ((GrSafeCastExpression) arg).getOperand();
                                if (expr instanceof GrListOrMap && !((GrListOrMap) expr).isMap()) {
                                    final PsiElement copy = expr.copy();
                                    PsiElement[] newVarargs = ((GrListOrMap) copy).getInitializers();
                                    for (PsiElement vararg : newVarargs) {
                                        anchor = argumentList.addAfter(vararg, anchor);
                                    }
                                    arg.delete();
                                    continue;
                                }
                            }
                        }
                        PsiElement curArg = getNextOfType(argumentList, anchor, GrExpression.class);
                        if (curArg == arg) {
                            anchor = arg;
                        } else {
                            final PsiElement copy = arg.copy();
                            anchor = argumentList.addAfter(copy, anchor);
                            arg.delete();
                        }
                    } else {
                        //arg is skipped. Parameter is optional
                        skipOptionals = true;
                    }
                }
            } else {
                if (skipOptionals && isParameterOptional(parameter))
                    continue;
                if (forceOptional(parameter)) {
                    skipOptionals = true;
                    continue;
                }
                try {
                    final GrExpression value = values[i];
                    if (i > 0 && (value == null || anchor == null)) {
                        PsiElement comma = Factory.createSingleLeafElement(GroovyTokenTypes.mCOMMA, ",", 0, 1, SharedImplUtil.findCharTableByTree(argumentList.getNode()), argumentList.getManager()).getPsi();
                        if (anchor == null)
                            anchor = argumentList.getLeftParen();
                        anchor = argumentList.addAfter(comma, anchor);
                    }
                    if (value != null) {
                        anchor = argumentList.addAfter(value, anchor);
                    }
                } catch (IncorrectOperationException e) {
                    LOG.error(e.getMessage());
                }
            }
        }
        for (PsiElement arg : argsToDelete) {
            arg.delete();
        }
        GrCall call = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
        if (argumentList.getText().trim().isEmpty() && (call == null || !PsiImplUtil.hasClosureArguments(call))) {
            argumentList = argumentList.replaceWithArgumentList(factory.createArgumentList());
        }
        CodeStyleManager.getInstance(argumentList.getProject()).reformat(argumentList);
    }
    if (toCatchExceptions) {
        final ThrownExceptionInfo[] exceptionInfos = changeInfo.getNewExceptions();
        PsiClassType[] exceptions = getExceptions(exceptionInfos, element, element.getManager());
        fixExceptions(element, exceptions);
    }
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) HashSet(com.intellij.util.containers.HashSet) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Aggregations

GrSafeCastExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression)4 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)3 Nullable (org.jetbrains.annotations.Nullable)2 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 PsiType (com.intellij.psi.PsiType)1 PsiWildcardType (com.intellij.psi.PsiWildcardType)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 HashSet (com.intellij.util.containers.HashSet)1 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)1 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)1 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)1 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)1 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)1 GrNewExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression)1 GrTypeCastExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression)1 GrAnonymousClassDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition)1