Search in sources :

Example 1 with ThrownExceptionInfo

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

Example 2 with ThrownExceptionInfo

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

the class MakeMethodStaticProcessor method changeSelf.

protected void changeSelf(PsiElementFactory factory, UsageInfo[] usages) throws IncorrectOperationException {
    final MethodJavaDocHelper javaDocHelper = new MethodJavaDocHelper(myMember);
    PsiParameterList paramList = myMember.getParameterList();
    PsiElement addParameterAfter = null;
    PsiDocTag anchor = null;
    List<PsiType> addedTypes = new ArrayList<>();
    final PsiClass containingClass = myMember.getContainingClass();
    LOG.assertTrue(containingClass != null);
    if (mySettings.isDelegate()) {
        List<ParameterInfoImpl> params = new ArrayList<>();
        PsiParameter[] parameters = myMember.getParameterList().getParameters();
        if (mySettings.isMakeClassParameter()) {
            params.add(new ParameterInfoImpl(-1, mySettings.getClassParameterName(), factory.createType(containingClass, PsiSubstitutor.EMPTY), "this"));
        }
        if (mySettings.isMakeFieldParameters()) {
            for (Settings.FieldParameter parameter : mySettings.getParameterOrderList()) {
                params.add(new ParameterInfoImpl(-1, mySettings.getClassParameterName(), parameter.type, parameter.field.getName()));
            }
        }
        for (int i = 0; i < parameters.length; i++) {
            params.add(new ParameterInfoImpl(i));
        }
        final PsiType returnType = myMember.getReturnType();
        LOG.assertTrue(returnType != null);
        JavaChangeSignatureUsageProcessor.generateDelegate(new JavaChangeInfoImpl(VisibilityUtil.getVisibilityModifier(myMember.getModifierList()), myMember, myMember.getName(), CanonicalTypes.createTypeWrapper(returnType), params.toArray(new ParameterInfoImpl[params.size()]), new ThrownExceptionInfo[0], false, Collections.<PsiMethod>emptySet(), Collections.<PsiMethod>emptySet()));
    }
    if (mySettings.isMakeClassParameter()) {
        // Add parameter for object
        PsiType parameterType = factory.createType(containingClass, PsiSubstitutor.EMPTY);
        addedTypes.add(parameterType);
        final String classParameterName = mySettings.getClassParameterName();
        PsiParameter parameter = factory.createParameter(classParameterName, parameterType);
        if (makeClassParameterFinal(usages)) {
            PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, true);
        }
        addParameterAfter = paramList.addAfter(parameter, null);
        anchor = javaDocHelper.addParameterAfter(classParameterName, anchor);
    }
    if (mySettings.isMakeFieldParameters()) {
        List<Settings.FieldParameter> parameters = mySettings.getParameterOrderList();
        for (Settings.FieldParameter fieldParameter : parameters) {
            final PsiType fieldParameterType = fieldParameter.field.getType();
            final PsiParameter parameter = factory.createParameter(fieldParameter.name, fieldParameterType);
            addedTypes.add(fieldParameterType);
            if (makeFieldParameterFinal(fieldParameter.field, usages)) {
                PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, true);
            }
            addParameterAfter = paramList.addAfter(parameter, addParameterAfter);
            anchor = javaDocHelper.addParameterAfter(fieldParameter.name, anchor);
        }
    }
    makeStatic(myMember);
    if (myAdditionalMethods != null) {
        for (PsiMethod method : myAdditionalMethods) {
            makeStatic(method);
        }
    }
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) ArrayList(java.util.ArrayList) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) ThrownExceptionInfo(com.intellij.refactoring.changeSignature.ThrownExceptionInfo) MethodJavaDocHelper(com.intellij.refactoring.util.javadoc.MethodJavaDocHelper) JavaChangeInfoImpl(com.intellij.refactoring.changeSignature.JavaChangeInfoImpl)

Example 3 with ThrownExceptionInfo

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

the class ChangeSignaturePropagationTest method generateExceptionInfos.

private static ThrownExceptionInfo[] generateExceptionInfos(PsiMethod method, ThrownExceptionInfo[] newExceptions) {
    final PsiClassType[] exceptions = method.getThrowsList().getReferencedTypes();
    ThrownExceptionInfo[] result = new ThrownExceptionInfo[exceptions.length + newExceptions.length];
    for (int i = 0; i < exceptions.length; i++) {
        result[i] = new JavaThrownExceptionInfo(i);
    }
    System.arraycopy(newExceptions, 0, result, exceptions.length, newExceptions.length);
    return result;
}
Also used : JavaThrownExceptionInfo(com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo) ThrownExceptionInfo(com.intellij.refactoring.changeSignature.ThrownExceptionInfo) JavaThrownExceptionInfo(com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo)

