forked from hakimel/reveal.js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathruntime-class-generation.html
463 lines (435 loc) · 27.6 KB
/
runtime-class-generation.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Runtime Class Generation, 2016</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/clock.css">
<link rel="stylesheet" href="css/theme/night.css" id="theme">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<style>
.documentation {
font-family: sans-serif;
color: #fff;
display: inline-block;
font-weight: 100;
text-align: center;
position:absolute;
margin: 15px;
top:0;
right:100px;
z-index: 100;
}
.documentation > div{
padding: 10px;
border-radius: 3px;
background: #343434;
display: inline-block;
}
.documentation div > a {
color: #cecece;
text-decoration: none;
padding: 10px;
border-radius: 3px;
background: #252525;
display: inline-block;
}
.documentation > div:hover {
background: #8b8b8b;
}
.documentation div:hover > a {
background: #606060;
}
</style>
</head>
<body>
<div style="display: block; position: absolute; bottom: 40px; left: 50%; width: 1000px; margin-left: -500px; z-index: 20;">
<table style="width: 100%">
<tr>
<td style="text-align: center; width: 20%"><span style="font-size: 80%; color: #e7ad52;">Organizer</span></td>
<td style="text-align: center; width: 20%"><span style="font-size: 80%; color: #e7ad52;">Conference</span></td>
<td style="text-align: center; width: 20%"></td>
<td style="text-align: center; width: 20%"><span style="font-size: 80%; color: #e7ad52;">Gold partner</span></td>
<td style="text-align: center; width: 20%"><span style="font-size: 80%; color: #e7ad52;">General partner</span></td>
</tr>
<tr>
<td style="text-align: center; vertical-align: bottom; padding-top: 2px; width: 20%">
<a target="_blank" href="http://www.gopas.cz" target="_blank"><img src="img/javadayscz2016/Gopas_skola_bile.png" height="30px"/></a>
</td>
<td style="text-align: center; vertical-align: bottom; padding-top: 2px; width: 20%">
<a target="_blank" href="http://www.javadays.cz" target="_blank"><img src="img/javadayscz2016/JD_white_02.png" height="40px"/></a>
</td>
<td style="text-align: center; vertical-align: bottom; padding-top: 2px; width: 20%">
<a target="_blank" href="http://www.fg.cz" target="_blank"><img src="img/jopenspace2013/FG_Forrest_neg.png" height="30px"/></a>
</td>
<td style="text-align: center; vertical-align: bottom; padding-top: 2px; width: 20%">
<a target="_blank" href="https://www.jetbrains.com/" target="_blank"><img src="img/javadayscz2016/horizontal_spelling_jetbrains_web_logo.png" height="20px"/></a>
</td>
<td style="text-align: center; vertical-align: bottom; padding-top: 2px; width: 20%">
<a target="_blank" href="http://www.commerzsystems.com/" target="_blank"><img src="img/javadayscz2016/Logo-CommerzSystems_JavaDays2016_Pocitacova_skola_GOPAS.png" height="30px"/></a>
</td>
</tr>
</table>
</div>
<div class="reveal">
<div class="slides">
<!-- INTRO -->
<section data-background="img/backgrounds/black.jpg">
<section data-background="img/backgrounds/black.jpg">
<h1>Runtime class generation</h1>
<h2>let the machine work for you</h2>
<h3>autumn 2016</h3>
<img class="plain" src="img/javadayscz2016/javadays-logo.png">
<p>
<small>Jan <a href="http://www.twiter.com/novoj">@Novoj</a> Novotný</small>
</p>
</section>
<section data-background="img/backgrounds/jno.jpg" data-background-size="contain" style="padding-left: 25%">
<h4>
<span style="font-size: 180%">Follow presentation with me at</span><br/>
<a href="http://novoj.github.io/reveal.js/runtime-class-generation.html" style="font-size: 105%">http://novoj.github.io/reveal.js/runtime-class-generation.html</a>
</h4>
<h4 style="margin-bottom: 2em;">
<span style="font-size: 180%">Code with me</span><br/>
<a href="https://github.com/novoj/RuntimeClassGeneration" style="font-size: 105%">git@github.com:novoj/RuntimeClassGeneration.git</a>
</h4>
<h1>About me</h1>
<ul style="margin-left: 3em;">
<li><a href="https://www.fg.cz">Java developer at FG Forrest</a></li>
<li><a href="https://kafemlejnek.tv">YouTuber at Kafemlejnek.TV</a></li>
<li><a href="http://blog.novoj.net">blogger at blog.novoj.net</a></li>
<li><a href="http://jopenspace.cz">jOpenspace non-conference co-organizer</a></li>
<li><a href="https://www.monkeytracker.cz">co-author of MonkeyTracker service</a></li>
<li>occasional speaker, father,<br/>MTB rider, runner ...</li>
</ul>
</section>
</section>
<!-- GOAL -->
<section data-background="img/backgrounds/brown.jpg">
<section>
<h1>Wouldn't it be nice if this code</h1>
<pre><code class="java" data-trim>public interface PersonDao {
void create(String firstName, String lastName, LocalDate birthDate);
void store(Person person);
Person getById();
boolean removeById();
}</code></pre>
<h2>was automatically implemented by machine?</h2>
</section>
<section>
<h1>It's already out there</h1>
<div style="width: 30%; display: inline-block; vertical-align: top; height: 40em;">
<p>Just to name a few:</p>
<ul>
<li><a href="http://docs.spring.io/spring-data/commons/docs/current/reference/html/#repositories.query-methods.details">Spring data</a></li>
<li><a href="http://www.mybatis.org/mybatis-3/java-api.html">MyBatis</a></li>
<li><a href="https://en.wikipedia.org/wiki/Active_record_pattern">Active Record (JRuby)</a></li>
</ul>
</div>
<div style="width: 68%; display: inline-block; vertical-align: top; height: 40em;">
<img src="img/runtime/spring-data.jpg" height="500px" style="position: absolute; left: 35%"/>
<img class="fragment current-visible" src="img/runtime/mybatis.jpg" height="500px" style="position: absolute; left: 35%"/>
<img class="fragment current-visible" src="img/runtime/ruby-on-rails.jpg" height="500px" style="position: absolute; left: 35%"/>
</div>
</section>
<section>
<h1>But how they do it?</h1>
<div style="margin: 2em">
<p>Using automatic class (code) generation in:</p>
<div style="width: 48%; display: inline-block">
<h3>compile time</h3>
<ul>
<li><a href="https://eclipse.org/aspectj/">AspectJ</a></li>
<li><a href="http://bytebuddy.net/#/">ByteBuddy</a></li>
<li><a href="https://kenai.com/projects/btrace">BTrace</a></li>
<li><a href="https://projectlombok.org/">Lombok</a></li>
</ul>
</div>
<div style="width: 48%; display: inline-block">
<h3>runtime</h3>
<ul>
<li>
<a href="https://docs.oracle.com/javase/8/docs/technotes/guides/reflection/proxy.html">JdkProxy</a></li>
<li><a href="https://github.com/cglib/cglib">CgLib</a></li>
<li><a href="http://jboss-javassist.github.io/javassist/">Javassist</a></li>
<li><a href="http://bytebuddy.net/#/">ByteBuddy</a></li>
</ul>
</div>
</div>
<p>Both approaches have their pros and cons.</p>
</section>
<section>
<h1>Project Lombok</h1>
<img src="img/runtime/lombok-data.png"/>
<p style="text-align: center; margin-top: -0.5em;">
<a href="http://localhost:8091?message=cz/novoj/generation/model/composite/LombokPerson.java">Lombok Person Example</a> /
<a href="http://localhost:8091?message=RuntimeClassGeneration/pom.xml:77">POM Example</a>
</p>
</section>
</section>
<section data-background="img/backgrounds/brown.jpg">
<section>
<h1>Let's prototype!</h1>
<p><strong>Branch:</strong> tutorial01</p>
<div style="width: 40%; height: 8em; vertical-align: top; display: inline-block">
Look at:<br/><br/>
<ul>
<li><a href="http://localhost:8091?message=cz/novoj/generation/model/Person.java">Person</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/model/PropertyAccessor.java">Property Accessor</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/contract/model/GenericBucket.java">Actual memory model</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/contract/model/GenericBucketProxyGenerator.java">Proxy implementation</a></li>
</ul>
</div>
<div style="width: 50%; height: 8em; display: inline-block; vertical-align: middle">
<img class="plain" src="img/runtime/jdk-proxy.png" style="margin-top: 4em"/>
</div>
<div class="fragment" style="margin-top: 1em;">
<div style="width: 48%; display: inline-block">
<h2>Pros</h2>
<ul>
<li>type safe code around hashmap</li>
<li>supports refactoring</li>
<li>supports searching</li>
</ul>
</div>
<div style="width: 48%; display: inline-block">
<h2>Cons</h2>
<ul>
<li>string parsing - method.getName()</li>
<li>ugly code</li>
<li>most likely pretty slow</li>
</ul>
</div>
</div>
</section>
<section>
<h1>Refactor!</h1>
<h2>Proxy class caching</h2>
<img class="plain" src="img/runtime/red-green-refactor.png" style="float: left; width: 30%; position: absolute; left: 0%; top: 0%">
<p><strong>Branch:</strong> tutorial02</p>
<div>
<ul style="width: 60%; margin-top: 4em;">
<li>java.lang.reflect.Proxy already caches classes</li>
<li>but we can do this more effectively</li>
<li>this is valid even for other class generators - I recommend disabling generator caching and implement your own</li>
</ul>
</div>
</section>
<section>
<h1>Refactor!</h1>
<h2>Method logic caching</h2>
<p><strong>Branch:</strong> tutorial03</p>
<img class="plain" src="img/runtime/red-green-refactor.png" style="float: left; width: 30%; position: absolute; left: 0%; top: 0%">
<div style="margin-top: 4em">
<div style="width: 45%; vertical-align: top; display: inline-block">
Let's make it even better:<br/><br/>
<ol>
<li>convert IF to lambda functions</li>
<li>cache pair Proxy.method + service lambda</li>
<li>cache information about method name decomposition, so that parsing is done only once</li>
</ol>
</div>
<div style="width: 40%; margin-left: 10%; vertical-align: top; display: inline-block">
Look at:<br/><br/>
<ul>
<li><a href="http://localhost:8091?message=cz/novoj/generation/contract/model/GenericBucketProxyGenerator.java:64">Concrete usage</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/proxyGenerator/JdkProxyDispatcherInvocationHandler.java">Dispatcher handler</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/proxyGenerator/infrastructure/MethodClassification.java">Method classification</a></li>
</ul>
</div>
</div>
</section>
<section>
<h1>Custom method body</h1>
<div style="width: 100%; vertical-align: top">
<img class="plain" src="img/runtime/manual-override.jpg" style="width: 30%; margin-right: 2em;">
<div style="width: 60%; display: inline-block">
<p><strong>Branch:</strong> tutorial04</p>
<div style="display: inline-block">
<p>What if we need custom logic on a proxy?</p>
<p>What if we want to mix and match custom logic with automatic one?</p>
</div>
<div style="display: inline-block">
<p>Default / abstract methods to the rescue.</p>
<h3>Look at:</h3>
<ul>
<li><a href="http://localhost:8091?message=cz/novoj/generation/model/traits/AgingPerson.java">Aging person trait</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/model/CustomizedPerson.java">Customized person contract</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/JdkInterfaceProxyGeneratorTest.java">Test suite</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/proxyGenerator/implementation/javassist/JavassistDispatcherInvocationHandler.java">Updated dispatcher handler</a></li>
</ul>
</div>
</div>
</div>
</section>
</section>
<section data-background="img/backgrounds/brown.jpg">
<section>
<h1>DAO implementation</h1>
<h2>all building blocks in place</h2>
<img class="plain" src="img/runtime/CRUD.gif" style="float: left; width: 30%; position: absolute; left: 0%; top: 50%">
<p><strong>Branch:</strong> tutorial05</p>
<div style="margin-top: 2em">
<p>Nothing much new here</p>
<h3>Look at:</h3>
<ul>
<li><a href="http://localhost:8091?message=cz/novoj/generation/contract/dao/Dao.java">Dao interface</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/dao/PersonDao.java">Person Dao "implementation"</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/dao/JavassistPersonDaoTest.java">Test suite</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/contract/dao/GenericBucketRepository.java">Memory repository</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/contract/dao/GenericBucketDaoProxyGenerator.java">Dao method implementation</a></li>
</ul>
</div>
</section>
<section>
<h1>DAO implementation</h1>
<h2>nicer create & add method</h2>
<img class="plain" src="img/runtime/CRUD.gif" style="float: left; width: 30%; position: absolute; left: 0%; top: 50%">
<p><strong>Branch:</strong> tutorial06</p>
<div style="margin-top: 2em">
<p>Let's get advantage of parameters!</p>
<h3>Look at:</h3>
<ul>
<li><a href="http://localhost:8091?message=cz/novoj/generation/dao/PersonDao.java:24">Person Dao "implementation"</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/dao/JavassistPersonDaoTest.java:38">Test suite</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/contract/dao/executor/AddDaoMethodExecutor.java">Add method executor</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/contract/dao/GenericBucketDaoProxyGenerator.java:30">Updated proxy generator</a></li>
</ul>
</div>
</section>
<section>
<h1>DAO implementation</h1>
<h2>finder / removal implementation</h2>
<div style="margin-top: 1em">
<img class="plain" src="img/runtime/query-process.png" height="500px">
</div>
</section>
<section>
<h1>DAO implementation</h1>
<h2>finder implementation</h2>
<p><strong>Branch:</strong> tutorial07</p>
<img class="plain" src="img/runtime/CRUD.gif" style="float: left; width: 30%; position: absolute; left: 0%; top: 50%">
<div style="margin-top: 1em">
<h3>Look at:</h3>
<ul>
<li><a href="http://localhost:8091?message=cz/novoj/generation/dao/PersonDao.java:22">Person Dao "implementation"</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/contract/dao/GenericBucketDaoProxyGenerator.java">Updated proxy generator</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/dao/JavassistPersonDaoTest.java:55">Test suite</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/contract/dao/executor/GetDaoMethodExecutor.java">Get method executor</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/contract/dao/executor/RemoveDaoMethodExecutor.java">Remove method executor</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/contract/dao/executor/dto/DaoMethodQuery.java">Query AST</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/contract/dao/query/keyword/Keyword.java">Keywords (reserved words)</a></li>
</ul>
</div>
</section>
</section>
<section data-background="img/backgrounds/brown.jpg">
<section>
<h1>Bonus</h1>
<h2>proxy serialization</h2>
<p><strong>Branch:</strong> master</p>
<div style="margin-top: 1em">
<p>We can't simply serialize proxy - class might not be known at the moment of deserialization.</p>
<p>We need to serialize "recipe" how to reconstruct the class.</p>
<h3>Look at:</h3>
<ul>
<li><a href="http://localhost:8091?message=cz/novoj/generation/contract/SerializableProxy.java">Serializable Proxy</a></li>
<li><a href="http://localhost:8091?message=cz/novoj/generation/proxyGenerator/implementation/javassist/model/JavassistSerializationTest.java">Test suite</a></li>
</ul>
</div>
</section>
<section>
<h1>Bonus</h1>
<h2>always repackage</h2>
<div style="width: 100%; text-align: center">
<pre style="display: inline-block; width: auto;"><code class="xml" style="max-height: 600px; width: 60em; font-size: 80%; line-height: 1.4em;" data-trim><plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>jarjar-maven-plugin</artifactId>
<version>1.9</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jarjar</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<includes>
<include>org.javassist:javassist</include>
</includes>
<rules>
<rule>
<pattern>javassist.**</pattern>
<result>cz.novoj.generation.internal.javassist.@1</result>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin></code></pre>
</div>
</section>
<section>
<h1>Bonus</h1>
<h2>additional topics</h2>
<ul>
<li>annotations on methods are not inherited in Java and some class generations utils
doesn't copy them on overriden methods on subclass, look what
<a href="http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/annotation/AnnotationUtils.html">Spring guys worked around this</a></li>
<li>generics resolution - is tricky, again see <a href="http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-core/4.2.0.RELEASE/org/springframework/core/GenericTypeResolver.java#GenericTypeResolver">Spring implementation</a></li>
<li>performance of proxies is <a href="https://zeroturnaround.com/rebellabs/testing-the-performance-of-4-java-runtime-code-generators-cglib-javassist-jdk-proxy-byte-buddy/">not so bad</a>,
but dynamic nature makes them much slower than static class implementation</li>
</ul>
</section>
</section>
<!-- OUTRO -->
<section data-background="img/backgrounds/black.jpg">
<img src="img/backgrounds/attention.jpg" width="800"/>
<h2>Thank your for your attention</h2>
<p>Contact me at <a href="https://www.twitter.com/novoj">@Novoj</a> or <a href="mailto:novotnaci@gmail.com">novotnaci@gmail.com</a></p>
</section>
</div>
<!-- REFERENCE: http://www.slideshare.net/RafaelWinterhalter/runtime-code-generation-for-the-jvm -->
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script src="js/jquery.min.js"></script>
<script src="js/clock.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
history: true,
controls: true,
progress: true,
history: true,
center: true,
width: 1200,
height: 900,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/fade/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
var deadline = new Date(Date.parse(new Date()) + 1.25 * 60 * 60 * 1000);
initializeClock(deadline);
</script>
</body>
</html>