• Stars
    star
    617
  • Rank 70,257 (Top 2 %)
  • Language
    Java
  • License
    Other
  • Created over 10 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Java Runtime Compiler

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.

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.

More Repositories

1

Chronicle-Queue

Micro second messaging that stores everything to disk
Java
3,130
star
2

Chronicle-Map

Replicate your Key Value Store across your network, with consistency, persistance and performance.
Java
2,669
star
3

Java-Thread-Affinity

Bind a java thread to a given core
Java
1,724
star
4

Zero-Allocation-Hashing

Zero-allocation hashing for Java
Java
762
star
5

OpenHFT

Parent module to include active modules
Shell
609
star
6

Chronicle-Core

Low level access to native memory, JVM and OS.
Java
528
star
7

Chronicle-Wire

A Low Garbage Java Serialisation Library that supports multiple formats
Java
464
star
8

Chronicle-Bytes

Chronicle Bytes has a similar purpose to Java NIO's ByteBuffer with many extensions
Java
382
star
9

Chronicle-Engine

A high performance, low latency, reactive processing framework
338
star
10

Java-Lang

Java Language support
Java
286
star
11

Chronicle-Network

A High Performance Network ( TCP/IP ) Library
Java
243
star
12

Chronicle-Logger

A sub microsecond java logger, supporting standard logging APIs such as Slf & Log4J
Java
220
star
13

Chronicle-Threads

Java
167
star
14

Chronicle-Values

Java
102
star
15

Chronicle-Algorithms

Java
77
star
16

JLBH

JLBH
Java
68
star
17

Chronicle-Queue-Demo

Sample programs for Chronicle Queue
Java
65
star
18

Chronicle-Accelerate

HFT meets Blockchain in Java platform
Java
59
star
19

Chronicle-TimeSeries

Multi-Threaded Time Series library
Java
59
star
20

Chronicle-Decentred

Framework for building Secure Scalable Microservices on Distributed Ledger Technology
Java
50
star
21

Chronicle-Salt

Chronicle wrapper for the NaCl library
Java
30
star
22

Chronicle-Test-Framework

Java
24
star
23

Chronicle-Ticker

A time ticker as a light weight clock
Java
23
star
24

RFC

RFC's used by OpenHFT
21
star
25

Chronicle-Websocket-Jetty

Provide Websocket access via Jetty
Java
17
star
26

Puzzles

OpenHFT Puzzles
Java
15
star
27

jvm-micro-benchmarks

Microbenchmarks for JVM code.
Java
14
star
28

Spoon

Java
11
star
29

Chronicle-Engine-GUI

CSS
11
star
30

Exception-Handler

Open a browser with a message about an Exception.
Java
10
star
31

Stage-Compiler

Java
8
star
32

Binary-Compatibility-Enforcer-Plugin

Wraps the Java API ComplianceChecker into a maven plugin
Java
7
star
33

Chronicle-Common

Java
6
star
34

Chronicle-Coder

Encode and decode data as symbols and readable words
Java
5
star
35

Posix

Java
5
star
36

Chronicle-Analytics

Support for Analytics
Java
4
star
37

Microservice-Benchmark

Open Microservices Benchmark
Java
4
star