SlideShare a Scribd company logo
IMPROVING THE JAVA
TYPE SYSTEM
João Loff - 56960
Alexandre Almeida - 64712
Tiago Aguiar - 64870
BEHAVIORS
180 LOC
“CtBehavior represents a method, a constructor, or
a static constructor”
for (CtBehavior behavior : clazz.getDeclaredBehaviors()){
behavior.instrument(new ExprEditor() { ... });
if(behavior instanceof CtMethod) { ...}
if(behavior instanceof CtConstructor) { ... }
}
FIELD INITIALIZATION
We start with:
We end up with:
@Assertion("true")
int foo;
foo++;
static java.util.HashSet f$writes = new java.util.HashSet();
@Assertion("true")
int foo;
if(!f$writes.contains(foo_hash_code)) //...
foo++
f$writes.add(foo_hash_code);
if(!(true)) //...
FIELD INITIALIZATION
How to we solve this?
DOUBLE CODE INJECTION
@Assertion("true")
int foo;
@Assertion("bar>foo")
int bar;
bar=1;
foo++;
FIELD INITIALIZATION
The first injection
The second injection
if(!f$writes.contains(bar_hash_code)) //...
bar=1;
f$writes.add(bar_hash_code);
if(!(bar>foo)) //...
if(!f$writes.contains(bar_hash_code)) //...
bar=1;
f$writes.add(bar_hash_code);
if(!f$writes.contains(bar_hash_code)) //...
if(!f$writes.contains(foo_hash_code)) //...
if(!(bar>foo)) //...
f$writes.add(bar_hash_code);
if(!(bar>foo)) //...
METHOD ASSERTION
This shouldn't return NPE!
METHOD WRAPPING
Object o = new Object();
public Object getAndClear() {
return this.o = null;
}
@Assertion("getAndClear() != null")
public void m() {
this.o.toString();
}
METHOD ASSERTION
We end up with something kinda like this:
A "new" method that returns the result of the original one
public void m() {
// temp "is a" bytecode var
temp = m$wrapped();
// now we check assertions
if(!(getAndClear() != null)) //...
// and now we return!
return temp;
}
@Assertion("getAndClear() != null")
public void m$wrapped() {
this.o.toString();
}
METHOD ASSERTIONS
Inherited Method Assertions
We recursively inject code from parents assertions
class Base {
@Assertion("false")
public int fooBar(int x) { ... }
}
public class Derived extends Base {
@Override
@Assertion("true")
public int fooBar(int x) { ... }
}
public int fooBar(int x) {
temp = fooBar$wrapped(x);
if(!(false)) //...
if(!(true)) //...
return temp;
}
CONSTRUCTOR ASSERTIONS
Remember Behaviors?
That was easy.
if((behavior instanceof CtConstructor) && hasAssertion(behavior)){
CtConstructor constructor = (CtConstructor) behavior;
constructor.insertBeforeBody(
"if(!(" + getAssertionValues(constructor) + ")) //... "
);
}
ASSERTIONS ON METHOD ENTRY
We can't break original functionality!
Previous example, now with entry assertion
Object o = new Object();
public Object getAndClear() {
return this.o = null;
}
@Assertion(
value="getAndClear() != null",
entry="getAndClear() == null"
)
public void m() {
this.o.toString();
}
ASSERTIONS ON METHOD ENTRY
We inject code at begining of method and at the exit.
It should return NPE!
public void m() {
// entry assertion
if(!(getAndClear() == null)) //...
temp = m$wrapped();
// value assertion
if(!(getAndClear() != null)) //...
return temp;
}
## As you can see (getAndClear() == null) expression returned true
[java] Exception in thread "main" java.lang.NullPointerException
[java] at ist.meic.pa.tests.Base.m$wrapped(Unknown Source)
[java] at ist.meic.pa.tests.Base.m(Unknown Source)
[java] ...
[java] The assertion getAndClear() != null is false
QUESTIONS?

More Related Content

PDF
JavaScript - Agora nervoso
PDF
C++ Programming - 1st Study
PPT
Intro to c programming
PDF
C++ Programming - 2nd Study
PPTX
Namespaces
KEY
Javascript - The core
KEY
Python Yield
PPTX
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
JavaScript - Agora nervoso
C++ Programming - 1st Study
Intro to c programming
C++ Programming - 2nd Study
Namespaces
Javascript - The core
Python Yield
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs

