private static long getLongValue(@NonNull JavaContext context, @NonNull Expression argument) { Object value = ConstantEvaluator.evaluate(context, argument); if (value instanceof Number) { return ((Number)value).longValue(); } return Long.MAX_VALUE; } }
private static long getLongValue( @NonNull JavaContext context, @NonNull PsiExpression argument) { Object value = ConstantEvaluator.evaluate(context, argument); if (value instanceof Number) { return ((Number)value).longValue(); } return Long.MAX_VALUE; } }
/** * Evaluates the given node and returns the constant value it resolves to, if any. Convenience * wrapper which creates a new {@linkplain ConstantEvaluator}, evaluates the node and returns * the result. * * @param context the context to use to resolve field references, if any * @param node the node to compute the constant value for * @return the corresponding constant value - a String, an Integer, a Float, and so on * @deprecated Use {@link #evaluate(JavaContext, PsiElement)} instead */ @Deprecated @Nullable public static Object evaluate(@NonNull JavaContext context, @NonNull Node node) { return new ConstantEvaluator(context).evaluate(node); }
Object constant = ConstantEvaluator.evaluate(context, argument); if (constant instanceof String) { String pattern = (String) constant;
@Nullable public static Long getAnnotationLongValue(@Nullable PsiAnnotation annotation, @NonNull String name) { if (annotation != null) { PsiAnnotationMemberValue attributeValue = annotation.findDeclaredAttributeValue(name); if (attributeValue == null && ATTR_VALUE.equals(name)) { attributeValue = annotation.findDeclaredAttributeValue(null); } // Use constant evaluator since we want to resolve field references as well if (attributeValue != null) { Object o = ConstantEvaluator.evaluate(null, attributeValue); if (o instanceof Number) { return ((Number)o).longValue(); } } } return null; }
@Nullable public static Double getAnnotationDoubleValue(@Nullable PsiAnnotation annotation, @NonNull String name) { if (annotation != null) { PsiAnnotationMemberValue attributeValue = annotation.findDeclaredAttributeValue(name); if (attributeValue == null && ATTR_VALUE.equals(name)) { attributeValue = annotation.findDeclaredAttributeValue(null); } // Use constant evaluator since we want to resolve field references as well if (attributeValue != null) { Object o = ConstantEvaluator.evaluate(null, attributeValue); if (o instanceof Number) { return ((Number)o).doubleValue(); } } } return null; }
Boolean.TRUE.equals(ConstantEvaluator.evaluate(context, args[0])) && Boolean.FALSE.equals(ConstantEvaluator.evaluate(context, args[1]))) { context.report(SET_READABLE, node, context.getLocation(node), "Setting file permissions to world-readable can be " + } else if ("setWritable".equals(methodName)) { if (args.length == 2 && Boolean.TRUE.equals(ConstantEvaluator.evaluate(context, args[0])) && Boolean.FALSE.equals(ConstantEvaluator.evaluate(context, args[1]))) { context.report(SET_WRITABLE, node, context.getLocation(node), "Setting file permissions to world-writable can be " +
/** * Evaluates the given node and returns the constant value it resolves to, if any. Convenience * wrapper which creates a new {@linkplain ConstantEvaluator}, evaluates the node and returns * the result. * * @param context the context to use to resolve field references, if any * @param node the node to compute the constant value for * @return the corresponding constant value - a String, an Integer, a Float, and so on */ @Nullable public static Object evaluate(@Nullable JavaContext context, @NonNull PsiElement node) { return new ConstantEvaluator(context).evaluate(node); }
@Nullable public static Boolean getAnnotationBooleanValue(@Nullable PsiAnnotation annotation, @NonNull String name) { if (annotation != null) { PsiAnnotationMemberValue attributeValue = annotation.findDeclaredAttributeValue(name); if (attributeValue == null && ATTR_VALUE.equals(name)) { attributeValue = annotation.findDeclaredAttributeValue(null); } // Use constant evaluator since we want to resolve field references as well if (attributeValue != null) { Object o = ConstantEvaluator.evaluate(null, attributeValue); if (o instanceof Boolean) { return (Boolean) o; } } } return null; }
@Nullable public static String getAnnotationStringValue(@Nullable PsiAnnotation annotation, @NonNull String name) { if (annotation != null) { PsiAnnotationMemberValue attributeValue = annotation.findDeclaredAttributeValue(name); if (attributeValue == null && ATTR_VALUE.equals(name)) { attributeValue = annotation.findDeclaredAttributeValue(null); } // Use constant evaluator since we want to resolve field references as well if (attributeValue != null) { Object o = ConstantEvaluator.evaluate(null, attributeValue); if (o instanceof String) { return (String) o; } } } return null; }
/** * Evaluates the given node and returns the constant string it resolves to, if any. Convenience * wrapper which creates a new {@linkplain ConstantEvaluator}, evaluates the node and returns * the result if the result is a string. * * @param context the context to use to resolve field references, if any * @param node the node to compute the constant value for * @param allowUnknown whether we should construct the string even if some parts of it are * unknown * @return the corresponding string, if any * @deprecated Use {@link #evaluateString(JavaContext, PsiElement, boolean)} instead */ @Deprecated @Nullable public static String evaluateString(@NonNull JavaContext context, @NonNull Node node, boolean allowUnknown) { ConstantEvaluator evaluator = new ConstantEvaluator(context); if (allowUnknown) { evaluator.allowUnknowns(); } Object value = evaluator.evaluate(node); return value instanceof String ? (String) value : null; }
@Override public void visitMethod(@NonNull JavaContext context, @Nullable JavaElementVisitor visitor, @NonNull PsiMethodCallExpression call, @NonNull PsiMethod method) { PsiExpression[] arguments = call.getArgumentList().getExpressions(); if (arguments.length == 1) { Object constant = ConstantEvaluator.evaluate(context, arguments[0]); if (constant != null && !Boolean.FALSE.equals(constant)) { context.report(ISSUE, call, context.getLocation(call), "Using `setJavaScriptEnabled` can introduce XSS vulnerabilities " + "into your application, review carefully."); } } }
/** * Evaluates the given node and returns the constant string it resolves to, if any. Convenience * wrapper which creates a new {@linkplain ConstantEvaluator}, evaluates the node and returns * the result if the result is a string. * * @param context the context to use to resolve field references, if any * @param node the node to compute the constant value for * @param allowUnknown whether we should construct the string even if some parts of it are * unknown * @return the corresponding string, if any */ @Nullable public static String evaluateString(@Nullable JavaContext context, @NonNull PsiElement node, boolean allowUnknown) { ConstantEvaluator evaluator = new ConstantEvaluator(context); if (allowUnknown) { evaluator.allowUnknowns(); } Object value = evaluator.evaluate(node); return value instanceof String ? (String) value : null; } }
private static void checkIntRange( @NonNull JavaContext context, @NonNull ResolvedAnnotation annotation, @NonNull Node argument) { Object object = ConstantEvaluator.evaluate(context, argument); if (!(object instanceof Number)) { return; } long value = ((Number)object).longValue(); long from = getLongAttribute(annotation, ATTR_FROM, Long.MIN_VALUE); long to = getLongAttribute(annotation, ATTR_TO, Long.MAX_VALUE); String message = getIntRangeError(value, from, to); if (message != null) { context.report(RANGE, argument, context.getLocation(argument), message); } }
@Override public void visitMethod(@NonNull JavaContext context, @Nullable JavaElementVisitor visitor, @NonNull PsiMethodCallExpression node, @NonNull PsiMethod method) { JavaEvaluator evaluator = context.getEvaluator(); if (!evaluator.methodMatches(method, PACKAGE_MANAGER_CLASS, true, JavaParser.TYPE_STRING, JavaParser.TYPE_INT)) { return; } PsiExpression[] arguments = node.getArgumentList().getExpressions(); if (arguments.length == 2) { PsiExpression second = arguments[1]; Object number = ConstantEvaluator.evaluate(context, second); if (number instanceof Number) { int flagValue = ((Number)number).intValue(); maybeReportIssue(flagValue, context, node, second); } } }
private static void checkFloatRange( @NonNull JavaContext context, @NonNull ResolvedAnnotation annotation, @NonNull Node argument) { Object object = ConstantEvaluator.evaluate(context, argument); if (!(object instanceof Number)) { return; } double value = ((Number)object).doubleValue(); double from = getDoubleAttribute(annotation, ATTR_FROM, Double.NEGATIVE_INFINITY); double to = getDoubleAttribute(annotation, ATTR_TO, Double.POSITIVE_INFINITY); boolean fromInclusive = getBoolean(annotation, ATTR_FROM_INCLUSIVE, true); boolean toInclusive = getBoolean(annotation, ATTR_TO_INCLUSIVE, true); String message = getFloatRangeError(value, from, to, fromInclusive, toInclusive, argument); if (message != null) { context.report(RANGE, argument, context.getLocation(argument), message); } }
private static void checkFloatRange( @NonNull JavaContext context, @NonNull PsiAnnotation annotation, @NonNull PsiElement argument) { Object object = ConstantEvaluator.evaluate(context, argument); if (!(object instanceof Number)) { return; } double value = ((Number)object).doubleValue(); double from = getDoubleAttribute(annotation, ATTR_FROM, Double.NEGATIVE_INFINITY); double to = getDoubleAttribute(annotation, ATTR_TO, Double.POSITIVE_INFINITY); boolean fromInclusive = getBoolean(annotation, ATTR_FROM_INCLUSIVE, true); boolean toInclusive = getBoolean(annotation, ATTR_TO_INCLUSIVE, true); String message = getFloatRangeError(value, from, to, fromInclusive, toInclusive, argument); if (message != null && !isIgnoredInIde(RANGE, context, argument)) { context.report(RANGE, argument, context.getLocation(argument), message); } }
@Nullable private static String getIntRangeError( @NonNull JavaContext context, @NonNull PsiAnnotation annotation, @NonNull PsiElement argument) { if (argument instanceof PsiNewExpression) { PsiNewExpression newExpression = (PsiNewExpression) argument; PsiArrayInitializerExpression initializer = newExpression.getArrayInitializer(); if (initializer != null) { for (PsiExpression expression : initializer.getInitializers()) { String error = getIntRangeError(context, annotation, expression); if (error != null) { return error; } } } } Object object = ConstantEvaluator.evaluate(context, argument); if (!(object instanceof Number)) { return null; } long value = ((Number)object).longValue(); long from = getLongAttribute(annotation, ATTR_FROM, Long.MIN_VALUE); long to = getLongAttribute(annotation, ATTR_TO, Long.MAX_VALUE); return getIntRangeError(value, from, to); }
@Override public void visitMethod(@NonNull JavaContext context, @Nullable JavaElementVisitor visitor, @NonNull PsiMethodCallExpression node, @NonNull PsiMethod method) { if (!context.getEvaluator().isMemberInSubClassOf(method, CIPHER, false)) { return; } PsiExpressionList argumentList = node.getArgumentList(); PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length == 1) { PsiExpression expression = arguments[0]; Object value = ConstantEvaluator.evaluate(context, expression); if (value instanceof String) { checkParameter(context, node, expression, (String)value, !(expression instanceof PsiLiteral)); } } }
Object value = ConstantEvaluator.evaluate(context, first); if (!(value instanceof String)) { return;