Need the ability to cast to any type to successfully run tests on VM with exceptions support
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] !== null && !s[s.length - 1].$instOf_")
774 .append(type.replace('/', '_'))
775 .append(") 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()) {
804 if (e.catch_cpx != 0) { //not finally
805 final String classInternalName = jc.getClassName(e.catch_cpx);
806 addReference(classInternalName);
807 out.append("if (e.$instOf_"+classInternalName.replace('/', '_')+") {");
808 out.append("gt="+e.handler_pc+"; continue;");
814 out.append("throw e;");
818 for (int j = prev; j <= i; j++) {
820 final int cc = readByte(byteCodes, j);
821 out.append(Integer.toString(cc));
828 private int generateIf(byte[] byteCodes, int i, final String test) throws IOException {
829 int indx = i + readIntArg(byteCodes, i);
830 out.append("if (s.pop() ").append(test).append(" s.pop()) { gt = " + indx);
831 out.append("; continue; }");
835 private int readIntArg(byte[] byteCodes, int offsetInstruction) {
836 final int indxHi = byteCodes[offsetInstruction + 1] << 8;
837 final int indxLo = byteCodes[offsetInstruction + 2];
838 return (indxHi & 0xffffff00) | (indxLo & 0xff);
840 private int readInt4(byte[] byteCodes, int offsetInstruction) {
841 final int d = byteCodes[offsetInstruction + 0] << 24;
842 final int c = byteCodes[offsetInstruction + 1] << 16;
843 final int b = byteCodes[offsetInstruction + 2] << 8;
844 final int a = byteCodes[offsetInstruction + 3];
845 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
847 private int readByte(byte[] byteCodes, int offsetInstruction) {
848 return (byteCodes[offsetInstruction] + 256) % 256;
851 private static void countArgs(String descriptor, boolean[] hasReturnType, StringBuilder sig, StringBuilder cnt) {
853 Boolean count = null;
854 boolean array = false;
856 int firstPos = sig.length();
857 while (i < descriptor.length()) {
858 char ch = descriptor.charAt(i++);
879 if (ch == 'J' || ch == 'D') {
885 hasReturnType[0] = true;
886 sig.insert(firstPos, ch);
888 sig.insert(firstPos, "_3");
895 hasReturnType[0] = false;
896 sig.insert(firstPos, 'V');
899 int next = descriptor.indexOf(';', i);
900 String realSig = mangleSig(descriptor, i - 1, next + 1);
908 sig.insert(firstPos, realSig);
910 sig.insert(firstPos, "_3");
912 hasReturnType[0] = true;
920 throw new IllegalStateException("Invalid char: " + ch);
925 private static String mangleSig(String txt, int first, int last) {
926 StringBuilder sb = new StringBuilder();
927 for (int i = first; i < last; i++) {
928 final char ch = txt.charAt(i);
930 case '/': sb.append('_'); break;
931 case '_': sb.append("_1"); break;
932 case ';': sb.append("_2"); break;
933 case '[': sb.append("_3"); break;
934 default: sb.append(ch); break;
937 return sb.toString();
940 private static String findMethodName(MethodData m, StringBuilder cnt) {
941 StringBuilder name = new StringBuilder();
942 if ("<init>".equals(m.getName())) { // NOI18N
943 name.append("cons"); // NOI18N
944 } else if ("<clinit>".equals(m.getName())) { // NOI18N
945 name.append("class"); // NOI18N
947 name.append(m.getName());
950 boolean hasReturn[] = { false };
951 countArgs(m.getInternalSig(), hasReturn, name, cnt);
952 return name.toString();
955 static String findMethodName(String[] mi, StringBuilder cnt, boolean[] hasReturn) {
956 StringBuilder name = new StringBuilder();
957 String descr = mi[2];//mi.getDescriptor();
959 if ("<init>".equals(nm)) { // NOI18N
960 name.append("cons"); // NOI18N
964 countArgs(descr, hasReturn, name, cnt);
965 return name.toString();
968 private int invokeStaticMethod(byte[] byteCodes, int i, boolean isStatic)
970 int methodIndex = readIntArg(byteCodes, i);
971 String[] mi = jc.getFieldInfoName(methodIndex);
972 boolean[] hasReturn = { false };
973 StringBuilder cnt = new StringBuilder();
974 String mn = findMethodName(mi, cnt, hasReturn);
976 for (int j = cnt.length() - 1; j >= 0; j--) {
977 out.append("var v" + j).append(" = s.pop(); ");
981 out.append("s.push(");
983 final String in = mi[0];
984 out.append(in.replace('/', '_'));
985 out.append("(false).");
990 out.append("s.pop()");
993 for (int j = 0; j < cnt.length(); j++) {
1007 private int invokeVirtualMethod(byte[] byteCodes, int i)
1008 throws IOException {
1009 int methodIndex = readIntArg(byteCodes, i);
1010 String[] mi = jc.getFieldInfoName(methodIndex);
1011 boolean[] hasReturn = { false };
1012 StringBuilder cnt = new StringBuilder();
1013 String mn = findMethodName(mi, cnt, hasReturn);
1015 for (int j = cnt.length() - 1; j >= 0; j--) {
1016 out.append("var v" + j).append(" = s.pop(); ");
1018 out.append("var self = s.pop(); ");
1020 out.append("s.push(");
1022 out.append("self.");
1026 for (int j = 0; j < cnt.length(); j++) {
1028 out.append("v" + j);
1039 private void addReference(String cn) throws IOException {
1040 if (requireReference(cn)) {
1041 out.append(" /* needs ").append(cn).append(" */");
1045 private void outType(String d, StringBuilder out) {
1047 while (d.charAt(0) == '[') {
1051 if (d.charAt(0) == 'L') {
1052 assert d.charAt(d.length() - 1) == ';';
1053 out.append(d.replace('/', '_').substring(0, d.length() - 1));
1059 private String encodeConstant(int entryIndex) throws IOException {
1060 String[] classRef = { null };
1061 String s = jc.stringValue(entryIndex, classRef);
1062 if (classRef[0] != null) {
1063 addReference(classRef[0]);
1068 private String javaScriptBody(String prefix, MethodData m, boolean isStatic) throws IOException {
1069 byte[] arr = m.findAnnotationData(true);
1073 final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
1074 class P extends AnnotationParser {
1080 String[] args = new String[30];
1084 protected void visitAttr(String type, String attr, String at, String value) {
1085 if (type.equals(jvmType)) {
1086 if ("body".equals(attr)) {
1088 } else if ("args".equals(attr)) {
1089 args[cnt++] = value;
1091 throw new IllegalArgumentException(attr);
1098 if (p.body == null) {
1101 StringBuilder cnt = new StringBuilder();
1102 final String mn = findMethodName(m, cnt);
1103 out.append(prefix).append(mn);
1104 out.append(" = function(");
1108 out.append(p.args[0]);
1115 for (int i = 0; i < cnt.length(); i++) {
1117 out.append(p.args[index]);
1121 out.append(") {").append("\n");
1123 out.append("\n}\n");
1126 private static String className(ClassData jc) {
1127 //return jc.getName().getInternalName().replace('/', '_');
1128 return jc.getClassName().replace('/', '_');
1131 private static String[] findAnnotation(
1132 byte[] arr, ClassData cd, final String className,
1133 final String... attrNames
1134 ) throws IOException {
1138 final String[] values = new String[attrNames.length];
1139 final boolean[] found = { false };
1140 final String jvmType = "L" + className.replace('.', '/') + ";";
1141 AnnotationParser ap = new AnnotationParser(false) {
1143 protected void visitAttr(String type, String attr, String at, String value) {
1144 if (type.equals(jvmType)) {
1146 for (int i = 0; i < attrNames.length; i++) {
1147 if (attrNames[i].equals(attr)) {
1156 return found[0] ? values : null;
1159 private CharSequence initField(FieldData v) {
1160 final String is = v.getInternalSig();
1161 if (is.length() == 1) {
1162 switch (is.charAt(0)) {
1168 case 'I': return " = 0;";
1170 case 'D': return " = 0.0;";
1172 throw new IllegalStateException(is);
1178 private static void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
1179 AnnotationParser ap = new AnnotationParser(true) {
1184 protected void visitAnnotationStart(String type) throws IOException {
1188 out.append('"').append(type).append("\" : {\n");
1193 protected void visitAnnotationEnd(String type) throws IOException {
1194 out.append("\n}\n");
1198 protected void visitAttr(String type, String attr, String attrType, String value)
1199 throws IOException {
1206 out.append(attr).append("__").append(attrType).append(" : ").append(value);