What's hot (20)

PDF
Burrowing through go! the book
PPTX
Lexical environment in ecma 262 5
PPT
C++totural file
PPT
PPTX
Type Driven Development with TypeScript
PDF
Lecture 4
PDF
Advanced Patterns with io.ReadWriter
PPTX
Самые вкусные баги из игрового кода: как ошибаются наши коллеги-программисты ...
PDF
Standford 2015 week9
PPT
Class ‘increment’
PDF
[KOSSA] C++ Programming - 17th Study - STL #3
PDF
Generics and Inference
KEY
openFrameworks 007 - 3D
PPTX
C sharp 8
PDF
Static and const members
PDF
Inheritance and polymorphism
PPTX
Groovy grails types, operators, objects
PPT
Cquestions
PDF
C++ L05-Functions
Burrowing through go! the book
Lexical environment in ecma 262 5
C++totural file
Type Driven Development with TypeScript
Lecture 4
Advanced Patterns with io.ReadWriter
Самые вкусные баги из игрового кода: как ошибаются наши коллеги-программисты ...
Standford 2015 week9
Class ‘increment’
[KOSSA] C++ Programming - 17th Study - STL #3
Generics and Inference
openFrameworks 007 - 3D
C sharp 8
Static and const members
Inheritance and polymorphism
Groovy grails types, operators, objects
Cquestions
C++ L05-Functions
Ad

Viewers also liked (13)

PDF
NCOSM Sociale media in het PO
PPTX
DOCX
Norm iso
PPTX
Universidad Soñada
PDF
Vashishtha_ABB_2015
PDF
PDF
Tammys Genealogy Resume Final
PDF
Some tears are less newsworthy
DOC
устав 2013
DOC
112476 oec8wo0 xva_20140328091033_65671
PDF
Soluciones 2 art 9
DOCX
Conferencistas
PPTX
Wycieczka: Kraków i Sandomierz
NCOSM Sociale media in het PO
Norm iso
Universidad Soñada
Vashishtha_ABB_2015
Tammys Genealogy Resume Final
Some tears are less newsworthy
устав 2013
112476 oec8wo0 xva_20140328091033_65671
Soluciones 2 art 9
Conferencistas
Wycieczka: Kraków i Sandomierz
Ad

Similar to Improving the java type system (20)

PDF
Google guava
PDF
ES6 patterns in the wild
PDF
  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf
DOCX
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
PPTX
ES6 Overview
PDF
Unbearable Test Code Smell
PPT
SDC - Einführung in Scala
PDF
Kotlin Generation
PPTX
Making Java more dynamic: runtime code generation for the JVM
PDF
Java 8 - Nuts and Bold - SFEIR Benelux
PDF
Transaction is a monad
PDF
TypeScript Introduction
PDF
JEEConf 2017 - Having fun with Javassist
PDF
COScheduler In Depth
PDF
Jason parsing
PPTX
Property Based Testing
PPT
OBJECTS IN Object Oriented Programming .ppt
ODP
Introduccion a Jasmin
PPSX
What's New In C# 7
PDF
Kotlin Overview (PT-BR)
Google guava
ES6 patterns in the wild
  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
ES6 Overview
Unbearable Test Code Smell
SDC - Einführung in Scala
Kotlin Generation
Making Java more dynamic: runtime code generation for the JVM
Java 8 - Nuts and Bold - SFEIR Benelux
Transaction is a monad
TypeScript Introduction
JEEConf 2017 - Having fun with Javassist
COScheduler In Depth
Jason parsing
Property Based Testing
OBJECTS IN Object Oriented Programming .ppt
Introduccion a Jasmin
What's New In C# 7
Kotlin Overview (PT-BR)

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Spectroscopy.pptx food analysis technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Electronic commerce courselecture one. Pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Empathic Computing: Creating Shared Understanding
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Encapsulation theory and applications.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Reach Out and Touch Someone: Haptics and Empathic Computing
NewMind AI Weekly Chronicles - August'25-Week II
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation_ Review paper, used for researhc scholars
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
sap open course for s4hana steps from ECC to s4
Spectroscopy.pptx food analysis technology
Review of recent advances in non-invasive hemoglobin estimation
Electronic commerce courselecture one. Pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Empathic Computing: Creating Shared Understanding
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The AUB Centre for AI in Media Proposal.docx
Encapsulation theory and applications.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Assigned Numbers - 2025 - Bluetooth® Document
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton

