-
Notifications
You must be signed in to change notification settings - Fork 1
/
decompiling-scala-code.html
476 lines (398 loc) · 43 KB
/
decompiling-scala-code.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
464
465
466
467
468
469
470
471
472
473
474
475
476
<!DOCTYPE html>
<html lang="en" prefix="og: http://ogp.me/ns# fb: https://www.facebook.com/2008/fbml">
<head>
<title>Decompiling Scala code - Stackdiver as a Service</title>
<!-- Using the latest rendering mode for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://www.lyh.me/decompiling-scala-code.html">
<meta name="author" content="Neville Li" />
<meta name="keywords" content="scala,fp" />
<meta name="description" content="I was bored today and decided to decompile some Scala code for fun and profit. I’m using Scala 2.12.2 and Java 1.8.0_121. Scala object package javap object Test01 { def main(args: Array[String]): Unit = Unit } public final class javap.Test01$ { public static javap.Test01$ MODULE$; public static {}; public void main(java.lang.String[]); private javap.Test01$(); } public final class javap.Test01 { public static void main(java.lang.String[]); } As we can see a Scala object is compiled to 2 Java classes, Test01 with static methods for Java compatibility and a Test01$ with a static instance of itself as MODULE$, so that Test01 can be used as an instance value in Scala. Class constructors package javap class Test02(val x: Int, val y: Int, z: Int) { def this(x: Int, y: Int) = this(x, y, 0) } public class javap.Test02 { private final int x; private final int y; public int x(); public int y(); public javap.Test02(int, int, int); public javap.Test02(int, int); } Looks like the default constructor (val x: Int, val y: Int, z: Int) and the overloaded one (x: Int, y: Int) each generated a Java constructor. However only x and y …" />
<meta property="og:site_name" content="Stackdiver as a Service" />
<meta property="og:type" content="article"/>
<meta property="og:title" content="Decompiling Scala code"/>
<meta property="og:url" content="https://www.lyh.me/decompiling-scala-code.html"/>
<meta property="og:description" content="I was bored today and decided to decompile some Scala code for fun and profit. I’m using Scala 2.12.2 and Java 1.8.0_121. Scala object package javap object Test01 { def main(args: Array[String]): Unit = Unit } public final class javap.Test01$ { public static javap.Test01$ MODULE$; public static {}; public void main(java.lang.String[]); private javap.Test01$(); } public final class javap.Test01 { public static void main(java.lang.String[]); } As we can see a Scala object is compiled to 2 Java classes, Test01 with static methods for Java compatibility and a Test01$ with a static instance of itself as MODULE$, so that Test01 can be used as an instance value in Scala. Class constructors package javap class Test02(val x: Int, val y: Int, z: Int) { def this(x: Int, y: Int) = this(x, y, 0) } public class javap.Test02 { private final int x; private final int y; public int x(); public int y(); public javap.Test02(int, int, int); public javap.Test02(int, int); } Looks like the default constructor (val x: Int, val y: Int, z: Int) and the overloaded one (x: Int, y: Int) each generated a Java constructor. However only x and y …"/>
<meta property="article:published_time" content="2017-05-06" />
<meta property="article:section" content="code" />
<meta property="article:tag" content="scala" />
<meta property="article:tag" content="fp" />
<meta property="article:author" content="Neville Li" />
<!-- Bootstrap -->
<link rel="stylesheet" href="https://www.lyh.me/theme/css/bootstrap.min.css" type="text/css"/>
<link href="https://www.lyh.me/theme/css/font-awesome.min.css" rel="stylesheet">
<link href="https://www.lyh.me/theme/css/pygments/monokai.css" rel="stylesheet">
<link href="https://www.lyh.me/theme/css/typogrify.css" rel="stylesheet">
<link rel="stylesheet" href="https://www.lyh.me/theme/css/style.css" type="text/css"/>
<link href="https://www.lyh.me/feeds/all.atom.xml" type="application/atom+xml" rel="alternate"
title="Stackdiver as a Service ATOM Feed"/>
<link href="https://www.lyh.me/feeds/code.atom.xml" type="application/atom+xml" rel="alternate"
title="Stackdiver as a Service code ATOM Feed"/>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="https://www.lyh.me/" class="navbar-brand">
Stackdiver as a Service </a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li><a href="https://www.lyh.me/pages/about-me.html">
About Me
</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
</div> <!-- /.navbar -->
<!-- Banner -->
<!-- End Banner -->
<!-- Content Container -->
<div class="container">
<div class="row">
<div class="col-sm-9">
<section id="content">
<article>
<header class="page-header">
<h1>
<a href="https://www.lyh.me/decompiling-scala-code.html"
rel="bookmark"
title="Permalink to Decompiling Scala code">
Decompiling Scala code
</a>
</h1>
</header>
<div class="entry-content">
<div class="panel">
<div class="panel-body">
<footer class="post-info">
<span class="label label-default">Date</span>
<span class="published">
<i class="fa fa-calendar"></i><time datetime="2017-05-06T17:14:00-04:00"> Sat 06 May 2017</time>
</span>
<span class="label label-default">Category</span>
<a href="https://www.lyh.me/category/code.html">code</a>
<span class="label label-default">Tags</span>
<a href="https://www.lyh.me/tag/scala.html">scala</a>
/
<a href="https://www.lyh.me/tag/fp.html">fp</a>
</footer><!-- /.post-info --> </div>
</div>
<p>I was bored today and decided to decompile some Scala code for fun and profit. I’m using Scala 2.12.2 and Java 1.8.0_121.</p>
<h2>Scala object</h2>
<div class="highlight"><pre><span></span><code><span class="k">package</span> <span class="nn">javap</span>
<span class="k">object</span> <span class="nc">Test01</span> <span class="o">{</span>
<span class="k">def</span> <span class="n">main</span><span class="o">(</span><span class="n">args</span><span class="k">:</span> <span class="kt">Array</span><span class="o">[</span><span class="kt">String</span><span class="o">])</span><span class="k">:</span> <span class="kt">Unit</span> <span class="o">=</span> <span class="nc">Unit</span>
<span class="o">}</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="kd">public</span> <span class="kd">final</span> <span class="kd">class</span> <span class="nc">javap</span><span class="p">.</span><span class="na">Test01$</span> <span class="p">{</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="n">javap</span><span class="p">.</span><span class="na">Test01$</span> <span class="n">MODULE$</span><span class="p">;</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="p">{};</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">main</span><span class="p">(</span><span class="n">java</span><span class="p">.</span><span class="na">lang</span><span class="p">.</span><span class="na">String</span><span class="o">[]</span><span class="p">);</span>
<span class="kd">private</span> <span class="n">javap</span><span class="p">.</span><span class="na">Test01$</span><span class="p">();</span>
<span class="p">}</span>
<span class="kd">public</span> <span class="kd">final</span> <span class="kd">class</span> <span class="nc">javap</span><span class="p">.</span><span class="na">Test01</span> <span class="p">{</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="p">(</span><span class="n">java</span><span class="p">.</span><span class="na">lang</span><span class="p">.</span><span class="na">String</span><span class="o">[]</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div>
<p>As we can see a Scala object is compiled to 2 Java classes, <code>Test01</code> with static methods for Java compatibility and a <code>Test01$</code> with a static instance of itself as <code>MODULE$</code>, so that <code>Test01</code> can be used as an instance value in Scala.</p>
<h2>Class constructors</h2>
<div class="highlight"><pre><span></span><code><span class="k">package</span> <span class="nn">javap</span>
<span class="k">class</span> <span class="nc">Test02</span><span class="o">(</span><span class="k">val</span> <span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="k">val</span> <span class="n">y</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">z</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span> <span class="o">{</span>
<span class="k">def</span> <span class="k">this</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">y</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span> <span class="k">=</span> <span class="k">this</span><span class="o">(</span><span class="n">x</span><span class="o">,</span> <span class="n">y</span><span class="o">,</span> <span class="mi">0</span><span class="o">)</span>
<span class="o">}</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">javap</span><span class="p">.</span><span class="na">Test02</span> <span class="p">{</span>
<span class="kd">private</span> <span class="kd">final</span> <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
<span class="kd">private</span> <span class="kd">final</span> <span class="kt">int</span> <span class="n">y</span><span class="p">;</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">x</span><span class="p">();</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">y</span><span class="p">();</span>
<span class="kd">public</span> <span class="n">javap</span><span class="p">.</span><span class="na">Test02</span><span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">);</span>
<span class="kd">public</span> <span class="n">javap</span><span class="p">.</span><span class="na">Test02</span><span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div>
<p>Looks like the default constructor <code>(val x: Int, val y: Int, z: Int)</code> and the overloaded one <code>(x: Int, y: Int)</code> each generated a Java constructor. However only <code>x</code> and <code>y</code> are <code>val</code>s and became public member methods of the class, while <code>z</code> is used as a constructor argument only.</p>
<h2>Traits</h2>
<div class="highlight"><pre><span></span><code><span class="k">package</span> <span class="nn">javap</span>
<span class="k">trait</span> <span class="nc">Test03</span> <span class="o">{</span>
<span class="k">val</span> <span class="n">x</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="o">???</span>
<span class="k">def</span> <span class="n">f</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="o">???</span>
<span class="o">}</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">javap</span><span class="p">.</span><span class="na">Test03</span> <span class="p">{</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kt">void</span> <span class="nf">javap$Test03$_setter_$x_$eq</span><span class="p">(</span><span class="kt">int</span><span class="p">);</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kt">int</span> <span class="nf">x</span><span class="p">();</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">int</span> <span class="nf">f$</span><span class="p">(</span><span class="n">javap</span><span class="p">.</span><span class="na">Test03</span><span class="p">,</span> <span class="kt">int</span><span class="p">);</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">f</span><span class="p">(</span><span class="kt">int</span><span class="p">);</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">$init$</span><span class="p">(</span><span class="n">javap</span><span class="p">.</span><span class="na">Test03</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div>
<p>So <code>trait</code>s are not too different from <code>object</code>s. However method <code>f</code> generated two versions, one static with the <code>Test03</code> itself as an additional argument and one member method. There’s also a setter method, most likely to allow overriding <code>x</code> in sub-types.</p>
<h2>Member <code>val</code>s</h2>
<div class="highlight"><pre><span></span><code><span class="k">package</span> <span class="nn">javap</span>
<span class="k">class</span> <span class="nc">Test04</span> <span class="o">{</span>
<span class="k">val</span> <span class="n">a</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="o">???</span>
<span class="k">lazy</span> <span class="k">val</span> <span class="n">b</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="o">???</span>
<span class="nd">@transient</span> <span class="k">lazy</span> <span class="k">val</span> <span class="n">c</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="o">???</span>
<span class="k">implicit</span> <span class="k">val</span> <span class="n">d</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="o">???</span>
<span class="o">}</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">javap</span><span class="p">.</span><span class="na">Test04</span> <span class="p">{</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="n">b</span><span class="p">;</span>
<span class="kd">private</span> <span class="kd">transient</span> <span class="kt">int</span> <span class="n">c</span><span class="p">;</span>
<span class="kd">private</span> <span class="kd">final</span> <span class="kt">int</span> <span class="n">a</span><span class="p">;</span>
<span class="kd">private</span> <span class="kd">final</span> <span class="kt">int</span> <span class="n">d</span><span class="p">;</span>
<span class="kd">private</span> <span class="kd">volatile</span> <span class="kt">byte</span> <span class="n">bitmap$0</span><span class="p">;</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">a</span><span class="p">();</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="nf">b$lzycompute</span><span class="p">();</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">b</span><span class="p">();</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="nf">c$lzycompute</span><span class="p">();</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">c</span><span class="p">();</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">d</span><span class="p">();</span>
<span class="kd">public</span> <span class="n">javap</span><span class="p">.</span><span class="na">Test04</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<p>Here we can see clearly that Scala <code>val</code>s are compiled as accessor methods and private field. <code>b</code> and <code>c</code> are lazy and generated two <code>lzycompute</code> methods. In addition <code>c</code> is transient and the underlying field is marked so as well. There’s also a <code>bitmap$0</code> to keep track of the states of <code>lazy val</code>s. See this StackOverflow <a href="http://stackoverflow.com/questions/6877040/in-compiled-scala-what-is-the-bitmap0-field0">thread</a>.</p>
<h2>Primitives</h2>
<div class="highlight"><pre><span></span><code><span class="k">package</span> <span class="nn">javap</span>
<span class="k">class</span> <span class="nc">Test05</span> <span class="o">{</span>
<span class="k">def</span> <span class="n">plus1</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">y</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="o">???</span>
<span class="k">def</span> <span class="n">plus2</span><span class="o">[</span><span class="kt">T</span><span class="o">](</span><span class="n">x</span><span class="k">:</span> <span class="kt">T</span><span class="o">,</span> <span class="n">y</span><span class="k">:</span> <span class="kt">T</span><span class="o">)</span><span class="k">:</span> <span class="kt">T</span> <span class="o">=</span> <span class="o">???</span>
<span class="k">def</span> <span class="n">plus3</span><span class="o">[</span><span class="kt">@specialized</span><span class="o">(</span><span class="kt">Int</span><span class="p">,</span> <span class="kt">Long</span><span class="p">,</span> <span class="kt">Float</span><span class="p">,</span> <span class="kt">Double</span><span class="o">)</span> <span class="kt">T</span><span class="o">](</span><span class="n">x</span><span class="k">:</span> <span class="kt">T</span><span class="o">,</span> <span class="n">y</span><span class="k">:</span> <span class="kt">T</span><span class="o">)</span><span class="k">:</span> <span class="kt">T</span> <span class="o">=</span> <span class="o">???</span>
<span class="o">}</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">javap</span><span class="p">.</span><span class="na">Test05</span> <span class="p">{</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">plus1</span><span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">);</span>
<span class="kd">public</span> <span class="o"><</span><span class="n">T</span><span class="o">></span> <span class="n">T</span> <span class="nf">plus2</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">T</span><span class="p">);</span>
<span class="kd">public</span> <span class="o"><</span><span class="n">T</span><span class="o">></span> <span class="n">T</span> <span class="nf">plus3</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">T</span><span class="p">);</span>
<span class="kd">public</span> <span class="kt">double</span> <span class="nf">plus3$mDc$sp</span><span class="p">(</span><span class="kt">double</span><span class="p">,</span> <span class="kt">double</span><span class="p">);</span>
<span class="kd">public</span> <span class="kt">float</span> <span class="nf">plus3$mFc$sp</span><span class="p">(</span><span class="kt">float</span><span class="p">,</span> <span class="kt">float</span><span class="p">);</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">plus3$mIc$sp</span><span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">);</span>
<span class="kd">public</span> <span class="kt">long</span> <span class="nf">plus3$mJc$sp</span><span class="p">(</span><span class="kt">long</span><span class="p">,</span> <span class="kt">long</span><span class="p">);</span>
<span class="kd">public</span> <span class="n">javap</span><span class="p">.</span><span class="na">Test05</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<p>Unlike Java, which has primitive (<code>int</code>, <code>long</code>, etc.) and boxed (<code>Integer</code>, <code>Long</code>, etc.) types, Scala has unified primitive types and will choose the appropriate type in generated code, like primitives in most cases and boxed types in generic classes. We can see here that <code>plus1</code> and <code>plus2</code> are identical to how one would write then in Java. However <code>plus3</code> generates one generic version and 4 specialized versions.</p>
<p>See these <a href="http://www.lyh.me/slides/primitives.html">slides</a> for a more in depth discussion of Scala primitives.</p>
<h2>Functions</h2>
<div class="highlight"><pre><span></span><code><span class="k">package</span> <span class="nn">javap</span>
<span class="k">class</span> <span class="nc">Test06</span> <span class="o">{</span>
<span class="k">def</span> <span class="n">map</span><span class="o">(</span><span class="n">xs</span><span class="k">:</span> <span class="kt">Array</span><span class="o">[</span><span class="kt">Int</span><span class="o">],</span> <span class="n">f</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=></span> <span class="nc">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Array</span><span class="o">[</span><span class="kt">Int</span><span class="o">]</span> <span class="k">=</span> <span class="n">xs</span><span class="o">.</span><span class="n">map</span><span class="o">(</span><span class="n">f</span><span class="o">)</span>
<span class="k">def</span> <span class="n">f</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="o">???</span>
<span class="k">val</span> <span class="n">g</span> <span class="k">=</span> <span class="n">f</span> <span class="k">_</span>
<span class="k">val</span> <span class="n">h</span> <span class="k">=</span> <span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span> <span class="k">=></span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="k">:</span> <span class="kt">Int</span>
<span class="o">}</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">javap</span><span class="p">.</span><span class="na">Test06</span> <span class="p">{</span>
<span class="kd">private</span> <span class="kd">final</span> <span class="n">scala</span><span class="p">.</span><span class="na">Function1</span><span class="o"><</span><span class="n">java</span><span class="p">.</span><span class="na">lang</span><span class="p">.</span><span class="na">Object</span><span class="p">,</span> <span class="n">java</span><span class="p">.</span><span class="na">lang</span><span class="p">.</span><span class="na">Object</span><span class="o">></span> <span class="n">g</span><span class="p">;</span>
<span class="kd">private</span> <span class="kd">final</span> <span class="n">scala</span><span class="p">.</span><span class="na">Function1</span><span class="o"><</span><span class="n">java</span><span class="p">.</span><span class="na">lang</span><span class="p">.</span><span class="na">Object</span><span class="p">,</span> <span class="n">java</span><span class="p">.</span><span class="na">lang</span><span class="p">.</span><span class="na">Object</span><span class="o">></span> <span class="n">h</span><span class="p">;</span>
<span class="kd">public</span> <span class="kt">int</span><span class="o">[]</span> <span class="nf">map</span><span class="p">(</span><span class="kt">int</span><span class="o">[]</span><span class="p">,</span> <span class="n">scala</span><span class="p">.</span><span class="na">Function1</span><span class="o"><</span><span class="n">java</span><span class="p">.</span><span class="na">lang</span><span class="p">.</span><span class="na">Object</span><span class="p">,</span> <span class="n">java</span><span class="p">.</span><span class="na">lang</span><span class="p">.</span><span class="na">Object</span><span class="o">></span><span class="p">);</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">f</span><span class="p">(</span><span class="kt">int</span><span class="p">);</span>
<span class="kd">public</span> <span class="n">scala</span><span class="p">.</span><span class="na">Function1</span><span class="o"><</span><span class="n">java</span><span class="p">.</span><span class="na">lang</span><span class="p">.</span><span class="na">Object</span><span class="p">,</span> <span class="n">java</span><span class="p">.</span><span class="na">lang</span><span class="p">.</span><span class="na">Object</span><span class="o">></span> <span class="nf">g</span><span class="p">();</span>
<span class="kd">public</span> <span class="n">scala</span><span class="p">.</span><span class="na">Function1</span><span class="o"><</span><span class="n">java</span><span class="p">.</span><span class="na">lang</span><span class="p">.</span><span class="na">Object</span><span class="p">,</span> <span class="n">java</span><span class="p">.</span><span class="na">lang</span><span class="p">.</span><span class="na">Object</span><span class="o">></span> <span class="nf">h</span><span class="p">();</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="nf">$anonfun$g$1</span><span class="p">(</span><span class="n">javap</span><span class="p">.</span><span class="na">Test06</span><span class="p">,</span> <span class="kt">int</span><span class="p">);</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="nf">$anonfun$h$1</span><span class="p">(</span><span class="kt">int</span><span class="p">);</span>
<span class="kd">public</span> <span class="n">javap</span><span class="p">.</span><span class="na">Test06</span><span class="p">();</span>
<span class="kd">private</span> <span class="kd">static</span> <span class="n">java</span><span class="p">.</span><span class="na">lang</span><span class="p">.</span><span class="na">Object</span> <span class="nf">$deserializeLambda$</span><span class="p">(</span><span class="n">java</span><span class="p">.</span><span class="na">lang</span><span class="p">.</span><span class="na">invoke</span><span class="p">.</span><span class="na">SerializedLambda</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div>
<p>These Scala anonymous functions are realy just instances of the trait <code>Function1[T, R]</code>. Scala generates both static methods (<code>$anonfun$g$1</code>, <code>$anonfun$h$1</code>) and function instances (<code>g()</code>, <code>h()</code>) for <code>g</code> and <code>f</code> since they are declared as <code>val</code>s. <code>f</code> is declared as a method and has no instances. This will probably happen at the call site.</p>
<h2>Other features</h2>
<div class="highlight"><pre><span></span><code><span class="k">package</span> <span class="nn">javap</span>
<span class="k">class</span> <span class="nc">Test07</span> <span class="o">{</span>
<span class="k">def</span> <span class="n">plus1</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">y</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">z</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="mi">0</span><span class="o">)</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="o">???</span>
<span class="k">def</span> <span class="n">plus2</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)(</span><span class="n">y</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)(</span><span class="n">z</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="o">???</span>
<span class="k">def</span> <span class="n">plus3</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)(</span><span class="k">implicit</span> <span class="n">y</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="o">???</span>
<span class="k">def</span> <span class="n">plus4</span><span class="o">[</span><span class="kt">T</span><span class="o">](</span><span class="n">x</span><span class="k">:</span> <span class="kt">T</span><span class="o">,</span> <span class="n">y</span><span class="k">:</span> <span class="kt">T</span><span class="o">)(</span><span class="k">implicit</span> <span class="n">num</span><span class="k">:</span> <span class="kt">Numeric</span><span class="o">[</span><span class="kt">T</span><span class="o">])</span><span class="k">:</span> <span class="kt">T</span> <span class="o">=</span> <span class="o">???</span>
<span class="k">def</span> <span class="n">plus5</span><span class="o">[</span><span class="kt">T:</span> <span class="kt">Numeric</span><span class="o">](</span><span class="n">x</span><span class="k">:</span> <span class="kt">T</span><span class="o">,</span> <span class="n">y</span><span class="k">:</span> <span class="kt">T</span><span class="o">)</span><span class="k">:</span> <span class="kt">T</span> <span class="o">=</span> <span class="o">???</span>
<span class="o">}</span>
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">javap</span><span class="p">.</span><span class="na">Test07</span> <span class="p">{</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">plus1</span><span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">);</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">plus1$default$3</span><span class="p">();</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">plus2</span><span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">);</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">plus3</span><span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">);</span>
<span class="kd">public</span> <span class="o"><</span><span class="n">T</span><span class="o">></span> <span class="n">T</span> <span class="nf">plus4</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">scala</span><span class="p">.</span><span class="na">math</span><span class="p">.</span><span class="na">Numeric</span><span class="o"><</span><span class="n">T</span><span class="o">></span><span class="p">);</span>
<span class="kd">public</span> <span class="o"><</span><span class="n">T</span><span class="o">></span> <span class="n">T</span> <span class="nf">plus5</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">scala</span><span class="p">.</span><span class="na">math</span><span class="p">.</span><span class="na">Numeric</span><span class="o"><</span><span class="n">T</span><span class="o">></span><span class="p">);</span>
<span class="kd">public</span> <span class="n">javap</span><span class="p">.</span><span class="na">Test07</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<p>Here we can see that there’s not much special in the generated methods. Method <code>plus1</code> with default argument generated a special case. While curried <code>plus2</code> and <code>plus3</code> with implicit argument don’t look special at all. <code>plus4</code> and <code>plus5</code> look identical since an <code>implicit</code> type class argument is equivalent to a context bound <code>[T: Numeric]</code>.</p>
<h2>Conclusion</h2>
<p>In my experience as a library builder, digging into generated byte code allows me to better understand the following topics</p>
<ul>
<li>Java inter-op, e.g. Scala features that translate well or badly when called in Java</li>
<li>Performance considerations, e.g. primitive vs. boxed types, class method vs. function instances</li>
<li>Serialization implications, e.g. pulling in non-serializable trait or object module, or initialization sequence of <code>@transient lazy val</code>s</li>
</ul>
<p>The script that generated these can be found at <a href="https://github.com/nevillelyh/scala-playground/blob/master/javap.sh">https://github.com/nevillelyh/scala-playground/blob/master/javap.sh</a>.</p>
</div>
<!-- /.entry-content -->
<hr />
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style">
<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
<a class="addthis_button_tweet"></a>
<a class="addthis_button_google_plusone" g:plusone:size="medium"></a>
</div>
<!-- AddThis Button END -->
<hr/>
<section class="comments" id="comments">
<h2>Comments</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'lyh'; // required: replace example with your forum shortname
var disqus_config = function () {
this.language = "en";
this.page.identifier = '2017-05-06-decompiling-scala-code';
this.page.url = 'https://www.lyh.me/decompiling-scala-code.html';
};
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by
Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</section>
</article>
</section>
</div>
<div class="col-sm-3" id="sidebar">
<aside>
<div id="aboutme">
<p>
<img width="100%" class="img-thumbnail" src="https://www.lyh.me//avatar.jpg"/>
</p>
<p>
<strong>About Neville Li</strong><br/>
Data infrastructure @<a href="https://twitter.com/Spotify">Spotify</a>, ex-@<a href="https://twitter.com/Yahoo">Yahoo</a> search, creator of <a href="https://github.com/spotify/scio">Scio</a>, technical cave & wreck diver, lefty guitar player
</p>
</div><!-- Sidebar -->
<section class="well well-sm">
<ul class="list-group list-group-flush">
<!-- Sidebar/Social -->
<li class="list-group-item">
<h4><i class="fa fa-home fa-lg"></i><span class="icon-label">Social</span></h4>
<ul class="list-group" id="social">
<li class="list-group-item"><a href="https://open.spotify.com/user/sinisa_lyh"><i class="fa fa-spotify fa-lg"></i> Spotify</a></li>
<li class="list-group-item"><a href="https://github.com/nevillelyh"><i class="fa fa-github-square fa-lg"></i> GitHub</a></li>
<li class="list-group-item"><a href="https://twitter.com/sinisa_lyh"><i class="fa fa-twitter-square fa-lg"></i> Twitter</a></li>
<li class="list-group-item"><a href="https://www.slideshare.net/sinisalyh"><i class="fa fa-slideshare fa-lg"></i> SlideShare</a></li>
<li class="list-group-item"><a href="https://www.youtube.com/user/sinisalyh/videos"><i class="fa fa-youtube-square fa-lg"></i> YouTube</a></li>
<li class="list-group-item"><a href="https://www.instagram.com/sinisa/"><i class="fa fa-instagram fa-lg"></i> Instagram</a></li>
<li class="list-group-item"><a href="https://www.flickr.com/photos/sinisa_lyh"><i class="fa fa-flickr fa-lg"></i> Flickr</a></li>
</ul>
</li>
<!-- End Sidebar/Social -->
<!-- Sidebar/Recent Posts -->
<li class="list-group-item">
<h4><i class="fa fa-home fa-lg"></i><span class="icon-label">Recent Posts</span></h4>
<ul class="list-group" id="recentposts">
<li class="list-group-item"><a href="https://www.lyh.me/magnolify.html">Magnolify</a></li>
<li class="list-group-item"><a href="https://www.lyh.me/featran.html">Featran</a></li>
<li class="list-group-item"><a href="https://www.lyh.me/automatic-type-class-derivation-with-shapeless.html">Automatic type-class derivation with Shapeless</a></li>
<li class="list-group-item"><a href="https://www.lyh.me/lambda-serialization.html">Lambda serialization</a></li>
<li class="list-group-item"><a href="https://www.lyh.me/lawfulness-of-aggregatebykey.html">Lawfulness of aggregateByKey</a></li>
</ul>
</li>
<!-- End Sidebar/Recent Posts -->
<!-- Sidebar/Categories -->
<li class="list-group-item">
<h4><i class="fa fa-home fa-lg"></i><span class="icon-label">Categories</span></h4>
<ul class="list-group" id="categories">
<li class="list-group-item">
<a href="https://www.lyh.me/category/code.html"><i class="fa fa-folder-open fa-lg"></i>code</a>
</li>
<li class="list-group-item">
<a href="https://www.lyh.me/category/misc.html"><i class="fa fa-folder-open fa-lg"></i>misc</a>
</li>
</ul>
</li>
<!-- End Sidebar/Categories -->
<!-- Sidebar/Twitter Timeline -->
<li class="list-group-item">
<h4><i class="fa fa-twitter fa-lg"></i><span class="icon-label">Latest Tweets</span></h4>
<div id="twitter_timeline">
<a class="twitter-timeline" data-width="250" data-height="300" data-dnt="true" data-theme="light" href="https://twitter.com/sinisa_lyh">Tweets by sinisa_lyh</a> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
</div>
</li>
<!-- End Sidebar/Twitter Timeline -->
</ul>
</section>
<!-- End Sidebar --> </aside>
</div>
</div>
</div>
<!-- End Content Container -->
<footer>
<div class="container">
<hr>
<div class="row">
<div class="col-xs-10">© 2020 Neville Li
· Powered by <a href="https://github.com/getpelican/pelican-themes/tree/master/pelican-bootstrap3" target="_blank">pelican-bootstrap3</a>,
<a href="http://docs.getpelican.com/" target="_blank">Pelican</a>,
<a href="http://getbootstrap.com" target="_blank">Bootstrap</a> <p><small> <a rel="license" href="https://creativecommons.org/licenses/by-nc/4.0/deed.en"><img alt="Creative Commons License" style="border-width:0" src="//i.creativecommons.org/l/by-nc/4.0/80x15.png" /></a>
Content
licensed under a <a rel="license" href="https://creativecommons.org/licenses/by-nc/4.0/deed.en">Creative Commons Attribution-NonCommercial 4.0 International License</a>, except where indicated otherwise.
</small></p>
</div>
<div class="col-xs-2"><p class="pull-right"><i class="fa fa-arrow-up"></i> <a href="#">Back to top</a></p></div>
</div>
</div>
</footer>
<script src="https://www.lyh.me/theme/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://www.lyh.me/theme/js/bootstrap.min.js"></script>
<!-- Enable responsive features in IE8 with Respond.js (https://github.com/scottjehl/Respond) -->
<script src="https://www.lyh.me/theme/js/respond.min.js"></script>
<!-- Disqus -->
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'lyh'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var s = document.createElement('script');
s.async = true;
s.type = 'text/javascript';
s.src = '//' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
<!-- End Disqus Code -->
<!-- Google Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-6988688-5']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- End Google Analytics Code -->
<script type="text/javascript">var addthis_config = {"data_track_addressbar": true};</script>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=sinisalyh"></script>
</body>
</html>