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.net.URLConnection;
24 import java.util.ArrayList;
25 import java.util.Enumeration;
26 import java.util.List;
27 import org.objectweb.asm.AnnotationVisitor;
28 import org.objectweb.asm.ClassReader;
29 import org.objectweb.asm.ClassVisitor;
30 import org.objectweb.asm.ClassWriter;
31 import org.objectweb.asm.Label;
32 import org.objectweb.asm.MethodVisitor;
33 import org.objectweb.asm.Opcodes;
34 import org.objectweb.asm.Type;
35 import org.objectweb.asm.signature.SignatureReader;
36 import org.objectweb.asm.signature.SignatureVisitor;
40 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
42 abstract class JsClassLoader extends ClassLoader {
43 JsClassLoader(ClassLoader parent) {
48 protected abstract URL findResource(String name);
51 protected abstract Enumeration<URL> findResources(String name);
54 protected Class<?> findClass(String name) throws ClassNotFoundException {
55 URL u = findResource(name.replace('.', '/') + ".class");
57 InputStream is = null;
60 byte[] arr = new byte[is.available()];
61 int len = is.read(arr);
62 if (len != arr.length) {
67 ClassReader cr = new ClassReader(arr);
68 FindInClass tst = new FindInClass(null);
71 ClassWriter w = new ClassWriterEx(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
72 FindInClass fic = new FindInClass(w);
74 arr = w.toByteArray();
77 return defineClass(name, arr, 0, arr.length);
79 } catch (IOException ex) {
80 throw new ClassNotFoundException("Can't load " + name, ex);
83 if (is != null) is.close();
84 } catch (IOException ex) {
85 throw new ClassNotFoundException(null, ex);
89 if (name.startsWith("org.apidesign.bck2brwsr.launcher.fximpl.Fn")) {
90 return Class.forName(name);
93 return super.findClass(name);
96 protected abstract Fn defineFn(String code, String... names);
99 private static final class FindInClass extends ClassVisitor {
101 private boolean found;
103 public FindInClass(ClassVisitor cv) {
104 super(Opcodes.ASM4, cv);
108 public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
110 super.visit(version, access, name, signature, superName, interfaces);
116 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
117 return new FindInMethod(name, desc,
118 super.visitMethod(access, name, desc, signature, exceptions)
122 private final class FindInMethod extends MethodVisitor {
123 private final String name;
124 private final String desc;
125 private List<String> args;
128 public FindInMethod(String name, String desc, MethodVisitor mv) {
129 super(Opcodes.ASM4, mv);
135 public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
136 if ("Lorg/apidesign/bck2brwsr/core/JavaScriptBody;".equals(desc)) { // NOI18N
138 return new FindInAnno();
140 return super.visitAnnotation(desc, visible);
143 private void generateJSBody(List<String> args, String body) {
149 public void visitCode() {
154 super.visitFieldInsn(
155 Opcodes.GETSTATIC, FindInClass.this.name,
156 "$$bck2brwsr$$" + name,
157 "Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;"
159 super.visitInsn(Opcodes.DUP);
160 Label ifNotNull = new Label();
161 super.visitJumpInsn(Opcodes.IFNONNULL, ifNotNull);
164 super.visitInsn(Opcodes.POP);
165 super.visitLdcInsn(Type.getObjectType(FindInClass.this.name));
166 super.visitLdcInsn(body);
167 super.visitIntInsn(Opcodes.SIPUSH, args.size());
168 super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
169 for (int i = 0; i < args.size(); i++) {
170 String name = args.get(i);
171 super.visitInsn(Opcodes.DUP);
172 super.visitIntInsn(Opcodes.BIPUSH, i);
173 super.visitLdcInsn(name);
174 super.visitInsn(Opcodes.AASTORE);
176 super.visitMethodInsn(Opcodes.INVOKESTATIC,
177 "org/apidesign/bck2brwsr/launcher/fximpl/Fn", "define",
178 "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/String;)Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;"
182 super.visitLabel(ifNotNull);
183 super.visitIntInsn(Opcodes.SIPUSH, args.size());
184 super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
186 class SV extends SignatureVisitor {
187 private boolean nowReturn;
188 private Type returnType;
196 public void visitBaseType(char descriptor) {
197 final Type t = Type.getType("" + descriptor);
202 FindInMethod.super.visitInsn(Opcodes.DUP);
203 FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index);
204 FindInMethod.super.visitVarInsn(t.getOpcode(Opcodes.ILOAD), index);
206 switch (descriptor) {
207 case 'I': factory = "java/lang/Integer"; break;
208 case 'J': factory = "java/lang/Long"; break;
209 case 'S': factory = "java/lang/Short"; break;
210 case 'F': factory = "java/lang/Float"; break;
211 case 'D': factory = "java/lang/Double"; break;
212 case 'Z': factory = "java/lang/Boolean"; break;
213 case 'C': factory = "java/lang/Character"; break;
214 case 'B': factory = "java/lang/Byte"; break;
215 default: throw new IllegalStateException(t.toString());
217 FindInMethod.super.visitMethodInsn(Opcodes.INVOKESTATIC,
218 factory, "valueOf", "(" + descriptor + ")L" + factory + ";"
220 FindInMethod.super.visitInsn(Opcodes.AASTORE);
225 public void visitClassType(String name) {
227 returnType = Type.getObjectType(name);
230 FindInMethod.super.visitInsn(Opcodes.DUP);
231 FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index);
232 FindInMethod.super.visitVarInsn(Opcodes.ALOAD, index);
233 FindInMethod.super.visitInsn(Opcodes.AASTORE);
238 public SignatureVisitor visitReturnType() {
246 SignatureReader sr = new SignatureReader(desc);
249 super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
250 "org/apidesign/bck2brwsr/launcher/fximpl/Fn", "invoke", "([Ljava/lang/Object;)Ljava/lang/Object;"
252 switch (sv.returnType.getSort()) {
254 super.visitInsn(Opcodes.RETURN);
258 super.visitTypeInsn(Opcodes.CHECKCAST, sv.returnType.getInternalName());
259 super.visitInsn(Opcodes.ARETURN);
262 super.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Number");
263 super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
264 "java/lang/Number", sv.returnType.getClassName() + "Value", "()" + sv.returnType.getDescriptor()
266 super.visitInsn(sv.returnType.getOpcode(Opcodes.IRETURN));
271 public void visitEnd() {
274 FindInClass.this.visitField(
275 Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
276 "$$bck2brwsr$$" + name,
277 "Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;",
287 private final class FindInAnno extends AnnotationVisitor {
288 private List<String> args = new ArrayList<String>();
291 public FindInAnno() {
296 public void visit(String name, Object value) {
298 args.add((String) value);
301 assert name.equals("body");
302 body = (String) value;
306 public AnnotationVisitor visitArray(String name) {
311 public void visitEnd() {
313 generateJSBody(args, body);
320 private class ClassWriterEx extends ClassWriter {
322 public ClassWriterEx(ClassReader classReader, int flags) {
323 super(classReader, flags);
327 protected String getCommonSuperClass(final String type1, final String type2) {
329 ClassLoader classLoader = JsClassLoader.this;
331 c = Class.forName(type1.replace('/', '.'), false, classLoader);
332 d = Class.forName(type2.replace('/', '.'), false, classLoader);
333 } catch (Exception e) {
334 throw new RuntimeException(e.toString());
336 if (c.isAssignableFrom(d)) {
339 if (d.isAssignableFrom(c)) {
342 if (c.isInterface() || d.isInterface()) {
343 return "java/lang/Object";
346 c = c.getSuperclass();
347 } while (!c.isAssignableFrom(d));
348 return c.getName().replace('.', '/');