Search in sources :

Example 1 with MethodJavaDocHelper

use of com.intellij.refactoring.util.javadoc.MethodJavaDocHelper in project intellij-community by JetBrains.

the class GroovyIntroduceParameterMethodUsagesProcessor method processChangeMethodSignature.

@Override
public boolean processChangeMethodSignature(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
    if (!(usage.getElement() instanceof GrMethod) || !isGroovyUsage(usage))
        return true;
    GrMethod method = (GrMethod) usage.getElement();
    final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(data.getParameterName(), method.getBlock());
    final MethodJavaDocHelper javaDocHelper = new MethodJavaDocHelper(method);
    final PsiParameter[] parameters = method.getParameterList().getParameters();
    data.getParametersToRemove().forEachDescending(new TIntProcedure() {

        @Override
        public boolean execute(final int paramNum) {
            try {
                PsiParameter param = parameters[paramNum];
                PsiDocTag tag = javaDocHelper.getTagForParameter(param);
                if (tag != null) {
                    tag.delete();
                }
                param.delete();
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
            return true;
        }
    });
    addParameter(method, javaDocHelper, data.getForcedType(), data.getParameterName(), data.isDeclareFinal(), data.getProject());
    fieldConflictsResolver.fix();
    return false;
}
Also used : MethodJavaDocHelper(com.intellij.refactoring.util.javadoc.MethodJavaDocHelper) TIntProcedure(gnu.trove.TIntProcedure) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 2 with MethodJavaDocHelper

use of com.intellij.refactoring.util.javadoc.MethodJavaDocHelper in project intellij-community by JetBrains.

the class JavaIntroduceParameterMethodUsagesProcessor method processChangeMethodSignature.

public boolean processChangeMethodSignature(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
    if (!(usage.getElement() instanceof PsiMethod) || !isJavaUsage(usage))
        return true;
    PsiMethod method = (PsiMethod) usage.getElement();
    final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(data.getParameterName(), method.getBody());
    final MethodJavaDocHelper javaDocHelper = new MethodJavaDocHelper(method);
    PsiElementFactory factory = JavaPsiFacade.getInstance(data.getProject()).getElementFactory();
    final PsiClass superClass = data.getMethodToSearchFor().getContainingClass();
    final PsiClass containingClass = method.getContainingClass();
    final PsiSubstitutor substitutor = superClass != null && containingClass != null ? TypeConversionUtil.getSuperClassSubstitutor(superClass, containingClass, PsiSubstitutor.EMPTY) : PsiSubstitutor.EMPTY;
    PsiParameter parameter = factory.createParameter(data.getParameterName(), substitutor.substitute(data.getForcedType()));
    PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, data.isDeclareFinal());
    final PsiParameterList parameterList = method.getParameterList();
    final PsiParameter[] parameters = parameterList.getParameters();
    data.getParametersToRemove().forEachDescending(new TIntProcedure() {

        public boolean execute(final int paramNum) {
            try {
                PsiParameter param = parameters[paramNum];
                PsiDocTag tag = javaDocHelper.getTagForParameter(param);
                if (tag != null) {
                    tag.delete();
                }
                param.delete();
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
            return true;
        }
    });
    final PsiParameter anchorParameter = getAnchorParameter(method);
    parameter = (PsiParameter) parameterList.addAfter(parameter, anchorParameter);
    JavaCodeStyleManager.getInstance(data.getProject()).shortenClassReferences(parameter);
    final PsiDocTag tagForAnchorParameter = javaDocHelper.getTagForParameter(anchorParameter);
    javaDocHelper.addParameterAfter(data.getParameterName(), tagForAnchorParameter);
    fieldConflictsResolver.fix();
    return false;
}
Also used : TIntProcedure(gnu.trove.TIntProcedure) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) MethodJavaDocHelper(com.intellij.refactoring.util.javadoc.MethodJavaDocHelper) FieldConflictsResolver(com.intellij.refactoring.util.FieldConflictsResolver) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 3 with MethodJavaDocHelper

use of com.intellij.refactoring.util.javadoc.MethodJavaDocHelper 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 4 with MethodJavaDocHelper

use of com.intellij.refactoring.util.javadoc.MethodJavaDocHelper in project intellij-community by JetBrains.

the class MakeClassStaticProcessor method changeSelf.

