SlideShare a Scribd company logo
Understanding
Java byte code
and the class file format
0xCAFEBABE
source code
byte code
JVM
javac scalac groovyc jrubyc
JIT compilerinterpreter
class loader
creates
reads
runs
source code byte code
void foo() {
}
RETURNreturn; 0xB1
source code byte code
int foo() {
return 1 + 2;
}
ICONST_1
ICONST_2
IADD
operand stack 1
2
13
IRETURN
0x04
0x05
0x60
0xAC
source code byte code
ICONST_2
IADD
IRETURN
BIPUSH
int foo() {
return 11 + 2;
}
operand stack 11
2
1113
0x05
0x60
0xAC
0x10 110x0B
source code byte code
int foo(int i) {
return i + 1;
}
ILOAD_1
ICONST_1
IADD
IRETURN
operand stack
local variable array
1 : i
0 : this
i
1
ii + 1
0x1B
0x04
0x60
0xAC
source code byte code
ILOAD_1
ICONST_1
IADD
operand stack
local variable array
1 : i
0 : this
1
i
int foo(int i) {
int i2 = i + 1;
return i2;
}
ISTORE_2
ILOAD_2
IRETURN
2 : i + 1
ii + 1
2 :2 : i + 1
0x1B
0x04
0x60
0x3D
0x1C
0xAC
source code byte code
long foo(long i) {
return i + 1L;
}
LLOAD_1
LCONST_1
LADD
LRETURN
operand stack
local variable array
2 : i (cn.)
1 : i
0 : this
i (cn.)
i
0x1F
0x0A
0x61
0xAD
i (cn.)
i
1L
1L (cn.)
i + 1L
i + 1L (cn.)
source code byte code
short foo(short i) {
return (short) (i + 1);
}
ILOAD_1
ICONST_1
IADD
I2S
IRETURN
operand stack
local variable array
1 : i
0 : this
1
iii + 1
0x1B
0x04
0x60
0x93
0xAC
Java type JVM type (non-array) JVM descriptor stack slots
boolean
I
Z 1
byte B 1
short S 1
char C 1
int I 1
long L J 2
float F F 1
double D D 2
void - V 0
java.lang.Object A Ljava/lang/Object; 1
source code byte code
pkg/Bar.class
void foo() {
return;
}
RETURN
package pkg;
class Bar {
}
constant pool (i.a. UTF-8)
0x0000
0x0000: foo
0x0001: ()V
0x0002: pkg/Bar
0xB1
0x0000 0x0000foo ()V()V0x0001
0x00000 1 10x0001
operand stack /////////////////
local variable array 0 : this
source code byte code
package pkg;
class Bar {
public static void foo() {
return;
}
}
RETURN
0x0009
operand stack /////////////////
local variable array /////////////////
0 0
Modifier Value
static 0x0008
public 0x0001
0x0009
constant pool
0xB1
0x0000 0x0000
0x0000 0x0001foo ()V
source code byte code
package pkg;
class Bar {
int foo(int i) {
return foo(i + 1);
}
}
ILOAD_1
ICONST_1
IADD
INVOKEVIRTUAL pkg/Bar foo (I)I
ALOAD_0
IRETURN
operand stack
local variable array
1 : i
0 : this
this
i
1
this
ii + 1
foo(i + 1)
0x2A
0x1B
0x04
0x60
0xAC
0xB6
constant pool
0x0002 0x0000 0x0001
source code byte code
package pkg;
class Bar {
static int foo(int i) {
return foo(i + 1);
}
}
ILOAD_0
ICONST_1
IADD
INVOKESTATIC pkg/Bar foo (I)I
IRETURN
operand stack
local variable array 0 : i
i
1
ii + 1foo(i + 1)
0x1A
0x04
0x60
0xB8 0x0002 0x0001 0x0000
0xAC
constant pool
source code byte code
package pkg;
class Bar {
@Override
public String toString() {
return super.toString();
}
}
ALOAD_0
INVOKESPECIAL java/lang/Object
toString
()Ljava/lang/String;
ARETURN
operand stack
local variable array 0 : this
thistoString()
0x1A
0xB7 0x0004
0x0005
0x0006
0xAC
constant pool
INVOKESPECIAL
INVOKEVIRTUAL
INVOKESTATIC
INVOKEINTERFACE
INVOKEDYNAMIC
Invokes a static method.
pkg/Bar foo ()V
pkg/Bar foo ()V
Invokes the most-specific version of an inherited method on a non-interface class.
pkg/Bar foo ()V
Invokes a super class‘s version of an inherited method.
Invokes a “constructor method”.
Invokes a private method.
Invokes an interface default method (Java 8).
pkg/Bar foo ()V
Invokes an interface method.
(Similar to INVOKEVIRTUAL but without virtual method table index optimization.)
foo ()V bootstrap
Queries the given bootstrap method for locating a method implementation at runtime.
(MethodHandle: Combines a specific method and an INVOKE* instruction.)
void foo() {
???(); // invokedynamic
}
foo() ? qux()
bootstrap()
bar()
void foo() {
bar();
}
In theory, this could be achieved by using reflection. However, using invokedynamic
offers a more native approach. This is especially important when dealing with
primitive types (double boxing).
Used to implement lambda expressions, more important for dynamic languages.
source code byte code
0x1A
0x06
0xA4
0x03
0xAC
0x1A
0x04
0x60
0xB8 0x0002 0x0000 0x0001
0xAC
package pkg;
class Bar {
static int foo(int i) {
if (i > 3) {
return 0;
} else {
return foo(i + 1);
}
}
}
ILOAD_0
ICONST_3
IF_ICMPLE
ICONST_0
IRETURN
ILOAD_0
ICONST_1
IADD
INVOKESTATIC
IRETURN
pkg/Bar foo (I)I
X
X:
operand stack
local variable array 0 : i
i
3
i
1
i + 1foo(i + 1)
constant pool
0x0008
0008:
java.lang.VerifyError: Inconsistent stackmap frames at branch
target XXX in method pkg.Bar.foo(I)I
at offset YYY
type inferencing
(until Java 5, failover for Java 6)
Follow any possible path of jump
instructions: Assert the consistency of the
contents of the operand stack and local
variable array for any possible path.
Inference might require several iterations
over a method‘s byte code.
Legacy: -XX:-UseSplitVerifier
type checking
(from Java 6)
The verifier rejects malformed class files and byte code:
• compliance to class file format
• type safety
• access violations
Require the otherwise infered information
about the contents of the operand stack
and the local variable array to be
embedded in the code as stack map frames
at any target of a jump instruction.
Checking requires exactly one iteration over
a method‘s byte code.
source code byte code
int foo() {
try {
return 1;
} catch (Exception e) {
return 0;
}
}
ICONST_1
IRETURN
ICONST_0
IRETURN
exception table:
0000:
0001:
0002:
0003:
<from> <to> <catch> java/lang/Exception0x0000 0x0001 0x0002
constant pool
0x0005
0x04
0xAC
0x03
0xAC
source code byte code
package pkg;
class Bar {
void foo() {
return;
}
}
RETURN
0x0000 foo ()V
0 1
foo
()V
pkg/Bar
java/lang/Object
0x0000
pkg/Bar
java/lang/Object
[]
[]
0xCAFEBABE
0x0052
0000:
0001:
0002:
0003:
0x0000
0x0002
0x0003
0x0000
0x0000
0x0000 0x0000 0x0001
0xB1
0x0000 0x0001
UTF-8[foo]
UTF-8[()V]
UTF-8[pkg/Bar]
UTF-8[java/lang/Object]
0x0004
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...
}
}
Class<?> dynamicType = new ByteBuddy()
.subclass(Object.class)
.method(named("toString"))
.intercept(value("Hello World!"))
.make()
.load(getClass().getClassLoader(),
ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
assertThat(dynamicType.newInstance().toString(),
is("Hello World!"));
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
An introduction to JVM performance
PPTX
Java byte code in practice
PPTX
A topology of memory leaks on the JVM
PPTX
Unit testing concurrent code
PPTX
Making Java more dynamic: runtime code generation for the JVM
PPTX
Migrating to JUnit 5
PDF
Advanced Debugging Using Java Bytecodes
PPT
Java Basics
An introduction to JVM performance
Java byte code in practice
A topology of memory leaks on the JVM
Unit testing concurrent code
Making Java more dynamic: runtime code generation for the JVM
Migrating to JUnit 5
Advanced Debugging Using Java Bytecodes
Java Basics

What's hot (18)

PDF
Java Generics - by Example
PDF
Java Concurrency by Example
PDF
Java Fundamentals
PDF
Java Class Design
PPT
Learning Java 1 – Introduction
PPT
Initial Java Core Concept
ODP
Java Generics
PPTX
Use of Apache Commons and Utilities
PDF
Java Simple Programs
PDF
Java Programming - 03 java control flow
DOC
Final JAVA Practical of BCA SEM-5.
PDF
Lambda Functions in Java 8
PDF
Java 8 Lambda Built-in Functional Interfaces
PDF
Easy Going Groovy 2nd season on DevLOVE
PDF
Java programs
PPTX
Java generics final
PPTX
Java Generics
ODP
Java Concurrency
Java Generics - by Example
Java Concurrency by Example
Java Fundamentals
Java Class Design
Learning Java 1 – Introduction
Initial Java Core Concept
Java Generics
Use of Apache Commons and Utilities
Java Simple Programs
Java Programming - 03 java control flow
Final JAVA Practical of BCA SEM-5.
Lambda Functions in Java 8
Java 8 Lambda Built-in Functional Interfaces
Easy Going Groovy 2nd season on DevLOVE
Java programs
Java generics final
Java Generics
Java Concurrency
Ad

Viewers also liked (16)

PPTX
Code generation for alternative languages
PPT
How to Change Permissions and Install a Module in Drupal - Musings of a Drupa...
PDF
The power of information - easy read version
PPTX
XML Schema Difference Analysis
PDF
EclipseLink: Beyond Relational and NoSQL to Polyglot and HTML5
PPTX
The Java memory model made easy
PPT
An introductory guide to the United Nations Convention on the Rights of Perso...
PDF
Responsive Typography II
PDF
PRINCE2 Foundation Training Manual by Frank Turley
PDF
National Care Standards - Easy Read Version
PDF
Making Great Presentations
PDF
4 great public speaking tips effective presentation skills training
PDF
Formal & informal organisational
PPTX
Scientific Method Variables (Teach)
PDF
Going to Mars with Groovy Domain-Specific Languages
PPTX
4. heredity and evolution
Code generation for alternative languages
How to Change Permissions and Install a Module in Drupal - Musings of a Drupa...
The power of information - easy read version
XML Schema Difference Analysis
EclipseLink: Beyond Relational and NoSQL to Polyglot and HTML5
The Java memory model made easy
An introductory guide to the United Nations Convention on the Rights of Perso...
Responsive Typography II
PRINCE2 Foundation Training Manual by Frank Turley
National Care Standards - Easy Read Version
Making Great Presentations
4 great public speaking tips effective presentation skills training
Formal & informal organisational
Scientific Method Variables (Teach)
Going to Mars with Groovy Domain-Specific Languages
4. heredity and evolution
Ad

Similar to Understanding Java byte code and the class file format (20)

PPTX
Arithmatic logic unit using VHDL (gates)
PDF
Javascript engine performance
PPTX
JVM bytecode - The secret language behind Java and Scala
PDF
How to avoid Go gotchas - Ivan Daniluk - Codemotion Milan 2016
PDF
Kotlin @ Devoxx 2011
PDF
Kotlin Slides from Devoxx 2011
KEY
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
PDF
StackOverflow
PDF
sizeof(Object): how much memory objects take on JVMs and when this may matter
PDF
Kotlin Bytecode Generation and Runtime Performance
PDF
Faster Python, FOSDEM
PDF
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
PPTX
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
KEY
Why Learn Python?
PDF
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
DOC
Computer hw1
PPTX
Lecture 04 Programming C for Beginners 001
KEY
JavaOne 2012 - JVM JIT for Dummies
PDF
Virtual machine and javascript engine
Arithmatic logic unit using VHDL (gates)
Javascript engine performance
JVM bytecode - The secret language behind Java and Scala
How to avoid Go gotchas - Ivan Daniluk - Codemotion Milan 2016
Kotlin @ Devoxx 2011
Kotlin Slides from Devoxx 2011
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
StackOverflow
sizeof(Object): how much memory objects take on JVMs and when this may matter
Kotlin Bytecode Generation and Runtime Performance
Faster Python, FOSDEM
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Why Learn Python?
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
Computer hw1
Lecture 04 Programming C for Beginners 001
JavaOne 2012 - JVM JIT for Dummies
Virtual machine and javascript engine

More from Rafael Winterhalter (8)

PPTX
Java and OpenJDK: disecting the ecosystem
PPTX
The definitive guide to java agents
PPTX
Byte code field report
PPTX
Event-Sourcing Microservices on the JVM
PPTX
Java 10, Java 11 and beyond
PPTX
Getting started with Java 9 modules
PPTX
Monitoring distributed (micro-)services
PPTX
An Overview of Project Jigsaw
Java and OpenJDK: disecting the ecosystem
The definitive guide to java agents
Byte code field report
Event-Sourcing Microservices on the JVM
Java 10, Java 11 and beyond
Getting started with Java 9 modules
Monitoring distributed (micro-)services
An Overview of Project Jigsaw

Recently uploaded (20)

PDF
System and Network Administration Chapter 2
PDF
medical staffing services at VALiNTRY
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Materi-Enum-and-Record-Data-Type (1).pptx
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
System and Network Administraation Chapter 3
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
PDF
AI in Product Development-omnex systems
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
DOCX
The Five Best AI Cover Tools in 2025.docx
PDF
PTS Company Brochure 2025 (1).pdf.......
System and Network Administration Chapter 2
medical staffing services at VALiNTRY
Materi_Pemrograman_Komputer-Looping.pptx
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Materi-Enum-and-Record-Data-Type (1).pptx
ManageIQ - Sprint 268 Review - Slide Deck
System and Network Administraation Chapter 3
The Role of Automation and AI in EHS Management for Data Centers.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Softaken Excel to vCard Converter Software.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
A REACT POMODORO TIMER WEB APPLICATION.pdf
AI in Product Development-omnex systems
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
The Five Best AI Cover Tools in 2025.docx
PTS Company Brochure 2025 (1).pdf.......

Understanding Java byte code and the class file format

  • 1. Understanding Java byte code and the class file format
  • 2. 0xCAFEBABE source code byte code JVM javac scalac groovyc jrubyc JIT compilerinterpreter class loader creates reads runs
  • 3. source code byte code void foo() { } RETURNreturn; 0xB1
  • 4. source code byte code int foo() { return 1 + 2; } ICONST_1 ICONST_2 IADD operand stack 1 2 13 IRETURN 0x04 0x05 0x60 0xAC
  • 5. source code byte code ICONST_2 IADD IRETURN BIPUSH int foo() { return 11 + 2; } operand stack 11 2 1113 0x05 0x60 0xAC 0x10 110x0B
  • 6. source code byte code int foo(int i) { return i + 1; } ILOAD_1 ICONST_1 IADD IRETURN operand stack local variable array 1 : i 0 : this i 1 ii + 1 0x1B 0x04 0x60 0xAC
  • 7. source code byte code ILOAD_1 ICONST_1 IADD operand stack local variable array 1 : i 0 : this 1 i int foo(int i) { int i2 = i + 1; return i2; } ISTORE_2 ILOAD_2 IRETURN 2 : i + 1 ii + 1 2 :2 : i + 1 0x1B 0x04 0x60 0x3D 0x1C 0xAC
  • 8. source code byte code long foo(long i) { return i + 1L; } LLOAD_1 LCONST_1 LADD LRETURN operand stack local variable array 2 : i (cn.) 1 : i 0 : this i (cn.) i 0x1F 0x0A 0x61 0xAD i (cn.) i 1L 1L (cn.) i + 1L i + 1L (cn.)
  • 9. source code byte code short foo(short i) { return (short) (i + 1); } ILOAD_1 ICONST_1 IADD I2S IRETURN operand stack local variable array 1 : i 0 : this 1 iii + 1 0x1B 0x04 0x60 0x93 0xAC
  • 10. Java type JVM type (non-array) JVM descriptor stack slots boolean I Z 1 byte B 1 short S 1 char C 1 int I 1 long L J 2 float F F 1 double D D 2 void - V 0 java.lang.Object A Ljava/lang/Object; 1
  • 11. source code byte code pkg/Bar.class void foo() { return; } RETURN package pkg; class Bar { } constant pool (i.a. UTF-8) 0x0000 0x0000: foo 0x0001: ()V 0x0002: pkg/Bar 0xB1 0x0000 0x0000foo ()V()V0x0001 0x00000 1 10x0001 operand stack ///////////////// local variable array 0 : this
  • 12. source code byte code package pkg; class Bar { public static void foo() { return; } } RETURN 0x0009 operand stack ///////////////// local variable array ///////////////// 0 0 Modifier Value static 0x0008 public 0x0001 0x0009 constant pool 0xB1 0x0000 0x0000 0x0000 0x0001foo ()V
  • 13. source code byte code package pkg; class Bar { int foo(int i) { return foo(i + 1); } } ILOAD_1 ICONST_1 IADD INVOKEVIRTUAL pkg/Bar foo (I)I ALOAD_0 IRETURN operand stack local variable array 1 : i 0 : this this i 1 this ii + 1 foo(i + 1) 0x2A 0x1B 0x04 0x60 0xAC 0xB6 constant pool 0x0002 0x0000 0x0001
  • 14. source code byte code package pkg; class Bar { static int foo(int i) { return foo(i + 1); } } ILOAD_0 ICONST_1 IADD INVOKESTATIC pkg/Bar foo (I)I IRETURN operand stack local variable array 0 : i i 1 ii + 1foo(i + 1) 0x1A 0x04 0x60 0xB8 0x0002 0x0001 0x0000 0xAC constant pool
  • 15. source code byte code package pkg; class Bar { @Override public String toString() { return super.toString(); } } ALOAD_0 INVOKESPECIAL java/lang/Object toString ()Ljava/lang/String; ARETURN operand stack local variable array 0 : this thistoString() 0x1A 0xB7 0x0004 0x0005 0x0006 0xAC constant pool
  • 16. INVOKESPECIAL INVOKEVIRTUAL INVOKESTATIC INVOKEINTERFACE INVOKEDYNAMIC Invokes a static method. pkg/Bar foo ()V pkg/Bar foo ()V Invokes the most-specific version of an inherited method on a non-interface class. pkg/Bar foo ()V Invokes a super class‘s version of an inherited method. Invokes a “constructor method”. Invokes a private method. Invokes an interface default method (Java 8). pkg/Bar foo ()V Invokes an interface method. (Similar to INVOKEVIRTUAL but without virtual method table index optimization.) foo ()V bootstrap Queries the given bootstrap method for locating a method implementation at runtime. (MethodHandle: Combines a specific method and an INVOKE* instruction.)
  • 17. void foo() { ???(); // invokedynamic } foo() ? qux() bootstrap() bar() void foo() { bar(); } In theory, this could be achieved by using reflection. However, using invokedynamic offers a more native approach. This is especially important when dealing with primitive types (double boxing). Used to implement lambda expressions, more important for dynamic languages.
  • 18. source code byte code 0x1A 0x06 0xA4 0x03 0xAC 0x1A 0x04 0x60 0xB8 0x0002 0x0000 0x0001 0xAC package pkg; class Bar { static int foo(int i) { if (i > 3) { return 0; } else { return foo(i + 1); } } } ILOAD_0 ICONST_3 IF_ICMPLE ICONST_0 IRETURN ILOAD_0 ICONST_1 IADD INVOKESTATIC IRETURN pkg/Bar foo (I)I X X: operand stack local variable array 0 : i i 3 i 1 i + 1foo(i + 1) constant pool 0x0008 0008:
  • 19. java.lang.VerifyError: Inconsistent stackmap frames at branch target XXX in method pkg.Bar.foo(I)I at offset YYY type inferencing (until Java 5, failover for Java 6) Follow any possible path of jump instructions: Assert the consistency of the contents of the operand stack and local variable array for any possible path. Inference might require several iterations over a method‘s byte code. Legacy: -XX:-UseSplitVerifier type checking (from Java 6) The verifier rejects malformed class files and byte code: • compliance to class file format • type safety • access violations Require the otherwise infered information about the contents of the operand stack and the local variable array to be embedded in the code as stack map frames at any target of a jump instruction. Checking requires exactly one iteration over a method‘s byte code.
  • 20. source code byte code int foo() { try { return 1; } catch (Exception e) { return 0; } } ICONST_1 IRETURN ICONST_0 IRETURN exception table: 0000: 0001: 0002: 0003: <from> <to> <catch> java/lang/Exception0x0000 0x0001 0x0002 constant pool 0x0005 0x04 0xAC 0x03 0xAC
  • 21. source code byte code package pkg; class Bar { void foo() { return; } } RETURN 0x0000 foo ()V 0 1 foo ()V pkg/Bar java/lang/Object 0x0000 pkg/Bar java/lang/Object [] [] 0xCAFEBABE 0x0052 0000: 0001: 0002: 0003: 0x0000 0x0002 0x0003 0x0000 0x0000 0x0000 0x0000 0x0001 0xB1 0x0000 0x0001 UTF-8[foo] UTF-8[()V] UTF-8[pkg/Bar] UTF-8[java/lang/Object] 0x0004
  • 22. 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... } }
  • 23. Class<?> dynamicType = new ByteBuddy() .subclass(Object.class) .method(named("toString")) .intercept(value("Hello World!")) .make() .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(dynamicType.newInstance().toString(), is("Hello World!"));