Search in sources :

Example 1 with VariableData

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

the class ExtractMethodDialog method getSignature.

protected String getSignature() {
    @NonNls final StringBuilder buffer = new StringBuilder();
    if (myGenerateAnnotations != null && myGenerateAnnotations.isSelected()) {
        final NullableNotNullManager nullManager = NullableNotNullManager.getInstance(myProject);
        buffer.append("@");
        buffer.append(StringUtil.getShortName(myNullness == Nullness.NULLABLE ? nullManager.getDefaultNullable() : nullManager.getDefaultNotNull()));
        buffer.append("\n");
    }
    final String visibilityString = VisibilityUtil.getVisibilityString(getVisibility());
    buffer.append(visibilityString);
    if (buffer.length() > 0) {
        buffer.append(" ");
    }
    if (isMakeStatic() && !isChainedConstructor()) {
        buffer.append("static ");
    }
    if (myTypeParameterList != null) {
        final String typeParamsText = myTypeParameterList.getText();
        if (!typeParamsText.isEmpty()) {
            buffer.append(typeParamsText);
            buffer.append(" ");
        }
    }
    if (isChainedConstructor()) {
        buffer.append(myTargetClass.getName());
    } else {
        buffer.append(PsiFormatUtil.formatType(mySelector != null ? mySelector.getSelectedType() : myReturnType, 0, PsiSubstitutor.EMPTY));
        buffer.append(" ");
        buffer.append(myNameField.getEnteredName());
    }
    buffer.append("(");
    final String INDENT = StringUtil.repeatSymbol(' ', buffer.length());
    final VariableData[] datas = myInputVariables;
    int count = 0;
    for (int i = 0; i < datas.length; i++) {
        VariableData data = datas[i];
        if (data.passAsParameter) {
            //String typeAndModifiers = PsiFormatUtil.formatVariable(data.variable,
            //  PsiFormatUtil.SHOW_MODIFIERS | PsiFormatUtil.SHOW_TYPE);
            PsiType type = data.type;
            if (i == datas.length - 1 && type instanceof PsiArrayType && myMakeVarargs != null && myMakeVarargs.isSelected()) {
                type = new PsiEllipsisType(((PsiArrayType) type).getComponentType());
            }
            String typeText = type.getPresentableText();
            if (count > 0) {
                buffer.append(",\n");
                buffer.append(INDENT);
            }
            buffer.append(typeText);
            buffer.append(" ");
            buffer.append(data.name);
            count++;
        }
    }
    buffer.append(")");
    if (myExceptions.length > 0) {
        buffer.append("\n");
        buffer.append("throws\n");
        for (PsiType exception : myExceptions) {
            buffer.append(INDENT);
            buffer.append(PsiFormatUtil.formatType(exception, 0, PsiSubstitutor.EMPTY));
            buffer.append("\n");
        }
    }
    return buffer.toString();
}
Also used : NonNls(org.jetbrains.annotations.NonNls) NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager) VariableData(com.intellij.refactoring.util.VariableData)

Example 2 with VariableData

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

the class AnonymousToInnerDialog method getVariableInfos.

public VariableInfo[] getVariableInfos() {
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(myProject);
    VariableInfo[] infos = new VariableInfo[myVariableData.length];
    for (int idx = 0; idx < myVariableData.length; idx++) {
        VariableData data = myVariableData[idx];
        VariableInfo info = myVariableToInfoMap.get(data.variable);
        info.passAsParameter = data.passAsParameter;
        info.parameterName = data.name;
        info.parameterName = data.name;
        String propertyName = codeStyleManager.variableNameToPropertyName(data.name, VariableKind.PARAMETER);
        info.fieldName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.FIELD);
        infos[idx] = info;
    }
    return infos;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) VariableData(com.intellij.refactoring.util.VariableData)

Example 3 with VariableData

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

the class ParametersFolder method foldParameterUsagesInBody.

public void foldParameterUsagesInBody(@NotNull List<VariableData> datum, PsiElement[] elements, SearchScope scope) {
    Map<VariableData, Set<PsiExpression>> equivalentExpressions = new LinkedHashMap<>();
    for (VariableData data : datum) {
        if (myDeleted.contains(data.variable))
            continue;
        final PsiExpression psiExpression = myExpressions.get(data.variable);
        if (psiExpression == null)
            continue;
        final Set<PsiExpression> eqExpressions = new HashSet<>();
        for (PsiReference reference : ReferencesSearch.search(data.variable, scope)) {
            final PsiExpression expression = findEquivalent(psiExpression, reference.getElement());
            if (expression != null && expression.isValid()) {
                eqExpressions.add(expression);
            }
        }
        equivalentExpressions.put(data, eqExpressions);
    }
    for (VariableData data : equivalentExpressions.keySet()) {
        final Set<PsiExpression> eqExpressions = equivalentExpressions.get(data);
        for (PsiExpression expression : eqExpressions) {
            //was replaced on previous step
            if (!expression.isValid())
                continue;
            final PsiExpression refExpression = JavaPsiFacade.getElementFactory(expression.getProject()).createExpressionFromText(data.name, expression);
            final PsiElement replaced = expression.replace(refExpression);
            for (int i = 0, psiElementsLength = elements.length; i < psiElementsLength; i++) {
                PsiElement psiElement = elements[i];
                if (expression == psiElement) {
                    elements[i] = replaced;
                    break;
                }
            }
        }
    }
}
Also used : VariableData(com.intellij.refactoring.util.VariableData)

