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 if (name.startsWith("javafx")) {
55 return Class.forName(name);
57 if (name.startsWith("netscape")) {
58 return Class.forName(name);
60 if (name.startsWith("com.sun")) {
61 return Class.forName(name);
63 if (name.equals(JsClassLoader.class.getName())) {
64 return JsClassLoader.class;
66 if (name.equals(Fn.class.getName())) {
69 URL u = findResource(name.replace('.', '/') + ".class");
71 InputStream is = null;
74 byte[] arr = new byte[is.available()];
76 while (len < arr.length) {
77 int read = is.read(arr, len, arr.length - len);
79 throw new IOException("Can't read " + u);
85 ClassReader cr = new ClassReader(arr);
86 FindInClass tst = new FindInClass(null);
89 ClassWriter w = new ClassWriterEx(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
90 FindInClass fic = new FindInClass(w);
92 arr = w.toByteArray();
95 return defineClass(name, arr, 0, arr.length);
97 } catch (IOException ex) {
98 throw new ClassNotFoundException("Can't load " + name, ex);
101 if (is != null) is.close();
102 } catch (IOException ex) {
103 throw new ClassNotFoundException(null, ex);
107 if (name.startsWith("org.apidesign.bck2brwsr.launcher.fximpl.Fn")) {
108 return Class.forName(name);
111 return super.findClass(name);
114 protected abstract Fn defineFn(String code, String... names);
117 private static final class FindInClass extends ClassVisitor {
121 public FindInClass(ClassVisitor cv) {
122 super(Opcodes.ASM4, cv);
126 public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
128 super.visit(version, access, name, signature, superName, interfaces);
134 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
135 return new FindInMethod(access, name, desc,
136 super.visitMethod(access & (~Opcodes.ACC_NATIVE), name, desc, signature, exceptions)
140 private final class FindInMethod extends MethodVisitor {
141 private final String name;
142 private final String desc;
143 private final int access;
144 private List<String> args;
146 private boolean bodyGenerated;
148 public FindInMethod(int access, String name, String desc, MethodVisitor mv) {
149 super(Opcodes.ASM4, mv);
150 this.access = access;
156 public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
157 if ("Lorg/apidesign/bck2brwsr/core/JavaScriptBody;".equals(desc)) { // NOI18N
159 return new FindInAnno();
161 return super.visitAnnotation(desc, visible);
164 private void generateJSBody(List<String> args, String body) {
170 public void visitCode() {
177 private boolean generateBody() {
181 bodyGenerated = true;
183 super.visitFieldInsn(
184 Opcodes.GETSTATIC, FindInClass.this.name,
185 "$$bck2brwsr$$" + name + "_" + found,
186 "Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;"
188 super.visitInsn(Opcodes.DUP);
189 Label ifNotNull = new Label();
190 super.visitJumpInsn(Opcodes.IFNONNULL, ifNotNull);
193 super.visitInsn(Opcodes.POP);
194 super.visitLdcInsn(Type.getObjectType(FindInClass.this.name));
195 super.visitLdcInsn(body);
196 super.visitIntInsn(Opcodes.SIPUSH, args.size());
197 super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
198 for (int i = 0; i < args.size(); i++) {
199 String name = args.get(i);
200 super.visitInsn(Opcodes.DUP);
201 super.visitIntInsn(Opcodes.BIPUSH, i);
202 super.visitLdcInsn(name);
203 super.visitInsn(Opcodes.AASTORE);
205 super.visitMethodInsn(Opcodes.INVOKESTATIC,
206 "org/apidesign/bck2brwsr/launcher/fximpl/Fn", "define",
207 "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/String;)Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;"
211 super.visitLabel(ifNotNull);
214 if ((access & Opcodes.ACC_STATIC) == 0) {
216 super.visitIntInsn(Opcodes.ALOAD, 0);
219 super.visitInsn(Opcodes.ACONST_NULL);
222 super.visitIntInsn(Opcodes.SIPUSH, args.size());
223 super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
225 class SV extends SignatureVisitor {
226 private boolean nowReturn;
227 private Type returnType;
235 public void visitBaseType(char descriptor) {
236 final Type t = Type.getType("" + descriptor);
241 FindInMethod.super.visitInsn(Opcodes.DUP);
242 FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index);
243 FindInMethod.super.visitVarInsn(t.getOpcode(Opcodes.ILOAD), index + offset);
245 switch (descriptor) {
246 case 'I': factory = "java/lang/Integer"; break;
247 case 'J': factory = "java/lang/Long"; break;
248 case 'S': factory = "java/lang/Short"; break;
249 case 'F': factory = "java/lang/Float"; break;
250 case 'D': factory = "java/lang/Double"; break;
251 case 'Z': factory = "java/lang/Boolean"; break;
252 case 'C': factory = "java/lang/Character"; break;
253 case 'B': factory = "java/lang/Byte"; break;
254 default: throw new IllegalStateException(t.toString());
256 FindInMethod.super.visitMethodInsn(Opcodes.INVOKESTATIC,
257 factory, "valueOf", "(" + descriptor + ")L" + factory + ";"
259 FindInMethod.super.visitInsn(Opcodes.AASTORE);
264 public void visitClassType(String name) {
266 returnType = Type.getObjectType(name);
269 FindInMethod.super.visitInsn(Opcodes.DUP);
270 FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index);
271 FindInMethod.super.visitVarInsn(Opcodes.ALOAD, index + offset);
272 FindInMethod.super.visitInsn(Opcodes.AASTORE);
277 public SignatureVisitor visitReturnType() {
285 SignatureReader sr = new SignatureReader(desc);
288 super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
289 "org/apidesign/bck2brwsr/launcher/fximpl/Fn", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"
291 switch (sv.returnType.getSort()) {
293 super.visitInsn(Opcodes.RETURN);
297 super.visitTypeInsn(Opcodes.CHECKCAST, sv.returnType.getInternalName());
298 super.visitInsn(Opcodes.ARETURN);
301 super.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Number");
302 super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
303 "java/lang/Number", sv.returnType.getClassName() + "Value", "()" + sv.returnType.getDescriptor()
305 super.visitInsn(sv.returnType.getOpcode(Opcodes.IRETURN));
311 public void visitEnd() {
314 if (generateBody()) {
316 super.visitMaxs(1, 0);
318 FindInClass.this.visitField(
319 Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
320 "$$bck2brwsr$$" + name + "_" + found,
321 "Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;",
331 private final class FindInAnno extends AnnotationVisitor {
332 private List<String> args = new ArrayList<String>();
335 public FindInAnno() {
340 public void visit(String name, Object value) {
342 args.add((String) value);
345 assert name.equals("body");
346 body = (String) value;
350 public AnnotationVisitor visitArray(String name) {
355 public void visitEnd() {
357 generateJSBody(args, body);
364 private class ClassWriterEx extends ClassWriter {
366 public ClassWriterEx(ClassReader classReader, int flags) {
367 super(classReader, flags);
371 protected String getCommonSuperClass(final String type1, final String type2) {
373 ClassLoader classLoader = JsClassLoader.this;
375 c = Class.forName(type1.replace('/', '.'), false, classLoader);
376 d = Class.forName(type2.replace('/', '.'), false, classLoader);
377 } catch (Exception e) {
378 throw new RuntimeException(e.toString());
380 if (c.isAssignableFrom(d)) {
383 if (d.isAssignableFrom(c)) {
386 if (c.isInterface() || d.isInterface()) {
387 return "java/lang/Object";
390 c = c.getSuperclass();
391 } while (!c.isAssignableFrom(d));
392 return c.getName().replace('.', '/');