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; } }
/** * 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); }
@Override public void visitMethodCallExpression(PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); if (mPsiMethod == expression.resolveMethod()) { PsiExpression[] expressions = expression.getArgumentList().getExpressions(); if (expressions.length > mParamIndex) { PsiExpression paramExpr = expressions[mParamIndex]; String value = ConstantEvaluator.evaluateString(mContext, paramExpr, false); if (RO_SERIALNO.equals(value) && !inCatchPlayServicesNotAvailableException(expression)) { String message = String.format(MESSAGE_DEVICE_IDENTIFIERS, RO_SERIALNO); mContext.report(ISSUE, paramExpr, mContext.getLocation(paramExpr), message); } } } } }
/** * 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; } }
PsiField field = (PsiField) resolved; if (field.getInitializer() != null) { return isArrayLiteral(field.getInitializer()); .getDeclaredElements()) { if (variable.equals(element)) { return isArrayLiteral(variable.getInitializer()); if (targetName.equals(reference.getReferenceName()) && reference.getQualifier() == null) { return isArrayLiteral(assign.getRExpression()); PsiExpression expression = parenthesizedExpression.getExpression(); if (expression != null) { return isArrayLiteral(expression); PsiExpression operand = castExpression.getOperand(); if (operand != null) { return isArrayLiteral(operand);
/** * 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; }
return ConstantEvaluator.isArrayLiteral(expressions[arg]);
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); }
String string = ConstantEvaluator.evaluateString(context, argument, false); String string = ConstantEvaluator.evaluateString(context, argument, false);
@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; }
((PsiArrayInitializerMemberValue) attributeValue).getInitializers(); List<String> result = Lists.newArrayListWithCapacity(initializers.length); ConstantEvaluator constantEvaluator = new ConstantEvaluator(null); for (PsiAnnotationMemberValue element : initializers) { Object o = constantEvaluator.evaluate(element); if (o instanceof String) { result.add((String)o); Object o = ConstantEvaluator.evaluate(null, attributeValue); if (o instanceof String) { return new String[]{(String) o};
@Override public void visitMethodCallExpression(PsiMethodCallExpression expression) { String method = expression.getMethodExpression().getCanonicalText(); if (!method.endsWith(".putString") && !method.endsWith(".putLong") && !method .endsWith(".putDouble")) { return; } PsiElement token = expression.getMethodExpression().getQualifier(); if (token == null || !mBundleReference.textMatches(token)) { return; } PsiExpression[] expressions = expression.getArgumentList().getExpressions(); String evaluatedName = ConstantEvaluator.evaluateString(mContext, expressions[0], false); if (evaluatedName != null) { addParam(evaluatedName, expressions[1].getText(), expression); } }
Object constant = ConstantEvaluator.evaluate(context, argument); if (constant instanceof String) { String pattern = (String) constant;
ConstantEvaluator constantEvaluator = new ConstantEvaluator(mContext); for (int index = 0; index < initializers.length; index++) { PsiAnnotationMemberValue expression = initializers[index]; Object o = constantEvaluator.evaluate(expression); if (o instanceof Number) { Number number = (Number) o;
String value = ConstantEvaluator.evaluateString(mContext, arg, false); if ("get".equals(value)) { PsiVariable variable = CleanupDetector.getVariableElement(expression); String value = ConstantEvaluator.evaluateString(mContext, arg, false); if (RO_SERIALNO.equals(value)) { mContext.report(ISSUE, arg, mContext.getLocation(arg),
@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; }
String string = ConstantEvaluator.evaluateString(context, argument, false); String string = ConstantEvaluator.evaluateString(context, argument, false);
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 " +
@Override public void visitMethod(@NonNull JavaContext context, @Nullable JavaElementVisitor visitor, @NonNull PsiMethodCallExpression node, @NonNull PsiMethod method) { JavaEvaluator evaluator = context.getEvaluator(); if (!evaluator.isMemberInClass(method, "android.database.sqlite.SQLiteDatabase")) { return; } int parameterCount = evaluator.getParameterCount(method); if (parameterCount == 0) { return; } if (!evaluator.parameterHasType(method, 0, TYPE_STRING)) { return; } // Try to resolve the String and look for STRING keys PsiExpression argument = node.getArgumentList().getExpressions()[0]; String sql = ConstantEvaluator.evaluateString(context, argument, true); if (sql != null && (sql.startsWith("CREATE TABLE") || sql.startsWith("ALTER TABLE")) && sql.matches(".*\\bSTRING\\b.*")) { String message = "Using column type STRING; did you mean to use TEXT? " + "(STRING is a numeric type and its value can be adjusted; for example, " + "strings that look like integers can drop leading zeroes. See issue " + "explanation for details.)"; context.report(ISSUE, node, context.getLocation(node), message); } } }