Search in sources :

Example 1 with SubstitutedExpressionEvaluationHelper

use of org.intellij.plugins.intelliLang.util.SubstitutedExpressionEvaluationHelper in project intellij-community by JetBrains.

the class PatternValidator method checkExpression.

private void checkExpression(PsiExpression expression, final PsiAnnotation[] annotations, ProblemsHolder holder) {
    if (annotations.length == 0)
        return;
    final PsiAnnotation psiAnnotation = annotations[0];
    // cache compiled pattern with annotation
    CachedValue<Pattern> p = psiAnnotation.getUserData(COMPLIED_PATTERN);
    if (p == null) {
        final CachedValueProvider<Pattern> provider = () -> {
            final String pattern = AnnotationUtilEx.calcAnnotationValue(psiAnnotation, "value");
            Pattern p1 = null;
            if (pattern != null) {
                try {
                    p1 = Pattern.compile(pattern);
                } catch (PatternSyntaxException e) {
                // pattern stays null
                }
            }
            return CachedValueProvider.Result.create(p1, (Object[]) annotations);
        };
        p = CachedValuesManager.getManager(expression.getProject()).createCachedValue(provider, false);
        psiAnnotation.putUserData(COMPLIED_PATTERN, p);
    }
    final Pattern pattern = p.getValue();
    if (pattern == null)
        return;
    List<PsiExpression> nonConstantElements = new SmartList<>();
    final Object result = new SubstitutedExpressionEvaluationHelper(expression.getProject()).computeExpression(expression, myConfiguration.getAdvancedConfiguration().getDfaOption(), false, nonConstantElements);
    final String o = result == null ? null : String.valueOf(result);
    if (o != null) {
        if (!pattern.matcher(o).matches()) {
            if (annotations.length > 1) {
                // the last element contains the element's actual annotation
                final String fqn = annotations[annotations.length - 1].getQualifiedName();
                assert fqn != null;
                final String name = StringUtil.getShortName(fqn);
                holder.registerProblem(expression, MessageFormat.format("Expression ''{0}'' doesn''t match ''{1}'' pattern: {2}", o, name, pattern.pattern()));
            } else {
                holder.registerProblem(expression, MessageFormat.format("Expression ''{0}'' doesn''t match pattern: {1}", o, pattern.pattern()));
            }
        }
    } else if (CHECK_NON_CONSTANT_VALUES) {
        for (PsiExpression expr : nonConstantElements) {
            final PsiElement e;
            if (expr instanceof PsiReferenceExpression) {
                e = ((PsiReferenceExpression) expr).resolve();
            } else if (expr instanceof PsiMethodCallExpression) {
                e = ((PsiMethodCallExpression) expr).getMethodExpression().resolve();
            } else {
                e = expr;
            }
            final PsiModifierListOwner owner = e instanceof PsiModifierListOwner ? (PsiModifierListOwner) e : null;
            LocalQuickFix quickFix;
            if (owner != null && PsiUtilEx.isLanguageAnnotationTarget(owner)) {
                PsiAnnotation[] resolvedAnnos = AnnotationUtilEx.getAnnotationFrom(owner, myConfiguration.getAdvancedConfiguration().getPatternAnnotationPair(), true);
                if (resolvedAnnos.length == 2 && annotations.length == 2 && Comparing.strEqual(resolvedAnnos[1].getQualifiedName(), annotations[1].getQualifiedName())) {
                    // both target and source annotated indirectly with the same anno
                    return;
                }
                final String classname = myConfiguration.getAdvancedConfiguration().getSubstAnnotationPair().first;
                quickFix = AnnotateFix.canApplyOn(owner) ? new AnnotateFix(classname) : new IntroduceVariableFix();
            } else {
                quickFix = new IntroduceVariableFix();
            }
            holder.registerProblem(expr, "Unsubstituted expression", quickFix);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) SubstitutedExpressionEvaluationHelper(org.intellij.plugins.intelliLang.util.SubstitutedExpressionEvaluationHelper) SmartList(com.intellij.util.SmartList) AnnotateFix(org.intellij.plugins.intelliLang.util.AnnotateFix) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 SmartList (com.intellij.util.SmartList)1 Pattern (java.util.regex.Pattern)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 AnnotateFix (org.intellij.plugins.intelliLang.util.AnnotateFix)1 SubstitutedExpressionEvaluationHelper (org.intellij.plugins.intelliLang.util.SubstitutedExpressionEvaluationHelper)1