Search in sources :

Example 1 with GrParameterInfo

use of org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo 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 2 with GrParameterInfo

use of org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo in project intellij-community by JetBrains.

the class RemoveUnusedGrParameterFix method createChangeInfo.

private static GrChangeInfoImpl createChangeInfo(GrMethod method, GrParameter parameter) {
    List<GrParameterInfo> params = new ArrayList<>();
    int i = 0;
    for (GrParameter p : method.getParameterList().getParameters()) {
        if (p != parameter) {
            params.add(new GrParameterInfo(p, i));
        }
        i++;
    }
    GrTypeElement typeElement = method.getReturnTypeElementGroovy();
    CanonicalTypes.Type wrapper = typeElement != null ? CanonicalTypes.createTypeWrapper(method.getReturnType()) : null;
    return new GrChangeInfoImpl(method, null, wrapper, method.getName(), params, null, false);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) CanonicalTypes(com.intellij.refactoring.util.CanonicalTypes) GrParameterInfo(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo) ArrayList(java.util.ArrayList) GrChangeInfoImpl(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeInfoImpl) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 3 with GrParameterInfo

use of org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo in project intellij-community by JetBrains.

the class GroovyIntroduceParameterObjectDelegate method createMergedParameterInfo.

@Override
public GrParameterInfo createMergedParameterInfo(GroovyIntroduceObjectClassDescriptor descriptor, GrMethod method, List<GrParameterInfo> oldMethodParameters) {
    final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(method.getProject());
    PsiType classType = elementFactory.createTypeByFQClassName(StringUtil.getQualifiedName(descriptor.getPackageName(), descriptor.getClassName()));
    return new GrParameterInfo(descriptor.getClassName(), null, null, classType, -1, false) {

        @Nullable
        @Override
        public PsiElement getActualValue(PsiElement callExpression, Object substitutor) {
            final IntroduceParameterObjectDelegate<PsiNamedElement, ParameterInfo, IntroduceParameterObjectClassDescriptor<PsiNamedElement, ParameterInfo>> delegate = findDelegate(callExpression);
            return delegate != null ? delegate.createNewParameterInitializerAtCallSite(callExpression, descriptor, oldMethodParameters, substitutor) : null;
        }
    };
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrParameterInfo(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo) PsiNamedElement(com.intellij.psi.PsiNamedElement) IntroduceParameterObjectClassDescriptor(com.intellij.refactoring.introduceParameterObject.IntroduceParameterObjectClassDescriptor) ParameterInfo(com.intellij.refactoring.changeSignature.ParameterInfo) GrParameterInfo(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Example 4 with GrParameterInfo

use of org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo in project intellij-community by JetBrains.

the class CreateParameterForFieldIntention method addParameter.

private static void addParameter(final GrField selectedValue, final GrMethod constructor, final Project project) {
    List<GrParameterInfo> parameters = new ArrayList<>();
    GrParameter[] constructorParameters = constructor.getParameters();
    for (int i = 0; i < constructorParameters.length; i++) {
        parameters.add(new GrParameterInfo(constructorParameters[i], i));
    }
    final String[] suggestedNames = JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.PARAMETER, selectedValue.getName(), null, null).names;
    final DefaultGroovyVariableNameValidator nameValidator = new DefaultGroovyVariableNameValidator(constructor, Collections.<String>emptyList(), false);
    String parameterName = ContainerUtil.find(suggestedNames, name -> !nameValidator.validateName(name, false).isEmpty());
    if (parameterName == null) {
        parameterName = nameValidator.validateName(suggestedNames[0], true);
    }
    parameters.add(new GrParameterInfo(parameterName, "null", "", selectedValue.getTypeGroovy(), -1, false));
    PsiClassType[] exceptionTypes = constructor.getThrowsList().getReferencedTypes();
    ThrownExceptionInfo[] thrownExceptionInfos = new ThrownExceptionInfo[exceptionTypes.length];
    for (int i = 0; i < exceptionTypes.length; i++) {
        new JavaThrownExceptionInfo(i, exceptionTypes[i]);
    }
    final GrChangeInfoImpl grChangeInfo = new GrChangeInfoImpl(constructor, null, null, constructor.getName(), parameters, thrownExceptionInfos, false);
    final String finalParameterName = parameterName;
    final GrChangeSignatureProcessor processor = new GrChangeSignatureProcessor(project, grChangeInfo) {

        @Override
        protected void performRefactoring(@NotNull UsageInfo[] usages) {
            super.performRefactoring(usages);
            final GrOpenBlock block = constructor.getBlock();
            LOG.assertTrue(block != null);
            final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
            final String text;
            if (StringUtil.equals(selectedValue.getName(), finalParameterName)) {
                text = "this." + selectedValue.getName() + " = " + finalParameterName;
            } else {
                text = selectedValue.getName() + " = " + finalParameterName;
            }
            final GrStatement assignment = factory.createStatementFromText(text);
            final GrStatement statement = block.addStatementBefore(assignment, null);
            final GrReferenceExpression ref = (GrReferenceExpression) ((GrAssignmentExpression) statement).getLValue();
            if (!PsiManager.getInstance(project).areElementsEquivalent(ref.resolve(), selectedValue)) {
                PsiUtil.qualifyMemberReference(ref, selectedValue, selectedValue.getName());
            }
        }
    };
    processor.run();
}
Also used : JavaThrownExceptionInfo(com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo) ArrayList(java.util.ArrayList) DefaultGroovyVariableNameValidator(org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator) GrChangeInfoImpl(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeInfoImpl) GrChangeSignatureProcessor(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeSignatureProcessor) ThrownExceptionInfo(com.intellij.refactoring.changeSignature.ThrownExceptionInfo) JavaThrownExceptionInfo(com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrParameterInfo(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Aggregations

GrParameterInfo (org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo)4 ArrayList (java.util.ArrayList)3 PsiType (com.intellij.psi.PsiType)2 NotNull (org.jetbrains.annotations.NotNull)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)2 GrChangeInfoImpl (org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeInfoImpl)2 PsiElement (com.intellij.psi.PsiElement)1 PsiNamedElement (com.intellij.psi.PsiNamedElement)1 JavaChangeSignatureDialog (com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog)1 JavaThrownExceptionInfo (com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo)1 ParameterInfo (com.intellij.refactoring.changeSignature.ParameterInfo)1 ParameterInfoImpl (com.intellij.refactoring.changeSignature.ParameterInfoImpl)1 ThrownExceptionInfo (com.intellij.refactoring.changeSignature.ThrownExceptionInfo)1 IntroduceParameterObjectClassDescriptor (com.intellij.refactoring.introduceParameterObject.IntroduceParameterObjectClassDescriptor)1 CanonicalTypes (com.intellij.refactoring.util.CanonicalTypes)1 List (java.util.List)1 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1