Improving the java type system

  • 1. IMPROVING THE JAVA TYPE SYSTEM João Loff - 56960 Alexandre Almeida - 64712 Tiago Aguiar - 64870
  • 2. BEHAVIORS 180 LOC “CtBehavior represents a method, a constructor, or a static constructor” for (CtBehavior behavior : clazz.getDeclaredBehaviors()){ behavior.instrument(new ExprEditor() { ... }); if(behavior instanceof CtMethod) { ...} if(behavior instanceof CtConstructor) { ... } }
  • 3. FIELD INITIALIZATION We start with: We end up with: @Assertion("true") int foo; foo++; static java.util.HashSet f$writes = new java.util.HashSet(); @Assertion("true") int foo; if(!f$writes.contains(foo_hash_code)) //... foo++ f$writes.add(foo_hash_code); if(!(true)) //...
  • 4. FIELD INITIALIZATION How to we solve this? DOUBLE CODE INJECTION @Assertion("true") int foo; @Assertion("bar>foo") int bar; bar=1; foo++;
  • 5. FIELD INITIALIZATION The first injection The second injection if(!f$writes.contains(bar_hash_code)) //... bar=1; f$writes.add(bar_hash_code); if(!(bar>foo)) //... if(!f$writes.contains(bar_hash_code)) //... bar=1; f$writes.add(bar_hash_code); if(!f$writes.contains(bar_hash_code)) //... if(!f$writes.contains(foo_hash_code)) //... if(!(bar>foo)) //... f$writes.add(bar_hash_code); if(!(bar>foo)) //...
  • 6. METHOD ASSERTION This shouldn't return NPE! METHOD WRAPPING Object o = new Object(); public Object getAndClear() { return this.o = null; } @Assertion("getAndClear() != null") public void m() { this.o.toString(); }
  • 7. METHOD ASSERTION We end up with something kinda like this: A "new" method that returns the result of the original one public void m() { // temp "is a" bytecode var temp = m$wrapped(); // now we check assertions if(!(getAndClear() != null)) //... // and now we return! return temp; } @Assertion("getAndClear() != null") public void m$wrapped() { this.o.toString(); }
  • 8. METHOD ASSERTIONS Inherited Method Assertions We recursively inject code from parents assertions class Base { @Assertion("false") public int fooBar(int x) { ... } } public class Derived extends Base { @Override @Assertion("true") public int fooBar(int x) { ... } } public int fooBar(int x) { temp = fooBar$wrapped(x); if(!(false)) //... if(!(true)) //... return temp; }
  • 9. CONSTRUCTOR ASSERTIONS Remember Behaviors? That was easy. if((behavior instanceof CtConstructor) && hasAssertion(behavior)){ CtConstructor constructor = (CtConstructor) behavior; constructor.insertBeforeBody( "if(!(" + getAssertionValues(constructor) + ")) //... " ); }
  • 10. ASSERTIONS ON METHOD ENTRY We can't break original functionality! Previous example, now with entry assertion Object o = new Object(); public Object getAndClear() { return this.o = null; } @Assertion( value="getAndClear() != null", entry="getAndClear() == null" ) public void m() { this.o.toString(); }
  • 11. ASSERTIONS ON METHOD ENTRY We inject code at begining of method and at the exit. It should return NPE! public void m() { // entry assertion if(!(getAndClear() == null)) //... temp = m$wrapped(); // value assertion if(!(getAndClear() != null)) //... return temp; } ## As you can see (getAndClear() == null) expression returned true [java] Exception in thread "main" java.lang.NullPointerException [java] at ist.meic.pa.tests.Base.m$wrapped(Unknown Source) [java] at ist.meic.pa.tests.Base.m(Unknown Source) [java] ... [java] The assertion getAndClear() != null is false