Merging lazyvm into default branch - it seems to be stable enough to be used.
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.bck2brwsr.core.JavaScriptBody;
23 import org.apidesign.javap.AnnotationParser;
24 import org.apidesign.javap.ClassData;
25 import org.apidesign.javap.FieldData;
26 import org.apidesign.javap.MethodData;
27 import static org.apidesign.javap.RuntimeConstants.*;
29 /** Translator of the code inside class files to JavaScript.
31 * @author Jaroslav Tulach <jtulach@netbeans.org>
33 abstract class ByteCodeToJavaScript {
37 protected ByteCodeToJavaScript(Appendable out) {
41 /* Collects additional required resources.
43 * @param internalClassName classes that were referenced and should be loaded in order the
44 * generated JavaScript code works properly. The names are in internal
45 * JVM form so String is <code>java/lang/String</code>.
47 protected abstract boolean requireReference(String internalClassName);
50 * @param resourcePath name of resources to read
52 protected abstract void requireScript(String resourcePath);
54 /** Allows subclasses to redefine what field a function representing a
55 * class gets assigned. By default it returns the suggested name followed
56 * by <code>" = "</code>;
58 * @param className suggested name of the class
60 /* protected */ String assignClass(String className) {
61 return className + " = ";
63 /* protected */ String accessClass(String classOperation) {
64 return classOperation;
68 * Converts a given class file to a JavaScript version.
70 * @param classFile input stream with code of the .class file
71 * @return the initialization code for this class, if any. Otherwise <code>null</code>
73 * @throws IOException if something goes wrong during read or write or translating
76 public String compile(InputStream classFile) throws IOException {
77 this.jc = new ClassData(classFile);
78 byte[] arrData = jc.findAnnotationData(true);
79 String[] arr = findAnnotation(arrData, jc,
80 "org.apidesign.bck2brwsr.core.ExtraJavaScript",
81 "resource", "processByteCode"
84 requireScript(arr[0]);
85 if ("0".equals(arr[1])) {
89 String[] proto = findAnnotation(arrData, jc,
90 "org.apidesign.bck2brwsr.core.JavaScriptPrototype",
91 "container", "prototype"
93 StringArray toInitilize = new StringArray();
94 final String className = className(jc);
95 out.append("\n\n").append(assignClass(className));
96 out.append("function CLS() {");
97 out.append("\n if (!CLS.prototype.$instOf_").append(className).append(") {");
98 for (FieldData v : jc.getFields()) {
100 out.append("\n CLS.").append(v.getName()).append(initField(v));
104 String sc = jc.getSuperClassName(); // with _
105 out.append("\n var pp = ").
106 append(accessClass(sc.replace('/', '_'))).append("(true);");
107 out.append("\n var p = CLS.prototype = pp;");
108 out.append("\n var c = p;");
109 out.append("\n var sprcls = pp.constructor.$class;");
111 out.append("\n var p = CLS.prototype = ").append(proto[1]).append(";");
112 out.append("\n var c = ").append(proto[0]).append(";");
113 out.append("\n var sprcls = null;");
115 for (MethodData m : jc.getMethods()) {
116 byte[] onlyArr = m.findAnnotationData(true);
117 String[] only = findAnnotation(onlyArr, jc,
118 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
122 if (only[0] != null && only[1] != null) {
123 out.append("\n p.").append(only[0]).append(" = ")
124 .append(only[1]).append(";");
130 mn = generateStaticMethod("\n c.", m, toInitilize);
132 mn = generateInstanceMethod("\n c.", m);
134 byte[] runAnno = m.findAnnotationData(false);
135 if (runAnno != null) {
136 out.append("\n c.").append(mn).append(".anno = {");
137 generateAnno(jc, out, runAnno);
141 out.append("\n c.constructor = CLS;");
142 out.append("\n c.$instOf_").append(className).append(" = true;");
143 for (String superInterface : jc.getSuperInterfaces()) {
144 out.append("\n c.$instOf_").append(superInterface.replace('/', '_')).append(" = true;");
146 out.append("\n CLS.$class = ");
147 out.append(accessClass("java_lang_Class(true);"));
148 out.append("\n CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
149 out.append("\n CLS.$class.superclass = sprcls;");
150 out.append("\n CLS.$class.cnstr = CLS;");
151 byte[] classAnno = jc.findAnnotationData(false);
152 if (classAnno != null) {
153 out.append("\n CLS.$class.anno = {");
154 generateAnno(jc, out, classAnno);
158 out.append("\n if (arguments.length === 0) {");
159 out.append("\n if (!(this instanceof CLS)) {");
160 out.append("\n return new CLS();");
162 for (FieldData v : jc.getFields()) {
163 byte[] onlyArr = v.findAnnotationData(true);
164 String[] only = findAnnotation(onlyArr, jc,
165 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
169 if (only[0] != null && only[1] != null) {
170 out.append("\n p.").append(only[0]).append(" = ")
171 .append(only[1]).append(";");
176 out.append("\n this.fld_").
177 append(v.getName()).append(initField(v));
180 out.append("\n return this;");
182 out.append("\n return arguments[0] ? new CLS() : CLS.prototype;");
184 StringBuilder sb = new StringBuilder();
185 for (String init : toInitilize.toArray()) {
186 sb.append("\n").append(init).append("();");
188 return sb.toString();
190 private String generateStaticMethod(String prefix, MethodData m, StringArray toInitilize) throws IOException {
191 String jsb = javaScriptBody(prefix, m, true);
195 StringBuilder argsCnt = new StringBuilder();
196 final String mn = findMethodName(m, argsCnt);
197 out.append(prefix).append(mn).append(" = function");
198 if (mn.equals("class__V")) {
199 toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
203 for (int index = 0, i = 0; i < argsCnt.length(); i++) {
205 out.append("arg").append(String.valueOf(index));
207 final String desc = null;// XXX findDescriptor(args.get(i).getDescriptor());
208 if (argsCnt.charAt(i) == '1') {
214 out.append(") {").append("\n");
215 final byte[] code = m.getCode();
217 int len = m.getMaxLocals();
218 for (int index = argsCnt.length(), i = argsCnt.length(); i < len; i++) {
220 out.append("arg").append(String.valueOf(i)).append(";\n");
222 out.append(" var s = new Array();\n");
225 out.append(" throw 'no code found for ").append(m.getInternalSig()).append("';\n");
231 private String generateInstanceMethod(String prefix, MethodData m) throws IOException {
232 String jsb = javaScriptBody(prefix, m, false);
236 StringBuilder argsCnt = new StringBuilder();
237 final String mn = findMethodName(m, argsCnt);
238 out.append(prefix).append(mn).append(" = function");
241 for (int index = 1, i = 0; i < argsCnt.length(); i++) {
243 out.append("arg").append(String.valueOf(index));
244 if (argsCnt.charAt(i) == '1') {
250 out.append(") {").append("\n");
251 final byte[] code = m.getCode();
253 int len = m.getMaxLocals();
254 for (int index = argsCnt.length(), i = argsCnt.length(); i < len; i++) {
256 out.append("arg").append(String.valueOf(i + 1)).append(";\n");
258 out.append(";\n var s = new Array();\n");
261 out.append(" throw 'no code found for ").append(m.getInternalSig()).append("';\n");
267 private void produceCode(byte[] byteCodes) throws IOException {
268 out.append("\n var gt = 0;\n for(;;) switch(gt) {\n");
269 for (int i = 0; i < byteCodes.length; i++) {
271 out.append(" case " + i).append(": ");
272 final int c = readByte(byteCodes, i);
279 out.append("s.push(arg0);");
286 out.append("s.push(arg1);");
293 out.append("s.push(arg2);");
300 out.append("s.push(arg3);");
307 final int indx = readByte(byteCodes, ++i);
308 out.append("s.push(arg").append(indx + ");");
316 final int indx = readByte(byteCodes, ++i);
317 out.append("arg" + indx).append(" = s.pop();");
325 out.append("arg0 = s.pop();");
332 out.append("arg1 = s.pop();");
339 out.append("arg2 = s.pop();");
346 out.append("arg3 = s.pop();");
352 out.append("s.push(s.pop() + s.pop());");
358 out.append("{ var tmp = s.pop(); s.push(s.pop() - tmp); }");
364 out.append("s.push(s.pop() * s.pop());");
368 out.append("{ var tmp = s.pop(); s.push(Math.floor(s.pop() / tmp)); }");
372 out.append("{ var tmp = s.pop(); s.push(s.pop() / tmp); }");
378 out.append("{ var d = s.pop(); s.push(s.pop() % d); }");
382 out.append("s.push(s.pop() & s.pop());");
386 out.append("s.push(s.pop() | s.pop());");
390 out.append("s.push(s.pop() ^ s.pop());");
396 out.append("s.push(- s.pop());");
400 out.append("{ var v = s.pop(); s.push(s.pop() << v); }");
404 out.append("{ var v = s.pop(); s.push(s.pop() >> v); }");
408 out.append("{ var v = s.pop(); s.push(s.pop() >>> v); }");
411 final int varIndx = readByte(byteCodes, ++i);
412 final int incrBy = byteCodes[++i];
414 out.append("arg" + varIndx).append("++;");
416 out.append("arg" + varIndx).append(" += " + incrBy).append(";");
421 out.append("return;");
428 out.append("return s.pop();");
439 out.append("/* number conversion */");
445 out.append("s.push(Math.floor(s.pop()));");
450 out.append("/* number conversion */");
452 case opc_aconst_null:
453 out.append("s.push(null);");
456 out.append("s.push(-1);");
462 out.append("s.push(0);");
468 out.append("s.push(1);");
472 out.append("s.push(2);");
475 out.append("s.push(3);");
478 out.append("s.push(4);");
481 out.append("s.push(5);");
484 int indx = readByte(byteCodes, ++i);
485 String v = encodeConstant(indx);
486 out.append("s.push(").append(v).append(");");
491 int indx = readIntArg(byteCodes, i);
493 String v = encodeConstant(indx);
494 out.append("s.push(").append(v).append(");");
502 out.append("{ var delta = s.pop() - s.pop(); s.push(delta < 0 ?-1 : (delta == 0 ? 0 : 1)); }");
506 i = generateIf(byteCodes, i, "===");
509 i = generateIf(byteCodes, i, "!=");
511 case opc_if_icmpeq: {
512 i = generateIf(byteCodes, i, "==");
516 int indx = i + readIntArg(byteCodes, i);
517 out.append("if (s.pop() == 0) { gt = " + indx);
518 out.append("; continue; }");
523 int indx = i + readIntArg(byteCodes, i);
524 out.append("if (s.pop() != 0) { gt = " + indx);
525 out.append("; continue; }");
530 int indx = i + readIntArg(byteCodes, i);
531 out.append("if (s.pop() < 0) { gt = " + indx);
532 out.append("; continue; }");
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; }");
557 case opc_ifnonnull: {
558 int indx = i + readIntArg(byteCodes, i);
559 out.append("if (s.pop() !== null) { gt = " + indx);
560 out.append("; continue; }");
565 int indx = i + readIntArg(byteCodes, i);
566 out.append("if (s.pop() === null) { gt = " + indx);
567 out.append("; continue; }");
572 i = generateIf(byteCodes, i, "!=");
575 i = generateIf(byteCodes, i, ">");
578 i = generateIf(byteCodes, i, ">=");
581 i = generateIf(byteCodes, i, "<");
584 i = generateIf(byteCodes, i, "<=");
587 int indx = i + readIntArg(byteCodes, i);
588 out.append("gt = " + indx).append("; continue;");
592 case opc_lookupswitch: {
593 int table = i / 4 * 4 + 4;
594 int dflt = i + readInt4(byteCodes, table);
596 int n = readInt4(byteCodes, table);
598 out.append("switch (s.pop()) {\n");
600 int cnstnt = readInt4(byteCodes, table);
602 int offset = i + readInt4(byteCodes, table);
604 out.append(" case " + cnstnt).append(": gt = " + offset).append("; continue;\n");
606 out.append(" default: gt = " + dflt).append("; continue;\n}");
610 case opc_tableswitch: {
611 int table = i / 4 * 4 + 4;
612 int dflt = i + readInt4(byteCodes, table);
614 int low = readInt4(byteCodes, table);
616 int high = readInt4(byteCodes, table);
618 out.append("switch (s.pop()) {\n");
619 while (low <= high) {
620 int offset = i + readInt4(byteCodes, table);
622 out.append(" case " + low).append(": gt = " + offset).append("; continue;\n");
625 out.append(" default: gt = " + dflt).append("; continue;\n}");
629 case opc_invokeinterface: {
630 i = invokeVirtualMethod(byteCodes, i) + 2;
633 case opc_invokevirtual:
634 i = invokeVirtualMethod(byteCodes, i);
636 case opc_invokespecial:
637 i = invokeStaticMethod(byteCodes, i, false);
639 case opc_invokestatic:
640 i = invokeStaticMethod(byteCodes, i, true);
643 int indx = readIntArg(byteCodes, i);
644 String ci = jc.getClassName(indx);
645 out.append("s.push(new ");
646 out.append(accessClass(ci.replace('/','_')));
653 int type = byteCodes[i++];
654 out.append("s.push(new Array(s.pop()).fillNulls());");
657 case opc_anewarray: {
658 i += 2; // skip type of array
659 out.append("s.push(new Array(s.pop()).fillNulls());");
662 case opc_multianewarray: {
664 int dim = readByte(byteCodes, ++i);
665 out.append("{ var a0 = new Array(s.pop()).fillNulls();");
666 for (int d = 1; d < dim; d++) {
667 out.append("\n var l" + d).append(" = s.pop();");
668 out.append("\n for (var i" + d).append (" = 0; i" + d).
669 append(" < a" + (d - 1)).
670 append(".length; i" + d).append("++) {");
671 out.append("\n var a" + d).
672 append (" = new Array(l" + d).append(").fillNulls();");
673 out.append("\n a" + (d - 1)).append("[i" + d).append("] = a" + d).
676 for (int d = 1; d < dim; d++) {
679 out.append("\ns.push(a0); }");
682 case opc_arraylength:
683 out.append("s.push(s.pop().length);");
693 out.append("{ var value = s.pop(); var indx = s.pop(); s.pop()[indx] = value; }");
704 out.append("{ var indx = s.pop(); s.push(s.pop()[indx]); }");
708 out.append("s.pop();");
710 out.append("s.pop();");
713 out.append("s.push(s[s.length - 1]);");
716 out.append("{ var v1 = s.pop(); var v2 = s.pop(); s.push(v1); s.push(v2); s.push(v1); }");
719 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); }");
722 out.append("s.push(" + byteCodes[++i] + ");");
725 out.append("s.push(" + readIntArg(byteCodes, i) + ");");
729 int indx = readIntArg(byteCodes, i);
730 String[] fi = jc.getFieldInfoName(indx);
731 out.append("s.push(s.pop().fld_").
732 append(fi[1]).append(");");
736 case opc_getstatic: {
737 int indx = readIntArg(byteCodes, i);
738 String[] fi = jc.getFieldInfoName(indx);
739 out.append("s.push(").append(accessClass(fi[0].replace('/', '_')));
740 out.append('.').append(fi[1]).append(");");
745 case opc_putstatic: {
746 int indx = readIntArg(byteCodes, i);
747 String[] fi = jc.getFieldInfoName(indx);
748 out.append(accessClass(fi[0].replace('/', '_')));
749 out.append('.').append(fi[1]).append(" = s.pop();");
755 int indx = readIntArg(byteCodes, i);
756 String[] fi = jc.getFieldInfoName(indx);
757 out.append("{ var v = s.pop(); s.pop().fld_")
758 .append(fi[1]).append(" = v; }");
762 case opc_checkcast: {
763 int indx = readIntArg(byteCodes, i);
764 final String type = jc.getClassName(indx);
765 if (!type.startsWith("[")) {
766 // no way to check arrays right now
767 out.append("if(s[s.length - 1] !== null && !s[s.length - 1].$instOf_")
768 .append(type.replace('/', '_'))
769 .append(") throw {};"); // XXX proper exception
774 case opc_instanceof: {
775 int indx = readIntArg(byteCodes, i);
776 final String type = jc.getClassName(indx);
777 out.append("s.push(s.pop().$instOf_")
778 .append(type.replace('/', '_'))
779 .append(" ? 1 : 0);");
784 out.append("{ var t = s.pop(); s = new Array(1); s[0] = t; throw t; }");
788 out.append("throw 'unknown bytecode " + c + "';");
793 for (int j = prev; j <= i; j++) {
795 final int cc = readByte(byteCodes, j);
796 out.append(Integer.toString(cc));
803 private int generateIf(byte[] byteCodes, int i, final String test) throws IOException {
804 int indx = i + readIntArg(byteCodes, i);
805 out.append("if (s.pop() ").append(test).append(" s.pop()) { gt = " + indx);
806 out.append("; continue; }");
810 private int readIntArg(byte[] byteCodes, int offsetInstruction) {
811 final int indxHi = byteCodes[offsetInstruction + 1] << 8;
812 final int indxLo = byteCodes[offsetInstruction + 2];
813 return (indxHi & 0xffffff00) | (indxLo & 0xff);
815 private int readInt4(byte[] byteCodes, int offsetInstruction) {
816 final int d = byteCodes[offsetInstruction + 0] << 24;
817 final int c = byteCodes[offsetInstruction + 1] << 16;
818 final int b = byteCodes[offsetInstruction + 2] << 8;
819 final int a = byteCodes[offsetInstruction + 3];
820 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
822 private int readByte(byte[] byteCodes, int offsetInstruction) {
823 return (byteCodes[offsetInstruction] + 256) % 256;
826 private static void countArgs(String descriptor, boolean[] hasReturnType, StringBuilder sig, StringBuilder cnt) {
828 Boolean count = null;
829 boolean array = false;
831 int firstPos = sig.length();
832 while (i < descriptor.length()) {
833 char ch = descriptor.charAt(i++);
854 if (ch == 'J' || ch == 'D') {
860 hasReturnType[0] = true;
861 sig.insert(firstPos, ch);
863 sig.insert(firstPos, "_3");
870 hasReturnType[0] = false;
871 sig.insert(firstPos, 'V');
874 int next = descriptor.indexOf(';', i);
875 String realSig = mangleSig(descriptor, i - 1, next + 1);
883 sig.insert(firstPos, realSig);
885 sig.insert(firstPos, "_3");
887 hasReturnType[0] = true;
895 throw new IllegalStateException("Invalid char: " + ch);
900 private static String mangleSig(String txt, int first, int last) {
901 StringBuilder sb = new StringBuilder();
902 for (int i = first; i < last; i++) {
903 final char ch = txt.charAt(i);
905 case '/': sb.append('_'); break;
906 case '_': sb.append("_1"); break;
907 case ';': sb.append("_2"); break;
908 case '[': sb.append("_3"); break;
909 default: sb.append(ch); break;
912 return sb.toString();
915 private static String findMethodName(MethodData m, StringBuilder cnt) {
916 StringBuilder name = new StringBuilder();
917 if ("<init>".equals(m.getName())) { // NOI18N
918 name.append("cons"); // NOI18N
919 } else if ("<clinit>".equals(m.getName())) { // NOI18N
920 name.append("class"); // NOI18N
922 name.append(m.getName());
925 boolean hasReturn[] = { false };
926 countArgs(m.getInternalSig(), hasReturn, name, cnt);
927 return name.toString();
930 static String findMethodName(String[] mi, StringBuilder cnt, boolean[] hasReturn) {
931 StringBuilder name = new StringBuilder();
932 String descr = mi[2];//mi.getDescriptor();
934 if ("<init>".equals(nm)) { // NOI18N
935 name.append("cons"); // NOI18N
939 countArgs(descr, hasReturn, name, cnt);
940 return name.toString();
943 private int invokeStaticMethod(byte[] byteCodes, int i, boolean isStatic)
945 int methodIndex = readIntArg(byteCodes, i);
946 String[] mi = jc.getFieldInfoName(methodIndex);
947 boolean[] hasReturn = { false };
948 StringBuilder cnt = new StringBuilder();
949 String mn = findMethodName(mi, cnt, hasReturn);
951 for (int j = cnt.length() - 1; j >= 0; j--) {
952 out.append("var v" + j).append(" = s.pop(); ");
956 out.append("s.push(");
958 final String in = mi[0];
959 out.append(accessClass(in.replace('/', '_')));
960 out.append("(false).");
965 out.append("s.pop()");
968 for (int j = 0; j < cnt.length(); j++) {
982 private int invokeVirtualMethod(byte[] byteCodes, int i)
984 int methodIndex = readIntArg(byteCodes, i);
985 String[] mi = jc.getFieldInfoName(methodIndex);
986 boolean[] hasReturn = { false };
987 StringBuilder cnt = new StringBuilder();
988 String mn = findMethodName(mi, cnt, hasReturn);
990 for (int j = cnt.length() - 1; j >= 0; j--) {
991 out.append("var v" + j).append(" = s.pop(); ");
993 out.append("var self = s.pop(); ");
995 out.append("s.push(");
1001 for (int j = 0; j < cnt.length(); j++) {
1003 out.append("v" + j);
1014 private void addReference(String cn) throws IOException {
1015 if (requireReference(cn)) {
1016 out.append(" /* needs ").append(cn).append(" */");
1020 private void outType(String d, StringBuilder out) {
1022 while (d.charAt(0) == '[') {
1026 if (d.charAt(0) == 'L') {
1027 assert d.charAt(d.length() - 1) == ';';
1028 out.append(d.replace('/', '_').substring(0, d.length() - 1));
1034 private String encodeConstant(int entryIndex) throws IOException {
1035 String[] classRef = { null };
1036 String s = jc.stringValue(entryIndex, classRef);
1037 if (classRef[0] != null) {
1038 addReference(classRef[0]);
1039 s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
1044 private String javaScriptBody(String prefix, MethodData m, boolean isStatic) throws IOException {
1045 byte[] arr = m.findAnnotationData(true);
1049 final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
1050 class P extends AnnotationParser {
1056 String[] args = new String[30];
1060 protected void visitAttr(String type, String attr, String at, String value) {
1061 if (type.equals(jvmType)) {
1062 if ("body".equals(attr)) {
1064 } else if ("args".equals(attr)) {
1065 args[cnt++] = value;
1067 throw new IllegalArgumentException(attr);
1074 if (p.body == null) {
1077 StringBuilder cnt = new StringBuilder();
1078 final String mn = findMethodName(m, cnt);
1079 out.append(prefix).append(mn);
1080 out.append(" = function(");
1084 out.append(p.args[0]);
1091 for (int i = 0; i < cnt.length(); i++) {
1093 out.append(p.args[index]);
1097 out.append(") {").append("\n");
1099 out.append("\n}\n");
1102 private static String className(ClassData jc) {
1103 //return jc.getName().getInternalName().replace('/', '_');
1104 return jc.getClassName().replace('/', '_');
1107 private static String[] findAnnotation(
1108 byte[] arr, ClassData cd, final String className,
1109 final String... attrNames
1110 ) throws IOException {
1114 final String[] values = new String[attrNames.length];
1115 final boolean[] found = { false };
1116 final String jvmType = "L" + className.replace('.', '/') + ";";
1117 AnnotationParser ap = new AnnotationParser(false) {
1119 protected void visitAttr(String type, String attr, String at, String value) {
1120 if (type.equals(jvmType)) {
1122 for (int i = 0; i < attrNames.length; i++) {
1123 if (attrNames[i].equals(attr)) {
1132 return found[0] ? values : null;
1135 private CharSequence initField(FieldData v) {
1136 final String is = v.getInternalSig();
1137 if (is.length() == 1) {
1138 switch (is.charAt(0)) {
1144 case 'I': return " = 0;";
1146 case 'D': return " = 0.0;";
1148 throw new IllegalStateException(is);
1154 private static void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
1155 AnnotationParser ap = new AnnotationParser(true) {
1160 protected void visitAnnotationStart(String type) throws IOException {
1164 out.append('"').append(type).append("\" : {\n");
1169 protected void visitAnnotationEnd(String type) throws IOException {
1170 out.append("\n}\n");
1174 protected void visitAttr(String type, String attr, String attrType, String value)
1175 throws IOException {
1182 out.append(attr).append("__").append(attrType).append(" : ").append(value);