Search in sources :

Example 16 with PsiParameter

use of com.intellij.psi.PsiParameter in project intellij-community by JetBrains.

the class ConstructorBodyGeneratorBase method start.

@Override
public StringBuilder start(StringBuilder buffer, @NotNull String name, @NotNull PsiParameter[] parameters) {
    buffer.append("public ").append(name).append("(");
    for (PsiParameter parameter : parameters) {
        buffer.append(parameter.getType().getPresentableText()).append(' ').append(parameter.getName()).append(',');
    }
    if (parameters.length > 0) {
        buffer.delete(buffer.length() - 1, buffer.length());
    }
    buffer.append("){\n");
    return buffer;
}
Also used : PsiParameter(com.intellij.psi.PsiParameter)

Example 17 with PsiParameter

use of com.intellij.psi.PsiParameter in project intellij-community by JetBrains.

the class RemoveUnusedParameterFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    final PsiParameter myParameter = (PsiParameter) startElement;
    if (!FileModificationService.getInstance().prepareFileForWrite(myParameter.getContainingFile()))
        return;
    removeReferences(myParameter);
}
Also used : PsiParameter(com.intellij.psi.PsiParameter)

Example 18 with PsiParameter

use of com.intellij.psi.PsiParameter in project intellij-community by JetBrains.

the class IntroduceParameterSettingsUI method createRemoveParamsPanel.

protected void createRemoveParamsPanel(GridBagConstraints gbConstraints, JPanel panel) {
    final JCheckBox[] removeParamsCb = new JCheckBox[myParametersToRemove.length];
    for (int i = 0; i < myParametersToRemove.length; i++) {
        PsiParameter parameter = myParametersToRemove[i];
        if (parameter == null)
            continue;
        final NonFocusableCheckBox cb = new NonFocusableCheckBox(RefactoringBundle.message("remove.parameter.0.no.longer.used", parameter.getName()));
        removeParamsCb[i] = cb;
        cb.setSelected(true);
        gbConstraints.gridy++;
        panel.add(cb, gbConstraints);
        final int i1 = i;
        cb.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                myParametersToRemoveChecked[i1] = cb.isSelected();
            }
        });
        myParametersToRemoveChecked[i] = true;
    }
    updateControls(removeParamsCb);
    if (myCbReplaceAllOccurences != null) {
        myCbReplaceAllOccurences.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                updateControls(removeParamsCb);
            }
        });
    }
}
Also used : ItemEvent(java.awt.event.ItemEvent) PsiParameter(com.intellij.psi.PsiParameter) ChangeEvent(javax.swing.event.ChangeEvent) NonFocusableCheckBox(com.intellij.ui.NonFocusableCheckBox) ChangeListener(javax.swing.event.ChangeListener) ItemListener(java.awt.event.ItemListener)

Example 19 with PsiParameter

use of com.intellij.psi.PsiParameter in project intellij-community by JetBrains.

the class NullParameterConstraintChecker method checkMethodParameters.

@NotNull
static PsiParameter[] checkMethodParameters(PsiMethod method) {
    if (method.getBody() == null)
        return PsiParameter.EMPTY_ARRAY;
    final Collection<PsiParameter> nullableParameters = new SmartList<>();
    final PsiParameter[] parameters = method.getParameterList().getParameters();
    for (int index = 0; index < parameters.length; index++) {
        PsiParameter parameter = parameters[index];
        if (!(parameter.getType() instanceof PsiPrimitiveType) && !NullableNotNullManager.isNotNull(parameter) && !NullableNotNullManager.isNullable(parameter) && JavaNullMethodArgumentUtil.hasNullArgument(method, index)) {
            nullableParameters.add(parameter);
        }
    }
    if (nullableParameters.isEmpty())
        return PsiParameter.EMPTY_ARRAY;
    final NullParameterConstraintChecker checker = new NullParameterConstraintChecker(nullableParameters);
    checker.analyzeMethod(method.getBody(), new StandardInstructionVisitor());
    return checker.myPossiblyViolatedParameters.stream().filter(checker.myUsedParameters::contains).toArray(PsiParameter[]::new);
}
Also used : PsiPrimitiveType(com.intellij.psi.PsiPrimitiveType) PsiParameter(com.intellij.psi.PsiParameter) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with PsiParameter

use of com.intellij.psi.PsiParameter in project intellij-community by JetBrains.

