Annotation to control conversion to JavaScript strings
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 03 Oct 2012 08:05:24 -0700
branchstrings
changeset 91f3b685bd7243
parent 90 e7be3cb29a72
child 92 bf4f95784c62
Annotation to control conversion to JavaScript
core/pom.xml
core/src/main/java/org/apidesign/bck2brwsr/core/ExtraJavaScript.java
core/src/main/java/org/apidesign/bck2brwsr/core/NoJavaScript.java
pom.xml
vm/pom.xml
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
vm/src/test/java/org/apidesign/vm4brwsr/StringSample.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/pom.xml	Wed Oct 03 08:05:24 2012 -0700
     1.3 @@ -0,0 +1,26 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
     1.6 +    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     1.7 +  <modelVersion>4.0.0</modelVersion>
     1.8 +  <parent>
     1.9 +    <groupId>org.apidesign</groupId>
    1.10 +    <artifactId>bck2brwsr</artifactId>
    1.11 +    <version>1.0-SNAPSHOT</version>
    1.12 +  </parent>
    1.13 +  <groupId>org.apidesign.bck2brwsr</groupId>
    1.14 +  <artifactId>core</artifactId>
    1.15 +  <version>1.0-SNAPSHOT</version>
    1.16 +  <name>core</name>
    1.17 +  <url>http://maven.apache.org</url>
    1.18 +  <properties>
    1.19 +    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    1.20 +  </properties>
    1.21 +  <dependencies>
    1.22 +    <dependency>
    1.23 +      <groupId>junit</groupId>
    1.24 +      <artifactId>junit</artifactId>
    1.25 +      <version>3.8.1</version>
    1.26 +      <scope>test</scope>
    1.27 +    </dependency>
    1.28 +  </dependencies>
    1.29 +</project>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/core/src/main/java/org/apidesign/bck2brwsr/core/ExtraJavaScript.java	Wed Oct 03 08:05:24 2012 -0700
     2.3 @@ -0,0 +1,19 @@
     2.4 +package org.apidesign.bck2brwsr.core;
     2.5 +
     2.6 +import java.lang.annotation.ElementType;
     2.7 +import java.lang.annotation.Retention;
     2.8 +import java.lang.annotation.RetentionPolicy;
     2.9 +import java.lang.annotation.Target;
    2.10 +
    2.11 +/**
    2.12 + *
    2.13 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.14 + */
    2.15 +@Retention(RetentionPolicy.CLASS)
    2.16 +@Target(ElementType.TYPE)
    2.17 +public @interface ExtraJavaScript {
    2.18 +    /** location of a script to load */
    2.19 +    String resource();
    2.20 +    /** should the class file still be processed or not? */
    2.21 +    boolean processByteCode() default true;
    2.22 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/core/src/main/java/org/apidesign/bck2brwsr/core/NoJavaScript.java	Wed Oct 03 08:05:24 2012 -0700
     3.3 @@ -0,0 +1,16 @@
     3.4 +package org.apidesign.bck2brwsr.core;
     3.5 +
     3.6 +import java.lang.annotation.ElementType;
     3.7 +import java.lang.annotation.Retention;
     3.8 +import java.lang.annotation.RetentionPolicy;
     3.9 +import java.lang.annotation.Target;
    3.10 +
    3.11 +/** Don't include given field or method in generated JavaScript.
    3.12 + *
    3.13 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.14 + */
    3.15 +@Retention(RetentionPolicy.CLASS)
    3.16 +@Target({ ElementType.METHOD, ElementType.FIELD })
    3.17 +public @interface NoJavaScript {
    3.18 +    
    3.19 +}
     4.1 --- a/pom.xml	Sun Sep 30 19:40:04 2012 -0700
     4.2 +++ b/pom.xml	Wed Oct 03 08:05:24 2012 -0700
     4.3 @@ -9,6 +9,7 @@
     4.4    <modules>
     4.5        <module>vm</module>
     4.6      <module>htmlpage</module>
     4.7 +    <module>core</module>
     4.8    </modules>
     4.9    <licenses>
    4.10        <license>
     5.1 --- a/vm/pom.xml	Sun Sep 30 19:40:04 2012 -0700
     5.2 +++ b/vm/pom.xml	Wed Oct 03 08:05:24 2012 -0700
     5.3 @@ -86,5 +86,11 @@
     5.4        <artifactId>org-netbeans-modules-classfile</artifactId>
     5.5        <type>jar</type>
     5.6      </dependency>
     5.7 +    <dependency>
     5.8 +      <groupId>org.apidesign.bck2brwsr</groupId>
     5.9 +      <artifactId>core</artifactId>
    5.10 +      <version>1.0-SNAPSHOT</version>
    5.11 +      <type>jar</type>
    5.12 +    </dependency>
    5.13    </dependencies>
    5.14  </project>
     6.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sun Sep 30 19:40:04 2012 -0700
     6.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Wed Oct 03 08:05:24 2012 -0700
     6.3 @@ -22,6 +22,9 @@
     6.4  import java.util.ArrayList;
     6.5  import java.util.Collection;
     6.6  import java.util.List;
     6.7 +import org.apidesign.bck2brwsr.core.ExtraJavaScript;
     6.8 +import org.netbeans.modules.classfile.Annotation;
     6.9 +import org.netbeans.modules.classfile.AnnotationComponent;
    6.10  import static org.netbeans.modules.classfile.ByteCodes.*;
    6.11  import org.netbeans.modules.classfile.CPClassInfo;
    6.12  import org.netbeans.modules.classfile.CPEntry;
    6.13 @@ -31,8 +34,10 @@
    6.14  import org.netbeans.modules.classfile.ClassFile;
    6.15  import org.netbeans.modules.classfile.ClassName;
    6.16  import org.netbeans.modules.classfile.Code;
    6.17 +import org.netbeans.modules.classfile.ElementValue;
    6.18  import org.netbeans.modules.classfile.Method;
    6.19  import org.netbeans.modules.classfile.Parameter;
    6.20 +import org.netbeans.modules.classfile.PrimitiveElementValue;
    6.21  import org.netbeans.modules.classfile.Variable;
    6.22  
    6.23  /** Translator of the code inside class files to JavaScript.
    6.24 @@ -62,14 +67,29 @@
    6.25       *   generated JavaScript code works properly. The names are in internal 
    6.26       *   JVM form so String is <code>java/lang/String</code>. Can be <code>null</code>
    6.27       *   if one is not interested in knowing references
    6.28 +     * @param scripts write only collection with names of resources to read
    6.29 +     * 
    6.30       * @throws IOException if something goes wrong during read or write or translating
    6.31       */
    6.32      
    6.33      public static void compile(
    6.34          InputStream classFile, Appendable out,
    6.35 -        Collection<? super String> references
    6.36 +        Collection<? super String> references,
    6.37 +        Collection<? super String> scripts
    6.38      ) throws IOException {
    6.39          ClassFile jc = new ClassFile(classFile, true);
    6.40 +        final ClassName extraAnn = ClassName.getClassName(ExtraJavaScript.class.getName().replace('.', '/'));
    6.41 +        Annotation a = jc.getAnnotation(extraAnn);
    6.42 +        if (a != null) {
    6.43 +            final ElementValue annVal = a.getComponent("resource").getValue();
    6.44 +            String res = ((PrimitiveElementValue)annVal).getValue().getValue().toString();
    6.45 +            scripts.add(res);
    6.46 +            final AnnotationComponent process = a.getComponent("processByteCode");
    6.47 +            if (process != null && "false".equals(process.getValue().toString())) {
    6.48 +                return;
    6.49 +            }
    6.50 +        }
    6.51 +        
    6.52          ByteCodeToJavaScript compiler = new ByteCodeToJavaScript(
    6.53              jc, out, references
    6.54          );
     7.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java	Sun Sep 30 19:40:04 2012 -0700
     7.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java	Wed Oct 03 08:05:24 2012 -0700
     7.3 @@ -48,24 +48,18 @@
     7.4              }
     7.5              if (name.startsWith("java/") 
     7.6                  && !name.equals("java/lang/Object")
     7.7 -                && !name.equals("java/lang/String")
     7.8                  && !name.equals("java/lang/StringBuilder")
     7.9                  && !name.equals("java/lang/AbstractStringBuilder")
    7.10              ) {
    7.11                  continue;
    7.12              }
    7.13 -            InputStream emul = GenJS.class.getResourceAsStream("emulation/" + name.replace('/', '_') + ".js");
    7.14 -            if (emul != null) {
    7.15 -                readResource(emul, out);
    7.16 -                continue;
    7.17 -            }
    7.18 -            
    7.19              InputStream is = GenJS.class.getClassLoader().getResourceAsStream(name + ".class");
    7.20              if (is == null) {
    7.21                  throw new IOException("Can't find class " + name); 
    7.22              }
    7.23 +            LinkedList<String> scripts = new LinkedList<String>();
    7.24              try {
    7.25 -                ByteCodeToJavaScript.compile(is, out, toProcess);
    7.26 +                ByteCodeToJavaScript.compile(is, out, toProcess, scripts);
    7.27              } catch (RuntimeException ex) {
    7.28                  if (out instanceof CharSequence) {
    7.29                      CharSequence seq = (CharSequence)out;
    7.30 @@ -84,6 +78,13 @@
    7.31                      );
    7.32                  }
    7.33              }
    7.34 +            for (String resource : scripts) {
    7.35 +                InputStream emul = GenJS.class.getResourceAsStream(resource);
    7.36 +                if (emul == null) {
    7.37 +                    throw new IOException("Can't find " + resource);
    7.38 +                }
    7.39 +                readResource(emul, out);
    7.40 +            }
    7.41          }
    7.42      }
    7.43      private static void readResource(InputStream emul, Appendable out) throws IOException {
     8.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/StringSample.java	Sun Sep 30 19:40:04 2012 -0700
     8.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StringSample.java	Wed Oct 03 08:05:24 2012 -0700
     8.3 @@ -1,9 +1,12 @@
     8.4  package org.apidesign.vm4brwsr;
     8.5  
     8.6 +import org.apidesign.bck2brwsr.core.ExtraJavaScript;
     8.7 +
     8.8  /**
     8.9   *
    8.10   * @author Jaroslav Tulach <jtulach@netbeans.org>
    8.11   */
    8.12 +@ExtraJavaScript(resource="/org/apidesign/vm4brwsr/emulation/java_lang_String.js")
    8.13  public class StringSample {
    8.14      public static final String HELLO = "Hello World!";
    8.15      private static int counter;