Skip to content

Commit

Permalink
Test class parser
Browse files Browse the repository at this point in the history
  • Loading branch information
zxh0 committed Oct 6, 2019
1 parent 53d8ec7 commit 271576f
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 6 deletions.
12 changes: 6 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.gradle
build

.DS_Store
*.swp

/java
.idea
.gradle

.idea
build
out
myjre
/java
37 changes: 37 additions & 0 deletions classfile/class_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package classfile

import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/require"
)

func TestJava8HW(t *testing.T) {
bytes, err := ioutil.ReadFile("../test/testdata/java8/HelloWorld.class")
require.NoError(t, err)

cf, err := Parse(bytes)
require.NoError(t, err)
require.Equal(t, uint16(52), cf.MajorVersion)
require.Equal(t, uint16(0), cf.MinorVersion)
require.Equal(t, 34, len(cf.ConstantPool))
require.Equal(t, uint16(0x21), cf.AccessFlags)
require.Equal(t, "HelloWorld", cf.GetThisClassName())
require.Equal(t, "java/lang/Object", cf.GetSuperClassName())
require.Equal(t, []string{}, cf.GetInterfaceNames())
require.Equal(t, 0, len(cf.Fields))
require.Equal(t, 2, len(cf.Methods))
require.Equal(t, 1, len(cf.AttributeTable.attributes))
require.Equal(t, "HelloWorld.java", cf.GetUTF8(cf.GetSourceFileIndex()))
}

func TestJava13HW(t *testing.T) {
bytes, err := ioutil.ReadFile("../test/testdata/java13/HelloWorld.class")
require.NoError(t, err)

cf, err := Parse(bytes)
require.NoError(t, err)
require.Equal(t, uint16(57), cf.MajorVersion)
require.Equal(t, uint16(0), cf.MinorVersion)
}
7 changes: 7 additions & 0 deletions test/hw_module/src/hello.modules/hello/HelloWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package hello;

class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
3 changes: 3 additions & 0 deletions test/hw_module/src/hello.modules/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module hello.modules {
exports hello;
}
14 changes: 14 additions & 0 deletions test/hw_module/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
set -e

alias javac='~/.sdkman/candidates/java/13.0.0-open/bin/javac'
alias jlink='~/.sdkman/candidates/java/13.0.0-open/bin/jlink'

javac -version | grep 'javac 13'
jlink --version | grep 13

OUT=myjre
rm -rf $OUT
javac --module-source-path src -d out -m hello.modules
jlink --module-path out --add-modules hello.modules,java.base --output $OUT
./$OUT/bin/java -m hello.modules/hello.HelloWorld
Binary file added test/testdata/java13/HelloWorld.class
Binary file not shown.
Binary file added test/testdata/java13/module-info.class
Binary file not shown.
Binary file added test/testdata/java8/HelloWorld.class
Binary file not shown.

0 comments on commit 271576f

Please sign in to comment.