the class GrSetStrongTypeIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, final Editor editor) throws IncorrectOperationException {
    PsiElement parent = element.getParent();
    PsiElement elementToBuildTemplate;
    GrVariable[] variables;
    if (parent instanceof GrVariable && parent.getParent() instanceof GrVariableDeclaration) {
        variables = ((GrVariableDeclaration) parent.getParent()).getVariables();
        elementToBuildTemplate = parent.getParent();
    } else if (parent instanceof GrVariable && parent.getParent() instanceof GrForInClause) {
        variables = new GrVariable[] { (GrVariable) parent };
        elementToBuildTemplate = parent.getParent().getParent();
    } else if (parent instanceof GrVariableDeclaration) {
        variables = ((GrVariableDeclaration) parent).getVariables();
        elementToBuildTemplate = parent;
    } else if (parent instanceof GrParameter && parent.getParent() instanceof GrParameterList) {
        variables = new GrVariable[] { (GrVariable) parent };
        elementToBuildTemplate = parent.getParent().getParent();
    } else if (parent instanceof GrVariable) {
        variables = new GrVariable[] { ((GrVariable) parent) };
        elementToBuildTemplate = parent;
    } else {
        return;
    }
    ArrayList<TypeConstraint> types = new ArrayList<>();
    if (parent.getParent() instanceof GrForInClause) {
        types.add(SupertypeConstraint.create(PsiUtil.extractIteratedType((GrForInClause) parent.getParent())));
    } else {
        for (GrVariable variable : variables) {
            GrExpression initializer = variable.getInitializerGroovy();
            if (initializer != null) {
                PsiType type = initializer.getType();
                if (type != null) {
                    types.add(SupertypeConstraint.create(type));
                }
            }
            if (variable instanceof GrParameter) {
                final PsiParameter parameter = (PsiParameter) variable;
                final PsiType type = getClosureParameterType(parameter);
                if (type != null) {
                    types.add(SupertypeConstraint.create(type));
                }
            }
        }
    }
    final String originalText = elementToBuildTemplate.getText();
    final TypeInfo typeInfo = getOrCreateTypeElement(parent, elementToBuildTemplate);
    final PsiElement replaceElement = typeInfo.elementToReplace;
    TypeConstraint[] constraints = types.toArray(new TypeConstraint[types.size()]);
    ChooseTypeExpression chooseTypeExpression = new ChooseTypeExpression(constraints, element.getManager(), replaceElement.getResolveScope());
    TemplateBuilderImpl builder = new TemplateBuilderImpl(elementToBuildTemplate);
    builder.replaceElement(replaceElement, chooseTypeExpression);
    final Document document = editor.getDocument();
    final RangeMarker rangeMarker = document.createRangeMarker(elementToBuildTemplate.getTextRange());
    rangeMarker.setGreedyToRight(true);
    rangeMarker.setGreedyToLeft(true);
    final PsiElement afterPostprocess = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(elementToBuildTemplate);
    final Template template = builder.buildTemplate();
    TextRange range = afterPostprocess.getTextRange();
    document.deleteString(range.getStartOffset(), range.getEndOffset());
    TemplateManager templateManager = TemplateManager.getInstance(project);
    templateManager.startTemplate(editor, template, new TemplateEditingAdapter() {

        @Override
        public void templateFinished(Template template, boolean brokenOff) {
            if (brokenOff) {
                ApplicationManager.getApplication().runWriteAction(() -> {
                    if (rangeMarker.isValid()) {
                        document.replaceString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), originalText);
                        editor.getCaretModel().moveToOffset(rangeMarker.getStartOffset() + typeInfo.originalOffset);
                    }
                });
            }
        }
    });
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) ArrayList(java.util.ArrayList) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Document(com.intellij.openapi.editor.Document) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) Template(com.intellij.codeInsight.template.Template) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) TemplateManager(com.intellij.codeInsight.template.TemplateManager) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) PsiParameter(com.intellij.psi.PsiParameter) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint)

Aggregations

PsiParameter (com.intellij.psi.PsiParameter)26 PsiMethod (com.intellij.psi.PsiMethod)6 PsiElement (com.intellij.psi.PsiElement)5 PsiType (com.intellij.psi.PsiType)5 ArrayList (java.util.ArrayList)5 PsiParameterList (com.intellij.psi.PsiParameterList)4 Nullable (org.jetbrains.annotations.Nullable)3 ClassMember (com.intellij.codeInsight.generation.ClassMember)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiField (com.intellij.psi.PsiField)2 PsiPrimitiveType (com.intellij.psi.PsiPrimitiveType)2 PsiVariable (com.intellij.psi.PsiVariable)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (com.android.annotations.Nullable)1 ResourceUrl (com.android.ide.common.resources.ResourceUrl)1 ResourceType (com.android.resources.ResourceType)1 Location (com.android.tools.klint.detector.api.Location)1 JavaEvaluator (com.android.tools.lint.client.api.JavaEvaluator)1 AddAnnotationPsiFix (com.intellij.codeInsight.intention.AddAnnotationPsiFix)1