Search in sources :

Example 41 with PsiExpression

use of com.intellij.psi.PsiExpression in project oxy-template-support-plugin by mutant-industries.

the class LiteralJsMacroReferenceContributor method registerReferenceProviders.

@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
    registrar.registerReferenceProvider(PlatformPatterns.psiElement(PsiLiteralExpression.class), new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            PsiLiteralExpression literalExpression = (PsiLiteralExpression) element;
            PsiMethodCallExpression callExpression;
            PsiNewExpression newMacroCallExpression;
            if (!(literalExpression.getValue() instanceof String)) {
                return PsiReference.EMPTY_ARRAY;
            }
            if ((newMacroCallExpression = PsiTreeUtil.getParentOfType(literalExpression, PsiNewExpressionImpl.class)) != null && newMacroCallExpression.getClassReference() != null) {
                if (MacroCall.class.getName().equals(newMacroCallExpression.getClassReference().getQualifiedName())) {
                    return new MacroReferenceSet(literalExpression).getAllReferences();
                }
            } else if ((callExpression = PsiTreeUtil.getParentOfType(literalExpression, PsiMethodCallExpression.class)) != null) {
                PsiExpression[] parameters = callExpression.getArgumentList().getExpressions();
                PsiReferenceExpression expression = callExpression.getMethodExpression();
                String callText = expression.getText();
                if (// TODO type check...
                callText.contains("ageUpdater.update")) {
                    if (parameters.length > 0 && literalExpression.isEquivalentTo(parameters[0])) {
                    // TODO template path reference
                    } else if (parameters.length > 1 && literalExpression.isEquivalentTo(parameters[1])) {
                        return new MacroReferenceSet(literalExpression).getAllReferences();
                    }
                }
            }
            return PsiReference.EMPTY_ARRAY;
        }
    });
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) MacroReferenceSet(ool.intellij.plugin.psi.reference.MacroReferenceSet) PsiExpression(com.intellij.psi.PsiExpression) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) NotNull(org.jetbrains.annotations.NotNull) PsiNewExpression(com.intellij.psi.PsiNewExpression) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiReferenceProvider(com.intellij.psi.PsiReferenceProvider) PsiNewExpressionImpl(com.intellij.psi.impl.source.tree.java.PsiNewExpressionImpl) PsiElement(com.intellij.psi.PsiElement)

Example 42 with PsiExpression

use of com.intellij.psi.PsiExpression in project PermissionsDispatcher by hotchemi.

the class CallOnRequestPermissionsResultDetector method checkMethodCall.

private static boolean checkMethodCall(PsiMethod method, PsiClass psiClass) {
    PsiCodeBlock codeBlock = method.getBody();
    if (codeBlock == null) {
        return false;
    }
    PsiStatement[] statements = codeBlock.getStatements();
    for (PsiStatement statement : statements) {
        if (!(statement instanceof PsiExpressionStatement)) {
            continue;
        }
        PsiExpression expression = ((PsiExpressionStatement) statement).getExpression();
        if (!(expression instanceof PsiCallExpression)) {
            continue;
        }
        PsiCallExpression callExpression = (PsiCallExpression) expression;
        String targetClassName = psiClass.getName() + "PermissionsDispatcher";
        PsiMethod resolveMethod = callExpression.resolveMethod();
        if (resolveMethod == null) {
            continue;
        }
        PsiClass containingClass = resolveMethod.getContainingClass();
        if (containingClass == null) {
            continue;
        }
        if (targetClassName.equals(containingClass.getName()) && "onRequestPermissionsResult".equals(resolveMethod.getName())) {
            return true;
        }
    }
    return false;
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiCodeBlock(com.intellij.psi.PsiCodeBlock) PsiExpressionStatement(com.intellij.psi.PsiExpressionStatement) PsiExpression(com.intellij.psi.PsiExpression) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) PsiCallExpression(com.intellij.psi.PsiCallExpression)

Example 43 with PsiExpression

use of com.intellij.psi.PsiExpression in project timber by JakeWharton.

the class WrongTimberUsageDetector method checkExceptionLogging.