Example 4 with ThrownExceptionInfo

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

the class GrChangeSignatureDialog method validateAndCommitData.

@Nullable
@Override
protected String validateAndCommitData() {
    if (myReturnTypeCodeFragment != null && !checkType((PsiTypeCodeFragment) myReturnTypeCodeFragment, true)) {
        return GroovyRefactoringBundle.message("return.type.is.wrong");
    }
    List<GrParameterTableModelItem> parameterInfos = myParametersTableModel.getItems();
    int newParameterCount = parameterInfos.size();
    for (int i = 0; i < newParameterCount; i++) {
        GrParameterTableModelItem item = parameterInfos.get(i);
        String name = item.parameter.getName();
        if (!StringUtil.isJavaIdentifier(name)) {
            return GroovyRefactoringBundle.message("name.is.wrong", name);
        }
        if (!checkType((PsiTypeCodeFragment) item.typeCodeFragment, i == newParameterCount - 1)) {
            return GroovyRefactoringBundle.message("type.for.parameter.is.incorrect", name);
        }
        try {
            item.parameter.setType(((PsiTypeCodeFragment) item.typeCodeFragment).getType());
        } catch (PsiTypeCodeFragment.TypeSyntaxException e) {
            LOG.error(e);
        } catch (PsiTypeCodeFragment.NoTypeException e) {
            item.parameter.setType(null);
        }
        String defaultValue = item.defaultValueCodeFragment.getText();
        final String initializer = item.initializerCodeFragment.getText();
        if (item.parameter.getOldIndex() < 0 && defaultValue.trim().isEmpty() && initializer.trim().isEmpty()) {
            return GroovyRefactoringBundle.message("specify.default.value", name);
        }
        item.parameter.setInitializer(initializer);
        item.parameter.setDefaultValue(defaultValue);
    }
    ThrownExceptionInfo[] exceptionInfos = myExceptionsModel.getThrownExceptions();
    PsiTypeCodeFragment[] typeCodeFragments = myExceptionsModel.getTypeCodeFragments();
    for (int i = 0; i < exceptionInfos.length; i++) {
        ThrownExceptionInfo exceptionInfo = exceptionInfos[i];
        PsiTypeCodeFragment typeCodeFragment = typeCodeFragments[i];
        try {
            PsiType type = typeCodeFragment.getType();
            if (!(type instanceof PsiClassType)) {
                return GroovyRefactoringBundle.message("changeSignature.wrong.type.for.exception", typeCodeFragment.getText());
            }
            PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
            PsiClassType throwable = factory.createTypeByFQClassName("java.lang.Throwable", myMethod.getMethod().getResolveScope());
            if (!throwable.isAssignableFrom(type)) {
                return GroovyRefactoringBundle.message("changeSignature.not.throwable.type", typeCodeFragment.getText());
            }
            exceptionInfo.setType((PsiClassType) type);
        } catch (PsiTypeCodeFragment.TypeSyntaxException e) {
            return GroovyRefactoringBundle.message("changeSignature.wrong.type.for.exception", typeCodeFragment.getText());
        } catch (PsiTypeCodeFragment.NoTypeException e) {
            return GroovyRefactoringBundle.message("changeSignature.no.type.for.exception");
        }
    }
    return null;
}
Also used : ThrownExceptionInfo(com.intellij.refactoring.changeSignature.ThrownExceptionInfo) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with ThrownExceptionInfo

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

ThrownExceptionInfo (com.intellij.refactoring.changeSignature.ThrownExceptionInfo)5 JavaThrownExceptionInfo (com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo)3 ParameterInfoImpl (com.intellij.refactoring.changeSignature.ParameterInfoImpl)2 ArrayList (java.util.ArrayList)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)1 JavaChangeInfoImpl (com.intellij.refactoring.changeSignature.JavaChangeInfoImpl)1 MethodJavaDocHelper (com.intellij.refactoring.util.javadoc.MethodJavaDocHelper)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)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 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)1 DefaultGroovyVariableNameValidator (org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator)1 GrChangeInfoImpl (org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeInfoImpl)1 GrChangeSignatureProcessor (org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeSignatureProcessor)1 GrParameterInfo (org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo)1