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 if (name.equals(FnUtils.class.getName())) {
76 URL u = findResource(name.replace('.', '/') + ".class");
78 InputStream is = null;
81 byte[] arr = new byte[is.available()];
83 while (len < arr.length) {
84 int read = is.read(arr, len, arr.length - len);
86 throw new IOException("Can't read " + u);
92 ClassReader cr = new ClassReader(arr);
93 FindInClass tst = new FindInClass(null);
96 ClassWriter w = new ClassWriterEx(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
97 FindInClass fic = new FindInClass(w);
99 arr = w.toByteArray();
102 return defineClass(name, arr, 0, arr.length);
104 } catch (IOException ex) {
105 throw new ClassNotFoundException("Can't load " + name, ex);
108 if (is != null) is.close();
109 } catch (IOException ex) {
110 throw new ClassNotFoundException(null, ex);
115 name.equals("org.apidesign.html.boot.spi.Fn") ||
116 name.equals("org.apidesign.html.boot.impl.FnUtils")
118 return Class.forName(name);
121 return super.findClass(name);
124 protected abstract Fn defineFn(String code, String... names);
127 private static final class FindInClass extends ClassVisitor {
131 public FindInClass(ClassVisitor cv) {
132 super(Opcodes.ASM4, cv);
136 public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
138 super.visit(version, access, name, signature, superName, interfaces);
144 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
145 return new FindInMethod(access, name, desc,
146 super.visitMethod(access & (~Opcodes.ACC_NATIVE), name, desc, signature, exceptions)
150 private final class FindInMethod extends MethodVisitor {
151 private final String name;
152 private final String desc;
153 private final int access;
154 private List<String> args;
156 private boolean bodyGenerated;
158 public FindInMethod(int access, String name, String desc, MethodVisitor mv) {
159 super(Opcodes.ASM4, mv);
160 this.access = access;
166 public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
167 if ("Lnet/java/html/js/JavaScriptBody;".equals(desc)) { // NOI18N
169 return new FindInAnno();
171 return super.visitAnnotation(desc, visible);
174 private void generateJSBody(List<String> args, String body) {
180 public void visitCode() {
187 private boolean generateBody() {
191 bodyGenerated = true;
193 super.visitFieldInsn(
194 Opcodes.GETSTATIC, FindInClass.this.name,
195 "$$fn$$" + name + "_" + found,
196 "Lorg/apidesign/html/boot/spi/Fn;"
198 super.visitInsn(Opcodes.DUP);
199 Label ifNotNull = new Label();
200 super.visitJumpInsn(Opcodes.IFNONNULL, ifNotNull);
203 super.visitInsn(Opcodes.POP);
204 super.visitLdcInsn(Type.getObjectType(FindInClass.this.name));
205 super.visitLdcInsn(body);
206 super.visitIntInsn(Opcodes.SIPUSH, args.size());
207 super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
208 for (int i = 0; i < args.size(); i++) {
209 String name = args.get(i);
210 super.visitInsn(Opcodes.DUP);
211 super.visitIntInsn(Opcodes.BIPUSH, i);
212 super.visitLdcInsn(name);
213 super.visitInsn(Opcodes.AASTORE);
215 super.visitMethodInsn(Opcodes.INVOKESTATIC,
216 "org/apidesign/html/boot/impl/FnUtils", "define",
217 "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/String;)Lorg/apidesign/html/boot/spi/Fn;"
221 super.visitLabel(ifNotNull);
224 if ((access & Opcodes.ACC_STATIC) == 0) {
226 super.visitIntInsn(Opcodes.ALOAD, 0);
229 super.visitInsn(Opcodes.ACONST_NULL);
232 super.visitIntInsn(Opcodes.SIPUSH, args.size());
233 super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
235 class SV extends SignatureVisitor {
236 private boolean nowReturn;
237 private Type returnType;
245 public void visitBaseType(char descriptor) {
246 final Type t = Type.getType("" + descriptor);
251 FindInMethod.super.visitInsn(Opcodes.DUP);
252 FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index);
253 FindInMethod.super.visitVarInsn(t.getOpcode(Opcodes.ILOAD), index + offset);
255 switch (descriptor) {
256 case 'I': factory = "java/lang/Integer"; break;
257 case 'J': factory = "java/lang/Long"; break;
258 case 'S': factory = "java/lang/Short"; break;
259 case 'F': factory = "java/lang/Float"; break;
260 case 'D': factory = "java/lang/Double"; break;
261 case 'Z': factory = "java/lang/Boolean"; break;
262 case 'C': factory = "java/lang/Character"; break;
263 case 'B': factory = "java/lang/Byte"; break;
264 default: throw new IllegalStateException(t.toString());
266 FindInMethod.super.visitMethodInsn(Opcodes.INVOKESTATIC,
267 factory, "valueOf", "(" + descriptor + ")L" + factory + ";"
269 FindInMethod.super.visitInsn(Opcodes.AASTORE);
274 public void visitClassType(String name) {
276 returnType = Type.getObjectType(name);
279 FindInMethod.super.visitInsn(Opcodes.DUP);
280 FindInMethod.super.visitIntInsn(Opcodes.SIPUSH, index);
281 FindInMethod.super.visitVarInsn(Opcodes.ALOAD, index + offset);
282 FindInMethod.super.visitInsn(Opcodes.AASTORE);
287 public SignatureVisitor visitReturnType() {
295 SignatureReader sr = new SignatureReader(desc);
298 super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
299 "org/apidesign/html/boot/spi/Fn", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"
301 switch (sv.returnType.getSort()) {
303 super.visitInsn(Opcodes.RETURN);
307 super.visitTypeInsn(Opcodes.CHECKCAST, sv.returnType.getInternalName());
308 super.visitInsn(Opcodes.ARETURN);
311 super.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Number");
312 super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
313 "java/lang/Number", sv.returnType.getClassName() + "Value", "()" + sv.returnType.getDescriptor()
315 super.visitInsn(sv.returnType.getOpcode(Opcodes.IRETURN));
321 public void visitEnd() {
324 if (generateBody()) {
326 super.visitMaxs(1, 0);
328 FindInClass.this.visitField(
329 Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
330 "$$fn$$" + name + "_" + found,
331 "Lorg/apidesign/html/boot/spi/Fn;",
341 private final class FindInAnno extends AnnotationVisitor {
342 private List<String> args = new ArrayList<String>();
345 public FindInAnno() {
350 public void visit(String name, Object value) {
352 args.add((String) value);
355 assert name.equals("body");
356 body = (String) value;
360 public AnnotationVisitor visitArray(String name) {
365 public void visitEnd() {
367 generateJSBody(args, body);
374 private class ClassWriterEx extends ClassWriter {
376 public ClassWriterEx(ClassReader classReader, int flags) {
377 super(classReader, flags);
381 protected String getCommonSuperClass(final String type1, final String type2) {
383 ClassLoader classLoader = JsClassLoader.this;
385 c = Class.forName(type1.replace('/', '.'), false, classLoader);
386 d = Class.forName(type2.replace('/', '.'), false, classLoader);
387 } catch (Exception e) {
388 throw new RuntimeException(e.toString());
390 if (c.isAssignableFrom(d)) {
393 if (d.isAssignableFrom(c)) {
396 if (c.isInterface() || d.isInterface()) {
397 return "java/lang/Object";
400 c = c.getSuperclass();
401 } while (!c.isAssignableFrom(d));
402 return c.getName().replace('.', '/');