# HG changeset patch # User Jaroslav Tulach # Date 1349276724 25200 # Node ID f3b685bd72433e7449cf164151766fff01483faa # Parent e7be3cb29a722e9970c66df18b1b7b6e8dbf2c3e Annotation to control conversion to JavaScript diff -r e7be3cb29a72 -r f3b685bd7243 core/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/pom.xml Wed Oct 03 08:05:24 2012 -0700 @@ -0,0 +1,26 @@ + + + 4.0.0 + + org.apidesign + bck2brwsr + 1.0-SNAPSHOT + + org.apidesign.bck2brwsr + core + 1.0-SNAPSHOT + core + http://maven.apache.org + + UTF-8 + + + + junit + junit + 3.8.1 + test + + + diff -r e7be3cb29a72 -r f3b685bd7243 core/src/main/java/org/apidesign/bck2brwsr/core/ExtraJavaScript.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/src/main/java/org/apidesign/bck2brwsr/core/ExtraJavaScript.java Wed Oct 03 08:05:24 2012 -0700 @@ -0,0 +1,19 @@ +package org.apidesign.bck2brwsr.core; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * @author Jaroslav Tulach + */ +@Retention(RetentionPolicy.CLASS) +@Target(ElementType.TYPE) +public @interface ExtraJavaScript { + /** location of a script to load */ + String resource(); + /** should the class file still be processed or not? */ + boolean processByteCode() default true; +} diff -r e7be3cb29a72 -r f3b685bd7243 core/src/main/java/org/apidesign/bck2brwsr/core/NoJavaScript.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/src/main/java/org/apidesign/bck2brwsr/core/NoJavaScript.java Wed Oct 03 08:05:24 2012 -0700 @@ -0,0 +1,16 @@ +package org.apidesign.bck2brwsr.core; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** Don't include given field or method in generated JavaScript. + * + * @author Jaroslav Tulach + */ +@Retention(RetentionPolicy.CLASS) +@Target({ ElementType.METHOD, ElementType.FIELD }) +public @interface NoJavaScript { + +} diff -r e7be3cb29a72 -r f3b685bd7243 pom.xml --- a/pom.xml Sun Sep 30 19:40:04 2012 -0700 +++ b/pom.xml Wed Oct 03 08:05:24 2012 -0700 @@ -9,6 +9,7 @@ vm htmlpage + core diff -r e7be3cb29a72 -r f3b685bd7243 vm/pom.xml --- a/vm/pom.xml Sun Sep 30 19:40:04 2012 -0700 +++ b/vm/pom.xml Wed Oct 03 08:05:24 2012 -0700 @@ -86,5 +86,11 @@ org-netbeans-modules-classfile jar + + org.apidesign.bck2brwsr + core + 1.0-SNAPSHOT + jar + diff -r e7be3cb29a72 -r f3b685bd7243 vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Sun Sep 30 19:40:04 2012 -0700 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Wed Oct 03 08:05:24 2012 -0700 @@ -22,6 +22,9 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import org.apidesign.bck2brwsr.core.ExtraJavaScript; +import org.netbeans.modules.classfile.Annotation; +import org.netbeans.modules.classfile.AnnotationComponent; import static org.netbeans.modules.classfile.ByteCodes.*; import org.netbeans.modules.classfile.CPClassInfo; import org.netbeans.modules.classfile.CPEntry; @@ -31,8 +34,10 @@ import org.netbeans.modules.classfile.ClassFile; import org.netbeans.modules.classfile.ClassName; import org.netbeans.modules.classfile.Code; +import org.netbeans.modules.classfile.ElementValue; import org.netbeans.modules.classfile.Method; import org.netbeans.modules.classfile.Parameter; +import org.netbeans.modules.classfile.PrimitiveElementValue; import org.netbeans.modules.classfile.Variable; /** Translator of the code inside class files to JavaScript. @@ -62,14 +67,29 @@ * generated JavaScript code works properly. The names are in internal * JVM form so String is java/lang/String. Can be null * if one is not interested in knowing references + * @param scripts write only collection with names of resources to read + * * @throws IOException if something goes wrong during read or write or translating */ public static void compile( InputStream classFile, Appendable out, - Collection references + Collection references, + Collection scripts ) throws IOException { ClassFile jc = new ClassFile(classFile, true); + final ClassName extraAnn = ClassName.getClassName(ExtraJavaScript.class.getName().replace('.', '/')); + Annotation a = jc.getAnnotation(extraAnn); + if (a != null) { + final ElementValue annVal = a.getComponent("resource").getValue(); + String res = ((PrimitiveElementValue)annVal).getValue().getValue().toString(); + scripts.add(res); + final AnnotationComponent process = a.getComponent("processByteCode"); + if (process != null && "false".equals(process.getValue().toString())) { + return; + } + } + ByteCodeToJavaScript compiler = new ByteCodeToJavaScript( jc, out, references ); diff -r e7be3cb29a72 -r f3b685bd7243 vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java Sun Sep 30 19:40:04 2012 -0700 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java Wed Oct 03 08:05:24 2012 -0700 @@ -48,24 +48,18 @@ } if (name.startsWith("java/") && !name.equals("java/lang/Object") - && !name.equals("java/lang/String") && !name.equals("java/lang/StringBuilder") && !name.equals("java/lang/AbstractStringBuilder") ) { continue; } - InputStream emul = GenJS.class.getResourceAsStream("emulation/" + name.replace('/', '_') + ".js"); - if (emul != null) { - readResource(emul, out); - continue; - } - InputStream is = GenJS.class.getClassLoader().getResourceAsStream(name + ".class"); if (is == null) { throw new IOException("Can't find class " + name); } + LinkedList scripts = new LinkedList(); try { - ByteCodeToJavaScript.compile(is, out, toProcess); + ByteCodeToJavaScript.compile(is, out, toProcess, scripts); } catch (RuntimeException ex) { if (out instanceof CharSequence) { CharSequence seq = (CharSequence)out; @@ -84,6 +78,13 @@ ); } } + for (String resource : scripts) { + InputStream emul = GenJS.class.getResourceAsStream(resource); + if (emul == null) { + throw new IOException("Can't find " + resource); + } + readResource(emul, out); + } } } private static void readResource(InputStream emul, Appendable out) throws IOException { diff -r e7be3cb29a72 -r f3b685bd7243 vm/src/test/java/org/apidesign/vm4brwsr/StringSample.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/StringSample.java Sun Sep 30 19:40:04 2012 -0700 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StringSample.java Wed Oct 03 08:05:24 2012 -0700 @@ -1,9 +1,12 @@ package org.apidesign.vm4brwsr; +import org.apidesign.bck2brwsr.core.ExtraJavaScript; + /** * * @author Jaroslav Tulach */ +@ExtraJavaScript(resource="/org/apidesign/vm4brwsr/emulation/java_lang_String.js") public class StringSample { public static final String HELLO = "Hello World!"; private static int counter;