Search in sources :

Example 1 with ParameterInfoImpl

use of com.intellij.refactoring.changeSignature.ParameterInfoImpl 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 ParameterInfoImpl

use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.

the class IntroduceParameterObjectDialog method createClassDescriptor.

@Override
protected JavaIntroduceParameterObjectClassDescriptor createClassDescriptor() {
    final boolean useExistingClass = useExistingClass();
    final String className;
    final String packageName;
    final boolean createInnerClass = myCreateInnerClassRadioButton.isSelected();
    if (createInnerClass) {
        className = getInnerClassName();
        packageName = "";
    } else if (useExistingClass) {
        final String existingClassName = getExistingClassName();
        className = StringUtil.getShortName(existingClassName);
        packageName = StringUtil.getPackageName(existingClassName);
    } else {
        packageName = getPackageName();
        className = getClassName();
    }
    final String newVisibility = myEscalateVisibilityCheckBox.isEnabled() && myEscalateVisibilityCheckBox.isSelected() ? VisibilityUtil.ESCALATE_VISIBILITY : null;
    final MoveDestination moveDestination = ((DestinationFolderComboBox) myDestinationCb).selectDirectory(new PackageWrapper(PsiManager.getInstance(myProject), packageName), false);
    final PsiParameterList parameterList = mySourceMethod.getParameterList();
    final List<ParameterInfoImpl> parameters = new ArrayList<>();
    for (VariableData data : myParameterTablePanel.getVariableData()) {
        if (data.passAsParameter) {
            parameters.add(new ParameterInfoImpl(parameterList.getParameterIndex((PsiParameter) data.variable), data.name, data.type));
        }
    }
    final ParameterInfoImpl[] infos = parameters.toArray(new ParameterInfoImpl[parameters.size()]);
    return new JavaIntroduceParameterObjectClassDescriptor(className, packageName, moveDestination, useExistingClass, createInnerClass, newVisibility, infos, mySourceMethod, myGenerateAccessorsCheckBox.isSelected());
}
Also used : ArrayList(java.util.ArrayList) DestinationFolderComboBox(com.intellij.refactoring.move.moveClassesOrPackages.DestinationFolderComboBox) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) VariableData(com.intellij.refactoring.util.VariableData)

Example 3 with ParameterInfoImpl

use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.

the class JavaIntroduceParameterObjectClassDescriptor method findCompatibleConstructor.

@Nullable
private PsiMethod findCompatibleConstructor(@NotNull PsiClass aClass) {
    ParameterInfoImpl[] paramsToMerge = getParamsToMerge();
    if (paramsToMerge.length == 1) {
        final ParameterInfoImpl parameterInfo = paramsToMerge[0];
        final PsiType paramType = parameterInfo.getTypeWrapper().getType(aClass);
        if (TypeConversionUtil.isPrimitiveWrapper(aClass.getQualifiedName())) {
            ParameterBean bean = new ParameterBean();
            bean.setField(aClass.findFieldByName("value", false));
            bean.setGetter(paramType.getCanonicalText() + "Value");
            myExistingClassProperties.put(parameterInfo, bean);
            for (PsiMethod constructor : aClass.getConstructors()) {
                if (isConstructorCompatible(constructor, new ParameterInfoImpl[] { parameterInfo }, aClass))
                    return constructor;
            }
        }
    }
    final PsiMethod[] constructors = aClass.getConstructors();
    PsiMethod compatibleConstructor = null;
    for (PsiMethod constructor : constructors) {
        if (isConstructorCompatible(constructor, paramsToMerge, aClass)) {
            compatibleConstructor = constructor;
            break;
        }
    }
    PsiField[] fields = aClass.getFields();
    if (compatibleConstructor == null && !areTypesCompatible(getParamsToMerge(), fields, aClass)) {
        return null;
    }
    final PsiVariable[] constructorParams = compatibleConstructor != null ? compatibleConstructor.getParameterList().getParameters() : fields;
    for (int i = 0; i < getParamsToMerge().length; i++) {
        final int oldIndex = getParamsToMerge()[i].getOldIndex();
        final ParameterInfoImpl methodParam = getParameterInfo(oldIndex);
        final ParameterBean bean = new ParameterBean();
        myExistingClassProperties.put(methodParam, bean);
        final PsiVariable var = constructorParams[i];
        final PsiField field = var instanceof PsiParameter ? findFieldAssigned((PsiParameter) var, compatibleConstructor) : (PsiField) var;
        if (field == null) {
            return null;
        }
        bean.setField(field);
        final PsiMethod getterForField = PropertyUtil.findGetterForField(field);
        if (getterForField != null) {
            bean.setGetter(getterForField.getName());
        }
        final PsiMethod setterForField = PropertyUtil.findSetterForField(field);
        if (setterForField != null) {
            bean.setSetter(setterForField.getName());
        }
    }
    return compatibleConstructor;
}
Also used : ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ParameterInfoImpl

use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.

the class JavaIntroduceParameterObjectClassDescriptor method createClass.

