1. Koichi
Sakata (阪田 浩一)
@jyukutyo
How
Scala
code
is
expressed
in
the
JVM
-‐
ScalaMatsuri
2016
-‐
2. 関ジャバです!
2
@jyukutyo
produced three Java framework books
one was translated into Korean
JUG Leader (KansaiJUG)
blog:Fight the Future
http://jyukutyo.hatenablog.com/
FURYU CORPORATION
age: 36
Koichi Sakata (阪田 浩一)
フリューの阪田です 関ジャバの会長です
14. ClassFile
{
u4
magic;
u2
minor_version;
u2
major_version;
u2
constant_pool_count;
cp_info
constant_pool[constant_pool_count-‐1];
u2
access_flags;
u2
this_class;
u2
super_class;
u2
interfaces_count;
u2
interfaces[interfaces_count];
...
-‐-‐
u1
represents
one-‐byte
quantity,
Hexadecimal
2
digit.
15. ...
u2
fields_count;
field_info
fields[fields_count];
u2
methods_count;
method_info
methods[methods_count];
u2
attributes_count;
attribute_info
attributes[attributes_count];
}
-‐-‐
u1
represents
one-‐byte
quantity,
Hexadecimal
2
digit.
u1が1バイト
16進数では2桁になります
42. public
static
void
main(java.lang.String[]);
0:
getstatic
#16
//
Field
HelloWorld$.
MODULE$:LHelloWorld$;
3:
aload_0
4:
invokevirtual
#18
//
Method
HelloWorld$.main:
([Ljava/lang/String;)V
7:
return
-‐-‐
4:
in
Java
HelloWorld$.MODULE$.main(arg);
43. public
final
class
HelloWorld$
-‐-‐
omit
Constant
pool
public
static
{};
0:
new
#2
//
class
HelloWorld$
3:
invokespecial
#12
//
Method
"<init>":()V
6:
return
-‐-‐
<init>
means
constructor
46. The
following
samples
are
quoted
from
tutorials
in
Scala
official
site
http://docs.scala-‐lang.org/tutorials/
これ以降で使用するサンプルは
Scalaの公式サイトのチュートリアルです
55. abstract
class
Term
case
class
Var(name:
String)
extends
Term
case
class
Fun(arg:
String,
body:
Term)
extends
Term
case
class
App(f:
Term,
v:
Term)
extends
Term
58. public
class
App
extends
Term
implements
s.Product,s.Serializable
public
static
s.Option<scala.Tuple2<Term,
Term>>
unapply(App);
public
static
App
apply(Term,
Term);
public
...
tupled();
public
...
curried();
public
Term
f();
public
Term
v();
public
App
copy(Term,
Term);
public
j.l.String
productPrefix();
public
boolean
equals(j.l.Object);
...
59. public
final
class
App$
extends
s.r.AbstractFunction2<Term,
Term,
App>
implements
scala.Serializable
public
App
apply(Term,
Term);
0:
new
#23
//
class
App
3:
dup
4:
aload_1
5:
aload_2
6:
invokespecial
#26
//
Method
App."<init>":(LTerm;LTerm;)V
9:
areturn
60. Fun
and
Var
looks
the
same
as
App
FunとVarも同様の内容です
61. Scala
compiler
generates
many
functions
from
few
snippets
of
code
Scalaコンパイラは少ないコードから
多くの機能を生成しています
63. object
MatchTest2
extends
App
{
def
matchTest(x:
Any):
Any
=
x
match
{
case
1
=>
"one"
case
"two"
=>
2
case
y:
Int
=>
"scala.Int"
}
println(matchTest("two"))
}
88. § Summary
§ JVM
is
excellent!
§ class
file
has
a
format
§ javap
is
useful
to
understand
Scala
at
the
bytecode
level
Try
typing
"javap"
after
you
compile
Scala
code