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 & (~Opcodes.ACC_NATIVE), 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;
127 private boolean bodyGenerated;
129 public FindInMethod(String name, String desc, MethodVisitor mv) {
130 super(Opcodes.ASM4, mv);
136 public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
137 if ("Lorg/apidesign/bck2brwsr/core/JavaScriptBody;".equals(desc)) { // NOI18N
139 return new FindInAnno();
141 return super.visitAnnotation(desc, visible);
144 private void generateJSBody(List<String> args, String body) {
150 public void visitCode() {
157 private boolean generateBody() {
161 bodyGenerated = true;
163 super.visitFieldInsn(
164 Opcodes.GETSTATIC, FindInClass.this.name,
165 "$$bck2brwsr$$" + name,
166 "Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;"
168 super.visitInsn(Opcodes.DUP);
169 Label ifNotNull = new Label();
170 super.visitJumpInsn(Opcodes.IFNONNULL, ifNotNull);
173 super.visitInsn(Opcodes.POP);
174 super.visitLdcInsn(Type.getObjectType(FindInClass.this.name));
175 super.visitLdcInsn(body);
176 super.visitIntInsn(Opcodes.SIPUSH, args.size());
177 super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
178 for (int i = 0; i < args.size(); i++) {
179 String name = args.get(i);
180 super.visitInsn(Opcodes.DUP);
181 super.visitIntInsn(Opcodes.BIPUSH, i);
182 super.visitLdcInsn(name);
183 super.visitInsn(Opcodes.AASTORE);
185 super.visitMethodInsn(Opcodes.INVOKESTATIC,
186 "org/apidesign/bck2brwsr/launcher/fximpl/Fn", "define",
187 "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/String;)Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;"
191 super.visitLabel(ifNotNull);
192 super.visitIntInsn(Opcodes.SIPUSH, args.size());
193 super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
195 class SV extends SignatureVisitor {
196 private boolean nowReturn;
197 private Type returnType;
205 public void visitBaseType(char descriptor) {
206 final Type t = Type.getType("" + descriptor);
211 FindInMethod.super.visitInsn(Opcodes.DUP);
212 FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index);
213 FindInMethod.super.visitVarInsn(t.getOpcode(Opcodes.ILOAD), index);
215 switch (descriptor) {
216 case 'I': factory = "java/lang/Integer"; break;
217 case 'J': factory = "java/lang/Long"; break;
218 case 'S': factory = "java/lang/Short"; break;
219 case 'F': factory = "java/lang/Float"; break;
220 case 'D': factory = "java/lang/Double"; break;
221 case 'Z': factory = "java/lang/Boolean"; break;
222 case 'C': factory = "java/lang/Character"; break;
223 case 'B': factory = "java/lang/Byte"; break;
224 default: throw new IllegalStateException(t.toString());
226 FindInMethod.super.visitMethodInsn(Opcodes.INVOKESTATIC,
227 factory, "valueOf", "(" + descriptor + ")L" + factory + ";"
229 FindInMethod.super.visitInsn(Opcodes.AASTORE);
234 public void visitClassType(String name) {
236 returnType = Type.getObjectType(name);
239 FindInMethod.super.visitInsn(Opcodes.DUP);
240 FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index);
241 FindInMethod.super.visitVarInsn(Opcodes.ALOAD, index);
242 FindInMethod.super.visitInsn(Opcodes.AASTORE);
247 public SignatureVisitor visitReturnType() {
255 SignatureReader sr = new SignatureReader(desc);
258 super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
259 "org/apidesign/bck2brwsr/launcher/fximpl/Fn", "invoke", "([Ljava/lang/Object;)Ljava/lang/Object;"
261 switch (sv.returnType.getSort()) {
263 super.visitInsn(Opcodes.RETURN);
267 super.visitTypeInsn(Opcodes.CHECKCAST, sv.returnType.getInternalName());
268 super.visitInsn(Opcodes.ARETURN);
271 super.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Number");
272 super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
273 "java/lang/Number", sv.returnType.getClassName() + "Value", "()" + sv.returnType.getDescriptor()
275 super.visitInsn(sv.returnType.getOpcode(Opcodes.IRETURN));
281 public void visitEnd() {
284 if (generateBody()) {
286 super.visitMaxs(1, 0);
288 FindInClass.this.visitField(
289 Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
290 "$$bck2brwsr$$" + name,
291 "Lorg/apidesign/bck2brwsr/launcher/fximpl/Fn;",
301 private final class FindInAnno extends AnnotationVisitor {
302 private List<String> args = new ArrayList<String>();
305 public FindInAnno() {
310 public void visit(String name, Object value) {
312 args.add((String) value);
315 assert name.equals("body");
316 body = (String) value;
320 public AnnotationVisitor visitArray(String name) {
325 public void visitEnd() {
327 generateJSBody(args, body);
334 private class ClassWriterEx extends ClassWriter {
336 public ClassWriterEx(ClassReader classReader, int flags) {
337 super(classReader, flags);
341 protected String getCommonSuperClass(final String type1, final String type2) {
343 ClassLoader classLoader = JsClassLoader.this;
345 c = Class.forName(type1.replace('/', '.'), false, classLoader);
346 d = Class.forName(type2.replace('/', '.'), false, classLoader);
347 } catch (Exception e) {
348 throw new RuntimeException(e.toString());
350 if (c.isAssignableFrom(d)) {
353 if (d.isAssignableFrom(c)) {
356 if (c.isInterface() || d.isInterface()) {
357 return "java/lang/Object";
360 c = c.getSuperclass();
361 } while (!c.isAssignableFrom(d));
362 return c.getName().replace('.', '/');