Rather than exposing Vector, providing abstraction to encapsulate manipulation with TrapData via 'iterator'
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import org.apidesign.javap.AnnotationParser;
23 import org.apidesign.javap.ClassData;
24 import org.apidesign.javap.FieldData;
25 import org.apidesign.javap.MethodData;
26 import static org.apidesign.javap.RuntimeConstants.*;
27 import org.apidesign.javap.TrapData;
28 import org.apidesign.javap.TrapDataIterator;
30 /** Translator of the code inside class files to JavaScript.
32 * @author Jaroslav Tulach <jtulach@netbeans.org>
34 public abstract class ByteCodeToJavaScript {
38 protected ByteCodeToJavaScript(Appendable out) {
42 /* Collects additional required resources.
44 * @param internalClassName classes that were referenced and should be loaded in order the
45 * generated JavaScript code works properly. The names are in internal
46 * JVM form so String is <code>java/lang/String</code>.
48 protected abstract boolean requireReference(String internalClassName);
51 * @param resourcePath name of resources to read
53 protected abstract void requireScript(String resourcePath);
55 /** Allows subclasses to redefine what field a function representing a
56 * class gets assigned. By default it returns the suggested name followed
57 * by <code>" = "</code>;
59 * @param className suggested name of the class
61 protected String assignClass(String className) {
62 return className + " = ";
66 * Converts a given class file to a JavaScript version.
68 * @param classFile input stream with code of the .class file
69 * @return the initialization code for this class, if any. Otherwise <code>null</code>
71 * @throws IOException if something goes wrong during read or write or translating
74 public String compile(InputStream classFile) throws IOException {
75 this.jc = new ClassData(classFile);
76 byte[] arrData = jc.findAnnotationData(true);
77 String[] arr = findAnnotation(arrData, jc,
78 "org.apidesign.bck2brwsr.core.ExtraJavaScript",
79 "resource", "processByteCode"
82 requireScript(arr[0]);
83 if ("0".equals(arr[1])) {
87 String[] proto = findAnnotation(arrData, jc,
88 "org.apidesign.bck2brwsr.core.JavaScriptPrototype",
89 "container", "prototype"
91 StringArray toInitilize = new StringArray();
92 final String className = className(jc);
93 out.append("\n\n").append(assignClass(className));
94 out.append("function CLS() {");
95 out.append("\n if (!CLS.prototype.$instOf_").append(className).append(") {");
96 for (FieldData v : jc.getFields()) {
98 out.append("\n CLS.").append(v.getName()).append(initField(v));
102 String sc = jc.getSuperClassName(); // with _
103 out.append("\n var pp = ").
104 append(sc.replace('/', '_')).append("(true);");
105 out.append("\n var p = CLS.prototype = pp;");
106 out.append("\n var c = p;");
107 out.append("\n var sprcls = pp.constructor.$class;");
109 out.append("\n var p = CLS.prototype = ").append(proto[1]).append(";");
110 out.append("\n var c = ").append(proto[0]).append(";");
111 out.append("\n var sprcls = null;");
113 for (MethodData m : jc.getMethods()) {
114 byte[] onlyArr = m.findAnnotationData(true);
115 String[] only = findAnnotation(onlyArr, jc,
116 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
120 if (only[0] != null && only[1] != null) {
121 out.append("\n p.").append(only[0]).append(" = ")
122 .append(only[1]).append(";");
128 mn = generateStaticMethod("\n c.", m, toInitilize);
130 mn = generateInstanceMethod("\n c.", m);
132 byte[] runAnno = m.findAnnotationData(false);
133 if (runAnno != null) {
134 out.append("\n c.").append(mn).append(".anno = {");
135 generateAnno(jc, out, runAnno);
139 out.append("\n c.constructor = CLS;");
140 out.append("\n c.$instOf_").append(className).append(" = true;");
141 for (String superInterface : jc.getSuperInterfaces()) {
142 out.append("\n c.$instOf_").append(superInterface.replace('/', '_')).append(" = true;");
144 out.append("\n CLS.$class = java_lang_Class(true);");
145 out.append("\n CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
146 out.append("\n CLS.$class.superclass = sprcls;");
147 out.append("\n CLS.$class.cnstr = CLS;");
148 byte[] classAnno = jc.findAnnotationData(false);
149 if (classAnno != null) {
150 out.append("\n CLS.$class.anno = {");
151 generateAnno(jc, out, classAnno);
155 out.append("\n if (arguments.length === 0) {");
156 out.append("\n if (!(this instanceof CLS)) {");
157 out.append("\n return new CLS();");
159 for (FieldData v : jc.getFields()) {
160 byte[] onlyArr = v.findAnnotationData(true);
161 String[] only = findAnnotation(onlyArr, jc,
162 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
166 if (only[0] != null && only[1] != null) {
167 out.append("\n p.").append(only[0]).append(" = ")
168 .append(only[1]).append(";");
173 out.append("\n this.fld_").
174 append(v.getName()).append(initField(v));
177 out.append("\n return this;");
179 out.append("\n return arguments[0] ? new CLS() : CLS.prototype;");
181 StringBuilder sb = new StringBuilder();
182 for (String init : toInitilize.toArray()) {
183 sb.append("\n").append(init).append("();");
185 return sb.toString();
187 private String generateStaticMethod(String prefix, MethodData m, StringArray toInitilize) throws IOException {
188 String jsb = javaScriptBody(prefix, m, true);
192 StringBuilder argsCnt = new StringBuilder();
193 final String mn = findMethodName(m, argsCnt);
194 out.append(prefix).append(mn).append(" = function");
195 if (mn.equals("class__V")) {
196 toInitilize.add(className(jc) + "(false)." + mn);
200 for (int index = 0, i = 0; i < argsCnt.length(); i++) {
202 out.append("arg").append(String.valueOf(index));
204 final String desc = null;// XXX findDescriptor(args.get(i).getDescriptor());
205 if (argsCnt.charAt(i) == '1') {
211 out.append(") {").append("\n");
212 final byte[] code = m.getCode();
214 int len = m.getMaxLocals();
215 for (int index = argsCnt.length(), i = argsCnt.length(); i < len; i++) {
217 out.append("arg").append(String.valueOf(i)).append(";\n");
219 out.append(" var s = new Array();\n");
222 out.append(" throw 'no code found for ").append(m.getInternalSig()).append("';\n");
228 private String generateInstanceMethod(String prefix, MethodData m) throws IOException {
229 String jsb = javaScriptBody(prefix, m, false);
233 StringBuilder argsCnt = new StringBuilder();
234 final String mn = findMethodName(m, argsCnt);
235 out.append(prefix).append(mn).append(" = function");
238 for (int index = 1, i = 0; i < argsCnt.length(); i++) {
240 out.append("arg").append(String.valueOf(index));
241 if (argsCnt.charAt(i) == '1') {
247 out.append(") {").append("\n");
248 final byte[] code = m.getCode();
250 int len = m.getMaxLocals();
251 for (int index = argsCnt.length(), i = argsCnt.length(); i < len; i++) {
253 out.append("arg").append(String.valueOf(i + 1)).append(";\n");
255 out.append(";\n var s = new Array();\n");
258 out.append(" throw 'no code found for ").append(m.getInternalSig()).append("';\n");
264 private void produceCode(MethodData md) throws IOException {
265 byte[] byteCodes = md.getCode();
267 TrapDataIterator trap = md.getTrapDataIterator();
268 out.append("\n var gt = 0;\n for(;;) switch(gt) {\n");
270 for (int i = 0; i < byteCodes.length; i++) {
274 out.append(" case " + i).append(": ");
278 final int c = readByte(byteCodes, i);
285 out.append("s.push(arg0);");
292 out.append("s.push(arg1);");
299 out.append("s.push(arg2);");
306 out.append("s.push(arg3);");
313 final int indx = readByte(byteCodes, ++i);
314 out.append("s.push(arg").append(indx + ");");
322 final int indx = readByte(byteCodes, ++i);
323 out.append("arg" + indx).append(" = s.pop();");
331 out.append("arg0 = s.pop();");
338 out.append("arg1 = s.pop();");
345 out.append("arg2 = s.pop();");
352 out.append("arg3 = s.pop();");
358 out.append("s.push(s.pop() + s.pop());");
364 out.append("{ var tmp = s.pop(); s.push(s.pop() - tmp); }");
370 out.append("s.push(s.pop() * s.pop());");
374 out.append("{ var tmp = s.pop(); s.push(Math.floor(s.pop() / tmp)); }");
378 out.append("{ var tmp = s.pop(); s.push(s.pop() / tmp); }");
384 out.append("{ var d = s.pop(); s.push(s.pop() % d); }");
388 out.append("s.push(s.pop() & s.pop());");
392 out.append("s.push(s.pop() | s.pop());");
396 out.append("s.push(s.pop() ^ s.pop());");
402 out.append("s.push(- s.pop());");
406 out.append("{ var v = s.pop(); s.push(s.pop() << v); }");
410 out.append("{ var v = s.pop(); s.push(s.pop() >> v); }");
414 out.append("{ var v = s.pop(); s.push(s.pop() >>> v); }");
417 final int varIndx = readByte(byteCodes, ++i);
418 final int incrBy = byteCodes[++i];
420 out.append("arg" + varIndx).append("++;");
422 out.append("arg" + varIndx).append(" += " + incrBy).append(";");
427 out.append("return;");
434 out.append("return s.pop();");
445 out.append("/* number conversion */");
451 out.append("s.push(Math.floor(s.pop()));");
456 out.append("/* number conversion */");
458 case opc_aconst_null:
459 out.append("s.push(null);");
462 out.append("s.push(-1);");
468 out.append("s.push(0);");
474 out.append("s.push(1);");
478 out.append("s.push(2);");
481 out.append("s.push(3);");
484 out.append("s.push(4);");
487 out.append("s.push(5);");
490 int indx = readByte(byteCodes, ++i);
491 String v = encodeConstant(indx);
492 out.append("s.push(").append(v).append(");");
497 int indx = readIntArg(byteCodes, i);
499 String v = encodeConstant(indx);
500 out.append("s.push(").append(v).append(");");
508 out.append("{ var delta = s.pop() - s.pop(); s.push(delta < 0 ?-1 : (delta == 0 ? 0 : 1)); }");
512 i = generateIf(byteCodes, i, "===");
515 i = generateIf(byteCodes, i, "!=");
517 case opc_if_icmpeq: {
518 i = generateIf(byteCodes, i, "==");
522 int indx = i + readIntArg(byteCodes, i);
523 out.append("if (s.pop() == 0) { gt = " + indx);
524 out.append("; continue; }");
529 int indx = i + readIntArg(byteCodes, i);
530 out.append("if (s.pop() != 0) { gt = " + indx);
531 out.append("; continue; }");
536 int indx = i + readIntArg(byteCodes, i);
537 out.append("if (s.pop() < 0) { gt = " + indx);
538 out.append("; continue; }");
543 int indx = i + readIntArg(byteCodes, i);
544 out.append("if (s.pop() <= 0) { gt = " + indx);
545 out.append("; continue; }");
550 int indx = i + readIntArg(byteCodes, i);
551 out.append("if (s.pop() > 0) { gt = " + indx);
552 out.append("; continue; }");
557 int indx = i + readIntArg(byteCodes, i);
558 out.append("if (s.pop() >= 0) { gt = " + indx);
559 out.append("; continue; }");
563 case opc_ifnonnull: {
564 int indx = i + readIntArg(byteCodes, i);
565 out.append("if (s.pop() !== null) { gt = " + indx);
566 out.append("; continue; }");
571 int indx = i + readIntArg(byteCodes, i);
572 out.append("if (s.pop() === null) { gt = " + indx);
573 out.append("; continue; }");
578 i = generateIf(byteCodes, i, "!=");
581 i = generateIf(byteCodes, i, ">");
584 i = generateIf(byteCodes, i, ">=");
587 i = generateIf(byteCodes, i, "<");
590 i = generateIf(byteCodes, i, "<=");
593 int indx = i + readIntArg(byteCodes, i);
594 out.append("gt = " + indx).append("; continue;");
598 case opc_lookupswitch: {
599 int table = i / 4 * 4 + 4;
600 int dflt = i + readInt4(byteCodes, table);
602 int n = readInt4(byteCodes, table);
604 out.append("switch (s.pop()) {\n");
606 int cnstnt = readInt4(byteCodes, table);
608 int offset = i + readInt4(byteCodes, table);
610 out.append(" case " + cnstnt).append(": gt = " + offset).append("; continue;\n");
612 out.append(" default: gt = " + dflt).append("; continue;\n}");
616 case opc_tableswitch: {
617 int table = i / 4 * 4 + 4;
618 int dflt = i + readInt4(byteCodes, table);
620 int low = readInt4(byteCodes, table);
622 int high = readInt4(byteCodes, table);
624 out.append("switch (s.pop()) {\n");
625 while (low <= high) {
626 int offset = i + readInt4(byteCodes, table);
628 out.append(" case " + low).append(": gt = " + offset).append("; continue;\n");
631 out.append(" default: gt = " + dflt).append("; continue;\n}");
635 case opc_invokeinterface: {
636 i = invokeVirtualMethod(byteCodes, i) + 2;
639 case opc_invokevirtual:
640 i = invokeVirtualMethod(byteCodes, i);
642 case opc_invokespecial:
643 i = invokeStaticMethod(byteCodes, i, false);
645 case opc_invokestatic:
646 i = invokeStaticMethod(byteCodes, i, true);
649 int indx = readIntArg(byteCodes, i);
650 String ci = jc.getClassName(indx);
651 out.append("s.push(new ");
652 out.append(ci.replace('/','_'));
659 int type = byteCodes[i++];
660 out.append("s.push(new Array(s.pop()).fillNulls());");
663 case opc_anewarray: {
664 i += 2; // skip type of array
665 out.append("s.push(new Array(s.pop()).fillNulls());");
668 case opc_multianewarray: {
670 int dim = readByte(byteCodes, ++i);
671 out.append("{ var a0 = new Array(s.pop()).fillNulls();");
672 for (int d = 1; d < dim; d++) {
673 out.append("\n var l" + d).append(" = s.pop();");
674 out.append("\n for (var i" + d).append (" = 0; i" + d).
675 append(" < a" + (d - 1)).
676 append(".length; i" + d).append("++) {");
677 out.append("\n var a" + d).
678 append (" = new Array(l" + d).append(").fillNulls();");
679 out.append("\n a" + (d - 1)).append("[i" + d).append("] = a" + d).
682 for (int d = 1; d < dim; d++) {
685 out.append("\ns.push(a0); }");
688 case opc_arraylength:
689 out.append("s.push(s.pop().length);");
699 out.append("{ var value = s.pop(); var indx = s.pop(); s.pop()[indx] = value; }");
710 out.append("{ var indx = s.pop(); s.push(s.pop()[indx]); }");
714 out.append("s.pop();");
716 out.append("s.pop();");
719 out.append("s.push(s[s.length - 1]);");
722 out.append("{ var v1 = s.pop(); var v2 = s.pop(); s.push(v1); s.push(v2); s.push(v1); }");
725 out.append("{ var v1 = s.pop(); var v2 = s.pop(); var v3 = s.pop(); s.push(v1); s.push(v3); s.push(v2); s.push(v1); }");
728 out.append("s.push(" + byteCodes[++i] + ");");
731 out.append("s.push(" + readIntArg(byteCodes, i) + ");");
735 int indx = readIntArg(byteCodes, i);
736 String[] fi = jc.getFieldInfoName(indx);
737 out.append("s.push(s.pop().fld_").
738 append(fi[1]).append(");");
742 case opc_getstatic: {
743 int indx = readIntArg(byteCodes, i);
744 String[] fi = jc.getFieldInfoName(indx);
745 out.append("s.push(").append(fi[0].replace('/', '_'));
746 out.append('.').append(fi[1]).append(");");
751 case opc_putstatic: {
752 int indx = readIntArg(byteCodes, i);
753 String[] fi = jc.getFieldInfoName(indx);
754 out.append(fi[0].replace('/', '_'));
755 out.append('.').append(fi[1]).append(" = s.pop();");
761 int indx = readIntArg(byteCodes, i);
762 String[] fi = jc.getFieldInfoName(indx);
763 out.append("{ var v = s.pop(); s.pop().fld_")
764 .append(fi[1]).append(" = v; }");
768 case opc_checkcast: {
769 int indx = readIntArg(byteCodes, i);
770 final String type = jc.getClassName(indx);
771 if (!type.startsWith("[")) {
772 // no way to check arrays right now
773 out.append("if(s[s.length - 1].$instOf_")
774 .append(type.replace('/', '_'))
775 .append(" != 1) throw {};"); // XXX proper exception
780 case opc_instanceof: {
781 int indx = readIntArg(byteCodes, i);
782 final String type = jc.getClassName(indx);
783 out.append("s.push(s.pop().$instOf_")
784 .append(type.replace('/', '_'))
785 .append(" ? 1 : 0);");
790 out.append("{ var t = s.pop(); s = new Array(1); s[0] = t; throw t; }");
794 out.append("throw 'unknown bytecode " + c + "';");
799 out.append("} catch (e) {");
800 for (TrapData e : trap.current()) {
801 if (e.catch_cpx != 0) { //not finally
802 final String classInternalName = jc.getClassName(e.catch_cpx);
803 addReference(classInternalName);
804 out.append("if (e.$instOf_"+classInternalName.replace('/', '_')+") {");
805 out.append("gt="+e.handler_pc+"; continue;");
811 out.append("throw e;");
815 for (int j = prev; j <= i; j++) {
817 final int cc = readByte(byteCodes, j);
818 out.append(Integer.toString(cc));
825 private int generateIf(byte[] byteCodes, int i, final String test) throws IOException {
826 int indx = i + readIntArg(byteCodes, i);
827 out.append("if (s.pop() ").append(test).append(" s.pop()) { gt = " + indx);
828 out.append("; continue; }");
832 private int readIntArg(byte[] byteCodes, int offsetInstruction) {
833 final int indxHi = byteCodes[offsetInstruction + 1] << 8;
834 final int indxLo = byteCodes[offsetInstruction + 2];
835 return (indxHi & 0xffffff00) | (indxLo & 0xff);
837 private int readInt4(byte[] byteCodes, int offsetInstruction) {
838 final int d = byteCodes[offsetInstruction + 0] << 24;
839 final int c = byteCodes[offsetInstruction + 1] << 16;
840 final int b = byteCodes[offsetInstruction + 2] << 8;
841 final int a = byteCodes[offsetInstruction + 3];
842 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
844 private int readByte(byte[] byteCodes, int offsetInstruction) {
845 return (byteCodes[offsetInstruction] + 256) % 256;
848 private static void countArgs(String descriptor, boolean[] hasReturnType, StringBuilder sig, StringBuilder cnt) {
850 Boolean count = null;
851 boolean array = false;
853 int firstPos = sig.length();
854 while (i < descriptor.length()) {
855 char ch = descriptor.charAt(i++);
876 if (ch == 'J' || ch == 'D') {
882 hasReturnType[0] = true;
883 sig.insert(firstPos, ch);
885 sig.insert(firstPos, "_3");
892 hasReturnType[0] = false;
893 sig.insert(firstPos, 'V');
896 int next = descriptor.indexOf(';', i);
897 String realSig = mangleSig(descriptor, i - 1, next + 1);
905 sig.insert(firstPos, realSig);
907 sig.insert(firstPos, "_3");
909 hasReturnType[0] = true;
917 throw new IllegalStateException("Invalid char: " + ch);
922 private static String mangleSig(String txt, int first, int last) {
923 StringBuilder sb = new StringBuilder();
924 for (int i = first; i < last; i++) {
925 final char ch = txt.charAt(i);
927 case '/': sb.append('_'); break;
928 case '_': sb.append("_1"); break;
929 case ';': sb.append("_2"); break;
930 case '[': sb.append("_3"); break;
931 default: sb.append(ch); break;
934 return sb.toString();
937 private static String findMethodName(MethodData m, StringBuilder cnt) {
938 StringBuilder name = new StringBuilder();
939 if ("<init>".equals(m.getName())) { // NOI18N
940 name.append("cons"); // NOI18N
941 } else if ("<clinit>".equals(m.getName())) { // NOI18N
942 name.append("class"); // NOI18N
944 name.append(m.getName());
947 boolean hasReturn[] = { false };
948 countArgs(m.getInternalSig(), hasReturn, name, cnt);
949 return name.toString();
952 static String findMethodName(String[] mi, StringBuilder cnt, boolean[] hasReturn) {
953 StringBuilder name = new StringBuilder();
954 String descr = mi[2];//mi.getDescriptor();
956 if ("<init>".equals(nm)) { // NOI18N
957 name.append("cons"); // NOI18N
961 countArgs(descr, hasReturn, name, cnt);
962 return name.toString();
965 private int invokeStaticMethod(byte[] byteCodes, int i, boolean isStatic)
967 int methodIndex = readIntArg(byteCodes, i);
968 String[] mi = jc.getFieldInfoName(methodIndex);
969 boolean[] hasReturn = { false };
970 StringBuilder cnt = new StringBuilder();
971 String mn = findMethodName(mi, cnt, hasReturn);
973 for (int j = cnt.length() - 1; j >= 0; j--) {
974 out.append("var v" + j).append(" = s.pop(); ");
978 out.append("s.push(");
980 final String in = mi[0];
981 out.append(in.replace('/', '_'));
982 out.append("(false).");
987 out.append("s.pop()");
990 for (int j = 0; j < cnt.length(); j++) {
1004 private int invokeVirtualMethod(byte[] byteCodes, int i)
1005 throws IOException {
1006 int methodIndex = readIntArg(byteCodes, i);
1007 String[] mi = jc.getFieldInfoName(methodIndex);
1008 boolean[] hasReturn = { false };
1009 StringBuilder cnt = new StringBuilder();
1010 String mn = findMethodName(mi, cnt, hasReturn);
1012 for (int j = cnt.length() - 1; j >= 0; j--) {
1013 out.append("var v" + j).append(" = s.pop(); ");
1015 out.append("var self = s.pop(); ");
1017 out.append("s.push(");
1019 out.append("self.");
1023 for (int j = 0; j < cnt.length(); j++) {
1025 out.append("v" + j);
1036 private void addReference(String cn) throws IOException {
1037 if (requireReference(cn)) {
1038 out.append(" /* needs ").append(cn).append(" */");
1042 private void outType(String d, StringBuilder out) {
1044 while (d.charAt(0) == '[') {
1048 if (d.charAt(0) == 'L') {
1049 assert d.charAt(d.length() - 1) == ';';
1050 out.append(d.replace('/', '_').substring(0, d.length() - 1));
1056 private String encodeConstant(int entryIndex) throws IOException {
1057 String[] classRef = { null };
1058 String s = jc.stringValue(entryIndex, classRef);
1059 if (classRef[0] != null) {
1060 addReference(classRef[0]);
1065 private String javaScriptBody(String prefix, MethodData m, boolean isStatic) throws IOException {
1066 byte[] arr = m.findAnnotationData(true);
1070 final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
1071 class P extends AnnotationParser {
1077 String[] args = new String[30];
1081 protected void visitAttr(String type, String attr, String at, String value) {
1082 if (type.equals(jvmType)) {
1083 if ("body".equals(attr)) {
1085 } else if ("args".equals(attr)) {
1086 args[cnt++] = value;
1088 throw new IllegalArgumentException(attr);
1095 if (p.body == null) {
1098 StringBuilder cnt = new StringBuilder();
1099 final String mn = findMethodName(m, cnt);
1100 out.append(prefix).append(mn);
1101 out.append(" = function(");
1105 out.append(p.args[0]);
1112 for (int i = 0; i < cnt.length(); i++) {
1114 out.append(p.args[index]);
1118 out.append(") {").append("\n");
1120 out.append("\n}\n");
1123 private static String className(ClassData jc) {
1124 //return jc.getName().getInternalName().replace('/', '_');
1125 return jc.getClassName().replace('/', '_');
1128 private static String[] findAnnotation(
1129 byte[] arr, ClassData cd, final String className,
1130 final String... attrNames
1131 ) throws IOException {
1135 final String[] values = new String[attrNames.length];
1136 final boolean[] found = { false };
1137 final String jvmType = "L" + className.replace('.', '/') + ";";
1138 AnnotationParser ap = new AnnotationParser(false) {
1140 protected void visitAttr(String type, String attr, String at, String value) {
1141 if (type.equals(jvmType)) {
1143 for (int i = 0; i < attrNames.length; i++) {
1144 if (attrNames[i].equals(attr)) {
1153 return found[0] ? values : null;
1156 private CharSequence initField(FieldData v) {
1157 final String is = v.getInternalSig();
1158 if (is.length() == 1) {
1159 switch (is.charAt(0)) {
1165 case 'I': return " = 0;";
1167 case 'D': return " = 0.0;";
1169 throw new IllegalStateException(is);
1175 private static void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
1176 AnnotationParser ap = new AnnotationParser(true) {
1181 protected void visitAnnotationStart(String type) throws IOException {
1185 out.append('"').append(type).append("\" : {\n");
1190 protected void visitAnnotationEnd(String type) throws IOException {
1191 out.append("\n}\n");
1195 protected void visitAttr(String type, String attr, String attrType, String value)
1196 throws IOException {
1203 out.append(attr).append("__").append(attrType).append(" : ").append(value);