@Override public boolean matches(ExpressionTree tree, VisitorState state) { Object value = ((JCExpression) tree).type.constValue(); return value instanceof String && !isValidSyntax((String) value); }
/** Returns the compile-time constant value of a tree if it has one, or {@code null}. */ @Nullable public static Object constValue(Tree tree) { if (tree == null) { return null; } tree = stripParentheses(tree); Type type = ASTHelpers.getType(tree); Object value; if (tree instanceof JCLiteral) { value = ((JCLiteral) tree).value; } else if (type != null) { value = type.constValue(); } else { return null; } if (type.hasTag(TypeTag.BOOLEAN) && value instanceof Integer) { return ((Integer) value) == 1; } return value; }
@Override public Description matchMethodInvocation( MethodInvocationTree methodInvocationTree, VisitorState state) { if (!BAD_REGEX_USAGE.matches(methodInvocationTree, state)) { return Description.NO_MATCH; } // TODO: Suggest fixes for more situations. Description.Builder descriptionBuilder = buildDescription(methodInvocationTree); ExpressionTree arg = methodInvocationTree.getArguments().get(0); String value = (String) ((JCExpression) arg).type.constValue(); String reasonInvalid = ""; if (".".equals(value)) { descriptionBuilder.addFix(SuggestedFix.replace(arg, "\"\\\\.\"")); reasonInvalid = "\".\" is a valid but useless regex"; } else { try { Pattern.compile(value); } catch (PatternSyntaxException e) { reasonInvalid = e.getMessage(); } } descriptionBuilder.setMessage(MESSAGE_BASE + reasonInvalid); return descriptionBuilder.build(); } }
/** Is this a constant type whose value is true? */ public boolean isTrue() { return tag == BOOLEAN && constValue() != null && ((Integer)constValue()).intValue() != 0; }
/** Is this a constant type whose value is false? */ public boolean isFalse() { return tag == BOOLEAN && constValue() != null && ((Integer)constValue()).intValue() == 0; }
/** Is this a constant type whose value is false? */ public boolean isFalse() { return tag == BOOLEAN && constValue() != null && ((Integer)constValue()).intValue() == 0; }
/** Is this a constant type whose value is true? */ public boolean isTrue() { return tag == BOOLEAN && constValue() != null && ((Integer)constValue()).intValue() != 0; }
/** * The constant value of this type, converted to String */ public String stringValue() { assert constValue() != null; if (tag == BOOLEAN) return ((Integer) constValue()).intValue() == 0 ? "false" : "true"; else if (tag == CHAR) return String.valueOf((char) ((Integer) constValue()).intValue()); else return constValue().toString(); }
/** * The constant value of this type, converted to String */ public String stringValue() { assert constValue() != null; if (tag == BOOLEAN) return ((Integer) constValue()).intValue() == 0 ? "false" : "true"; else if (tag == CHAR) return String.valueOf((char) ((Integer) constValue()).intValue()); else return constValue().toString(); }
/** * The constant value of this type, converted to String */ public String stringValue() { Object cv = Assert.checkNonNull(constValue()); return cv.toString(); }
@Override public boolean matches(ExpressionTree tree, VisitorState state) { Object value = ((JCExpression) tree).type.constValue(); return value instanceof String && !isValidSyntax((String) value); }
/** * The constant value of this type, converted to String */ public String stringValue() { Object cv = Assert.checkNonNull(constValue()); return cv.toString(); }
@Override public void scan(JCTree tree) { if (tree != null && tree.type != null && tree.type.constValue() != null) { checkStringConstant(tree.pos(), tree.type.constValue()); } super.scan(tree); }
/**Checks whether the given expression is a compile-time constant, as per JLS 15.28. * * @param expression the expression to check * @return true if and only if the given expression represents a compile-time constant value * @since 0.91 */ public boolean isCompileTimeConstantExpression(TreePath expression) { TypeMirror attributeTree = info.getTrees().getTypeMirror(expression); Type attributeTreeImpl = (Type) attributeTree; return attributeTreeImpl != null && attributeTreeImpl.constValue() != null; }
private JCTree convert(JCTree tree, Type pt) { if (tree.type == pt) return tree; JCTree result = make_at(tree.pos()).TypeCast(make.Type(pt), (JCExpression)tree); result.type = (tree.type.constValue() != null) ? cfolder.coerce(tree.type, pt) : pt; return result; }
private JCTree convert(JCTree tree, Type pt) { if (tree.type == pt) return tree; JCTree result = make_at(tree.pos()).TypeCast(make.Type(pt), (JCExpression)tree); result.type = (tree.type.constValue() != null) ? cfolder.coerce(tree.type, pt) : pt; return result; }
private JCTree convert(JCTree tree, Type pt) { if (tree.type == pt || tree.type.hasTag(BOT)) return tree; JCTree result = make_at(tree.pos()).TypeCast(make.Type(pt), (JCExpression)tree); result.type = (tree.type.constValue() != null) ? cfolder.coerce(tree.type, pt) : pt; return result; }
private JCTree convert(JCTree tree, Type pt) { if (tree.type == pt || tree.type.hasTag(BOT)) return tree; JCTree result = make_at(tree.pos()).TypeCast(make.Type(pt), (JCExpression)tree); result.type = (tree.type.constValue() != null) ? cfolder.coerce(tree.type, pt) : pt; return result; }
public void visitTypeCast(JCTypeCast tree) { Type clazztype = attribType(tree.clazz, env); Type exprtype = attribExpr(tree.expr, env, Infer.anyPoly); Type owntype = chk.checkCastable(tree.expr.pos(), exprtype, clazztype); if (exprtype.constValue() != null) owntype = cfolder.coerce(exprtype, owntype); result = check(tree, capture(owntype), VAL, pkind, pt); }
public void visitTypeCast(JCTypeCast tree) { Type clazztype = attribType(tree.clazz, env); Type exprtype = attribExpr(tree.expr, env, Infer.anyPoly); Type owntype = chk.checkCastable(tree.expr.pos(), exprtype, clazztype); if (exprtype.constValue() != null) owntype = cfolder.coerce(exprtype, owntype); result = check(tree, capture(owntype), VAL, pkind, pt); }