Coder Social home page Coder Social logo

java-runtime-compiler's Introduction

Chronicle Runtime Compiler

badge javadoc GitHub release%20notes subscribe brightgreen measure?project=OpenHFT Java Runtime Compiler&metric=alert status

This takes a String, compiles it and loads it returning you a class from what you built. By default it uses the current ClassLoader. It supports nested classes, otherwise builds one class at a time.

On maven central

You can include in your project with

<dependency>
    <groupId>net.openhft</groupId>
    <artifactId>compiler</artifactId>
    <version><!-- The latest version (see above) --></version>
</dependency>

Simple example

You need a CachedCompiler and access to your JDK’s tools.jar.

// dynamically you can call
String className = "mypackage.MyClass";
String javaCode = "package mypackage;\n" +
                 "public class MyClass implements Runnable {\n" +
                 "    public void run() {\n" +
                 "        System.out.println(\"Hello World\");\n" +
                 "    }\n" +
                 "}\n";
Class aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(className, javaCode);
Runnable runner = (Runnable) aClass.newInstance();
runner.run();
````

I suggest making your class implement a KnownInterface of your choice as this will allow you to call/manipulate instances of you generated class.

Another more hacky way is to use this to override a class, provided it hasn't been loaded already.
This means you can redefine an existing class and provide the methods and fields used match,
you have compiler redefine a class and code already compiled to use the class will still work.

## Using the CachedCompiler.

In this example, you can configure the compiler to write the files to a specific directory when you are in debug mode.

```java
private static final CachedCompiler JCC = CompilerUtils.DEBUGGING ?
                                                   new CachedCompiler(new File(parent, "src/test/java"), new File(parent, "target/compiled")) :
                                                   CompilerUtils.CACHED_COMPILER;

By selecting the src directory to match where your IDE looks for those files, it will allow your debugger to set into the code you have generated at runtime.

Note: you may need to delete these files if you want to regenerate them.

java-runtime-compiler's People

Contributors

danielshaya avatar dekmm avatar dependabot[bot] avatar dpisklov avatar epickrram avatar glukos avatar hft-team-city avatar jansturenielsen avatar jerryshea avatar lburgazzoli avatar leventov avatar minborg avatar mlesniak avatar nvdpsingh avatar peter-lawrey avatar robaustin avatar sullis avatar surajpattanaik avatar tomshercliff avatar yukoba avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.