protected void changeSelf(final PsiElementFactory factory, final UsageInfo[] usages) throws IncorrectOperationException {
    PsiClass containingClass = myMember.getContainingClass();
    //Add fields
    if (mySettings.isMakeClassParameter()) {
        PsiType type = factory.createType(containingClass, PsiSubstitutor.EMPTY);
        final String classParameterName = mySettings.getClassParameterName();
        final String fieldName = convertToFieldName(classParameterName);
        myMember.add(factory.createField(fieldName, type));
    }
    if (mySettings.isMakeFieldParameters()) {
        List<Settings.FieldParameter> parameters = mySettings.getParameterOrderList();
        for (Settings.FieldParameter fieldParameter : parameters) {
            final PsiType type = fieldParameter.type;
            final PsiField field = factory.createField(convertToFieldName(fieldParameter.name), type);
            myMember.add(field);
        }
    }
    PsiMethod[] constructors = myMember.getConstructors();
    if (constructors.length == 0) {
        final PsiMethod defConstructor = (PsiMethod) myMember.add(factory.createConstructor());
        constructors = new PsiMethod[] { defConstructor };
    }
    boolean generateFinalParams = CodeStyleSettingsManager.getSettings(myProject).GENERATE_FINAL_PARAMETERS;
    for (PsiMethod constructor : constructors) {
        final MethodJavaDocHelper javaDocHelper = new MethodJavaDocHelper(constructor);
        PsiParameterList paramList = constructor.getParameterList();
        PsiElement addParameterAfter = null;
        PsiDocTag anchor = null;
        if (mySettings.isMakeClassParameter()) {
            // Add parameter for object
            PsiType parameterType = factory.createType(containingClass, PsiSubstitutor.EMPTY);
            final String classParameterName = mySettings.getClassParameterName();
            PsiParameter parameter = factory.createParameter(classParameterName, parameterType);
            PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, makeClassParameterFinal(usages) || generateFinalParams);
            addParameterAfter = paramList.addAfter(parameter, null);
            anchor = javaDocHelper.addParameterAfter(classParameterName, anchor);
            addAssignmentToField(classParameterName, constructor);
        }
        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);
                PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, makeFieldParameterFinal(fieldParameter.field, usages) || generateFinalParams);
                addParameterAfter = paramList.addAfter(parameter, addParameterAfter);
                anchor = javaDocHelper.addParameterAfter(fieldParameter.name, anchor);
                addAssignmentToField(fieldParameter.name, constructor);
            }
            for (UsageInfo usage : usages) {
                if (usage instanceof InternalUsageInfo) {
                    final PsiElement element = usage.getElement();
                    final PsiElement referencedElement = ((InternalUsageInfo) usage).getReferencedElement();
                    if (referencedElement instanceof PsiField && mySettings.getNameForField((PsiField) referencedElement) != null) {
                        final PsiField field = PsiTreeUtil.getParentOfType(element, PsiField.class);
                        if (field != null) {
                            MoveInstanceMembersUtil.moveInitializerToConstructor(factory, constructor, field);
                        }
                    }
                }
            }
        }
        for (PsiField field : myFieldsToSplit) {
            MoveInstanceMembersUtil.moveInitializerToConstructor(factory, constructor, field);
        }
    }
    setupTypeParameterList(myMember);
    // Add static modifier
    final PsiModifierList modifierList = myMember.getModifierList();
    modifierList.setModifierProperty(PsiModifier.STATIC, true);
    modifierList.setModifierProperty(PsiModifier.FINAL, false);
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) MethodJavaDocHelper(com.intellij.refactoring.util.javadoc.MethodJavaDocHelper) UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)4 MethodJavaDocHelper (com.intellij.refactoring.util.javadoc.MethodJavaDocHelper)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 TIntProcedure (gnu.trove.TIntProcedure)2 JavaChangeInfoImpl (com.intellij.refactoring.changeSignature.JavaChangeInfoImpl)1 ParameterInfoImpl (com.intellij.refactoring.changeSignature.ParameterInfoImpl)1 ThrownExceptionInfo (com.intellij.refactoring.changeSignature.ThrownExceptionInfo)1 FieldConflictsResolver (com.intellij.refactoring.util.FieldConflictsResolver)1 UsageInfo (com.intellij.usageView.UsageInfo)1 ArrayList (java.util.ArrayList)1 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)1