Skip to content

Commit a40b1dd

Browse files
author
NatureFreshMeat
committed
Added JsonConverter and converter class
1 parent f70dbbb commit a40b1dd

File tree

3 files changed

+44
-72
lines changed

3 files changed

+44
-72
lines changed
Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,64 @@
11
packagech.ri.jsjvm.utils;
22

3+
importjava.io.ByteArrayOutputStream;
4+
importjava.io.File;
35
importjava.io.IOException;
4-
importjava.lang.reflect.Method;
5-
importjava.net.URL;
6-
importjava.util.Enumeration;
6+
importjava.io.InputStream;
7+
importjava.io.PrintStream;
8+
79

810
publicclassClassToJson
911
{
1012

1113
publicstaticvoidmain(String[] args) throwsException
1214
{
13-
14-
ClassConverterLoaderloader = newClassConverterLoader();
15-
Class<?> class1 = loader.loadClass( TestClass.class.getName() );
16-
Methodmethod = class1.getMethod("main", String[].class);
17-
method.invoke(null, newObject[]{null});
15+
convert( TestClass.class.getName() );
1816
}
1917

18+
privatestaticfinalStringoutputDir = "test/classes/";
2019

21-
publicstaticclassClassConverterLoaderextendsClassLoader
20+
privatestaticvoidconvert(StringclassName) throwsIOException
2221
{
23-
24-
25-
@Override
26-
publicEnumeration<URL> getResources(Stringname) throwsIOException
27-
{
28-
System.out.println("getResources: " + name);
29-
returnsuper.getResources(name);
30-
}
31-
32-
@Override
33-
publicClass<?> findClass(Stringname) throwsClassNotFoundException
34-
{
35-
System.out.println("findClass: " + name);
36-
returnsuper.findClass(name);
37-
}
38-
39-
@Override
40-
publicClass<?> loadClass(Stringname) throwsClassNotFoundException
41-
{
42-
System.out.println("loadClass: " + name);
43-
returnsuper.loadClass(name);
44-
}
45-
46-
@Override
47-
protectedURLfindResource(Stringname)
48-
{
49-
System.out.println("findResource: " + name);
50-
returnsuper.findResource(name);
51-
}
52-
53-
@Override
54-
protectedStringfindLibrary(Stringlibname)
55-
{
56-
System.out.println("findLibrary: " + libname);
57-
returnsuper.findLibrary(libname);
58-
}
59-
60-
@Override
61-
protectedClass<?> loadClass(Stringname, booleanresolve)
62-
throwsClassNotFoundException
22+
FileoutputFile = newFile(outputDir + className + ".json");
23+
24+
if (outputFile.exists())
25+
outputFile.delete();
26+
27+
PrintStreamprinter = newPrintStream(outputFile);
28+
printer.print("{\"name\":\"" + className + "\",\"bytes\":[");
29+
30+
InputStreamstream = ClassToJson.class.getResourceAsStream( "/" + className.replace(".", "/") + ".class" );
31+
32+
ByteArrayOutputStreamoutput = newByteArrayOutputStream(4096);
33+
34+
byte[] buffer = newbyte[4096];
35+
intoffset = 0;
36+
37+
while(true)
6338
{
64-
System.out.println("loadClass: " + name + "," + resolve);
65-
returnsuper.loadClass(name, resolve);
39+
intcount = stream.read(buffer);
40+
41+
if (count <= 0)
42+
break;
43+
44+
output.write(buffer, offset, count);
45+
offset += count;
6646
}
67-
68-
@Override
69-
publicURLgetResource(Stringname)
47+
48+
byte[] bytes = output.toByteArray();
49+
for (inti=0; i<bytes.length; i++)
7050
{
71-
System.out.println("getResource" + name);
72-
returnsuper.getResource(name);
51+
printer.print( bytes[i] & 0xFF );
52+
53+
if (i<bytes.length-1)
54+
printer.print(",");
7355
}
7456

57+
printer.print("]}");
58+
59+
printer.flush();
60+
printer.close();
61+
7562
}
7663

7764
}
Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
packagech.ri.jsjvm.utils;
22

3-
importjava.util.Date;
4-
importjava.util.Vector;
53

64
publicclassTestClass
75
{
8-
static
9-
{
10-
Dated = newDate();
11-
d.getTime();
12-
13-
Vector<String> x = newVector<String>();
14-
x.clear();
15-
16-
System.out.println("Loaded");
17-
}
186

197
/**
208
* @param args
219
*/
2210
publicstaticvoidmain(String[] args)
2311
{
24-
System.out.println("main");
25-
date = newDate();
2612
}
27-
28-
publicstaticDatedate;
2913
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"ch.ri.jsjvm.utils.TestClass","bytes":[202,254,186,190,0,0,0,50,0,20,7,0,2,1,0,27,99,104,47,114,105,47,106,115,106,118,109,47,117,116,105,108,115,47,84,101,115,116,67,108,97,115,115,7,0,4,1,0,16,106,97,118,97,47,108,97,110,103,47,79,98,106,101,99,116,1,0,6,60,105,110,105,116,62,1,0,3,40,41,86,1,0,4,67,111,100,101,10,0,3,0,9,12,0,5,0,6,1,0,15,76,105,110,101,78,117,109,98,101,114,84,97,98,108,101,1,0,18,76,111,99,97,108,86,97,114,105,97,98,108,101,84,97,98,108,101,1,0,4,116,104,105,115,1,0,29,76,99,104,47,114,105,47,106,115,106,118,109,47,117,116,105,108,115,47,84,101,115,116,67,108,97,115,115,59,1,0,4,109,97,105,110,1,0,22,40,91,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,41,86,1,0,4,97,114,103,115,1,0,19,91,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,1,0,10,83,111,117,114,99,101,70,105,108,101,1,0,14,84,101,115,116,67,108,97,115,115,46,106,97,118,97,0,33,0,1,0,3,0,0,0,0,0,2,0,1,0,5,0,6,0,1,0,7,0,0,0,47,0,1,0,1,0,0,0,5,42,183,0,8,177,0,0,0,2,0,10,0,0,0,6,0,1,0,0,0,4,0,11,0,0,0,12,0,1,0,0,0,5,0,12,0,13,0,0,0,9,0,14,0,15,0,1,0,7,0,0,0,43,0,0,0,1,0,0,0,1,177,0,0,0,2,0,10,0,0,0,6,0,1,0,0,0,12,0,11,0,0,0,12,0,1,0,0,0,1,0,16,0,17,0,0,0,1,0,18,0,0,0,2,0,19]}

0 commit comments

Comments
(0)