Example 4 with VariableData

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

the class SuggestedParamTypesTest method doTest.

private void doTest(String... types) throws Exception {
    configureByFile(BASE_PATH + getTestName(false) + ".java");
    final Editor editor = getEditor();
    final PsiFile file = getFile();
    final Project project = getProject();
    int startOffset = editor.getSelectionModel().getSelectionStart();
    int endOffset = editor.getSelectionModel().getSelectionEnd();
    PsiElement[] elements;
    PsiExpression expr = CodeInsightUtil.findExpressionInRange(file, startOffset, endOffset);
    if (expr != null) {
        elements = new PsiElement[] { expr };
    } else {
        elements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset);
    }
    assertTrue(elements.length > 0);
    final ExtractMethodProcessor processor = new ExtractMethodProcessor(project, editor, elements, null, "Extract Method", "newMethod", null);
    processor.prepare();
    for (final VariableData data : processor.getInputVariables().getInputVariables()) {
        final PsiExpression[] occurrences = ParameterTablePanel.findVariableOccurrences(elements, data.variable);
        final TypeSelectorManager manager = new TypeSelectorManagerImpl(project, data.type, occurrences, true) {

            @Override
            protected boolean isUsedAfter() {
                return processor.isOutputVariable(data.variable);
            }
        };
        final JComponent component = manager.getTypeSelector().getComponent();
        if (types.length > 1) {
            assertTrue("One type suggested", component instanceof JComboBox);
            final DefaultComboBoxModel model = (DefaultComboBoxModel) ((JComboBox) component).getModel();
            assertEquals(types.length, model.getSize());
            for (int i = 0, typesLength = types.length; i < typesLength; i++) {
                String type = types[i];
                assertEquals(type, model.getElementAt(i).toString());
            }
        } else if (types.length == 1) {
            assertTrue("Multiple types suggested", component instanceof JLabel);
            assertEquals(types[0], ((JLabel) component).getText());
        }
    }
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) ExtractMethodProcessor(com.intellij.refactoring.extractMethod.ExtractMethodProcessor) VariableData(com.intellij.refactoring.util.VariableData) Project(com.intellij.openapi.project.Project) TypeSelectorManager(com.intellij.refactoring.ui.TypeSelectorManager) TypeSelectorManagerImpl(com.intellij.refactoring.ui.TypeSelectorManagerImpl) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 5 with VariableData

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

the class MakeMethodStaticTest method performWithFields.

private static void performWithFields(boolean delegate) {
    PsiElement element = TargetElementUtil.findTargetElement(myEditor, TargetElementUtil.ELEMENT_NAME_ACCEPTED);
    assertTrue(element instanceof PsiMethod);
    PsiMethod method = (PsiMethod) element;
    final ArrayList<VariableData> parametersForFields = new ArrayList<>();
    final boolean addClassParameter = MakeStaticUtil.buildVariableData(method, parametersForFields);
    new MakeMethodStaticProcessor(getProject(), method, new Settings(true, addClassParameter ? "anObject" : null, parametersForFields.toArray(new VariableData[parametersForFields.size()]), delegate)).run();
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) ArrayList(java.util.ArrayList) MakeMethodStaticProcessor(com.intellij.refactoring.makeStatic.MakeMethodStaticProcessor) VariableData(com.intellij.refactoring.util.VariableData) PsiElement(com.intellij.psi.PsiElement) Settings(com.intellij.refactoring.makeStatic.Settings)

Aggregations

VariableData (com.intellij.refactoring.util.VariableData)20 ArrayList (java.util.ArrayList)4 PsiElement (com.intellij.psi.PsiElement)3 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)2 Settings (com.intellij.refactoring.makeStatic.Settings)2 MultiMap (com.intellij.util.containers.MultiMap)2 THashMap (gnu.trove.THashMap)2 NullableNotNullManager (com.intellij.codeInsight.NullableNotNullManager)1 Editor (com.intellij.openapi.editor.Editor)1 DocumentAdapter (com.intellij.openapi.editor.event.DocumentAdapter)1 DocumentEvent (com.intellij.openapi.editor.event.DocumentEvent)1 Project (com.intellij.openapi.project.Project)1 PsiClass (com.intellij.psi.PsiClass)1 PsiExpression (com.intellij.psi.PsiExpression)1 PsiFile (com.intellij.psi.PsiFile)1 PsiMethod (com.intellij.psi.PsiMethod)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 ParameterInfoImpl (com.intellij.refactoring.changeSignature.ParameterInfoImpl)1 ExtractMethodProcessor (com.intellij.refactoring.extractMethod.ExtractMethodProcessor)1 MakeClassStaticProcessor (com.intellij.refactoring.makeStatic.MakeClassStaticProcessor)1