private static void checkExceptionLogging(JavaContext context, PsiMethodCallExpression call) {
    PsiExpression[] arguments = call.getArgumentList().getExpressions();
    if (arguments.length > 1) {
        boolean isFirstParameterThrowable = isSubclassOf(context, arguments[0], Throwable.class);
        if (isFirstParameterThrowable) {
            PsiExpression secondArgument = arguments[1];
            String message = findLiteralValue(secondArgument);
            boolean callsGetMessage = false;
            if (secondArgument instanceof PsiMethodCallExpression) {
                PsiMethodCallExpression callExpression = (PsiMethodCallExpression) secondArgument;
                callsGetMessage = callExpression.getMethodExpression().getCanonicalText().endsWith("getMessage");
            }
            if (callsGetMessage) {
                context.report(ISSUE_EXCEPTION_LOGGING, secondArgument, context.getLocation(secondArgument), "Explicitly logging exception message is redundant");
            } else if (message == null || "".equals(message)) {
                context.report(ISSUE_EXCEPTION_LOGGING, secondArgument, context.getLocation(secondArgument), "Use single-argument log method instead of null/empty message");
            }
        }
    }
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression)

Example 44 with PsiExpression

use of com.intellij.psi.PsiExpression in project timber by JakeWharton.

the class WrongTimberUsageDetector method checkFormatArguments.

private static void checkFormatArguments(JavaContext context, PsiMethodCallExpression call) {
    PsiExpression[] arguments = call.getArgumentList().getExpressions();
    if (arguments.length == 0) {
        return;
    }
    int startIndexOfArguments = 1;
    PsiExpression formatStringArg = arguments[0];
    if (isSubclassOf(context, formatStringArg, Throwable.class)) {
        if (arguments.length == 1) {
            return;
        }
        formatStringArg = arguments[1];
        startIndexOfArguments++;
    }
    String formatString = findLiteralValue(formatStringArg);
    // We passed for example a method call
    if (formatString == null) {
        return;
    }
    int argumentCount = getFormatArgumentCount(formatString);
    int passedArgCount = arguments.length - startIndexOfArguments;
    if (argumentCount < passedArgCount) {
        context.report(ISSUE_ARG_COUNT, call, context.getLocation(call), String.format("Wrong argument count, format string `%1$s` requires " + "`%2$d` but format call supplies `%3$d`", formatString, argumentCount, passedArgCount));
        return;
    }
    if (argumentCount == 0) {
        return;
    }
    List<String> types = getStringArgumentTypes(formatString);
    PsiExpression argument = null;
    int argumentIndex = startIndexOfArguments;
    boolean valid;
    for (int i = 0; i < types.size(); i++) {
        String formatType = types.get(i);
        if (argumentIndex != arguments.length) {
            argument = arguments[argumentIndex++];
        } else {
            context.report(ISSUE_ARG_COUNT, call, context.getLocation(call), String.format("Wrong argument count, format string `%1$s` requires " + "`%2$d` but format call supplies `%3$d`", formatString, argumentCount, passedArgCount));
        }
        Class type = getType(argument);
        if (type == null) {
            continue;
        }
        char last = formatType.charAt(formatType.length() - 1);
        if (formatType.length() >= 2 && Character.toLowerCase(formatType.charAt(formatType.length() - 2)) == 't') {
            // Date time conversion.
            switch(last) {
                // time
                case 'H':
                case 'I':
                case 'k':
                case 'l':
                case 'M':
                case 'S':
                case 'L':
                case 'N':
                case 'p':
                case 'z':
                case 'Z':
                case 's':
                case 'Q':
                // date
                case 'B':
                case 'b':
                case 'h':
                case 'A':
                case 'a':
                case 'C':
                case 'Y':
                case 'y':
                case 'j':
                case 'm':
                case 'd':
                case 'e':
                // date/time
                case 'R':
                case 'T':
                case 'r':
                case 'D':
                case 'F':
                case 'c':
                    valid = type == Integer.TYPE || type == Calendar.class || type == Date.class;
                    if (!valid) {
                        String message = String.format("Wrong argument type for date formatting argument '#%1$d' " + "in `%2$s`: conversion is '`%3$s`', received `%4$s` " + "(argument #%5$d in method call)", i + 1, formatString, formatType, type.getSimpleName(), startIndexOfArguments + i + 1);
                        context.report(ISSUE_ARG_TYPES, call, context.getLocation(argument), message);
                    }
                    break;
                default:
                    String message = String.format("Wrong suffix for date format '#%1$d' " + "in `%2$s`: conversion is '`%3$s`', received `%4$s` " + "(argument #%5$d in method call)", i + 1, formatString, formatType, type.getSimpleName(), startIndexOfArguments + i + 1);
                    context.report(ISSUE_FORMAT, call, context.getLocation(argument), message);
            }
            continue;
        }
        switch(last) {
            case 'b':
            case 'B':
                valid = type == Boolean.TYPE;
                break;
            case 'x':
            case 'X':
            case 'd':
            case 'o':
            case 'e':
            case 'E':
            case 'f':
            case 'g':
            case 'G':
            case 'a':
            case 'A':
                valid = type == Integer.TYPE || type == Float.TYPE || type == Double.TYPE || type == Long.TYPE || type == Byte.TYPE || type == Short.TYPE;
                break;
            case 'c':
            case 'C':
                valid = type == Character.TYPE;
                break;
            case 'h':
            case 'H':
                valid = type != Boolean.TYPE && !Number.class.isAssignableFrom(type);
                break;
            case 's':
            case 'S':
            default:
                valid = true;
        }
        if (!valid) {
            String message = String.format("Wrong argument type for formatting argument '#%1$d' " + "in `%2$s`: conversion is '`%3$s`', received `%4$s` " + "(argument #%5$d in method call)", i + 1, formatString, formatType, type.getSimpleName(), startIndexOfArguments + i + 1);
            context.report(ISSUE_ARG_TYPES, call, context.getLocation(argument), message);
        }
    }
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) Calendar(java.util.Calendar) PsiClass(com.intellij.psi.PsiClass)

