SlideShare a Scribd company logo
Code generation for
alternative languages
and Java runtimes
interface Framework {
<T> Class<? extends T> secure(Class<T> type);
}
class Service {
@Secured(user = "ADMIN")
void deleteEverything() {
// delete everything...
}
}
@interface Secured {
String user();
}
class UserHolder {
static String user = "ANONYMOUS";
}
does not know aboutdepends on
discoversatruntime
class Service {
@Secured(user = "ADMIN")
void deleteEverything() {
if(!"ADMIN".equals(UserHolder.user)) {
throw new IllegalStateException("Wrong user");
}
// delete everything...
}
}
redefine class
(build time, agent)
create subclass
(Liskov substitution)
class SecuredService extends Service {
@Override
void deleteEverything() {
if(!"ADMIN".equals(UserHolder.user)) {
throw new IllegalStateException("Wrong user");
}
super.deleteEverything();
}
}
class Service {
@Secured(user = "ADMIN")
void deleteEverything() {
// delete everything...
}
}
0xCAFEBABE
source code
byte code
JVM
javac scalac groovyc jrubyc
JIT compilerinterpreter
class loader
creates
reads
runs
Code generation for alternative languages
class Sample$MockitoMock extends Sample {
@Override String getValue() {
return Mockito.mock(this,
new Object[0],
Sample.class.getMethod("getValue"));
}
String original$getValue() {
return super.getValue();
}
@Override void setValue(String x) {
Mockito.mock(this,
new Object[] { x },
Sample.class.getMethod("setValue",
String.class));
}
void original$setValue(String x) {
super.setValue(x);
}
}
class Sample {
String value;
String getValue() {
return value;
}
void setValue(String x) {
this.value = x;
}
}
class Sample {
var value: String? = null
fun getValue() = value
fun setValue(x: String?) {
this.value = x
}
}
final class Sample {
String value;
final String getValue() {
return value;
}
final void setValue(String x) {
this.value = x;
}
}
open class Sample {
var value: String? = null
open fun getValue() = value
open fun setValue(x: String?) {
this.value = x
}
}
class Sample {
String value;
String getValue() {
return value;
}
void setValue(String x) {
this.value = x;
}
}
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<configuration>
<compilerPlugins>
<plugin>all-open</plugin>
</compilerPlugins>
<pluginOptions>
<option>all-open:annotation=pkg.MyOpen</option>
</pluginOptions>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
annotation class MyOpen
final class Sample {
String value;
final String getValue() {
if (MockitoInfra.isMockMode(this)) {
return Mockito.mock( this,
new Object[0],
Sample.class.getMethod("getValue"));
} else {
return value;
}
}
final void setValue(String x) {
if (MockitoInfra.isMockMode(this)) {
Mockito.mock( this,
new Object[] { x },
Sample.class.getMethod("setValue",
String.class));
} else {
this.value = x;
}
}
}
final class Sample {
String value;
final String getValue() {
return value;
}
final void setValue(String x) {
this.value = x;
}
}
class MockitoInfra {
static final WeakConcurrentMap<Object, MockitoHandler> mocks;
static final ThreadLocal<Boolean> selfCall;
static boolean isMock(Object instance) {
return mocks.containsKey(instance);
}
static void registerMock(Object instance, MockHandler handler) {
mocks.put(instance, handler);
}
static boolean isMockMode(Object instance) {
return isMock(instance) && selfCall.get();
}
static void setMockMode(boolean mode) {
selfCall.set(mode);
}
}
https://github.com/raphw/weak-lock-free
package java.lang.instrumentation;
interface Instrumentation {
void addTransformer(ClassFileTransformer transformer, boolean retransform);
void retransformClasses(Class<?>... types);
void appendToBootstrapSearch(File file);
// ...
}
interface ClassFileTransformer {
byte[] transform(ClassLoader loader,
String name,
Class<?> type,
ProtectionDomain pd,
byte[] buffer);
}
NOPE!
Java virtual
machine
[stack, JIT]
Dalvik virtual
machine
[register, JIT]
Android
runtime
[register, AOT]
public class SimplePlugin implements Plugin {
@Override
public boolean matches(TypeDescription target) {
return target.getName().equals("Foo");
}
@Override
public DynamicType.Builder<?> apply(
DynamicType.Builder<?> builder,
TypeDescription typeDescription) {
return builder.method(named("bar"))
.intercept(value("Hello World!"));
}
}
https://github.com/raphw/byte-buddy
Build-time instrumentation with Maven.
<plugin>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<transformations>
<transformation>
<plugin>pkg.SimplePlugin</plugin>
</transformation>
</transformations>
</configuration>
</plugin>
http://rafael.codes
@rafaelcodes
http://documents4j.com
https://github.com/documents4j/documents4j
http://bytebuddy.net
https://github.com/raphw/byte-buddy

More Related Content

PPTX
Migrating to JUnit 5
PPTX
Making Java more dynamic: runtime code generation for the JVM
PPTX
Getting started with Java 9 modules
PPTX
Unit testing concurrent code
PPTX
Mastering Java Bytecode With ASM - 33rd degree, 2012
PDF
JDK Power Tools
PDF
Bytecode manipulation with Javassist and ASM
PDF
Java Bytecode for Discriminating Developers - JavaZone 2011
Migrating to JUnit 5
Making Java more dynamic: runtime code generation for the JVM
Getting started with Java 9 modules
Unit testing concurrent code
Mastering Java Bytecode With ASM - 33rd degree, 2012
JDK Power Tools
Bytecode manipulation with Javassist and ASM
Java Bytecode for Discriminating Developers - JavaZone 2011

