2 * HTML via Java(tm) Language Bindings
3 * Copyright (C) 2013 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. apidesign.org
13 * designates this particular file as subject to the
14 * "Classpath" exception as provided by apidesign.org
15 * in the License file that accompanied this code.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. Look for COPYING file in the top folder.
19 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
21 package org.apidesign.html.boot.impl;
23 import org.apidesign.html.boot.spi.Fn;
24 import java.io.IOException;
25 import java.io.InputStream;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.List;
30 import org.objectweb.asm.AnnotationVisitor;
31 import org.objectweb.asm.ClassReader;
32 import org.objectweb.asm.ClassVisitor;
33 import org.objectweb.asm.ClassWriter;
34 import org.objectweb.asm.Label;
35 import org.objectweb.asm.MethodVisitor;
36 import org.objectweb.asm.Opcodes;
37 import org.objectweb.asm.Type;
38 import org.objectweb.asm.signature.SignatureReader;
39 import org.objectweb.asm.signature.SignatureVisitor;
43 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
45 abstract class JsClassLoader extends ClassLoader {
46 JsClassLoader(ClassLoader parent) {
51 protected abstract URL findResource(String name);
54 protected abstract Enumeration<URL> findResources(String name);
57 protected Class<?> findClass(String name) throws ClassNotFoundException {
58 if (name.startsWith("javafx")) {
59 return Class.forName(name);
61 if (name.startsWith("netscape")) {
62 return Class.forName(name);
64 if (name.startsWith("com.sun")) {
65 return Class.forName(name);
67 if (name.equals(JsClassLoader.class.getName())) {
68 return JsClassLoader.class;
70 if (name.equals(Fn.class.getName())) {
73 URL u = findResource(name.replace('.', '/') + ".class");
75 InputStream is = null;
78 byte[] arr = new byte[is.available()];
80 while (len < arr.length) {
81 int read = is.read(arr, len, arr.length - len);
83 throw new IOException("Can't read " + u);
89 ClassReader cr = new ClassReader(arr);
90 FindInClass tst = new FindInClass(null);
93 ClassWriter w = new ClassWriterEx(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
94 FindInClass fic = new FindInClass(w);
96 arr = w.toByteArray();
99 return defineClass(name, arr, 0, arr.length);
101 } catch (IOException ex) {
102 throw new ClassNotFoundException("Can't load " + name, ex);
105 if (is != null) is.close();
106 } catch (IOException ex) {
107 throw new ClassNotFoundException(null, ex);
111 if (name.startsWith("org.apidesign.html.boot.spi.Fn")) {
112 return Class.forName(name);
115 return super.findClass(name);
118 protected abstract Fn defineFn(String code, String... names);
121 private static final class FindInClass extends ClassVisitor {
125 public FindInClass(ClassVisitor cv) {
126 super(Opcodes.ASM4, cv);
130 public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
132 super.visit(version, access, name, signature, superName, interfaces);
138 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
139 return new FindInMethod(access, name, desc,
140 super.visitMethod(access & (~Opcodes.ACC_NATIVE), name, desc, signature, exceptions)
144 private final class FindInMethod extends MethodVisitor {
145 private final String name;
146 private final String desc;
147 private final int access;
148 private List<String> args;
150 private boolean bodyGenerated;
152 public FindInMethod(int access, String name, String desc, MethodVisitor mv) {
153 super(Opcodes.ASM4, mv);
154 this.access = access;
160 public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
161 if ("Lnet/java/html/js/JavaScriptBody;".equals(desc)) { // NOI18N
163 return new FindInAnno();
165 return super.visitAnnotation(desc, visible);
168 private void generateJSBody(List<String> args, String body) {
174 public void visitCode() {
181 private boolean generateBody() {
185 bodyGenerated = true;
187 super.visitFieldInsn(
188 Opcodes.GETSTATIC, FindInClass.this.name,
189 "$$fn$$" + name + "_" + found,
190 "Lorg/apidesign/html/boot/spi/Fn;"
192 super.visitInsn(Opcodes.DUP);
193 Label ifNotNull = new Label();
194 super.visitJumpInsn(Opcodes.IFNONNULL, ifNotNull);
197 super.visitInsn(Opcodes.POP);
198 super.visitLdcInsn(Type.getObjectType(FindInClass.this.name));
199 super.visitLdcInsn(body);
200 super.visitIntInsn(Opcodes.SIPUSH, args.size());
201 super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
202 for (int i = 0; i < args.size(); i++) {
203 String name = args.get(i);
204 super.visitInsn(Opcodes.DUP);
205 super.visitIntInsn(Opcodes.BIPUSH, i);
206 super.visitLdcInsn(name);
207 super.visitInsn(Opcodes.AASTORE);
209 super.visitMethodInsn(Opcodes.INVOKESTATIC,
210 "org/apidesign/html/boot/spi/Fn", "define",
211 "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/String;)Lorg/apidesign/html/boot/spi/Fn;"
215 super.visitLabel(ifNotNull);
218 if ((access & Opcodes.ACC_STATIC) == 0) {
220 super.visitIntInsn(Opcodes.ALOAD, 0);
223 super.visitInsn(Opcodes.ACONST_NULL);
226 super.visitIntInsn(Opcodes.SIPUSH, args.size());
227 super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
229 class SV extends SignatureVisitor {
230 private boolean nowReturn;
231 private Type returnType;
239 public void visitBaseType(char descriptor) {
240 final Type t = Type.getType("" + descriptor);
245 FindInMethod.super.visitInsn(Opcodes.DUP);
246 FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index);
247 FindInMethod.super.visitVarInsn(t.getOpcode(Opcodes.ILOAD), index + offset);
249 switch (descriptor) {
250 case 'I': factory = "java/lang/Integer"; break;
251 case 'J': factory = "java/lang/Long"; break;
252 case 'S': factory = "java/lang/Short"; break;
253 case 'F': factory = "java/lang/Float"; break;
254 case 'D': factory = "java/lang/Double"; break;
255 case 'Z': factory = "java/lang/Boolean"; break;
256 case 'C': factory = "java/lang/Character"; break;
257 case 'B': factory = "java/lang/Byte"; break;
258 default: throw new IllegalStateException(t.toString());
260 FindInMethod.super.visitMethodInsn(Opcodes.INVOKESTATIC,
261 factory, "valueOf", "(" + descriptor + ")L" + factory + ";"
263 FindInMethod.super.visitInsn(Opcodes.AASTORE);
268 public void visitClassType(String name) {
270 returnType = Type.getObjectType(name);
273 FindInMethod.super.visitInsn(Opcodes.DUP);
274 FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index);
275 FindInMethod.super.visitVarInsn(Opcodes.ALOAD, index + offset);
276 FindInMethod.super.visitInsn(Opcodes.AASTORE);
281 public SignatureVisitor visitReturnType() {
289 SignatureReader sr = new SignatureReader(desc);
292 super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
293 "org/apidesign/html/boot/spi/Fn", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"
295 switch (sv.returnType.getSort()) {
297 super.visitInsn(Opcodes.RETURN);
301 super.visitTypeInsn(Opcodes.CHECKCAST, sv.returnType.getInternalName());
302 super.visitInsn(Opcodes.ARETURN);
305 super.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Number");
306 super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
307 "java/lang/Number", sv.returnType.getClassName() + "Value", "()" + sv.returnType.getDescriptor()
309 super.visitInsn(sv.returnType.getOpcode(Opcodes.IRETURN));
315 public void visitEnd() {
318 if (generateBody()) {
320 super.visitMaxs(1, 0);
322 FindInClass.this.visitField(
323 Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
324 "$$fn$$" + name + "_" + found,
325 "Lorg/apidesign/html/boot/spi/Fn;",
335 private final class FindInAnno extends AnnotationVisitor {
336 private List<String> args = new ArrayList<String>();
339 public FindInAnno() {
344 public void visit(String name, Object value) {
346 args.add((String) value);
349 assert name.equals("body");
350 body = (String) value;
354 public AnnotationVisitor visitArray(String name) {
359 public void visitEnd() {
361 generateJSBody(args, body);
368 private class ClassWriterEx extends ClassWriter {
370 public ClassWriterEx(ClassReader classReader, int flags) {
371 super(classReader, flags);
375 protected String getCommonSuperClass(final String type1, final String type2) {
377 ClassLoader classLoader = JsClassLoader.this;
379 c = Class.forName(type1.replace('/', '.'), false, classLoader);
380 d = Class.forName(type2.replace('/', '.'), false, classLoader);
381 } catch (Exception e) {
382 throw new RuntimeException(e.toString());
384 if (c.isAssignableFrom(d)) {
387 if (d.isAssignableFrom(c)) {
390 if (c.isInterface() || d.isInterface()) {
391 return "java/lang/Object";
394 c = c.getSuperclass();
395 } while (!c.isAssignableFrom(d));
396 return c.getName().replace('.', '/');