launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoader.java
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.bck2brwsr.launcher.fximpl;
20 import java.io.IOException;
21 import java.io.InputStream;
23 import java.util.ArrayList;
24 import java.util.Enumeration;
25 import java.util.List;
26 import org.objectweb.asm.AnnotationVisitor;
27 import org.objectweb.asm.ClassReader;
28 import org.objectweb.asm.ClassVisitor;
29 import org.objectweb.asm.ClassWriter;
30 import org.objectweb.asm.Label;
31 import org.objectweb.asm.MethodVisitor;
32 import org.objectweb.asm.Opcodes;
33 import org.objectweb.asm.Type;
34 import org.objectweb.asm.signature.SignatureReader;
35 import org.objectweb.asm.signature.SignatureVisitor;
39 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
41 abstract class JsClassLoader extends ClassLoader {
42 JsClassLoader(ClassLoader parent) {
47 protected abstract URL findResource(String name);
50 protected abstract Enumeration<URL> findResources(String name);
53 protected Class<?> findClass(String name) throws ClassNotFoundException {
54 URL u = findResource(name.replace('.', '/') + ".class");
56 InputStream is = null;
59 byte[] arr = new byte[is.available()];
61 while (len < arr.length) {
62 int read = is.read(arr, len, arr.length - len);
64 throw new IOException("Can't read " + u);
70 ClassReader cr = new ClassReader(arr);
71 FindInClass tst = new FindInClass(null);
74 ClassWriter w = new ClassWriterEx(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
75 FindInClass fic = new FindInClass(w);
77 arr = w.toByteArray();
80 return defineClass(name, arr, 0, arr.length);
82 } catch (IOException ex) {
83 throw new ClassNotFoundException("Can't load " + name, ex);
86 if (is != null) is.close();
87 } catch (IOException ex) {
88 throw new ClassNotFoundException(null, ex);
92 if (name.startsWith("org.apidesign.bck2brwsr.launcher.fximpl.Fn")) {
93 return Class.forName(name);
96 return super.findClass(name);
99 protected abstract Fn defineFn(String code, String... names);
102 private static final class FindInClass extends ClassVisitor {
104 private boolean found;
106 public FindInClass(ClassVisitor cv) {
107 super(Opcodes.ASM4, cv);
111 public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
113 super.visit(version, access, name, signature, superName, interfaces);
119 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
120 return new FindInMethod(name, desc,
121 super.visitMethod(access & (~Opcodes.ACC_NATIVE), name, desc, signature, exceptions)
125 private final class FindInMethod extends MethodVisitor {
126 private final String name;
127 private final String desc;
128 private List<String> args;
130 private boolean bodyGenerated;
132 public FindInMethod(String name, String desc, MethodVisitor mv) {
133 super(Opcodes.ASM4, mv);
139 public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
140 if ("Lorg/apidesign/bck2brwsr/core/JavaScriptBody;".equals(desc)) { // NOI18N
142 return new FindInAnno();
144 return super.visitAnnotation(desc, visible);
147 private void generateJSBody(List<String> args, String body) {
153 public void visitCode() {
160 private boolean generateBody() {
164 bodyGenerated = true;
166 super.visitFieldInsn(
167 Opcodes.GETSTATIC, FindInClass.this.name,
168 "$$bck2brwsr$$" + name,
169 "Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;"
171 super.visitInsn(Opcodes.DUP);
172 Label ifNotNull = new Label();
173 super.visitJumpInsn(Opcodes.IFNONNULL, ifNotNull);
176 super.visitInsn(Opcodes.POP);
177 super.visitLdcInsn(Type.getObjectType(FindInClass.this.name));
178 super.visitLdcInsn(body);
179 super.visitIntInsn(Opcodes.SIPUSH, args.size());
180 super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
181 for (int i = 0; i < args.size(); i++) {
182 String name = args.get(i);
183 super.visitInsn(Opcodes.DUP);
184 super.visitIntInsn(Opcodes.BIPUSH, i);
185 super.visitLdcInsn(name);
186 super.visitInsn(Opcodes.AASTORE);
188 super.visitMethodInsn(Opcodes.INVOKESTATIC,
189 "org/apidesign/bck2brwsr/launcher/fximpl/Fn", "define",
190 "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/String;)Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;"
194 super.visitLabel(ifNotNull);
195 super.visitIntInsn(Opcodes.SIPUSH, args.size());
196 super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
198 class SV extends SignatureVisitor {
199 private boolean nowReturn;
200 private Type returnType;
208 public void visitBaseType(char descriptor) {
209 final Type t = Type.getType("" + descriptor);
214 FindInMethod.super.visitInsn(Opcodes.DUP);
215 FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index);
216 FindInMethod.super.visitVarInsn(t.getOpcode(Opcodes.ILOAD), index);
218 switch (descriptor) {
219 case 'I': factory = "java/lang/Integer"; break;
220 case 'J': factory = "java/lang/Long"; break;
221 case 'S': factory = "java/lang/Short"; break;
222 case 'F': factory = "java/lang/Float"; break;
223 case 'D': factory = "java/lang/Double"; break;
224 case 'Z': factory = "java/lang/Boolean"; break;
225 case 'C': factory = "java/lang/Character"; break;
226 case 'B': factory = "java/lang/Byte"; break;
227 default: throw new IllegalStateException(t.toString());
229 FindInMethod.super.visitMethodInsn(Opcodes.INVOKESTATIC,
230 factory, "valueOf", "(" + descriptor + ")L" + factory + ";"
232 FindInMethod.super.visitInsn(Opcodes.AASTORE);
237 public void visitClassType(String name) {
239 returnType = Type.getObjectType(name);
242 FindInMethod.super.visitInsn(Opcodes.DUP);
243 FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index);
244 FindInMethod.super.visitVarInsn(Opcodes.ALOAD, index);
245 FindInMethod.super.visitInsn(Opcodes.AASTORE);
250 public SignatureVisitor visitReturnType() {
258 SignatureReader sr = new SignatureReader(desc);
261 super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
262 "org/apidesign/bck2brwsr/launcher/fximpl/Fn", "invoke", "([Ljava/lang/Object;)Ljava/lang/Object;"
264 switch (sv.returnType.getSort()) {
266 super.visitInsn(Opcodes.RETURN);
270 super.visitTypeInsn(Opcodes.CHECKCAST, sv.returnType.getInternalName());
271 super.visitInsn(Opcodes.ARETURN);
274 super.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Number");
275 super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
276 "java/lang/Number", sv.returnType.getClassName() + "Value", "()" + sv.returnType.getDescriptor()
278 super.visitInsn(sv.returnType.getOpcode(Opcodes.IRETURN));
284 public void visitEnd() {
287 if (generateBody()) {
289 super.visitMaxs(1, 0);
291 FindInClass.this.visitField(
292 Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
293 "$$bck2brwsr$$" + name,
294 "Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;",
304 private final class FindInAnno extends AnnotationVisitor {
305 private List<String> args = new ArrayList<String>();
308 public FindInAnno() {
313 public void visit(String name, Object value) {
315 args.add((String) value);
318 assert name.equals("body");
319 body = (String) value;
323 public AnnotationVisitor visitArray(String name) {
328 public void visitEnd() {
330 generateJSBody(args, body);
337 private class ClassWriterEx extends ClassWriter {
339 public ClassWriterEx(ClassReader classReader, int flags) {
340 super(classReader, flags);
344 protected String getCommonSuperClass(final String type1, final String type2) {
346 ClassLoader classLoader = JsClassLoader.this;
348 c = Class.forName(type1.replace('/', '.'), false, classLoader);
349 d = Class.forName(type2.replace('/', '.'), false, classLoader);
350 } catch (Exception e) {
351 throw new RuntimeException(e.toString());
353 if (c.isAssignableFrom(d)) {
356 if (d.isAssignableFrom(c)) {
359 if (c.isInterface() || d.isInterface()) {
360 return "java/lang/Object";
363 c = c.getSuperclass();
364 } while (!c.isAssignableFrom(d));
365 return c.getName().replace('.', '/');