Search in sources :

Example 6 with PsiParameter

use of com.intellij.psi.PsiParameter in project kotlin by JetBrains.

the class ResourceEvaluator method getResourceTypes.

/**
     * Evaluates the given node and returns the resource types applicable to the
     * node, if any.
     *
     * @param element the element to compute the types for
     * @return the corresponding resource types
     */
@Nullable
public EnumSet<ResourceType> getResourceTypes(@Nullable PsiElement element) {
    if (element == null) {
        return null;
    }
    if (element instanceof PsiConditionalExpression) {
        PsiConditionalExpression expression = (PsiConditionalExpression) element;
        Object known = ConstantEvaluator.evaluate(null, expression.getCondition());
        if (known == Boolean.TRUE && expression.getThenExpression() != null) {
            return getResourceTypes(expression.getThenExpression());
        } else if (known == Boolean.FALSE && expression.getElseExpression() != null) {
            return getResourceTypes(expression.getElseExpression());
        } else {
            EnumSet<ResourceType> left = getResourceTypes(expression.getThenExpression());
            EnumSet<ResourceType> right = getResourceTypes(expression.getElseExpression());
            if (left == null) {
                return right;
            } else if (right == null) {
                return left;
            } else {
                EnumSet<ResourceType> copy = EnumSet.copyOf(left);
                copy.addAll(right);
                return copy;
            }
        }
    } else if (element instanceof PsiParenthesizedExpression) {
        PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression) element;
        return getResourceTypes(parenthesizedExpression.getExpression());
    } else if (element instanceof PsiMethodCallExpression && mAllowDereference) {
        PsiMethodCallExpression call = (PsiMethodCallExpression) element;
        PsiReferenceExpression expression = call.getMethodExpression();
        PsiMethod method = call.resolveMethod();
        if (method != null && method.getContainingClass() != null) {
            EnumSet<ResourceType> types = getTypesFromAnnotations(method);
            if (types != null) {
                return types;
            }
            String qualifiedName = method.getContainingClass().getQualifiedName();
            String name = expression.getReferenceName();
            if ((CLASS_RESOURCES.equals(qualifiedName) || CLASS_CONTEXT.equals(qualifiedName) || CLASS_FRAGMENT.equals(qualifiedName) || CLASS_V4_FRAGMENT.equals(qualifiedName) || CLS_TYPED_ARRAY.equals(qualifiedName)) && name != null && name.startsWith("get")) {
                PsiExpression[] args = call.getArgumentList().getExpressions();
                if (args.length > 0) {
                    types = getResourceTypes(args[0]);
                    if (types != null) {
                        return types;
                    }
                }
            }
        }
    } else if (element instanceof PsiReference) {
        ResourceUrl url = getResourceConstant(element);
        if (url != null) {
            return EnumSet.of(url.type);
        }
        PsiElement resolved = ((PsiReference) element).resolve();
        if (resolved instanceof PsiField) {
            url = getResourceConstant(resolved);
            if (url != null) {
                return EnumSet.of(url.type);
            }
            PsiField field = (PsiField) resolved;
            if (field.getInitializer() != null) {
                return getResourceTypes(field.getInitializer());
            }
            return null;
        } else if (resolved instanceof PsiParameter) {
            return getTypesFromAnnotations((PsiParameter) resolved);
        } else if (resolved instanceof PsiLocalVariable) {
            PsiLocalVariable variable = (PsiLocalVariable) resolved;
            PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiStatement.class, false);
            if (statement != null) {
                PsiStatement prev = PsiTreeUtil.getPrevSiblingOfType(statement, PsiStatement.class);
                String targetName = variable.getName();
                if (targetName == null) {
                    return null;
                }
                while (prev != null) {
                    if (prev instanceof PsiDeclarationStatement) {
                        PsiDeclarationStatement prevStatement = (PsiDeclarationStatement) prev;
                        for (PsiElement e : prevStatement.getDeclaredElements()) {
                            if (variable.equals(e)) {
                                return getResourceTypes(variable.getInitializer());
                            }
                        }
                    } else if (prev instanceof PsiExpressionStatement) {
                        PsiExpression expression = ((PsiExpressionStatement) prev).getExpression();
                        if (expression instanceof PsiAssignmentExpression) {
                            PsiAssignmentExpression assign = (PsiAssignmentExpression) expression;
                            PsiExpression lhs = assign.getLExpression();
                            if (lhs instanceof PsiReferenceExpression) {
                                PsiReferenceExpression reference = (PsiReferenceExpression) lhs;
                                if (targetName.equals(reference.getReferenceName()) && reference.getQualifier() == null) {
                                    return getResourceTypes(assign.getRExpression());
                                }
                            }
                        }
                    }
                    prev = PsiTreeUtil.getPrevSiblingOfType(prev, PsiStatement.class);
                }
            }
        }
    }
    return null;
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiDeclarationStatement(com.intellij.psi.PsiDeclarationStatement) PsiExpression(com.intellij.psi.PsiExpression) PsiMethod(com.intellij.psi.PsiMethod) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) EnumSet(java.util.EnumSet) PsiReference(com.intellij.psi.PsiReference) ResourceType(com.android.resources.ResourceType) PsiAssignmentExpression(com.intellij.psi.PsiAssignmentExpression) PsiLocalVariable(com.intellij.psi.PsiLocalVariable) PsiConditionalExpression(com.intellij.psi.PsiConditionalExpression) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) PsiParameter(com.intellij.psi.PsiParameter) PsiExpressionStatement(com.intellij.psi.PsiExpressionStatement) PsiField(com.intellij.psi.PsiField) PsiParenthesizedExpression(com.intellij.psi.PsiParenthesizedExpression) ResourceUrl(com.android.ide.common.resources.ResourceUrl) PsiElement(com.intellij.psi.PsiElement) Nullable(com.android.annotations.Nullable)

