Using Generated ASTs

View: New views
2 Messages — Rating Filter:   Alert me  

Using Generated ASTs

by Matt Fowles :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

All~

I have the following code (assume that getLocation() does something reasonable):

        CompilationUnit cu = new CompilationUnit("AstTests.java");

        PackageMemberClassDeclaration clazz = new PackageMemberClassDeclaration(
                getLocation(),
                null,
                Mod.PUBLIC,
                "HandMade",
                null,
                new Type[]{}
        );

        Block body = new Block(getLocation());
        body.addStatement(
                new ReturnStatement(
                        getLocation(),
                        new Literal(
                                getLocation(),
                                Integer.valueOf(3)
                        )
                )
        );

        MethodDeclarator method = new MethodDeclarator(
                getLocation(),
                null,
                (short)(Mod.PUBLIC),
                new BasicType(getLocation(), BasicType.DOUBLE),
                "caclulate",
                new FormalParameter[0],
                new Type[0],
                body
        );
        clazz.addDeclaredMethod(method);
        cu.addPackageMemberTypeDeclaration(clazz);

which I believe is identical to:

class HandMade {
    public double calculate() {
        return 3;
    }
}


How do I go from my CompilationUnit cu, to having an instance of
Class<HandMade>?

Thanks,
Matt

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Using Generated ASTs

by Matt Fowles :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

All~

I was able to achieve what I wanted by adding the following method to
SimpleCompiler.  Is this the correct approach?

=== src/org/codehaus/janino/SimpleCompiler.java
==================================================================
--- src/org/codehaus/janino/SimpleCompiler.java (revision 72624)
+++ src/org/codehaus/janino/SimpleCompiler.java (local)
@@ -233,7 +233,18 @@
             DebuggingInformation.DEFAULT_DEBUGGING_INFORMATION
         );
     }
+
+    public void cook(Java.CompilationUnit compilationUnit)
+    throws CompileException, Parser.ParseException,
Scanner.ScanException, IOException {
+        this.setUpClassLoaders();

+        // Compile the classes and load them.
+        this.compileToClassLoader(
+            compilationUnit,
+            DebuggingInformation.DEFAULT_DEBUGGING_INFORMATION
+        );
+    }
+

Matt

On Tue, Apr 29, 2008 at 5:47 PM, Matt Fowles <matt.fowles@...> wrote:

> All~
>
>  I have the following code (assume that getLocation() does something reasonable):
>
>         CompilationUnit cu = new CompilationUnit("AstTests.java");
>
>         PackageMemberClassDeclaration clazz = new PackageMemberClassDeclaration(
>                 getLocation(),
>                 null,
>                 Mod.PUBLIC,
>                 "HandMade",
>                 null,
>                 new Type[]{}
>         );
>
>         Block body = new Block(getLocation());
>         body.addStatement(
>                 new ReturnStatement(
>                         getLocation(),
>                         new Literal(
>                                 getLocation(),
>                                 Integer.valueOf(3)
>                         )
>                 )
>         );
>
>         MethodDeclarator method = new MethodDeclarator(
>                 getLocation(),
>                 null,
>                 (short)(Mod.PUBLIC),
>                 new BasicType(getLocation(), BasicType.DOUBLE),
>                 "caclulate",
>                 new FormalParameter[0],
>                 new Type[0],
>                 body
>         );
>         clazz.addDeclaredMethod(method);
>         cu.addPackageMemberTypeDeclaration(clazz);
>
>  which I believe is identical to:
>
>  class HandMade {
>     public double calculate() {
>         return 3;
>     }
>  }
>
>
>  How do I go from my CompilationUnit cu, to having an instance of
>  Class<HandMade>?
>
>  Thanks,
>  Matt
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email