Search in sources :

Example 6 with PsiType

use of com.intellij.psi.PsiType in project intellij-community by JetBrains.

the class GrVariableInplaceRenamer method renameSynthetic.

@Override
protected void renameSynthetic(String newName) {
    PsiNamedElement elementToRename = getVariable();
    if (elementToRename instanceof ClosureSyntheticParameter && !"it".equals(newName)) {
        final GrClosableBlock closure = ((ClosureSyntheticParameter) elementToRename).getClosure();
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
        final PsiType type = ((ClosureSyntheticParameter) elementToRename).getTypeGroovy();
        final GrParameter newParam = factory.createParameter(newName, TypesUtil.unboxPrimitiveTypeWrapper(type));
        final GrParameter added = closure.addParameter(newParam);
        JavaCodeStyleManager.getInstance(added.getProject()).shortenClassReferences(added);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) PsiNamedElement(com.intellij.psi.PsiNamedElement) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) ClosureSyntheticParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.ClosureSyntheticParameter) PsiType(com.intellij.psi.PsiType)

Example 7 with PsiType

use of com.intellij.psi.PsiType in project intellij-community by JetBrains.

the class GrInplaceVariableIntroducer method addAdditionalVariables.

@Override
protected void addAdditionalVariables(TemplateBuilderImpl builder) {
    GrVariable variable = getVariable();
    assert variable != null && variable.getInitializerGroovy() != null;
    final PsiType initializerType = variable.getInitializerGroovy().getType();
    TypeConstraint[] constraints = initializerType != null && !initializerType.equals(PsiType.NULL) ? new SupertypeConstraint[] { SupertypeConstraint.create(initializerType) } : TypeConstraint.EMPTY_ARRAY;
    ChooseTypeExpression typeExpression = new ChooseTypeExpression(constraints, variable.getManager(), variable.getResolveScope(), true, GroovyApplicationSettings.getInstance().INTRODUCE_LOCAL_SELECT_DEF);
    PsiElement element = getTypeELementOrDef(variable);
    if (element == null)
        return;
    builder.replaceElement(element, "Variable_type", typeExpression, true, true);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Example 8 with PsiType

use of com.intellij.psi.PsiType in project intellij-community by JetBrains.

the class CreateParameterFromUsageFix method showDialog.

private static void showDialog(final PsiMethod method, final GrReferenceExpression ref, final Project project) {
    final String name = ref.getReferenceName();
    final List<PsiType> types = GroovyExpectedTypesProvider.getDefaultExpectedTypes(ref);
    PsiType unboxed = types.isEmpty() ? null : TypesUtil.unboxPrimitiveTypeWrapper(types.get(0));
    @NotNull final PsiType type = unboxed != null ? unboxed : PsiType.getJavaLangObject(ref.getManager(), ref.getResolveScope());
    if (method instanceof GrMethod) {
        GrMethodDescriptor descriptor = new GrMethodDescriptor((GrMethod) method);
        GrChangeSignatureDialog dialog = new GrChangeSignatureDialog(project, descriptor, true, ref);
        List<GrParameterInfo> parameters = dialog.getParameters();
        parameters.add(createParameterInfo(name, type));
        dialog.setParameterInfos(parameters);
        dialog.show();
    } else if (method != null) {
        JavaChangeSignatureDialog dialog = new JavaChangeSignatureDialog(project, method, false, ref);
        final List<ParameterInfoImpl> parameterInfos = new ArrayList<>(Arrays.asList(ParameterInfoImpl.fromMethod(method)));
        ParameterInfoImpl parameterInfo = new ParameterInfoImpl(-1, name, type, PsiTypesUtil.getDefaultValueOfType(type), false);
        if (!method.isVarArgs()) {
            parameterInfos.add(parameterInfo);
        } else {
            parameterInfos.add(parameterInfos.size() - 1, parameterInfo);
        }
        dialog.setParameterInfos(parameterInfos);
        dialog.show();
    }
}
Also used : GrMethodDescriptor(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrMethodDescriptor) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) NotNull(org.jetbrains.annotations.NotNull) GrChangeSignatureDialog(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeSignatureDialog) GrParameterInfo(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo) JavaChangeSignatureDialog(com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog) ArrayList(java.util.ArrayList) List(java.util.List) PsiType(com.intellij.psi.PsiType)

Example 9 with PsiType

use of com.intellij.psi.PsiType in project intellij-community by JetBrains.

the class CreateSetterFromUsageFix method getArgumentTypes.

@Override
protected PsiType[] getArgumentTypes() {
    final GrReferenceExpression ref = getRefExpr();
    assert PsiUtil.isLValue(ref);
    PsiType initializer = TypeInferenceHelper.getInitializerTypeFor(ref);
    if (initializer == null || initializer == PsiType.NULL) {
        initializer = TypesUtil.getJavaLangObject(ref);
    }
    return new PsiType[] { initializer };
}
Also used : GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType)

Example 10 with PsiType

use of com.intellij.psi.PsiType in project intellij-community by JetBrains.

the class ConvertSimpleGetterToPropertyIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    GrMethod method = (GrMethod) element.getParent();
    GrOpenBlock block = method.getBlock();
    if (block == null)
        return;
    GrStatement statement = block.getStatements()[0];
    GrExpression value;
    if (statement instanceof GrReturnStatement) {
        value = ((GrReturnStatement) statement).getReturnValue();
    } else {
        value = (GrExpression) statement;
    }
    String fieldName = GroovyPropertyUtils.getPropertyNameByGetter(method);
    if (fieldName == null)
        return;
    PsiClass aClass = method.getContainingClass();
    if (aClass == null)
        return;
    List<String> modifiers = ContainerUtil.newArrayList();
    for (String modifier : MODIFIERS_TO_CHECK) {
        if (method.hasModifierProperty(modifier))
            modifiers.add(modifier);
    }
    modifiers.add(PsiModifier.FINAL);
    GrTypeElement returnTypeElement = method.getReturnTypeElementGroovy();
    PsiType returnType = returnTypeElement == null ? null : returnTypeElement.getType();
    GrVariableDeclaration declaration = GroovyPsiElementFactory.getInstance(project).createFieldDeclaration(ArrayUtil.toStringArray(modifiers), fieldName, value, returnType);
    PsiElement replaced = method.replace(declaration);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiClass(com.intellij.psi.PsiClass) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) PsiType(com.intellij.psi.PsiType)

Aggregations

PsiType (com.intellij.psi.PsiType)157 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)24 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)24 NotNull (org.jetbrains.annotations.NotNull)21 PsiClassType (com.intellij.psi.PsiClassType)20 PsiClass (com.intellij.psi.PsiClass)14 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 PsiPrimitiveType (com.intellij.psi.PsiPrimitiveType)12 ArrayList (java.util.ArrayList)9 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)9 PsiArrayType (com.intellij.psi.PsiArrayType)7 PsiExpression (com.intellij.psi.PsiExpression)7 PsiField (com.intellij.psi.PsiField)7 NonNls (org.jetbrains.annotations.NonNls)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 PsiLiteralExpression (com.intellij.psi.PsiLiteralExpression)6 PsiMethod (com.intellij.psi.PsiMethod)6 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)6 GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)6