Example 45 with PsiExpression

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

the class EqualityToEqualsFix method doFix.

@Override
public void doFix(Project project, ProblemDescriptor descriptor) {
    final PsiElement comparisonToken = descriptor.getPsiElement();
    final PsiElement parent = comparisonToken.getParent();
    if (!(parent instanceof PsiBinaryExpression)) {
        return;
    }
    final PsiBinaryExpression expression = (PsiBinaryExpression) parent;
    boolean negated = false;
    final IElementType tokenType = expression.getOperationTokenType();
    if (JavaTokenType.NE.equals(tokenType)) {
        negated = true;
    }
    final PsiExpression lhs = expression.getLOperand();
    final PsiExpression strippedLhs = ParenthesesUtils.stripParentheses(lhs);
    if (strippedLhs == null) {
        return;
    }
    final PsiExpression rhs = expression.getROperand();
    final PsiExpression strippedRhs = ParenthesesUtils.stripParentheses(rhs);
    if (strippedRhs == null) {
        return;
    }
    @NonNls final String expString;
    if (PsiUtil.isLanguageLevel7OrHigher(expression)) {
        expString = "java.util.Objects.equals(" + strippedLhs.getText() + ',' + strippedRhs.getText() + ')';
    } else if (ParenthesesUtils.getPrecedence(strippedLhs) > ParenthesesUtils.METHOD_CALL_PRECEDENCE) {
        expString = '(' + strippedLhs.getText() + ").equals(" + strippedRhs.getText() + ')';
    } else {
        expString = strippedLhs.getText() + ".equals(" + strippedRhs.getText() + ')';
    }
    @NonNls final String newExpression;
    if (negated) {
        newExpression = '!' + expString;
    } else {
        newExpression = expString;
    }
    PsiReplacementUtil.replaceExpressionAndShorten(expression, newExpression);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) NonNls(org.jetbrains.annotations.NonNls) PsiExpression(com.intellij.psi.PsiExpression) PsiElement(com.intellij.psi.PsiElement) PsiBinaryExpression(com.intellij.psi.PsiBinaryExpression)

Aggregations

PsiExpression (com.intellij.psi.PsiExpression)72 PsiElement (com.intellij.psi.PsiElement)28 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)13 PsiMethodCallExpression (com.intellij.psi.PsiMethodCallExpression)11 Nullable (org.jetbrains.annotations.Nullable)9 PsiReference (com.intellij.psi.PsiReference)8 PsiType (com.intellij.psi.PsiType)8 NonNls (org.jetbrains.annotations.NonNls)8 PsiClass (com.intellij.psi.PsiClass)7 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)6 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)6 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)6 PsiMethod (com.intellij.psi.PsiMethod)6 PsiNewExpression (com.intellij.psi.PsiNewExpression)6 PsiPolyadicExpression (com.intellij.psi.PsiPolyadicExpression)6 PsiStatement (com.intellij.psi.PsiStatement)6 Project (com.intellij.openapi.project.Project)5 PsiBinaryExpression (com.intellij.psi.PsiBinaryExpression)5 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)5 PsiElementFactory (com.intellij.psi.PsiElementFactory)5