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.Vector;
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");
220 produceCode(code, m.getexception_table());
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");
256 produceCode(code, m.getexception_table());
258 out.append(" throw 'no code found for ").append(m.getInternalSig()).append("';\n");
264 private void produceCode(byte[] byteCodes, Vector exceptionTable) throws IOException {
266 final java.util.Map<Short, TrapData> exStart = new java.util.HashMap<Short, TrapData>();
267 final java.util.Map<Short, TrapData> exStop = new java.util.HashMap<Short, TrapData>();
268 for (int i=0 ; i < exceptionTable.size(); i++) {
269 final TrapData td = (TrapData)exceptionTable.elementAt(i);
270 exStart.put(td.start_pc, td);
271 exStop.put(td.end_pc, td);
273 final java.util.Deque<TrapData> current = new java.util.ArrayDeque<TrapData>();
274 out.append("\n var gt = 0;\n for(;;) switch(gt) {\n");
276 for (int i = 0; i < byteCodes.length; i++) {
279 TrapData e = exStart.get((short)i);
283 e = exStop.get((short)i);
289 out.append(" case " + i).append(": ");
290 if (!current.isEmpty()) {
293 final int c = readByte(byteCodes, i);
300 out.append("s.push(arg0);");
307 out.append("s.push(arg1);");
314 out.append("s.push(arg2);");
321 out.append("s.push(arg3);");
328 final int indx = readByte(byteCodes, ++i);
329 out.append("s.push(arg").append(indx + ");");
337 final int indx = readByte(byteCodes, ++i);
338 out.append("arg" + indx).append(" = s.pop();");
346 out.append("arg0 = s.pop();");
353 out.append("arg1 = s.pop();");
360 out.append("arg2 = s.pop();");
367 out.append("arg3 = s.pop();");
373 out.append("s.push(s.pop() + s.pop());");
379 out.append("{ var tmp = s.pop(); s.push(s.pop() - tmp); }");
385 out.append("s.push(s.pop() * s.pop());");
389 out.append("{ var tmp = s.pop(); s.push(Math.floor(s.pop() / tmp)); }");
393 out.append("{ var tmp = s.pop(); s.push(s.pop() / tmp); }");
399 out.append("{ var d = s.pop(); s.push(s.pop() % d); }");
403 out.append("s.push(s.pop() & s.pop());");
407 out.append("s.push(s.pop() | s.pop());");
411 out.append("s.push(s.pop() ^ s.pop());");
417 out.append("s.push(- s.pop());");
421 out.append("{ var v = s.pop(); s.push(s.pop() << v); }");
425 out.append("{ var v = s.pop(); s.push(s.pop() >> v); }");
429 out.append("{ var v = s.pop(); s.push(s.pop() >>> v); }");
432 final int varIndx = readByte(byteCodes, ++i);
433 final int incrBy = byteCodes[++i];
435 out.append("arg" + varIndx).append("++;");
437 out.append("arg" + varIndx).append(" += " + incrBy).append(";");
442 out.append("return;");
449 out.append("return s.pop();");
460 out.append("/* number conversion */");
466 out.append("s.push(Math.floor(s.pop()));");
471 out.append("/* number conversion */");
473 case opc_aconst_null:
474 out.append("s.push(null);");
477 out.append("s.push(-1);");
483 out.append("s.push(0);");
489 out.append("s.push(1);");
493 out.append("s.push(2);");
496 out.append("s.push(3);");
499 out.append("s.push(4);");
502 out.append("s.push(5);");
505 int indx = readByte(byteCodes, ++i);
506 String v = encodeConstant(indx);
507 out.append("s.push(").append(v).append(");");
512 int indx = readIntArg(byteCodes, i);
514 String v = encodeConstant(indx);
515 out.append("s.push(").append(v).append(");");
523 out.append("{ var delta = s.pop() - s.pop(); s.push(delta < 0 ?-1 : (delta == 0 ? 0 : 1)); }");
527 i = generateIf(byteCodes, i, "===");
530 i = generateIf(byteCodes, i, "!=");
532 case opc_if_icmpeq: {
533 i = generateIf(byteCodes, i, "==");
537 int indx = i + readIntArg(byteCodes, i);
538 out.append("if (s.pop() == 0) { gt = " + indx);
539 out.append("; continue; }");
544 int indx = i + readIntArg(byteCodes, i);
545 out.append("if (s.pop() != 0) { gt = " + indx);
546 out.append("; continue; }");
551 int indx = i + readIntArg(byteCodes, i);
552 out.append("if (s.pop() < 0) { gt = " + indx);
553 out.append("; continue; }");
558 int indx = i + readIntArg(byteCodes, i);
559 out.append("if (s.pop() <= 0) { gt = " + indx);
560 out.append("; continue; }");
565 int indx = i + readIntArg(byteCodes, i);
566 out.append("if (s.pop() > 0) { gt = " + indx);
567 out.append("; continue; }");
572 int indx = i + readIntArg(byteCodes, i);
573 out.append("if (s.pop() >= 0) { gt = " + indx);
574 out.append("; continue; }");
578 case opc_ifnonnull: {
579 int indx = i + readIntArg(byteCodes, i);
580 out.append("if (s.pop() !== null) { gt = " + indx);
581 out.append("; continue; }");
586 int indx = i + readIntArg(byteCodes, i);
587 out.append("if (s.pop() === null) { gt = " + indx);
588 out.append("; continue; }");
593 i = generateIf(byteCodes, i, "!=");
596 i = generateIf(byteCodes, i, ">");
599 i = generateIf(byteCodes, i, ">=");
602 i = generateIf(byteCodes, i, "<");
605 i = generateIf(byteCodes, i, "<=");
608 int indx = i + readIntArg(byteCodes, i);
609 out.append("gt = " + indx).append("; continue;");
613 case opc_lookupswitch: {
614 int table = i / 4 * 4 + 4;
615 int dflt = i + readInt4(byteCodes, table);
617 int n = readInt4(byteCodes, table);
619 out.append("switch (s.pop()) {\n");
621 int cnstnt = readInt4(byteCodes, table);
623 int offset = i + readInt4(byteCodes, table);
625 out.append(" case " + cnstnt).append(": gt = " + offset).append("; continue;\n");
627 out.append(" default: gt = " + dflt).append("; continue;\n}");
631 case opc_tableswitch: {
632 int table = i / 4 * 4 + 4;
633 int dflt = i + readInt4(byteCodes, table);
635 int low = readInt4(byteCodes, table);
637 int high = readInt4(byteCodes, table);
639 out.append("switch (s.pop()) {\n");
640 while (low <= high) {
641 int offset = i + readInt4(byteCodes, table);
643 out.append(" case " + low).append(": gt = " + offset).append("; continue;\n");
646 out.append(" default: gt = " + dflt).append("; continue;\n}");
650 case opc_invokeinterface: {
651 i = invokeVirtualMethod(byteCodes, i) + 2;
654 case opc_invokevirtual:
655 i = invokeVirtualMethod(byteCodes, i);
657 case opc_invokespecial:
658 i = invokeStaticMethod(byteCodes, i, false);
660 case opc_invokestatic:
661 i = invokeStaticMethod(byteCodes, i, true);
664 int indx = readIntArg(byteCodes, i);
665 String ci = jc.getClassName(indx);
666 out.append("s.push(new ");
667 out.append(ci.replace('/','_'));
674 int type = byteCodes[i++];
675 out.append("s.push(new Array(s.pop()).fillNulls());");
678 case opc_anewarray: {
679 i += 2; // skip type of array
680 out.append("s.push(new Array(s.pop()).fillNulls());");
683 case opc_multianewarray: {
685 int dim = readByte(byteCodes, ++i);
686 out.append("{ var a0 = new Array(s.pop()).fillNulls();");
687 for (int d = 1; d < dim; d++) {
688 out.append("\n var l" + d).append(" = s.pop();");
689 out.append("\n for (var i" + d).append (" = 0; i" + d).
690 append(" < a" + (d - 1)).
691 append(".length; i" + d).append("++) {");
692 out.append("\n var a" + d).
693 append (" = new Array(l" + d).append(").fillNulls();");
694 out.append("\n a" + (d - 1)).append("[i" + d).append("] = a" + d).
697 for (int d = 1; d < dim; d++) {
700 out.append("\ns.push(a0); }");
703 case opc_arraylength:
704 out.append("s.push(s.pop().length);");
714 out.append("{ var value = s.pop(); var indx = s.pop(); s.pop()[indx] = value; }");
725 out.append("{ var indx = s.pop(); s.push(s.pop()[indx]); }");
729 out.append("s.pop();");
731 out.append("s.pop();");
734 out.append("s.push(s[s.length - 1]);");
737 out.append("{ var v1 = s.pop(); var v2 = s.pop(); s.push(v1); s.push(v2); s.push(v1); }");
740 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); }");
743 out.append("s.push(" + byteCodes[++i] + ");");
746 out.append("s.push(" + readIntArg(byteCodes, i) + ");");
750 int indx = readIntArg(byteCodes, i);
751 String[] fi = jc.getFieldInfoName(indx);
752 out.append("s.push(s.pop().fld_").
753 append(fi[1]).append(");");
757 case opc_getstatic: {
758 int indx = readIntArg(byteCodes, i);
759 String[] fi = jc.getFieldInfoName(indx);
760 out.append("s.push(").append(fi[0].replace('/', '_'));
761 out.append('.').append(fi[1]).append(");");
766 case opc_putstatic: {
767 int indx = readIntArg(byteCodes, i);
768 String[] fi = jc.getFieldInfoName(indx);
769 out.append(fi[0].replace('/', '_'));
770 out.append('.').append(fi[1]).append(" = s.pop();");
776 int indx = readIntArg(byteCodes, i);
777 String[] fi = jc.getFieldInfoName(indx);
778 out.append("{ var v = s.pop(); s.pop().fld_")
779 .append(fi[1]).append(" = v; }");
783 case opc_checkcast: {
784 int indx = readIntArg(byteCodes, i);
785 final String type = jc.getClassName(indx);
786 if (!type.startsWith("[")) {
787 // no way to check arrays right now
788 out.append("if(s[s.length - 1].$instOf_")
789 .append(type.replace('/', '_'))
790 .append(" != 1) throw {};"); // XXX proper exception
795 case opc_instanceof: {
796 int indx = readIntArg(byteCodes, i);
797 final String type = jc.getClassName(indx);
798 out.append("s.push(s.pop().$instOf_")
799 .append(type.replace('/', '_'))
800 .append(" ? 1 : 0);");
805 out.append("{ var t = s.pop(); s = new Array(1); s[0] = t; throw t; }");
809 out.append("throw 'unknown bytecode " + c + "';");
813 if (!current.isEmpty()) {
814 out.append("} catch (e) {");
815 for (TrapData e : current) {
816 if (e.catch_cpx != 0) { //not finally
817 final String classInternalName = jc.getClassName(e.catch_cpx);
818 addReference(classInternalName);
819 out.append("if (e.$instOf_"+classInternalName.replace('/', '_')+") {");
820 out.append("gt="+e.handler_pc+"; continue;");
826 out.append("throw e;");
830 for (int j = prev; j <= i; j++) {
832 final int cc = readByte(byteCodes, j);
833 out.append(Integer.toString(cc));
840 private int generateIf(byte[] byteCodes, int i, final String test) throws IOException {
841 int indx = i + readIntArg(byteCodes, i);
842 out.append("if (s.pop() ").append(test).append(" s.pop()) { gt = " + indx);
843 out.append("; continue; }");
847 private int readIntArg(byte[] byteCodes, int offsetInstruction) {
848 final int indxHi = byteCodes[offsetInstruction + 1] << 8;
849 final int indxLo = byteCodes[offsetInstruction + 2];
850 return (indxHi & 0xffffff00) | (indxLo & 0xff);
852 private int readInt4(byte[] byteCodes, int offsetInstruction) {
853 final int d = byteCodes[offsetInstruction + 0] << 24;
854 final int c = byteCodes[offsetInstruction + 1] << 16;
855 final int b = byteCodes[offsetInstruction + 2] << 8;
856 final int a = byteCodes[offsetInstruction + 3];
857 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
859 private int readByte(byte[] byteCodes, int offsetInstruction) {
860 return (byteCodes[offsetInstruction] + 256) % 256;
863 private static void countArgs(String descriptor, boolean[] hasReturnType, StringBuilder sig, StringBuilder cnt) {
865 Boolean count = null;
866 boolean array = false;
868 int firstPos = sig.length();
869 while (i < descriptor.length()) {
870 char ch = descriptor.charAt(i++);
891 if (ch == 'J' || ch == 'D') {
897 hasReturnType[0] = true;
898 sig.insert(firstPos, ch);
900 sig.insert(firstPos, "_3");
907 hasReturnType[0] = false;
908 sig.insert(firstPos, 'V');
911 int next = descriptor.indexOf(';', i);
912 String realSig = mangleSig(descriptor, i - 1, next + 1);
920 sig.insert(firstPos, realSig);
922 sig.insert(firstPos, "_3");
924 hasReturnType[0] = true;
932 throw new IllegalStateException("Invalid char: " + ch);
937 private static String mangleSig(String txt, int first, int last) {
938 StringBuilder sb = new StringBuilder();
939 for (int i = first; i < last; i++) {
940 final char ch = txt.charAt(i);
942 case '/': sb.append('_'); break;
943 case '_': sb.append("_1"); break;
944 case ';': sb.append("_2"); break;
945 case '[': sb.append("_3"); break;
946 default: sb.append(ch); break;
949 return sb.toString();
952 private static String findMethodName(MethodData m, StringBuilder cnt) {
953 StringBuilder name = new StringBuilder();
954 if ("<init>".equals(m.getName())) { // NOI18N
955 name.append("cons"); // NOI18N
956 } else if ("<clinit>".equals(m.getName())) { // NOI18N
957 name.append("class"); // NOI18N
959 name.append(m.getName());
962 boolean hasReturn[] = { false };
963 countArgs(m.getInternalSig(), hasReturn, name, cnt);
964 return name.toString();
967 static String findMethodName(String[] mi, StringBuilder cnt, boolean[] hasReturn) {
968 StringBuilder name = new StringBuilder();
969 String descr = mi[2];//mi.getDescriptor();
971 if ("<init>".equals(nm)) { // NOI18N
972 name.append("cons"); // NOI18N
976 countArgs(descr, hasReturn, name, cnt);
977 return name.toString();
980 private int invokeStaticMethod(byte[] byteCodes, int i, boolean isStatic)
982 int methodIndex = readIntArg(byteCodes, i);
983 String[] mi = jc.getFieldInfoName(methodIndex);
984 boolean[] hasReturn = { false };
985 StringBuilder cnt = new StringBuilder();
986 String mn = findMethodName(mi, cnt, hasReturn);
988 for (int j = cnt.length() - 1; j >= 0; j--) {
989 out.append("var v" + j).append(" = s.pop(); ");
993 out.append("s.push(");
995 final String in = mi[0];
996 out.append(in.replace('/', '_'));
997 out.append("(false).");
1002 out.append("s.pop()");
1005 for (int j = 0; j < cnt.length(); j++) {
1007 out.append("v" + j);
1019 private int invokeVirtualMethod(byte[] byteCodes, int i)
1020 throws IOException {
1021 int methodIndex = readIntArg(byteCodes, i);
1022 String[] mi = jc.getFieldInfoName(methodIndex);
1023 boolean[] hasReturn = { false };
1024 StringBuilder cnt = new StringBuilder();
1025 String mn = findMethodName(mi, cnt, hasReturn);
1027 for (int j = cnt.length() - 1; j >= 0; j--) {
1028 out.append("var v" + j).append(" = s.pop(); ");
1030 out.append("var self = s.pop(); ");
1032 out.append("s.push(");
1034 out.append("self.");
1038 for (int j = 0; j < cnt.length(); j++) {
1040 out.append("v" + j);
1051 private void addReference(String cn) throws IOException {
1052 if (requireReference(cn)) {
1053 out.append(" /* needs ").append(cn).append(" */");
1057 private void outType(String d, StringBuilder out) {
1059 while (d.charAt(0) == '[') {
1063 if (d.charAt(0) == 'L') {
1064 assert d.charAt(d.length() - 1) == ';';
1065 out.append(d.replace('/', '_').substring(0, d.length() - 1));
1071 private String encodeConstant(int entryIndex) throws IOException {
1072 String[] classRef = { null };
1073 String s = jc.stringValue(entryIndex, classRef);
1074 if (classRef[0] != null) {
1075 addReference(classRef[0]);
1080 private String javaScriptBody(String prefix, MethodData m, boolean isStatic) throws IOException {
1081 byte[] arr = m.findAnnotationData(true);
1085 final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
1086 class P extends AnnotationParser {
1092 String[] args = new String[30];
1096 protected void visitAttr(String type, String attr, String at, String value) {
1097 if (type.equals(jvmType)) {
1098 if ("body".equals(attr)) {
1100 } else if ("args".equals(attr)) {
1101 args[cnt++] = value;
1103 throw new IllegalArgumentException(attr);
1110 if (p.body == null) {
1113 StringBuilder cnt = new StringBuilder();
1114 final String mn = findMethodName(m, cnt);
1115 out.append(prefix).append(mn);
1116 out.append(" = function(");
1120 out.append(p.args[0]);
1127 for (int i = 0; i < cnt.length(); i++) {
1129 out.append(p.args[index]);
1133 out.append(") {").append("\n");
1135 out.append("\n}\n");
1138 private static String className(ClassData jc) {
1139 //return jc.getName().getInternalName().replace('/', '_');
1140 return jc.getClassName().replace('/', '_');
1143 private static String[] findAnnotation(
1144 byte[] arr, ClassData cd, final String className,
1145 final String... attrNames
1146 ) throws IOException {
1150 final String[] values = new String[attrNames.length];
1151 final boolean[] found = { false };
1152 final String jvmType = "L" + className.replace('.', '/') + ";";
1153 AnnotationParser ap = new AnnotationParser(false) {
1155 protected void visitAttr(String type, String attr, String at, String value) {
1156 if (type.equals(jvmType)) {
1158 for (int i = 0; i < attrNames.length; i++) {
1159 if (attrNames[i].equals(attr)) {
1168 return found[0] ? values : null;
1171 private CharSequence initField(FieldData v) {
1172 final String is = v.getInternalSig();
1173 if (is.length() == 1) {
1174 switch (is.charAt(0)) {
1180 case 'I': return " = 0;";
1182 case 'D': return " = 0.0;";
1184 throw new IllegalStateException(is);
1190 private static void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
1191 AnnotationParser ap = new AnnotationParser(true) {
1196 protected void visitAnnotationStart(String type) throws IOException {
1200 out.append('"').append(type).append("\" : {\n");
1205 protected void visitAnnotationEnd(String type) throws IOException {
1206 out.append("\n}\n");
1210 protected void visitAttr(String type, String attr, String attrType, String value)
1211 throws IOException {
1218 out.append(attr).append("__").append(attrType).append(" : ").append(value);