launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoader.java
Separating the Fn class outside of the classloader and moving methods the generated code deals with into it
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.net.URLClassLoader;
24 import java.util.ArrayList;
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;
37 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
39 abstract class JsClassLoader extends URLClassLoader {
40 JsClassLoader(URL[] urls, ClassLoader parent) {
45 protected Class<?> findClass(String name) throws ClassNotFoundException {
46 URL u = findResource(name.replace('.', '/') + ".class");
48 InputStream is = null;
51 ClassReader cr = new ClassReader(is);
52 ClassWriter w = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
53 FindInClass fic = new FindInClass(w);
55 byte[] arr = w.toByteArray();
56 return defineClass(name, arr, 0, arr.length);
57 } catch (IOException ex) {
58 throw new ClassNotFoundException("Can't load " + name, ex);
61 if (is != null) is.close();
62 } catch (IOException ex) {
63 throw new ClassNotFoundException(null, ex);
67 if (name.startsWith("org.apidesign.bck2brwsr.launcher.fximpl.Fn")) {
68 return Class.forName(name);
71 return super.findClass(name);
74 protected abstract Fn defineFn(String code, String... names);
77 private static final class FindInClass extends ClassVisitor {
80 public FindInClass(ClassVisitor cv) {
81 super(Opcodes.ASM4, cv);
85 public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
87 super.visit(version, access, name, signature, superName, interfaces);
93 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
94 return new FindInMethod(name,
95 super.visitMethod(access, name, desc, signature, exceptions)
99 private final class FindInMethod extends MethodVisitor {
100 private final String name;
101 private List<String> args;
104 public FindInMethod(String name, MethodVisitor mv) {
105 super(Opcodes.ASM4, mv);
110 public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
111 if ("Lorg/apidesign/bck2brwsr/core/JavaScriptBody;".equals(desc)) { // NOI18N
112 return new FindInAnno();
114 return super.visitAnnotation(desc, visible);
117 private void generateJSBody(List<String> args, String body) {
123 public void visitCode() {
128 super.visitFieldInsn(
129 Opcodes.GETSTATIC, FindInClass.this.name,
130 "$$bck2brwsr$$" + name,
131 "Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;"
133 super.visitInsn(Opcodes.DUP);
134 Label ifNotNull = new Label();
135 super.visitJumpInsn(Opcodes.IFNONNULL, ifNotNull);
138 super.visitInsn(Opcodes.POP);
139 super.visitLdcInsn(Type.getObjectType(FindInClass.this.name));
140 super.visitLdcInsn(body);
141 super.visitIntInsn(Opcodes.SIPUSH, args.size());
142 super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
143 for (int i = 0; i < args.size(); i++) {
144 String name = args.get(i);
145 super.visitInsn(Opcodes.DUP);
146 super.visitIntInsn(Opcodes.BIPUSH, i);
147 super.visitLdcInsn(name);
148 super.visitInsn(Opcodes.AASTORE);
150 super.visitMethodInsn(Opcodes.INVOKESTATIC,
151 "org/apidesign/bck2brwsr/launcher/fximpl/Fn", "define",
152 "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/String;)Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;"
156 super.visitLabel(ifNotNull);
157 super.visitIntInsn(Opcodes.SIPUSH, args.size());
158 super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
159 super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
160 "org/apidesign/bck2brwsr/launcher/fximpl/Fn", "invoke", "([Ljava/lang/Object;)Ljava/lang/Object;"
162 super.visitInsn(Opcodes.ARETURN);
166 public void visitEnd() {
169 FindInClass.this.visitField(
170 Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
171 "$$bck2brwsr$$" + name,
172 "Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;",
182 private final class FindInAnno extends AnnotationVisitor {
183 private List<String> args = new ArrayList<String>();
186 public FindInAnno() {
191 public void visit(String name, Object value) {
193 args.add((String) value);
196 assert name.equals("body");
197 body = (String) value;
201 public AnnotationVisitor visitArray(String name) {
206 public void visitEnd() {
208 generateJSBody(args, body);