Package com.sun.source.tree

Examples of com.sun.source.tree.LiteralTree


* @author alexeagle@google.com (Alex Eagle)
*/
@RunWith(JUnit4.class)
public class StringLiteralTest {
  @Test public void matches() {
    LiteralTree tree = mock(LiteralTree.class);
    when(tree.getValue()).thenReturn("a string literal");
    assertTrue(new StringLiteral("a string literal").matches(tree, null));
  }
View Full Code Here


    when(tree.getValue()).thenReturn("a string literal");
    assertTrue(new StringLiteral("a string literal").matches(tree, null));
  }
 
  @Test public void notMatches() {
    LiteralTree tree = mock(LiteralTree.class);
    when(tree.getValue()).thenReturn("a string literal");
    assertFalse(new StringLiteral("different string").matches(tree, null));
   
    IdentifierTree idTree = mock(IdentifierTree.class);
    assertFalse(new StringLiteral("test").matches(idTree, null));
   
    LiteralTree intTree = mock(LiteralTree.class);
    when(intTree.getValue()).thenReturn(5);
    assertFalse(new StringLiteral("test").matches(intTree, null));
  }
View Full Code Here

  }

  @Override
  public boolean matches(ExpressionTree expressionTree, VisitorState state) {
    if (expressionTree instanceof LiteralTree) {
      LiteralTree literalTree = (LiteralTree) expressionTree;
      Object actualValue = literalTree.getValue();
      return actualValue instanceof String && actualValue.equals(value);
    } else {
      return false;
    }
  }
View Full Code Here

      MethodInvocationTree stringFormatInvocation = (MethodInvocationTree) t;
      if (!(stringFormatInvocation.getArguments().get(0)
          instanceof LiteralTree)) {
        return false;
      }
      LiteralTree firstArg = (LiteralTree)
          stringFormatInvocation.getArguments().get(0);
      String literal = firstArg.getValue().toString();
      return !invalidFormatCharacters.matcher(literal).find();
    }
View Full Code Here

  @Override
  public Description matchMethodInvocation(MethodInvocationTree t, VisitorState state) {
    if (PRECONDITIONS_CHECK.matches(t, state) && t.getArguments().size() >= 2
        && t.getArguments().get(1) instanceof LiteralTree) {
      LiteralTree formatStringTree = (LiteralTree) t.getArguments().get(1);
      if (formatStringTree.getValue() instanceof String) {
        String formatString = (String) formatStringTree.getValue();
        int expectedArgs = expectedArguments(formatString);
        if (expectedArgs < t.getArguments().size() - 2
            && BAD_PLACEHOLDER_REGEX.matcher(formatString).find()) {
          return describe(t, state);
        }
View Full Code Here

   */
  private static final Pattern BAD_PLACEHOLDER_REGEX =
      Pattern.compile("\\$s|%(?:\\d+\\$)??[dbBhHScCdoxXeEfgGaAtTn]|\\{\\d+\\}");

  public Description describe(MethodInvocationTree t, VisitorState state) {
    LiteralTree formatTree = (LiteralTree) t.getArguments().get(1);

    String fixedFormatString =
        BAD_PLACEHOLDER_REGEX.matcher(state.getSourceForNode((JCTree) formatTree))
            .replaceAll("%s");
    if (expectedArguments(fixedFormatString) == t.getArguments().size() - 2) {
View Full Code Here

    if (!kindIs(Kind.INT_LITERAL).matches(operand, state)) {
      return Description.NO_MATCH;
    }

    LiteralTree rightOperand = (LiteralTree) operand;
    if (((Integer) rightOperand.getValue()) != 0) {
      return Description.NO_MATCH;
    }

    // Find and replace enclosing Statement.
    StatementTree enclosingStmt =
View Full Code Here

    return ret;
  }

  private LiteralTree literalTree() {
     // Checked implicitly in CompilationTestUtils by Tree.Kind parameter
    @SuppressWarnings("unchecked")
    LiteralTree ret =
        (LiteralTree) MoreTrees.findSubtree(baseTree(), Tree.Kind.STRING_LITERAL, "literal");
    return ret;
  }
View Full Code Here

TOP

Related Classes of com.sun.source.tree.LiteralTree

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.