@Override
public PsiClass createClass(PsiMethod method, ReadWriteAccessDetector.Access[] accessors) {
    if (isUseExistingClass()) {
        return getExistingClass();
    }
    final ParameterObjectBuilder beanClassBuilder = new ParameterObjectBuilder();
    beanClassBuilder.setVisibility(isCreateInnerClass() ? PsiModifier.PRIVATE : PsiModifier.PUBLIC);
    beanClassBuilder.setProject(method.getProject());
    beanClassBuilder.setTypeArguments(getTypeParameters());
    beanClassBuilder.setClassName(getClassName());
    beanClassBuilder.setPackageName(getPackageName());
    PsiParameter[] parameters = method.getParameterList().getParameters();
    final ParameterInfoImpl[] parameterInfos = getParamsToMerge();
    for (int i = 0; i < parameterInfos.length; i++) {
        PsiParameter parameter = parameters[parameterInfos[i].getOldIndex()];
        final boolean setterRequired = accessors[i] == ReadWriteAccessDetector.Access.Write;
        final String newName = parameterInfos[i].getName();
        beanClassBuilder.addField(parameter, newName, parameterInfos[i].getTypeWrapper().getType(method), setterRequired);
    }
    final String classString = beanClassBuilder.buildBeanClass();
    try {
        final PsiFileFactory factory = PsiFileFactory.getInstance(method.getProject());
        final PsiJavaFile newFile = (PsiJavaFile) factory.createFileFromText(getClassName() + ".java", JavaFileType.INSTANCE, classString);
        if (isCreateInnerClass()) {
            final PsiClass containingClass = method.getContainingClass();
            final PsiClass[] classes = newFile.getClasses();
            assert classes.length > 0 : classString;
            final PsiClass innerClass = (PsiClass) containingClass.add(classes[0]);
            PsiUtil.setModifierProperty(innerClass, PsiModifier.STATIC, true);
            return (PsiClass) JavaCodeStyleManager.getInstance(newFile.getProject()).shortenClassReferences(innerClass);
        } else {
            final PsiFile containingFile = method.getContainingFile();
            final PsiDirectory containingDirectory = containingFile.getContainingDirectory();
            final PsiDirectory directory;
            final MoveDestination moveDestination = getMoveDestination();
            if (moveDestination != null) {
                directory = moveDestination.getTargetDirectory(containingDirectory);
            } else {
                final Module module = ModuleUtilCore.findModuleForPsiElement(containingFile);
                directory = PackageUtil.findOrCreateDirectoryForPackage(module, getPackageName(), containingDirectory, true, true);
            }
            if (directory != null) {
                PsiFile file = directory.findFile(newFile.getName());
                if (file == null) {
                    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(method.getManager().getProject());
                    final PsiElement shortenedFile = JavaCodeStyleManager.getInstance(newFile.getProject()).shortenClassReferences(newFile);
                    final PsiElement reformattedFile = codeStyleManager.reformat(shortenedFile);
                    file = (PsiFile) directory.add(reformattedFile);
                }
                return ((PsiJavaFile) file).getClasses()[0];
            }
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return null;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) MoveDestination(com.intellij.refactoring.MoveDestination) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module)

Example 5 with ParameterInfoImpl

use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.

the class ChangeSignatureBaseTest method doTest.

protected void doTest(@Nullable String returnType, @Nullable final String[] parameters, @Nullable final String[] exceptions, boolean delegate) {
    GenParams genParams = parameters == null ? new SimpleParameterGen() : method -> {
        ParameterInfoImpl[] parameterInfos = new ParameterInfoImpl[parameters.length];
        for (int i = 0; i < parameters.length; i++) {
            PsiType type = myFactory.createTypeFromText(parameters[i], method);
            parameterInfos[i] = new ParameterInfoImpl(-1, "p" + (i + 1), type);
        }
        return parameterInfos;
    };
    GenExceptions genExceptions = exceptions == null ? new SimpleExceptionsGen() : method -> {
        ThrownExceptionInfo[] exceptionInfos = new ThrownExceptionInfo[exceptions.length];
        for (int i = 0; i < exceptions.length; i++) {
            PsiType type = myFactory.createTypeFromText(exceptions[i], method);
            exceptionInfos[i] = new JavaThrownExceptionInfo(-1, (PsiClassType) type);
        }
        return exceptionInfos;
    };
    doTest(null, null, returnType, genParams, genExceptions, delegate);
}
Also used : JavaThrownExceptionInfo(com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) ThrownExceptionInfo(com.intellij.refactoring.changeSignature.ThrownExceptionInfo) JavaThrownExceptionInfo(com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo)

Aggregations

ParameterInfoImpl (com.intellij.refactoring.changeSignature.ParameterInfoImpl)34 ArrayList (java.util.ArrayList)10 ChangeSignatureProcessor (com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)6 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)5 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)4 JavaChangeSignatureDialog (com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog)4 HashSet (java.util.HashSet)3 JavaChangeInfoImpl (com.intellij.refactoring.changeSignature.JavaChangeInfoImpl)2 JavaMethodDescriptor (com.intellij.refactoring.changeSignature.JavaMethodDescriptor)2 ThrownExceptionInfo (com.intellij.refactoring.changeSignature.ThrownExceptionInfo)2 IntroduceParameterObjectProcessor (com.intellij.refactoring.introduceParameterObject.IntroduceParameterObjectProcessor)2 JavaIntroduceParameterObjectClassDescriptor (com.intellij.refactoring.introduceparameterobject.JavaIntroduceParameterObjectClassDescriptor)2 UsageInfo (com.intellij.usageView.UsageInfo)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)1 FindUsagesHandler (com.intellij.find.findUsages.FindUsagesHandler)1 FindUsagesManager (com.intellij.find.findUsages.FindUsagesManager)1 JavaMethodFindUsagesOptions (com.intellij.find.findUsages.JavaMethodFindUsagesOptions)1 FindManagerImpl (com.intellij.find.impl.FindManagerImpl)1