What's hot (19)

PPTX
Use of Apache Commons and Utilities
PPTX
모던자바의 역습
PDF
GeeCON 2017 - TestContainers. Integration testing without the hassle
PPTX
Monitoring distributed (micro-)services
PPTX
Byte code field report
PPTX
Java 10, Java 11 and beyond
PDF
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
DOCX
Advance Java Programs skeleton
PDF
Java7 New Features and Code Examples
PPT
Building a java tracer
PDF
Sailing with Java 8 Streams
PDF
Java 8: the good, the bad and the ugly (JBCNConf 2017)
PDF
Java 8: the good, the bad and the ugly (Oracle Code Brussels 2017)
PDF
Apache Commons - Don\'t re-invent the wheel
DOC
Ad java prac sol set
PDF
Java 7 New Features
PPTX
Club of anonimous developers "Refactoring: Legacy code"
PDF
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
PDF
Java 5 and 6 New Features
Use of Apache Commons and Utilities
모던자바의 역습
GeeCON 2017 - TestContainers. Integration testing without the hassle
Monitoring distributed (micro-)services
Byte code field report
Java 10, Java 11 and beyond
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
Advance Java Programs skeleton
Java7 New Features and Code Examples
Building a java tracer
Sailing with Java 8 Streams
Java 8: the good, the bad and the ugly (JBCNConf 2017)
Java 8: the good, the bad and the ugly (Oracle Code Brussels 2017)
Apache Commons - Don\'t re-invent the wheel
Ad java prac sol set
Java 7 New Features
Club of anonimous developers "Refactoring: Legacy code"
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Java 5 and 6 New Features
Ad

Similar to Code generation for alternative languages (20)

PDF
Kotlin @ Devoxx 2011
PDF
Kotlin Slides from Devoxx 2011
PPTX
從零開始學 Android
PDF
Intro to Kotlin
PPTX
K is for Kotlin
PDF
Kotlin: A pragmatic language by JetBrains
PDF
Kotlin for Android Developers - 1
PDF
Kotlin, smarter development for the jvm
PDF
Little Helpers for Android Development with Kotlin
PPTX
Android Application Development (1).pptx
PDF
Building microservices with Kotlin
PDF
2014-11-01 01 Денис Нелюбин. О сортах кофе
PPTX
Kotlin : Happy Development
PDF
Kotlin: forse è la volta buona (Trento)
PDF
Kotlin: Why Do You Care?
PDF
A quick and fast intro to Kotlin
PPTX
Kotlin. One language to dominate them all.
PDF
Develop your next app with kotlin @ AndroidMakersFr 2017
PDF
Kotlin 在 Web 方面的应用
PPTX
Kotlin Language Features - A Java comparison
Kotlin @ Devoxx 2011
Kotlin Slides from Devoxx 2011
從零開始學 Android
Intro to Kotlin
K is for Kotlin
Kotlin: A pragmatic language by JetBrains
Kotlin for Android Developers - 1
Kotlin, smarter development for the jvm
Little Helpers for Android Development with Kotlin
Android Application Development (1).pptx
Building microservices with Kotlin
2014-11-01 01 Денис Нелюбин. О сортах кофе
Kotlin : Happy Development
Kotlin: forse è la volta buona (Trento)
Kotlin: Why Do You Care?
A quick and fast intro to Kotlin
Kotlin. One language to dominate them all.
Develop your next app with kotlin @ AndroidMakersFr 2017
Kotlin 在 Web 方面的应用
Kotlin Language Features - A Java comparison
Ad

More from Rafael Winterhalter (9)

PPTX
Java and OpenJDK: disecting the ecosystem
PPTX
The definitive guide to java agents
PPTX
Event-Sourcing Microservices on the JVM
PPTX
An Overview of Project Jigsaw
PPTX
The Java memory model made easy
PPTX
An introduction to JVM performance
PPTX
Java byte code in practice
PPTX
Understanding Java byte code and the class file format
PPTX
A topology of memory leaks on the JVM
Java and OpenJDK: disecting the ecosystem
The definitive guide to java agents
Event-Sourcing Microservices on the JVM
An Overview of Project Jigsaw
The Java memory model made easy
An introduction to JVM performance
Java byte code in practice
Understanding Java byte code and the class file format
A topology of memory leaks on the JVM

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPT
Introduction Database Management System for Course Database
PPTX
ai tools demonstartion for schools and inter college
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
L1 - Introduction to python Backend.pptx
PDF
top salesforce developer skills in 2025.pdf
PPTX
Introduction to Artificial Intelligence
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
medical staffing services at VALiNTRY
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
ManageIQ - Sprint 268 Review - Slide Deck
Internet Downloader Manager (IDM) Crack 6.42 Build 41
VVF-Customer-Presentation2025-Ver1.9.pptx
How to Choose the Right IT Partner for Your Business in Malaysia
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
How Creative Agencies Leverage Project Management Software.pdf
Introduction Database Management System for Course Database
ai tools demonstartion for schools and inter college
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Navsoft: AI-Powered Business Solutions & Custom Software Development
Operating system designcfffgfgggggggvggggggggg
L1 - Introduction to python Backend.pptx
top salesforce developer skills in 2025.pdf
Introduction to Artificial Intelligence
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
medical staffing services at VALiNTRY

Code generation for alternative languages