Example #1
0
 public void testClassDecl() {
   final NewExpression newExpression =
       Expressions.new_(
           Object.class,
           Collections.<Expression>emptyList(),
           Arrays.<MemberDeclaration>asList(
               new FieldDeclaration(
                   Modifier.PUBLIC | Modifier.FINAL,
                   new ParameterExpression(String.class, "foo"),
                   Expressions.constant("bar")),
               new ClassDeclaration(
                   Modifier.PUBLIC | Modifier.STATIC,
                   "MyClass",
                   null,
                   Collections.<Type>emptyList(),
                   Arrays.<MemberDeclaration>asList(
                       new FieldDeclaration(
                           0, new ParameterExpression(int.class, "x"), Expressions.constant(0)))),
               new FieldDeclaration(0, new ParameterExpression(int.class, "i"), null)));
   assertEquals(
       "new Object(){\n"
           + "  public final String foo = \"bar\";\n"
           + "  public static class MyClass {\n"
           + "    int x = 0;\n"
           + "  }\n"
           + "  int i;\n"
           + "}",
       Expressions.toString(newExpression));
 }
Example #2
0
 public void testBlockBuilder2() {
   BlockBuilder statements = new BlockBuilder();
   Expression element = statements.append("element", Expressions.constant(null));
   Expression comparator =
       statements.append("comparator", Expressions.constant(null, Comparator.class));
   Expression treeSet =
       statements.append("treeSet", Expressions.new_(TreeSet.class, Arrays.asList(comparator)));
   statements.add(Expressions.return_(null, Expressions.call(treeSet, "add", element)));
   assertEquals(
       "{\n"
           + "  final java.util.Comparator comparator = null;\n"
           + "  final java.util.TreeSet treeSet = new java.util.TreeSet(\n"
           + "    comparator);\n"
           + "  return treeSet.add(null);\n"
           + "}\n",
       Expressions.toString(statements.toBlock()));
 }
Example #3
0
 public void testWriteAnonymousClass() {
   // final List<String> baz = Arrays.asList("foo", "bar");
   // new AbstractList<String>() {
   //     public int size() {
   //         return baz.size();
   //     }
   //     public String get(int index) {
   //         return ((String) baz.get(index)).toUpperCase();
   //     }
   // }
   final ParameterExpression bazParameter =
       Expressions.parameter(Types.of(List.class, String.class), "baz");
   final ParameterExpression indexParameter = Expressions.parameter(Integer.TYPE, "index");
   BlockExpression e =
       Expressions.block(
           Expressions.declare(
               Modifier.FINAL,
               bazParameter,
               Expressions.call(
                   Arrays.class,
                   "asList",
                   Arrays.<Expression>asList(
                       Expressions.constant("foo"), Expressions.constant("bar")))),
           Expressions.statement(
               Expressions.new_(
                   Types.of(AbstractList.class, String.class),
                   Collections.<Expression>emptyList(),
                   Arrays.<MemberDeclaration>asList(
                       Expressions.fieldDecl(
                           Modifier.PUBLIC | Modifier.FINAL,
                           Expressions.parameter(String.class, "qux"),
                           Expressions.constant("xyzzy")),
                       Expressions.methodDecl(
                           Modifier.PUBLIC,
                           Integer.TYPE,
                           "size",
                           Collections.<ParameterExpression>emptyList(),
                           Blocks.toFunctionBlock(
                               Expressions.call(
                                   bazParameter, "size", Collections.<Expression>emptyList()))),
                       Expressions.methodDecl(
                           Modifier.PUBLIC,
                           String.class,
                           "get",
                           Arrays.asList(indexParameter),
                           Blocks.toFunctionBlock(
                               Expressions.call(
                                   Expressions.convert_(
                                       Expressions.call(
                                           bazParameter,
                                           "get",
                                           Arrays.<Expression>asList(indexParameter)),
                                       String.class),
                                   "toUpperCase",
                                   Collections.<Expression>emptyList())))))));
   assertEquals(
       "{\n"
           + "  final java.util.List<String> baz = java.util.Arrays.asList(\"foo\", \"bar\");\n"
           + "  new java.util.AbstractList<String>(){\n"
           + "    public final String qux = \"xyzzy\";\n"
           + "    public int size() {\n"
           + "      return baz.size();\n"
           + "    }\n"
           + "\n"
           + "    public String get(int index) {\n"
           + "      return ((String) baz.get(index)).toUpperCase();\n"
           + "    }\n"
           + "\n"
           + "  };\n"
           + "}\n",
       Expressions.toString(e));
 }