@Test void test_GetLiterals_string_concatenation_with_constant_and_parenthesis() { TestCase testcase = getArg("\"a\" + (b + \"c\")", "final String b = \"q\";"); ExpressionTree expr = testcase.expr; ExpressionTree a = ((BinaryExpressionTree) expr).leftOperand(); BinaryExpressionTree bc = (BinaryExpressionTree) ((ParenthesizedTree) ((BinaryExpressionTree) expr).rightOperand()).expression(); ExpressionTree b = ((VariableTree) (testcase.preStatements.get(0))).initializer(); ExpressionTree c = bc.rightOperand(); Optional<LiteralTree[]> result = getLiterals(expr); assertThat(result).isPresent(); assertThat(result.get()) .hasSize(3) .allMatch(t -> t.is(Tree.Kind.STRING_LITERAL)) .containsExactly((LiteralTree) a, (LiteralTree) b, (LiteralTree) c); }
@VisibleForTesting protected static Optional<LiteralTree[]> getLiterals(ExpressionTree expr) { switch (expr.kind()) { case PLUS: return getLiteralsFromStringConcatenation((BinaryExpressionTree) expr); case IDENTIFIER: return getLiteralsFromFinalVariables((IdentifierTree) expr); case PARENTHESIZED_EXPRESSION: return getLiterals(ExpressionUtils.skipParentheses(expr)); case STRING_LITERAL: return Optional.of(new LiteralTree[] {(LiteralTree) expr}); default: return Optional.empty(); } }
AbstractRegexCheck extends AbstractMethodDetection implements RegexCheck { @VisibleForTesting protected static Optional<LiteralTree[]> getLiterals(ExpressionTree expr) { switch (expr.kind()) { case PLUS: return getLiteralsFromStringConcatenation((BinaryExpressionTree) expr); case IDENTIFIER: return getLiteralsFromFinalVariables((IdentifierTree) expr); case PARENTHESIZED_EXPRESSION: return getLiterals(ExpressionUtils.skipParentheses(expr)); case STRING_LITERAL: return Optional.of(new LiteralTree[] {(LiteralTree) expr}); default: return Optional.empty(); } } }
AbstractRegexCheck extends AbstractMethodDetection implements RegexCheck { @VisibleForTesting protected static Optional<LiteralTree[]> getLiterals(ExpressionTree expr) { switch (expr.kind()) { case PLUS: return getLiteralsFromStringConcatenation((BinaryExpressionTree) expr); case IDENTIFIER: return getLiteralsFromFinalVariables((IdentifierTree) expr); case PARENTHESIZED_EXPRESSION: return getLiterals(ExpressionUtils.skipParentheses(expr)); case STRING_LITERAL: return Optional.of(new LiteralTree[] {(LiteralTree) expr}); default: return Optional.empty(); } }  }
AbstractRegexCheck extends AbstractMethodDetection implements RegexCheck { @VisibleForTesting protected static Optional<LiteralTree[]> getLiterals(ExpressionTree expr) { switch (expr.kind()) { case PLUS: return getLiteralsFromStringConcatenation((BinaryExpressionTree) expr); case IDENTIFIER: return getLiteralsFromFinalVariables((IdentifierTree) expr); case PARENTHESIZED_EXPRESSION: return getLiterals(ExpressionUtils.skipParentheses(expr)); case STRING_LITERAL: return Optional.of(new LiteralTree[] {(LiteralTree) expr}); default: return Optional.empty(); } }  @Override final void setContext(JavaFileScannerContext context); @Override List<Tree.Kind> nodesToVisit(); abstract void checkRegex(RegexParseResult regexForLiterals, MethodInvocationTree mit); final void reportIssue(RegexSyntaxElement regexTree, String message, @Nullable Integer cost, List<RegexCheck.RegexIssueLocation> secondaries); final void reportIssue(Tree javaTree, String message, @Nullable Integer cost, List<RegexCheck.RegexIssueLocation> secondaries); }
AbstractRegexCheck extends AbstractMethodDetection implements RegexCheck { @VisibleForTesting protected static Optional<LiteralTree[]> getLiterals(ExpressionTree expr) { switch (expr.kind()) { case PLUS: return getLiteralsFromStringConcatenation((BinaryExpressionTree) expr); case IDENTIFIER: return getLiteralsFromFinalVariables((IdentifierTree) expr); case PARENTHESIZED_EXPRESSION: return getLiterals(ExpressionUtils.skipParentheses(expr)); case STRING_LITERAL: return Optional.of(new LiteralTree[] {(LiteralTree) expr}); default: return Optional.empty(); } }  @Override final void setContext(JavaFileScannerContext context); @Override List<Tree.Kind> nodesToVisit(); abstract void checkRegex(RegexParseResult regexForLiterals, MethodInvocationTree mit); final void reportIssue(RegexSyntaxElement regexTree, String message, @Nullable Integer cost, List<RegexCheck.RegexIssueLocation> secondaries); final void reportIssue(Tree javaTree, String message, @Nullable Integer cost, List<RegexCheck.RegexIssueLocation> secondaries);  }