Example 7 with PsiParameter

use of com.intellij.psi.PsiParameter in project kotlin by JetBrains.

the class JavaEvaluator method parametersMatch.

/**
     * Returns true if the given method's parameters are the exact types specified.
     *
     * @param method        the method in question
     * @param argumentTypes the names of the types of the parameters
     * @return true if this method is defined in the given class and with the given parameters
     */
public boolean parametersMatch(@NonNull PsiMethod method, @NonNull String... argumentTypes) {
    PsiParameterList parameterList = method.getParameterList();
    if (parameterList.getParametersCount() != argumentTypes.length) {
        return false;
    }
    PsiParameter[] parameters = parameterList.getParameters();
    for (int i = 0; i < parameters.length; i++) {
        PsiType type = parameters[i].getType();
        if (!type.getCanonicalText().equals(argumentTypes[i])) {
            return false;
        }
    }
    return true;
}
Also used : PsiParameter(com.intellij.psi.PsiParameter) PsiParameterList(com.intellij.psi.PsiParameterList) PsiType(com.intellij.psi.PsiType)

Example 8 with PsiParameter

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

the class ConvertToInstanceMethodDialog method doAction.

protected void doAction() {
    final PsiVariable targetVariable = (PsiVariable) myList.getSelectedValue();
    LOG.assertTrue(targetVariable instanceof PsiParameter, targetVariable);
    final ConvertToInstanceMethodProcessor processor = new ConvertToInstanceMethodProcessor(myMethod.getProject(), myMethod, (PsiParameter) targetVariable, myVisibilityPanel.getVisibility());
    if (!verifyTargetClass(processor.getTargetClass()))
        return;
    invokeRefactoring(processor);
}
Also used : PsiParameter(com.intellij.psi.PsiParameter) PsiVariable(com.intellij.psi.PsiVariable)

Example 9 with PsiParameter

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

the class AnnotateOverriddenMethodParameterFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement psiElement = descriptor.getPsiElement();
    PsiParameter parameter = PsiTreeUtil.getParentOfType(psiElement, PsiParameter.class, false);
    if (parameter == null)
        return;
    PsiMethod method = PsiTreeUtil.getParentOfType(parameter, PsiMethod.class);
    if (method == null)
        return;
    PsiParameter[] parameters = method.getParameterList().getParameters();
    int index = ArrayUtilRt.find(parameters, parameter);
    List<PsiParameter> toAnnotate = new ArrayList<>();
    PsiMethod[] methods = OverridingMethodsSearch.search(method).toArray(PsiMethod.EMPTY_ARRAY);
    for (PsiMethod psiMethod : methods) {
        PsiParameter[] psiParameters = psiMethod.getParameterList().getParameters();
        if (index >= psiParameters.length)
            continue;
        PsiParameter psiParameter = psiParameters[index];
        if (!AnnotationUtil.isAnnotated(psiParameter, myAnnotation, false, false) && psiMethod.getManager().isInProject(psiMethod)) {
            toAnnotate.add(psiParameter);
        }
    }
    FileModificationService.getInstance().preparePsiElementsForWrite(toAnnotate);
    for (PsiParameter psiParam : toAnnotate) {
        assert psiParam != null : toAnnotate;
        if (AnnotationUtil.isAnnotatingApplicable(psiParam, myAnnotation)) {
            AddAnnotationPsiFix fix = new AddAnnotationPsiFix(myAnnotation, psiParam, PsiNameValuePair.EMPTY_ARRAY, myAnnosToRemove);
            fix.invoke(project, psiParam.getContainingFile(), psiParam, psiParam);
        }
    }
}
Also used : AddAnnotationPsiFix(com.intellij.codeInsight.intention.AddAnnotationPsiFix) PsiParameter(com.intellij.psi.PsiParameter) PsiMethod(com.intellij.psi.PsiMethod) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement)

Example 10 with PsiParameter

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

the class ConstructorBodyGeneratorBase method generateSuperCallIfNeeded.

@Override
public void generateSuperCallIfNeeded(@NotNull StringBuilder buffer, @NotNull PsiParameter[] parameters) {
    if (parameters.length > 0) {
        buffer.append("super(");
        for (int j = 0; j < parameters.length; j++) {
            PsiParameter param = parameters[j];
            buffer.append(param.getName());
            if (j < parameters.length - 1)
                buffer.append(",");
        }
        buffer.append(")");
        appendSemicolon(buffer);
        buffer.append("\n");
    }
}
Also used : PsiParameter(com.intellij.psi.PsiParameter)

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