1.1 --- a/.hgtags Tue Mar 05 07:57:16 2013 +0100
1.2 +++ b/.hgtags Fri May 17 10:27:41 2013 +0200
1.3 @@ -1,2 +1,12 @@
1.4 0a115f1c6f3c70458fc479ae82b4d7fcdeb7e95a jdk7-b147_base
1.5 7367a296a9ec4a88e0292a41244c96283818e563 bck2brwsr-0.3
1.6 +caf1e66268fd4100d57922d973ae09a6bf3be847 release-${releaseVersion}
1.7 +30e9ac29654fba6f67d0921e7e3aa21133442592 release-0.5
1.8 +caf1e66268fd4100d57922d973ae09a6bf3be847 release-0.4
1.9 +caf1e66268fd4100d57922d973ae09a6bf3be847 release-${releaseVersion}
1.10 +0000000000000000000000000000000000000000 release-${releaseVersion}
1.11 +52a4a5f868bccc67d50ad17f793b9ebabdf75d88 release-0.6
1.12 +6792dc0bafb9c76a099e45bfc9e967d6a2823827 release-0.7
1.13 +623816269b75e53fffb4b19960df7040a3c20056 release-0.7
1.14 +23572dc719bd630817d11eaabdee4565f63ef8e1 release-0.7.1
1.15 +56abd247f421febd8b2c5e59d666968692e11555 release-0.7.2
2.1 --- a/benchmarks/matrix-multiplication/pom.xml Tue Mar 05 07:57:16 2013 +0100
2.2 +++ b/benchmarks/matrix-multiplication/pom.xml Fri May 17 10:27:41 2013 +0200
2.3 @@ -4,12 +4,12 @@
2.4
2.5 <groupId>org.apidesign.bck2brwsr</groupId>
2.6 <artifactId>matrix.multiplication</artifactId>
2.7 - <version>0.4-SNAPSHOT</version>
2.8 + <version>0.8-SNAPSHOT</version>
2.9 <packaging>jar</packaging>
2.10 <parent>
2.11 <artifactId>benchmarks</artifactId>
2.12 <groupId>org.apidesign.bck2brwsr</groupId>
2.13 - <version>0.4-SNAPSHOT</version>
2.14 + <version>0.8-SNAPSHOT</version>
2.15 </parent>
2.16
2.17 <name>Matrix multiplication</name>
2.18 @@ -37,6 +37,36 @@
2.19 <skip>true</skip>
2.20 </configuration>
2.21 </plugin>
2.22 + <plugin>
2.23 + <groupId>org.codehaus.mojo</groupId>
2.24 + <artifactId>xml-maven-plugin</artifactId>
2.25 + <version>1.0</version>
2.26 + <executions>
2.27 + <execution>
2.28 + <goals>
2.29 + <goal>transform</goal>
2.30 + </goals>
2.31 + <phase>install</phase>
2.32 + </execution>
2.33 + </executions>
2.34 + <configuration>
2.35 + <transformationSets>
2.36 + <transformationSet>
2.37 + <dir>target/surefire-reports</dir>
2.38 + <outputDir>target/surefire-reports</outputDir>
2.39 + <includes>
2.40 + <include>TEST*.xml</include>
2.41 + </includes>
2.42 + <stylesheet>src/main/select-time.xsl</stylesheet>
2.43 + <fileMappers>
2.44 + <fileMapper implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
2.45 + <targetExtension>.csv</targetExtension>
2.46 + </fileMapper>
2.47 + </fileMappers>
2.48 + </transformationSet>
2.49 + </transformationSets>
2.50 + </configuration>
2.51 + </plugin>
2.52 </plugins>
2.53 </build>
2.54
2.55 @@ -44,7 +74,7 @@
2.56 <dependency>
2.57 <groupId>org.apidesign.bck2brwsr</groupId>
2.58 <artifactId>emul.mini</artifactId>
2.59 - <version>0.4-SNAPSHOT</version>
2.60 + <version>0.8-SNAPSHOT</version>
2.61 </dependency>
2.62 <dependency>
2.63 <groupId>org.testng</groupId>
2.64 @@ -61,7 +91,13 @@
2.65 <dependency>
2.66 <groupId>org.apidesign.bck2brwsr</groupId>
2.67 <artifactId>vmtest</artifactId>
2.68 - <version>0.4-SNAPSHOT</version>
2.69 + <version>0.8-SNAPSHOT</version>
2.70 + <scope>test</scope>
2.71 + </dependency>
2.72 + <dependency>
2.73 + <groupId>org.apidesign.bck2brwsr</groupId>
2.74 + <artifactId>launcher.http</artifactId>
2.75 + <version>0.8-SNAPSHOT</version>
2.76 <scope>test</scope>
2.77 </dependency>
2.78 </dependencies>
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/benchmarks/matrix-multiplication/src/main/select-time.xsl Fri May 17 10:27:41 2013 +0200
3.3 @@ -0,0 +1,55 @@
3.4 +<?xml version="1.0" encoding="UTF-8"?>
3.5 +<!--
3.6 +
3.7 + Back 2 Browser Bytecode Translator
3.8 + Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
3.9 +
3.10 + This program is free software: you can redistribute it and/or modify
3.11 + it under the terms of the GNU General Public License as published by
3.12 + the Free Software Foundation, version 2 of the License.
3.13 +
3.14 + This program is distributed in the hope that it will be useful,
3.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
3.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.17 + GNU General Public License for more details.
3.18 +
3.19 + You should have received a copy of the GNU General Public License
3.20 + along with this program. Look for COPYING file in the top folder.
3.21 + If not, see http://opensource.org/licenses/GPL-2.0.
3.22 +
3.23 +-->
3.24 +
3.25 +<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
3.26 + <xsl:output method="text"/>
3.27 +
3.28 + <xsl:template match="/">
3.29 + <xsl:apply-templates mode="header" select="testsuite/testcase"/><xsl:text>End
3.30 +</xsl:text>
3.31 + <xsl:apply-templates mode="value" select="testsuite/testcase"/><xsl:text>NaN
3.32 +</xsl:text>
3.33 + </xsl:template>
3.34 +
3.35 +
3.36 + <xsl:template match="testcase" mode="header">
3.37 + <xsl:if test="contains(@name,'tenThousand')">
3.38 + <xsl:if test="not(contains(@name, '[Java]'))">
3.39 + <xsl:if test="not(contains(@name, '[Compare'))">
3.40 + <xsl:value-of select="@name"/>
3.41 + <xsl:text>,</xsl:text>
3.42 + </xsl:if>
3.43 + </xsl:if>
3.44 + </xsl:if>
3.45 + </xsl:template>
3.46 +
3.47 + <xsl:template match="testcase" mode="value">
3.48 + <xsl:if test="contains(@name,'tenThousand')">
3.49 + <xsl:if test="not(contains(@name, '[Java]'))">
3.50 + <xsl:if test="not(contains(@name, '[Compare'))">
3.51 + <xsl:value-of select="@time"/>
3.52 + <xsl:text>,</xsl:text>
3.53 + </xsl:if>
3.54 + </xsl:if>
3.55 + </xsl:if>
3.56 + </xsl:template>
3.57 +
3.58 +</xsl:stylesheet>
4.1 --- a/benchmarks/matrix-multiplication/src/test/java/org/apidesign/benchmark/matrixmul/MatrixTest.java Tue Mar 05 07:57:16 2013 +0100
4.2 +++ b/benchmarks/matrix-multiplication/src/test/java/org/apidesign/benchmark/matrixmul/MatrixTest.java Fri May 17 10:27:41 2013 +0200
4.3 @@ -31,6 +31,22 @@
4.4 }
4.5
4.6 @Compare(scripting = false)
4.7 + public String oneIteration() throws IOException {
4.8 +
4.9 + Matrix m1 = new Matrix(5);
4.10 + Matrix m2 = new Matrix(5);
4.11 +
4.12 + m1.generateData();
4.13 + m2.generateData();
4.14 +
4.15 + Matrix res = m1.multiply(m2);
4.16 +
4.17 + StringBuilder sb = new StringBuilder();
4.18 + res.printOn(sb);
4.19 + return sb.toString();
4.20 + }
4.21 +
4.22 + @Compare(scripting = false)
4.23 public String tenThousandIterations() throws IOException {
4.24
4.25 Matrix m1 = new Matrix(5);
4.26 @@ -50,6 +66,27 @@
4.27 return sb.toString();
4.28 }
4.29
4.30 + @Compare(scripting = false)
4.31 + public String tenUselessIterations() throws IOException {
4.32 +
4.33 + Matrix m1 = new Matrix(5);
4.34 + Matrix m2 = new Matrix(5);
4.35 +
4.36 + m1.generateData();
4.37 + m2.generateData();
4.38 +
4.39 + Matrix res = null;
4.40 + for (int i = 0; i < 10; i++) {
4.41 + res = m1.multiply(m2);
4.42 + m1 = res;
4.43 + }
4.44 +
4.45 + StringBuilder sb = new StringBuilder();
4.46 + res.printOn(sb);
4.47 + return sb.toString();
4.48 + }
4.49 +
4.50 +
4.51 @Factory
4.52 public static Object[] create() {
4.53 return VMTest.create(MatrixTest.class);
5.1 --- a/benchmarks/pom.xml Tue Mar 05 07:57:16 2013 +0100
5.2 +++ b/benchmarks/pom.xml Fri May 17 10:27:41 2013 +0200
5.3 @@ -4,11 +4,11 @@
5.4 <parent>
5.5 <artifactId>bck2brwsr</artifactId>
5.6 <groupId>org.apidesign</groupId>
5.7 - <version>0.4-SNAPSHOT</version>
5.8 + <version>0.8-SNAPSHOT</version>
5.9 </parent>
5.10 <groupId>org.apidesign.bck2brwsr</groupId>
5.11 <artifactId>benchmarks</artifactId>
5.12 - <version>0.4-SNAPSHOT</version>
5.13 + <version>0.8-SNAPSHOT</version>
5.14 <packaging>pom</packaging>
5.15 <name>Performance benchmarks</name>
5.16 <modules>
6.1 --- a/dew/pom.xml Tue Mar 05 07:57:16 2013 +0100
6.2 +++ b/dew/pom.xml Fri May 17 10:27:41 2013 +0200
6.3 @@ -4,11 +4,11 @@
6.4 <parent>
6.5 <groupId>org.apidesign</groupId>
6.6 <artifactId>bck2brwsr</artifactId>
6.7 - <version>0.4-SNAPSHOT</version>
6.8 + <version>0.8-SNAPSHOT</version>
6.9 </parent>
6.10 <groupId>org.apidesign.bck2brwsr</groupId>
6.11 <artifactId>dew</artifactId>
6.12 - <version>0.4-SNAPSHOT</version>
6.13 + <version>0.8-SNAPSHOT</version>
6.14 <name>Development Environment for Web</name>
6.15 <url>http://maven.apache.org</url>
6.16 <build>
6.17 @@ -42,6 +42,14 @@
6.18 </arguments>
6.19 </configuration>
6.20 </plugin>
6.21 + <plugin>
6.22 + <groupId>org.apache.maven.plugins</groupId>
6.23 + <artifactId>maven-deploy-plugin</artifactId>
6.24 + <version>2.7</version>
6.25 + <configuration>
6.26 + <skip>true</skip>
6.27 + </configuration>
6.28 + </plugin>
6.29 </plugins>
6.30 </build>
6.31 <properties>
7.1 --- a/ide/editor/pom.xml Tue Mar 05 07:57:16 2013 +0100
7.2 +++ b/ide/editor/pom.xml Fri May 17 10:27:41 2013 +0200
7.3 @@ -4,19 +4,18 @@
7.4 <parent>
7.5 <artifactId>ide</artifactId>
7.6 <groupId>org.apidesign.bck2brwsr</groupId>
7.7 - <version>0.4-SNAPSHOT</version>
7.8 + <version>0.8-SNAPSHOT</version>
7.9 </parent>
7.10
7.11 - <groupId>org.apidesign.bck2brwsr.ide.editor</groupId>
7.12 + <groupId>org.apidesign.bck2brwsr.ide</groupId>
7.13 <artifactId>editor</artifactId>
7.14 - <version>0.4-SNAPSHOT</version>
7.15 + <version>0.8-SNAPSHOT</version>
7.16 <packaging>nbm</packaging>
7.17
7.18 <name>Editor Support for Bck2Brwsr</name>
7.19
7.20 <properties>
7.21 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
7.22 - <netbeans.version>RELEASE72</netbeans.version>
7.23 <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
7.24 </properties>
7.25
7.26 @@ -40,71 +39,59 @@
7.27 <dependency>
7.28 <groupId>org.netbeans.api</groupId>
7.29 <artifactId>org-netbeans-api-annotations-common</artifactId>
7.30 - <version>${netbeans.version}</version>
7.31 </dependency>
7.32 <dependency>
7.33 <groupId>org.netbeans.api</groupId>
7.34 <artifactId>org-netbeans-modules-java-source</artifactId>
7.35 - <version>${netbeans.version}</version>
7.36 </dependency>
7.37 <dependency>
7.38 <groupId>org.netbeans.api</groupId>
7.39 <artifactId>org-netbeans-libs-javacapi</artifactId>
7.40 - <version>${netbeans.version}</version>
7.41 </dependency>
7.42 <dependency>
7.43 <groupId>org.netbeans.api</groupId>
7.44 <artifactId>org-netbeans-spi-java-hints</artifactId>
7.45 - <version>${netbeans.version}</version>
7.46 </dependency>
7.47 <dependency>
7.48 <groupId>org.netbeans.api</groupId>
7.49 <artifactId>org-netbeans-modules-parsing-api</artifactId>
7.50 - <version>${netbeans.version}</version>
7.51 </dependency>
7.52 <dependency>
7.53 <groupId>org.netbeans.api</groupId>
7.54 <artifactId>org-netbeans-spi-editor-hints</artifactId>
7.55 - <version>${netbeans.version}</version>
7.56 </dependency>
7.57 <dependency>
7.58 <groupId>org.netbeans.api</groupId>
7.59 <artifactId>org-openide-util</artifactId>
7.60 - <version>${netbeans.version}</version>
7.61 </dependency>
7.62 <dependency>
7.63 <groupId>org.netbeans.api</groupId>
7.64 <artifactId>org-netbeans-modules-java-lexer</artifactId>
7.65 - <version>${netbeans.version}</version>
7.66 </dependency>
7.67 <dependency>
7.68 <groupId>org.netbeans.api</groupId>
7.69 <artifactId>org-netbeans-modules-lexer</artifactId>
7.70 - <version>${netbeans.version}</version>
7.71 </dependency>
7.72 <dependency>
7.73 <groupId>org.apidesign.bck2brwsr</groupId>
7.74 <artifactId>core</artifactId>
7.75 - <version>0.4-SNAPSHOT</version>
7.76 + <version>0.8-SNAPSHOT</version>
7.77 <type>jar</type>
7.78 <scope>test</scope>
7.79 </dependency>
7.80 <dependency>
7.81 <groupId>org.netbeans.api</groupId>
7.82 <artifactId>org-netbeans-modules-java-hints-test</artifactId>
7.83 - <version>${netbeans.version}</version>
7.84 <scope>test</scope>
7.85 </dependency>
7.86 <dependency>
7.87 <groupId>org.netbeans.api</groupId>
7.88 <artifactId>org-netbeans-libs-junit4</artifactId>
7.89 - <version>${netbeans.version}</version>
7.90 <scope>test</scope>
7.91 </dependency>
7.92 <dependency>
7.93 <groupId>org.netbeans.modules</groupId>
7.94 <artifactId>org-netbeans-lib-nbjavac</artifactId>
7.95 - <version>${netbeans.version}</version>
7.96 <scope>test</scope>
7.97 </dependency>
7.98 <dependency>
8.1 --- a/ide/pom.xml Tue Mar 05 07:57:16 2013 +0100
8.2 +++ b/ide/pom.xml Fri May 17 10:27:41 2013 +0200
8.3 @@ -4,14 +4,26 @@
8.4 <parent>
8.5 <artifactId>bck2brwsr</artifactId>
8.6 <groupId>org.apidesign</groupId>
8.7 - <version>0.4-SNAPSHOT</version>
8.8 + <version>0.8-SNAPSHOT</version>
8.9 </parent>
8.10 <groupId>org.apidesign.bck2brwsr</groupId>
8.11 <artifactId>ide</artifactId>
8.12 - <version>0.4-SNAPSHOT</version>
8.13 + <version>0.8-SNAPSHOT</version>
8.14 <packaging>pom</packaging>
8.15 <name>IDE Support</name>
8.16 <modules>
8.17 <module>editor</module>
8.18 </modules>
8.19 + <build>
8.20 + <plugins>
8.21 + <plugin>
8.22 + <groupId>org.apache.maven.plugins</groupId>
8.23 + <artifactId>maven-deploy-plugin</artifactId>
8.24 + <version>2.7</version>
8.25 + <configuration>
8.26 + <skip>true</skip>
8.27 + </configuration>
8.28 + </plugin>
8.29 + </plugins>
8.30 + </build>
8.31 </project>
9.1 --- a/javaquery/api/pom.xml Tue Mar 05 07:57:16 2013 +0100
9.2 +++ b/javaquery/api/pom.xml Fri May 17 10:27:41 2013 +0200
9.3 @@ -4,11 +4,11 @@
9.4 <parent>
9.5 <groupId>org.apidesign.bck2brwsr</groupId>
9.6 <artifactId>javaquery</artifactId>
9.7 - <version>0.4-SNAPSHOT</version>
9.8 + <version>0.8-SNAPSHOT</version>
9.9 </parent>
9.10 <groupId>org.apidesign.bck2brwsr</groupId>
9.11 <artifactId>javaquery.api</artifactId>
9.12 - <version>0.4-SNAPSHOT</version>
9.13 + <version>0.8-SNAPSHOT</version>
9.14 <name>JavaQuery API</name>
9.15 <url>http://maven.apache.org</url>
9.16 <build>
9.17 @@ -18,8 +18,8 @@
9.18 <artifactId>maven-compiler-plugin</artifactId>
9.19 <version>2.3.2</version>
9.20 <configuration>
9.21 - <source>1.6</source>
9.22 - <target>1.6</target>
9.23 + <source>1.7</source>
9.24 + <target>1.7</target>
9.25 </configuration>
9.26 </plugin>
9.27 <plugin>
9.28 @@ -73,5 +73,11 @@
9.29 <version>${project.version}</version>
9.30 <scope>test</scope>
9.31 </dependency>
9.32 + <dependency>
9.33 + <groupId>${project.groupId}</groupId>
9.34 + <artifactId>launcher.http</artifactId>
9.35 + <version>${project.version}</version>
9.36 + <scope>test</scope>
9.37 + </dependency>
9.38 </dependencies>
9.39 </project>
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java Fri May 17 10:27:41 2013 +0200
10.3 @@ -0,0 +1,155 @@
10.4 +/**
10.5 + * Back 2 Browser Bytecode Translator
10.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
10.7 + *
10.8 + * This program is free software: you can redistribute it and/or modify
10.9 + * it under the terms of the GNU General Public License as published by
10.10 + * the Free Software Foundation, version 2 of the License.
10.11 + *
10.12 + * This program is distributed in the hope that it will be useful,
10.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10.15 + * GNU General Public License for more details.
10.16 + *
10.17 + * You should have received a copy of the GNU General Public License
10.18 + * along with this program. Look for COPYING file in the top folder.
10.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
10.20 + */
10.21 +package org.apidesign.bck2brwsr.htmlpage;
10.22 +
10.23 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
10.24 +
10.25 +/**
10.26 + *
10.27 + * @author Jaroslav Tulach <jtulach@netbeans.org>
10.28 + */
10.29 +public final class ConvertTypes {
10.30 + ConvertTypes() {
10.31 + }
10.32 +
10.33 + public static String toString(Object object, String property) {
10.34 + Object ret = getProperty(object, property);
10.35 + return ret == null ? null : ret.toString();
10.36 + }
10.37 +
10.38 + public static double toDouble(Object object, String property) {
10.39 + Object ret = getProperty(object, property);
10.40 + return ret instanceof Number ? ((Number)ret).doubleValue() : Double.NaN;
10.41 + }
10.42 +
10.43 + public static int toInt(Object object, String property) {
10.44 + Object ret = getProperty(object, property);
10.45 + return ret instanceof Number ? ((Number)ret).intValue() : Integer.MIN_VALUE;
10.46 + }
10.47 +
10.48 + public static <T> T toModel(Class<T> modelClass, Object object, String property) {
10.49 + Object ret = getProperty(object, property);
10.50 + if (ret == null || modelClass.isInstance(ret)) {
10.51 + return modelClass.cast(ret);
10.52 + }
10.53 + throw new IllegalStateException("Value " + ret + " is not of type " + modelClass);
10.54 + }
10.55 +
10.56 + public static String toJSON(Object value) {
10.57 + if (value == null) {
10.58 + return "null";
10.59 + }
10.60 + if (value instanceof Enum) {
10.61 + value = value.toString();
10.62 + }
10.63 + if (value instanceof String) {
10.64 + return '"' +
10.65 + ((String)value).
10.66 + replace("\"", "\\\"").
10.67 + replace("\n", "\\n").
10.68 + replace("\r", "\\r").
10.69 + replace("\t", "\\t")
10.70 + + '"';
10.71 + }
10.72 + return value.toString();
10.73 + }
10.74 +
10.75 + @JavaScriptBody(args = { "object", "property" },
10.76 + body = "if (property === null) return object;\n"
10.77 + + "var p = object[property]; return p ? p : null;"
10.78 + )
10.79 + private static Object getProperty(Object object, String property) {
10.80 + return null;
10.81 + }
10.82 +
10.83 + public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
10.84 + int h = whenDone.hashCode();
10.85 + String name;
10.86 + for (;;) {
10.87 + name = "jsonp" + Integer.toHexString(h);
10.88 + if (defineIfUnused(name, jsonResult, whenDone)) {
10.89 + return name;
10.90 + }
10.91 + h++;
10.92 + }
10.93 + }
10.94 +
10.95 + @JavaScriptBody(args = { "name", "arr", "run" }, body =
10.96 + "if (window[name]) return false;\n "
10.97 + + "window[name] = function(data) {\n "
10.98 + + " delete window[name];\n"
10.99 + + " var el = window.document.getElementById(name);\n"
10.100 + + " el.parentNode.removeChild(el);\n"
10.101 + + " arr[0] = data;\n"
10.102 + + " run.run__V();\n"
10.103 + + "};\n"
10.104 + + "return true;\n"
10.105 + )
10.106 + private static boolean defineIfUnused(String name, Object[] arr, Runnable run) {
10.107 + return true;
10.108 + }
10.109 +
10.110 + @JavaScriptBody(args = { "url", "arr", "callback" }, body = ""
10.111 + + "var request = new XMLHttpRequest();\n"
10.112 + + "request.open('GET', url, true);\n"
10.113 + + "request.setRequestHeader('Content-Type', 'application/json; charset=utf-8');\n"
10.114 + + "request.onreadystatechange = function() {\n"
10.115 + + " if (this.readyState!==4) return;\n"
10.116 + + " try {\n"
10.117 + + " arr[0] = eval('(' + this.response + ')');\n"
10.118 + + " } catch (error) {;\n"
10.119 + + " throw 'Cannot parse' + error + ':' + this.response;\n"
10.120 + + " };\n"
10.121 + + " callback.run__V();\n"
10.122 + + "};"
10.123 + + "request.send();"
10.124 + )
10.125 + private static void loadJSON(
10.126 + String url, Object[] jsonResult, Runnable whenDone
10.127 + ) {
10.128 + }
10.129 +
10.130 + public static void loadJSON(
10.131 + String url, Object[] jsonResult, Runnable whenDone, String jsonp
10.132 + ) {
10.133 + if (jsonp == null) {
10.134 + loadJSON(url, jsonResult, whenDone);
10.135 + } else {
10.136 + loadJSONP(url, jsonp);
10.137 + }
10.138 + }
10.139 +
10.140 + @JavaScriptBody(args = { "url", "jsonp" }, body =
10.141 + "var scrpt = window.document.createElement('script');\n "
10.142 + + "scrpt.setAttribute('src', url);\n "
10.143 + + "scrpt.setAttribute('id', jsonp);\n "
10.144 + + "scrpt.setAttribute('type', 'text/javascript');\n "
10.145 + + "var body = document.getElementsByTagName('body')[0];\n "
10.146 + + "body.appendChild(scrpt);\n"
10.147 + )
10.148 + private static void loadJSONP(String url, String jsonp) {
10.149 +
10.150 + }
10.151 +
10.152 + public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
10.153 + for (int i = 0; i < props.length; i++) {
10.154 + values[i] = getProperty(jsonObject, props[i]);
10.155 + }
10.156 + }
10.157 +
10.158 +}
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOList.java Fri May 17 10:27:41 2013 +0200
11.3 @@ -0,0 +1,167 @@
11.4 +/**
11.5 + * Back 2 Browser Bytecode Translator
11.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
11.7 + *
11.8 + * This program is free software: you can redistribute it and/or modify
11.9 + * it under the terms of the GNU General Public License as published by
11.10 + * the Free Software Foundation, version 2 of the License.
11.11 + *
11.12 + * This program is distributed in the hope that it will be useful,
11.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11.15 + * GNU General Public License for more details.
11.16 + *
11.17 + * You should have received a copy of the GNU General Public License
11.18 + * along with this program. Look for COPYING file in the top folder.
11.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
11.20 + */
11.21 +package org.apidesign.bck2brwsr.htmlpage;
11.22 +
11.23 +import java.util.ArrayList;
11.24 +import java.util.Collection;
11.25 +import java.util.Iterator;
11.26 +import org.apidesign.bck2brwsr.core.JavaScriptOnly;
11.27 +
11.28 +/**
11.29 + *
11.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
11.31 + */
11.32 +public final class KOList<T> extends ArrayList<T> {
11.33 + private final String name;
11.34 + private final String[] deps;
11.35 + private Knockout model;
11.36 + private Runnable onchange;
11.37 +
11.38 + public KOList(String name, String... deps) {
11.39 + this.name = name;
11.40 + this.deps = deps;
11.41 + }
11.42 +
11.43 + public void assign(Knockout model) {
11.44 + if (this.model != model) {
11.45 + this.model = model;
11.46 + notifyChange();
11.47 + }
11.48 + }
11.49 +
11.50 + public KOList<T> onChange(Runnable r) {
11.51 + if (this.onchange != null) {
11.52 + throw new IllegalStateException();
11.53 + }
11.54 + this.onchange = r;
11.55 + return this;
11.56 + }
11.57 +
11.58 + @Override
11.59 + public boolean add(T e) {
11.60 + boolean ret = super.add(e);
11.61 + notifyChange();
11.62 + return ret;
11.63 + }
11.64 +
11.65 + @Override
11.66 + public boolean addAll(Collection<? extends T> c) {
11.67 + boolean ret = super.addAll(c);
11.68 + notifyChange();
11.69 + return ret;
11.70 + }
11.71 +
11.72 + @Override
11.73 + public boolean addAll(int index, Collection<? extends T> c) {
11.74 + boolean ret = super.addAll(index, c);
11.75 + notifyChange();
11.76 + return ret;
11.77 + }
11.78 +
11.79 + @Override
11.80 + public boolean remove(Object o) {
11.81 + boolean ret = super.remove(o);
11.82 + notifyChange();
11.83 + return ret;
11.84 + }
11.85 +
11.86 + @Override
11.87 + public void clear() {
11.88 + super.clear();
11.89 + notifyChange();
11.90 + }
11.91 +
11.92 + @Override
11.93 + public boolean removeAll(Collection<?> c) {
11.94 + boolean ret = super.removeAll(c);
11.95 + notifyChange();
11.96 + return ret;
11.97 + }
11.98 +
11.99 + @Override
11.100 + public boolean retainAll(Collection<?> c) {
11.101 + boolean ret = super.retainAll(c);
11.102 + notifyChange();
11.103 + return ret;
11.104 + }
11.105 +
11.106 + @Override
11.107 + public T set(int index, T element) {
11.108 + T ret = super.set(index, element);
11.109 + notifyChange();
11.110 + return ret;
11.111 + }
11.112 +
11.113 + @Override
11.114 + public void add(int index, T element) {
11.115 + super.add(index, element);
11.116 + notifyChange();
11.117 + }
11.118 +
11.119 + @Override
11.120 + public T remove(int index) {
11.121 + T ret = super.remove(index);
11.122 + notifyChange();
11.123 + return ret;
11.124 + }
11.125 +
11.126 + @Override
11.127 + public String toString() {
11.128 + Iterator<T> it = iterator();
11.129 + if (!it.hasNext()) {
11.130 + return "[]";
11.131 + }
11.132 + String sep = "";
11.133 + StringBuilder sb = new StringBuilder();
11.134 + sb.append('[');
11.135 + while (it.hasNext()) {
11.136 + T t = it.next();
11.137 + sb.append(sep);
11.138 + sb.append(ConvertTypes.toJSON(t));
11.139 + sep = ",";
11.140 + }
11.141 + sb.append(']');
11.142 + return sb.toString();
11.143 + }
11.144 +
11.145 +
11.146 + @JavaScriptOnly(name = "koArray", value = "function() { return this.toArray___3Ljava_lang_Object_2(); }")
11.147 + private static native int koArray();
11.148 +
11.149 + private void notifyChange() {
11.150 + Knockout m = model;
11.151 + if (m != null) {
11.152 + m.valueHasMutated(name);
11.153 + for (String dependant : deps) {
11.154 + m.valueHasMutated(dependant);
11.155 + }
11.156 + }
11.157 + Runnable r = onchange;
11.158 + if (r != null) {
11.159 + r.run();
11.160 + }
11.161 + }
11.162 +
11.163 + @Override
11.164 + public KOList clone() {
11.165 + KOList ko = (KOList)super.clone();
11.166 + ko.model = null;
11.167 + return ko;
11.168 + }
11.169 +
11.170 +}
12.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java Tue Mar 05 07:57:16 2013 +0100
12.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java Fri May 17 10:27:41 2013 +0200
12.3 @@ -18,6 +18,7 @@
12.4 package org.apidesign.bck2brwsr.htmlpage;
12.5
12.6 import java.lang.reflect.Method;
12.7 +import java.util.List;
12.8 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
12.9 import org.apidesign.bck2brwsr.core.JavaScriptBody;
12.10
12.11 @@ -29,38 +30,40 @@
12.12 public class Knockout {
12.13 /** used by tests */
12.14 static Knockout next;
12.15 -
12.16 - Knockout() {
12.17 + private final Object model;
12.18 +
12.19 + Knockout(Object model) {
12.20 + this.model = model == null ? this : model;
12.21 }
12.22
12.23 public static <M> Knockout applyBindings(
12.24 - Class<M> modelClass, M model, String[] propsGettersAndSetters
12.25 + Object model, String[] propsGettersAndSetters,
12.26 + String[] methodsAndSignatures
12.27 + ) {
12.28 + applyImpl(propsGettersAndSetters, model.getClass(), model, model, methodsAndSignatures);
12.29 + return new Knockout(model);
12.30 + }
12.31 + public static <M> Knockout applyBindings(
12.32 + Class<M> modelClass, M model, String[] propsGettersAndSetters,
12.33 + String[] methodsAndSignatures
12.34 ) {
12.35 Knockout bindings = next;
12.36 next = null;
12.37 if (bindings == null) {
12.38 - bindings = new Knockout();
12.39 + bindings = new Knockout(null);
12.40 }
12.41 - for (int i = 0; i < propsGettersAndSetters.length; i += 4) {
12.42 - try {
12.43 - Method getter = modelClass.getMethod(propsGettersAndSetters[i + 3]);
12.44 - bind(bindings, model, propsGettersAndSetters[i],
12.45 - propsGettersAndSetters[i + 1],
12.46 - propsGettersAndSetters[i + 2],
12.47 - getter.getReturnType().isPrimitive()
12.48 - );
12.49 - } catch (NoSuchMethodException ex) {
12.50 - throw new IllegalStateException(ex.getMessage());
12.51 - }
12.52 - }
12.53 + applyImpl(propsGettersAndSetters, modelClass, bindings, model, methodsAndSignatures);
12.54 applyBindings(bindings);
12.55 return bindings;
12.56 }
12.57
12.58 - @JavaScriptBody(args = { "prop" }, body =
12.59 - "this[prop].valueHasMutated();"
12.60 + public void valueHasMutated(String prop) {
12.61 + valueHasMutated(model, prop);
12.62 + }
12.63 + @JavaScriptBody(args = { "self", "prop" }, body =
12.64 + "self[prop].valueHasMutated();"
12.65 )
12.66 - public void valueHasMutated(String prop) {
12.67 + public void valueHasMutated(Object self, String prop) {
12.68 }
12.69
12.70
12.71 @@ -68,26 +71,60 @@
12.72 public static void triggerEvent(String id, String ev) {
12.73 }
12.74
12.75 - @JavaScriptBody(args = { "bindings", "model", "prop", "getter", "setter", "primitive" }, body =
12.76 + @JavaScriptBody(args = { "bindings", "model", "prop", "getter", "setter", "primitive", "array" }, body =
12.77 "var bnd = {\n"
12.78 - + " read: function() {\n"
12.79 + + " 'read': function() {\n"
12.80 + " var v = model[getter]();\n"
12.81 + + " if (array) v = v.koArray();\n"
12.82 + " return v;\n"
12.83 + " },\n"
12.84 - + " owner: bindings\n"
12.85 + + " 'owner': bindings\n"
12.86 + "};\n"
12.87 + "if (setter != null) {\n"
12.88 - + " bnd.write = function(val) {\n"
12.89 + + " bnd['write'] = function(val) {\n"
12.90 + " model[setter](primitive ? new Number(val) : val);\n"
12.91 + " };\n"
12.92 + "}\n"
12.93 - + "bindings[prop] = ko.computed(bnd);"
12.94 + + "bindings[prop] = ko['computed'](bnd);"
12.95 )
12.96 private static void bind(
12.97 - Object bindings, Object model, String prop, String getter, String setter, boolean primitive
12.98 + Object bindings, Object model, String prop, String getter, String setter, boolean primitive, boolean array
12.99 + ) {
12.100 + }
12.101 +
12.102 + @JavaScriptBody(args = { "bindings", "model", "prop", "sig" }, body =
12.103 + "bindings[prop] = function(data, ev) { model[sig](data, ev); };"
12.104 + )
12.105 + private static void expose(
12.106 + Object bindings, Object model, String prop, String sig
12.107 ) {
12.108 }
12.109
12.110 @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);")
12.111 private static void applyBindings(Object bindings) {}
12.112 +
12.113 + private static void applyImpl(
12.114 + String[] propsGettersAndSetters,
12.115 + Class<?> modelClass,
12.116 + Object bindings,
12.117 + Object model,
12.118 + String[] methodsAndSignatures
12.119 + ) throws IllegalStateException, SecurityException {
12.120 + for (int i = 0; i < propsGettersAndSetters.length; i += 4) {
12.121 + try {
12.122 + Method getter = modelClass.getMethod(propsGettersAndSetters[i + 3]);
12.123 + bind(bindings, model, propsGettersAndSetters[i],
12.124 + propsGettersAndSetters[i + 1],
12.125 + propsGettersAndSetters[i + 2],
12.126 + getter.getReturnType().isPrimitive(),
12.127 + List.class.isAssignableFrom(getter.getReturnType()));
12.128 + } catch (NoSuchMethodException ex) {
12.129 + throw new IllegalStateException(ex.getMessage());
12.130 + }
12.131 + }
12.132 + for (int i = 0; i < methodsAndSignatures.length; i += 2) {
12.133 + expose(
12.134 + bindings, model, methodsAndSignatures[i], methodsAndSignatures[i + 1]);
12.135 + }
12.136 + }
12.137 }
13.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Tue Mar 05 07:57:16 2013 +0100
13.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Fri May 17 10:27:41 2013 +0200
13.3 @@ -20,23 +20,30 @@
13.4 import java.io.IOException;
13.5 import java.io.InputStream;
13.6 import java.io.OutputStreamWriter;
13.7 +import java.io.StringWriter;
13.8 import java.io.Writer;
13.9 +import java.lang.annotation.AnnotationTypeMismatchException;
13.10 +import java.lang.annotation.IncompleteAnnotationException;
13.11 +import java.lang.reflect.Method;
13.12 import java.util.ArrayList;
13.13 import java.util.Collection;
13.14 import java.util.Collections;
13.15 import java.util.HashMap;
13.16 +import java.util.HashSet;
13.17 import java.util.LinkedHashSet;
13.18 import java.util.List;
13.19 -import java.util.Locale;
13.20 import java.util.Map;
13.21 import java.util.Set;
13.22 +import java.util.WeakHashMap;
13.23 import javax.annotation.processing.AbstractProcessor;
13.24 import javax.annotation.processing.Completion;
13.25 import javax.annotation.processing.Completions;
13.26 +import javax.annotation.processing.ProcessingEnvironment;
13.27 import javax.annotation.processing.Processor;
13.28 import javax.annotation.processing.RoundEnvironment;
13.29 import javax.annotation.processing.SupportedAnnotationTypes;
13.30 import javax.lang.model.element.AnnotationMirror;
13.31 +import javax.lang.model.element.AnnotationValue;
13.32 import javax.lang.model.element.Element;
13.33 import javax.lang.model.element.ElementKind;
13.34 import javax.lang.model.element.ExecutableElement;
13.35 @@ -44,13 +51,21 @@
13.36 import javax.lang.model.element.PackageElement;
13.37 import javax.lang.model.element.TypeElement;
13.38 import javax.lang.model.element.VariableElement;
13.39 +import javax.lang.model.type.ArrayType;
13.40 import javax.lang.model.type.MirroredTypeException;
13.41 +import javax.lang.model.type.TypeKind;
13.42 import javax.lang.model.type.TypeMirror;
13.43 +import javax.lang.model.util.Elements;
13.44 +import javax.lang.model.util.Types;
13.45 import javax.tools.Diagnostic;
13.46 import javax.tools.FileObject;
13.47 import javax.tools.StandardLocation;
13.48 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
13.49 +import org.apidesign.bck2brwsr.htmlpage.api.Model;
13.50 import org.apidesign.bck2brwsr.htmlpage.api.On;
13.51 +import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
13.52 +import org.apidesign.bck2brwsr.htmlpage.api.OnPropertyChange;
13.53 +import org.apidesign.bck2brwsr.htmlpage.api.OnReceive;
13.54 import org.apidesign.bck2brwsr.htmlpage.api.Page;
13.55 import org.apidesign.bck2brwsr.htmlpage.api.Property;
13.56 import org.openide.util.lookup.ServiceProvider;
13.57 @@ -62,89 +77,70 @@
13.58 */
13.59 @ServiceProvider(service=Processor.class)
13.60 @SupportedAnnotationTypes({
13.61 + "org.apidesign.bck2brwsr.htmlpage.api.Model",
13.62 "org.apidesign.bck2brwsr.htmlpage.api.Page",
13.63 + "org.apidesign.bck2brwsr.htmlpage.api.OnFunction",
13.64 + "org.apidesign.bck2brwsr.htmlpage.api.OnReceive",
13.65 + "org.apidesign.bck2brwsr.htmlpage.api.OnPropertyChange",
13.66 + "org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty",
13.67 "org.apidesign.bck2brwsr.htmlpage.api.On"
13.68 })
13.69 public final class PageProcessor extends AbstractProcessor {
13.70 + private final Map<Element,String> models = new WeakHashMap<>();
13.71 + private final Map<Element,Prprt[]> verify = new WeakHashMap<>();
13.72 @Override
13.73 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
13.74 - for (Element e : roundEnv.getElementsAnnotatedWith(Page.class)) {
13.75 - Page p = e.getAnnotation(Page.class);
13.76 - if (p == null) {
13.77 - continue;
13.78 - }
13.79 - PackageElement pe = (PackageElement)e.getEnclosingElement();
13.80 - String pkg = pe.getQualifiedName().toString();
13.81 -
13.82 - ProcessPage pp;
13.83 - try {
13.84 - InputStream is = openStream(pkg, p.xhtml());
13.85 - pp = ProcessPage.readPage(is);
13.86 - is.close();
13.87 - } catch (IOException iOException) {
13.88 - processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't read " + p.xhtml(), e);
13.89 - return false;
13.90 - }
13.91 - Writer w;
13.92 - String className = p.className();
13.93 - if (className.isEmpty()) {
13.94 - int indx = p.xhtml().indexOf('.');
13.95 - className = p.xhtml().substring(0, indx);
13.96 - }
13.97 - try {
13.98 - FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e);
13.99 - w = new OutputStreamWriter(java.openOutputStream());
13.100 - try {
13.101 - w.append("package " + pkg + ";\n");
13.102 - w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
13.103 - w.append("final class ").append(className).append(" {\n");
13.104 - w.append(" private boolean locked;\n");
13.105 - if (!initializeOnClick(className, (TypeElement) e, w, pp)) {
13.106 - return false;
13.107 - }
13.108 - for (String id : pp.ids()) {
13.109 - String tag = pp.tagNameForId(id);
13.110 - String type = type(tag);
13.111 - w.append(" ").append("public final ").
13.112 - append(type).append(' ').append(cnstnt(id)).append(" = new ").
13.113 - append(type).append("(\"").append(id).append("\");\n");
13.114 - }
13.115 - List<String> propsGetSet = new ArrayList<String>();
13.116 - Map<String,Collection<String>> propsDeps = new HashMap<String, Collection<String>>();
13.117 - generateComputedProperties(w, e.getEnclosedElements(), propsGetSet, propsDeps);
13.118 - generateProperties(w, p.properties(), propsGetSet, propsDeps);
13.119 - w.append(" private org.apidesign.bck2brwsr.htmlpage.Knockout ko;\n");
13.120 - if (!propsGetSet.isEmpty()) {
13.121 - w.write("public " + className + " applyBindings() {\n");
13.122 - w.write(" ko = org.apidesign.bck2brwsr.htmlpage.Knockout.applyBindings(");
13.123 - w.write(className + ".class, this, ");
13.124 - w.write("new String[] {\n");
13.125 - String sep = "";
13.126 - for (String n : propsGetSet) {
13.127 - w.write(sep);
13.128 - if (n == null) {
13.129 - w.write(" null");
13.130 - } else {
13.131 - w.write(" \"" + n + "\"");
13.132 - }
13.133 - sep = ",\n";
13.134 - }
13.135 - w.write("\n });\n return this;\n}\n");
13.136 -
13.137 - w.write("public void triggerEvent(Element e, OnEvent ev) {\n");
13.138 - w.write(" org.apidesign.bck2brwsr.htmlpage.Knockout.triggerEvent(e.getId(), ev.getElementPropertyName());\n");
13.139 - w.write("}\n");
13.140 - }
13.141 - w.append("}\n");
13.142 - } finally {
13.143 - w.close();
13.144 - }
13.145 - } catch (IOException ex) {
13.146 - processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't create " + className + ".java", e);
13.147 - return false;
13.148 + boolean ok = true;
13.149 + for (Element e : roundEnv.getElementsAnnotatedWith(Model.class)) {
13.150 + if (!processModel(e)) {
13.151 + ok = false;
13.152 }
13.153 }
13.154 - return true;
13.155 + for (Element e : roundEnv.getElementsAnnotatedWith(Page.class)) {
13.156 + if (!processPage(e)) {
13.157 + ok = false;
13.158 + }
13.159 + }
13.160 + if (roundEnv.processingOver()) {
13.161 + models.clear();
13.162 + for (Map.Entry<Element, Prprt[]> entry : verify.entrySet()) {
13.163 + TypeElement te = (TypeElement)entry.getKey();
13.164 + String fqn = processingEnv.getElementUtils().getBinaryName(te).toString();
13.165 + Element finalElem = processingEnv.getElementUtils().getTypeElement(fqn);
13.166 + if (finalElem == null) {
13.167 + continue;
13.168 + }
13.169 + Prprt[] props;
13.170 + Model m = finalElem.getAnnotation(Model.class);
13.171 + if (m != null) {
13.172 + props = Prprt.wrap(processingEnv, finalElem, m.properties());
13.173 + } else {
13.174 + Page p = finalElem.getAnnotation(Page.class);
13.175 + props = Prprt.wrap(processingEnv, finalElem, p.properties());
13.176 + }
13.177 + for (Prprt p : props) {
13.178 + boolean[] isModel = { false };
13.179 + boolean[] isEnum = { false };
13.180 + boolean[] isPrimitive = { false };
13.181 + String t = checkType(p, isModel, isEnum, isPrimitive);
13.182 + if (isEnum[0]) {
13.183 + continue;
13.184 + }
13.185 + if (isPrimitive[0]) {
13.186 + continue;
13.187 + }
13.188 + if (isModel[0]) {
13.189 + continue;
13.190 + }
13.191 + if ("java.lang.String".equals(t)) {
13.192 + continue;
13.193 + }
13.194 + error("The type " + t + " should be defined by @Model annotation", entry.getKey());
13.195 + }
13.196 + }
13.197 + verify.clear();
13.198 + }
13.199 + return ok;
13.200 }
13.201
13.202 private InputStream openStream(String pkg, String name) throws IOException {
13.203 @@ -157,6 +153,236 @@
13.204 }
13.205 }
13.206
13.207 + private void error(String msg, Element e) {
13.208 + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, msg, e);
13.209 + }
13.210 +
13.211 + private boolean processModel(Element e) {
13.212 + boolean ok = true;
13.213 + Model m = e.getAnnotation(Model.class);
13.214 + if (m == null) {
13.215 + return true;
13.216 + }
13.217 + String pkg = findPkgName(e);
13.218 + Writer w;
13.219 + String className = m.className();
13.220 + models.put(e, className);
13.221 + try {
13.222 + StringWriter body = new StringWriter();
13.223 + List<String> propsGetSet = new ArrayList<>();
13.224 + List<String> functions = new ArrayList<>();
13.225 + Map<String, Collection<String>> propsDeps = new HashMap<>();
13.226 + Map<String, Collection<String>> functionDeps = new HashMap<>();
13.227 + Prprt[] props = createProps(e, m.properties());
13.228 +
13.229 + if (!generateComputedProperties(body, props, e.getEnclosedElements(), propsGetSet, propsDeps)) {
13.230 + ok = false;
13.231 + }
13.232 + if (!generateOnChange(e, propsDeps, props, className, functionDeps)) {
13.233 + ok = false;
13.234 + }
13.235 + if (!generateProperties(e, body, props, propsGetSet, propsDeps, functionDeps)) {
13.236 + ok = false;
13.237 + }
13.238 + if (!generateFunctions(e, body, className, e.getEnclosedElements(), functions)) {
13.239 + ok = false;
13.240 + }
13.241 + FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e);
13.242 + w = new OutputStreamWriter(java.openOutputStream());
13.243 + try {
13.244 + w.append("package " + pkg + ";\n");
13.245 + w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
13.246 + w.append("import org.apidesign.bck2brwsr.htmlpage.KOList;\n");
13.247 + w.append("import org.apidesign.bck2brwsr.core.JavaScriptOnly;\n");
13.248 + w.append("final class ").append(className).append(" implements Cloneable {\n");
13.249 + w.append(" private boolean locked;\n");
13.250 + w.append(" private org.apidesign.bck2brwsr.htmlpage.Knockout ko;\n");
13.251 + w.append(body.toString());
13.252 + w.append(" private static Class<" + inPckName(e) + "> modelFor() { return null; }\n");
13.253 + w.append(" public ").append(className).append("() {\n");
13.254 + w.append(" intKnckt();\n");
13.255 + w.append(" };\n");
13.256 + w.append(" private void intKnckt() {\n");
13.257 + w.append(" ko = org.apidesign.bck2brwsr.htmlpage.Knockout.applyBindings(this, ");
13.258 + writeStringArray(propsGetSet, w);
13.259 + w.append(", ");
13.260 + writeStringArray(functions, w);
13.261 + w.append(" );\n");
13.262 + w.append(" };\n");
13.263 + w.append(" ").append(className).append("(Object json) {\n");
13.264 + int values = 0;
13.265 + for (int i = 0; i < propsGetSet.size(); i += 4) {
13.266 + Prprt p = findPrprt(props, propsGetSet.get(i));
13.267 + if (p == null) {
13.268 + continue;
13.269 + }
13.270 + values++;
13.271 + }
13.272 + w.append(" Object[] ret = new Object[" + values + "];\n");
13.273 + w.append(" org.apidesign.bck2brwsr.htmlpage.ConvertTypes.extractJSON(json, new String[] {\n");
13.274 + for (int i = 0; i < propsGetSet.size(); i += 4) {
13.275 + Prprt p = findPrprt(props, propsGetSet.get(i));
13.276 + if (p == null) {
13.277 + continue;
13.278 + }
13.279 + w.append(" \"").append(propsGetSet.get(i)).append("\",\n");
13.280 + }
13.281 + w.append(" }, ret);\n");
13.282 + for (int i = 0, cnt = 0, prop = 0; i < propsGetSet.size(); i += 4) {
13.283 + final String pn = propsGetSet.get(i);
13.284 + Prprt p = findPrprt(props, pn);
13.285 + if (p == null) {
13.286 + continue;
13.287 + }
13.288 + boolean[] isModel = { false };
13.289 + boolean[] isEnum = { false };
13.290 + boolean isPrimitive[] = { false };
13.291 + String type = checkType(props[prop++], isModel, isEnum, isPrimitive);
13.292 + if (p.array()) {
13.293 + w.append("if (ret[" + cnt + "] instanceof Object[]) {\n");
13.294 + w.append(" for (Object e : ((Object[])ret[" + cnt + "])) {\n");
13.295 + if (isModel[0]) {
13.296 + w.append(" this.prop_").append(pn).append(".add(new ");
13.297 + w.append(type).append("(e));\n");
13.298 + } else if (isEnum[0]) {
13.299 + w.append(" this.prop_").append(pn);
13.300 + w.append(".add(e == null ? null : ");
13.301 + w.append(type).append(".valueOf((String)e));\n");
13.302 + } else {
13.303 + if (isPrimitive(type)) {
13.304 + w.append(" this.prop_").append(pn).append(".add(((Number)e).");
13.305 + w.append(type).append("Value());\n");
13.306 + } else {
13.307 + w.append(" this.prop_").append(pn).append(".add((");
13.308 + w.append(type).append(")e);\n");
13.309 + }
13.310 + }
13.311 + w.append(" }\n");
13.312 + w.append("}\n");
13.313 + } else {
13.314 + if (isEnum[0]) {
13.315 + w.append(" this.prop_").append(pn);
13.316 + w.append(" = ret[" + cnt + "] == null ? null : ");
13.317 + w.append(type).append(".valueOf((String)ret[" + cnt + "]);\n");
13.318 + } else if (isPrimitive(type)) {
13.319 + w.append(" this.prop_").append(pn);
13.320 + w.append(" = ((Number)").append("ret[" + cnt + "]).");
13.321 + w.append(type).append("Value();\n");
13.322 + } else {
13.323 + w.append(" this.prop_").append(pn);
13.324 + w.append(" = (").append(type).append(')');
13.325 + w.append("ret[" + cnt + "];\n");
13.326 + }
13.327 + }
13.328 + cnt++;
13.329 + }
13.330 + w.append(" intKnckt();\n");
13.331 + w.append(" };\n");
13.332 + writeToString(props, w);
13.333 + writeClone(className, props, w);
13.334 + w.append("}\n");
13.335 + } finally {
13.336 + w.close();
13.337 + }
13.338 + } catch (IOException ex) {
13.339 + error("Can't create " + className + ".java", e);
13.340 + return false;
13.341 + }
13.342 + return ok;
13.343 + }
13.344 +
13.345 + private boolean processPage(Element e) {
13.346 + boolean ok = true;
13.347 + Page p = e.getAnnotation(Page.class);
13.348 + if (p == null) {
13.349 + return true;
13.350 + }
13.351 + String pkg = findPkgName(e);
13.352 +
13.353 + ProcessPage pp;
13.354 + try (InputStream is = openStream(pkg, p.xhtml())) {
13.355 + pp = ProcessPage.readPage(is);
13.356 + is.close();
13.357 + } catch (IOException iOException) {
13.358 + error("Can't read " + p.xhtml() + " as " + iOException.getMessage(), e);
13.359 + ok = false;
13.360 + pp = null;
13.361 + }
13.362 + Writer w;
13.363 + String className = p.className();
13.364 + if (className.isEmpty()) {
13.365 + int indx = p.xhtml().indexOf('.');
13.366 + className = p.xhtml().substring(0, indx);
13.367 + }
13.368 + try {
13.369 + StringWriter body = new StringWriter();
13.370 + List<String> propsGetSet = new ArrayList<>();
13.371 + List<String> functions = new ArrayList<>();
13.372 + Map<String, Collection<String>> propsDeps = new HashMap<>();
13.373 + Map<String, Collection<String>> functionDeps = new HashMap<>();
13.374 +
13.375 + Prprt[] props = createProps(e, p.properties());
13.376 + if (!generateComputedProperties(body, props, e.getEnclosedElements(), propsGetSet, propsDeps)) {
13.377 + ok = false;
13.378 + }
13.379 + if (!generateOnChange(e, propsDeps, props, className, functionDeps)) {
13.380 + ok = false;
13.381 + }
13.382 + if (!generateProperties(e, body, props, propsGetSet, propsDeps, functionDeps)) {
13.383 + ok = false;
13.384 + }
13.385 + if (!generateFunctions(e, body, className, e.getEnclosedElements(), functions)) {
13.386 + ok = false;
13.387 + }
13.388 + if (!generateReceive(e, body, className, e.getEnclosedElements(), functions)) {
13.389 + ok = false;
13.390 + }
13.391 +
13.392 + FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e);
13.393 + w = new OutputStreamWriter(java.openOutputStream());
13.394 + try {
13.395 + w.append("package " + pkg + ";\n");
13.396 + w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
13.397 + w.append("import org.apidesign.bck2brwsr.htmlpage.KOList;\n");
13.398 + w.append("final class ").append(className).append(" {\n");
13.399 + w.append(" private boolean locked;\n");
13.400 + if (!initializeOnClick(className, (TypeElement) e, w, pp)) {
13.401 + ok = false;
13.402 + } else {
13.403 + if (pp != null) for (String id : pp.ids()) {
13.404 + String tag = pp.tagNameForId(id);
13.405 + String type = type(tag);
13.406 + w.append(" ").append("public final ").
13.407 + append(type).append(' ').append(cnstnt(id)).append(" = new ").
13.408 + append(type).append("(\"").append(id).append("\");\n");
13.409 + }
13.410 + }
13.411 + w.append(" private org.apidesign.bck2brwsr.htmlpage.Knockout ko;\n");
13.412 + w.append(body.toString());
13.413 + if (!propsGetSet.isEmpty()) {
13.414 + w.write("public " + className + " applyBindings() {\n");
13.415 + w.write(" ko = org.apidesign.bck2brwsr.htmlpage.Knockout.applyBindings(");
13.416 + w.write(className + ".class, this, ");
13.417 + writeStringArray(propsGetSet, w);
13.418 + w.append(", ");
13.419 + writeStringArray(functions, w);
13.420 + w.write(");\n return this;\n}\n");
13.421 +
13.422 + w.write("public void triggerEvent(Element e, OnEvent ev) {\n");
13.423 + w.write(" org.apidesign.bck2brwsr.htmlpage.Knockout.triggerEvent(e.getId(), ev.getElementPropertyName());\n");
13.424 + w.write("}\n");
13.425 + }
13.426 + w.append("}\n");
13.427 + } finally {
13.428 + w.close();
13.429 + }
13.430 + } catch (IOException ex) {
13.431 + error("Can't create " + className + ".java", e);
13.432 + return false;
13.433 + }
13.434 + return ok;
13.435 + }
13.436 +
13.437 private static String type(String tag) {
13.438 if (tag.equals("title")) {
13.439 return "Title";
13.440 @@ -177,12 +403,13 @@
13.441 }
13.442
13.443 private static String cnstnt(String id) {
13.444 - return id.toUpperCase(Locale.ENGLISH).replace('.', '_').replace('-', '_');
13.445 + return id.replace('.', '_').replace('-', '_');
13.446 }
13.447
13.448 private boolean initializeOnClick(
13.449 String className, TypeElement type, Writer w, ProcessPage pp
13.450 ) throws IOException {
13.451 + boolean ok = true;
13.452 TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType();
13.453 { //for (Element clazz : pe.getEnclosedElements()) {
13.454 // if (clazz.getKind() != ElementKind.CLASS) {
13.455 @@ -195,46 +422,27 @@
13.456 On oc = method.getAnnotation(On.class);
13.457 if (oc != null) {
13.458 for (String id : oc.id()) {
13.459 + if (pp == null) {
13.460 + error("id = " + id + " not found in HTML page.", method);
13.461 + ok = false;
13.462 + continue;
13.463 + }
13.464 if (pp.tagNameForId(id) == null) {
13.465 - processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "id = " + id + " does not exist in the HTML page. Found only " + pp.ids(), method);
13.466 - return false;
13.467 + error("id = " + id + " does not exist in the HTML page. Found only " + pp.ids(), method);
13.468 + ok = false;
13.469 + continue;
13.470 }
13.471 ExecutableElement ee = (ExecutableElement)method;
13.472 - StringBuilder params = new StringBuilder();
13.473 - {
13.474 - boolean first = true;
13.475 - for (VariableElement ve : ee.getParameters()) {
13.476 - if (!first) {
13.477 - params.append(", ");
13.478 - }
13.479 - first = false;
13.480 - if (ve.asType() == stringType) {
13.481 - params.append('"').append(id).append('"');
13.482 - continue;
13.483 - }
13.484 - String rn = ve.asType().toString();
13.485 - int last = rn.lastIndexOf('.');
13.486 - if (last >= 0) {
13.487 - rn = rn.substring(last + 1);
13.488 - }
13.489 - if (rn.equals(className)) {
13.490 - params.append(className).append(".this");
13.491 - continue;
13.492 - }
13.493 - processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
13.494 - "@On method can only accept String or " + className + " arguments",
13.495 - ee
13.496 - );
13.497 - return false;
13.498 - }
13.499 - }
13.500 + CharSequence params = wrapParams(ee, id, className, "ev", null);
13.501 if (!ee.getModifiers().contains(Modifier.STATIC)) {
13.502 - processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method has to be static", ee);
13.503 - return false;
13.504 + error("@On method has to be static", ee);
13.505 + ok = false;
13.506 + continue;
13.507 }
13.508 if (ee.getModifiers().contains(Modifier.PRIVATE)) {
13.509 - processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method can't be private", ee);
13.510 - return false;
13.511 + error("@On method can't be private", ee);
13.512 + ok = false;
13.513 + continue;
13.514 }
13.515 w.append(" OnEvent." + oc.event()).append(".of(").append(cnstnt(id)).
13.516 append(").perform(new OnDispatch(" + dispatchCnt + "));\n");
13.517 @@ -252,10 +460,10 @@
13.518 }
13.519 w.append(" }\n");
13.520 if (dispatchCnt > 0) {
13.521 - w.append("class OnDispatch implements Runnable {\n");
13.522 + w.append("class OnDispatch implements OnHandler {\n");
13.523 w.append(" private final int dispatch;\n");
13.524 w.append(" OnDispatch(int d) { dispatch = d; }\n");
13.525 - w.append(" public void run() {\n");
13.526 + w.append(" public void onEvent(Object ev) {\n");
13.527 w.append(" switch (dispatch) {\n");
13.528 w.append(dispatch);
13.529 w.append(" }\n");
13.530 @@ -265,7 +473,7 @@
13.531
13.532
13.533 }
13.534 - return true;
13.535 + return ok;
13.536 }
13.537
13.538 @Override
13.539 @@ -279,8 +487,7 @@
13.540
13.541 Element cls = findClass(element);
13.542 Page p = cls.getAnnotation(Page.class);
13.543 - PackageElement pe = (PackageElement) cls.getEnclosingElement();
13.544 - String pkg = pe.getQualifiedName().toString();
13.545 + String pkg = findPkgName(cls);
13.546 ProcessPage pp;
13.547 try {
13.548 InputStream is = openStream(pkg, p.xhtml());
13.549 @@ -290,7 +497,7 @@
13.550 return Collections.emptyList();
13.551 }
13.552
13.553 - List<Completion> cc = new ArrayList<Completion>();
13.554 + List<Completion> cc = new ArrayList<>();
13.555 userText = userText.substring(1);
13.556 for (String id : pp.ids()) {
13.557 if (id.startsWith(userText)) {
13.558 @@ -311,44 +518,89 @@
13.559 return e.getEnclosingElement();
13.560 }
13.561
13.562 - private static void generateProperties(
13.563 - Writer w, Property[] properties, Collection<String> props,
13.564 - Map<String,Collection<String>> deps
13.565 + private boolean generateProperties(
13.566 + Element where,
13.567 + Writer w, Prprt[] properties,
13.568 + Collection<String> props,
13.569 + Map<String,Collection<String>> deps,
13.570 + Map<String,Collection<String>> functionDeps
13.571 ) throws IOException {
13.572 - for (Property p : properties) {
13.573 - final String tn = typeName(p);
13.574 - String[] gs = toGetSet(p.name(), tn);
13.575 + boolean ok = true;
13.576 + for (Prprt p : properties) {
13.577 + final String tn;
13.578 + tn = typeName(where, p);
13.579 + String[] gs = toGetSet(p.name(), tn, p.array());
13.580
13.581 - w.write("private " + tn + " prop_" + p.name() + ";\n");
13.582 - w.write("public " + tn + " " + gs[0] + "() {\n");
13.583 - w.write(" if (locked) throw new IllegalStateException();\n");
13.584 - w.write(" return prop_" + p.name() + ";\n");
13.585 - w.write("}\n");
13.586 - w.write("public void " + gs[1] + "(" + tn + " v) {\n");
13.587 - w.write(" if (locked) throw new IllegalStateException();\n");
13.588 - w.write(" prop_" + p.name() + " = v;\n");
13.589 - w.write(" if (ko != null) {\n");
13.590 - w.write(" ko.valueHasMutated(\"" + p.name() + "\");\n");
13.591 - final Collection<String> dependants = deps.get(p.name());
13.592 - if (dependants != null) {
13.593 - for (String depProp : dependants) {
13.594 - w.write(" ko.valueHasMutated(\"" + depProp + "\");\n");
13.595 + if (p.array()) {
13.596 + w.write("private KOList<" + tn + "> prop_" + p.name() + " = new KOList<" + tn + ">(\""
13.597 + + p.name() + "\"");
13.598 + Collection<String> dependants = deps.get(p.name());
13.599 + if (dependants != null) {
13.600 + for (String depProp : dependants) {
13.601 + w.write(", ");
13.602 + w.write('\"');
13.603 + w.write(depProp);
13.604 + w.write('\"');
13.605 + }
13.606 }
13.607 + w.write(")");
13.608 +
13.609 + dependants = functionDeps.get(p.name());
13.610 + if (dependants != null) {
13.611 + w.write(".onChange(new Runnable() { public void run() {\n");
13.612 + for (String call : dependants) {
13.613 + w.append(call);
13.614 + }
13.615 + w.write("}})");
13.616 + }
13.617 + w.write(";\n");
13.618 +
13.619 + w.write("public java.util.List<" + tn + "> " + gs[0] + "() {\n");
13.620 + w.write(" if (locked) throw new IllegalStateException();\n");
13.621 + w.write(" prop_" + p.name() + ".assign(ko);\n");
13.622 + w.write(" return prop_" + p.name() + ";\n");
13.623 + w.write("}\n");
13.624 + } else {
13.625 + w.write("private " + tn + " prop_" + p.name() + ";\n");
13.626 + w.write("public " + tn + " " + gs[0] + "() {\n");
13.627 + w.write(" if (locked) throw new IllegalStateException();\n");
13.628 + w.write(" return prop_" + p.name() + ";\n");
13.629 + w.write("}\n");
13.630 + w.write("public void " + gs[1] + "(" + tn + " v) {\n");
13.631 + w.write(" if (locked) throw new IllegalStateException();\n");
13.632 + w.write(" prop_" + p.name() + " = v;\n");
13.633 + w.write(" if (ko != null) {\n");
13.634 + w.write(" ko.valueHasMutated(\"" + p.name() + "\");\n");
13.635 + Collection<String> dependants = deps.get(p.name());
13.636 + if (dependants != null) {
13.637 + for (String depProp : dependants) {
13.638 + w.write(" ko.valueHasMutated(\"" + depProp + "\");\n");
13.639 + }
13.640 + }
13.641 + w.write(" }\n");
13.642 + dependants = functionDeps.get(p.name());
13.643 + if (dependants != null) {
13.644 + for (String call : dependants) {
13.645 + w.append(call);
13.646 + }
13.647 + }
13.648 + w.write("}\n");
13.649 }
13.650 - w.write(" }\n");
13.651 - w.write("}\n");
13.652
13.653 props.add(p.name());
13.654 props.add(gs[2]);
13.655 props.add(gs[3]);
13.656 props.add(gs[0]);
13.657 }
13.658 + return ok;
13.659 }
13.660
13.661 private boolean generateComputedProperties(
13.662 - Writer w, Collection<? extends Element> arr, Collection<String> props,
13.663 + Writer w, Prprt[] fixedProps,
13.664 + Collection<? extends Element> arr, Collection<String> props,
13.665 Map<String,Collection<String>> deps
13.666 ) throws IOException {
13.667 + boolean ok = true;
13.668 for (Element e : arr) {
13.669 if (e.getKind() != ElementKind.METHOD) {
13.670 continue;
13.671 @@ -357,30 +609,43 @@
13.672 continue;
13.673 }
13.674 ExecutableElement ee = (ExecutableElement)e;
13.675 - final String tn = ee.getReturnType().toString();
13.676 + final TypeMirror rt = ee.getReturnType();
13.677 + final Types tu = processingEnv.getTypeUtils();
13.678 + TypeMirror ert = tu.erasure(rt);
13.679 + String tn = fqn(ert, ee);
13.680 + boolean array = false;
13.681 + if (tn.equals("java.util.List")) {
13.682 + array = true;
13.683 + }
13.684 +
13.685 final String sn = ee.getSimpleName().toString();
13.686 - String[] gs = toGetSet(sn, tn);
13.687 + String[] gs = toGetSet(sn, tn, array);
13.688
13.689 w.write("public " + tn + " " + gs[0] + "() {\n");
13.690 w.write(" if (locked) throw new IllegalStateException();\n");
13.691 int arg = 0;
13.692 for (VariableElement pe : ee.getParameters()) {
13.693 final String dn = pe.getSimpleName().toString();
13.694 - final String dt = pe.asType().toString();
13.695 - String[] call = toGetSet(dn, dt);
13.696 +
13.697 + if (!verifyPropName(pe, dn, fixedProps)) {
13.698 + ok = false;
13.699 + }
13.700 +
13.701 + final String dt = fqn(pe.asType(), ee);
13.702 + String[] call = toGetSet(dn, dt, false);
13.703 w.write(" " + dt + " arg" + (++arg) + " = ");
13.704 w.write(call[0] + "();\n");
13.705
13.706 Collection<String> depends = deps.get(dn);
13.707 if (depends == null) {
13.708 - depends = new LinkedHashSet<String>();
13.709 + depends = new LinkedHashSet<>();
13.710 deps.put(dn, depends);
13.711 }
13.712 depends.add(sn);
13.713 }
13.714 w.write(" try {\n");
13.715 w.write(" locked = true;\n");
13.716 - w.write(" return " + e.getEnclosingElement().getSimpleName() + '.' + e.getSimpleName() + "(");
13.717 + w.write(" return " + fqn(ee.getEnclosingElement().asType(), ee) + '.' + e.getSimpleName() + "(");
13.718 String sep = "";
13.719 for (int i = 1; i <= arg; i++) {
13.720 w.write(sep);
13.721 @@ -392,17 +657,17 @@
13.722 w.write(" locked = false;\n");
13.723 w.write(" }\n");
13.724 w.write("}\n");
13.725 -
13.726 +
13.727 props.add(e.getSimpleName().toString());
13.728 props.add(gs[2]);
13.729 props.add(null);
13.730 props.add(gs[0]);
13.731 }
13.732
13.733 - return true;
13.734 + return ok;
13.735 }
13.736
13.737 - private static String[] toGetSet(String name, String type) {
13.738 + private static String[] toGetSet(String name, String type, boolean array) {
13.739 String n = Character.toUpperCase(name.charAt(0)) + name.substring(1);
13.740 String bck2brwsrType = "L" + type.replace('.', '_') + "_2";
13.741 if ("int".equals(type)) {
13.742 @@ -417,6 +682,14 @@
13.743 bck2brwsrType = "Z";
13.744 }
13.745 final String nu = n.replace('.', '_');
13.746 + if (array) {
13.747 + return new String[] {
13.748 + "get" + n,
13.749 + null,
13.750 + "get" + nu + "__Ljava_util_List_2",
13.751 + null
13.752 + };
13.753 + }
13.754 return new String[]{
13.755 pref + n,
13.756 "set" + n,
13.757 @@ -425,11 +698,702 @@
13.758 };
13.759 }
13.760
13.761 - private static String typeName(Property p) {
13.762 - try {
13.763 - return p.type().getName();
13.764 - } catch (MirroredTypeException ex) {
13.765 - return ex.getTypeMirror().toString();
13.766 + private String typeName(Element where, Prprt p) {
13.767 + String ret;
13.768 + boolean[] isModel = { false };
13.769 + boolean[] isEnum = { false };
13.770 + boolean isPrimitive[] = { false };
13.771 + ret = checkType(p, isModel, isEnum, isPrimitive);
13.772 + if (p.array()) {
13.773 + String bt = findBoxedType(ret);
13.774 + if (bt != null) {
13.775 + return bt;
13.776 + }
13.777 + }
13.778 + return ret;
13.779 + }
13.780 +
13.781 + private static String findBoxedType(String ret) {
13.782 + if (ret.equals("boolean")) {
13.783 + return Boolean.class.getName();
13.784 + }
13.785 + if (ret.equals("byte")) {
13.786 + return Byte.class.getName();
13.787 + }
13.788 + if (ret.equals("short")) {
13.789 + return Short.class.getName();
13.790 + }
13.791 + if (ret.equals("char")) {
13.792 + return Character.class.getName();
13.793 + }
13.794 + if (ret.equals("int")) {
13.795 + return Integer.class.getName();
13.796 + }
13.797 + if (ret.equals("long")) {
13.798 + return Long.class.getName();
13.799 + }
13.800 + if (ret.equals("float")) {
13.801 + return Float.class.getName();
13.802 + }
13.803 + if (ret.equals("double")) {
13.804 + return Double.class.getName();
13.805 + }
13.806 + return null;
13.807 + }
13.808 +
13.809 + private boolean verifyPropName(Element e, String propName, Prprt[] existingProps) {
13.810 + StringBuilder sb = new StringBuilder();
13.811 + String sep = "";
13.812 + for (Prprt Prprt : existingProps) {
13.813 + if (Prprt.name().equals(propName)) {
13.814 + return true;
13.815 + }
13.816 + sb.append(sep);
13.817 + sb.append('"');
13.818 + sb.append(Prprt.name());
13.819 + sb.append('"');
13.820 + sep = ", ";
13.821 + }
13.822 + error(
13.823 + propName + " is not one of known properties: " + sb
13.824 + , e
13.825 + );
13.826 + return false;
13.827 + }
13.828 +
13.829 + private static String findPkgName(Element e) {
13.830 + for (;;) {
13.831 + if (e.getKind() == ElementKind.PACKAGE) {
13.832 + return ((PackageElement)e).getQualifiedName().toString();
13.833 + }
13.834 + e = e.getEnclosingElement();
13.835 }
13.836 }
13.837 +
13.838 + private boolean generateFunctions(
13.839 + Element clazz, StringWriter body, String className,
13.840 + List<? extends Element> enclosedElements, List<String> functions
13.841 + ) {
13.842 + for (Element m : enclosedElements) {
13.843 + if (m.getKind() != ElementKind.METHOD) {
13.844 + continue;
13.845 + }
13.846 + ExecutableElement e = (ExecutableElement)m;
13.847 + OnFunction onF = e.getAnnotation(OnFunction.class);
13.848 + if (onF == null) {
13.849 + continue;
13.850 + }
13.851 + if (!e.getModifiers().contains(Modifier.STATIC)) {
13.852 + error("@OnFunction method needs to be static", e);
13.853 + return false;
13.854 + }
13.855 + if (e.getModifiers().contains(Modifier.PRIVATE)) {
13.856 + error("@OnFunction method cannot be private", e);
13.857 + return false;
13.858 + }
13.859 + if (e.getReturnType().getKind() != TypeKind.VOID) {
13.860 + error("@OnFunction method should return void", e);
13.861 + return false;
13.862 + }
13.863 + String n = e.getSimpleName().toString();
13.864 + body.append("private void ").append(n).append("(Object data, Object ev) {\n");
13.865 + body.append(" ").append(clazz.getSimpleName()).append(".").append(n).append("(");
13.866 + body.append(wrapParams(e, null, className, "ev", "data"));
13.867 + body.append(");\n");
13.868 + body.append("}\n");
13.869 +
13.870 + functions.add(n);
13.871 + functions.add(n + "__VLjava_lang_Object_2Ljava_lang_Object_2");
13.872 + }
13.873 + return true;
13.874 + }
13.875 +
13.876 + private boolean generateOnChange(Element clazz, Map<String,Collection<String>> propDeps,
13.877 + Prprt[] properties, String className,
13.878 + Map<String, Collection<String>> functionDeps
13.879 + ) {
13.880 + for (Element m : clazz.getEnclosedElements()) {
13.881 + if (m.getKind() != ElementKind.METHOD) {
13.882 + continue;
13.883 + }
13.884 + ExecutableElement e = (ExecutableElement) m;
13.885 + OnPropertyChange onPC = e.getAnnotation(OnPropertyChange.class);
13.886 + if (onPC == null) {
13.887 + continue;
13.888 + }
13.889 + for (String pn : onPC.value()) {
13.890 + if (findPrprt(properties, pn) == null && findDerivedFrom(propDeps, pn).isEmpty()) {
13.891 + error("No Prprt named '" + pn + "' in the model", clazz);
13.892 + return false;
13.893 + }
13.894 + }
13.895 + if (!e.getModifiers().contains(Modifier.STATIC)) {
13.896 + error("@OnPrprtChange method needs to be static", e);
13.897 + return false;
13.898 + }
13.899 + if (e.getModifiers().contains(Modifier.PRIVATE)) {
13.900 + error("@OnPrprtChange method cannot be private", e);
13.901 + return false;
13.902 + }
13.903 + if (e.getReturnType().getKind() != TypeKind.VOID) {
13.904 + error("@OnPrprtChange method should return void", e);
13.905 + return false;
13.906 + }
13.907 + String n = e.getSimpleName().toString();
13.908 +
13.909 +
13.910 + for (String pn : onPC.value()) {
13.911 + StringBuilder call = new StringBuilder();
13.912 + call.append(" ").append(clazz.getSimpleName()).append(".").append(n).append("(");
13.913 + call.append(wrapPropName(e, className, "name", pn));
13.914 + call.append(");\n");
13.915 +
13.916 + Collection<String> change = functionDeps.get(pn);
13.917 + if (change == null) {
13.918 + change = new ArrayList<>();
13.919 + functionDeps.put(pn, change);
13.920 + }
13.921 + change.add(call.toString());
13.922 + for (String dpn : findDerivedFrom(propDeps, pn)) {
13.923 + change = functionDeps.get(dpn);
13.924 + if (change == null) {
13.925 + change = new ArrayList<>();
13.926 + functionDeps.put(dpn, change);
13.927 + }
13.928 + change.add(call.toString());
13.929 + }
13.930 + }
13.931 + }
13.932 + return true;
13.933 + }
13.934 +
13.935 + private boolean generateReceive(
13.936 + Element clazz, StringWriter body, String className,
13.937 + List<? extends Element> enclosedElements, List<String> functions
13.938 + ) {
13.939 + for (Element m : enclosedElements) {
13.940 + if (m.getKind() != ElementKind.METHOD) {
13.941 + continue;
13.942 + }
13.943 + ExecutableElement e = (ExecutableElement)m;
13.944 + OnReceive onR = e.getAnnotation(OnReceive.class);
13.945 + if (onR == null) {
13.946 + continue;
13.947 + }
13.948 + if (!e.getModifiers().contains(Modifier.STATIC)) {
13.949 + error("@OnReceive method needs to be static", e);
13.950 + return false;
13.951 + }
13.952 + if (e.getModifiers().contains(Modifier.PRIVATE)) {
13.953 + error("@OnReceive method cannot be private", e);
13.954 + return false;
13.955 + }
13.956 + if (e.getReturnType().getKind() != TypeKind.VOID) {
13.957 + error("@OnReceive method should return void", e);
13.958 + return false;
13.959 + }
13.960 + String modelClass = null;
13.961 + boolean expectsList = false;
13.962 + List<String> args = new ArrayList<>();
13.963 + {
13.964 + for (VariableElement ve : e.getParameters()) {
13.965 + TypeMirror modelType = null;
13.966 + if (ve.asType().toString().equals(className)) {
13.967 + args.add(className + ".this");
13.968 + } else if (isModel(ve.asType())) {
13.969 + modelType = ve.asType();
13.970 + } else if (ve.asType().getKind() == TypeKind.ARRAY) {
13.971 + modelType = ((ArrayType)ve.asType()).getComponentType();
13.972 + expectsList = true;
13.973 + }
13.974 + if (modelType != null) {
13.975 + if (modelClass != null) {
13.976 + error("There can be only one model class among arguments", e);
13.977 + } else {
13.978 + modelClass = modelType.toString();
13.979 + if (expectsList) {
13.980 + args.add("arr");
13.981 + } else {
13.982 + args.add("arr[0]");
13.983 + }
13.984 + }
13.985 + }
13.986 + }
13.987 + }
13.988 + if (modelClass == null) {
13.989 + error("The method needs to have one @Model class as parameter", e);
13.990 + }
13.991 + String n = e.getSimpleName().toString();
13.992 + body.append("public void ").append(n).append("(");
13.993 + StringBuilder assembleURL = new StringBuilder();
13.994 + String jsonpVarName = null;
13.995 + {
13.996 + String sep = "";
13.997 + boolean skipJSONP = onR.jsonp().isEmpty();
13.998 + for (String p : findParamNames(e, onR.url(), assembleURL)) {
13.999 + if (!skipJSONP && p.equals(onR.jsonp())) {
13.1000 + skipJSONP = true;
13.1001 + jsonpVarName = p;
13.1002 + continue;
13.1003 + }
13.1004 + body.append(sep);
13.1005 + body.append("String ").append(p);
13.1006 + sep = ", ";
13.1007 + }
13.1008 + if (!skipJSONP) {
13.1009 + error(
13.1010 + "Name of jsonp attribute ('" + onR.jsonp() +
13.1011 + "') is not used in url attribute '" + onR.url() + "'", e
13.1012 + );
13.1013 + }
13.1014 + }
13.1015 + body.append(") {\n");
13.1016 + body.append(" final Object[] result = { null };\n");
13.1017 + body.append(
13.1018 + " class ProcessResult implements Runnable {\n" +
13.1019 + " @Override\n" +
13.1020 + " public void run() {\n" +
13.1021 + " Object value = result[0];\n");
13.1022 + body.append(
13.1023 + " " + modelClass + "[] arr;\n");
13.1024 + body.append(
13.1025 + " if (value instanceof Object[]) {\n" +
13.1026 + " Object[] data = ((Object[])value);\n" +
13.1027 + " arr = new " + modelClass + "[data.length];\n" +
13.1028 + " for (int i = 0; i < data.length; i++) {\n" +
13.1029 + " arr[i] = new " + modelClass + "(data[i]);\n" +
13.1030 + " }\n" +
13.1031 + " } else {\n" +
13.1032 + " arr = new " + modelClass + "[1];\n" +
13.1033 + " arr[0] = new " + modelClass + "(value);\n" +
13.1034 + " }\n"
13.1035 + );
13.1036 + {
13.1037 + body.append(clazz.getSimpleName()).append(".").append(n).append("(");
13.1038 + String sep = "";
13.1039 + for (String arg : args) {
13.1040 + body.append(sep);
13.1041 + body.append(arg);
13.1042 + sep = ", ";
13.1043 + }
13.1044 + body.append(");\n");
13.1045 + }
13.1046 + body.append(
13.1047 + " }\n" +
13.1048 + " }\n"
13.1049 + );
13.1050 + body.append(" ProcessResult pr = new ProcessResult();\n");
13.1051 + if (jsonpVarName != null) {
13.1052 + body.append(" String ").append(jsonpVarName).
13.1053 + append(" = org.apidesign.bck2brwsr.htmlpage.ConvertTypes.createJSONP(result, pr);\n");
13.1054 + }
13.1055 + body.append(" org.apidesign.bck2brwsr.htmlpage.ConvertTypes.loadJSON(\n ");
13.1056 + body.append(assembleURL);
13.1057 + body.append(", result, pr, ").append(jsonpVarName).append("\n );\n");
13.1058 +// body.append(" ").append(clazz.getSimpleName()).append(".").append(n).append("(");
13.1059 +// body.append(wrapParams(e, null, className, "ev", "data"));
13.1060 +// body.append(");\n");
13.1061 + body.append("}\n");
13.1062 + }
13.1063 + return true;
13.1064 + }
13.1065 +
13.1066 + private CharSequence wrapParams(
13.1067 + ExecutableElement ee, String id, String className, String evName, String dataName
13.1068 + ) {
13.1069 + TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType();
13.1070 + StringBuilder params = new StringBuilder();
13.1071 + boolean first = true;
13.1072 + for (VariableElement ve : ee.getParameters()) {
13.1073 + if (!first) {
13.1074 + params.append(", ");
13.1075 + }
13.1076 + first = false;
13.1077 + String toCall = null;
13.1078 + if (ve.asType() == stringType) {
13.1079 + if (ve.getSimpleName().contentEquals("id")) {
13.1080 + params.append('"').append(id).append('"');
13.1081 + continue;
13.1082 + }
13.1083 + toCall = "org.apidesign.bck2brwsr.htmlpage.ConvertTypes.toString(";
13.1084 + }
13.1085 + if (ve.asType().getKind() == TypeKind.DOUBLE) {
13.1086 + toCall = "org.apidesign.bck2brwsr.htmlpage.ConvertTypes.toDouble(";
13.1087 + }
13.1088 + if (ve.asType().getKind() == TypeKind.INT) {
13.1089 + toCall = "org.apidesign.bck2brwsr.htmlpage.ConvertTypes.toInt(";
13.1090 + }
13.1091 + if (dataName != null && ve.getSimpleName().contentEquals(dataName) && isModel(ve.asType())) {
13.1092 + toCall = "org.apidesign.bck2brwsr.htmlpage.ConvertTypes.toModel(" + ve.asType() + ".class, ";
13.1093 + }
13.1094 +
13.1095 + if (toCall != null) {
13.1096 + params.append(toCall);
13.1097 + if (dataName != null && ve.getSimpleName().contentEquals(dataName)) {
13.1098 + params.append(dataName);
13.1099 + params.append(", null");
13.1100 + } else {
13.1101 + if (evName == null) {
13.1102 + final StringBuilder sb = new StringBuilder();
13.1103 + sb.append("Unexpected string parameter name.");
13.1104 + if (dataName != null) {
13.1105 + sb.append(" Try \"").append(dataName).append("\"");
13.1106 + }
13.1107 + error(sb.toString(), ee);
13.1108 + }
13.1109 + params.append(evName);
13.1110 + params.append(", \"");
13.1111 + params.append(ve.getSimpleName().toString());
13.1112 + params.append("\"");
13.1113 + }
13.1114 + params.append(")");
13.1115 + continue;
13.1116 + }
13.1117 + String rn = fqn(ve.asType(), ee);
13.1118 + int last = rn.lastIndexOf('.');
13.1119 + if (last >= 0) {
13.1120 + rn = rn.substring(last + 1);
13.1121 + }
13.1122 + if (rn.equals(className)) {
13.1123 + params.append(className).append(".this");
13.1124 + continue;
13.1125 + }
13.1126 + error(
13.1127 + "@On method can only accept String named 'id' or " + className + " arguments",
13.1128 + ee
13.1129 + );
13.1130 + }
13.1131 + return params;
13.1132 + }
13.1133 +
13.1134 +
13.1135 + private CharSequence wrapPropName(
13.1136 + ExecutableElement ee, String className, String propName, String propValue
13.1137 + ) {
13.1138 + TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType();
13.1139 + StringBuilder params = new StringBuilder();
13.1140 + boolean first = true;
13.1141 + for (VariableElement ve : ee.getParameters()) {
13.1142 + if (!first) {
13.1143 + params.append(", ");
13.1144 + }
13.1145 + first = false;
13.1146 + if (ve.asType() == stringType) {
13.1147 + if (propName != null && ve.getSimpleName().contentEquals(propName)) {
13.1148 + params.append('"').append(propValue).append('"');
13.1149 + } else {
13.1150 + error("Unexpected string parameter name. Try \"" + propName + "\".", ee);
13.1151 + }
13.1152 + continue;
13.1153 + }
13.1154 + String rn = fqn(ve.asType(), ee);
13.1155 + int last = rn.lastIndexOf('.');
13.1156 + if (last >= 0) {
13.1157 + rn = rn.substring(last + 1);
13.1158 + }
13.1159 + if (rn.equals(className)) {
13.1160 + params.append(className).append(".this");
13.1161 + continue;
13.1162 + }
13.1163 + error(
13.1164 + "@OnPrprtChange method can only accept String or " + className + " arguments",
13.1165 + ee);
13.1166 + }
13.1167 + return params;
13.1168 + }
13.1169 +
13.1170 + private boolean isModel(TypeMirror tm) {
13.1171 + final Element e = processingEnv.getTypeUtils().asElement(tm);
13.1172 + if (e == null) {
13.1173 + return false;
13.1174 + }
13.1175 + for (Element ch : e.getEnclosedElements()) {
13.1176 + if (ch.getKind() == ElementKind.METHOD) {
13.1177 + ExecutableElement ee = (ExecutableElement)ch;
13.1178 + if (ee.getParameters().isEmpty() && ee.getSimpleName().contentEquals("modelFor")) {
13.1179 + return true;
13.1180 + }
13.1181 + }
13.1182 + }
13.1183 + return models.values().contains(e.getSimpleName().toString());
13.1184 + }
13.1185 +
13.1186 + private void writeStringArray(List<String> strings, Writer w) throws IOException {
13.1187 + w.write("new String[] {\n");
13.1188 + String sep = "";
13.1189 + for (String n : strings) {
13.1190 + w.write(sep);
13.1191 + if (n == null) {
13.1192 + w.write(" null");
13.1193 + } else {
13.1194 + w.write(" \"" + n + "\"");
13.1195 + }
13.1196 + sep = ",\n";
13.1197 + }
13.1198 + w.write("\n }");
13.1199 + }
13.1200 +
13.1201 + private void writeToString(Prprt[] props, Writer w) throws IOException {
13.1202 + w.write(" public String toString() {\n");
13.1203 + w.write(" StringBuilder sb = new StringBuilder();\n");
13.1204 + w.write(" sb.append('{');\n");
13.1205 + String sep = "";
13.1206 + for (Prprt p : props) {
13.1207 + w.write(sep);
13.1208 + w.append(" sb.append('\"').append(\"" + p.name() + "\")");
13.1209 + w.append(".append('\"').append(\":\");\n");
13.1210 + w.append(" sb.append(org.apidesign.bck2brwsr.htmlpage.ConvertTypes.toJSON(prop_");
13.1211 + w.append(p.name()).append("));\n");
13.1212 + sep = " sb.append(',');\n";
13.1213 + }
13.1214 + w.write(" sb.append('}');\n");
13.1215 + w.write(" return sb.toString();\n");
13.1216 + w.write(" }\n");
13.1217 + }
13.1218 + private void writeClone(String className, Prprt[] props, Writer w) throws IOException {
13.1219 + w.write(" public " + className + " clone() {\n");
13.1220 + w.write(" " + className + " ret = new " + className + "();\n");
13.1221 + for (Prprt p : props) {
13.1222 + if (!p.array()) {
13.1223 + boolean isModel[] = { false };
13.1224 + boolean isEnum[] = { false };
13.1225 + boolean isPrimitive[] = { false };
13.1226 + checkType(p, isModel, isEnum, isPrimitive);
13.1227 + if (!isModel[0]) {
13.1228 + w.write(" ret.prop_" + p.name() + " = prop_" + p.name() + ";\n");
13.1229 + continue;
13.1230 + }
13.1231 + w.write(" ret.prop_" + p.name() + " = prop_" + p.name() + ".clone();\n");
13.1232 + } else {
13.1233 + w.write(" ret.prop_" + p.name() + " = prop_" + p.name() + ".clone();\n");
13.1234 + }
13.1235 + }
13.1236 +
13.1237 + w.write(" return ret;\n");
13.1238 + w.write(" }\n");
13.1239 + }
13.1240 +
13.1241 + private String inPckName(Element e) {
13.1242 + StringBuilder sb = new StringBuilder();
13.1243 + while (e.getKind() != ElementKind.PACKAGE) {
13.1244 + if (sb.length() == 0) {
13.1245 + sb.append(e.getSimpleName());
13.1246 + } else {
13.1247 + sb.insert(0, '.');
13.1248 + sb.insert(0, e.getSimpleName());
13.1249 + }
13.1250 + e = e.getEnclosingElement();
13.1251 + }
13.1252 + return sb.toString();
13.1253 + }
13.1254 +
13.1255 + private String fqn(TypeMirror pt, Element relative) {
13.1256 + if (pt.getKind() == TypeKind.ERROR) {
13.1257 + final Elements eu = processingEnv.getElementUtils();
13.1258 + PackageElement pckg = eu.getPackageOf(relative);
13.1259 + return pckg.getQualifiedName() + "." + pt.toString();
13.1260 + }
13.1261 + return pt.toString();
13.1262 + }
13.1263 +
13.1264 + private String checkType(Prprt p, boolean[] isModel, boolean[] isEnum, boolean[] isPrimitive) {
13.1265 + TypeMirror tm;
13.1266 + try {
13.1267 + String ret = p.typeName(processingEnv);
13.1268 + TypeElement e = processingEnv.getElementUtils().getTypeElement(ret);
13.1269 + if (e == null) {
13.1270 + isModel[0] = true;
13.1271 + isEnum[0] = false;
13.1272 + isPrimitive[0] = false;
13.1273 + return ret;
13.1274 + }
13.1275 + tm = e.asType();
13.1276 + } catch (MirroredTypeException ex) {
13.1277 + tm = ex.getTypeMirror();
13.1278 + }
13.1279 + tm = processingEnv.getTypeUtils().erasure(tm);
13.1280 + isPrimitive[0] = tm.getKind().isPrimitive();
13.1281 + final Element e = processingEnv.getTypeUtils().asElement(tm);
13.1282 + final Model m = e == null ? null : e.getAnnotation(Model.class);
13.1283 +
13.1284 + String ret;
13.1285 + if (m != null) {
13.1286 + ret = findPkgName(e) + '.' + m.className();
13.1287 + isModel[0] = true;
13.1288 + models.put(e, m.className());
13.1289 + } else if (findModelForMthd(e)) {
13.1290 + ret = ((TypeElement)e).getQualifiedName().toString();
13.1291 + isModel[0] = true;
13.1292 + } else {
13.1293 + ret = tm.toString();
13.1294 + }
13.1295 + TypeMirror enm = processingEnv.getElementUtils().getTypeElement("java.lang.Enum").asType();
13.1296 + enm = processingEnv.getTypeUtils().erasure(enm);
13.1297 + isEnum[0] = processingEnv.getTypeUtils().isSubtype(tm, enm);
13.1298 + return ret;
13.1299 + }
13.1300 +
13.1301 + private static boolean findModelForMthd(Element clazz) {
13.1302 + if (clazz == null) {
13.1303 + return false;
13.1304 + }
13.1305 + for (Element e : clazz.getEnclosedElements()) {
13.1306 + if (e.getKind() == ElementKind.METHOD) {
13.1307 + ExecutableElement ee = (ExecutableElement)e;
13.1308 + if (
13.1309 + ee.getSimpleName().contentEquals("modelFor") &&
13.1310 + ee.getParameters().isEmpty()
13.1311 + ) {
13.1312 + return true;
13.1313 + }
13.1314 + }
13.1315 + }
13.1316 + return false;
13.1317 + }
13.1318 +
13.1319 + private Iterable<String> findParamNames(Element e, String url, StringBuilder assembleURL) {
13.1320 + List<String> params = new ArrayList<>();
13.1321 +
13.1322 + for (int pos = 0; ;) {
13.1323 + int next = url.indexOf('{', pos);
13.1324 + if (next == -1) {
13.1325 + assembleURL.append('"')
13.1326 + .append(url.substring(pos))
13.1327 + .append('"');
13.1328 + return params;
13.1329 + }
13.1330 + int close = url.indexOf('}', next);
13.1331 + if (close == -1) {
13.1332 + error("Unbalanced '{' and '}' in " + url, e);
13.1333 + return params;
13.1334 + }
13.1335 + final String paramName = url.substring(next + 1, close);
13.1336 + params.add(paramName);
13.1337 + assembleURL.append('"')
13.1338 + .append(url.substring(pos, next))
13.1339 + .append("\" + ").append(paramName).append(" + ");
13.1340 + pos = close + 1;
13.1341 + }
13.1342 + }
13.1343 +
13.1344 + private static Prprt findPrprt(Prprt[] properties, String propName) {
13.1345 + for (Prprt p : properties) {
13.1346 + if (propName.equals(p.name())) {
13.1347 + return p;
13.1348 + }
13.1349 + }
13.1350 + return null;
13.1351 + }
13.1352 +
13.1353 + private boolean isPrimitive(String type) {
13.1354 + return
13.1355 + "int".equals(type) ||
13.1356 + "double".equals(type) ||
13.1357 + "long".equals(type) ||
13.1358 + "short".equals(type) ||
13.1359 + "byte".equals(type) ||
13.1360 + "float".equals(type);
13.1361 + }
13.1362 +
13.1363 + private static Collection<String> findDerivedFrom(Map<String, Collection<String>> propsDeps, String derivedProp) {
13.1364 + Set<String> names = new HashSet<>();
13.1365 + for (Map.Entry<String, Collection<String>> e : propsDeps.entrySet()) {
13.1366 + if (e.getValue().contains(derivedProp)) {
13.1367 + names.add(e.getKey());
13.1368 + }
13.1369 + }
13.1370 + return names;
13.1371 + }
13.1372 +
13.1373 + private Prprt[] createProps(Element e, Property[] arr) {
13.1374 + Prprt[] ret = Prprt.wrap(processingEnv, e, arr);
13.1375 + Prprt[] prev = verify.put(e, ret);
13.1376 + if (prev != null) {
13.1377 + error("Two sets of properties for ", e);
13.1378 + }
13.1379 + return ret;
13.1380 + }
13.1381 +
13.1382 + private static class Prprt {
13.1383 + private final Element e;
13.1384 + private final AnnotationMirror tm;
13.1385 + private final Property p;
13.1386 +
13.1387 + public Prprt(Element e, AnnotationMirror tm, Property p) {
13.1388 + this.e = e;
13.1389 + this.tm = tm;
13.1390 + this.p = p;
13.1391 + }
13.1392 +
13.1393 + String name() {
13.1394 + return p.name();
13.1395 + }
13.1396 +
13.1397 + boolean array() {
13.1398 + return p.array();
13.1399 + }
13.1400 +
13.1401 + String typeName(ProcessingEnvironment env) {
13.1402 + try {
13.1403 + return p.type().getName();
13.1404 + } catch (IncompleteAnnotationException | AnnotationTypeMismatchException ex) {
13.1405 + for (Object v : getAnnoValues(env)) {
13.1406 + String s = v.toString().replace(" ", "");
13.1407 + if (s.startsWith("type=") && s.endsWith(".class")) {
13.1408 + return s.substring(5, s.length() - 6);
13.1409 + }
13.1410 + }
13.1411 + throw ex;
13.1412 + }
13.1413 + }
13.1414 +
13.1415 +
13.1416 + static Prprt[] wrap(ProcessingEnvironment pe, Element e, Property[] arr) {
13.1417 + if (arr.length == 0) {
13.1418 + return new Prprt[0];
13.1419 + }
13.1420 +
13.1421 + if (e.getKind() != ElementKind.CLASS) {
13.1422 + throw new IllegalStateException("" + e.getKind());
13.1423 + }
13.1424 + TypeElement te = (TypeElement)e;
13.1425 + List<? extends AnnotationValue> val = null;
13.1426 + for (AnnotationMirror an : te.getAnnotationMirrors()) {
13.1427 + for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : an.getElementValues().entrySet()) {
13.1428 + if (entry.getKey().getSimpleName().contentEquals("properties")) {
13.1429 + val = (List)entry.getValue().getValue();
13.1430 + break;
13.1431 + }
13.1432 + }
13.1433 + }
13.1434 + if (val == null || val.size() != arr.length) {
13.1435 + pe.getMessager().printMessage(Diagnostic.Kind.ERROR, "" + val, e);
13.1436 + return new Prprt[0];
13.1437 + }
13.1438 + Prprt[] ret = new Prprt[arr.length];
13.1439 + BIG: for (int i = 0; i < ret.length; i++) {
13.1440 + AnnotationMirror am = (AnnotationMirror)val.get(i).getValue();
13.1441 + ret[i] = new Prprt(e, am, arr[i]);
13.1442 +
13.1443 + }
13.1444 + return ret;
13.1445 + }
13.1446 +
13.1447 + private List<? extends Object> getAnnoValues(ProcessingEnvironment pe) {
13.1448 + try {
13.1449 + Class<?> trees = Class.forName("com.sun.tools.javac.api.JavacTrees");
13.1450 + Method m = trees.getMethod("instance", ProcessingEnvironment.class);
13.1451 + Object instance = m.invoke(null, pe);
13.1452 + m = instance.getClass().getMethod("getPath", Element.class, AnnotationMirror.class);
13.1453 + Object path = m.invoke(instance, e, tm);
13.1454 + m = path.getClass().getMethod("getLeaf");
13.1455 + Object leaf = m.invoke(path);
13.1456 + m = leaf.getClass().getMethod("getArguments");
13.1457 + return (List)m.invoke(leaf);
13.1458 + } catch (Exception ex) {
13.1459 + return Collections.emptyList();
13.1460 + }
13.1461 + }
13.1462 + }
13.1463 +
13.1464 }
14.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/ComputedProperty.java Tue Mar 05 07:57:16 2013 +0100
14.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/ComputedProperty.java Fri May 17 10:27:41 2013 +0200
14.3 @@ -22,16 +22,11 @@
14.4 import java.lang.annotation.RetentionPolicy;
14.5 import java.lang.annotation.Target;
14.6
14.7 -/** Can be used in classes annotated with {@link Page} annotation to
14.8 - * define a derived property. Value of derived property is based on values
14.9 - * of {@link Property} as enumerated by {@link Page#properties()}.
14.10 - * <p>
14.11 - * The name of the derived property is the name of the method. The arguments
14.12 - * of the method define the property names (from {@link Page#properties()} list)
14.13 - * the value of property depends on.
14.14 - *
14.15 +/**
14.16 + * @deprecated Replaced by new {@link net.java.html.json.ComputedProperty net.java.html.json} API.
14.17 * @author Jaroslav Tulach <jtulach@netbeans.org>
14.18 */
14.19 +@Deprecated
14.20 @Retention(RetentionPolicy.SOURCE)
14.21 @Target(ElementType.METHOD)
14.22 public @interface ComputedProperty {
15.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java Tue Mar 05 07:57:16 2013 +0100
15.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java Fri May 17 10:27:41 2013 +0200
15.3 @@ -61,14 +61,18 @@
15.4
15.5 /** Executes given runnable when user performs a "click" on the given
15.6 * element.
15.7 + * @param data an array of one element to fill with event parameter (if any)
15.8 * @param r the runnable to execute, never null
15.9 */
15.10 @JavaScriptBody(
15.11 args={ "ev", "r" },
15.12 body="var e = window.document.getElementById(this._id());\n"
15.13 - + "e[ev._id()] = function() { r.run__V(); };\n"
15.14 + + "e[ev._id()] = function(ev) {\n"
15.15 + + " var d = ev ? ev : null;\n"
15.16 + + " r.onEvent__VLjava_lang_Object_2(d);\n"
15.17 + + "};\n"
15.18 )
15.19 - final void on(OnEvent ev, Runnable r) {
15.20 + final void on(OnEvent ev, OnHandler r) {
15.21 }
15.22
15.23 /** Shows alert message dialog in a browser.
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
16.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Model.java Fri May 17 10:27:41 2013 +0200
16.3 @@ -0,0 +1,38 @@
16.4 +/**
16.5 + * Back 2 Browser Bytecode Translator
16.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
16.7 + *
16.8 + * This program is free software: you can redistribute it and/or modify
16.9 + * it under the terms of the GNU General Public License as published by
16.10 + * the Free Software Foundation, version 2 of the License.
16.11 + *
16.12 + * This program is distributed in the hope that it will be useful,
16.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16.15 + * GNU General Public License for more details.
16.16 + *
16.17 + * You should have received a copy of the GNU General Public License
16.18 + * along with this program. Look for COPYING file in the top folder.
16.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
16.20 + */
16.21 +package org.apidesign.bck2brwsr.htmlpage.api;
16.22 +
16.23 +import java.lang.annotation.ElementType;
16.24 +import java.lang.annotation.Retention;
16.25 +import java.lang.annotation.RetentionPolicy;
16.26 +import java.lang.annotation.Target;
16.27 +
16.28 +/**
16.29 + * @deprecated Replaced by new {@link net.java.html.json.Model net.java.html.json} API.
16.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
16.31 + */
16.32 +@Retention(RetentionPolicy.SOURCE)
16.33 +@Target(ElementType.TYPE)
16.34 +@Deprecated
16.35 +public @interface Model {
16.36 + /** Name of the model class */
16.37 + String className();
16.38 + /** List of properties in the model.
16.39 + */
16.40 + Property[] properties();
16.41 +}
17.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnController.java Tue Mar 05 07:57:16 2013 +0100
17.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnController.java Fri May 17 10:27:41 2013 +0200
17.3 @@ -30,14 +30,33 @@
17.4 this.arr = arr;
17.5 }
17.6
17.7 + /** Registers an event handler on associated {@link OnEvent}
17.8 + * and {@link Element}
17.9 + *
17.10 + * @param handler the handler to be called when the event occurs
17.11 + */
17.12 + public void perform(final OnHandler handler) {
17.13 + for (Element e : arr) {
17.14 + e.on(event, handler);
17.15 + }
17.16 + }
17.17 +
17.18 /** Registers a runnable to be performed on associated {@link OnEvent}
17.19 * and {@link Element}.
17.20 *
17.21 * @see OnEvent#of
17.22 */
17.23 - public void perform(Runnable r) {
17.24 + public void perform(final Runnable r) {
17.25 + class W implements OnHandler {
17.26 + @Override
17.27 + public void onEvent(Object event) throws Exception {
17.28 + r.run();
17.29 + }
17.30 + }
17.31 + perform(new W());
17.32 + OnHandler w = new W();
17.33 for (Element e : arr) {
17.34 - e.on(event, r);
17.35 + e.on(event, w);
17.36 }
17.37 }
17.38 }
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
18.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnFunction.java Fri May 17 10:27:41 2013 +0200
18.3 @@ -0,0 +1,33 @@
18.4 +/**
18.5 + * Back 2 Browser Bytecode Translator
18.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
18.7 + *
18.8 + * This program is free software: you can redistribute it and/or modify
18.9 + * it under the terms of the GNU General Public License as published by
18.10 + * the Free Software Foundation, version 2 of the License.
18.11 + *
18.12 + * This program is distributed in the hope that it will be useful,
18.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18.15 + * GNU General Public License for more details.
18.16 + *
18.17 + * You should have received a copy of the GNU General Public License
18.18 + * along with this program. Look for COPYING file in the top folder.
18.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
18.20 + */
18.21 +package org.apidesign.bck2brwsr.htmlpage.api;
18.22 +
18.23 +import java.lang.annotation.ElementType;
18.24 +import java.lang.annotation.Retention;
18.25 +import java.lang.annotation.RetentionPolicy;
18.26 +import java.lang.annotation.Target;
18.27 +
18.28 +/**
18.29 + * @deprecated Replaced by new {@link net.java.html.json.Function net.java.html.json} API.
18.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
18.31 + */
18.32 +@Target(ElementType.METHOD)
18.33 +@Retention(RetentionPolicy.SOURCE)
18.34 +@Deprecated
18.35 +public @interface OnFunction {
18.36 +}
19.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
19.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnHandler.java Fri May 17 10:27:41 2013 +0200
19.3 @@ -0,0 +1,33 @@
19.4 +/**
19.5 + * Back 2 Browser Bytecode Translator
19.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
19.7 + *
19.8 + * This program is free software: you can redistribute it and/or modify
19.9 + * it under the terms of the GNU General Public License as published by
19.10 + * the Free Software Foundation, version 2 of the License.
19.11 + *
19.12 + * This program is distributed in the hope that it will be useful,
19.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19.15 + * GNU General Public License for more details.
19.16 + *
19.17 + * You should have received a copy of the GNU General Public License
19.18 + * along with this program. Look for COPYING file in the top folder.
19.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
19.20 + */
19.21 +package org.apidesign.bck2brwsr.htmlpage.api;
19.22 +
19.23 +/** Handler to be called when an event in an HTML {@link Page} appears.
19.24 + * @see OnEvent
19.25 + * @see OnController
19.26 + *
19.27 + * @author Jaroslav Tulach <jtulach@netbeans.org>
19.28 + */
19.29 +public interface OnHandler {
19.30 + /** Called when a DOM event appears
19.31 + *
19.32 + * @param event the event as produced by the browser
19.33 + * @throws Exception execution can throw exception
19.34 + */
19.35 + public void onEvent(Object event) throws Exception;
19.36 +}
20.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
20.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnPropertyChange.java Fri May 17 10:27:41 2013 +0200
20.3 @@ -0,0 +1,38 @@
20.4 +/**
20.5 + * Back 2 Browser Bytecode Translator
20.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
20.7 + *
20.8 + * This program is free software: you can redistribute it and/or modify
20.9 + * it under the terms of the GNU General Public License as published by
20.10 + * the Free Software Foundation, version 2 of the License.
20.11 + *
20.12 + * This program is distributed in the hope that it will be useful,
20.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20.15 + * GNU General Public License for more details.
20.16 + *
20.17 + * You should have received a copy of the GNU General Public License
20.18 + * along with this program. Look for COPYING file in the top folder.
20.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
20.20 + */
20.21 +package org.apidesign.bck2brwsr.htmlpage.api;
20.22 +
20.23 +import java.lang.annotation.ElementType;
20.24 +import java.lang.annotation.Retention;
20.25 +import java.lang.annotation.RetentionPolicy;
20.26 +import java.lang.annotation.Target;
20.27 +
20.28 +/**
20.29 + * @deprecated Replaced by new {@link net.java.html.json.OnPropertyChange net.java.html.json} API.
20.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
20.31 + */
20.32 +@Retention(RetentionPolicy.SOURCE)
20.33 +@Target(ElementType.METHOD)
20.34 +@Deprecated
20.35 +public @interface OnPropertyChange {
20.36 + /** Name(s) of the properties. One wishes to observe.
20.37 + *
20.38 + * @return valid java identifier
20.39 + */
20.40 + String[] value();
20.41 +}
21.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
21.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnReceive.java Fri May 17 10:27:41 2013 +0200
21.3 @@ -0,0 +1,53 @@
21.4 +/**
21.5 + * Back 2 Browser Bytecode Translator
21.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
21.7 + *
21.8 + * This program is free software: you can redistribute it and/or modify
21.9 + * it under the terms of the GNU General Public License as published by
21.10 + * the Free Software Foundation, version 2 of the License.
21.11 + *
21.12 + * This program is distributed in the hope that it will be useful,
21.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21.15 + * GNU General Public License for more details.
21.16 + *
21.17 + * You should have received a copy of the GNU General Public License
21.18 + * along with this program. Look for COPYING file in the top folder.
21.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
21.20 + */
21.21 +package org.apidesign.bck2brwsr.htmlpage.api;
21.22 +
21.23 +import java.lang.annotation.ElementType;
21.24 +import java.lang.annotation.Retention;
21.25 +import java.lang.annotation.RetentionPolicy;
21.26 +import java.lang.annotation.Target;
21.27 +
21.28 +/**
21.29 + * @deprecated Replaced by new {@link net.java.html.json.OnReceive net.java.html.json} API.
21.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
21.31 + * @since 0.6
21.32 + */
21.33 +@Retention(RetentionPolicy.SOURCE)
21.34 +@Target(ElementType.METHOD)
21.35 +@Deprecated
21.36 +public @interface OnReceive {
21.37 + /** The URL to connect to. Can contain variable names surrounded by '{' and '}'.
21.38 + * Those parameters will then become variables of the associated method.
21.39 + *
21.40 + * @return the (possibly parametrized) url to connect to
21.41 + */
21.42 + String url();
21.43 +
21.44 + /** Support for <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> requires
21.45 + * a callback from the server generated page to a function defined in the
21.46 + * system. The name of such function is usually specified as a property
21.47 + * (of possibly different names). By defining the <code>jsonp</code> attribute
21.48 + * one turns on the <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a>
21.49 + * transmission and specifies the name of the property. The property should
21.50 + * also be used in the {@link #url()} attribute on appropriate place.
21.51 + *
21.52 + * @return name of a property to carry the name of <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a>
21.53 + * callback function.
21.54 + */
21.55 + String jsonp() default "";
21.56 +}
22.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Property.java Tue Mar 05 07:57:16 2013 +0100
22.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Property.java Fri May 17 10:27:41 2013 +0200
22.3 @@ -20,15 +20,36 @@
22.4 import java.lang.annotation.Retention;
22.5 import java.lang.annotation.RetentionPolicy;
22.6 import java.lang.annotation.Target;
22.7 +import java.util.List;
22.8
22.9 -/** Represents a property in a generated model of an HTML
22.10 - * {@link Page}.
22.11 - *
22.12 +/**
22.13 + * @deprecated Replaced by new {@link net.java.html.json.Property net.java.html.json} API.
22.14 * @author Jaroslav Tulach <jtulach@netbeans.org>
22.15 */
22.16 @Retention(RetentionPolicy.SOURCE)
22.17 @Target({})
22.18 +@Deprecated
22.19 public @interface Property {
22.20 + /** Name of the property. Will be used to define proper getter and setter
22.21 + * in the associated class.
22.22 + *
22.23 + * @return valid java identifier
22.24 + */
22.25 String name();
22.26 +
22.27 + /** Type of the property. Can either be primitive type (like <code>int.class</code>,
22.28 + * <code>double.class</code>, etc.), {@link String} or complex model
22.29 + * class (defined by {@link Model} property).
22.30 + *
22.31 + * @return the class of the property
22.32 + */
22.33 Class<?> type();
22.34 +
22.35 + /** Is this property an array of the {@link #type()} or a single value?
22.36 + * If the property is an array, only its getter (returning mutable {@link List} of
22.37 + * the boxed {@link #type()}).
22.38 + *
22.39 + * @return true, if this is supposed to be an array of values.
22.40 + */
22.41 + boolean array() default false;
22.42 }
23.1 --- a/javaquery/api/src/main/resources/org/apidesign/bck2brwsr/htmlpage/knockout-2.2.1.js Tue Mar 05 07:57:16 2013 +0100
23.2 +++ b/javaquery/api/src/main/resources/org/apidesign/bck2brwsr/htmlpage/knockout-2.2.1.js Fri May 17 10:27:41 2013 +0200
23.3 @@ -2193,7 +2193,14 @@
23.4 else
23.5 element[attrName] = attrValue;
23.6 } else if (!toRemove) {
23.7 - element.setAttribute(attrName, attrValue.toString());
23.8 + try {
23.9 + element.setAttribute(attrName, attrValue.toString());
23.10 + } catch (err) {
23.11 + // ignore for now
23.12 + if (console) {
23.13 + console.log("Can't set attribute " + attrName + " to " + attrValue + " error: " + err);
23.14 + }
23.15 + }
23.16 }
23.17
23.18 // Treat "name" specially - although you can think of it as an attribute, it also needs
24.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
24.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/Compile.java Fri May 17 10:27:41 2013 +0200
24.3 @@ -0,0 +1,203 @@
24.4 +/**
24.5 + * Back 2 Browser Bytecode Translator
24.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
24.7 + *
24.8 + * This program is free software: you can redistribute it and/or modify
24.9 + * it under the terms of the GNU General Public License as published by
24.10 + * the Free Software Foundation, version 2 of the License.
24.11 + *
24.12 + * This program is distributed in the hope that it will be useful,
24.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24.15 + * GNU General Public License for more details.
24.16 + *
24.17 + * You should have received a copy of the GNU General Public License
24.18 + * along with this program. Look for COPYING file in the top folder.
24.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
24.20 + */
24.21 +package org.apidesign.bck2brwsr.htmlpage;
24.22 +
24.23 +import java.io.ByteArrayInputStream;
24.24 +import java.io.ByteArrayOutputStream;
24.25 +import java.io.IOException;
24.26 +import java.io.InputStream;
24.27 +import java.io.OutputStream;
24.28 +import java.net.URI;
24.29 +import java.net.URISyntaxException;
24.30 +import java.util.ArrayList;
24.31 +import java.util.Arrays;
24.32 +import java.util.HashMap;
24.33 +import java.util.List;
24.34 +import java.util.Map;
24.35 +import java.util.regex.Matcher;
24.36 +import java.util.regex.Pattern;
24.37 +import javax.tools.Diagnostic;
24.38 +import javax.tools.DiagnosticListener;
24.39 +import javax.tools.FileObject;
24.40 +import javax.tools.ForwardingJavaFileManager;
24.41 +import javax.tools.JavaFileManager;
24.42 +import javax.tools.JavaFileObject;
24.43 +import javax.tools.JavaFileObject.Kind;
24.44 +import javax.tools.SimpleJavaFileObject;
24.45 +import javax.tools.StandardJavaFileManager;
24.46 +import javax.tools.StandardLocation;
24.47 +import javax.tools.ToolProvider;
24.48 +
24.49 +/**
24.50 + *
24.51 + * @author Jaroslav Tulach <jtulach@netbeans.org>
24.52 + */
24.53 +final class Compile implements DiagnosticListener<JavaFileObject> {
24.54 + private final List<Diagnostic<? extends JavaFileObject>> errors = new ArrayList<>();
24.55 + private final Map<String, byte[]> classes;
24.56 + private final String pkg;
24.57 + private final String cls;
24.58 + private final String html;
24.59 +
24.60 + private Compile(String html, String code) throws IOException {
24.61 + this.pkg = findPkg(code);
24.62 + this.cls = findCls(code);
24.63 + this.html = html;
24.64 + classes = compile(html, code);
24.65 + }
24.66 +
24.67 + /** Performs compilation of given HTML page and associated Java code
24.68 + */
24.69 + public static Compile create(String html, String code) throws IOException {
24.70 + return new Compile(html, code);
24.71 + }
24.72 +
24.73 + /** Checks for given class among compiled resources */
24.74 + public byte[] get(String res) {
24.75 + return classes.get(res);
24.76 + }
24.77 +
24.78 + /** Obtains errors created during compilation.
24.79 + */
24.80 + public List<Diagnostic<? extends JavaFileObject>> getErrors() {
24.81 + List<Diagnostic<? extends JavaFileObject>> err = new ArrayList<>();
24.82 + for (Diagnostic<? extends JavaFileObject> diagnostic : errors) {
24.83 + if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
24.84 + err.add(diagnostic);
24.85 + }
24.86 + }
24.87 + return err;
24.88 + }
24.89 +
24.90 + private Map<String, byte[]> compile(final String html, final String code) throws IOException {
24.91 + StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(this, null, null);
24.92 +
24.93 + final Map<String, ByteArrayOutputStream> class2BAOS = new HashMap<>();
24.94 +
24.95 + JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) {
24.96 + @Override
24.97 + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
24.98 + return code;
24.99 + }
24.100 + };
24.101 + final JavaFileObject htmlFile = new SimpleJavaFileObject(URI.create("mem://mem2"), Kind.OTHER) {
24.102 + @Override
24.103 + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
24.104 + return html;
24.105 + }
24.106 +
24.107 + @Override
24.108 + public InputStream openInputStream() throws IOException {
24.109 + return new ByteArrayInputStream(html.getBytes());
24.110 + }
24.111 + };
24.112 +
24.113 + final URI scratch;
24.114 + try {
24.115 + scratch = new URI("mem://mem3");
24.116 + } catch (URISyntaxException ex) {
24.117 + throw new IOException(ex);
24.118 + }
24.119 +
24.120 + JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(sjfm) {
24.121 + @Override
24.122 + public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
24.123 + if (kind == Kind.CLASS) {
24.124 + final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
24.125 +
24.126 + class2BAOS.put(className.replace('.', '/') + ".class", buffer);
24.127 + return new SimpleJavaFileObject(sibling.toUri(), kind) {
24.128 + @Override
24.129 + public OutputStream openOutputStream() throws IOException {
24.130 + return buffer;
24.131 + }
24.132 + };
24.133 + }
24.134 +
24.135 + if (kind == Kind.SOURCE) {
24.136 + return new SimpleJavaFileObject(scratch/*sibling.toUri()*/, kind) {
24.137 + private final ByteArrayOutputStream data = new ByteArrayOutputStream();
24.138 + @Override
24.139 + public OutputStream openOutputStream() throws IOException {
24.140 + return data;
24.141 + }
24.142 +
24.143 + @Override
24.144 + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
24.145 + data.close();
24.146 + return new String(data.toByteArray());
24.147 + }
24.148 + };
24.149 + }
24.150 +
24.151 + throw new IllegalStateException();
24.152 + }
24.153 +
24.154 + @Override
24.155 + public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
24.156 + if (location == StandardLocation.SOURCE_PATH) {
24.157 + if (packageName.equals(pkg)) {
24.158 + return htmlFile;
24.159 + }
24.160 + }
24.161 +
24.162 + return null;
24.163 + }
24.164 +
24.165 + };
24.166 +
24.167 + ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, /*XXX:*/Arrays.asList("-source", "1.7", "-target", "1.7"), null, Arrays.asList(file)).call();
24.168 +
24.169 + Map<String, byte[]> result = new HashMap<>();
24.170 +
24.171 + for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) {
24.172 + result.put(e.getKey(), e.getValue().toByteArray());
24.173 + }
24.174 +
24.175 + return result;
24.176 + }
24.177 +
24.178 +
24.179 + @Override
24.180 + public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
24.181 + errors.add(diagnostic);
24.182 + }
24.183 + private static String findPkg(String java) throws IOException {
24.184 + Pattern p = Pattern.compile("package\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}*;", Pattern.MULTILINE);
24.185 + Matcher m = p.matcher(java);
24.186 + if (!m.find()) {
24.187 + throw new IOException("Can't find package declaration in the java file");
24.188 + }
24.189 + String pkg = m.group(1);
24.190 + return pkg;
24.191 + }
24.192 + private static String findCls(String java) throws IOException {
24.193 + Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE);
24.194 + Matcher m = p.matcher(java);
24.195 + if (!m.find()) {
24.196 + throw new IOException("Can't find package declaration in the java file");
24.197 + }
24.198 + String cls = m.group(1);
24.199 + return cls;
24.200 + }
24.201 +
24.202 + String getHtml() {
24.203 + String fqn = "'" + pkg + '.' + cls + "'";
24.204 + return html.replace("'${fqn}'", fqn);
24.205 + }
24.206 +}
25.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
25.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypesTest.java Fri May 17 10:27:41 2013 +0200
25.3 @@ -0,0 +1,63 @@
25.4 +/**
25.5 + * Back 2 Browser Bytecode Translator
25.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
25.7 + *
25.8 + * This program is free software: you can redistribute it and/or modify
25.9 + * it under the terms of the GNU General Public License as published by
25.10 + * the Free Software Foundation, version 2 of the License.
25.11 + *
25.12 + * This program is distributed in the hope that it will be useful,
25.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25.15 + * GNU General Public License for more details.
25.16 + *
25.17 + * You should have received a copy of the GNU General Public License
25.18 + * along with this program. Look for COPYING file in the top folder.
25.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
25.20 + */
25.21 +package org.apidesign.bck2brwsr.htmlpage;
25.22 +
25.23 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
25.24 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
25.25 +import org.apidesign.bck2brwsr.vmtest.VMTest;
25.26 +import org.testng.annotations.Factory;
25.27 +
25.28 +/**
25.29 + *
25.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
25.31 + */
25.32 +public class ConvertTypesTest {
25.33 + @JavaScriptBody(args = { "includeSex" }, body = "var json = new Object();"
25.34 + + "json.firstName = 'son';\n"
25.35 + + "json.lastName = 'dj';\n"
25.36 + + "if (includeSex) json.sex = 'MALE';\n"
25.37 + + "return json;"
25.38 + )
25.39 + private static native Object createJSON(boolean includeSex);
25.40 +
25.41 + @BrwsrTest
25.42 + public void testConvertToPeople() throws Exception {
25.43 + final Object o = createJSON(true);
25.44 +
25.45 + Person p = new Person(o);
25.46 +
25.47 + assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
25.48 + assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
25.49 + assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
25.50 + }
25.51 +
25.52 + @BrwsrTest
25.53 + public void testConvertToPeopleWithoutSex() throws Exception {
25.54 + final Object o = createJSON(false);
25.55 +
25.56 + Person p = new Person(o);
25.57 +
25.58 + assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
25.59 + assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
25.60 + assert p.getSex() == null : "No sex: " + p.getSex();
25.61 + }
25.62 +
25.63 + @Factory public static Object[] create() {
25.64 + return VMTest.create(ConvertTypesTest.class);
25.65 + }
25.66 +}
25.67 \ No newline at end of file
26.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
26.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java Fri May 17 10:27:41 2013 +0200
26.3 @@ -0,0 +1,392 @@
26.4 +/**
26.5 + * Back 2 Browser Bytecode Translator
26.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
26.7 + *
26.8 + * This program is free software: you can redistribute it and/or modify
26.9 + * it under the terms of the GNU General Public License as published by
26.10 + * the Free Software Foundation, version 2 of the License.
26.11 + *
26.12 + * This program is distributed in the hope that it will be useful,
26.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26.15 + * GNU General Public License for more details.
26.16 + *
26.17 + * You should have received a copy of the GNU General Public License
26.18 + * along with this program. Look for COPYING file in the top folder.
26.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
26.20 + */
26.21 +package org.apidesign.bck2brwsr.htmlpage;
26.22 +
26.23 +import java.util.Arrays;
26.24 +import java.util.Iterator;
26.25 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
26.26 +import org.apidesign.bck2brwsr.htmlpage.api.OnReceive;
26.27 +import org.apidesign.bck2brwsr.htmlpage.api.Page;
26.28 +import org.apidesign.bck2brwsr.htmlpage.api.Property;
26.29 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
26.30 +import org.apidesign.bck2brwsr.vmtest.Http;
26.31 +import org.apidesign.bck2brwsr.vmtest.VMTest;
26.32 +import org.json.JSONException;
26.33 +import org.json.JSONObject;
26.34 +import org.json.JSONTokener;
26.35 +import org.testng.annotations.Test;
26.36 +import static org.testng.Assert.*;
26.37 +import org.testng.annotations.Factory;
26.38 +
26.39 +/** Need to verify that models produce reasonable JSON objects.
26.40 + *
26.41 + * @author Jaroslav Tulach <jtulach@netbeans.org>
26.42 + */
26.43 +@Page(xhtml = "Empty.html", className = "JSONik", properties = {
26.44 + @Property(name = "fetched", type = PersonImpl.class),
26.45 + @Property(name = "fetchedCount", type = int.class),
26.46 + @Property(name = "fetchedSex", type = Sex.class, array = true)
26.47 +})
26.48 +public class JSONTest {
26.49 + private JSONik js;
26.50 + private Integer orig;
26.51 +
26.52 + @Test public void personToString() throws JSONException {
26.53 + Person p = new Person();
26.54 + p.setSex(Sex.MALE);
26.55 + p.setFirstName("Jarda");
26.56 + p.setLastName("Tulach");
26.57 +
26.58 + JSONTokener t = new JSONTokener(p.toString());
26.59 + JSONObject o;
26.60 + try {
26.61 + o = new JSONObject(t);
26.62 + } catch (JSONException ex) {
26.63 + throw new AssertionError("Can't parse " + p.toString(), ex);
26.64 + }
26.65 +
26.66 + Iterator it = o.sortedKeys();
26.67 + assertEquals(it.next(), "firstName");
26.68 + assertEquals(it.next(), "lastName");
26.69 + assertEquals(it.next(), "sex");
26.70 +
26.71 + assertEquals(o.getString("firstName"), "Jarda");
26.72 + assertEquals(o.getString("lastName"), "Tulach");
26.73 + assertEquals(o.getString("sex"), "MALE");
26.74 + }
26.75 +
26.76 + @BrwsrTest public void toJSONInABrowser() throws Throwable {
26.77 + Person p = new Person();
26.78 + p.setSex(Sex.MALE);
26.79 + p.setFirstName("Jarda");
26.80 + p.setLastName("Tulach");
26.81 +
26.82 + Object json;
26.83 + try {
26.84 + json = parseJSON(p.toString());
26.85 + } catch (Throwable ex) {
26.86 + throw new IllegalStateException("Can't parse " + p).initCause(ex);
26.87 + }
26.88 +
26.89 + Person p2 = new Person(json);
26.90 +
26.91 + assert p2.getFirstName().equals(p.getFirstName()) :
26.92 + "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
26.93 + }
26.94 +
26.95 + @Test public void personWithWildCharactersAndNulls() throws JSONException {
26.96 + Person p = new Person();
26.97 + p.setFirstName("'\"\n");
26.98 + p.setLastName("\t\r\u0002");
26.99 +
26.100 + JSONTokener t = new JSONTokener(p.toString());
26.101 + JSONObject o;
26.102 + try {
26.103 + o = new JSONObject(t);
26.104 + } catch (JSONException ex) {
26.105 + throw new AssertionError("Can't parse " + p.toString(), ex);
26.106 + }
26.107 +
26.108 + Iterator it = o.sortedKeys();
26.109 + assertEquals(it.next(), "firstName");
26.110 + assertEquals(it.next(), "lastName");
26.111 + assertEquals(it.next(), "sex");
26.112 +
26.113 + assertEquals(o.getString("firstName"), p.getFirstName());
26.114 + assertEquals(o.getString("lastName"), p.getLastName());
26.115 + assertEquals(o.get("sex"), JSONObject.NULL);
26.116 + }
26.117 +
26.118 + @Test public void personsInArray() throws JSONException {
26.119 + Person p1 = new Person();
26.120 + p1.setFirstName("One");
26.121 +
26.122 + Person p2 = new Person();
26.123 + p2.setFirstName("Two");
26.124 +
26.125 + People arr = new People();
26.126 + arr.getInfo().add(p1);
26.127 + arr.getInfo().add(p2);
26.128 + arr.getNicknames().add("Prvn\u00ed k\u016f\u0148");
26.129 + final String n2 = "Druh\u00fd hlem\u00fd\u017e\u010f, star\u0161\u00ed";
26.130 + arr.getNicknames().add(n2);
26.131 + arr.getAge().add(33);
26.132 + arr.getAge().add(73);
26.133 +
26.134 +
26.135 + final String json = arr.toString();
26.136 +
26.137 + JSONTokener t = new JSONTokener(json);
26.138 + JSONObject o;
26.139 + try {
26.140 + o = new JSONObject(t);
26.141 + } catch (JSONException ex) {
26.142 + throw new AssertionError("Can't parse " + json, ex);
26.143 + }
26.144 +
26.145 + assertEquals(o.getJSONArray("info").getJSONObject(0).getString("firstName"), "One");
26.146 + assertEquals(o.getJSONArray("nicknames").getString(1), n2);
26.147 + assertEquals(o.getJSONArray("age").getInt(1), 73);
26.148 + }
26.149 +
26.150 +
26.151 + @OnReceive(url="/{url}")
26.152 + static void fetch(Person p, JSONik model) {
26.153 + model.setFetched(p);
26.154 + }
26.155 +
26.156 + @OnReceive(url="/{url}")
26.157 + static void fetchArray(Person[] p, JSONik model) {
26.158 + model.setFetchedCount(p.length);
26.159 + model.setFetched(p[0]);
26.160 + }
26.161 +
26.162 + @OnReceive(url="/{url}")
26.163 + static void fetchPeople(People p, JSONik model) {
26.164 + model.setFetchedCount(p.getInfo().size());
26.165 + model.setFetched(p.getInfo().get(0));
26.166 + }
26.167 +
26.168 + @OnReceive(url="/{url}")
26.169 + static void fetchPeopleAge(People p, JSONik model) {
26.170 + int sum = 0;
26.171 + for (int a : p.getAge()) {
26.172 + sum += a;
26.173 + }
26.174 + model.setFetchedCount(sum);
26.175 + }
26.176 +
26.177 + @Http(@Http.Resource(
26.178 + content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
26.179 + path="/person.json",
26.180 + mimeType = "application/json"
26.181 + ))
26.182 + @BrwsrTest public void loadAndParseJSON() throws InterruptedException {
26.183 + if (js == null) {
26.184 + js = new JSONik();
26.185 + js.applyBindings();
26.186 +
26.187 + js.fetch("person.json");
26.188 + }
26.189 +
26.190 + Person p = js.getFetched();
26.191 + if (p == null) {
26.192 + throw new InterruptedException();
26.193 + }
26.194 +
26.195 + assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
26.196 + assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
26.197 + }
26.198 +
26.199 + @OnReceive(url="/{url}?callme={me}", jsonp = "me")
26.200 + static void fetchViaJSONP(Person p, JSONik model) {
26.201 + model.setFetched(p);
26.202 + }
26.203 +
26.204 + @Http(@Http.Resource(
26.205 + content = "$0({'firstName': 'Mitar', 'sex': 'MALE'})",
26.206 + path="/person.json",
26.207 + mimeType = "application/javascript",
26.208 + parameters = { "callme" }
26.209 + ))
26.210 + @BrwsrTest public void loadAndParseJSONP() throws InterruptedException {
26.211 +
26.212 + if (js == null) {
26.213 + orig = scriptElements();
26.214 + assert orig > 0 : "There should be some scripts on the page";
26.215 +
26.216 + js = new JSONik();
26.217 + js.applyBindings();
26.218 +
26.219 + js.fetchViaJSONP("person.json");
26.220 + }
26.221 +
26.222 + Person p = js.getFetched();
26.223 + if (p == null) {
26.224 + throw new InterruptedException();
26.225 + }
26.226 +
26.227 + assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName();
26.228 + assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
26.229 +
26.230 + int now = scriptElements();
26.231 +
26.232 + assert orig == now : "The set of elements is unchanged. Delta: " + (now - orig);
26.233 + }
26.234 +
26.235 + @JavaScriptBody(args = { }, body = "return window.document.getElementsByTagName('script').length;")
26.236 + private static native int scriptElements();
26.237 +
26.238 + @JavaScriptBody(args = { "s" }, body = "return window.JSON.parse(s);")
26.239 + private static native Object parseJSON(String s);
26.240 +
26.241 + @Http(@Http.Resource(
26.242 + content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
26.243 + path="/person.json",
26.244 + mimeType = "application/json"
26.245 + ))
26.246 + @BrwsrTest public void loadAndParseJSONSentToArray() throws InterruptedException {
26.247 + if (js == null) {
26.248 + js = new JSONik();
26.249 + js.applyBindings();
26.250 +
26.251 + js.fetchArray("person.json");
26.252 + }
26.253 +
26.254 + Person p = js.getFetched();
26.255 + if (p == null) {
26.256 + throw new InterruptedException();
26.257 + }
26.258 +
26.259 + assert p != null : "We should get our person back: " + p;
26.260 + assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
26.261 + assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
26.262 + }
26.263 +
26.264 + @Http(@Http.Resource(
26.265 + content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]",
26.266 + path="/person.json",
26.267 + mimeType = "application/json"
26.268 + ))
26.269 + @BrwsrTest public void loadAndParseJSONArraySingle() throws InterruptedException {
26.270 + if (js == null) {
26.271 + js = new JSONik();
26.272 + js.applyBindings();
26.273 +
26.274 + js.fetch("person.json");
26.275 + }
26.276 +
26.277 + Person p = js.getFetched();
26.278 + if (p == null) {
26.279 + throw new InterruptedException();
26.280 + }
26.281 +
26.282 + assert p != null : "We should get our person back: " + p;
26.283 + assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
26.284 + assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
26.285 + }
26.286 +
26.287 + @Http(@Http.Resource(
26.288 + content = "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}",
26.289 + path="/people.json",
26.290 + mimeType = "application/json"
26.291 + ))
26.292 + @BrwsrTest public void loadAndParseArrayInPeople() throws InterruptedException {
26.293 + if (js == null) {
26.294 + js = new JSONik();
26.295 + js.applyBindings();
26.296 +
26.297 + js.fetchPeople("people.json");
26.298 + }
26.299 +
26.300 + if (0 == js.getFetchedCount()) {
26.301 + throw new InterruptedException();
26.302 + }
26.303 +
26.304 + assert js.getFetchedCount() == 1 : "One person loaded: " + js.getFetchedCount();
26.305 +
26.306 + Person p = js.getFetched();
26.307 +
26.308 + assert p != null : "We should get our person back: " + p;
26.309 + assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
26.310 + assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
26.311 + }
26.312 +
26.313 + @Http(@Http.Resource(
26.314 + content = "{'age':[1, 2, 3]}",
26.315 + path="/people.json",
26.316 + mimeType = "application/json"
26.317 + ))
26.318 + @BrwsrTest public void loadAndParseArrayOfIntegers() throws InterruptedException {
26.319 + if (js == null) {
26.320 + js = new JSONik();
26.321 + js.applyBindings();
26.322 +
26.323 + js.fetchPeopleAge("people.json");
26.324 + }
26.325 +
26.326 + if (0 == js.getFetchedCount()) {
26.327 + throw new InterruptedException();
26.328 + }
26.329 +
26.330 + assert js.getFetchedCount() == 6 : "1 + 2 + 3 is " + js.getFetchedCount();
26.331 + }
26.332 +
26.333 + @OnReceive(url="/{url}")
26.334 + static void fetchPeopleSex(People p, JSONik model) {
26.335 + model.setFetchedCount(1);
26.336 + model.getFetchedSex().addAll(p.getSex());
26.337 + }
26.338 +
26.339 +
26.340 + @Http(@Http.Resource(
26.341 + content = "{'sex':['FEMALE', 'MALE', 'MALE']}",
26.342 + path="/people.json",
26.343 + mimeType = "application/json"
26.344 + ))
26.345 + @BrwsrTest public void loadAndParseArrayOfEnums() throws InterruptedException {
26.346 + if (js == null) {
26.347 + js = new JSONik();
26.348 + js.applyBindings();
26.349 +
26.350 + js.fetchPeopleSex("people.json");
26.351 + }
26.352 +
26.353 + if (0 == js.getFetchedCount()) {
26.354 + throw new InterruptedException();
26.355 + }
26.356 +
26.357 + assert js.getFetchedCount() == 1 : "Loaded";
26.358 +
26.359 + assert js.getFetchedSex().size() == 3 : "Three values " + js.getFetchedSex();
26.360 + assert js.getFetchedSex().get(0) == Sex.FEMALE : "Female first " + js.getFetchedSex();
26.361 + assert js.getFetchedSex().get(1) == Sex.MALE : "male 2nd " + js.getFetchedSex();
26.362 + assert js.getFetchedSex().get(2) == Sex.MALE : "male 3rd " + js.getFetchedSex();
26.363 + }
26.364 +
26.365 + @Http(@Http.Resource(
26.366 + content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'},"
26.367 + + "{'firstName': 'Peter', 'sex': 'MALE'}"
26.368 + + "]",
26.369 + path="/person.json",
26.370 + mimeType = "application/json"
26.371 + ))
26.372 + @BrwsrTest public void loadAndParseJSONArray() throws InterruptedException {
26.373 + if (js == null) {
26.374 + js = new JSONik();
26.375 + js.applyBindings();
26.376 + js.fetchArray("person.json");
26.377 + }
26.378 +
26.379 +
26.380 + Person p = js.getFetched();
26.381 + if (p == null) {
26.382 + throw new InterruptedException();
26.383 + }
26.384 +
26.385 + assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount();
26.386 + assert p != null : "We should get our person back: " + p;
26.387 + assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
26.388 + assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
26.389 + }
26.390 +
26.391 + @Factory public static Object[] create() {
26.392 + return VMTest.create(JSONTest.class);
26.393 + }
26.394 +
26.395 +}
27.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java Tue Mar 05 07:57:16 2013 +0100
27.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java Fri May 17 10:27:41 2013 +0200
27.3 @@ -17,21 +17,29 @@
27.4 */
27.5 package org.apidesign.bck2brwsr.htmlpage;
27.6
27.7 +import java.util.List;
27.8 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
27.9 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
27.10 import org.apidesign.bck2brwsr.htmlpage.api.OnEvent;
27.11 +import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
27.12 import org.apidesign.bck2brwsr.htmlpage.api.Page;
27.13 import org.apidesign.bck2brwsr.htmlpage.api.Property;
27.14 import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
27.15 import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
27.16 import org.apidesign.bck2brwsr.vmtest.VMTest;
27.17 +import static org.testng.Assert.assertEquals;
27.18 import org.testng.annotations.Factory;
27.19 +import org.testng.annotations.Test;
27.20
27.21 /**
27.22 *
27.23 * @author Jaroslav Tulach <jtulach@netbeans.org>
27.24 */
27.25 @Page(xhtml="Knockout.xhtml", className="KnockoutModel", properties={
27.26 - @Property(name="name", type=String.class)
27.27 + @Property(name="name", type=String.class),
27.28 + @Property(name="results", type=String.class, array = true),
27.29 + @Property(name="callbackCount", type=int.class),
27.30 + @Property(name="people", type=PersonImpl.class, array = true)
27.31 })
27.32 public class KnockoutTest {
27.33
27.34 @@ -44,19 +52,207 @@
27.35 KnockoutModel m = new KnockoutModel();
27.36 m.setName("Kukuc");
27.37 m.applyBindings();
27.38 - assert "Kukuc".equals(m.INPUT.getValue()) : "Value is really kukuc: " + m.INPUT.getValue();
27.39 - m.INPUT.setValue("Jardo");
27.40 - m.triggerEvent(m.INPUT, OnEvent.CHANGE);
27.41 + assert "Kukuc".equals(m.input.getValue()) : "Value is really kukuc: " + m.input.getValue();
27.42 + m.input.setValue("Jardo");
27.43 + m.triggerEvent(m.input, OnEvent.CHANGE);
27.44 assert "Jardo".equals(m.getName()) : "Name property updated: " + m.getName();
27.45 }
27.46
27.47 + @HtmlFragment(
27.48 + "<ul id='ul' data-bind='foreach: results'>\n"
27.49 + + " <li data-bind='text: $data, click: $root.call'/>\n"
27.50 + + "</ul>\n"
27.51 + )
27.52 + @BrwsrTest public void displayContentOfArray() {
27.53 + KnockoutModel m = new KnockoutModel();
27.54 + m.getResults().add("Ahoj");
27.55 + m.applyBindings();
27.56 +
27.57 + int cnt = countChildren("ul");
27.58 + assert cnt == 1 : "One child, but was " + cnt;
27.59 +
27.60 + m.getResults().add("Hi");
27.61 +
27.62 + cnt = countChildren("ul");
27.63 + assert cnt == 2 : "Two children now, but was " + cnt;
27.64 +
27.65 + triggerChildClick("ul", 1);
27.66 +
27.67 + assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
27.68 + assert "Hi".equals(m.getName()) : "We got callback from 2nd child " + m.getName();
27.69 + }
27.70 +
27.71 + @HtmlFragment(
27.72 + "<ul id='ul' data-bind='foreach: cmpResults'>\n"
27.73 + + " <li><b data-bind='text: $data'></b></li>\n"
27.74 + + "</ul>\n"
27.75 + )
27.76 + @BrwsrTest public void displayContentOfDerivedArray() {
27.77 + KnockoutModel m = new KnockoutModel();
27.78 + m.getResults().add("Ahoj");
27.79 + m.applyBindings();
27.80 +
27.81 + int cnt = countChildren("ul");
27.82 + assert cnt == 1 : "One child, but was " + cnt;
27.83 +
27.84 + m.getResults().add("hello");
27.85 +
27.86 + cnt = countChildren("ul");
27.87 + assert cnt == 2 : "Two children now, but was " + cnt;
27.88 + }
27.89 +
27.90 + @HtmlFragment(
27.91 + "<ul id='ul' data-bind='foreach: people'>\n"
27.92 + + " <li data-bind='text: $data.firstName, click: $root.removePerson'></li>\n"
27.93 + + "</ul>\n"
27.94 + )
27.95 + @BrwsrTest public void displayContentOfArrayOfPeople() {
27.96 + KnockoutModel m = new KnockoutModel();
27.97 +
27.98 + final Person first = new Person();
27.99 + first.setFirstName("first");
27.100 + m.getPeople().add(first);
27.101 +
27.102 + m.applyBindings();
27.103 +
27.104 + int cnt = countChildren("ul");
27.105 + assert cnt == 1 : "One child, but was " + cnt;
27.106 +
27.107 + final Person second = new Person();
27.108 + second.setFirstName("second");
27.109 + m.getPeople().add(second);
27.110 +
27.111 + cnt = countChildren("ul");
27.112 + assert cnt == 2 : "Two children now, but was " + cnt;
27.113 +
27.114 + triggerChildClick("ul", 1);
27.115 +
27.116 + assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
27.117 +
27.118 + cnt = countChildren("ul");
27.119 + assert cnt == 1 : "Again one child, but was " + cnt;
27.120 +
27.121 + String txt = childText("ul", 0);
27.122 + assert "first".equals(txt) : "Expecting 'first': " + txt;
27.123 +
27.124 + first.setFirstName("changed");
27.125 +
27.126 + txt = childText("ul", 0);
27.127 + assert "changed".equals(txt) : "Expecting 'changed': " + txt;
27.128 + }
27.129 +
27.130 + @ComputedProperty
27.131 + static Person firstPerson(List<Person> people) {
27.132 + return people.isEmpty() ? null : people.get(0);
27.133 + }
27.134 +
27.135 + @HtmlFragment(
27.136 + "<p id='ul' data-bind='with: firstPerson'>\n"
27.137 + + " <span data-bind='text: firstName, click: changeSex'></span>\n"
27.138 + + "</p>\n"
27.139 + )
27.140 + @BrwsrTest public void accessFirstPersonWithOnFunction() {
27.141 + trasfertToFemale();
27.142 + }
27.143 +
27.144 + @HtmlFragment(
27.145 + "<ul id='ul' data-bind='foreach: people'>\n"
27.146 + + " <li data-bind='text: $data.firstName, click: changeSex'></li>\n"
27.147 + + "</ul>\n"
27.148 + )
27.149 + @BrwsrTest public void onPersonFunction() {
27.150 + trasfertToFemale();
27.151 + }
27.152 +
27.153 + private void trasfertToFemale() {
27.154 + KnockoutModel m = new KnockoutModel();
27.155 +
27.156 + final Person first = new Person();
27.157 + first.setFirstName("first");
27.158 + first.setSex(Sex.MALE);
27.159 + m.getPeople().add(first);
27.160 +
27.161 +
27.162 + m.applyBindings();
27.163 +
27.164 + int cnt = countChildren("ul");
27.165 + assert cnt == 1 : "One child, but was " + cnt;
27.166 +
27.167 +
27.168 + triggerChildClick("ul", 0);
27.169 +
27.170 + assert first.getSex() == Sex.FEMALE : "Transverted to female: " + first.getSex();
27.171 + }
27.172 +
27.173 + @Test public void cloneModel() {
27.174 + Person model = new Person();
27.175 +
27.176 + model.setFirstName("first");
27.177 + Person snd = model.clone();
27.178 + snd.setFirstName("clone");
27.179 + assertEquals("first", model.getFirstName(), "Value has not changed");
27.180 + assertEquals("clone", snd.getFirstName(), "Value has changed in clone");
27.181 + }
27.182 +
27.183 +
27.184 + @Test public void deepCopyOnClone() {
27.185 + People model = new People();
27.186 + model.getNicknames().add("Jarda");
27.187 + assertEquals(model.getNicknames().size(), 1, "One element");
27.188 + People snd = model.clone();
27.189 + snd.getNicknames().clear();
27.190 + assertEquals(snd.getNicknames().size(), 0, "Clone is empty");
27.191 + assertEquals(model.getNicknames().size(), 1, "Still one element");
27.192 + }
27.193 +
27.194 +
27.195 + @OnFunction
27.196 + static void call(KnockoutModel m, String data) {
27.197 + m.setName(data);
27.198 + m.setCallbackCount(m.getCallbackCount() + 1);
27.199 + }
27.200 +
27.201 + @OnFunction
27.202 + static void removePerson(KnockoutModel model, Person data) {
27.203 + model.setCallbackCount(model.getCallbackCount() + 1);
27.204 + model.getPeople().remove(data);
27.205 + }
27.206 +
27.207 +
27.208 @ComputedProperty
27.209 static String helloMessage(String name) {
27.210 return "Hello " + name + "!";
27.211 }
27.212
27.213 + @ComputedProperty
27.214 + static List<String> cmpResults(List<String> results) {
27.215 + return results;
27.216 + }
27.217 +
27.218 @Factory
27.219 public static Object[] create() {
27.220 return VMTest.create(KnockoutTest.class);
27.221 }
27.222 +
27.223 + @JavaScriptBody(args = { "id" }, body =
27.224 + "var e = window.document.getElementById(id);\n "
27.225 + + "if (typeof e === 'undefined') return -2;\n "
27.226 + + "return e.children.length;\n "
27.227 + )
27.228 + private static native int countChildren(String id);
27.229 +
27.230 + @JavaScriptBody(args = { "id", "pos" }, body =
27.231 + "var e = window.document.getElementById(id);\n "
27.232 + + "var ev = window.document.createEvent('MouseEvents');\n "
27.233 + + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
27.234 + + "e.children[pos].dispatchEvent(ev);\n "
27.235 + )
27.236 + private static native void triggerChildClick(String id, int pos);
27.237 +
27.238 + @JavaScriptBody(args = { "id", "pos" }, body =
27.239 + "var e = window.document.getElementById(id);\n "
27.240 + + "var t = e.children[pos].innerHTML;\n "
27.241 + + "return t ? t : null;"
27.242 + )
27.243 + private static native String childText(String id, int pos);
27.244 }
28.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java Tue Mar 05 07:57:16 2013 +0100
28.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java Fri May 17 10:27:41 2013 +0200
28.3 @@ -18,8 +18,13 @@
28.4 package org.apidesign.bck2brwsr.htmlpage;
28.5
28.6 import java.util.ArrayList;
28.7 +import java.util.Collections;
28.8 +import java.util.Iterator;
28.9 import java.util.List;
28.10 +import java.util.ListIterator;
28.11 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
28.12 +import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
28.13 +import org.apidesign.bck2brwsr.htmlpage.api.OnPropertyChange;
28.14 import org.apidesign.bck2brwsr.htmlpage.api.Page;
28.15 import org.apidesign.bck2brwsr.htmlpage.api.Property;
28.16 import static org.testng.Assert.*;
28.17 @@ -30,17 +35,22 @@
28.18 *
28.19 * @author Jaroslav Tulach <jtulach@netbeans.org>
28.20 */
28.21 -@Page(xhtml = "Empty.html", className = "Model", properties = {
28.22 +@Page(xhtml = "Empty.html", className = "Modelik", properties = {
28.23 @Property(name = "value", type = int.class),
28.24 - @Property(name = "unrelated", type = long.class)
28.25 + @Property(name = "count", type = int.class),
28.26 + @Property(name = "unrelated", type = long.class),
28.27 + @Property(name = "names", type = String.class, array = true),
28.28 + @Property(name = "values", type = int.class, array = true),
28.29 + @Property(name = "people", type = PersonImpl.class, array = true),
28.30 + @Property(name = "changedProperty", type=String.class)
28.31 })
28.32 public class ModelTest {
28.33 - private Model model;
28.34 - private static Model leakedModel;
28.35 + private Modelik model;
28.36 + private static Modelik leakedModel;
28.37
28.38 @BeforeMethod
28.39 public void createModel() {
28.40 - model = new Model();
28.41 + model = new Modelik();
28.42 }
28.43
28.44 @Test public void classGeneratedWithSetterGetter() {
28.45 @@ -53,6 +63,75 @@
28.46 assertEquals(16, model.getPowerValue());
28.47 }
28.48
28.49 + @Test public void arrayIsMutable() {
28.50 + assertEquals(model.getNames().size(), 0, "Is empty");
28.51 + model.getNames().add("Jarda");
28.52 + assertEquals(model.getNames().size(), 1, "One element");
28.53 + }
28.54 +
28.55 + @Test public void arrayChangesNotified() {
28.56 + MockKnockout my = new MockKnockout();
28.57 + MockKnockout.next = my;
28.58 +
28.59 + model.applyBindings();
28.60 +
28.61 + model.getNames().add("Hello");
28.62 +
28.63 + assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
28.64 + assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
28.65 +
28.66 + my.mutated.clear();
28.67 +
28.68 + Iterator<String> it = model.getNames().iterator();
28.69 + assertEquals(it.next(), "Hello");
28.70 + it.remove();
28.71 +
28.72 + assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
28.73 + assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
28.74 +
28.75 + my.mutated.clear();
28.76 +
28.77 + ListIterator<String> lit = model.getNames().listIterator();
28.78 + lit.add("Jarda");
28.79 +
28.80 + assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
28.81 + assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
28.82 + }
28.83 +
28.84 + @Test public void autoboxedArray() {
28.85 + MockKnockout my = new MockKnockout();
28.86 + MockKnockout.next = my;
28.87 +
28.88 + model.applyBindings();
28.89 +
28.90 + model.getValues().add(10);
28.91 +
28.92 + assertEquals(model.getValues().get(0), Integer.valueOf(10), "Really ten");
28.93 + }
28.94 +
28.95 + @Test public void derivedArrayProp() {
28.96 + MockKnockout my = new MockKnockout();
28.97 + MockKnockout.next = my;
28.98 +
28.99 + model.applyBindings();
28.100 +
28.101 + model.setCount(10);
28.102 +
28.103 + List<String> arr = model.getRepeat();
28.104 + assertEquals(arr.size(), 10, "Ten items: " + arr);
28.105 +
28.106 + my.mutated.clear();
28.107 +
28.108 + model.setCount(5);
28.109 +
28.110 + arr = model.getRepeat();
28.111 + assertEquals(arr.size(), 5, "Five items: " + arr);
28.112 +
28.113 + assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
28.114 + assertTrue(my.mutated.contains("repeat"), "Array is in there: " + my.mutated);
28.115 + assertTrue(my.mutated.contains("count"), "Count is in there: " + my.mutated);
28.116 + }
28.117 +
28.118 @Test public void derivedPropertiesAreNotified() {
28.119 MockKnockout my = new MockKnockout();
28.120 MockKnockout.next = my;
28.121 @@ -61,6 +140,9 @@
28.122
28.123 model.setValue(33);
28.124
28.125 + // not interested in change of this property
28.126 + my.mutated.remove("changedProperty");
28.127 +
28.128 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
28.129 assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
28.130 assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
28.131 @@ -68,7 +150,11 @@
28.132 my.mutated.clear();
28.133
28.134 model.setUnrelated(44);
28.135 - assertEquals(my.mutated.size(), 1, "One property changed");
28.136 +
28.137 +
28.138 + // not interested in change of this property
28.139 + my.mutated.remove("changedProperty");
28.140 + assertEquals(my.mutated.size(), 1, "One property changed: " + my.mutated);
28.141 assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
28.142 }
28.143
28.144 @@ -92,11 +178,43 @@
28.145 }
28.146 }
28.147
28.148 + @OnFunction
28.149 + static void doSomething() {
28.150 + }
28.151 +
28.152 @ComputedProperty
28.153 static int powerValue(int value) {
28.154 return value * value;
28.155 }
28.156
28.157 + @OnPropertyChange({ "powerValue", "unrelated" })
28.158 + static void aPropertyChanged(Modelik m, String name) {
28.159 + m.setChangedProperty(name);
28.160 + }
28.161 +
28.162 + @OnPropertyChange({ "values" })
28.163 + static void anArrayPropertyChanged(String name, Modelik m) {
28.164 + m.setChangedProperty(name);
28.165 + }
28.166 +
28.167 + @Test public void changeAnything() {
28.168 + model.setCount(44);
28.169 + assertNull(model.getChangedProperty(), "No observed value change");
28.170 + }
28.171 + @Test public void changeValue() {
28.172 + model.setValue(33);
28.173 + assertEquals(model.getChangedProperty(), "powerValue", "power property changed");
28.174 + }
28.175 + @Test public void changeUnrelated() {
28.176 + model.setUnrelated(333);
28.177 + assertEquals(model.getChangedProperty(), "unrelated", "unrelated changed");
28.178 + }
28.179 +
28.180 + @Test public void changeInArray() {
28.181 + model.getValues().add(10);
28.182 + assertEquals(model.getChangedProperty(), "values", "Something added into the array");
28.183 + }
28.184 +
28.185 @ComputedProperty
28.186 static String notAllowedRead() {
28.187 return "Not allowed callback: " + leakedModel.getUnrelated();
28.188 @@ -108,12 +226,31 @@
28.189 return "Not allowed callback!";
28.190 }
28.191
28.192 + @ComputedProperty
28.193 + static List<String> repeat(int count) {
28.194 + return Collections.nCopies(count, "Hello");
28.195 + }
28.196 +
28.197 static class MockKnockout extends Knockout {
28.198 - List<String> mutated = new ArrayList<String>();
28.199 + List<String> mutated = new ArrayList<>();
28.200 +
28.201 + MockKnockout() {
28.202 + super(null);
28.203 + }
28.204
28.205 @Override
28.206 public void valueHasMutated(String prop) {
28.207 mutated.add(prop);
28.208 }
28.209 }
28.210 +
28.211 + public @Test void hasPersonPropertyAndComputedFullName() {
28.212 + List<Person> arr = model.getPeople();
28.213 + assertEquals(arr.size(), 0, "By default empty");
28.214 + Person p = null;
28.215 + if (p != null) {
28.216 + String fullNameGenerated = p.getFullName();
28.217 + assertNotNull(fullNameGenerated);
28.218 + }
28.219 + }
28.220 }
29.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PageController.java Tue Mar 05 07:57:16 2013 +0100
29.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PageController.java Fri May 17 10:27:41 2013 +0200
29.3 @@ -50,7 +50,7 @@
29.4 if (PAGE != ref) {
29.5 throw new IllegalStateException("Both references should be the same. " + ref + " != " + PAGE);
29.6 }
29.7 - ref.PG_TITLE.setText("You want this window to be named " + ref.PG_TEXT.getValue());
29.8 + ref.pg_title.setText("You want this window to be named " + ref.pg_text.getValue());
29.9 }
29.10
29.11 @On(event = CLICK, id={ "pg.title", "pg.text" })
29.12 @@ -58,6 +58,11 @@
29.13 if (!id.equals("pg.title")) {
29.14 throw new IllegalStateException();
29.15 }
29.16 - PAGE.PG_TITLE.setText(id);
29.17 + PAGE.pg_title.setText(id);
29.18 + }
29.19 +
29.20 + @On(event = CLICK, id={ "pg.canvas" })
29.21 + static void clickCanvas(String id, double layerX) {
29.22 + PAGE.pg_canvas.setWidth((int) layerX);
29.23 }
29.24 }
30.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
30.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PageTest.java Fri May 17 10:27:41 2013 +0200
30.3 @@ -0,0 +1,53 @@
30.4 +/**
30.5 + * Back 2 Browser Bytecode Translator
30.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
30.7 + *
30.8 + * This program is free software: you can redistribute it and/or modify
30.9 + * it under the terms of the GNU General Public License as published by
30.10 + * the Free Software Foundation, version 2 of the License.
30.11 + *
30.12 + * This program is distributed in the hope that it will be useful,
30.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30.15 + * GNU General Public License for more details.
30.16 + *
30.17 + * You should have received a copy of the GNU General Public License
30.18 + * along with this program. Look for COPYING file in the top folder.
30.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
30.20 + */
30.21 +package org.apidesign.bck2brwsr.htmlpage;
30.22 +
30.23 +import java.io.IOException;
30.24 +import java.util.Locale;
30.25 +import javax.tools.Diagnostic;
30.26 +import javax.tools.JavaFileObject;
30.27 +import static org.testng.Assert.*;
30.28 +import org.testng.annotations.Test;
30.29 +
30.30 +/** Verify errors emitted by the processor.
30.31 + *
30.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
30.33 + */
30.34 +public class PageTest {
30.35 + @Test public void verifyWrongType() throws IOException {
30.36 + String html = "<html><body>"
30.37 + + "</body></html>";
30.38 + String code = "package x.y.z;\n"
30.39 + + "import org.apidesign.bck2brwsr.htmlpage.api.*;\n"
30.40 + + "@Page(xhtml=\"index.xhtml\", className=\"Model\", properties={\n"
30.41 + + " @Property(name=\"prop\", type=Runnable.class)\n"
30.42 + + "})\n"
30.43 + + "class X {\n"
30.44 + + "}\n";
30.45 +
30.46 + Compile c = Compile.create(html, code);
30.47 + assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
30.48 + for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
30.49 + String msg = e.getMessage(Locale.ENGLISH);
30.50 + if (!msg.contains("Runnable")) {
30.51 + fail("Should contain warning about Runnable: " + msg);
30.52 + }
30.53 + }
30.54 + }
30.55 +
30.56 +}
31.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
31.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PersonImpl.java Fri May 17 10:27:41 2013 +0200
31.3 @@ -0,0 +1,62 @@
31.4 +/**
31.5 + * Back 2 Browser Bytecode Translator
31.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
31.7 + *
31.8 + * This program is free software: you can redistribute it and/or modify
31.9 + * it under the terms of the GNU General Public License as published by
31.10 + * the Free Software Foundation, version 2 of the License.
31.11 + *
31.12 + * This program is distributed in the hope that it will be useful,
31.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31.15 + * GNU General Public License for more details.
31.16 + *
31.17 + * You should have received a copy of the GNU General Public License
31.18 + * along with this program. Look for COPYING file in the top folder.
31.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
31.20 + */
31.21 +package org.apidesign.bck2brwsr.htmlpage;
31.22 +
31.23 +import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
31.24 +import org.apidesign.bck2brwsr.htmlpage.api.Model;
31.25 +import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
31.26 +import org.apidesign.bck2brwsr.htmlpage.api.Property;
31.27 +
31.28 +/**
31.29 + *
31.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
31.31 + */
31.32 +@Model(className = "Person", properties = {
31.33 + @Property(name = "firstName", type = String.class),
31.34 + @Property(name = "lastName", type = String.class),
31.35 + @Property(name = "sex", type = Sex.class)
31.36 +})
31.37 +final class PersonImpl {
31.38 + @ComputedProperty
31.39 + public static String fullName(String firstName, String lastName) {
31.40 + return firstName + " " + lastName;
31.41 + }
31.42 +
31.43 + @ComputedProperty
31.44 + public static String sexType(Sex sex) {
31.45 + return sex == null ? "unknown" : sex.toString();
31.46 + }
31.47 +
31.48 + @OnFunction
31.49 + static void changeSex(Person p) {
31.50 + if (p.getSex() == Sex.MALE) {
31.51 + p.setSex(Sex.FEMALE);
31.52 + } else {
31.53 + p.setSex(Sex.MALE);
31.54 + }
31.55 + }
31.56 +
31.57 + @Model(className = "People", properties = {
31.58 + @Property(array = true, name = "info", type = Person.class),
31.59 + @Property(array = true, name = "nicknames", type = String.class),
31.60 + @Property(array = true, name = "age", type = int.class),
31.61 + @Property(array = true, name = "sex", type = Sex.class)
31.62 + })
31.63 + public class PeopleImpl {
31.64 + }
31.65 +}
32.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java Tue Mar 05 07:57:16 2013 +0100
32.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java Fri May 17 10:27:41 2013 +0200
32.3 @@ -36,11 +36,12 @@
32.4 assertNotNull(is, "Sample HTML page found");
32.5 ProcessPage res = ProcessPage.readPage(is);
32.6 final Set<String> ids = res.ids();
32.7 - assertEquals(ids.size(), 3, "Three ids found: " + ids);
32.8 + assertEquals(ids.size(), 4, "Four ids found: " + ids);
32.9
32.10 assertEquals(res.tagNameForId("pg.title"), "title");
32.11 assertEquals(res.tagNameForId("pg.button"), "button");
32.12 assertEquals(res.tagNameForId("pg.text"), "input");
32.13 + assertEquals(res.tagNameForId("pg.canvas"), "canvas");
32.14 }
32.15
32.16 @Test public void testCompileAndRunPageController() throws Exception {
32.17 @@ -53,11 +54,13 @@
32.18 + "doc.title.innerHTML = 'nothing';\n"
32.19 + "doc.text = new Object();\n"
32.20 + "doc.text.value = 'something';\n"
32.21 + + "doc.canvas = new Object();\n"
32.22 + "doc.getElementById = function(id) {\n"
32.23 + " switch(id) {\n"
32.24 + " case 'pg.button': return doc.button;\n"
32.25 + " case 'pg.title': return doc.title;\n"
32.26 + " case 'pg.text': return doc.text;\n"
32.27 + + " case 'pg.canvas': return doc.canvas;\n"
32.28 + " }\n"
32.29 + " throw id;\n"
32.30 + " }\n"
32.31 @@ -92,11 +95,13 @@
32.32 + "doc.title.innerHTML = 'nothing';\n"
32.33 + "doc.text = new Object();\n"
32.34 + "doc.text.value = 'something';\n"
32.35 + + "doc.canvas = new Object();\n"
32.36 + "doc.getElementById = function(id) {\n"
32.37 + " switch(id) {\n"
32.38 + " case 'pg.button': return doc.button;\n"
32.39 + " case 'pg.title': return doc.title;\n"
32.40 + " case 'pg.text': return doc.text;\n"
32.41 + + " case 'pg.canvas': return doc.canvas;\n"
32.42 + " }\n"
32.43 + " throw id;\n"
32.44 + " }\n"
32.45 @@ -123,12 +128,61 @@
32.46 assertEquals(ret, "pg.title", "Title has been passed to the method argument");
32.47 }
32.48
32.49 + @Test public void clickWithArgumentAndParameterCalled() throws Exception {
32.50 + StringBuilder sb = new StringBuilder();
32.51 + sb.append(
32.52 + "var window = new Object();\n"
32.53 + + "var doc = new Object();\n"
32.54 + + "var eventObject = new Object();\n"
32.55 + + "eventObject.layerX = 100;\n"
32.56 + + "doc.button = new Object();\n"
32.57 + + "doc.title = new Object();\n"
32.58 + + "doc.title.innerHTML = 'nothing';\n"
32.59 + + "doc.text = new Object();\n"
32.60 + + "doc.text.value = 'something';\n"
32.61 + + "doc.canvas = new Object();\n"
32.62 + + "doc.canvas.width = 200;\n"
32.63 + + "doc.getElementById = function(id) {\n"
32.64 + + " switch(id) {\n"
32.65 + + " case 'pg.button': return doc.button;\n"
32.66 + + " case 'pg.title': return doc.title;\n"
32.67 + + " case 'pg.text': return doc.text;\n"
32.68 + + " case 'pg.canvas': return doc.canvas;\n"
32.69 + + " }\n"
32.70 + + " throw id;\n"
32.71 + + " }\n"
32.72 + + "\n"
32.73 + + "function clickAndCheck() {\n"
32.74 + + " doc.canvas.onclick(eventObject);\n"
32.75 + + " return doc.canvas.width.toString();\n"
32.76 + + "};\n"
32.77 + + "\n"
32.78 + + "window.document = doc;\n"
32.79 + );
32.80 + Invocable i = compileClass(sb,
32.81 + "org/apidesign/bck2brwsr/htmlpage/PageController"
32.82 + );
32.83 +
32.84 + Object ret = null;
32.85 + try {
32.86 + ret = i.invokeFunction("clickAndCheck");
32.87 + } catch (ScriptException ex) {
32.88 + fail("Execution failed in " + sb, ex);
32.89 + } catch (NoSuchMethodException ex) {
32.90 + fail("Cannot find method in " + sb, ex);
32.91 + }
32.92 + assertEquals(ret, "100", "layerX has been passed to the method argument");
32.93 + }
32.94 +
32.95 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
32.96 if (sb == null) {
32.97 sb = new StringBuilder();
32.98 }
32.99 Bck2Brwsr.generate(sb, ProcessPageTest.class.getClassLoader(), names);
32.100 sb.append("var vm = this.bck2brwsr();\n");
32.101 + for (String c : names) {
32.102 + sb.append("vm.loadClass('").append(c.replace('/', '.')).append("');\n");
32.103 + }
32.104
32.105 ScriptEngineManager sem = new ScriptEngineManager();
32.106 ScriptEngine js = sem.getEngineByExtension("js");
33.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
33.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/Sex.java Fri May 17 10:27:41 2013 +0200
33.3 @@ -0,0 +1,26 @@
33.4 +/**
33.5 + * Back 2 Browser Bytecode Translator
33.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
33.7 + *
33.8 + * This program is free software: you can redistribute it and/or modify
33.9 + * it under the terms of the GNU General Public License as published by
33.10 + * the Free Software Foundation, version 2 of the License.
33.11 + *
33.12 + * This program is distributed in the hope that it will be useful,
33.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33.15 + * GNU General Public License for more details.
33.16 + *
33.17 + * You should have received a copy of the GNU General Public License
33.18 + * along with this program. Look for COPYING file in the top folder.
33.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
33.20 + */
33.21 +package org.apidesign.bck2brwsr.htmlpage;
33.22 +
33.23 +/**
33.24 + *
33.25 + * @author Jaroslav Tulach <jtulach@netbeans.org>
33.26 + */
33.27 +public enum Sex {
33.28 + MALE, FEMALE;
33.29 +}
34.1 --- a/javaquery/api/src/test/resources/org/apidesign/bck2brwsr/htmlpage/TestPage.html Tue Mar 05 07:57:16 2013 +0100
34.2 +++ b/javaquery/api/src/test/resources/org/apidesign/bck2brwsr/htmlpage/TestPage.html Fri May 17 10:27:41 2013 +0200
34.3 @@ -26,5 +26,6 @@
34.4 <body>
34.5 New title: <input id="pg.text"/>
34.6 <button id="pg.button">Change title!</button>
34.7 + <canvas id="pg.canvas" width="100"/>
34.8 </body>
34.9 </html>
35.1 --- a/javaquery/demo-calculator-dynamic/nbactions.xml Tue Mar 05 07:57:16 2013 +0100
35.2 +++ b/javaquery/demo-calculator-dynamic/nbactions.xml Fri May 17 10:27:41 2013 +0200
35.3 @@ -23,7 +23,7 @@
35.4 <actionName>run</actionName>
35.5 <goals>
35.6 <goal>process-classes</goal>
35.7 - <goal>org.apidesign.bck2brwsr:mojo:0.3-SNAPSHOT:brwsr</goal>
35.8 + <goal>bck2brwsr:brwsr</goal>
35.9 </goals>
35.10 </action>
35.11 </actions>
36.1 --- a/javaquery/demo-calculator-dynamic/pom.xml Tue Mar 05 07:57:16 2013 +0100
36.2 +++ b/javaquery/demo-calculator-dynamic/pom.xml Fri May 17 10:27:41 2013 +0200
36.3 @@ -4,7 +4,7 @@
36.4
36.5 <groupId>org.apidesign.bck2brwsr</groupId>
36.6 <artifactId>demo.calculator</artifactId>
36.7 - <version>0.4-SNAPSHOT</version>
36.8 + <version>0.8-SNAPSHOT</version>
36.9 <packaging>jar</packaging>
36.10
36.11 <name>JavaQuery Demo - Calculator</name>
36.12 @@ -17,8 +17,8 @@
36.13 <plugins>
36.14 <plugin>
36.15 <groupId>org.apidesign.bck2brwsr</groupId>
36.16 - <artifactId>mojo</artifactId>
36.17 - <version>0.4-SNAPSHOT</version>
36.18 + <artifactId>bck2brwsr-maven-plugin</artifactId>
36.19 + <version>${project.version}</version>
36.20 <executions>
36.21 <execution>
36.22 <goals>
36.23 @@ -93,13 +93,13 @@
36.24 <dependency>
36.25 <groupId>org.apidesign.bck2brwsr</groupId>
36.26 <artifactId>emul</artifactId>
36.27 - <version>0.4-SNAPSHOT</version>
36.28 + <version>${project.version}</version>
36.29 <classifier>rt</classifier>
36.30 </dependency>
36.31 <dependency>
36.32 <groupId>org.apidesign.bck2brwsr</groupId>
36.33 <artifactId>javaquery.api</artifactId>
36.34 - <version>0.4-SNAPSHOT</version>
36.35 + <version>${project.version}</version>
36.36 </dependency>
36.37 <dependency>
36.38 <groupId>org.testng</groupId>
36.39 @@ -112,7 +112,7 @@
36.40 <artifactId>vm4brwsr</artifactId>
36.41 <classifier>js</classifier>
36.42 <type>zip</type>
36.43 - <version>0.4-SNAPSHOT</version>
36.44 + <version>${project.version}</version>
36.45 <scope>provided</scope>
36.46 </dependency>
36.47 </dependencies>
37.1 --- a/javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/demo/calc/Calc.java Tue Mar 05 07:57:16 2013 +0100
37.2 +++ b/javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/demo/calc/Calc.java Fri May 17 10:27:41 2013 +0200
37.3 @@ -17,9 +17,11 @@
37.4 */
37.5 package org.apidesign.bck2brwsr.demo.calc;
37.6
37.7 +import java.util.List;
37.8 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
37.9 import org.apidesign.bck2brwsr.htmlpage.api.On;
37.10 import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
37.11 +import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
37.12 import org.apidesign.bck2brwsr.htmlpage.api.Page;
37.13 import org.apidesign.bck2brwsr.htmlpage.api.Property;
37.14
37.15 @@ -33,11 +35,12 @@
37.16 @Property(name = "memory", type = double.class),
37.17 @Property(name = "display", type = double.class),
37.18 @Property(name = "operation", type = String.class),
37.19 - @Property(name = "hover", type = boolean.class)
37.20 + @Property(name = "hover", type = boolean.class),
37.21 + @Property(name = "history", type = HistoryImpl.class, array = true)
37.22 })
37.23 public class Calc {
37.24 static {
37.25 - new Calculator().applyBindings();
37.26 + new Calculator().applyBindings().setOperation("plus");
37.27 }
37.28
37.29 @On(event = CLICK, id="clear")
37.30 @@ -48,31 +51,48 @@
37.31 }
37.32
37.33 @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
37.34 - static void applyOp(Calculator c, String op) {
37.35 + static void applyOp(Calculator c, String id) {
37.36 c.setMemory(c.getDisplay());
37.37 - c.setOperation(op);
37.38 + c.setOperation(id);
37.39 c.setDisplay(0);
37.40 }
37.41
37.42 @On(event = MOUSE_OVER, id= { "result" })
37.43 - static void attemptingIn(Calculator c, String op) {
37.44 + static void attemptingIn(Calculator c) {
37.45 c.setHover(true);
37.46 }
37.47 @On(event = MOUSE_OUT, id= { "result" })
37.48 - static void attemptingOut(Calculator c, String op) {
37.49 + static void attemptingOut(Calculator c) {
37.50 c.setHover(false);
37.51 }
37.52
37.53 @On(event = CLICK, id="result")
37.54 static void computeTheValue(Calculator c) {
37.55 - c.setDisplay(compute(
37.56 + final double newValue = compute(
37.57 c.getOperation(),
37.58 c.getMemory(),
37.59 c.getDisplay()
37.60 - ));
37.61 + );
37.62 + c.setDisplay(newValue);
37.63 + if (!containsValue(c.getHistory(), newValue)) {
37.64 + History h = new History();
37.65 + h.setValue(newValue);
37.66 + h.setOperation(c.getOperation());
37.67 + c.getHistory().add(h);
37.68 + }
37.69 c.setMemory(0);
37.70 }
37.71
37.72 + @OnFunction
37.73 + static void recoverMemory(Calculator c, History data) {
37.74 + c.setDisplay(data.getValue());
37.75 + }
37.76 +
37.77 + @OnFunction
37.78 + static void removeMemory(Calculator c, History data) {
37.79 + c.getHistory().remove(data);
37.80 + }
37.81 +
37.82 private static double compute(String op, double memory, double display) {
37.83 switch (op) {
37.84 case "plus": return memory + display;
37.85 @@ -84,18 +104,18 @@
37.86 }
37.87
37.88 @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"})
37.89 - static void addDigit(String digit, Calculator c) {
37.90 - digit = digit.substring(1);
37.91 + static void addDigit(String id, Calculator c) {
37.92 + id = id.substring(1);
37.93
37.94 double v = c.getDisplay();
37.95 if (v == 0.0) {
37.96 - c.setDisplay(Integer.parseInt(digit));
37.97 + c.setDisplay(Integer.parseInt(id));
37.98 } else {
37.99 String txt = Double.toString(v);
37.100 if (txt.endsWith(".0")) {
37.101 txt = txt.substring(0, txt.length() - 2);
37.102 }
37.103 - txt = txt + digit;
37.104 + txt = txt + id;
37.105 c.setDisplay(Double.parseDouble(txt));
37.106 }
37.107 }
37.108 @@ -109,4 +129,18 @@
37.109 }
37.110 return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
37.111 }
37.112 +
37.113 + @ComputedProperty
37.114 + static boolean emptyHistory(List<?> history) {
37.115 + return history.isEmpty();
37.116 + }
37.117 +
37.118 + private static boolean containsValue(List<History> arr, final double newValue) {
37.119 + for (History history : arr) {
37.120 + if (history.getValue() == newValue) {
37.121 + return true;
37.122 + }
37.123 + }
37.124 + return false;
37.125 + }
37.126 }
38.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
38.2 +++ b/javaquery/demo-calculator-dynamic/src/main/java/org/apidesign/bck2brwsr/demo/calc/HistoryImpl.java Fri May 17 10:27:41 2013 +0200
38.3 @@ -0,0 +1,43 @@
38.4 +/**
38.5 + * Back 2 Browser Bytecode Translator
38.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
38.7 + *
38.8 + * This program is free software: you can redistribute it and/or modify
38.9 + * it under the terms of the GNU General Public License as published by
38.10 + * the Free Software Foundation, version 2 of the License.
38.11 + *
38.12 + * This program is distributed in the hope that it will be useful,
38.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38.15 + * GNU General Public License for more details.
38.16 + *
38.17 + * You should have received a copy of the GNU General Public License
38.18 + * along with this program. Look for COPYING file in the top folder.
38.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
38.20 + */
38.21 +package org.apidesign.bck2brwsr.demo.calc;
38.22 +
38.23 +import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
38.24 +import org.apidesign.bck2brwsr.htmlpage.api.Model;
38.25 +import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
38.26 +import org.apidesign.bck2brwsr.htmlpage.api.Property;
38.27 +
38.28 +/**
38.29 + *
38.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
38.31 + */
38.32 +@Model(className = "History", properties = {
38.33 + @Property(name = "value", type = double.class),
38.34 + @Property(name = "operation", type = String.class)
38.35 +})
38.36 +public class HistoryImpl {
38.37 + @ComputedProperty
38.38 + static String resultOf(String operation) {
38.39 + return "result of " + operation;
38.40 + }
38.41 +
38.42 + @OnFunction
38.43 + static void twice(History data) {
38.44 + data.setValue(2.0 * data.getValue());
38.45 + }
38.46 +}
39.1 --- a/javaquery/demo-calculator-dynamic/src/main/resources/org/apidesign/bck2brwsr/demo/calc/Calculator.xhtml Tue Mar 05 07:57:16 2013 +0100
39.2 +++ b/javaquery/demo-calculator-dynamic/src/main/resources/org/apidesign/bck2brwsr/demo/calc/Calculator.xhtml Fri May 17 10:27:41 2013 +0200
39.3 @@ -78,82 +78,25 @@
39.4 </table>
39.5 <div data-bind="text: displayPreview"></div>
39.6
39.7 + <h4>Previous Results</h4>
39.8 +
39.9 + <div data-bind="if: emptyHistory">No results yet.</div>
39.10 + <ul data-bind="foreach: history">
39.11 + <li>
39.12 + <span data-bind="text: $data.value"></span> -
39.13 + <a href="#" data-bind="click: $root.recoverMemory">Use</a>
39.14 + <a href="#" data-bind="click: $root.removeMemory">Remove</a>
39.15 + <a href="#" data-bind="click: $data.twice">Double</a> -
39.16 + <span data-bind="text: $data.resultOf"></span>
39.17 + </li>
39.18 + </ul>
39.19 +
39.20 <script src="bck2brwsr.js"></script>
39.21 <script type="text/javascript">
39.22 - var vm = bck2brwsr('demo.calculator-0.3-SNAPSHOT.jar');
39.23 + var vm = bck2brwsr('demo.calculator-0.6.jar');
39.24 vm.loadClass('org.apidesign.bck2brwsr.demo.calc.Calc');
39.25 </script>
39.26
39.27 <hr/>
39.28 - <pre>
39.29 - <span class="keyword-directive">package</span> org.apidesign.bck2brwsr.mavenhtml;
39.30 -
39.31 - <span class="keyword-directive">import</span> org.apidesign.bck2brwsr.htmlpage.api.OnClick;
39.32 - <span class="keyword-directive">import</span> org.apidesign.bck2brwsr.htmlpage.api.Page;
39.33 -
39.34 - <span class="comment">/**</span> <span class="comment">HTML5</span><span class="comment"> & </span><span class="comment">Java</span> <span class="comment">demo</span> <span class="comment">showing</span> <span class="comment">the</span> <span class="comment">power</span> <span class="comment">of</span> <a href="http://wiki.apidesign.org/wiki/AnnotationProcessor">annotation processors</a>
39.35 - <span class="comment"> * </span><span class="comment">as</span> <span class="comment">well</span> <span class="comment">as</span> <span class="comment">other</span> <span class="comment">goodies</span><span class="comment">, including type-safe association between</span>
39.36 - <span class="comment"> * </span><span class="comment">an XHTML page and Java.</span>
39.37 - <span class="comment"> * </span>
39.38 - <span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">Jaroslav</span> <span class="comment">Tulach</span> <span class="ST0"><jaroslav.tulach@apidesign.org></span>
39.39 - <span class="comment">*/</span>
39.40 - @Page(xhtml=<span class="string">"</span><span class="string">Calculator.xhtml</span><span class="string">"</span>)
39.41 - <span class="keyword-directive">public</span> <span class="keyword-directive">class</span> App {
39.42 - <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">double</span> memory;
39.43 - <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> String operation;
39.44 -
39.45 - @OnClick(id=<span class="string">"</span><span class="string">clear</span><span class="string">"</span>)
39.46 - <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> clear() {
39.47 - memory = <span class="number">0</span>;
39.48 - operation = <span class="keyword-directive">null</span>;
39.49 - Calculator.DISPLAY.setValue(<span class="string">"</span><span class="string">0</span><span class="string">"</span>);
39.50 - }
39.51 -
39.52 - @OnClick(id= { <span class="string">"</span><span class="string">plus</span><span class="string">"</span>, <span class="string">"</span><span class="string">minus</span><span class="string">"</span>, <span class="string">"</span><span class="string">mul</span><span class="string">"</span>, <span class="string">"</span><span class="string">div</span><span class="string">"</span> })
39.53 - <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> applyOp(String op) {
39.54 - memory = getValue();
39.55 - operation = op;
39.56 - Calculator.DISPLAY.setValue(<span class="string">"</span><span class="string">0</span><span class="string">"</span>);
39.57 - }
39.58 -
39.59 - @OnClick(id=<span class="string">"</span><span class="string">result</span><span class="string">"</span>)
39.60 - <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> computeTheValue() {
39.61 - <span class="keyword-directive">switch</span> (operation) {
39.62 - <span class="keyword-directive">case</span> <span class="string">"</span><span class="string">plus</span><span class="string">"</span>: setValue(memory + getValue()); <span class="keyword-directive">break</span>;
39.63 - <span class="keyword-directive">case</span> <span class="string">"</span><span class="string">minus</span><span class="string">"</span>: setValue(memory - getValue()); <span class="keyword-directive">break</span>;
39.64 - <span class="keyword-directive">case</span> <span class="string">"</span><span class="string">mul</span><span class="string">"</span>: setValue(memory * getValue()); <span class="keyword-directive">break</span>;
39.65 - <span class="keyword-directive">case</span> <span class="string">"</span><span class="string">div</span><span class="string">"</span>: setValue(memory / getValue()); <span class="keyword-directive">break</span>;
39.66 - <span class="keyword-directive">default</span>: <span class="keyword-directive">throw</span> <span class="keyword-directive">new</span> IllegalStateException(operation);
39.67 - }
39.68 - }
39.69 -
39.70 - @OnClick(id={<span class="string">"</span><span class="string">n0</span><span class="string">"</span>, <span class="string">"</span><span class="string">n1</span><span class="string">"</span>, <span class="string">"</span><span class="string">n2</span><span class="string">"</span>, <span class="string">"</span><span class="string">n3</span><span class="string">"</span>, <span class="string">"</span><span class="string">n4</span><span class="string">"</span>, <span class="string">"</span><span class="string">n5</span><span class="string">"</span>, <span class="string">"</span><span class="string">n6</span><span class="string">"</span>, <span class="string">"</span><span class="string">n7</span><span class="string">"</span>, <span class="string">"</span><span class="string">n8</span><span class="string">"</span>, <span class="string">"</span><span class="string">n9</span><span class="string">"</span>})
39.71 - <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> addDigit(String digit) {
39.72 - digit = digit.substring(<span class="number">1</span>);
39.73 - String v = Calculator.DISPLAY.getValue();
39.74 - <span class="keyword-directive">if</span> (getValue() == <span class="number">0.0</span>) {
39.75 - Calculator.DISPLAY.setValue(digit);
39.76 - } <span class="keyword-directive">else</span> {
39.77 - Calculator.DISPLAY.setValue(v + digit);
39.78 - }
39.79 - }
39.80 -
39.81 - <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> setValue(<span class="keyword-directive">double</span> v) {
39.82 - StringBuilder sb = <span class="keyword-directive">new</span> StringBuilder();
39.83 - sb.append(v);
39.84 - Calculator.DISPLAY.setValue(sb.toString());
39.85 - }
39.86 -
39.87 - <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">double</span> getValue() {
39.88 - <span class="keyword-directive">try</span> {
39.89 - <span class="keyword-directive">return</span> Double.parseDouble(Calculator.DISPLAY.getValue());
39.90 - } <span class="keyword-directive">catch</span> (NumberFormatException ex) {
39.91 - Calculator.DISPLAY.setValue(<span class="string">"</span><span class="string">err</span><span class="string">"</span>);
39.92 - <span class="keyword-directive">return</span> <span class="number">0.0</span>;
39.93 - }
39.94 - }
39.95 - }
39.96 -
39.97 - </pre>
39.98 </body>
39.99 </html>
40.1 --- a/javaquery/demo-calculator/nbactions.xml Tue Mar 05 07:57:16 2013 +0100
40.2 +++ b/javaquery/demo-calculator/nbactions.xml Fri May 17 10:27:41 2013 +0200
40.3 @@ -23,7 +23,7 @@
40.4 <actionName>run</actionName>
40.5 <goals>
40.6 <goal>package</goal>
40.7 - <goal>org.apidesign.bck2brwsr:mojo:0.3-SNAPSHOT:brwsr</goal>
40.8 + <goal>bck2brwsr:brwsr</goal>
40.9 </goals>
40.10 <properties>
40.11 <skipTests>true</skipTests>
41.1 --- a/javaquery/demo-calculator/pom.xml Tue Mar 05 07:57:16 2013 +0100
41.2 +++ b/javaquery/demo-calculator/pom.xml Fri May 17 10:27:41 2013 +0200
41.3 @@ -4,7 +4,7 @@
41.4
41.5 <groupId>org.apidesign.bck2brwsr</groupId>
41.6 <artifactId>demo.static.calculator</artifactId>
41.7 - <version>0.4-SNAPSHOT</version>
41.8 + <version>0.8-SNAPSHOT</version>
41.9 <packaging>jar</packaging>
41.10
41.11 <name>JavaQuery Demo - Calculator - Static Compilation</name>
41.12 @@ -12,16 +12,18 @@
41.13
41.14 <properties>
41.15 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
41.16 + <bck2brwsr.obfuscationlevel>MINIMAL</bck2brwsr.obfuscationlevel>
41.17 </properties>
41.18 <build>
41.19 <plugins>
41.20 <plugin>
41.21 <groupId>org.apidesign.bck2brwsr</groupId>
41.22 - <artifactId>mojo</artifactId>
41.23 - <version>0.4-SNAPSHOT</version>
41.24 + <artifactId>bck2brwsr-maven-plugin</artifactId>
41.25 + <version>${project.version}</version>
41.26 <executions>
41.27 <execution>
41.28 <goals>
41.29 + <goal>j2js</goal>
41.30 <goal>brwsr</goal>
41.31 </goals>
41.32 </execution>
41.33 @@ -29,6 +31,8 @@
41.34 <configuration>
41.35 <directory>${project.build.directory}/${project.build.finalName}-bck2brwsr/public_html/</directory>
41.36 <startpage>index.xhtml</startpage>
41.37 + <javascript>${project.build.directory}/bck2brwsr.js</javascript>
41.38 + <obfuscation>${bck2brwsr.obfuscationlevel}</obfuscation>
41.39 </configuration>
41.40 </plugin>
41.41 <plugin>
41.42 @@ -94,21 +98,13 @@
41.43 <dependency>
41.44 <groupId>org.apidesign.bck2brwsr</groupId>
41.45 <artifactId>emul</artifactId>
41.46 - <version>0.4-SNAPSHOT</version>
41.47 + <version>${project.version}</version>
41.48 <classifier>rt</classifier>
41.49 </dependency>
41.50 <dependency>
41.51 <groupId>org.apidesign.bck2brwsr</groupId>
41.52 <artifactId>javaquery.api</artifactId>
41.53 - <version>0.4-SNAPSHOT</version>
41.54 - </dependency>
41.55 - <dependency>
41.56 - <groupId>org.apidesign.bck2brwsr</groupId>
41.57 - <artifactId>vm4brwsr</artifactId>
41.58 - <classifier>js</classifier>
41.59 - <type>zip</type>
41.60 - <version>0.4-SNAPSHOT</version>
41.61 - <scope>provided</scope>
41.62 + <version>${project.version}</version>
41.63 </dependency>
41.64 </dependencies>
41.65 </project>
42.1 --- a/javaquery/demo-calculator/src/main/assembly/bck2brwsr.xml Tue Mar 05 07:57:16 2013 +0100
42.2 +++ b/javaquery/demo-calculator/src/main/assembly/bck2brwsr.xml Fri May 17 10:27:41 2013 +0200
42.3 @@ -37,15 +37,6 @@
42.4 <include>*:rt</include>
42.5 </includes>
42.6 </dependencySet>
42.7 - <dependencySet>
42.8 - <useProjectArtifact>false</useProjectArtifact>
42.9 - <scope>provided</scope>
42.10 - <includes>
42.11 - <include>*:js</include>
42.12 - </includes>
42.13 - <unpack>true</unpack>
42.14 - <outputDirectory>/</outputDirectory>
42.15 - </dependencySet>
42.16 </dependencySets>
42.17 <files>
42.18 <file>
42.19 @@ -53,6 +44,10 @@
42.20 <outputDirectory>/</outputDirectory>
42.21 </file>
42.22 <file>
42.23 + <source>${project.build.directory}/bck2brwsr.js</source>
42.24 + <outputDirectory>/</outputDirectory>
42.25 + </file>
42.26 + <file>
42.27 <source>${project.build.directory}/classes/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml</source>
42.28 <outputDirectory>/</outputDirectory>
42.29 <destName>index.xhtml</destName>
43.1 --- a/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java Tue Mar 05 07:57:16 2013 +0100
43.2 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java Fri May 17 10:27:41 2013 +0200
43.3 @@ -17,9 +17,11 @@
43.4 */
43.5 package org.apidesign.bck2brwsr.demo.calc.staticcompilation;
43.6
43.7 +import java.util.List;
43.8 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
43.9 import org.apidesign.bck2brwsr.htmlpage.api.On;
43.10 import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
43.11 +import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
43.12 import org.apidesign.bck2brwsr.htmlpage.api.Page;
43.13 import org.apidesign.bck2brwsr.htmlpage.api.Property;
43.14
43.15 @@ -33,11 +35,12 @@
43.16 @Property(name = "memory", type = double.class),
43.17 @Property(name = "display", type = double.class),
43.18 @Property(name = "operation", type = String.class),
43.19 - @Property(name = "hover", type = boolean.class)
43.20 + @Property(name = "hover", type = boolean.class),
43.21 + @Property(name = "history", type = double.class, array = true)
43.22 })
43.23 public class Calc {
43.24 static {
43.25 - new Calculator().applyBindings();
43.26 + new Calculator().applyBindings().setOperation("plus");
43.27 }
43.28
43.29 @On(event = CLICK, id="clear")
43.30 @@ -48,31 +51,45 @@
43.31 }
43.32
43.33 @On(event = CLICK, id= { "plus", "minus", "mul", "div" })
43.34 - static void applyOp(Calculator c, String op) {
43.35 + static void applyOp(Calculator c, String id) {
43.36 c.setMemory(c.getDisplay());
43.37 - c.setOperation(op);
43.38 + c.setOperation(id);
43.39 c.setDisplay(0);
43.40 }
43.41
43.42 @On(event = MOUSE_OVER, id= { "result" })
43.43 - static void attemptingIn(Calculator c, String op) {
43.44 + static void attemptingIn(Calculator c) {
43.45 c.setHover(true);
43.46 }
43.47 @On(event = MOUSE_OUT, id= { "result" })
43.48 - static void attemptingOut(Calculator c, String op) {
43.49 + static void attemptingOut(Calculator c) {
43.50 c.setHover(false);
43.51 }
43.52
43.53 @On(event = CLICK, id="result")
43.54 static void computeTheValue(Calculator c) {
43.55 - c.setDisplay(compute(
43.56 + final double newValue = compute(
43.57 c.getOperation(),
43.58 c.getMemory(),
43.59 c.getDisplay()
43.60 - ));
43.61 + );
43.62 + c.setDisplay(newValue);
43.63 + if (!c.getHistory().contains(newValue)) {
43.64 + c.getHistory().add(newValue);
43.65 + }
43.66 c.setMemory(0);
43.67 }
43.68
43.69 + @OnFunction
43.70 + static void recoverMemory(Calculator c, double data) {
43.71 + c.setDisplay(data);
43.72 + }
43.73 +
43.74 + @OnFunction
43.75 + static void removeMemory(Calculator c, double data) {
43.76 + c.getHistory().remove(data);
43.77 + }
43.78 +
43.79 private static double compute(String op, double memory, double display) {
43.80 switch (op) {
43.81 case "plus": return memory + display;
43.82 @@ -84,18 +101,18 @@
43.83 }
43.84
43.85 @On(event = CLICK, id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"})
43.86 - static void addDigit(String digit, Calculator c) {
43.87 - digit = digit.substring(1);
43.88 + static void addDigit(String id, Calculator c) {
43.89 + id = id.substring(1);
43.90
43.91 double v = c.getDisplay();
43.92 if (v == 0.0) {
43.93 - c.setDisplay(Integer.parseInt(digit));
43.94 + c.setDisplay(Integer.parseInt(id));
43.95 } else {
43.96 String txt = Double.toString(v);
43.97 if (txt.endsWith(".0")) {
43.98 txt = txt.substring(0, txt.length() - 2);
43.99 }
43.100 - txt = txt + digit;
43.101 + txt = txt + id;
43.102 c.setDisplay(Double.parseDouble(txt));
43.103 }
43.104 }
43.105 @@ -109,4 +126,9 @@
43.106 }
43.107 return "Attempt to compute " + memory + " " + operation + " " + display + " = " + compute(operation, memory, display);
43.108 }
43.109 +
43.110 + @ComputedProperty
43.111 + static boolean emptyHistory(List<?> history) {
43.112 + return history.isEmpty();
43.113 + }
43.114 }
44.1 --- a/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml Tue Mar 05 07:57:16 2013 +0100
44.2 +++ b/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml Fri May 17 10:27:41 2013 +0200
44.3 @@ -76,10 +76,22 @@
44.4 </tr>
44.5 </tbody>
44.6 </table>
44.7 +
44.8 + <h4>Previous Results</h4>
44.9 +
44.10 + <div data-bind="if: emptyHistory">No results yet.</div>
44.11 + <ul data-bind="foreach: history">
44.12 + <li>
44.13 + <span data-bind="text: $data"></span> -
44.14 + <a href="#" data-bind="click: $root.recoverMemory">Use</a>
44.15 + <a href="#" data-bind="click: $root.removeMemory">Remove</a>
44.16 + </li>
44.17 + </ul>
44.18 +
44.19 <div data-bind="text: displayPreview"></div>
44.20 - <script src="bck2brwsr.js"/>
44.21 + <script src="bck2brwsr.js"></script>
44.22 <script>
44.23 - var vm = bck2brwsr('demo.static.calculator-0.3-SNAPSHOT.jar');
44.24 + var vm = bck2brwsr('demo.static.calculator-0.6.jar');
44.25 vm.loadClass('org.apidesign.bck2brwsr.demo.calc.staticcompilation.Calc');
44.26 </script>
44.27 </body>
45.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
45.2 +++ b/javaquery/demo-twitter/bck2brwsr-assembly.xml Fri May 17 10:27:41 2013 +0200
45.3 @@ -0,0 +1,62 @@
45.4 +<?xml version="1.0"?>
45.5 +<!--
45.6 +
45.7 + Back 2 Browser Bytecode Translator
45.8 + Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
45.9 +
45.10 + This program is free software: you can redistribute it and/or modify
45.11 + it under the terms of the GNU General Public License as published by
45.12 + the Free Software Foundation, version 2 of the License.
45.13 +
45.14 + This program is distributed in the hope that it will be useful,
45.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
45.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45.17 + GNU General Public License for more details.
45.18 +
45.19 + You should have received a copy of the GNU General Public License
45.20 + along with this program. Look for COPYING file in the top folder.
45.21 + If not, see http://opensource.org/licenses/GPL-2.0.
45.22 +
45.23 +-->
45.24 +<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
45.25 + xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
45.26 +
45.27 + <id>bck2brwsr</id>
45.28 + <formats>
45.29 + <format>zip</format>
45.30 + </formats>
45.31 + <baseDirectory>public_html</baseDirectory>
45.32 + <dependencySets>
45.33 + <dependencySet>
45.34 + <useProjectArtifact>false</useProjectArtifact>
45.35 + <scope>runtime</scope>
45.36 + <outputDirectory>lib</outputDirectory>
45.37 + <includes>
45.38 + <include>*:jar</include>
45.39 + <include>*:rt</include>
45.40 + </includes>
45.41 + </dependencySet>
45.42 + </dependencySets>
45.43 + <fileSets>
45.44 + <fileSet>
45.45 + <directory>${project.build.directory}/classes/org/apidesign/bck2brwsr/demo/twitter/</directory>
45.46 + <includes>
45.47 + <include>**/*</include>
45.48 + </includes>
45.49 + <excludes>
45.50 + <exclude>**/*.class</exclude>
45.51 + </excludes>
45.52 + <outputDirectory>/</outputDirectory>
45.53 + </fileSet>
45.54 + </fileSets>
45.55 + <files>
45.56 + <file>
45.57 + <source>${project.build.directory}/${project.build.finalName}.jar</source>
45.58 + <outputDirectory>/</outputDirectory>
45.59 + </file>
45.60 + <file>
45.61 + <source>${project.build.directory}/bck2brwsr.js</source>
45.62 + <outputDirectory>/</outputDirectory>
45.63 + </file>
45.64 + </files>
45.65 +</assembly>
45.66 \ No newline at end of file
46.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
46.2 +++ b/javaquery/demo-twitter/nb-configuration.xml Fri May 17 10:27:41 2013 +0200
46.3 @@ -0,0 +1,37 @@
46.4 +<?xml version="1.0" encoding="UTF-8"?>
46.5 +<!--
46.6 +
46.7 + Back 2 Browser Bytecode Translator
46.8 + Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
46.9 +
46.10 + This program is free software: you can redistribute it and/or modify
46.11 + it under the terms of the GNU General Public License as published by
46.12 + the Free Software Foundation, version 2 of the License.
46.13 +
46.14 + This program is distributed in the hope that it will be useful,
46.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
46.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46.17 + GNU General Public License for more details.
46.18 +
46.19 + You should have received a copy of the GNU General Public License
46.20 + along with this program. Look for COPYING file in the top folder.
46.21 + If not, see http://opensource.org/licenses/GPL-2.0.
46.22 +
46.23 +-->
46.24 +<project-shared-configuration>
46.25 + <!--
46.26 +This file contains additional configuration written by modules in the NetBeans IDE.
46.27 +The configuration is intended to be shared among all the users of project and
46.28 +therefore it is assumed to be part of version control checkout.
46.29 +Without this configuration present, some functionality in the IDE may be limited or fail altogether.
46.30 +-->
46.31 + <properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
46.32 + <!--
46.33 +Properties that influence various parts of the IDE, especially code formatting and the like.
46.34 +You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
46.35 +That way multiple projects can share the same settings (useful for formatting rules for example).
46.36 +Any value defined here will override the pom.xml file value but is only applicable to the current project.
46.37 +-->
46.38 + <netbeans.compile.on.save>none</netbeans.compile.on.save>
46.39 + </properties>
46.40 +</project-shared-configuration>
47.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
47.2 +++ b/javaquery/demo-twitter/nbactions.xml Fri May 17 10:27:41 2013 +0200
47.3 @@ -0,0 +1,29 @@
47.4 +<?xml version="1.0" encoding="UTF-8"?>
47.5 +<!--
47.6 +
47.7 + Back 2 Browser Bytecode Translator
47.8 + Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
47.9 +
47.10 + This program is free software: you can redistribute it and/or modify
47.11 + it under the terms of the GNU General Public License as published by
47.12 + the Free Software Foundation, version 2 of the License.
47.13 +
47.14 + This program is distributed in the hope that it will be useful,
47.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
47.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47.17 + GNU General Public License for more details.
47.18 +
47.19 + You should have received a copy of the GNU General Public License
47.20 + along with this program. Look for COPYING file in the top folder.
47.21 + If not, see http://opensource.org/licenses/GPL-2.0.
47.22 +
47.23 +-->
47.24 +<actions>
47.25 + <action>
47.26 + <actionName>run</actionName>
47.27 + <goals>
47.28 + <goal>process-classes</goal>
47.29 + <goal>bck2brwsr:brwsr</goal>
47.30 + </goals>
47.31 + </action>
47.32 +</actions>
48.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
48.2 +++ b/javaquery/demo-twitter/pom.xml Fri May 17 10:27:41 2013 +0200
48.3 @@ -0,0 +1,151 @@
48.4 +<?xml version="1.0" encoding="UTF-8"?>
48.5 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
48.6 + <modelVersion>4.0.0</modelVersion>
48.7 + <parent>
48.8 + <artifactId>javaquery</artifactId>
48.9 + <groupId>org.apidesign.bck2brwsr</groupId>
48.10 + <version>0.8-SNAPSHOT</version>
48.11 + </parent>
48.12 +
48.13 + <groupId>org.apidesign.bck2brwsr</groupId>
48.14 + <artifactId>demo-twitter</artifactId>
48.15 + <version>0.8-SNAPSHOT</version>
48.16 + <packaging>jar</packaging>
48.17 +
48.18 + <name>Bck2Brwsr's Twttr</name>
48.19 + <description>
48.20 + Rewrite of knockoutjs example to use model written in Java and
48.21 + execute using Bck2Brwsr virtual machine.
48.22 + </description>
48.23 +
48.24 + <repositories>
48.25 + <repository>
48.26 + <id>java.net</id>
48.27 + <name>Java.net</name>
48.28 + <url>https://maven.java.net/content/repositories/releases/</url>
48.29 + <snapshots>
48.30 + </snapshots>
48.31 + </repository>
48.32 + <repository>
48.33 + <id>netbeans</id>
48.34 + <name>NetBeans</name>
48.35 + <url>http://bits.netbeans.org/maven2/</url>
48.36 + </repository>
48.37 + </repositories>
48.38 + <pluginRepositories>
48.39 + <pluginRepository>
48.40 + <id>java.net</id>
48.41 + <name>Java.net</name>
48.42 + <url>https://maven.java.net/content/repositories/releases/</url>
48.43 + <snapshots>
48.44 + </snapshots>
48.45 + </pluginRepository>
48.46 + </pluginRepositories>
48.47 +
48.48 + <properties>
48.49 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
48.50 + <bck2brwsr.obfuscationlevel>MINIMAL</bck2brwsr.obfuscationlevel>
48.51 + </properties>
48.52 + <build>
48.53 + <plugins>
48.54 + <plugin>
48.55 + <groupId>org.apidesign.bck2brwsr</groupId>
48.56 + <artifactId>bck2brwsr-maven-plugin</artifactId>
48.57 + <version>${project.version}</version>
48.58 + <executions>
48.59 + <execution>
48.60 + <goals>
48.61 + <goal>brwsr</goal>
48.62 + <goal>j2js</goal>
48.63 + </goals>
48.64 + </execution>
48.65 + </executions>
48.66 + <configuration>
48.67 + <startpage>org/apidesign/bck2brwsr/demo/twitter/index.html</startpage>
48.68 + <javascript>${project.build.directory}/bck2brwsr.js</javascript>
48.69 + <obfuscation>${bck2brwsr.obfuscationlevel}</obfuscation>
48.70 + </configuration>
48.71 + </plugin>
48.72 + <plugin>
48.73 + <groupId>org.apache.maven.plugins</groupId>
48.74 + <artifactId>maven-compiler-plugin</artifactId>
48.75 + <version>2.3.2</version>
48.76 + <configuration>
48.77 + <source>1.7</source>
48.78 + <target>1.7</target>
48.79 + </configuration>
48.80 + </plugin>
48.81 + <plugin>
48.82 + <groupId>org.apache.maven.plugins</groupId>
48.83 + <artifactId>maven-jar-plugin</artifactId>
48.84 + <version>2.4</version>
48.85 + <configuration>
48.86 + <archive>
48.87 + <manifest>
48.88 + <addClasspath>true</addClasspath>
48.89 + <classpathPrefix>lib/</classpathPrefix>
48.90 + </manifest>
48.91 + </archive>
48.92 + </configuration>
48.93 + </plugin>
48.94 + <plugin>
48.95 + <groupId>org.apache.maven.plugins</groupId>
48.96 + <artifactId>maven-deploy-plugin</artifactId>
48.97 + <version>2.7</version>
48.98 + <configuration>
48.99 + <skip>true</skip>
48.100 + </configuration>
48.101 + </plugin>
48.102 + <plugin>
48.103 + <artifactId>maven-assembly-plugin</artifactId>
48.104 + <version>2.4</version>
48.105 + <executions>
48.106 + <execution>
48.107 + <id>distro-assembly</id>
48.108 + <phase>package</phase>
48.109 + <goals>
48.110 + <goal>single</goal>
48.111 + </goals>
48.112 + <configuration>
48.113 + <descriptors>
48.114 + <descriptor>bck2brwsr-assembly.xml</descriptor>
48.115 + </descriptors>
48.116 + </configuration>
48.117 + </execution>
48.118 + </executions>
48.119 + </plugin>
48.120 + </plugins>
48.121 + </build>
48.122 +
48.123 + <dependencies>
48.124 + <dependency>
48.125 + <groupId>org.apidesign.bck2brwsr</groupId>
48.126 + <artifactId>emul</artifactId>
48.127 + <version>${project.version}</version>
48.128 + <classifier>rt</classifier>
48.129 + </dependency>
48.130 + <dependency>
48.131 + <groupId>org.apidesign.bck2brwsr</groupId>
48.132 + <artifactId>javaquery.api</artifactId>
48.133 + <version>${project.version}</version>
48.134 + </dependency>
48.135 + <dependency>
48.136 + <groupId>org.testng</groupId>
48.137 + <artifactId>testng</artifactId>
48.138 + <version>6.5.2</version>
48.139 + <scope>test</scope>
48.140 + </dependency>
48.141 + <dependency>
48.142 + <groupId>org.apidesign.bck2brwsr</groupId>
48.143 + <artifactId>vmtest</artifactId>
48.144 + <version>${project.version}</version>
48.145 + <scope>test</scope>
48.146 + </dependency>
48.147 + <dependency>
48.148 + <groupId>org.apidesign.bck2brwsr</groupId>
48.149 + <artifactId>launcher.http</artifactId>
48.150 + <version>${project.version}</version>
48.151 + <scope>runtime</scope>
48.152 + </dependency>
48.153 + </dependencies>
48.154 +</project>
49.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
49.2 +++ b/javaquery/demo-twitter/src/main/java/org/apidesign/bck2brwsr/demo/twitter/TwitterClient.java Fri May 17 10:27:41 2013 +0200
49.3 @@ -0,0 +1,194 @@
49.4 +/**
49.5 + * Back 2 Browser Bytecode Translator
49.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
49.7 + *
49.8 + * This program is free software: you can redistribute it and/or modify
49.9 + * it under the terms of the GNU General Public License as published by
49.10 + * the Free Software Foundation, version 2 of the License.
49.11 + *
49.12 + * This program is distributed in the hope that it will be useful,
49.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
49.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49.15 + * GNU General Public License for more details.
49.16 + *
49.17 + * You should have received a copy of the GNU General Public License
49.18 + * along with this program. Look for COPYING file in the top folder.
49.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
49.20 + */
49.21 +package org.apidesign.bck2brwsr.demo.twitter;
49.22 +
49.23 +import java.util.Arrays;
49.24 +import java.util.List;
49.25 +import org.apidesign.bck2brwsr.htmlpage.api.*;
49.26 +import org.apidesign.bck2brwsr.htmlpage.api.Page;
49.27 +import org.apidesign.bck2brwsr.htmlpage.api.Property;
49.28 +import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
49.29 +
49.30 +/** Controller class for access to Twitter.
49.31 + *
49.32 + * @author Jaroslav Tulach
49.33 + */
49.34 +@Page(xhtml="index.html", className="TwitterModel", properties={
49.35 + @Property(name="savedLists", type=Tweeters.class, array = true),
49.36 + @Property(name="activeTweetersName", type=String.class),
49.37 + @Property(name="activeTweeters", type=String.class, array = true),
49.38 + @Property(name="userNameToAdd", type=String.class),
49.39 + @Property(name="currentTweets", type=Tweet.class, array = true)
49.40 +})
49.41 +public class TwitterClient {
49.42 + @Model(className = "Tweeters", properties = {
49.43 + @Property(name="name", type = String.class),
49.44 + @Property(name="userNames", type = String.class, array = true)
49.45 + })
49.46 + static class Twttrs {
49.47 + }
49.48 + @Model(className = "Tweet", properties = {
49.49 + @Property(name = "from_user", type = String.class),
49.50 + @Property(name = "from_user_id", type = int.class),
49.51 + @Property(name = "profile_image_url", type = String.class),
49.52 + @Property(name = "text", type = String.class),
49.53 + @Property(name = "created_at", type = String.class),
49.54 + })
49.55 + static final class Twt {
49.56 + @ComputedProperty static String html(String text) {
49.57 + StringBuilder sb = new StringBuilder(320);
49.58 + for (int pos = 0;;) {
49.59 + int http = text.indexOf("http", pos);
49.60 + if (http == -1) {
49.61 + sb.append(text.substring(pos));
49.62 + return sb.toString();
49.63 + }
49.64 + int spc = text.indexOf(' ', http);
49.65 + if (spc == -1) {
49.66 + spc = text.length();
49.67 + }
49.68 + sb.append(text.substring(pos, http));
49.69 + String url = text.substring(http, spc);
49.70 + sb.append("<a href='").append(url).append("'>").append(url).append("</a>");
49.71 + pos = spc;
49.72 + }
49.73 + }
49.74 +
49.75 + @ComputedProperty static String userUrl(String from_user) {
49.76 + return "http://twitter.com/" + from_user;
49.77 + }
49.78 + }
49.79 + @Model(className = "TwitterQuery", properties = {
49.80 + @Property(array = true, name = "results", type = Twt.class)
49.81 + })
49.82 + public static final class TwttrQr {
49.83 + }
49.84 +
49.85 + @OnReceive(url="{root}/search.json?{query}&callback={me}", jsonp="me")
49.86 + static void queryTweets(TwitterModel page, TwitterQuery q) {
49.87 + page.getCurrentTweets().clear();
49.88 + page.getCurrentTweets().addAll(q.getResults());
49.89 + }
49.90 +
49.91 + @OnPropertyChange("activeTweetersName")
49.92 + static void changeTweetersList(TwitterModel model) {
49.93 + Tweeters people = findByName(model.getSavedLists(), model.getActiveTweetersName());
49.94 + model.getActiveTweeters().clear();
49.95 + model.getActiveTweeters().addAll(people.getUserNames());
49.96 + }
49.97 +
49.98 + @OnPropertyChange({ "activeTweeters", "activeTweetersCount" })
49.99 + static void refreshTweets(TwitterModel model) {
49.100 + StringBuilder sb = new StringBuilder();
49.101 + sb.append("rpp=25&q=");
49.102 + String sep = "";
49.103 + for (String p : model.getActiveTweeters()) {
49.104 + sb.append(sep);
49.105 + sb.append("from:");
49.106 + sb.append(p);
49.107 + sep = " OR ";
49.108 + }
49.109 + model.queryTweets("http://search.twitter.com", sb.toString());
49.110 + }
49.111 +
49.112 + static {
49.113 + final TwitterModel model = new TwitterModel();
49.114 + final List<Tweeters> svdLst = model.getSavedLists();
49.115 + svdLst.add(newTweeters("API Design", "JaroslavTulach"));
49.116 + svdLst.add(newTweeters("Celebrities", "JohnCleese", "MCHammer", "StephenFry", "algore", "StevenSanderson"));
49.117 + svdLst.add(newTweeters("Microsoft people", "BillGates", "shanselman", "ScottGu"));
49.118 + svdLst.add(newTweeters("NetBeans", "GeertjanW","monacotoni", "NetBeans", "petrjiricka"));
49.119 + svdLst.add(newTweeters("Tech pundits", "Scobleizer", "LeoLaporte", "techcrunch", "BoingBoing", "timoreilly", "codinghorror"));
49.120 +
49.121 + model.setActiveTweetersName("NetBeans");
49.122 +
49.123 + model.applyBindings();
49.124 + }
49.125 +
49.126 + @ComputedProperty
49.127 + static boolean hasUnsavedChanges(List<String> activeTweeters, List<Tweeters> savedLists, String activeTweetersName) {
49.128 + Tweeters tw = findByName(savedLists, activeTweetersName);
49.129 + if (activeTweeters == null) {
49.130 + return false;
49.131 + }
49.132 + return !tw.getUserNames().equals(activeTweeters);
49.133 + }
49.134 +
49.135 + @ComputedProperty
49.136 + static int activeTweetersCount(List<String> activeTweeters) {
49.137 + return activeTweeters.size();
49.138 + }
49.139 +
49.140 + @ComputedProperty
49.141 + static boolean userNameToAddIsValid(
49.142 + String userNameToAdd, String activeTweetersName, List<Tweeters> savedLists, List<String> activeTweeters
49.143 + ) {
49.144 + return userNameToAdd != null &&
49.145 + userNameToAdd.matches("[a-zA-Z0-9_]{1,15}") &&
49.146 + !activeTweeters.contains(userNameToAdd);
49.147 + }
49.148 +
49.149 + @OnFunction
49.150 + static void deleteList(TwitterModel model) {
49.151 + final List<Tweeters> sl = model.getSavedLists();
49.152 + sl.remove(findByName(sl, model.getActiveTweetersName()));
49.153 + if (sl.isEmpty()) {
49.154 + final Tweeters t = new Tweeters();
49.155 + t.setName("New");
49.156 + sl.add(t);
49.157 + }
49.158 + model.setActiveTweetersName(sl.get(0).getName());
49.159 + }
49.160 +
49.161 + @OnFunction
49.162 + static void saveChanges(TwitterModel model) {
49.163 + Tweeters t = findByName(model.getSavedLists(), model.getActiveTweetersName());
49.164 + int indx = model.getSavedLists().indexOf(t);
49.165 + if (indx != -1) {
49.166 + t.setName(model.getActiveTweetersName());
49.167 + t.getUserNames().clear();
49.168 + t.getUserNames().addAll(model.getActiveTweeters());
49.169 + }
49.170 + }
49.171 +
49.172 + @OnFunction
49.173 + static void addUser(TwitterModel model) {
49.174 + String n = model.getUserNameToAdd();
49.175 + model.getActiveTweeters().add(n);
49.176 + }
49.177 + @OnFunction
49.178 + static void removeUser(String data, TwitterModel model) {
49.179 + model.getActiveTweeters().remove(data);
49.180 + }
49.181 +
49.182 + private static Tweeters findByName(List<Tweeters> list, String name) {
49.183 + for (Tweeters l : list) {
49.184 + if (l.getName() != null && l.getName().equals(name)) {
49.185 + return l;
49.186 + }
49.187 + }
49.188 + return list.isEmpty() ? new Tweeters() : list.get(0);
49.189 + }
49.190 +
49.191 + private static Tweeters newTweeters(String listName, String... userNames) {
49.192 + Tweeters t = new Tweeters();
49.193 + t.setName(listName);
49.194 + t.getUserNames().addAll(Arrays.asList(userNames));
49.195 + return t;
49.196 + }
49.197 +}
50.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
50.2 +++ b/javaquery/demo-twitter/src/main/resources/org/apidesign/bck2brwsr/demo/twitter/index.html Fri May 17 10:27:41 2013 +0200
50.3 @@ -0,0 +1,99 @@
50.4 +<?xml version="1.0" encoding="UTF-8"?>
50.5 +<!--
50.6 +
50.7 + Back 2 Browser Bytecode Translator
50.8 + Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
50.9 +
50.10 + This program is free software: you can redistribute it and/or modify
50.11 + it under the terms of the GNU General Public License as published by
50.12 + the Free Software Foundation, version 2 of the License.
50.13 +
50.14 + This program is distributed in the hope that it will be useful,
50.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
50.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50.17 + GNU General Public License for more details.
50.18 +
50.19 + You should have received a copy of the GNU General Public License
50.20 + along with this program. Look for COPYING file in the top folder.
50.21 + If not, see http://opensource.org/licenses/GPL-2.0.
50.22 +
50.23 +-->
50.24 +
50.25 +<!--
50.26 + Copied from knockout.js Twitter example:
50.27 + http://knockoutjs.com/examples/twitter.html
50.28 +-->
50.29 +
50.30 +<!DOCTYPE html>
50.31 +<html xmlns="http://www.w3.org/1999/xhtml">
50.32 + <head>
50.33 + <title>Bck2Brwsr's Twitter</title>
50.34 + </head>
50.35 + <body>
50.36 + <link href='twitterExample.css' rel='Stylesheet' ></link>
50.37 +
50.38 + <style type='text/css'>
50.39 + .liveExample select { height: 1.7em; }
50.40 + .liveExample button { height: 2em; }
50.41 + </style>
50.42 +
50.43 +
50.44 + <h2>Bck2Brwsr's Twitter</h2>
50.45 +
50.46 + <p>
50.47 + This code based on original <a href="http://knockoutjs.com/examples/twitter.html">knockout.js Twitter example</a> and
50.48 + uses almost unmodified HTML code. It just changes the model. It
50.49 + is written in Java language and it is executed using <a href="http://bck2brwsr.apidesign.org">Bck2Brwsr</a>
50.50 + virtual machine. The Java source code has about 190 lines and is available
50.51 + <a href="http://source.apidesign.org/hg/bck2brwsr/file/7fc6b7e9c982/javaquery/demo-twitter/src/main/java/org/apidesign/bck2brwsr/demo/twitter/TwitterClient.java">here</a>
50.52 + - in fact it may even be more dense than the original JavaScript model.
50.53 + </p>
50.54 +
50.55 + <div class='liveExample'>
50.56 + <div class='configuration'>
50.57 + <div class='listChooser'>
50.58 + <button data-bind='click: deleteList, enable: activeTweetersName'>Delete</button>
50.59 + <button data-bind='click: saveChanges, enable: hasUnsavedChanges'>Save</button>
50.60 + <select data-bind='options: savedLists, optionsValue: "name", value: activeTweetersName'> </select>
50.61 + </div>
50.62 +
50.63 + <p>Currently viewing <span data-bind='text: activeTweetersCount'> </span> user(s):</p>
50.64 + <div class='currentUsers' >
50.65 + <ul data-bind='foreach: activeTweeters'>
50.66 + <li>
50.67 + <button data-bind='click: $root.removeUser'>Remove</button>
50.68 + <div data-bind='text: $data'> </div>
50.69 + </li>
50.70 + </ul>
50.71 + </div>
50.72 +
50.73 + <form data-bind='submit: addUser'>
50.74 + <label>Add user:</label>
50.75 + <input data-bind='value: userNameToAdd, valueUpdate: "keyup", css: { invalid: !userNameToAddIsValid() }' />
50.76 + <button data-bind='enable: userNameToAddIsValid' type='submit'>Add</button>
50.77 + </form>
50.78 + </div>
50.79 + <div class='tweets'>
50.80 + <div class='loadingIndicator'>Loading...</div>
50.81 + <table data-bind='foreach: currentTweets' width='100%'>
50.82 + <tr>
50.83 + <td><img data-bind='attr: { src: profile_image_url }' /></td>
50.84 + <td>
50.85 + <a class='twitterUser' data-bind='attr: { href: userUrl }, text: from_user'> </a>
50.86 + <span data-bind='html: html'> </span>
50.87 + <div class='tweetInfo' data-bind='text: created_at'> </div>
50.88 + </td>
50.89 + </tr>
50.90 + </table>
50.91 + </div>
50.92 + </div>
50.93 +
50.94 + <script src="bck2brwsr.js"></script>
50.95 + <script type="text/javascript">
50.96 + var vm = bck2brwsr('demo-twitter-0.8-SNAPSHOT.jar');
50.97 + vm.loadClass('org.apidesign.bck2brwsr.demo.twitter.TwitterClient');
50.98 + </script>
50.99 +
50.100 +
50.101 + </body>
50.102 +</html>
51.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
51.2 +++ b/javaquery/demo-twitter/src/main/resources/org/apidesign/bck2brwsr/demo/twitter/twitterExample.css Fri May 17 10:27:41 2013 +0200
51.3 @@ -0,0 +1,50 @@
51.4 +/**
51.5 + * Back 2 Browser Bytecode Translator
51.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
51.7 + *
51.8 + * This program is free software: you can redistribute it and/or modify
51.9 + * it under the terms of the GNU General Public License as published by
51.10 + * the Free Software Foundation, version 2 of the License.
51.11 + *
51.12 + * This program is distributed in the hope that it will be useful,
51.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
51.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51.15 + * GNU General Public License for more details.
51.16 + *
51.17 + * You should have received a copy of the GNU General Public License
51.18 + * along with this program. Look for COPYING file in the top folder.
51.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
51.20 + */
51.21 +
51.22 +/*
51.23 + Copied from knockout.js Twitter example:
51.24 + http://knockoutjs.com/examples/twitter.html
51.25 +*/
51.26 +
51.27 +.configuration, .tweets, .tweets td { font-family: Verdana; font-size: 13px; }
51.28 +.configuration { background-color: #DEDEDE; border: 2px solid gray; float:left; height: 40em; width: 40%; padding: 0.5em; border-right-width:0; }
51.29 +.tweets { width: 55%; border: 2px solid gray; height: 40em; overflow: scroll; overflow-x: hidden; background-color: Black; color: White; padding: 0.5em; position: relative; }
51.30 +.tweets table { border-width: 0;}
51.31 +.tweets tr { vertical-align: top; }
51.32 +.tweets td { padding: 0.4em 0.3em 1em 0.4em; border-width: 0; }
51.33 +.tweets img { width: 4em; }
51.34 +.tweetInfo { color: Gray; font-size: 0.9em; }
51.35 +.twitterUser { color: #77AAFF; text-decoration: none; font-size: 1.1em; font-weight: bold; }
51.36 +input.invalid { border: 1px solid red !important; background-color: #FFAAAA !important; }
51.37 +
51.38 +.listChooser select, .listChooser button { vertical-align:top; }
51.39 +.listChooser select { width: 60%; font-size:1.2em; height:1.4em; }
51.40 +.listChooser button { width: 19%; height:1.68em; float:right; }
51.41 +
51.42 +.currentUsers { height: 28em; overflow-y: auto; overflow-x: hidden; }
51.43 +.currentUsers button { float: right; height: 2.5em; margin: 0.1em; padding-left: 1em; padding-right: 1em; }
51.44 +.currentUsers ul, .configuration li { list-style: none; margin: 0; padding: 0 }
51.45 +.currentUsers li { height: 2.4em; font-size: 1.2em; background-color: #A7D0E3; border: 1px solid gray; margin-bottom: 0.3em; -webkit-border-radius: 5px; -moz-border-radius: 5px; -webkit-box-shadow: 0 0.2em 0.5em gray; -moz-box-shadow: 0 0.2em 0.5em gray; }
51.46 +.currentUsers li div { padding: 0.6em; }
51.47 +.currentUsers li:hover { background-color: #EEC; }
51.48 +
51.49 +.configuration form label { width: 25%; display: inline-block; text-align:right; overflow: hidden; }
51.50 +.configuration form input { width:40%; font-size: 1.3em; border:1px solid silver; background-color: White; padding: 0.1em; }
51.51 +.configuration form button { width: 20%; margin-left: 0.3em; height: 2em; }
51.52 +
51.53 +.loadingIndicator { position: absolute; top: 0.1em; left: 0.1em; font: 0.8em Arial; background-color: #229; color: White; padding: 0.2em 0.5em 0.2em 0.5em; display: none; }
52.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
52.2 +++ b/javaquery/demo-twitter/src/test/java/org/apidesign/bck2brwsr/demo/twitter/TwitterClientTest.java Fri May 17 10:27:41 2013 +0200
52.3 @@ -0,0 +1,67 @@
52.4 +/**
52.5 + * Back 2 Browser Bytecode Translator
52.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
52.7 + *
52.8 + * This program is free software: you can redistribute it and/or modify
52.9 + * it under the terms of the GNU General Public License as published by
52.10 + * the Free Software Foundation, version 2 of the License.
52.11 + *
52.12 + * This program is distributed in the hope that it will be useful,
52.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
52.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52.15 + * GNU General Public License for more details.
52.16 + *
52.17 + * You should have received a copy of the GNU General Public License
52.18 + * along with this program. Look for COPYING file in the top folder.
52.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
52.20 + */
52.21 +package org.apidesign.bck2brwsr.demo.twitter;
52.22 +
52.23 +import java.util.List;
52.24 +import static org.testng.Assert.*;
52.25 +import org.testng.annotations.BeforeMethod;
52.26 +import org.testng.annotations.Test;
52.27 +
52.28 +/** We can unit test the TwitterModel smoothly.
52.29 + *
52.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
52.31 + */
52.32 +public class TwitterClientTest {
52.33 + private TwitterModel model;
52.34 +
52.35 +
52.36 + @BeforeMethod
52.37 + public void initModel() {
52.38 + model = new TwitterModel().applyBindings();
52.39 + }
52.40 +
52.41 + @Test public void testIsValidToAdd() {
52.42 + model.setUserNameToAdd("Joe");
52.43 + Tweeters t = new Tweeters();
52.44 + t.setName("test");
52.45 + model.getSavedLists().add(t);
52.46 + model.setActiveTweetersName("test");
52.47 +
52.48 + assertTrue(model.isUserNameToAddIsValid(), "Joe is OK");
52.49 + TwitterClient.addUser(model);
52.50 + assertFalse(model.isUserNameToAddIsValid(), "Can't add Joe for the 2nd time");
52.51 + assertEquals(t.getUserNames().size(), 0, "Original tweeters list remains empty");
52.52 +
52.53 + List<String> mod = model.getActiveTweeters();
52.54 + assertTrue(model.isHasUnsavedChanges(), "We have modifications");
52.55 + assertEquals(mod.size(), 1, "One element in the list");
52.56 + assertEquals(mod.get(0), "Joe", "Its name is Joe");
52.57 +
52.58 + assertSame(model.getActiveTweeters(), mod, "Editing list is the modified one");
52.59 +
52.60 + TwitterClient.saveChanges(model);
52.61 + assertFalse(model.isHasUnsavedChanges(), "Does not have anything to save");
52.62 +
52.63 + assertSame(model.getActiveTweeters(), mod, "Still editing the old modified one");
52.64 + }
52.65 +
52.66 + @Test public void httpAtTheEnd() {
52.67 + String res = TwitterClient.Twt.html("Ahoj http://kuk");
52.68 + assertEquals(res, "Ahoj <a href='http://kuk'>http://kuk</a>");
52.69 + }
52.70 +}
53.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
53.2 +++ b/javaquery/demo-twitter/src/test/java/org/apidesign/bck2brwsr/demo/twitter/TwitterProtocolTest.java Fri May 17 10:27:41 2013 +0200
53.3 @@ -0,0 +1,94 @@
53.4 +/**
53.5 + * Back 2 Browser Bytecode Translator
53.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
53.7 + *
53.8 + * This program is free software: you can redistribute it and/or modify
53.9 + * it under the terms of the GNU General Public License as published by
53.10 + * the Free Software Foundation, version 2 of the License.
53.11 + *
53.12 + * This program is distributed in the hope that it will be useful,
53.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
53.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53.15 + * GNU General Public License for more details.
53.16 + *
53.17 + * You should have received a copy of the GNU General Public License
53.18 + * along with this program. Look for COPYING file in the top folder.
53.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
53.20 + */
53.21 +package org.apidesign.bck2brwsr.demo.twitter;
53.22 +
53.23 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
53.24 +import org.apidesign.bck2brwsr.vmtest.Http;
53.25 +import org.apidesign.bck2brwsr.vmtest.VMTest;
53.26 +import org.testng.annotations.Factory;
53.27 +
53.28 +/**
53.29 + *
53.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
53.31 + */
53.32 +public class TwitterProtocolTest {
53.33 + private TwitterModel page;
53.34 + @Http(@Http.Resource(
53.35 + path = "/search.json",
53.36 + mimeType = "application/json",
53.37 + parameters = {"callback"},
53.38 + content = "$0({\"completed_in\":0.04,\"max_id\":320055706885689344,\"max_id_str\""
53.39 + + ":\"320055706885689344\",\"page\":1,\"query\":\"from%3AJaroslavTulach\",\"refresh_url\":"
53.40 + + "\"?since_id=320055706885689344&q=from%3AJaroslavTulach\","
53.41 + + "\"results\":[{\"created_at\":\"Fri, 05 Apr 2013 06:10:01 +0000\","
53.42 + + "\"from_user\":\"JaroslavTulach\",\"from_user_id\":420944648,\"from_user_id_str\":"
53.43 + + "\"420944648\",\"from_user_name\":\"Jaroslav Tulach\",\"geo\":null,\"id\":320055706885689344,"
53.44 + + "\"id_str\":\"320055706885689344\",\"iso_language_code\":\"en\",\"metadata\":{\"result_type\":"
53.45 + + "\"recent\"},\"profile_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_images\\/1656828312\\/jst_normal.gif\","
53.46 + + "\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/1656828312\\/jst_normal.gif\","
53.47 + + "\"source\":\"<a href="http:\\/\\/twitter.com\\/">web<\\/a>\",\"text\":"
53.48 + + "\"@tom_enebo Amzng! Not that I would like #ruby, but I am really glad you guys stabilized the plugin + "
53.49 + + "made it work in #netbeans 7.3! Gd wrk.\",\"to_user\":\"tom_enebo\",\"to_user_id\":14498747,"
53.50 + + "\"to_user_id_str\":\"14498747\",\"to_user_name\":\"tom_enebo\",\"in_reply_to_status_id\":319832359509839872,"
53.51 + + "\"in_reply_to_status_id_str\":\"319832359509839872\"},{\"created_at\":\"Thu, 04 Apr 2013 07:33:06 +0000\","
53.52 + + "\"from_user\":\"JaroslavTulach\",\"from_user_id\":420944648,\"from_user_id_str\":"
53.53 + + "\"420944648\",\"from_user_name\":\"Jaroslav Tulach\",\"geo\":null,\"id\":319714227088678913,"
53.54 + + "\"id_str\":\"319714227088678913\",\"iso_language_code\":\"en\",\"metadata\":{\"result_type\":"
53.55 + + "\"recent\"},\"profile_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_images\\/1656828312\\/jst_normal.gif\","
53.56 + + "\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/1656828312\\/jst_normal.gif\","
53.57 + + "\"source\":\"<a href="http:\\/\\/twitter.com\\/">web<\\/a>\",\"text\":"
53.58 + + "\"RT @drkrab: At #erlangfactory @joerl: Frameworks grow in complexity until nobody can use them.\"},"
53.59 + + "{\"created_at\":\"Tue, 02 Apr 2013 07:44:34 +0000\",\"from_user\":\"JaroslavTulach\","
53.60 + + "\"from_user_id\":420944648,\"from_user_id_str\":\"420944648\",\"from_user_name\":\"Jaroslav Tulach\","
53.61 + + "\"geo\":null,\"id\":318992336145248256,\"id_str\":\"318992336145248256\",\"iso_language_code\":\"en\","
53.62 + + "\"metadata\":{\"result_type\":\"recent\"},\"profile_image_url\":"
53.63 + + "\"http:\\/\\/a0.twimg.com\\/profile_images\\/1656828312\\/jst_normal.gif\","
53.64 + + "\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/1656828312\\/jst_normal.gif\","
53.65 + + "\"source\":\"<a href="http:\\/\\/twitter.com\\/">web<\\/a>\",\"text\":"
53.66 + + "\"Twitter renamed to twttr http:\\/\\/t.co\\/tqaN4T1xlZ - good, I don't have to rename #bck2brwsr!\"},"
53.67 + + "{\"created_at\":\"Sun, 31 Mar 2013 03:52:04 +0000\",\"from_user\":\"JaroslavTulach\",\"from_user_id\":420944648,"
53.68 + + "\"from_user_id_str\":\"420944648\",\"from_user_name\":\"Jaroslav Tulach\",\"geo\":null,"
53.69 + + "\"id\":318209051223789568,\"id_str\":\"318209051223789568\",\"iso_language_code\":\"en\",\"metadata\":"
53.70 + + "{\"result_type\":\"recent\"},\"profile_image_url\":"
53.71 + + "\"http:\\/\\/a0.twimg.com\\/profile_images\\/1656828312\\/jst_normal.gif\","
53.72 + + "\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/1656828312\\/jst_normal.gif\","
53.73 + + "\"source\":\"<a href="http:\\/\\/twitter.com\\/">web<\\/a>\",\"text\":"
53.74 + + "\"Math proofs without words. Ingenious: http:\\/\\/t.co\\/sz7yVbfpGw\"}],\"results_per_page\":100,"
53.75 + + "\"since_id\":0,\"since_id_str\":\"0\"})"
53.76 + ))
53.77 + @BrwsrTest public void readFromTwttr() throws InterruptedException {
53.78 + if (page == null) {
53.79 + page = new TwitterModel();
53.80 + page.applyBindings();
53.81 + page.queryTweets("", "q=xyz");
53.82 + }
53.83 +
53.84 + if (page.getCurrentTweets().isEmpty()) {
53.85 + throw new InterruptedException();
53.86 + }
53.87 +
53.88 + assert 4 == page.getCurrentTweets().size() : "Four tweets: " + page.getCurrentTweets();
53.89 +
53.90 + String firstDate = page.getCurrentTweets().get(0).getCreated_at();
53.91 + assert "Fri, 05 Apr 2013 06:10:01 +0000".equals(firstDate) : "Date is OK: " + firstDate;
53.92 + }
53.93 +
53.94 + @Factory public static Object[] create() {
53.95 + return VMTest.create(TwitterProtocolTest.class);
53.96 + }
53.97 +}
54.1 --- a/javaquery/pom.xml Tue Mar 05 07:57:16 2013 +0100
54.2 +++ b/javaquery/pom.xml Fri May 17 10:27:41 2013 +0200
54.3 @@ -4,16 +4,17 @@
54.4 <parent>
54.5 <artifactId>bck2brwsr</artifactId>
54.6 <groupId>org.apidesign</groupId>
54.7 - <version>0.4-SNAPSHOT</version>
54.8 + <version>0.8-SNAPSHOT</version>
54.9 </parent>
54.10 <groupId>org.apidesign.bck2brwsr</groupId>
54.11 <artifactId>javaquery</artifactId>
54.12 - <version>0.4-SNAPSHOT</version>
54.13 + <version>0.8-SNAPSHOT</version>
54.14 <packaging>pom</packaging>
54.15 <name>JavaQuery API and Demo</name>
54.16 <modules>
54.17 <module>api</module>
54.18 <module>demo-calculator</module>
54.19 <module>demo-calculator-dynamic</module>
54.20 - </modules>
54.21 -</project>
54.22 + <module>demo-twitter</module>
54.23 + </modules>
54.24 +</project>
54.25 \ No newline at end of file
55.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
55.2 +++ b/launcher/api/pom.xml Fri May 17 10:27:41 2013 +0200
55.3 @@ -0,0 +1,31 @@
55.4 +<?xml version="1.0"?>
55.5 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55.6 + <modelVersion>4.0.0</modelVersion>
55.7 + <parent>
55.8 + <groupId>org.apidesign.bck2brwsr</groupId>
55.9 + <artifactId>launcher-pom</artifactId>
55.10 + <version>0.8-SNAPSHOT</version>
55.11 + </parent>
55.12 + <groupId>org.apidesign.bck2brwsr</groupId>
55.13 + <artifactId>launcher</artifactId>
55.14 + <version>0.8-SNAPSHOT</version>
55.15 + <name>Launcher API</name>
55.16 + <url>http://maven.apache.org</url>
55.17 + <properties>
55.18 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
55.19 + </properties>
55.20 + <build>
55.21 + <plugins>
55.22 + <plugin>
55.23 + <groupId>org.apache.maven.plugins</groupId>
55.24 + <artifactId>maven-javadoc-plugin</artifactId>
55.25 + <configuration>
55.26 + <subpackages>org.apidesign.bck2brwsr.launcher</subpackages>
55.27 + <skip>false</skip>
55.28 + </configuration>
55.29 + </plugin>
55.30 + </plugins>
55.31 + </build>
55.32 + <dependencies>
55.33 + </dependencies>
55.34 +</project>
56.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
56.2 +++ b/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Fri May 17 10:27:41 2013 +0200
56.3 @@ -0,0 +1,115 @@
56.4 +/**
56.5 + * Back 2 Browser Bytecode Translator
56.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
56.7 + *
56.8 + * This program is free software: you can redistribute it and/or modify
56.9 + * it under the terms of the GNU General Public License as published by
56.10 + * the Free Software Foundation, version 2 of the License.
56.11 + *
56.12 + * This program is distributed in the hope that it will be useful,
56.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
56.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
56.15 + * GNU General Public License for more details.
56.16 + *
56.17 + * You should have received a copy of the GNU General Public License
56.18 + * along with this program. Look for COPYING file in the top folder.
56.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
56.20 + */
56.21 +package org.apidesign.bck2brwsr.launcher;
56.22 +
56.23 +import java.io.IOException;
56.24 +import java.io.InputStream;
56.25 +import java.util.ArrayList;
56.26 +import java.util.List;
56.27 +import java.util.concurrent.CountDownLatch;
56.28 +import java.util.concurrent.TimeUnit;
56.29 +
56.30 +/** Represents individual method invocation, its context and its result.
56.31 + *
56.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
56.33 + */
56.34 +public final class InvocationContext {
56.35 + final CountDownLatch wait = new CountDownLatch(1);
56.36 + final Class<?> clazz;
56.37 + final String methodName;
56.38 + private final Launcher launcher;
56.39 + private String result;
56.40 + private Throwable exception;
56.41 + String html;
56.42 + final List<Resource> resources = new ArrayList<>();
56.43 +
56.44 + InvocationContext(Launcher launcher, Class<?> clazz, String methodName) {
56.45 + this.launcher = launcher;
56.46 + this.clazz = clazz;
56.47 + this.methodName = methodName;
56.48 + }
56.49 +
56.50 + /** An HTML fragment to be available for the execution. Useful primarily when
56.51 + * executing in a browser via {@link Launcher#createBrowser(java.lang.String)}.
56.52 + * @param html the html fragment
56.53 + */
56.54 + public void setHtmlFragment(String html) {
56.55 + this.html = html;
56.56 + }
56.57 +
56.58 + /** HTTP resource to be available during execution. An invocation may
56.59 + * perform an HTTP query and obtain a resource relative to the page.
56.60 + */
56.61 + public void addHttpResource(String relativePath, String mimeType, String[] parameters, InputStream content) {
56.62 + if (relativePath == null || mimeType == null || content == null || parameters == null) {
56.63 + throw new NullPointerException();
56.64 + }
56.65 + resources.add(new Resource(content, mimeType, relativePath, parameters));
56.66 + }
56.67 +
56.68 + /** Invokes the associated method.
56.69 + * @return the textual result of the invocation
56.70 + */
56.71 + public String invoke() throws IOException {
56.72 + launcher.runMethod(this);
56.73 + return toString();
56.74 + }
56.75 +
56.76 + /** Obtains textual result of the invocation.
56.77 + * @return text representing the exception or result value
56.78 + */
56.79 + @Override
56.80 + public String toString() {
56.81 + if (exception != null) {
56.82 + return exception.toString();
56.83 + }
56.84 + return result;
56.85 + }
56.86 +
56.87 + /**
56.88 + * @param timeOut
56.89 + * @throws InterruptedException
56.90 + */
56.91 + void await(long timeOut) throws InterruptedException {
56.92 + wait.await(timeOut, TimeUnit.MILLISECONDS);
56.93 + }
56.94 +
56.95 + void result(String r, Throwable e) {
56.96 + this.result = r;
56.97 + this.exception = e;
56.98 + wait.countDown();
56.99 + }
56.100 +
56.101 +
56.102 + static final class Resource {
56.103 + final InputStream httpContent;
56.104 + final String httpType;
56.105 + final String httpPath;
56.106 + final String[] parameters;
56.107 +
56.108 + Resource(InputStream httpContent, String httpType, String httpPath,
56.109 + String[] parameters
56.110 + ) {
56.111 + httpContent.mark(Integer.MAX_VALUE);
56.112 + this.httpContent = httpContent;
56.113 + this.httpType = httpType;
56.114 + this.httpPath = httpPath;
56.115 + this.parameters = parameters;
56.116 + }
56.117 + }
56.118 +}
57.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
57.2 +++ b/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java Fri May 17 10:27:41 2013 +0200
57.3 @@ -0,0 +1,166 @@
57.4 +/**
57.5 + * Back 2 Browser Bytecode Translator
57.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
57.7 + *
57.8 + * This program is free software: you can redistribute it and/or modify
57.9 + * it under the terms of the GNU General Public License as published by
57.10 + * the Free Software Foundation, version 2 of the License.
57.11 + *
57.12 + * This program is distributed in the hope that it will be useful,
57.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
57.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57.15 + * GNU General Public License for more details.
57.16 + *
57.17 + * You should have received a copy of the GNU General Public License
57.18 + * along with this program. Look for COPYING file in the top folder.
57.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
57.20 + */
57.21 +package org.apidesign.bck2brwsr.launcher;
57.22 +
57.23 +import java.io.Closeable;
57.24 +import java.io.File;
57.25 +import java.io.IOException;
57.26 +import java.lang.reflect.Constructor;
57.27 +
57.28 +/** An abstraction for executing tests in a Bck2Brwsr virtual machine.
57.29 + * Either in {@link Launcher#createJavaScript JavaScript engine},
57.30 + * or in {@link Launcher#createBrowser external browser}.
57.31 + * <p>
57.32 + * There also are methods to {@link #showDir(java.io.File, java.lang.String) display pages}
57.33 + * in an external browser served by internal HTTP server.
57.34 + *
57.35 + * @author Jaroslav Tulach <jtulach@netbeans.org>
57.36 + */
57.37 +public abstract class Launcher {
57.38 +
57.39 + Launcher() {
57.40 + }
57.41 +
57.42 + /** Initializes the launcher. This may mean starting a web browser or
57.43 + * initializing execution engine.
57.44 + * @throws IOException if something goes wrong
57.45 + */
57.46 + public abstract void initialize() throws IOException;
57.47 +
57.48 + /** Shuts down the launcher.
57.49 + * @throws IOException if something goes wrong
57.50 + */
57.51 + public abstract void shutdown() throws IOException;
57.52 +
57.53 +
57.54 + /** Builds an invocation context. The context can later be customized
57.55 + * and {@link InvocationContext#invoke() invoked}.
57.56 + *
57.57 + * @param clazz the class to execute method from
57.58 + * @param method the method to execute
57.59 + * @return the context pointing to the selected method
57.60 + */
57.61 + public InvocationContext createInvocation(Class<?> clazz, String method) {
57.62 + return new InvocationContext(this, clazz, method);
57.63 + }
57.64 +
57.65 +
57.66 + /** Creates launcher that uses internal JavaScript engine (Rhino).
57.67 + * @return the launcher
57.68 + */
57.69 + public static Launcher createJavaScript() {
57.70 + try {
57.71 + Class<?> c = loadClass("org.apidesign.bck2brwsr.launcher.JSLauncher");
57.72 + return (Launcher) c.newInstance();
57.73 + } catch (Exception ex) {
57.74 + throw new IllegalStateException("Please include org.apidesign.bck2brwsr:launcher.html dependency!", ex);
57.75 + }
57.76 + }
57.77 +
57.78 + /** Creates launcher that is using external browser.
57.79 + *
57.80 + * @param cmd <code>null</code> to use <code>java.awt.Desktop</code> to show the launcher
57.81 + * or a string to execute in an external process (with a parameter to the URL)
57.82 + * @return launcher executing in external browser.
57.83 + */
57.84 + public static Launcher createBrowser(String cmd) {
57.85 + String msg = "Trying to create browser '" + cmd + "'";
57.86 + try {
57.87 + Class<?> c;
57.88 + if ("fxbrwsr".equals(cmd)) { // NOI18N
57.89 + msg = "Please include org.apidesign.bck2brwsr:launcher.fx dependency!";
57.90 + c = loadClass("org.apidesign.bck2brwsr.launcher.FXBrwsrLauncher"); // NOI18N
57.91 + } else {
57.92 + msg = "Please include org.apidesign.bck2brwsr:launcher.html dependency!";
57.93 + c = loadClass("org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher"); // NOI18N
57.94 + if ("bck2brwsr".equals(cmd)) { // NOI18N
57.95 + // use default executable
57.96 + cmd = null;
57.97 + }
57.98 + }
57.99 + Constructor<?> cnstr = c.getConstructor(String.class);
57.100 + return (Launcher) cnstr.newInstance(cmd);
57.101 + } catch (Exception ex) {
57.102 + throw new IllegalStateException(msg, ex);
57.103 + }
57.104 + }
57.105 +
57.106 + /** Starts an HTTP server which provides access to classes and resources
57.107 + * available in the <code>classes</code> URL and shows a start page
57.108 + * available as {@link ClassLoader#getResource(java.lang.String)} from the
57.109 + * provide classloader. Opens a browser with URL showing the start page.
57.110 + *
57.111 + * @param classes classloader offering access to classes and resources
57.112 + * @param startpage page to show in the browser
57.113 + * @return interface that allows one to stop the server
57.114 + * @throws IOException if something goes wrong
57.115 + */
57.116 + public static Closeable showURL(ClassLoader classes, String startpage) throws IOException {
57.117 + return showURL(null, classes, startpage);
57.118 + }
57.119 + /** Starts an HTTP server which provides access to classes and resources
57.120 + * available in the <code>classes</code> URL and shows a start page
57.121 + * available as {@link ClassLoader#getResource(java.lang.String)} from the
57.122 + * provide classloader. Opens a browser with URL showing the start page.
57.123 + *
57.124 + * @param brwsr name of browser to use or <code>null</code>
57.125 + * @param classes classloader offering access to classes and resources
57.126 + * @param startpage page to show in the browser
57.127 + * @return interface that allows one to stop the server
57.128 + * @throws IOException if something goes wrong
57.129 + * @since 0.7
57.130 + */
57.131 + public static Closeable showURL(String brwsr, ClassLoader classes, String startpage) throws IOException {
57.132 + Launcher l = createBrowser(brwsr);
57.133 + l.addClassLoader(classes);
57.134 + l.showURL(startpage);
57.135 + return (Closeable) l;
57.136 + }
57.137 + /** Starts an HTTP server which provides access to certain directory.
57.138 + * The <code>startpage</code> should be relative location inside the root
57.139 + * directory. Opens a browser with URL showing the start page.
57.140 + *
57.141 + * @param directory the root directory on disk
57.142 + * @param startpage relative path from the root to the page
57.143 + * @exception IOException if something goes wrong.
57.144 + */
57.145 + public static Closeable showDir(File directory, String startpage) throws IOException {
57.146 + Launcher l = createBrowser(null);
57.147 + l.showDirectory(directory, startpage);
57.148 + return (Closeable) l;
57.149 + }
57.150 +
57.151 + abstract InvocationContext runMethod(InvocationContext c) throws IOException;
57.152 +
57.153 +
57.154 + private static Class<?> loadClass(String cn) throws ClassNotFoundException {
57.155 + return Launcher.class.getClassLoader().loadClass(cn);
57.156 + }
57.157 +
57.158 + void showDirectory(File directory, String startpage) throws IOException {
57.159 + throw new UnsupportedOperationException();
57.160 + }
57.161 +
57.162 + void showURL(String startpage) throws IOException {
57.163 + throw new UnsupportedOperationException();
57.164 + }
57.165 +
57.166 + void addClassLoader(ClassLoader classes) {
57.167 + throw new UnsupportedOperationException();
57.168 + }
57.169 +}
58.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
58.2 +++ b/launcher/fx/pom.xml Fri May 17 10:27:41 2013 +0200
58.3 @@ -0,0 +1,58 @@
58.4 +<?xml version="1.0"?>
58.5 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
58.6 + <modelVersion>4.0.0</modelVersion>
58.7 + <parent>
58.8 + <groupId>org.apidesign.bck2brwsr</groupId>
58.9 + <artifactId>launcher-pom</artifactId>
58.10 + <version>0.8-SNAPSHOT</version>
58.11 + </parent>
58.12 + <groupId>org.apidesign.bck2brwsr</groupId>
58.13 + <artifactId>launcher.fx</artifactId>
58.14 + <version>0.8-SNAPSHOT</version>
58.15 + <name>FXBrwsr Launcher</name>
58.16 + <url>http://maven.apache.org</url>
58.17 + <build>
58.18 + <plugins>
58.19 + <plugin>
58.20 + <groupId>org.apache.maven.plugins</groupId>
58.21 + <artifactId>maven-compiler-plugin</artifactId>
58.22 + <version>2.3.2</version>
58.23 + <configuration>
58.24 + <source>1.7</source>
58.25 + <target>1.7</target>
58.26 + </configuration>
58.27 + </plugin>
58.28 + <plugin>
58.29 + <groupId>org.apache.maven.plugins</groupId>
58.30 + <artifactId>maven-javadoc-plugin</artifactId>
58.31 + <configuration>
58.32 + <subpackages>org.apidesign.bck2brwsr.launcher.fx</subpackages>
58.33 + <skip>false</skip>
58.34 + </configuration>
58.35 + </plugin>
58.36 + </plugins>
58.37 + </build>
58.38 + <properties>
58.39 + <jfxrt.jar>${java.home}/lib/jfxrt.jar</jfxrt.jar>
58.40 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
58.41 + </properties>
58.42 + <dependencies>
58.43 + <dependency>
58.44 + <groupId>${project.groupId}</groupId>
58.45 + <artifactId>launcher</artifactId>
58.46 + <version>${project.version}</version>
58.47 + </dependency>
58.48 + <dependency>
58.49 + <groupId>org.glassfish.grizzly</groupId>
58.50 + <artifactId>grizzly-http-server</artifactId>
58.51 + <version>${grizzly.version}</version>
58.52 + </dependency>
58.53 + <dependency>
58.54 + <groupId>com.oracle</groupId>
58.55 + <artifactId>javafx</artifactId>
58.56 + <version>2.2</version>
58.57 + <scope>system</scope>
58.58 + <systemPath>${jfxrt.jar}</systemPath>
58.59 + </dependency>
58.60 + </dependencies>
58.61 +</project>
59.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
59.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Fri May 17 10:27:41 2013 +0200
59.3 @@ -0,0 +1,634 @@
59.4 +/**
59.5 + * Back 2 Browser Bytecode Translator
59.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
59.7 + *
59.8 + * This program is free software: you can redistribute it and/or modify
59.9 + * it under the terms of the GNU General Public License as published by
59.10 + * the Free Software Foundation, version 2 of the License.
59.11 + *
59.12 + * This program is distributed in the hope that it will be useful,
59.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
59.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
59.15 + * GNU General Public License for more details.
59.16 + *
59.17 + * You should have received a copy of the GNU General Public License
59.18 + * along with this program. Look for COPYING file in the top folder.
59.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
59.20 + */
59.21 +package org.apidesign.bck2brwsr.launcher;
59.22 +
59.23 +import java.io.Closeable;
59.24 +import java.io.File;
59.25 +import java.io.IOException;
59.26 +import java.io.InputStream;
59.27 +import java.io.InterruptedIOException;
59.28 +import java.io.OutputStream;
59.29 +import java.io.Reader;
59.30 +import java.io.UnsupportedEncodingException;
59.31 +import java.io.Writer;
59.32 +import java.net.URI;
59.33 +import java.net.URISyntaxException;
59.34 +import java.net.URL;
59.35 +import java.util.ArrayList;
59.36 +import java.util.Arrays;
59.37 +import java.util.Enumeration;
59.38 +import java.util.LinkedHashSet;
59.39 +import java.util.List;
59.40 +import java.util.Set;
59.41 +import java.util.concurrent.BlockingQueue;
59.42 +import java.util.concurrent.Callable;
59.43 +import java.util.concurrent.CountDownLatch;
59.44 +import java.util.concurrent.LinkedBlockingQueue;
59.45 +import java.util.concurrent.TimeUnit;
59.46 +import java.util.logging.Level;
59.47 +import java.util.logging.Logger;
59.48 +import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource;
59.49 +import org.glassfish.grizzly.PortRange;
59.50 +import org.glassfish.grizzly.http.server.HttpHandler;
59.51 +import org.glassfish.grizzly.http.server.HttpServer;
59.52 +import org.glassfish.grizzly.http.server.NetworkListener;
59.53 +import org.glassfish.grizzly.http.server.Request;
59.54 +import org.glassfish.grizzly.http.server.Response;
59.55 +import org.glassfish.grizzly.http.server.ServerConfiguration;
59.56 +import org.glassfish.grizzly.http.util.HttpStatus;
59.57 +import org.glassfish.grizzly.threadpool.ThreadPoolConfig;
59.58 +
59.59 +/**
59.60 + * Lightweight server to launch Bck2Brwsr applications and tests.
59.61 + * Supports execution in native browser as well as Java's internal
59.62 + * execution engine.
59.63 + */
59.64 +abstract class BaseHTTPLauncher extends Launcher implements Closeable, Callable<HttpServer> {
59.65 + static final Logger LOG = Logger.getLogger(BaseHTTPLauncher.class.getName());
59.66 + private static final InvocationContext END = new InvocationContext(null, null, null);
59.67 + private final Set<ClassLoader> loaders = new LinkedHashSet<>();
59.68 + private final BlockingQueue<InvocationContext> methods = new LinkedBlockingQueue<>();
59.69 + private long timeOut;
59.70 + private final Res resources = new Res();
59.71 + private final String cmd;
59.72 + private Object[] brwsr;
59.73 + private HttpServer server;
59.74 + private CountDownLatch wait;
59.75 +
59.76 + public BaseHTTPLauncher(String cmd) {
59.77 + this.cmd = cmd;
59.78 + addClassLoader(BaseHTTPLauncher.class.getClassLoader());
59.79 + setTimeout(180000);
59.80 + }
59.81 +
59.82 + @Override
59.83 + InvocationContext runMethod(InvocationContext c) throws IOException {
59.84 + loaders.add(c.clazz.getClassLoader());
59.85 + methods.add(c);
59.86 + try {
59.87 + c.await(timeOut);
59.88 + } catch (InterruptedException ex) {
59.89 + throw new IOException(ex);
59.90 + }
59.91 + return c;
59.92 + }
59.93 +
59.94 + public void setTimeout(long ms) {
59.95 + timeOut = ms;
59.96 + }
59.97 +
59.98 + public void addClassLoader(ClassLoader url) {
59.99 + this.loaders.add(url);
59.100 + }
59.101 +
59.102 + ClassLoader[] loaders() {
59.103 + return loaders.toArray(new ClassLoader[loaders.size()]);
59.104 + }
59.105 +
59.106 + public void showURL(String startpage) throws IOException {
59.107 + if (!startpage.startsWith("/")) {
59.108 + startpage = "/" + startpage;
59.109 + }
59.110 + HttpServer s = initServer(".", true);
59.111 + int last = startpage.lastIndexOf('/');
59.112 + String prefix = startpage.substring(0, last);
59.113 + String simpleName = startpage.substring(last);
59.114 + s.getServerConfiguration().addHttpHandler(new SubTree(resources, prefix), "/");
59.115 + server = s;
59.116 + try {
59.117 + launchServerAndBrwsr(s, simpleName);
59.118 + } catch (URISyntaxException | InterruptedException ex) {
59.119 + throw new IOException(ex);
59.120 + }
59.121 + }
59.122 +
59.123 + void showDirectory(File dir, String startpage) throws IOException {
59.124 + if (!startpage.startsWith("/")) {
59.125 + startpage = "/" + startpage;
59.126 + }
59.127 + HttpServer s = initServer(dir.getPath(), false);
59.128 + try {
59.129 + launchServerAndBrwsr(s, startpage);
59.130 + } catch (URISyntaxException | InterruptedException ex) {
59.131 + throw new IOException(ex);
59.132 + }
59.133 + }
59.134 +
59.135 + @Override
59.136 + public void initialize() throws IOException {
59.137 + try {
59.138 + executeInBrowser();
59.139 + } catch (InterruptedException ex) {
59.140 + final InterruptedIOException iio = new InterruptedIOException(ex.getMessage());
59.141 + iio.initCause(ex);
59.142 + throw iio;
59.143 + } catch (Exception ex) {
59.144 + if (ex instanceof IOException) {
59.145 + throw (IOException)ex;
59.146 + }
59.147 + if (ex instanceof RuntimeException) {
59.148 + throw (RuntimeException)ex;
59.149 + }
59.150 + throw new IOException(ex);
59.151 + }
59.152 + }
59.153 +
59.154 + private HttpServer initServer(String path, boolean addClasses) throws IOException {
59.155 + HttpServer s = HttpServer.createSimpleServer(path, new PortRange(8080, 65535));
59.156 +
59.157 + ThreadPoolConfig fewThreads = ThreadPoolConfig.defaultConfig().copy().
59.158 + setPoolName("Fx/Bck2 Brwsr").
59.159 + setCorePoolSize(1).
59.160 + setMaxPoolSize(5);
59.161 + ThreadPoolConfig oneKernel = ThreadPoolConfig.defaultConfig().copy().
59.162 + setPoolName("Kernel Fx/Bck2").
59.163 + setCorePoolSize(1).
59.164 + setMaxPoolSize(3);
59.165 + for (NetworkListener nl : s.getListeners()) {
59.166 + nl.getTransport().setWorkerThreadPoolConfig(fewThreads);
59.167 + nl.getTransport().setKernelThreadPoolConfig(oneKernel);
59.168 + }
59.169 +
59.170 + final ServerConfiguration conf = s.getServerConfiguration();
59.171 + if (addClasses) {
59.172 + conf.addHttpHandler(new VM(), "/bck2brwsr.js");
59.173 + conf.addHttpHandler(new Classes(resources), "/classes/");
59.174 + }
59.175 + return s;
59.176 + }
59.177 +
59.178 + private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
59.179 + wait = new CountDownLatch(1);
59.180 + server = initServer(".", true);
59.181 + final ServerConfiguration conf = server.getServerConfiguration();
59.182 +
59.183 + class DynamicResourceHandler extends HttpHandler {
59.184 + private final InvocationContext ic;
59.185 + public DynamicResourceHandler(InvocationContext ic) {
59.186 + if (ic == null || ic.resources.isEmpty()) {
59.187 + throw new NullPointerException();
59.188 + }
59.189 + this.ic = ic;
59.190 + for (Resource r : ic.resources) {
59.191 + conf.addHttpHandler(this, r.httpPath);
59.192 + }
59.193 + }
59.194 +
59.195 + public void close() {
59.196 + conf.removeHttpHandler(this);
59.197 + }
59.198 +
59.199 + @Override
59.200 + public void service(Request request, Response response) throws Exception {
59.201 + for (Resource r : ic.resources) {
59.202 + if (r.httpPath.equals(request.getRequestURI())) {
59.203 + LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
59.204 + response.setContentType(r.httpType);
59.205 + r.httpContent.reset();
59.206 + String[] params = null;
59.207 + if (r.parameters.length != 0) {
59.208 + params = new String[r.parameters.length];
59.209 + for (int i = 0; i < r.parameters.length; i++) {
59.210 + params[i] = request.getParameter(r.parameters[i]);
59.211 + if (params[i] == null) {
59.212 + if ("http.method".equals(r.parameters[i])) {
59.213 + params[i] = request.getMethod().toString();
59.214 + } else if ("http.requestBody".equals(r.parameters[i])) {
59.215 + Reader rdr = request.getReader();
59.216 + StringBuilder sb = new StringBuilder();
59.217 + for (;;) {
59.218 + int ch = rdr.read();
59.219 + if (ch == -1) {
59.220 + break;
59.221 + }
59.222 + sb.append((char)ch);
59.223 + }
59.224 + params[i] = sb.toString();
59.225 + }
59.226 + }
59.227 + if (params[i] == null) {
59.228 + params[i] = "null";
59.229 + }
59.230 + }
59.231 + }
59.232 +
59.233 + copyStream(r.httpContent, response.getOutputStream(), null, params);
59.234 + }
59.235 + }
59.236 + }
59.237 + }
59.238 +
59.239 + conf.addHttpHandler(new Page(resources, harnessResource()), "/execute");
59.240 +
59.241 + conf.addHttpHandler(new HttpHandler() {
59.242 + int cnt;
59.243 + List<InvocationContext> cases = new ArrayList<>();
59.244 + DynamicResourceHandler prev;
59.245 + @Override
59.246 + public void service(Request request, Response response) throws Exception {
59.247 + String id = request.getParameter("request");
59.248 + String value = request.getParameter("result");
59.249 + if (value != null && value.indexOf((char)0xC5) != -1) {
59.250 + value = toUTF8(value);
59.251 + }
59.252 +
59.253 +
59.254 + InvocationContext mi = null;
59.255 + int caseNmbr = -1;
59.256 +
59.257 + if (id != null && value != null) {
59.258 + LOG.log(Level.INFO, "Received result for case {0} = {1}", new Object[]{id, value});
59.259 + value = decodeURL(value);
59.260 + int indx = Integer.parseInt(id);
59.261 + cases.get(indx).result(value, null);
59.262 + if (++indx < cases.size()) {
59.263 + mi = cases.get(indx);
59.264 + LOG.log(Level.INFO, "Re-executing case {0}", indx);
59.265 + caseNmbr = indx;
59.266 + }
59.267 + } else {
59.268 + if (!cases.isEmpty()) {
59.269 + LOG.info("Re-executing test cases");
59.270 + mi = cases.get(0);
59.271 + caseNmbr = 0;
59.272 + }
59.273 + }
59.274 +
59.275 + if (prev != null) {
59.276 + prev.close();
59.277 + prev = null;
59.278 + }
59.279 +
59.280 + if (mi == null) {
59.281 + mi = methods.take();
59.282 + caseNmbr = cnt++;
59.283 + }
59.284 + if (mi == END) {
59.285 + response.getWriter().write("");
59.286 + wait.countDown();
59.287 + cnt = 0;
59.288 + LOG.log(Level.INFO, "End of data reached. Exiting.");
59.289 + return;
59.290 + }
59.291 +
59.292 + if (!mi.resources.isEmpty()) {
59.293 + prev = new DynamicResourceHandler(mi);
59.294 + }
59.295 +
59.296 + cases.add(mi);
59.297 + final String cn = mi.clazz.getName();
59.298 + final String mn = mi.methodName;
59.299 + LOG.log(Level.INFO, "Request for {0} case. Sending {1}.{2}", new Object[]{caseNmbr, cn, mn});
59.300 + response.getWriter().write("{"
59.301 + + "className: '" + cn + "', "
59.302 + + "methodName: '" + mn + "', "
59.303 + + "request: " + caseNmbr
59.304 + );
59.305 + if (mi.html != null) {
59.306 + response.getWriter().write(", html: '");
59.307 + response.getWriter().write(encodeJSON(mi.html));
59.308 + response.getWriter().write("'");
59.309 + }
59.310 + response.getWriter().write("}");
59.311 + }
59.312 + }, "/data");
59.313 +
59.314 + this.brwsr = launchServerAndBrwsr(server, "/execute");
59.315 + }
59.316 +
59.317 + private static String encodeJSON(String in) {
59.318 + StringBuilder sb = new StringBuilder();
59.319 + for (int i = 0; i < in.length(); i++) {
59.320 + char ch = in.charAt(i);
59.321 + if (ch < 32 || ch == '\'' || ch == '"') {
59.322 + sb.append("\\u");
59.323 + String hs = "0000" + Integer.toHexString(ch);
59.324 + hs = hs.substring(hs.length() - 4);
59.325 + sb.append(hs);
59.326 + } else {
59.327 + sb.append(ch);
59.328 + }
59.329 + }
59.330 + return sb.toString();
59.331 + }
59.332 +
59.333 + @Override
59.334 + public void shutdown() throws IOException {
59.335 + methods.offer(END);
59.336 + for (;;) {
59.337 + int prev = methods.size();
59.338 + try {
59.339 + if (wait != null && wait.await(timeOut, TimeUnit.MILLISECONDS)) {
59.340 + break;
59.341 + }
59.342 + } catch (InterruptedException ex) {
59.343 + throw new IOException(ex);
59.344 + }
59.345 + if (prev == methods.size()) {
59.346 + LOG.log(
59.347 + Level.WARNING,
59.348 + "Timeout and no test has been executed meanwhile (at {0}). Giving up.",
59.349 + methods.size()
59.350 + );
59.351 + break;
59.352 + }
59.353 + LOG.log(Level.INFO,
59.354 + "Timeout, but tests got from {0} to {1}. Trying again.",
59.355 + new Object[]{prev, methods.size()}
59.356 + );
59.357 + }
59.358 + stopServerAndBrwsr(server, brwsr);
59.359 + }
59.360 +
59.361 + static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
59.362 + for (;;) {
59.363 + int ch = is.read();
59.364 + if (ch == -1) {
59.365 + break;
59.366 + }
59.367 + if (ch == '$' && params.length > 0) {
59.368 + int cnt = is.read() - '0';
59.369 + if (baseURL != null && cnt == 'U' - '0') {
59.370 + os.write(baseURL.getBytes("UTF-8"));
59.371 + } else {
59.372 + if (cnt >= 0 && cnt < params.length) {
59.373 + os.write(params[cnt].getBytes("UTF-8"));
59.374 + } else {
59.375 + os.write('$');
59.376 + os.write(cnt + '0');
59.377 + }
59.378 + }
59.379 + } else {
59.380 + os.write(ch);
59.381 + }
59.382 + }
59.383 + }
59.384 +
59.385 + private Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
59.386 + server.start();
59.387 + NetworkListener listener = server.getListeners().iterator().next();
59.388 + int port = listener.getPort();
59.389 +
59.390 + URI uri = new URI("http://localhost:" + port + page);
59.391 + return showBrwsr(uri);
59.392 + }
59.393 + private static String toUTF8(String value) throws UnsupportedEncodingException {
59.394 + byte[] arr = new byte[value.length()];
59.395 + for (int i = 0; i < arr.length; i++) {
59.396 + arr[i] = (byte)value.charAt(i);
59.397 + }
59.398 + return new String(arr, "UTF-8");
59.399 + }
59.400 +
59.401 + private static String decodeURL(String s) {
59.402 + for (;;) {
59.403 + int pos = s.indexOf('%');
59.404 + if (pos == -1) {
59.405 + return s;
59.406 + }
59.407 + int i = Integer.parseInt(s.substring(pos + 1, pos + 2), 16);
59.408 + s = s.substring(0, pos) + (char)i + s.substring(pos + 2);
59.409 + }
59.410 + }
59.411 +
59.412 + private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException {
59.413 + if (brwsr == null) {
59.414 + return;
59.415 + }
59.416 + Process process = (Process)brwsr[0];
59.417 +
59.418 + server.stop();
59.419 + InputStream stdout = process.getInputStream();
59.420 + InputStream stderr = process.getErrorStream();
59.421 + drain("StdOut", stdout);
59.422 + drain("StdErr", stderr);
59.423 + process.destroy();
59.424 + int res;
59.425 + try {
59.426 + res = process.waitFor();
59.427 + } catch (InterruptedException ex) {
59.428 + throw new IOException(ex);
59.429 + }
59.430 + LOG.log(Level.INFO, "Exit code: {0}", res);
59.431 +
59.432 + deleteTree((File)brwsr[1]);
59.433 + }
59.434 +
59.435 + private static void drain(String name, InputStream is) throws IOException {
59.436 + int av = is.available();
59.437 + if (av > 0) {
59.438 + StringBuilder sb = new StringBuilder();
59.439 + sb.append("v== ").append(name).append(" ==v\n");
59.440 + while (av-- > 0) {
59.441 + sb.append((char)is.read());
59.442 + }
59.443 + sb.append("\n^== ").append(name).append(" ==^");
59.444 + LOG.log(Level.INFO, sb.toString());
59.445 + }
59.446 + }
59.447 +
59.448 + private void deleteTree(File file) {
59.449 + if (file == null) {
59.450 + return;
59.451 + }
59.452 + File[] arr = file.listFiles();
59.453 + if (arr != null) {
59.454 + for (File s : arr) {
59.455 + deleteTree(s);
59.456 + }
59.457 + }
59.458 + file.delete();
59.459 + }
59.460 +
59.461 + @Override
59.462 + public HttpServer call() throws Exception {
59.463 + return server;
59.464 + }
59.465 +
59.466 + @Override
59.467 + public void close() throws IOException {
59.468 + shutdown();
59.469 + }
59.470 +
59.471 + protected Object[] showBrwsr(URI uri) throws IOException {
59.472 + LOG.log(Level.INFO, "Showing {0}", uri);
59.473 + if (cmd == null) {
59.474 + try {
59.475 + LOG.log(Level.INFO, "Trying Desktop.browse on {0} {2} by {1}", new Object[] {
59.476 + System.getProperty("java.vm.name"),
59.477 + System.getProperty("java.vm.vendor"),
59.478 + System.getProperty("java.vm.version"),
59.479 + });
59.480 + java.awt.Desktop.getDesktop().browse(uri);
59.481 + LOG.log(Level.INFO, "Desktop.browse successfully finished");
59.482 + return null;
59.483 + } catch (UnsupportedOperationException ex) {
59.484 + LOG.log(Level.INFO, "Desktop.browse not supported: {0}", ex.getMessage());
59.485 + LOG.log(Level.FINE, null, ex);
59.486 + }
59.487 + }
59.488 + {
59.489 + String cmdName = cmd == null ? "xdg-open" : cmd;
59.490 + String[] cmdArr = {
59.491 + cmdName, uri.toString()
59.492 + };
59.493 + LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmdArr));
59.494 + final Process process = Runtime.getRuntime().exec(cmdArr);
59.495 + return new Object[] { process, null };
59.496 + }
59.497 + }
59.498 +
59.499 + abstract void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException;
59.500 + abstract String harnessResource();
59.501 +
59.502 + class Res {
59.503 + public InputStream get(String resource) throws IOException {
59.504 + URL u = null;
59.505 + for (ClassLoader l : loaders) {
59.506 + Enumeration<URL> en = l.getResources(resource);
59.507 + while (en.hasMoreElements()) {
59.508 + u = en.nextElement();
59.509 + if (u.toExternalForm().matches("^.*emul.*rt\\.jar.*$")) {
59.510 + return u.openStream();
59.511 + }
59.512 + }
59.513 + }
59.514 + if (u != null) {
59.515 + if (u.toExternalForm().contains("rt.jar")) {
59.516 + LOG.log(Level.WARNING, "Fallback to bootclasspath for {0}", u);
59.517 + }
59.518 + return u.openStream();
59.519 + }
59.520 + throw new IOException("Can't find " + resource);
59.521 + }
59.522 + }
59.523 +
59.524 + private static class Page extends HttpHandler {
59.525 + final String resource;
59.526 + private final String[] args;
59.527 + private final Res res;
59.528 +
59.529 + public Page(Res res, String resource, String... args) {
59.530 + this.res = res;
59.531 + this.resource = resource;
59.532 + this.args = args.length == 0 ? new String[] { "$0" } : args;
59.533 + }
59.534 +
59.535 + @Override
59.536 + public void service(Request request, Response response) throws Exception {
59.537 + String r = computePage(request);
59.538 + if (r.startsWith("/")) {
59.539 + r = r.substring(1);
59.540 + }
59.541 + String[] replace = {};
59.542 + if (r.endsWith(".html")) {
59.543 + response.setContentType("text/html");
59.544 + LOG.info("Content type text/html");
59.545 + replace = args;
59.546 + }
59.547 + if (r.endsWith(".xhtml")) {
59.548 + response.setContentType("application/xhtml+xml");
59.549 + LOG.info("Content type application/xhtml+xml");
59.550 + replace = args;
59.551 + }
59.552 + OutputStream os = response.getOutputStream();
59.553 + try (InputStream is = res.get(r)) {
59.554 + copyStream(is, os, request.getRequestURL().toString(), replace);
59.555 + } catch (IOException ex) {
59.556 + response.setDetailMessage(ex.getLocalizedMessage());
59.557 + response.setError();
59.558 + response.setStatus(404);
59.559 + }
59.560 + }
59.561 +
59.562 + protected String computePage(Request request) {
59.563 + String r = resource;
59.564 + if (r == null) {
59.565 + r = request.getHttpHandlerPath();
59.566 + }
59.567 + return r;
59.568 + }
59.569 + }
59.570 +
59.571 + private static class SubTree extends Page {
59.572 +
59.573 + public SubTree(Res res, String resource, String... args) {
59.574 + super(res, resource, args);
59.575 + }
59.576 +
59.577 + @Override
59.578 + protected String computePage(Request request) {
59.579 + return resource + request.getHttpHandlerPath();
59.580 + }
59.581 +
59.582 +
59.583 + }
59.584 +
59.585 + private class VM extends HttpHandler {
59.586 + @Override
59.587 + public void service(Request request, Response response) throws Exception {
59.588 + response.setCharacterEncoding("UTF-8");
59.589 + response.setContentType("text/javascript");
59.590 + StringBuilder sb = new StringBuilder();
59.591 + generateBck2BrwsrJS(sb, BaseHTTPLauncher.this.resources);
59.592 + response.getWriter().write(sb.toString());
59.593 + }
59.594 + }
59.595 +
59.596 + private static class Classes extends HttpHandler {
59.597 + private final Res loader;
59.598 +
59.599 + public Classes(Res loader) {
59.600 + this.loader = loader;
59.601 + }
59.602 +
59.603 + @Override
59.604 + public void service(Request request, Response response) throws Exception {
59.605 + String res = request.getHttpHandlerPath();
59.606 + if (res.startsWith("/")) {
59.607 + res = res.substring(1);
59.608 + }
59.609 + try (InputStream is = loader.get(res)) {
59.610 + response.setContentType("text/javascript");
59.611 + Writer w = response.getWriter();
59.612 + w.append("[");
59.613 + for (int i = 0;; i++) {
59.614 + int b = is.read();
59.615 + if (b == -1) {
59.616 + break;
59.617 + }
59.618 + if (i > 0) {
59.619 + w.append(", ");
59.620 + }
59.621 + if (i % 20 == 0) {
59.622 + w.write("\n");
59.623 + }
59.624 + if (b > 127) {
59.625 + b = b - 256;
59.626 + }
59.627 + w.append(Integer.toString(b));
59.628 + }
59.629 + w.append("\n]");
59.630 + } catch (IOException ex) {
59.631 + response.setStatus(HttpStatus.NOT_FOUND_404);
59.632 + response.setError();
59.633 + response.setDetailMessage(ex.getMessage());
59.634 + }
59.635 + }
59.636 + }
59.637 +}
60.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
60.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/FXBrwsrLauncher.java Fri May 17 10:27:41 2013 +0200
60.3 @@ -0,0 +1,161 @@
60.4 +/**
60.5 + * Back 2 Browser Bytecode Translator
60.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
60.7 + *
60.8 + * This program is free software: you can redistribute it and/or modify
60.9 + * it under the terms of the GNU General Public License as published by
60.10 + * the Free Software Foundation, version 2 of the License.
60.11 + *
60.12 + * This program is distributed in the hope that it will be useful,
60.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
60.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60.15 + * GNU General Public License for more details.
60.16 + *
60.17 + * You should have received a copy of the GNU General Public License
60.18 + * along with this program. Look for COPYING file in the top folder.
60.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
60.20 + */
60.21 +package org.apidesign.bck2brwsr.launcher;
60.22 +
60.23 +import org.apidesign.bck2brwsr.launcher.fximpl.FXBrwsr;
60.24 +import java.io.IOException;
60.25 +import java.io.InputStream;
60.26 +import java.lang.reflect.Method;
60.27 +import java.net.URI;
60.28 +import java.net.URL;
60.29 +import java.net.URLClassLoader;
60.30 +import java.util.Enumeration;
60.31 +import java.util.List;
60.32 +
60.33 +import java.util.concurrent.Executors;
60.34 +import java.util.jar.Manifest;
60.35 +import java.util.logging.Level;
60.36 +import java.util.logging.Logger;
60.37 +import javafx.application.Platform;
60.38 +import org.apidesign.bck2brwsr.launcher.fximpl.JVMBridge;
60.39 +
60.40 +/**
60.41 + *
60.42 + * @author Jaroslav Tulach <jtulach@netbeans.org>
60.43 + */
60.44 +final class FXBrwsrLauncher extends BaseHTTPLauncher {
60.45 + private static final Logger LOG = Logger.getLogger(FXBrwsrLauncher.class.getName());
60.46 + static {
60.47 + try {
60.48 + Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
60.49 + m.setAccessible(true);
60.50 + URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
60.51 + LOG.log(Level.INFO, "url : {0}", l);
60.52 + m.invoke(ClassLoader.getSystemClassLoader(), l);
60.53 + } catch (Exception ex) {
60.54 + throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
60.55 + }
60.56 + }
60.57 +
60.58 + public FXBrwsrLauncher(String ignore) {
60.59 + super(null);
60.60 + }
60.61 +
60.62 + @Override
60.63 + protected Object[] showBrwsr(final URI url) throws IOException {
60.64 + try {
60.65 + LOG.log(Level.INFO, "showBrwsr for {0}", url);
60.66 + JVMBridge.registerClassLoaders(loaders());
60.67 + LOG.info("About to launch WebView");
60.68 + Executors.newSingleThreadExecutor().submit(new Runnable() {
60.69 + @Override
60.70 + public void run() {
60.71 + LOG.log(Level.INFO, "In FX thread. Launching!");
60.72 + try {
60.73 + FXBrwsr.launch(FXBrwsr.class, url.toString());
60.74 + LOG.log(Level.INFO, "Launcher is back. Closing");
60.75 + close();
60.76 + System.exit(0);
60.77 + } catch (Throwable ex) {
60.78 + LOG.log(Level.WARNING, "Error launching Web View", ex);
60.79 + }
60.80 + }
60.81 + });
60.82 + } catch (Throwable ex) {
60.83 + LOG.log(Level.WARNING, "Can't open WebView", ex);
60.84 + }
60.85 + return null;
60.86 + }
60.87 +
60.88 + @Override
60.89 + void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException {
60.90 + sb.append("(function() {\n"
60.91 + + " var impl = this.bck2brwsr;\n"
60.92 + + " this.bck2brwsr = function() { return impl; };\n");
60.93 + if (isDebugged()) {
60.94 + sb.append("var scr = window.document.createElement('script');\n");
60.95 + sb.append("scr.type = 'text/javascript';\n");
60.96 + sb.append("scr.src = 'https://getfirebug.com/firebug-lite.js';\n");
60.97 + sb.append("scr.text = '{ startOpened: true }';\n");
60.98 + sb.append("var head = window.document.getElementsByTagName('head')[0];");
60.99 + sb.append("head.appendChild(scr);\n");
60.100 + sb.append("var html = window.document.getElementsByTagName('html')[0];");
60.101 + sb.append("html.debug = true;\n");
60.102 + }
60.103 +
60.104 + sb.append("})(window);\n");
60.105 + JVMBridge.onBck2BrwsrLoad();
60.106 + }
60.107 +
60.108 + @Override
60.109 + public void close() throws IOException {
60.110 + super.close();
60.111 + Platform.exit();
60.112 + }
60.113 +
60.114 + String harnessResource() {
60.115 + return "org/apidesign/bck2brwsr/launcher/fximpl/harness.xhtml";
60.116 + }
60.117 +
60.118 + public static void main(String... args) throws IOException {
60.119 + String startPage = null;
60.120 +
60.121 + final ClassLoader cl = FXBrwsrLauncher.class.getClassLoader();
60.122 + startPage = findStartPage(cl, startPage);
60.123 + if (startPage == null) {
60.124 + throw new NullPointerException("Can't find StartPage tag in manifests!");
60.125 + }
60.126 +
60.127 + Launcher.showURL("fxbrwsr", cl, startPage);
60.128 + }
60.129 +
60.130 + private static String findStartPage(final ClassLoader cl, String startPage) throws IOException {
60.131 + Enumeration<URL> en = cl.getResources("META-INF/MANIFEST.MF");
60.132 + while (en.hasMoreElements()) {
60.133 + URL url = en.nextElement();
60.134 + Manifest mf;
60.135 + try (InputStream is = url.openStream()) {
60.136 + mf = new Manifest(is);
60.137 + }
60.138 + String sp = mf.getMainAttributes().getValue("StartPage");
60.139 + if (sp != null) {
60.140 + startPage = sp;
60.141 + break;
60.142 + }
60.143 + }
60.144 + return startPage;
60.145 + }
60.146 +
60.147 + private static boolean isDebugged() {
60.148 + try {
60.149 + return isDebuggedImpl();
60.150 + } catch (LinkageError e) {
60.151 + return false;
60.152 + }
60.153 + }
60.154 +
60.155 + private static boolean isDebuggedImpl() {
60.156 + java.lang.management.RuntimeMXBean runtime;
60.157 + runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
60.158 + List<String> args = runtime.getInputArguments();
60.159 + if (args.contains("-Xdebug")) { // NOI18N
60.160 + return true;
60.161 + }
60.162 + return false;
60.163 + }
60.164 +}
61.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
61.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fx/LauncherFX.java Fri May 17 10:27:41 2013 +0200
61.3 @@ -0,0 +1,32 @@
61.4 +/**
61.5 + * Back 2 Browser Bytecode Translator
61.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
61.7 + *
61.8 + * This program is free software: you can redistribute it and/or modify
61.9 + * it under the terms of the GNU General Public License as published by
61.10 + * the Free Software Foundation, version 2 of the License.
61.11 + *
61.12 + * This program is distributed in the hope that it will be useful,
61.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
61.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61.15 + * GNU General Public License for more details.
61.16 + *
61.17 + * You should have received a copy of the GNU General Public License
61.18 + * along with this program. Look for COPYING file in the top folder.
61.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
61.20 + */
61.21 +package org.apidesign.bck2brwsr.launcher.fx;
61.22 +
61.23 +import org.apidesign.bck2brwsr.launcher.Launcher;
61.24 +
61.25 +/** This is a launcher for the <a href="http://bck2brwsr.apidesign.org">Bck2Brwsr</a>
61.26 + * project that is using <b>JavaFX</b>'s WebView to display the browser inside
61.27 + * real Java virtual machine. Use {@link Launcher} methods to access this
61.28 + * functionality via public, supported methods.
61.29 + *
61.30 + * @author Jaroslav Tulach
61.31 + */
61.32 +public final class LauncherFX {
61.33 + private LauncherFX() {
61.34 + }
61.35 +}
62.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
62.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Console.java Fri May 17 10:27:41 2013 +0200
62.3 @@ -0,0 +1,395 @@
62.4 +/**
62.5 + * Back 2 Browser Bytecode Translator
62.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
62.7 + *
62.8 + * This program is free software: you can redistribute it and/or modify
62.9 + * it under the terms of the GNU General Public License as published by
62.10 + * the Free Software Foundation, version 2 of the License.
62.11 + *
62.12 + * This program is distributed in the hope that it will be useful,
62.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
62.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
62.15 + * GNU General Public License for more details.
62.16 + *
62.17 + * You should have received a copy of the GNU General Public License
62.18 + * along with this program. Look for COPYING file in the top folder.
62.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
62.20 + */
62.21 +package org.apidesign.bck2brwsr.launcher.fximpl;
62.22 +
62.23 +import java.io.IOException;
62.24 +import java.io.InputStream;
62.25 +import java.io.UnsupportedEncodingException;
62.26 +import java.lang.reflect.InvocationTargetException;
62.27 +import java.lang.reflect.Method;
62.28 +import java.lang.reflect.Modifier;
62.29 +import java.net.URL;
62.30 +import java.util.Enumeration;
62.31 +import javafx.scene.web.WebEngine;
62.32 +import netscape.javascript.JSObject;
62.33 +
62.34 +/**
62.35 + *
62.36 + * @author Jaroslav Tulach <jtulach@netbeans.org>
62.37 + */
62.38 +public final class Console {
62.39 + public Console() {
62.40 + }
62.41 +
62.42 + private static Object getAttr(Object elem, String attr) {
62.43 + return InvokeJS.CObject.call("getAttr", elem, attr);
62.44 + }
62.45 +
62.46 + private static void setAttr(String id, String attr, Object value) {
62.47 + InvokeJS.CObject.call("setAttrId", id, attr, value);
62.48 + }
62.49 + private static void setAttr(Object id, String attr, Object value) {
62.50 + InvokeJS.CObject.call("setAttr", id, attr, value);
62.51 + }
62.52 +
62.53 + private static void closeWindow() {}
62.54 +
62.55 + private static Object textArea;
62.56 + private static Object statusArea;
62.57 +
62.58 + private static void log(String newText) {
62.59 + if (textArea == null) {
62.60 + return;
62.61 + }
62.62 + String attr = "value";
62.63 + setAttr(textArea, attr, getAttr(textArea, attr) + "\n" + newText);
62.64 + setAttr(textArea, "scrollTop", getAttr(textArea, "scrollHeight"));
62.65 + }
62.66 +
62.67 + private static void beginTest(Case c) {
62.68 + Object[] arr = new Object[2];
62.69 + beginTest(c.getClassName() + "." + c.getMethodName(), c, arr);
62.70 + textArea = arr[0];
62.71 + statusArea = arr[1];
62.72 + }
62.73 +
62.74 + private static void finishTest(Case c, Object res) {
62.75 + if ("null".equals(res)) {
62.76 + setAttr(statusArea, "innerHTML", "Success");
62.77 + } else {
62.78 + setAttr(statusArea, "innerHTML", "Result " + res);
62.79 + }
62.80 + statusArea = null;
62.81 + textArea = null;
62.82 + }
62.83 +
62.84 + private static final String BEGIN_TEST =
62.85 + "var ul = window.document.getElementById('bck2brwsr.result');\n"
62.86 + + "var li = window.document.createElement('li');\n"
62.87 + + "var span = window.document.createElement('span');"
62.88 + + "span.innerHTML = test + ' - ';\n"
62.89 + + "var details = window.document.createElement('a');\n"
62.90 + + "details.innerHTML = 'Details';\n"
62.91 + + "details.href = '#';\n"
62.92 + + "var p = window.document.createElement('p');\n"
62.93 + + "var status = window.document.createElement('a');\n"
62.94 + + "status.innerHTML = 'running';"
62.95 + + "details.onclick = function() { li.appendChild(p); li.removeChild(details); status.innerHTML = 'Run Again'; status.href = '#'; };\n"
62.96 + + "status.onclick = function() { c.again(arr); }\n"
62.97 + + "var pre = window.document.createElement('textarea');\n"
62.98 + + "pre.cols = 100;"
62.99 + + "pre.rows = 10;"
62.100 + + "li.appendChild(span);\n"
62.101 + + "li.appendChild(status);\n"
62.102 + + "var span = window.document.createElement('span');"
62.103 + + "span.innerHTML = ' ';\n"
62.104 + + "li.appendChild(span);\n"
62.105 + + "li.appendChild(details);\n"
62.106 + + "p.appendChild(pre);\n"
62.107 + + "ul.appendChild(li);\n"
62.108 + + "arr[0] = pre;\n"
62.109 + + "arr[1] = status;\n";
62.110 +
62.111 + private static void beginTest(String test, Case c, Object[] arr) {
62.112 + InvokeJS.CObject.call("beginTest", test, c, arr);
62.113 + }
62.114 +
62.115 + private static final String LOAD_TEXT =
62.116 + "var request = new XMLHttpRequest();\n"
62.117 + + "request.open('GET', url, true);\n"
62.118 + + "request.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');\n"
62.119 + + "request.onreadystatechange = function() {\n"
62.120 + + " if (this.readyState!==4) return;\n"
62.121 + + " try {"
62.122 + + " arr[0] = this.responseText;\n"
62.123 + + " callback.run__V();\n"
62.124 + + " } catch (e) { alert(e); }"
62.125 + + "};"
62.126 + + "request.send();";
62.127 + private static void loadText(String url, Runnable callback, String[] arr) throws IOException {
62.128 + InvokeJS.CObject.call("loadText", url, new Run(callback), arr);
62.129 + }
62.130 +
62.131 + public static void runHarness(String url) throws IOException {
62.132 + new Console().harness(url);
62.133 + }
62.134 +
62.135 + public void harness(String url) throws IOException {
62.136 + log("Connecting to " + url);
62.137 + Request r = new Request(url);
62.138 + }
62.139 +
62.140 + private static class Request implements Runnable {
62.141 + private final String[] arr = { null };
62.142 + private final String url;
62.143 + private Case c;
62.144 + private int retries;
62.145 +
62.146 + private Request(String url) throws IOException {
62.147 + this.url = url;
62.148 + loadText(url, this, arr);
62.149 + }
62.150 + private Request(String url, String u) throws IOException {
62.151 + this.url = url;
62.152 + loadText(u, this, arr);
62.153 + }
62.154 +
62.155 + @Override
62.156 + public void run() {
62.157 + try {
62.158 + if (c == null) {
62.159 + String data = arr[0];
62.160 +
62.161 + if (data == null) {
62.162 + log("Some error exiting");
62.163 + closeWindow();
62.164 + return;
62.165 + }
62.166 +
62.167 + if (data.isEmpty()) {
62.168 + log("No data, exiting");
62.169 + closeWindow();
62.170 + return;
62.171 + }
62.172 +
62.173 + c = Case.parseData(data);
62.174 + beginTest(c);
62.175 + log("Got \"" + data + "\"");
62.176 + } else {
62.177 + log("Processing \"" + arr[0] + "\" for " + retries + " time");
62.178 + }
62.179 + Object result = retries++ >= 10 ? "java.lang.InterruptedException:timeout" : c.runTest();
62.180 + finishTest(c, result);
62.181 +
62.182 + String u = url + "?request=" + c.getRequestId() + "&result=" + result;
62.183 + new Request(url, u);
62.184 + } catch (Exception ex) {
62.185 + if (ex instanceof InterruptedException) {
62.186 + log("Re-scheduling in 100ms");
62.187 + schedule(this, 100);
62.188 + return;
62.189 + }
62.190 + log(ex.getClass().getName() + ":" + ex.getMessage());
62.191 + }
62.192 + }
62.193 + }
62.194 +
62.195 + private static String encodeURL(String r) throws UnsupportedEncodingException {
62.196 + final String SPECIAL = "%$&+,/:;=?@";
62.197 + StringBuilder sb = new StringBuilder();
62.198 + byte[] utf8 = r.getBytes("UTF-8");
62.199 + for (int i = 0; i < utf8.length; i++) {
62.200 + int ch = utf8[i] & 0xff;
62.201 + if (ch < 32 || ch > 127 || SPECIAL.indexOf(ch) >= 0) {
62.202 + final String numbers = "0" + Integer.toHexString(ch);
62.203 + sb.append("%").append(numbers.substring(numbers.length() - 2));
62.204 + } else {
62.205 + if (ch == 32) {
62.206 + sb.append("+");
62.207 + } else {
62.208 + sb.append((char)ch);
62.209 + }
62.210 + }
62.211 + }
62.212 + return sb.toString();
62.213 + }
62.214 +
62.215 + static String invoke(String clazz, String method) throws
62.216 + ClassNotFoundException, InvocationTargetException, IllegalAccessException,
62.217 + InstantiationException, InterruptedException {
62.218 + final Object r = new Case(null).invokeMethod(clazz, method);
62.219 + return r == null ? "null" : r.toString().toString();
62.220 + }
62.221 +
62.222 + /** Helper method that inspects the classpath and loads given resource
62.223 + * (usually a class file). Used while running tests in Rhino.
62.224 + *
62.225 + * @param name resource name to find
62.226 + * @return the array of bytes in the given resource
62.227 + * @throws IOException I/O in case something goes wrong
62.228 + */
62.229 + public static byte[] read(String name) throws IOException {
62.230 + URL u = null;
62.231 + Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
62.232 + while (en.hasMoreElements()) {
62.233 + u = en.nextElement();
62.234 + }
62.235 + if (u == null) {
62.236 + throw new IOException("Can't find " + name);
62.237 + }
62.238 + try (InputStream is = u.openStream()) {
62.239 + byte[] arr;
62.240 + arr = new byte[is.available()];
62.241 + int offset = 0;
62.242 + while (offset < arr.length) {
62.243 + int len = is.read(arr, offset, arr.length - offset);
62.244 + if (len == -1) {
62.245 + throw new IOException("Can't read " + name);
62.246 + }
62.247 + offset += len;
62.248 + }
62.249 + return arr;
62.250 + }
62.251 + }
62.252 +
62.253 + private static void turnAssetionStatusOn() {
62.254 + }
62.255 +
62.256 + private static Object schedule(Runnable r, int time) {
62.257 + return InvokeJS.CObject.call("schedule", new Run(r), time);
62.258 + }
62.259 +
62.260 + private static final class Case {
62.261 + private final Object data;
62.262 + private Object inst;
62.263 +
62.264 + private Case(Object data) {
62.265 + this.data = data;
62.266 + }
62.267 +
62.268 + public static Case parseData(String s) {
62.269 + return new Case(toJSON(s));
62.270 + }
62.271 +
62.272 + public String getMethodName() {
62.273 + return (String) value("methodName", data);
62.274 + }
62.275 +
62.276 + public String getClassName() {
62.277 + return (String) value("className", data);
62.278 + }
62.279 +
62.280 + public int getRequestId() {
62.281 + Object v = value("request", data);
62.282 + if (v instanceof Number) {
62.283 + return ((Number)v).intValue();
62.284 + }
62.285 + return Integer.parseInt(v.toString());
62.286 + }
62.287 +
62.288 + public String getHtmlFragment() {
62.289 + return (String) value("html", data);
62.290 + }
62.291 +
62.292 + void again(Object[] arr) {
62.293 + try {
62.294 + textArea = arr[0];
62.295 + statusArea = arr[1];
62.296 + setAttr(textArea, "value", "");
62.297 + runTest();
62.298 + } catch (Exception ex) {
62.299 + log(ex.getClass().getName() + ":" + ex.getMessage());
62.300 + }
62.301 + }
62.302 +
62.303 + private Object runTest() throws IllegalAccessException,
62.304 + IllegalArgumentException, ClassNotFoundException, UnsupportedEncodingException,
62.305 + InvocationTargetException, InstantiationException, InterruptedException {
62.306 + if (this.getHtmlFragment() != null) {
62.307 + setAttr("bck2brwsr.fragment", "innerHTML", this.getHtmlFragment());
62.308 + }
62.309 + log("Invoking " + this.getClassName() + '.' + this.getMethodName() + " as request: " + this.getRequestId());
62.310 + Object result = invokeMethod(this.getClassName(), this.getMethodName());
62.311 + setAttr("bck2brwsr.fragment", "innerHTML", "");
62.312 + log("Result: " + result);
62.313 + result = encodeURL("" + result);
62.314 + log("Sending back: ...?request=" + this.getRequestId() + "&result=" + result);
62.315 + return result;
62.316 + }
62.317 +
62.318 + private Object invokeMethod(String clazz, String method)
62.319 + throws ClassNotFoundException, InvocationTargetException,
62.320 + InterruptedException, IllegalAccessException, IllegalArgumentException,
62.321 + InstantiationException {
62.322 + Method found = null;
62.323 + Class<?> c = Class.forName(clazz);
62.324 + for (Method m : c.getMethods()) {
62.325 + if (m.getName().equals(method)) {
62.326 + found = m;
62.327 + }
62.328 + }
62.329 + Object res;
62.330 + if (found != null) {
62.331 + try {
62.332 + if ((found.getModifiers() & Modifier.STATIC) != 0) {
62.333 + res = found.invoke(null);
62.334 + } else {
62.335 + if (inst == null) {
62.336 + inst = c.newInstance();
62.337 + }
62.338 + res = found.invoke(inst);
62.339 + }
62.340 + } catch (Throwable ex) {
62.341 + if (ex instanceof InvocationTargetException) {
62.342 + ex = ((InvocationTargetException) ex).getTargetException();
62.343 + }
62.344 + if (ex instanceof InterruptedException) {
62.345 + throw (InterruptedException)ex;
62.346 + }
62.347 + res = ex.getClass().getName() + ":" + ex.getMessage();
62.348 + }
62.349 + } else {
62.350 + res = "Can't find method " + method + " in " + clazz;
62.351 + }
62.352 + return res;
62.353 + }
62.354 +
62.355 + private static Object toJSON(String s) {
62.356 + return InvokeJS.CObject.call("toJSON", s);
62.357 + }
62.358 +
62.359 + private static Object value(String p, Object d) {
62.360 + return ((JSObject)d).getMember(p);
62.361 + }
62.362 + }
62.363 +
62.364 + private static String safe(String txt) {
62.365 + return "try {" + txt + "} catch (err) { alert(err); }";
62.366 + }
62.367 +
62.368 + static {
62.369 + turnAssetionStatusOn();
62.370 + }
62.371 +
62.372 + private static final class InvokeJS {
62.373 + static final JSObject CObject = initJS();
62.374 +
62.375 + private static JSObject initJS() {
62.376 + WebEngine web = (WebEngine) System.getProperties().get("webEngine");
62.377 + return (JSObject) web.executeScript("(function() {"
62.378 + + "var CObject = {};"
62.379 +
62.380 + + "CObject.getAttr = function(elem, attr) { return elem[attr].toString(); };"
62.381 +
62.382 + + "CObject.setAttrId = function(id, attr, value) { window.document.getElementById(id)[attr] = value; };"
62.383 + + "CObject.setAttr = function(elem, attr, value) { elem[attr] = value; };"
62.384 +
62.385 + + "CObject.beginTest = function(test, c, arr) {" + safe(BEGIN_TEST) + "};"
62.386 +
62.387 + + "CObject.loadText = function(url, callback, arr) {" + safe(LOAD_TEXT.replace("run__V", "run")) + "};"
62.388 +
62.389 + + "CObject.schedule = function(r, time) { return window.setTimeout(function() { r.run(); }, time); };"
62.390 +
62.391 + + "CObject.toJSON = function(s) { return eval('(' + s + ')'); };"
62.392 +
62.393 + + "return CObject;"
62.394 + + "})(this)");
62.395 + }
62.396 + }
62.397 +
62.398 +}
63.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
63.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/FXBrwsr.java Fri May 17 10:27:41 2013 +0200
63.3 @@ -0,0 +1,184 @@
63.4 +/**
63.5 + * Back 2 Browser Bytecode Translator
63.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
63.7 + *
63.8 + * This program is free software: you can redistribute it and/or modify
63.9 + * it under the terms of the GNU General Public License as published by
63.10 + * the Free Software Foundation, version 2 of the License.
63.11 + *
63.12 + * This program is distributed in the hope that it will be useful,
63.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
63.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63.15 + * GNU General Public License for more details.
63.16 + *
63.17 + * You should have received a copy of the GNU General Public License
63.18 + * along with this program. Look for COPYING file in the top folder.
63.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
63.20 + */
63.21 +package org.apidesign.bck2brwsr.launcher.fximpl;
63.22 +
63.23 +import java.util.List;
63.24 +import java.util.TooManyListenersException;
63.25 +import java.util.logging.Level;
63.26 +import java.util.logging.Logger;
63.27 +import javafx.application.Application;
63.28 +import javafx.application.Platform;
63.29 +import javafx.beans.value.ChangeListener;
63.30 +import javafx.beans.value.ObservableValue;
63.31 +import javafx.event.ActionEvent;
63.32 +import javafx.event.EventHandler;
63.33 +import javafx.geometry.HPos;
63.34 +import javafx.geometry.Insets;
63.35 +import javafx.geometry.Pos;
63.36 +import javafx.geometry.VPos;
63.37 +import javafx.scene.Node;
63.38 +import javafx.scene.Scene;
63.39 +import javafx.scene.control.Button;
63.40 +import javafx.scene.layout.ColumnConstraints;
63.41 +import javafx.scene.layout.GridPane;
63.42 +import javafx.scene.layout.Pane;
63.43 +import javafx.scene.layout.Priority;
63.44 +import javafx.scene.layout.VBox;
63.45 +import javafx.scene.text.Text;
63.46 +import javafx.scene.web.WebEngine;
63.47 +import javafx.scene.web.WebEvent;
63.48 +import javafx.scene.web.WebView;
63.49 +import javafx.stage.Modality;
63.50 +import javafx.stage.Stage;
63.51 +import netscape.javascript.JSObject;
63.52 +
63.53 +/**
63.54 + * Demonstrates a WebView object accessing a web page.
63.55 + *
63.56 + * @see javafx.scene.web.WebView
63.57 + * @see javafx.scene.web.WebEngine
63.58 + */
63.59 +public class FXBrwsr extends Application {
63.60 + private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName());
63.61 +
63.62 + @Override
63.63 + public void start(Stage primaryStage) throws Exception {
63.64 + Pane root = new WebViewPane(getParameters().getUnnamed());
63.65 + primaryStage.setScene(new Scene(root, 1024, 768));
63.66 + LOG.info("Showing the stage");
63.67 + primaryStage.show();
63.68 + LOG.log(Level.INFO, "State shown: {0}", primaryStage.isShowing());
63.69 + }
63.70 +
63.71 + /**
63.72 + * Create a resizable WebView pane
63.73 + */
63.74 + private class WebViewPane extends Pane {
63.75 + private final JVMBridge bridge = new JVMBridge();
63.76 +
63.77 + public WebViewPane(List<String> params) {
63.78 + LOG.log(Level.INFO, "Initializing WebView with {0}", params);
63.79 + VBox.setVgrow(this, Priority.ALWAYS);
63.80 + setMaxWidth(Double.MAX_VALUE);
63.81 + setMaxHeight(Double.MAX_VALUE);
63.82 + WebView view = new WebView();
63.83 + view.setMinSize(500, 400);
63.84 + view.setPrefSize(500, 400);
63.85 + final WebEngine eng = view.getEngine();
63.86 + try {
63.87 + JVMBridge.addBck2BrwsrLoad(new InitBck2Brwsr(eng));
63.88 + } catch (TooManyListenersException ex) {
63.89 + LOG.log(Level.SEVERE, null, ex);
63.90 + }
63.91 +
63.92 + if (params.size() > 0) {
63.93 + LOG.log(Level.INFO, "loading page {0}", params.get(0));
63.94 + eng.load(params.get(0));
63.95 + LOG.fine("back from load");
63.96 + }
63.97 + eng.setOnAlert(new EventHandler<WebEvent<String>>() {
63.98 + @Override
63.99 + public void handle(WebEvent<String> t) {
63.100 + final Stage dialogStage = new Stage();
63.101 + dialogStage.initModality(Modality.WINDOW_MODAL);
63.102 + dialogStage.setTitle("Warning");
63.103 + final Button button = new Button("Close");
63.104 + final Text text = new Text(t.getData());
63.105 +
63.106 + VBox box = new VBox();
63.107 + box.setAlignment(Pos.CENTER);
63.108 + box.setSpacing(10);
63.109 + box.setPadding(new Insets(10));
63.110 + box.getChildren().addAll(text, button);
63.111 +
63.112 + dialogStage.setScene(new Scene(box));
63.113 +
63.114 + button.setCancelButton(true);
63.115 + button.setOnAction(new EventHandler<ActionEvent>() {
63.116 + @Override
63.117 + public void handle(ActionEvent t) {
63.118 + dialogStage.close();
63.119 + }
63.120 + });
63.121 +
63.122 + dialogStage.centerOnScreen();
63.123 + dialogStage.showAndWait();
63.124 + }
63.125 + });
63.126 + GridPane grid = new GridPane();
63.127 + grid.setVgap(5);
63.128 + grid.setHgap(5);
63.129 + GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
63.130 + grid.getColumnConstraints().addAll(new ColumnConstraints(100, 100, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true), new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true));
63.131 + grid.getChildren().addAll(view);
63.132 + getChildren().add(grid);
63.133 + }
63.134 +
63.135 + boolean initBck2Brwsr(WebEngine webEngine) {
63.136 + JSObject jsobj = (JSObject) webEngine.executeScript("window");
63.137 + LOG.log(Level.FINE, "window: {0}", jsobj);
63.138 + Object prev = jsobj.getMember("bck2brwsr");
63.139 + if ("undefined".equals(prev)) {
63.140 + System.getProperties().put("webEngine", webEngine);
63.141 + jsobj.setMember("bck2brwsr", bridge);
63.142 + return true;
63.143 + }
63.144 + return false;
63.145 + }
63.146 +
63.147 + @Override
63.148 + protected void layoutChildren() {
63.149 + List<Node> managed = getManagedChildren();
63.150 + double width = getWidth();
63.151 + double height = getHeight();
63.152 + double top = getInsets().getTop();
63.153 + double right = getInsets().getRight();
63.154 + double left = getInsets().getLeft();
63.155 + double bottom = getInsets().getBottom();
63.156 + for (int i = 0; i < managed.size(); i++) {
63.157 + Node child = managed.get(i);
63.158 + layoutInArea(child, left, top, width - left - right, height - top - bottom, 0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
63.159 + }
63.160 + }
63.161 +
63.162 + private class InitBck2Brwsr implements ChangeListener<Void>, Runnable {
63.163 + private final WebEngine eng;
63.164 +
63.165 + public InitBck2Brwsr(WebEngine eng) {
63.166 + this.eng = eng;
63.167 + }
63.168 +
63.169 + @Override
63.170 + public synchronized void changed(ObservableValue<? extends Void> ov, Void t, Void t1) {
63.171 + Platform.runLater(this);
63.172 + try {
63.173 + wait();
63.174 + } catch (InterruptedException ex) {
63.175 + LOG.log(Level.SEVERE, null, ex);
63.176 + }
63.177 + }
63.178 +
63.179 + @Override
63.180 + public synchronized void run() {
63.181 + initBck2Brwsr(eng);
63.182 + notifyAll();
63.183 + }
63.184 + }
63.185 + }
63.186 +
63.187 +}
64.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
64.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JVMBridge.java Fri May 17 10:27:41 2013 +0200
64.3 @@ -0,0 +1,64 @@
64.4 +/**
64.5 + * Back 2 Browser Bytecode Translator
64.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
64.7 + *
64.8 + * This program is free software: you can redistribute it and/or modify
64.9 + * it under the terms of the GNU General Public License as published by
64.10 + * the Free Software Foundation, version 2 of the License.
64.11 + *
64.12 + * This program is distributed in the hope that it will be useful,
64.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
64.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64.15 + * GNU General Public License for more details.
64.16 + *
64.17 + * You should have received a copy of the GNU General Public License
64.18 + * along with this program. Look for COPYING file in the top folder.
64.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
64.20 + */
64.21 +package org.apidesign.bck2brwsr.launcher.fximpl;
64.22 +
64.23 +import java.util.TooManyListenersException;
64.24 +import javafx.beans.value.ChangeListener;
64.25 +
64.26 +/**
64.27 + *
64.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
64.29 + */
64.30 +public final class JVMBridge {
64.31 + private static ClassLoader[] ldrs;
64.32 + private static ChangeListener<Void> onBck2BrwsrLoad;
64.33 +
64.34 + public static void registerClassLoaders(ClassLoader[] loaders) {
64.35 + ldrs = loaders.clone();
64.36 + }
64.37 +
64.38 + public static void addBck2BrwsrLoad(ChangeListener<Void> l) throws TooManyListenersException {
64.39 + if (onBck2BrwsrLoad != null) {
64.40 + throw new TooManyListenersException();
64.41 + }
64.42 + onBck2BrwsrLoad = l;
64.43 + }
64.44 +
64.45 + public static void onBck2BrwsrLoad() {
64.46 + ChangeListener<Void> l = onBck2BrwsrLoad;
64.47 + if (l != null) {
64.48 + l.changed(null, null, null);
64.49 + }
64.50 + }
64.51 +
64.52 + public Class<?> loadClass(String name) throws ClassNotFoundException {
64.53 + System.err.println("trying to load " + name);
64.54 + ClassNotFoundException ex = null;
64.55 + if (ldrs != null) for (ClassLoader l : ldrs) {
64.56 + try {
64.57 + return Class.forName(name, true, l);
64.58 + } catch (ClassNotFoundException ex2) {
64.59 + ex = ex2;
64.60 + }
64.61 + }
64.62 + if (ex == null) {
64.63 + ex = new ClassNotFoundException("No loaders");
64.64 + }
64.65 + throw ex;
64.66 + }
64.67 +}
65.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
65.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Run.java Fri May 17 10:27:41 2013 +0200
65.3 @@ -0,0 +1,35 @@
65.4 +/**
65.5 + * Back 2 Browser Bytecode Translator
65.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
65.7 + *
65.8 + * This program is free software: you can redistribute it and/or modify
65.9 + * it under the terms of the GNU General Public License as published by
65.10 + * the Free Software Foundation, version 2 of the License.
65.11 + *
65.12 + * This program is distributed in the hope that it will be useful,
65.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
65.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
65.15 + * GNU General Public License for more details.
65.16 + *
65.17 + * You should have received a copy of the GNU General Public License
65.18 + * along with this program. Look for COPYING file in the top folder.
65.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
65.20 + */
65.21 +
65.22 +package org.apidesign.bck2brwsr.launcher.fximpl;
65.23 +
65.24 +/**
65.25 + *
65.26 + * @author Jaroslav Tulach <jtulach@netbeans.org>
65.27 + */
65.28 +public final class Run implements Runnable {
65.29 + private final Runnable r;
65.30 + Run(Runnable r) {
65.31 + this.r = r;
65.32 + }
65.33 +
65.34 + @Override
65.35 + public void run() {
65.36 + r.run();
65.37 + }
65.38 +}
66.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
66.2 +++ b/launcher/fx/src/main/resources/org/apidesign/bck2brwsr/launcher/fximpl/harness.xhtml Fri May 17 10:27:41 2013 +0200
66.3 @@ -0,0 +1,52 @@
66.4 +<?xml version="1.0" encoding="UTF-8"?>
66.5 +<!--
66.6 +
66.7 + Back 2 Browser Bytecode Translator
66.8 + Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
66.9 +
66.10 + This program is free software: you can redistribute it and/or modify
66.11 + it under the terms of the GNU General Public License as published by
66.12 + the Free Software Foundation, version 2 of the License.
66.13 +
66.14 + This program is distributed in the hope that it will be useful,
66.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
66.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
66.17 + GNU General Public License for more details.
66.18 +
66.19 + You should have received a copy of the GNU General Public License
66.20 + along with this program. Look for COPYING file in the top folder.
66.21 + If not, see http://opensource.org/licenses/GPL-2.0.
66.22 +
66.23 +-->
66.24 +<!DOCTYPE html>
66.25 +<html xmlns="http://www.w3.org/1999/xhtml">
66.26 + <head>
66.27 + <title>Bck2Brwsr Harness</title>
66.28 + </head>
66.29 + <body>
66.30 + <script src="/bck2brwsr.js"></script>
66.31 + <script>
66.32 + var vm = bck2brwsr();
66.33 + </script>
66.34 +
66.35 + <h1>Bck2Brwsr Execution Harness</h1>
66.36 +
66.37 + <ul id="bck2brwsr.result" style="width: 100%;" >
66.38 + </ul>
66.39 +
66.40 + <div id="bck2brwsr.fragment"/>
66.41 +
66.42 + <script type="text/javascript">
66.43 + try {
66.44 + (function() {
66.45 + var cls = vm.loadClass('org.apidesign.bck2brwsr.launcher.fximpl.Console');
66.46 + // fxbrwsr mangling
66.47 + var inst = cls.newInstance();
66.48 + inst.harness('$U/../data');
66.49 + })();
66.50 + } catch (err) {
66.51 + alert('Error executing harness: ' + err);
66.52 + }
66.53 + </script>
66.54 + </body>
66.55 +</html>
67.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
67.2 +++ b/launcher/http/pom.xml Fri May 17 10:27:41 2013 +0200
67.3 @@ -0,0 +1,60 @@
67.4 +<?xml version="1.0"?>
67.5 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
67.6 + <modelVersion>4.0.0</modelVersion>
67.7 + <parent>
67.8 + <groupId>org.apidesign.bck2brwsr</groupId>
67.9 + <artifactId>launcher-pom</artifactId>
67.10 + <version>0.8-SNAPSHOT</version>
67.11 + </parent>
67.12 + <groupId>org.apidesign.bck2brwsr</groupId>
67.13 + <artifactId>launcher.http</artifactId>
67.14 + <version>0.8-SNAPSHOT</version>
67.15 + <name>Bck2Brwsr Launcher</name>
67.16 + <url>http://maven.apache.org</url>
67.17 + <build>
67.18 + <plugins>
67.19 + <plugin>
67.20 + <groupId>org.apache.maven.plugins</groupId>
67.21 + <artifactId>maven-compiler-plugin</artifactId>
67.22 + <version>2.3.2</version>
67.23 + <configuration>
67.24 + <source>1.7</source>
67.25 + <target>1.7</target>
67.26 + </configuration>
67.27 + </plugin>
67.28 + <plugin>
67.29 + <groupId>org.apache.maven.plugins</groupId>
67.30 + <artifactId>maven-javadoc-plugin</artifactId>
67.31 + <configuration>
67.32 + <subpackages>org.apidesign.bck2brwsr.launcher.b2b</subpackages>
67.33 + <skip>false</skip>
67.34 + </configuration>
67.35 + </plugin>
67.36 + </plugins>
67.37 + </build>
67.38 + <properties>
67.39 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
67.40 + </properties>
67.41 + <dependencies>
67.42 + <dependency>
67.43 + <groupId>${project.groupId}</groupId>
67.44 + <artifactId>launcher</artifactId>
67.45 + <version>${project.version}</version>
67.46 + </dependency>
67.47 + <dependency>
67.48 + <groupId>${project.groupId}</groupId>
67.49 + <artifactId>launcher.fx</artifactId>
67.50 + <version>${project.version}</version>
67.51 + </dependency>
67.52 + <dependency>
67.53 + <groupId>org.glassfish.grizzly</groupId>
67.54 + <artifactId>grizzly-http-server</artifactId>
67.55 + <version>${grizzly.version}</version>
67.56 + </dependency>
67.57 + <dependency>
67.58 + <groupId>${project.groupId}</groupId>
67.59 + <artifactId>vm4brwsr</artifactId>
67.60 + <version>${project.version}</version>
67.61 + </dependency>
67.62 + </dependencies>
67.63 +</project>
68.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
68.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Fri May 17 10:27:41 2013 +0200
68.3 @@ -0,0 +1,70 @@
68.4 +/**
68.5 + * Back 2 Browser Bytecode Translator
68.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
68.7 + *
68.8 + * This program is free software: you can redistribute it and/or modify
68.9 + * it under the terms of the GNU General Public License as published by
68.10 + * the Free Software Foundation, version 2 of the License.
68.11 + *
68.12 + * This program is distributed in the hope that it will be useful,
68.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
68.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
68.15 + * GNU General Public License for more details.
68.16 + *
68.17 + * You should have received a copy of the GNU General Public License
68.18 + * along with this program. Look for COPYING file in the top folder.
68.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
68.20 + */
68.21 +package org.apidesign.bck2brwsr.launcher;
68.22 +
68.23 +import java.io.IOException;
68.24 +import java.io.InputStream;
68.25 +import org.apidesign.vm4brwsr.Bck2Brwsr;
68.26 +
68.27 +/**
68.28 + * Lightweight server to launch Bck2Brwsr applications and tests.
68.29 + * Supports execution in native browser as well as Java's internal
68.30 + * execution engine.
68.31 + */
68.32 +final class Bck2BrwsrLauncher extends BaseHTTPLauncher {
68.33 +
68.34 + public Bck2BrwsrLauncher(String cmd) {
68.35 + super(cmd);
68.36 + }
68.37 +
68.38 + @Override
68.39 + String harnessResource() {
68.40 + return "org/apidesign/bck2brwsr/launcher/harness.xhtml";
68.41 + }
68.42 +
68.43 + @Override
68.44 + void generateBck2BrwsrJS(StringBuilder sb, final Res loader) throws IOException {
68.45 + class R implements Bck2Brwsr.Resources {
68.46 + @Override
68.47 + public InputStream get(String resource) throws IOException {
68.48 + return loader.get(resource);
68.49 + }
68.50 + }
68.51 +
68.52 + Bck2Brwsr.generate(sb, new R());
68.53 + sb.append(
68.54 + "(function WrapperVM(global) {"
68.55 + + " function ldCls(res) {\n"
68.56 + + " var request = new XMLHttpRequest();\n"
68.57 + + " request.open('GET', '/classes/' + res, false);\n"
68.58 + + " request.send();\n"
68.59 + + " if (request.status !== 200) return null;\n"
68.60 + + " var arr = eval('(' + request.responseText + ')');\n"
68.61 + + " return arr;\n"
68.62 + + " }\n"
68.63 + + " var prevvm = global.bck2brwsr;\n"
68.64 + + " global.bck2brwsr = function() {\n"
68.65 + + " var args = Array.prototype.slice.apply(arguments);\n"
68.66 + + " args.unshift(ldCls);\n"
68.67 + + " return prevvm.apply(null, args);\n"
68.68 + + " };\n"
68.69 + + "})(this);\n"
68.70 + );
68.71 + }
68.72 +
68.73 +}
69.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
69.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java Fri May 17 10:27:41 2013 +0200
69.3 @@ -0,0 +1,135 @@
69.4 +/**
69.5 + * Back 2 Browser Bytecode Translator
69.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
69.7 + *
69.8 + * This program is free software: you can redistribute it and/or modify
69.9 + * it under the terms of the GNU General Public License as published by
69.10 + * the Free Software Foundation, version 2 of the License.
69.11 + *
69.12 + * This program is distributed in the hope that it will be useful,
69.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
69.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
69.15 + * GNU General Public License for more details.
69.16 + *
69.17 + * You should have received a copy of the GNU General Public License
69.18 + * along with this program. Look for COPYING file in the top folder.
69.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
69.20 + */
69.21 +package org.apidesign.bck2brwsr.launcher;
69.22 +
69.23 +import org.apidesign.bck2brwsr.launcher.impl.Console;
69.24 +import java.io.IOException;
69.25 +import java.io.InputStream;
69.26 +import java.net.URL;
69.27 +import java.util.Enumeration;
69.28 +import java.util.LinkedHashSet;
69.29 +import java.util.Set;
69.30 +import java.util.logging.Level;
69.31 +import java.util.logging.Logger;
69.32 +import javax.script.Invocable;
69.33 +import javax.script.ScriptEngine;
69.34 +import javax.script.ScriptEngineManager;
69.35 +import javax.script.ScriptException;
69.36 +import org.apidesign.vm4brwsr.Bck2Brwsr;
69.37 +
69.38 +/**
69.39 + * Tests execution in Java's internal scripting engine.
69.40 + */
69.41 +final class JSLauncher extends Launcher {
69.42 + private static final Logger LOG = Logger.getLogger(JSLauncher.class.getName());
69.43 + private Set<ClassLoader> loaders = new LinkedHashSet<>();
69.44 + private final Res resources = new Res();
69.45 + private Invocable code;
69.46 + private StringBuilder codeSeq;
69.47 + private Object console;
69.48 +
69.49 + JSLauncher() {
69.50 + addClassLoader(Bck2Brwsr.class.getClassLoader());
69.51 + }
69.52 +
69.53 + @Override InvocationContext runMethod(InvocationContext mi) {
69.54 + loaders.add(mi.clazz.getClassLoader());
69.55 + try {
69.56 + long time = System.currentTimeMillis();
69.57 + LOG.log(Level.FINE, "Invoking {0}.{1}", new Object[]{mi.clazz.getName(), mi.methodName});
69.58 + String res = code.invokeMethod(
69.59 + console,
69.60 + "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
69.61 + mi.clazz.getName(), mi.methodName).toString();
69.62 + time = System.currentTimeMillis() - time;
69.63 + LOG.log(Level.FINE, "Resut of {0}.{1} = {2} in {3} ms", new Object[]{mi.clazz.getName(), mi.methodName, res, time});
69.64 + mi.result(res, null);
69.65 + } catch (ScriptException | NoSuchMethodException ex) {
69.66 + mi.result(null, ex);
69.67 + }
69.68 + return mi;
69.69 + }
69.70 +
69.71 + public void addClassLoader(ClassLoader url) {
69.72 + this.loaders.add(url);
69.73 + }
69.74 +
69.75 + @Override
69.76 + public void initialize() throws IOException {
69.77 + try {
69.78 + initRhino();
69.79 + } catch (Exception ex) {
69.80 + if (ex instanceof IOException) {
69.81 + throw (IOException)ex;
69.82 + }
69.83 + if (ex instanceof RuntimeException) {
69.84 + throw (RuntimeException)ex;
69.85 + }
69.86 + throw new IOException(ex);
69.87 + }
69.88 + }
69.89 +
69.90 + private void initRhino() throws IOException, ScriptException, NoSuchMethodException {
69.91 + StringBuilder sb = new StringBuilder();
69.92 + Bck2Brwsr.generate(sb, new Res());
69.93 +
69.94 + ScriptEngineManager sem = new ScriptEngineManager();
69.95 + ScriptEngine mach = sem.getEngineByExtension("js");
69.96 +
69.97 + sb.append(
69.98 + "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.impl.Console.read);"
69.99 + + "\nfunction initVM() { return vm; };"
69.100 + + "\n");
69.101 +
69.102 + Object res = mach.eval(sb.toString());
69.103 + if (!(mach instanceof Invocable)) {
69.104 + throw new IOException("It is invocable object: " + res);
69.105 + }
69.106 + code = (Invocable) mach;
69.107 + codeSeq = sb;
69.108 +
69.109 + Object vm = code.invokeFunction("initVM");
69.110 + console = code.invokeMethod(vm, "loadClass", Console.class.getName());
69.111 + }
69.112 +
69.113 + @Override
69.114 + public void shutdown() throws IOException {
69.115 + }
69.116 +
69.117 + @Override
69.118 + public String toString() {
69.119 + return codeSeq.toString();
69.120 + }
69.121 +
69.122 + private class Res implements Bck2Brwsr.Resources {
69.123 + @Override
69.124 + public InputStream get(String resource) throws IOException {
69.125 + for (ClassLoader l : loaders) {
69.126 + URL u = null;
69.127 + Enumeration<URL> en = l.getResources(resource);
69.128 + while (en.hasMoreElements()) {
69.129 + u = en.nextElement();
69.130 + }
69.131 + if (u != null) {
69.132 + return u.openStream();
69.133 + }
69.134 + }
69.135 + throw new IOException("Can't find " + resource);
69.136 + }
69.137 + }
69.138 +}
70.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
70.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/b2b/LauncherBck2Brwsr.java Fri May 17 10:27:41 2013 +0200
70.3 @@ -0,0 +1,33 @@
70.4 +/**
70.5 + * Back 2 Browser Bytecode Translator
70.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
70.7 + *
70.8 + * This program is free software: you can redistribute it and/or modify
70.9 + * it under the terms of the GNU General Public License as published by
70.10 + * the Free Software Foundation, version 2 of the License.
70.11 + *
70.12 + * This program is distributed in the hope that it will be useful,
70.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
70.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
70.15 + * GNU General Public License for more details.
70.16 + *
70.17 + * You should have received a copy of the GNU General Public License
70.18 + * along with this program. Look for COPYING file in the top folder.
70.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
70.20 + */
70.21 +package org.apidesign.bck2brwsr.launcher.b2b;
70.22 +
70.23 +import java.awt.Desktop;
70.24 +import org.apidesign.bck2brwsr.launcher.Launcher;
70.25 +
70.26 +/** This is a launcher for the <a href="http://bck2brwsr.apidesign.org">Bck2Brwsr</a>
70.27 + * project that is using {@link Desktop} (or {@link Process}) to display the
70.28 + * external browser in separate process. Use {@link Launcher} methods to access this
70.29 + * functionality via public, supported methods.
70.30 + *
70.31 + * @author Jaroslav Tulach
70.32 + */
70.33 +public final class LauncherBck2Brwsr {
70.34 + private LauncherBck2Brwsr() {
70.35 + }
70.36 +}
71.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
71.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Fri May 17 10:27:41 2013 +0200
71.3 @@ -0,0 +1,356 @@
71.4 +/**
71.5 + * Back 2 Browser Bytecode Translator
71.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
71.7 + *
71.8 + * This program is free software: you can redistribute it and/or modify
71.9 + * it under the terms of the GNU General Public License as published by
71.10 + * the Free Software Foundation, version 2 of the License.
71.11 + *
71.12 + * This program is distributed in the hope that it will be useful,
71.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
71.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
71.15 + * GNU General Public License for more details.
71.16 + *
71.17 + * You should have received a copy of the GNU General Public License
71.18 + * along with this program. Look for COPYING file in the top folder.
71.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
71.20 + */
71.21 +package org.apidesign.bck2brwsr.launcher.impl;
71.22 +
71.23 +import java.io.IOException;
71.24 +import java.io.InputStream;
71.25 +import java.io.UnsupportedEncodingException;
71.26 +import java.lang.reflect.InvocationTargetException;
71.27 +import java.lang.reflect.Method;
71.28 +import java.lang.reflect.Modifier;
71.29 +import java.net.URL;
71.30 +import java.util.Enumeration;
71.31 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
71.32 +
71.33 +/**
71.34 + *
71.35 + * @author Jaroslav Tulach <jtulach@netbeans.org>
71.36 + */
71.37 +public class Console {
71.38 + private Console() {
71.39 + }
71.40 + static {
71.41 + turnAssetionStatusOn();
71.42 + }
71.43 +
71.44 + @JavaScriptBody(args = {"id", "attr"}, body =
71.45 + "return window.document.getElementById(id)[attr].toString();")
71.46 + private static native Object getAttr(String id, String attr);
71.47 + @JavaScriptBody(args = {"elem", "attr"}, body =
71.48 + "return elem[attr].toString();")
71.49 + private static native Object getAttr(Object elem, String attr);
71.50 +
71.51 + @JavaScriptBody(args = {"id", "attr", "value"}, body =
71.52 + "window.document.getElementById(id)[attr] = value;")
71.53 + private static native void setAttr(String id, String attr, Object value);
71.54 + @JavaScriptBody(args = {"elem", "attr", "value"}, body =
71.55 + "elem[attr] = value;")
71.56 + private static native void setAttr(Object id, String attr, Object value);
71.57 +
71.58 + @JavaScriptBody(args = {}, body = "return; window.close();")
71.59 + private static native void closeWindow();
71.60 +
71.61 + private static Object textArea;
71.62 + private static Object statusArea;
71.63 +
71.64 + private static void log(String newText) {
71.65 + if (textArea == null) {
71.66 + return;
71.67 + }
71.68 + String attr = "value";
71.69 + setAttr(textArea, attr, getAttr(textArea, attr) + "\n" + newText);
71.70 + setAttr(textArea, "scrollTop", getAttr(textArea, "scrollHeight"));
71.71 + }
71.72 +
71.73 + private static void beginTest(Case c) {
71.74 + Object[] arr = new Object[2];
71.75 + beginTest(c.getClassName() + "." + c.getMethodName(), c, arr);
71.76 + textArea = arr[0];
71.77 + statusArea = arr[1];
71.78 + }
71.79 +
71.80 + private static void finishTest(Case c, Object res) {
71.81 + if ("null".equals(res)) {
71.82 + setAttr(statusArea, "innerHTML", "Success");
71.83 + } else {
71.84 + setAttr(statusArea, "innerHTML", "Result " + res);
71.85 + }
71.86 + statusArea = null;
71.87 + textArea = null;
71.88 + }
71.89 +
71.90 + @JavaScriptBody(args = { "test", "c", "arr" }, body =
71.91 + "var ul = window.document.getElementById('bck2brwsr.result');\n"
71.92 + + "var li = window.document.createElement('li');\n"
71.93 + + "var span = window.document.createElement('span');"
71.94 + + "span.innerHTML = test + ' - ';\n"
71.95 + + "var details = window.document.createElement('a');\n"
71.96 + + "details.innerHTML = 'Details';\n"
71.97 + + "details.href = '#';\n"
71.98 + + "var p = window.document.createElement('p');\n"
71.99 + + "var status = window.document.createElement('a');\n"
71.100 + + "status.innerHTML = 'running';"
71.101 + + "details.onclick = function() { li.appendChild(p); li.removeChild(details); status.innerHTML = 'Run Again'; status.href = '#'; };\n"
71.102 + + "status.onclick = function() { c.again__V_3Ljava_lang_Object_2(arr); }\n"
71.103 + + "var pre = window.document.createElement('textarea');\n"
71.104 + + "pre.cols = 100;"
71.105 + + "pre.rows = 10;"
71.106 + + "li.appendChild(span);\n"
71.107 + + "li.appendChild(status);\n"
71.108 + + "var span = window.document.createElement('span');"
71.109 + + "span.innerHTML = ' ';\n"
71.110 + + "li.appendChild(span);\n"
71.111 + + "li.appendChild(details);\n"
71.112 + + "p.appendChild(pre);\n"
71.113 + + "ul.appendChild(li);\n"
71.114 + + "arr[0] = pre;\n"
71.115 + + "arr[1] = status;\n"
71.116 + )
71.117 + private static native void beginTest(String test, Case c, Object[] arr);
71.118 +
71.119 + @JavaScriptBody(args = { "url", "callback", "arr" }, body = ""
71.120 + + "var request = new XMLHttpRequest();\n"
71.121 + + "request.open('GET', url, true);\n"
71.122 + + "request.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');\n"
71.123 + + "request.onreadystatechange = function() {\n"
71.124 + + " if (this.readyState!==4) return;\n"
71.125 + + " arr[0] = this.responseText;\n"
71.126 + + " callback.run__V();\n"
71.127 + + "};"
71.128 + + "request.send();"
71.129 + )
71.130 + private static native void loadText(String url, Runnable callback, String[] arr) throws IOException;
71.131 +
71.132 + public static void harness(String url) throws IOException {
71.133 + log("Connecting to " + url);
71.134 + Request r = new Request(url);
71.135 + }
71.136 +
71.137 + private static class Request implements Runnable {
71.138 + private final String[] arr = { null };
71.139 + private final String url;
71.140 + private Case c;
71.141 + private int retries;
71.142 +
71.143 + private Request(String url) throws IOException {
71.144 + this.url = url;
71.145 + loadText(url, this, arr);
71.146 + }
71.147 + private Request(String url, String u) throws IOException {
71.148 + this.url = url;
71.149 + loadText(u, this, arr);
71.150 + }
71.151 +
71.152 + @Override
71.153 + public void run() {
71.154 + try {
71.155 + if (c == null) {
71.156 + String data = arr[0];
71.157 +
71.158 + if (data == null) {
71.159 + log("Some error exiting");
71.160 + closeWindow();
71.161 + return;
71.162 + }
71.163 +
71.164 + if (data.isEmpty()) {
71.165 + log("No data, exiting");
71.166 + closeWindow();
71.167 + return;
71.168 + }
71.169 +
71.170 + c = Case.parseData(data);
71.171 + beginTest(c);
71.172 + log("Got \"" + data + "\"");
71.173 + } else {
71.174 + log("Processing \"" + arr[0] + "\" for " + retries + " time");
71.175 + }
71.176 + Object result = retries++ >= 10 ? "java.lang.InterruptedException:timeout" : c.runTest();
71.177 + finishTest(c, result);
71.178 +
71.179 + String u = url + "?request=" + c.getRequestId() + "&result=" + result;
71.180 + new Request(url, u);
71.181 + } catch (Exception ex) {
71.182 + if (ex instanceof InterruptedException) {
71.183 + log("Re-scheduling in 100ms");
71.184 + schedule(this, 100);
71.185 + return;
71.186 + }
71.187 + log(ex.getClass().getName() + ":" + ex.getMessage());
71.188 + }
71.189 + }
71.190 + }
71.191 +
71.192 + private static String encodeURL(String r) throws UnsupportedEncodingException {
71.193 + final String SPECIAL = "%$&+,/:;=?@";
71.194 + StringBuilder sb = new StringBuilder();
71.195 + byte[] utf8 = r.getBytes("UTF-8");
71.196 + for (int i = 0; i < utf8.length; i++) {
71.197 + int ch = utf8[i] & 0xff;
71.198 + if (ch < 32 || ch > 127 || SPECIAL.indexOf(ch) >= 0) {
71.199 + final String numbers = "0" + Integer.toHexString(ch);
71.200 + sb.append("%").append(numbers.substring(numbers.length() - 2));
71.201 + } else {
71.202 + if (ch == 32) {
71.203 + sb.append("+");
71.204 + } else {
71.205 + sb.append((char)ch);
71.206 + }
71.207 + }
71.208 + }
71.209 + return sb.toString();
71.210 + }
71.211 +
71.212 + static String invoke(String clazz, String method) throws
71.213 + ClassNotFoundException, InvocationTargetException, IllegalAccessException,
71.214 + InstantiationException, InterruptedException {
71.215 + final Object r = new Case(null).invokeMethod(clazz, method);
71.216 + return r == null ? "null" : r.toString().toString();
71.217 + }
71.218 +
71.219 + /** Helper method that inspects the classpath and loads given resource
71.220 + * (usually a class file). Used while running tests in Rhino.
71.221 + *
71.222 + * @param name resource name to find
71.223 + * @return the array of bytes in the given resource
71.224 + * @throws IOException I/O in case something goes wrong
71.225 + */
71.226 + public static byte[] read(String name) throws IOException {
71.227 + URL u = null;
71.228 + Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
71.229 + while (en.hasMoreElements()) {
71.230 + u = en.nextElement();
71.231 + }
71.232 + if (u == null) {
71.233 + throw new IOException("Can't find " + name);
71.234 + }
71.235 + try (InputStream is = u.openStream()) {
71.236 + byte[] arr;
71.237 + arr = new byte[is.available()];
71.238 + int offset = 0;
71.239 + while (offset < arr.length) {
71.240 + int len = is.read(arr, offset, arr.length - offset);
71.241 + if (len == -1) {
71.242 + throw new IOException("Can't read " + name);
71.243 + }
71.244 + offset += len;
71.245 + }
71.246 + return arr;
71.247 + }
71.248 + }
71.249 +
71.250 + @JavaScriptBody(args = {}, body = "vm.desiredAssertionStatus = true;")
71.251 + private static void turnAssetionStatusOn() {
71.252 + }
71.253 +
71.254 + @JavaScriptBody(args = {"r", "time"}, body =
71.255 + "return window.setTimeout(function() { r.run__V(); }, time);")
71.256 + private static native Object schedule(Runnable r, int time);
71.257 +
71.258 + private static final class Case {
71.259 + private final Object data;
71.260 + private Object inst;
71.261 +
71.262 + private Case(Object data) {
71.263 + this.data = data;
71.264 + }
71.265 +
71.266 + public static Case parseData(String s) {
71.267 + return new Case(toJSON(s));
71.268 + }
71.269 +
71.270 + public String getMethodName() {
71.271 + return value("methodName", data);
71.272 + }
71.273 +
71.274 + public String getClassName() {
71.275 + return value("className", data);
71.276 + }
71.277 +
71.278 + public String getRequestId() {
71.279 + return value("request", data);
71.280 + }
71.281 +
71.282 + public String getHtmlFragment() {
71.283 + return value("html", data);
71.284 + }
71.285 +
71.286 + void again(Object[] arr) {
71.287 + try {
71.288 + textArea = arr[0];
71.289 + statusArea = arr[1];
71.290 + setAttr(textArea, "value", "");
71.291 + runTest();
71.292 + } catch (Exception ex) {
71.293 + log(ex.getClass().getName() + ":" + ex.getMessage());
71.294 + }
71.295 + }
71.296 +
71.297 + private Object runTest() throws IllegalAccessException,
71.298 + IllegalArgumentException, ClassNotFoundException, UnsupportedEncodingException,
71.299 + InvocationTargetException, InstantiationException, InterruptedException {
71.300 + if (this.getHtmlFragment() != null) {
71.301 + setAttr("bck2brwsr.fragment", "innerHTML", this.getHtmlFragment());
71.302 + }
71.303 + log("Invoking " + this.getClassName() + '.' + this.getMethodName() + " as request: " + this.getRequestId());
71.304 + Object result = invokeMethod(this.getClassName(), this.getMethodName());
71.305 + setAttr("bck2brwsr.fragment", "innerHTML", "");
71.306 + log("Result: " + result);
71.307 + result = encodeURL("" + result);
71.308 + log("Sending back: ...?request=" + this.getRequestId() + "&result=" + result);
71.309 + return result;
71.310 + }
71.311 +
71.312 + private Object invokeMethod(String clazz, String method)
71.313 + throws ClassNotFoundException, InvocationTargetException,
71.314 + InterruptedException, IllegalAccessException, IllegalArgumentException,
71.315 + InstantiationException {
71.316 + Method found = null;
71.317 + Class<?> c = Class.forName(clazz);
71.318 + for (Method m : c.getMethods()) {
71.319 + if (m.getName().equals(method)) {
71.320 + found = m;
71.321 + }
71.322 + }
71.323 + Object res;
71.324 + if (found != null) {
71.325 + try {
71.326 + if ((found.getModifiers() & Modifier.STATIC) != 0) {
71.327 + res = found.invoke(null);
71.328 + } else {
71.329 + if (inst == null) {
71.330 + inst = c.newInstance();
71.331 + }
71.332 + res = found.invoke(inst);
71.333 + }
71.334 + } catch (Throwable ex) {
71.335 + if (ex instanceof InvocationTargetException) {
71.336 + ex = ((InvocationTargetException) ex).getTargetException();
71.337 + }
71.338 + if (ex instanceof InterruptedException) {
71.339 + throw (InterruptedException)ex;
71.340 + }
71.341 + res = ex.getClass().getName() + ":" + ex.getMessage();
71.342 + }
71.343 + } else {
71.344 + res = "Can't find method " + method + " in " + clazz;
71.345 + }
71.346 + return res;
71.347 + }
71.348 +
71.349 + @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
71.350 + private static native Object toJSON(String s);
71.351 +
71.352 + @JavaScriptBody(args = {"p", "d"}, body =
71.353 + "var v = d[p];\n"
71.354 + + "if (typeof v === 'undefined') return null;\n"
71.355 + + "return v.toString();"
71.356 + )
71.357 + private static native String value(String p, Object d);
71.358 + }
71.359 +}
72.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
72.2 +++ b/launcher/http/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml Fri May 17 10:27:41 2013 +0200
72.3 @@ -0,0 +1,43 @@
72.4 +<?xml version="1.0" encoding="UTF-8"?>
72.5 +<!--
72.6 +
72.7 + Back 2 Browser Bytecode Translator
72.8 + Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
72.9 +
72.10 + This program is free software: you can redistribute it and/or modify
72.11 + it under the terms of the GNU General Public License as published by
72.12 + the Free Software Foundation, version 2 of the License.
72.13 +
72.14 + This program is distributed in the hope that it will be useful,
72.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
72.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
72.17 + GNU General Public License for more details.
72.18 +
72.19 + You should have received a copy of the GNU General Public License
72.20 + along with this program. Look for COPYING file in the top folder.
72.21 + If not, see http://opensource.org/licenses/GPL-2.0.
72.22 +
72.23 +-->
72.24 +<!DOCTYPE html>
72.25 +<html xmlns="http://www.w3.org/1999/xhtml">
72.26 + <head>
72.27 + <title>Bck2Brwsr Harness</title>
72.28 + </head>
72.29 + <body>
72.30 + <script src="/bck2brwsr.js"></script>
72.31 + <script>
72.32 + var vm = bck2brwsr();
72.33 + </script>
72.34 +
72.35 + <h1>Bck2Brwsr Execution Harness</h1>
72.36 +
72.37 + <ul id="bck2brwsr.result" style="width: 100%;" >
72.38 + </ul>
72.39 +
72.40 + <div id="bck2brwsr.fragment"/>
72.41 +
72.42 + <script type="text/javascript">
72.43 + vm.loadClass('org.apidesign.bck2brwsr.launcher.impl.Console').harness__VLjava_lang_String_2('$U/../data');
72.44 + </script>
72.45 + </body>
72.46 +</html>
73.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
73.2 +++ b/launcher/pom.xml Fri May 17 10:27:41 2013 +0200
73.3 @@ -0,0 +1,22 @@
73.4 +<?xml version="1.0" encoding="UTF-8"?>
73.5 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
73.6 + <modelVersion>4.0.0</modelVersion>
73.7 + <parent>
73.8 + <artifactId>bck2brwsr</artifactId>
73.9 + <groupId>org.apidesign</groupId>
73.10 + <version>0.8-SNAPSHOT</version>
73.11 + </parent>
73.12 + <groupId>org.apidesign.bck2brwsr</groupId>
73.13 + <artifactId>launcher-pom</artifactId>
73.14 + <version>0.8-SNAPSHOT</version>
73.15 + <packaging>pom</packaging>
73.16 + <name>Launchers</name>
73.17 + <properties>
73.18 + <grizzly.version>2.3.2</grizzly.version>
73.19 + </properties>
73.20 + <modules>
73.21 + <module>api</module>
73.22 + <module>http</module>
73.23 + <module>fx</module>
73.24 + </modules>
73.25 +</project>
73.26 \ No newline at end of file
74.1 --- a/pom.xml Tue Mar 05 07:57:16 2013 +0100
74.2 +++ b/pom.xml Fri May 17 10:27:41 2013 +0200
74.3 @@ -3,7 +3,7 @@
74.4 <modelVersion>4.0.0</modelVersion>
74.5 <groupId>org.apidesign</groupId>
74.6 <artifactId>bck2brwsr</artifactId>
74.7 - <version>0.4-SNAPSHOT</version>
74.8 + <version>0.8-SNAPSHOT</version>
74.9 <packaging>pom</packaging>
74.10 <name>Back 2 Browser</name>
74.11 <parent>
74.12 @@ -11,11 +11,17 @@
74.13 <artifactId>jvnet-parent</artifactId>
74.14 <version>3</version>
74.15 </parent>
74.16 + <properties>
74.17 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
74.18 + <netbeans.version>RELEASE73</netbeans.version>
74.19 + <license>COPYING</license>
74.20 + </properties>
74.21 <modules>
74.22 <module>dew</module>
74.23 <module>javaquery</module>
74.24 <module>benchmarks</module>
74.25 <module>ide</module>
74.26 + <module>launcher</module>
74.27 <module>rt</module>
74.28 </modules>
74.29 <licenses>
74.30 @@ -33,6 +39,7 @@
74.31 <connection>scm:hg:http://source.apidesign.org/hg/bck2brwsr</connection>
74.32 <developerConnection>scm:hg:https://source.apidesign.org/hg/bck2brwsr</developerConnection>
74.33 <url>http://source.apidesign.org/hg/bck2brwsr</url>
74.34 + <tag>HEAD</tag>
74.35 </scm>
74.36 <repositories>
74.37 <repository>
74.38 @@ -78,14 +85,24 @@
74.39 <exclude>*</exclude>
74.40 <exclude>.*/**</exclude>
74.41 <exclude>rt/emul/*/src/main/**</exclude>
74.42 - <exclude>rt/javap/**</exclude>
74.43 - <exclude>rt/mojo/src/main/resources/archetype-resources/**</exclude>
74.44 - <exclude>rt/vmtest/src/test/resources/**</exclude>
74.45 + <exclude>rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeParser.java</exclude>
74.46 + <exclude>rt/archetype/src/main/resources/archetype-resources/**</exclude>
74.47 + <exclude>rt/emul/compact/src/test/resources/**</exclude>
74.48 <exclude>dew/src/main/resources/org/apidesign/bck2brwsr/dew/**</exclude>
74.49 <exclude>javaquery/api/src/main/resources/org/apidesign/bck2brwsr/htmlpage/knockout*.js</exclude>
74.50 </excludes>
74.51 </configuration>
74.52 </plugin>
74.53 + <plugin>
74.54 + <artifactId>maven-release-plugin</artifactId>
74.55 + <version>2.4</version>
74.56 + <configuration>
74.57 + <mavenExecutorId>forked-path</mavenExecutorId>
74.58 + <useReleaseProfile>false</useReleaseProfile>
74.59 + <arguments>-Pjvnet-release -Pgpg</arguments>
74.60 + <tag>release-${releaseVersion}</tag>
74.61 + </configuration>
74.62 + </plugin>
74.63 </plugins>
74.64 <pluginManagement>
74.65 <plugins>
74.66 @@ -102,6 +119,15 @@
74.67 <skip>true</skip>
74.68 </configuration>
74.69 </plugin>
74.70 + <plugin>
74.71 + <groupId>org.apache.maven.plugins</groupId>
74.72 + <artifactId>maven-compiler-plugin</artifactId>
74.73 + <version>2.3.2</version>
74.74 + <configuration>
74.75 + <source>1.7</source>
74.76 + <target>1.7</target>
74.77 + </configuration>
74.78 + </plugin>
74.79 </plugins>
74.80 </pluginManagement>
74.81 </build>
74.82 @@ -122,19 +148,113 @@
74.83 <dependency>
74.84 <groupId>org.netbeans.api</groupId>
74.85 <artifactId>org-netbeans-modules-classfile</artifactId>
74.86 - <version>RELEASE72</version>
74.87 + <version>${netbeans.version}</version>
74.88 <type>jar</type>
74.89 </dependency>
74.90 <dependency>
74.91 <groupId>org.netbeans.api</groupId>
74.92 <artifactId>org-openide-util-lookup</artifactId>
74.93 - <version>RELEASE72</version>
74.94 + <version>${netbeans.version}</version>
74.95 <scope>compile</scope>
74.96 <type>jar</type>
74.97 </dependency>
74.98 + <dependency>
74.99 + <groupId>org.netbeans.api</groupId>
74.100 + <artifactId>org-netbeans-api-annotations-common</artifactId>
74.101 + <version>${netbeans.version}</version>
74.102 + </dependency>
74.103 + <dependency>
74.104 + <groupId>org.netbeans.api</groupId>
74.105 + <artifactId>org-netbeans-modules-java-source</artifactId>
74.106 + <version>${netbeans.version}</version>
74.107 + </dependency>
74.108 + <dependency>
74.109 + <groupId>org.netbeans.api</groupId>
74.110 + <artifactId>org-netbeans-libs-javacapi</artifactId>
74.111 + <version>${netbeans.version}</version>
74.112 + </dependency>
74.113 + <dependency>
74.114 + <groupId>org.netbeans.api</groupId>
74.115 + <artifactId>org-netbeans-spi-java-hints</artifactId>
74.116 + <version>${netbeans.version}</version>
74.117 + </dependency>
74.118 + <dependency>
74.119 + <groupId>org.netbeans.api</groupId>
74.120 + <artifactId>org-netbeans-modules-parsing-api</artifactId>
74.121 + <version>${netbeans.version}</version>
74.122 + </dependency>
74.123 + <dependency>
74.124 + <groupId>org.netbeans.api</groupId>
74.125 + <artifactId>org-netbeans-spi-editor-hints</artifactId>
74.126 + <version>${netbeans.version}</version>
74.127 + </dependency>
74.128 + <dependency>
74.129 + <groupId>org.netbeans.api</groupId>
74.130 + <artifactId>org-openide-util</artifactId>
74.131 + <version>${netbeans.version}</version>
74.132 + </dependency>
74.133 + <dependency>
74.134 + <groupId>org.netbeans.api</groupId>
74.135 + <artifactId>org-netbeans-modules-java-lexer</artifactId>
74.136 + <version>${netbeans.version}</version>
74.137 + </dependency>
74.138 + <dependency>
74.139 + <groupId>org.netbeans.api</groupId>
74.140 + <artifactId>org-netbeans-modules-lexer</artifactId>
74.141 + <version>${netbeans.version}</version>
74.142 + </dependency>
74.143 + <dependency>
74.144 + <groupId>org.netbeans.api</groupId>
74.145 + <artifactId>org-netbeans-modules-java-hints-test</artifactId>
74.146 + <version>${netbeans.version}</version>
74.147 + </dependency>
74.148 + <dependency>
74.149 + <groupId>org.netbeans.api</groupId>
74.150 + <artifactId>org-netbeans-libs-junit4</artifactId>
74.151 + <version>${netbeans.version}</version>
74.152 + </dependency>
74.153 + <dependency>
74.154 + <groupId>org.netbeans.modules</groupId>
74.155 + <artifactId>org-netbeans-lib-nbjavac</artifactId>
74.156 + <version>${netbeans.version}</version>
74.157 + </dependency>
74.158 + <dependency>
74.159 + <groupId>org.netbeans.modules</groupId>
74.160 + <artifactId>org-netbeans-modules-web-browser-api</artifactId>
74.161 + <version>${netbeans.version}</version>
74.162 + <exclusions>
74.163 + <exclusion>
74.164 + <artifactId>org-netbeans-core</artifactId>
74.165 + <groupId>org.netbeans.modules</groupId>
74.166 + </exclusion>
74.167 + <exclusion>
74.168 + <artifactId>org-netbeans-core-multiview</artifactId>
74.169 + <groupId>org.netbeans.api</groupId>
74.170 + </exclusion>
74.171 + <exclusion>
74.172 + <artifactId>org-netbeans-libs-lucene</artifactId>
74.173 + <groupId>org.netbeans.api</groupId>
74.174 + </exclusion>
74.175 + <exclusion>
74.176 + <artifactId>org-netbeans-modules-diff</artifactId>
74.177 + <groupId>org.netbeans.api</groupId>
74.178 + </exclusion>
74.179 + <exclusion>
74.180 + <artifactId>org-netbeans-modules-editor-fold</artifactId>
74.181 + <groupId>org.netbeans.api</groupId>
74.182 + </exclusion>
74.183 + <exclusion>
74.184 + <artifactId>org-netbeans-modules-editor-guards</artifactId>
74.185 + <groupId>org.netbeans.api</groupId>
74.186 + </exclusion>
74.187 + </exclusions>
74.188 + </dependency>
74.189 + <dependency>
74.190 + <artifactId>org-netbeans-modules-projectapi</artifactId>
74.191 + <groupId>org.netbeans.api</groupId>
74.192 + <type>jar</type>
74.193 + <version>${netbeans.version}</version>
74.194 + </dependency>
74.195 </dependencies>
74.196 </dependencyManagement>
74.197 - <properties>
74.198 - <license>COPYING</license>
74.199 - </properties>
74.200 -</project>
74.201 \ No newline at end of file
74.202 +</project>
75.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
75.2 +++ b/rt/archetype/pom.xml Fri May 17 10:27:41 2013 +0200
75.3 @@ -0,0 +1,87 @@
75.4 +<?xml version="1.0" encoding="UTF-8"?>
75.5 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
75.6 + <modelVersion>4.0.0</modelVersion>
75.7 + <parent>
75.8 + <artifactId>rt</artifactId>
75.9 + <groupId>org.apidesign.bck2brwsr</groupId>
75.10 + <version>0.8-SNAPSHOT</version>
75.11 + </parent>
75.12 + <groupId>org.apidesign.bck2brwsr</groupId>
75.13 + <artifactId>bck2brwsr-archetype-html-sample</artifactId>
75.14 + <version>0.8-SNAPSHOT</version>
75.15 + <packaging>jar</packaging>
75.16 + <name>Bck2Brwsr Maven Archetype</name>
75.17 + <description>
75.18 + Creates a skeletal HTML page and associated Java controller class.
75.19 + Runs in any browser (even without Java plugin) with the help of Bck2Brwsr
75.20 + virtual machine.
75.21 + </description>
75.22 + <build>
75.23 + <resources>
75.24 + <resource>
75.25 + <directory>src/main/resources</directory>
75.26 + <filtering>true</filtering>
75.27 + <includes>
75.28 + <include>**/pom.xml</include>
75.29 + </includes>
75.30 + </resource>
75.31 + <resource>
75.32 + <directory>src/main/resources</directory>
75.33 + <filtering>false</filtering>
75.34 + <excludes>
75.35 + <exclude>**/pom.xml</exclude>
75.36 + </excludes>
75.37 + </resource>
75.38 + </resources>
75.39 + <plugins>
75.40 + <plugin>
75.41 + <groupId>org.apache.maven.plugins</groupId>
75.42 + <artifactId>maven-compiler-plugin</artifactId>
75.43 + <version>2.3.2</version>
75.44 + <configuration>
75.45 + <source>1.6</source>
75.46 + <target>1.6</target>
75.47 + </configuration>
75.48 + </plugin>
75.49 + <plugin>
75.50 + <groupId>org.apache.maven.plugins</groupId>
75.51 + <artifactId>maven-resources-plugin</artifactId>
75.52 + <version>2.6</version>
75.53 + <configuration>
75.54 + <escapeString>\</escapeString>
75.55 + <target>1.6</target>
75.56 + </configuration>
75.57 + </plugin>
75.58 + <plugin>
75.59 + <groupId>org.apache.maven.plugins</groupId>
75.60 + <artifactId>maven-surefire-plugin</artifactId>
75.61 + <configuration>
75.62 + <skipTests>true</skipTests>
75.63 + </configuration>
75.64 + <executions>
75.65 + <execution>
75.66 + <id>test</id>
75.67 + <goals>
75.68 + <goal>test</goal>
75.69 + </goals>
75.70 + <phase>integration-test</phase>
75.71 + <configuration>
75.72 + <additionalClasspathElements>
75.73 + <additionalClasspathElement>${project.build.directory}/bck2brwsr-archetype-html-sample-${project.version}.jar</additionalClasspathElement>
75.74 + </additionalClasspathElements>
75.75 + <skipTests>false</skipTests>
75.76 + </configuration>
75.77 + </execution>
75.78 +
75.79 + </executions>
75.80 + </plugin>
75.81 + </plugins>
75.82 + </build>
75.83 + <dependencies>
75.84 + <dependency>
75.85 + <groupId>org.testng</groupId>
75.86 + <artifactId>testng</artifactId>
75.87 + <scope>test</scope>
75.88 + </dependency>
75.89 + </dependencies>
75.90 +</project>
76.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
76.2 +++ b/rt/archetype/src/main/java/org/apidesign/bck2brwsr/archetype/package-info.java Fri May 17 10:27:41 2013 +0200
76.3 @@ -0,0 +1,18 @@
76.4 +/**
76.5 + * Back 2 Browser Bytecode Translator
76.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
76.7 + *
76.8 + * This program is free software: you can redistribute it and/or modify
76.9 + * it under the terms of the GNU General Public License as published by
76.10 + * the Free Software Foundation, version 2 of the License.
76.11 + *
76.12 + * This program is distributed in the hope that it will be useful,
76.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
76.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
76.15 + * GNU General Public License for more details.
76.16 + *
76.17 + * You should have received a copy of the GNU General Public License
76.18 + * along with this program. Look for COPYING file in the top folder.
76.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
76.20 + */
76.21 +package org.apidesign.bck2brwsr.archetype;
77.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
77.2 +++ b/rt/archetype/src/main/resources/META-INF/maven/archetype-metadata.xml Fri May 17 10:27:41 2013 +0200
77.3 @@ -0,0 +1,55 @@
77.4 +<?xml version="1.0" encoding="UTF-8"?>
77.5 +<!--
77.6 +
77.7 + Back 2 Browser Bytecode Translator
77.8 + Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
77.9 +
77.10 + This program is free software: you can redistribute it and/or modify
77.11 + it under the terms of the GNU General Public License as published by
77.12 + the Free Software Foundation, version 2 of the License.
77.13 +
77.14 + This program is distributed in the hope that it will be useful,
77.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
77.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
77.17 + GNU General Public License for more details.
77.18 +
77.19 + You should have received a copy of the GNU General Public License
77.20 + along with this program. Look for COPYING file in the top folder.
77.21 + If not, see http://opensource.org/licenses/GPL-2.0.
77.22 +
77.23 +-->
77.24 +<archetype-descriptor name="Get Java Bck2Brwsr!">
77.25 + <fileSets>
77.26 + <fileSet filtered="true" packaged="true">
77.27 + <directory>src/main/java</directory>
77.28 + <includes>
77.29 + <include>**/App.java</include>
77.30 + </includes>
77.31 + </fileSet>
77.32 + <fileSet filtered="true" packaged="true">
77.33 + <directory>src/main/resources</directory>
77.34 + <includes>
77.35 + <include>**/*.xhtml</include>
77.36 + <include>**/*.html</include>
77.37 + </includes>
77.38 + </fileSet>
77.39 + <fileSet filtered="true" packaged="true">
77.40 + <directory>src/test/java</directory>
77.41 + <includes>
77.42 + <include>**/*Test.java</include>
77.43 + </includes>
77.44 + </fileSet>
77.45 + <fileSet filtered="false" packaged="false">
77.46 + <directory></directory>
77.47 + <includes>
77.48 + <include>nbactions.xml</include>
77.49 + </includes>
77.50 + </fileSet>
77.51 + <fileSet filtered="true" packaged="false">
77.52 + <directory></directory>
77.53 + <includes>
77.54 + <include>bck2brwsr-assembly.xml</include>
77.55 + </includes>
77.56 + </fileSet>
77.57 + </fileSets>
77.58 +</archetype-descriptor>
77.59 \ No newline at end of file
78.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
78.2 +++ b/rt/archetype/src/main/resources/archetype-resources/bck2brwsr-assembly.xml Fri May 17 10:27:41 2013 +0200
78.3 @@ -0,0 +1,61 @@
78.4 +<?xml version="1.0"?>
78.5 +<!--
78.6 +
78.7 + Back 2 Browser Bytecode Translator
78.8 + Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
78.9 +
78.10 + This program is free software: you can redistribute it and/or modify
78.11 + it under the terms of the GNU General Public License as published by
78.12 + the Free Software Foundation, version 2 of the License.
78.13 +
78.14 + This program is distributed in the hope that it will be useful,
78.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
78.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
78.17 + GNU General Public License for more details.
78.18 +
78.19 + You should have received a copy of the GNU General Public License
78.20 + along with this program. Look for COPYING file in the top folder.
78.21 + If not, see http://opensource.org/licenses/GPL-2.0.
78.22 +
78.23 +-->
78.24 +<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
78.25 + xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
78.26 +
78.27 + <id>bck2brwsr</id>
78.28 + <formats>
78.29 + <format>zip</format>
78.30 + </formats>
78.31 + <baseDirectory>public_html</baseDirectory>
78.32 + <dependencySets>
78.33 + <dependencySet>
78.34 + <useProjectArtifact>false</useProjectArtifact>
78.35 + <scope>runtime</scope>
78.36 + <outputDirectory>lib</outputDirectory>
78.37 + <includes>
78.38 + <include>*:jar</include>
78.39 + <include>*:rt</include>
78.40 + </includes>
78.41 + </dependencySet>
78.42 + <dependencySet>
78.43 + <useProjectArtifact>false</useProjectArtifact>
78.44 + <scope>provided</scope>
78.45 + <includes>
78.46 + <include>*:js</include>
78.47 + </includes>
78.48 + <unpack>true</unpack>
78.49 + <outputDirectory>/</outputDirectory>
78.50 + </dependencySet>
78.51 + </dependencySets>
78.52 + <files>
78.53 + <file>
78.54 + <source>${project.build.directory}/${project.build.finalName}.jar</source>
78.55 + <outputDirectory>/</outputDirectory>
78.56 + </file>
78.57 + <file>
78.58 + <source>${project.build.directory}/classes/${package.replace('.','/')}/index.html</source>
78.59 + <outputDirectory>/</outputDirectory>
78.60 + <destName>index.html</destName>
78.61 + </file>
78.62 + </files>
78.63 +
78.64 +</assembly>
78.65 \ No newline at end of file
79.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
79.2 +++ b/rt/archetype/src/main/resources/archetype-resources/nbactions.xml Fri May 17 10:27:41 2013 +0200
79.3 @@ -0,0 +1,10 @@
79.4 +<?xml version="1.0" encoding="UTF-8"?>
79.5 +<actions>
79.6 + <action>
79.7 + <actionName>run</actionName>
79.8 + <goals>
79.9 + <goal>process-classes</goal>
79.10 + <goal>bck2brwsr:brwsr</goal>
79.11 + </goals>
79.12 + </action>
79.13 +</actions>
80.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
80.2 +++ b/rt/archetype/src/main/resources/archetype-resources/pom.xml Fri May 17 10:27:41 2013 +0200
80.3 @@ -0,0 +1,135 @@
80.4 +<?xml version="1.0"?>
80.5 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
80.6 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
80.7 + <modelVersion>4.0.0</modelVersion>
80.8 +
80.9 + <groupId>\${groupId}</groupId>
80.10 + <artifactId>\${artifactId}</artifactId>
80.11 + <version>\${version}</version>
80.12 + <packaging>jar</packaging>
80.13 +
80.14 + <name>\${artifactId}</name>
80.15 +
80.16 + <repositories>
80.17 + <repository>
80.18 + <id>java.net</id>
80.19 + <name>Java.net</name>
80.20 + <url>https://maven.java.net/content/repositories/releases/</url>
80.21 + <snapshots>
80.22 + <enabled>true</enabled>
80.23 + </snapshots>
80.24 + </repository>
80.25 + <repository>
80.26 + <id>netbeans</id>
80.27 + <name>NetBeans</name>
80.28 + <url>http://bits.netbeans.org/maven2/</url>
80.29 + </repository>
80.30 + </repositories>
80.31 + <pluginRepositories>
80.32 + <pluginRepository>
80.33 + <id>java.net</id>
80.34 + <name>Java.net</name>
80.35 + <url>https://maven.java.net/content/repositories/releases/</url>
80.36 + <snapshots>
80.37 + <enabled>true</enabled>
80.38 + </snapshots>
80.39 + </pluginRepository>
80.40 + </pluginRepositories>
80.41 +
80.42 + <properties>
80.43 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
80.44 + </properties>
80.45 + <build>
80.46 + <plugins>
80.47 + <plugin>
80.48 + <groupId>org.apidesign.bck2brwsr</groupId>
80.49 + <artifactId>bck2brwsr-maven-plugin</artifactId>
80.50 + <version>${project.version}</version>
80.51 + <executions>
80.52 + <execution>
80.53 + <goals>
80.54 + <goal>brwsr</goal>
80.55 + </goals>
80.56 + </execution>
80.57 + </executions>
80.58 + <configuration>
80.59 + <startpage>\${package.replace('.','/')}/index.html</startpage>
80.60 + </configuration>
80.61 + </plugin>
80.62 + <plugin>
80.63 + <groupId>org.apache.maven.plugins</groupId>
80.64 + <artifactId>maven-compiler-plugin</artifactId>
80.65 + <version>2.3.2</version>
80.66 + <configuration>
80.67 + <source>1.7</source>
80.68 + <target>1.7</target>
80.69 + </configuration>
80.70 + </plugin>
80.71 + <plugin>
80.72 + <groupId>org.apache.maven.plugins</groupId>
80.73 + <artifactId>maven-jar-plugin</artifactId>
80.74 + <version>2.4</version>
80.75 + <configuration>
80.76 + <archive>
80.77 + <manifest>
80.78 + <addClasspath>true</addClasspath>
80.79 + <classpathPrefix>lib/</classpathPrefix>
80.80 + </manifest>
80.81 + </archive>
80.82 + </configuration>
80.83 + </plugin>
80.84 + <plugin>
80.85 + <artifactId>maven-assembly-plugin</artifactId>
80.86 + <version>2.4</version>
80.87 + <executions>
80.88 + <execution>
80.89 + <id>distro-assembly</id>
80.90 + <phase>package</phase>
80.91 + <goals>
80.92 + <goal>single</goal>
80.93 + </goals>
80.94 + <configuration>
80.95 + <descriptors>
80.96 + <descriptor>bck2brwsr-assembly.xml</descriptor>
80.97 + </descriptors>
80.98 + </configuration>
80.99 + </execution>
80.100 + </executions>
80.101 + </plugin>
80.102 + </plugins>
80.103 + </build>
80.104 +
80.105 + <dependencies>
80.106 + <dependency>
80.107 + <groupId>org.apidesign.bck2brwsr</groupId>
80.108 + <artifactId>emul</artifactId>
80.109 + <version>${project.version}</version>
80.110 + <classifier>rt</classifier>
80.111 + </dependency>
80.112 + <dependency>
80.113 + <groupId>org.apidesign.bck2brwsr</groupId>
80.114 + <artifactId>javaquery.api</artifactId>
80.115 + <version>${project.version}</version>
80.116 + </dependency>
80.117 + <dependency>
80.118 + <groupId>org.testng</groupId>
80.119 + <artifactId>testng</artifactId>
80.120 + <version>6.5.2</version>
80.121 + <scope>test</scope>
80.122 + </dependency>
80.123 + <dependency>
80.124 + <groupId>org.apidesign.bck2brwsr</groupId>
80.125 + <artifactId>vm4brwsr</artifactId>
80.126 + <classifier>js</classifier>
80.127 + <type>zip</type>
80.128 + <version>${project.version}</version>
80.129 + <scope>provided</scope>
80.130 + </dependency>
80.131 + <dependency>
80.132 + <groupId>org.apidesign.bck2brwsr</groupId>
80.133 + <artifactId>vmtest</artifactId>
80.134 + <version>${project.version}</version>
80.135 + <scope>test</scope>
80.136 + </dependency>
80.137 + </dependencies>
80.138 +</project>
81.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
81.2 +++ b/rt/archetype/src/main/resources/archetype-resources/src/main/java/App.java Fri May 17 10:27:41 2013 +0200
81.3 @@ -0,0 +1,88 @@
81.4 +package ${package};
81.5 +
81.6 +import java.util.List;
81.7 +import org.apidesign.bck2brwsr.htmlpage.api.*;
81.8 +import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
81.9 +import org.apidesign.bck2brwsr.htmlpage.api.Page;
81.10 +import org.apidesign.bck2brwsr.htmlpage.api.Property;
81.11 +import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
81.12 +
81.13 +/** This is the controller class for associated index.html page. The <code>Index</code>
81.14 + * is autogenerated by parsing the index.html page. It fields represent individual
81.15 + * elements annotated by "id" in the page.
81.16 + */
81.17 +@Page(xhtml="index.html", className="Index", properties={
81.18 + @Property(name="name", type=String.class),
81.19 + @Property(name="messages", type=String.class, array=true),
81.20 +})
81.21 +public class App {
81.22 + static {
81.23 + Index model = new Index();
81.24 + model.setName("World");
81.25 + model.applyBindings();
81.26 + }
81.27 +
81.28 + /**
81.29 + * @param m the model of the index page creates in static initializer
81.30 + */
81.31 + @On(event = CLICK, id="hello")
81.32 + static void hello(Index m) {
81.33 + display(m.getHelloMessage(), m);
81.34 + m.getMessages().add(m.getHelloMessage());
81.35 + }
81.36 +
81.37 + /** Reacts when mouse moves over the canvas.
81.38 + *
81.39 + * @param m the model of the page
81.40 + * @param x property "x" extracted from the event generated by the browser
81.41 + * @param y property "y" from the mouse event
81.42 + */
81.43 + @On(event = MOUSE_MOVE, id="canvas")
81.44 + static void clearPoint(Index m, int x, int y) {
81.45 + GraphicsContext g = m.canvas.getContext();
81.46 + boolean even = (x + y) % 2 == 0;
81.47 + if (even) {
81.48 + g.setFillStyle("blue");
81.49 + } else {
81.50 + g.setFillStyle("red");
81.51 + }
81.52 + g.clearRect(0, 0, 1000, 1000);
81.53 + g.setFont("italic 40px Calibri");
81.54 + g.fillText(m.getHelloMessage(), 10, 40);
81.55 + }
81.56 +
81.57 + /** Callback function called by the KnockOut/Java binding on elements
81.58 + * representing href's with individual messages being their data.
81.59 + *
81.60 + * @param data the data associated with the element
81.61 + * @param m the model of the page
81.62 + */
81.63 + @OnFunction
81.64 + static void display(String data, Index m) {
81.65 + GraphicsContext g = m.canvas.getContext();
81.66 + g.clearRect(0, 0, 1000, 1000);
81.67 + g.setFillStyle("black");
81.68 + g.setFont("italic 40px Calibri");
81.69 + g.fillText(data, 10, 40);
81.70 + }
81.71 +
81.72 + /** Callback function.
81.73 + *
81.74 + * @param data data associated with the actual element on the page
81.75 + * @param m the model of the page
81.76 + */
81.77 + @OnFunction
81.78 + static void remove(String data, Index m) {
81.79 + m.getMessages().remove(data);
81.80 + }
81.81 +
81.82 + @ComputedProperty
81.83 + static String helloMessage(String name) {
81.84 + return "Hello " + name + "!";
81.85 + }
81.86 +
81.87 + @ComputedProperty
81.88 + static boolean noMessages(List<String> messages) {
81.89 + return messages.isEmpty();
81.90 + }
81.91 +}
82.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
82.2 +++ b/rt/archetype/src/main/resources/archetype-resources/src/main/resources/index.html Fri May 17 10:27:41 2013 +0200
82.3 @@ -0,0 +1,31 @@
82.4 +<?xml version="1.0" encoding="UTF-8"?>
82.5 +<!DOCTYPE html>
82.6 +<html xmlns="http://www.w3.org/1999/xhtml">
82.7 + <head>
82.8 + <title>Bck2Brwsr's Hello World</title>
82.9 + </head>
82.10 + <body>
82.11 + <h1 data-bind="text: helloMessage">Loading Bck2Brwsr's Hello World...</h1>
82.12 + Your name: <input id='input' data-bind="value: name, valueUpdate: 'afterkeydown'"></input>
82.13 + <button id="hello">Say Hello!</button>
82.14 + <p>
82.15 + <canvas id="canvas" width="300" height="50">
82.16 + </canvas>
82.17 + </p>
82.18 +
82.19 +
82.20 + <div data-bind="if: noMessages">No message displayed yet.</div>
82.21 + <ul data-bind="foreach: messages">
82.22 + <li>
82.23 + <a href="#" data-bind="text: $data, click: $root.display"></a>
82.24 + (<a href="#" data-bind="click: $root.remove">delete</a>)
82.25 + </li>
82.26 + </ul>
82.27 +
82.28 + <script src="bck2brwsr.js"></script>
82.29 + <script type="text/javascript">
82.30 + var vm = bck2brwsr('${artifactId}-${version}.jar');
82.31 + vm.loadClass('${package}.App');
82.32 + </script>
82.33 + </body>
82.34 +</html>
83.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
83.2 +++ b/rt/archetype/src/main/resources/archetype-resources/src/test/java/AppTest.java Fri May 17 10:27:41 2013 +0200
83.3 @@ -0,0 +1,26 @@
83.4 +package ${package};
83.5 +
83.6 +import static org.testng.Assert.*;
83.7 +import org.testng.annotations.BeforeMethod;
83.8 +import org.testng.annotations.Test;
83.9 +
83.10 +/** Demonstrating POJO testing of HTML page model. Runs in good old HotSpot
83.11 + * as it does not reference any HTML elements or browser functionality. Just
83.12 + * operates on the page model.
83.13 + *
83.14 + * @author Jaroslav Tulach <jtulach@netbeans.org>
83.15 + */
83.16 +public class AppTest {
83.17 + private Index model;
83.18 +
83.19 +
83.20 + @BeforeMethod
83.21 + public void initModel() {
83.22 + model = new Index().applyBindings();
83.23 + }
83.24 +
83.25 + @Test public void testHelloMessage() {
83.26 + model.setName("Joe");
83.27 + assertEquals(model.getHelloMessage(), "Hello Joe!", "Cleared after pressing +");
83.28 + }
83.29 +}
84.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
84.2 +++ b/rt/archetype/src/main/resources/archetype-resources/src/test/java/InconsistencyTest.java Fri May 17 10:27:41 2013 +0200
84.3 @@ -0,0 +1,40 @@
84.4 +package ${package};
84.5 +
84.6 +import org.apidesign.bck2brwsr.vmtest.Compare;
84.7 +import org.apidesign.bck2brwsr.vmtest.VMTest;
84.8 +import org.testng.annotations.Factory;
84.9 +
84.10 +/** Bck2brwsr cares about compatibility with real Java. Whatever API is
84.11 + * supported by bck2brwsr, it needs to behave the same way as when running
84.12 + * in HotSpot VM.
84.13 + * <p>
84.14 + * There can be bugs, however. To help us fix them, we kindly ask you to
84.15 + * write an "inconsistency" test. A test that compares behavior of the API
84.16 + * between real VM and bck2brwsr VM. This class is skeleton of such test.
84.17 + *
84.18 + * @author Jaroslav Tulach <jtulach@netbeans.org>
84.19 + */
84.20 +public class InconsistencyTest {
84.21 + /** A method to demonstrate inconsistency between bck2brwsr and HotSpot.
84.22 + * Make calls to an API that behaves strangely, return some result at
84.23 + * the end. No need to use any <code>assert</code>.
84.24 + *
84.25 + * @return value to compare between HotSpot and bck2brwsr
84.26 + */
84.27 + @Compare
84.28 + public int checkStringHashCode() throws Exception {
84.29 + return "Is string hashCode the same?".hashCode();
84.30 + }
84.31 +
84.32 + /** Factory method that creates a three tests for each method annotated with
84.33 + * {@link org.apidesign.bck2brwsr.vmtest.Compare}. One executes the code in
84.34 + * HotSpot, one in Rhino and the last one compares the results.
84.35 + *
84.36 + * @see org.apidesign.bck2brwsr.vmtest.VMTest
84.37 + */
84.38 + @Factory
84.39 + public static Object[] create() {
84.40 + return VMTest.create(InconsistencyTest.class);
84.41 + }
84.42 +
84.43 +}
85.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
85.2 +++ b/rt/archetype/src/main/resources/archetype-resources/src/test/java/IntegrationTest.java Fri May 17 10:27:41 2013 +0200
85.3 @@ -0,0 +1,46 @@
85.4 +package ${package};
85.5 +
85.6 +import org.apidesign.bck2brwsr.htmlpage.api.OnEvent;
85.7 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
85.8 +import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
85.9 +import org.apidesign.bck2brwsr.vmtest.VMTest;
85.10 +import org.testng.annotations.Factory;
85.11 +
85.12 +/** Sometimes it is useful to run tests inside of the real browser.
85.13 + * To do that just annotate your method with {@link org.apidesign.bck2brwsr.vmtest.BrwsrTest}
85.14 + * and that is it. If your code references elements on the HTML page,
85.15 + * you can pass in an {@link org.apidesign.bck2brwsr.vmtest.HtmlFragment} which
85.16 + * will be made available on the page before your test starts.
85.17 + *
85.18 + * @author Jaroslav Tulach <jtulach@netbeans.org>
85.19 + */
85.20 +public class IntegrationTest {
85.21 +
85.22 + /** Write to testing code here. Use <code>assert</code> (but not TestNG's
85.23 + * Assert, as TestNG is not compiled with target 1.6 yet).
85.24 + */
85.25 + @HtmlFragment(
85.26 + "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
85.27 + "Your name: <input id='input' data-bind=\"value: name, valueUpdate: 'afterkeydown'\"></input>\n" +
85.28 + "<button id=\"hello\">Say Hello!</button>\n" +
85.29 + "<p>\n" +
85.30 + " <canvas id=\"canvas\" width=\"300\" height=\"50\"></canvas>\n" +
85.31 + "</p>\n"
85.32 + )
85.33 + @BrwsrTest
85.34 + public void modifyValueAssertChangeInModel() {
85.35 + Index m = new Index();
85.36 + m.setName("Joe Hacker");
85.37 + m.applyBindings();
85.38 + assert "Joe Hacker".equals(m.input.getValue()) : "Value is really Joe Hacker: " + m.input.getValue();
85.39 + m.input.setValue("Happy Joe");
85.40 + m.triggerEvent(m.input, OnEvent.CHANGE);
85.41 + assert "Happy Joe".equals(m.getName()) : "Name property updated to Happy Joe: " + m.getName();
85.42 + }
85.43 +
85.44 + @Factory
85.45 + public static Object[] create() {
85.46 + return VMTest.create(IntegrationTest.class);
85.47 + }
85.48 +
85.49 +}
86.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
86.2 +++ b/rt/archetype/src/test/java/org/apidesign/bck2brwsr/archetype/ArchetypeVersionTest.java Fri May 17 10:27:41 2013 +0200
86.3 @@ -0,0 +1,101 @@
86.4 +/**
86.5 + * Back 2 Browser Bytecode Translator
86.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
86.7 + *
86.8 + * This program is free software: you can redistribute it and/or modify
86.9 + * it under the terms of the GNU General Public License as published by
86.10 + * the Free Software Foundation, version 2 of the License.
86.11 + *
86.12 + * This program is distributed in the hope that it will be useful,
86.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
86.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
86.15 + * GNU General Public License for more details.
86.16 + *
86.17 + * You should have received a copy of the GNU General Public License
86.18 + * along with this program. Look for COPYING file in the top folder.
86.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
86.20 + */
86.21 +package org.apidesign.bck2brwsr.archetype;
86.22 +
86.23 +import java.net.URL;
86.24 +import javax.xml.XMLConstants;
86.25 +import javax.xml.parsers.DocumentBuilderFactory;
86.26 +import javax.xml.xpath.XPathConstants;
86.27 +import javax.xml.xpath.XPathExpression;
86.28 +import javax.xml.xpath.XPathFactory;
86.29 +import org.testng.annotations.Test;
86.30 +import static org.testng.Assert.*;
86.31 +import org.testng.annotations.BeforeClass;
86.32 +import org.w3c.dom.Document;
86.33 +import org.w3c.dom.NodeList;
86.34 +
86.35 +/**
86.36 + *
86.37 + * @author Jaroslav Tulach <jtulach@netbeans.org>
86.38 + */
86.39 +public class ArchetypeVersionTest {
86.40 + private String version;
86.41 +
86.42 + public ArchetypeVersionTest() {
86.43 + }
86.44 +
86.45 + @BeforeClass public void readCurrentVersion() throws Exception {
86.46 + final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
86.47 + URL u = l.getResource("META-INF/maven/org.apidesign.bck2brwsr/bck2brwsr-archetype-html-sample/pom.xml");
86.48 + assertNotNull(u, "Own pom found: " + System.getProperty("java.class.path"));
86.49 +
86.50 + final XPathFactory fact = XPathFactory.newInstance();
86.51 + fact.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
86.52 +
86.53 + XPathExpression xp = fact.newXPath().compile("project/version/text()");
86.54 +
86.55 + Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(u.openStream());
86.56 + version = xp.evaluate(dom);
86.57 +
86.58 + assertFalse(version.isEmpty(), "There should be some version string");
86.59 + }
86.60 +
86.61 +
86.62 + @Test public void testComparePomDepsVersions() throws Exception {
86.63 + final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
86.64 + URL r = l.getResource("archetype-resources/pom.xml");
86.65 + assertNotNull(r, "Archetype pom found");
86.66 +
86.67 + final XPathFactory fact = XPathFactory.newInstance();
86.68 + XPathExpression xp2 = fact.newXPath().compile(
86.69 + "//version[../groupId/text() = 'org.apidesign.bck2brwsr']/text()"
86.70 + );
86.71 +
86.72 + Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
86.73 + NodeList arch = (NodeList) xp2.evaluate(dom, XPathConstants.NODESET);
86.74 +
86.75 + if (arch.getLength() < 3) {
86.76 + fail("There should be at least three dependencies to bck2brwsr APIs: " + arch.getLength());
86.77 + }
86.78 +
86.79 + for (int i = 0; i < arch.getLength(); i++) {
86.80 + assertEquals(arch.item(i).getTextContent(), version, i + "th dependency needs to be on latest version of bck2brwsr");
86.81 + }
86.82 + }
86.83 +
86.84 + @Test public void testNbActions() throws Exception {
86.85 + final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
86.86 + URL r = l.getResource("archetype-resources/nbactions.xml");
86.87 + assertNotNull(r, "Archetype nb file found");
86.88 +
86.89 + final XPathFactory fact = XPathFactory.newInstance();
86.90 + XPathExpression xp2 = fact.newXPath().compile(
86.91 + "//goal/text()"
86.92 + );
86.93 +
86.94 + Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
86.95 + NodeList goals = (NodeList) xp2.evaluate(dom, XPathConstants.NODESET);
86.96 +
86.97 + for (int i = 0; i < goals.getLength(); i++) {
86.98 + String s = goals.item(i).getTextContent();
86.99 + if (s.contains("bck2brwsr")) {
86.100 + assertFalse(s.matches(".*bck2brwsr.*[0-9].*"), "No numbers: " + s);
86.101 + }
86.102 + }
86.103 + }
86.104 +}
87.1 --- a/rt/core/pom.xml Tue Mar 05 07:57:16 2013 +0100
87.2 +++ b/rt/core/pom.xml Fri May 17 10:27:41 2013 +0200
87.3 @@ -4,11 +4,11 @@
87.4 <parent>
87.5 <groupId>org.apidesign.bck2brwsr</groupId>
87.6 <artifactId>rt</artifactId>
87.7 - <version>0.4-SNAPSHOT</version>
87.8 + <version>0.8-SNAPSHOT</version>
87.9 </parent>
87.10 <groupId>org.apidesign.bck2brwsr</groupId>
87.11 <artifactId>core</artifactId>
87.12 - <version>0.4-SNAPSHOT</version>
87.13 + <version>0.8-SNAPSHOT</version>
87.14 <name>Bck2Brwsr Native Annotations</name>
87.15 <url>http://maven.apache.org</url>
87.16 <build>
88.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
88.2 +++ b/rt/emul/brwsrtest/pom.xml Fri May 17 10:27:41 2013 +0200
88.3 @@ -0,0 +1,52 @@
88.4 +<?xml version="1.0"?>
88.5 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
88.6 + <modelVersion>4.0.0</modelVersion>
88.7 + <parent>
88.8 + <groupId>org.apidesign.bck2brwsr</groupId>
88.9 + <artifactId>emul.pom</artifactId>
88.10 + <version>0.8-SNAPSHOT</version>
88.11 + </parent>
88.12 + <groupId>org.apidesign.bck2brwsr</groupId>
88.13 + <artifactId>brwsrtest</artifactId>
88.14 + <version>0.8-SNAPSHOT</version>
88.15 + <name>Tests Inside Real Browser</name>
88.16 + <url>http://maven.apache.org</url>
88.17 + <properties>
88.18 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
88.19 + </properties>
88.20 + <build>
88.21 + <plugins>
88.22 + <plugin>
88.23 + <groupId>org.apache.maven.plugins</groupId>
88.24 + <artifactId>maven-deploy-plugin</artifactId>
88.25 + <version>2.7</version>
88.26 + <configuration>
88.27 + <skip>true</skip>
88.28 + </configuration>
88.29 + </plugin>
88.30 + <plugin>
88.31 + <groupId>org.apache.maven.plugins</groupId>
88.32 + <artifactId>maven-surefire-plugin</artifactId>
88.33 + <configuration>
88.34 + <systemPropertyVariables>
88.35 + <vmtest.js>brwsr</vmtest.js>
88.36 + </systemPropertyVariables>
88.37 + </configuration>
88.38 + </plugin>
88.39 + </plugins>
88.40 + </build>
88.41 + <dependencies>
88.42 + <dependency>
88.43 + <groupId>${project.groupId}</groupId>
88.44 + <artifactId>vmtest</artifactId>
88.45 + <version>${project.version}</version>
88.46 + <scope>test</scope>
88.47 + </dependency>
88.48 + <dependency>
88.49 + <groupId>${project.groupId}</groupId>
88.50 + <artifactId>launcher.http</artifactId>
88.51 + <version>${project.version}</version>
88.52 + <scope>provided</scope>
88.53 + </dependency>
88.54 + </dependencies>
88.55 +</project>
89.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
89.2 +++ b/rt/emul/brwsrtest/src/test/java/org/apidesign/bck2brwsr/brwsrtest/BooleanTest.java Fri May 17 10:27:41 2013 +0200
89.3 @@ -0,0 +1,49 @@
89.4 +/**
89.5 + * Back 2 Browser Bytecode Translator
89.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
89.7 + *
89.8 + * This program is free software: you can redistribute it and/or modify
89.9 + * it under the terms of the GNU General Public License as published by
89.10 + * the Free Software Foundation, version 2 of the License.
89.11 + *
89.12 + * This program is distributed in the hope that it will be useful,
89.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
89.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
89.15 + * GNU General Public License for more details.
89.16 + *
89.17 + * You should have received a copy of the GNU General Public License
89.18 + * along with this program. Look for COPYING file in the top folder.
89.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
89.20 + */
89.21 +package org.apidesign.bck2brwsr.brwsrtest;
89.22 +
89.23 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
89.24 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
89.25 +import org.apidesign.bck2brwsr.vmtest.Compare;
89.26 +import org.apidesign.bck2brwsr.vmtest.VMTest;
89.27 +import org.testng.annotations.Factory;
89.28 +
89.29 +/**
89.30 + *
89.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
89.32 + */
89.33 +public class BooleanTest {
89.34 + @JavaScriptBody(args = { "tr" }, body = "return tr ? true : false;")
89.35 + private static native Object trueFalse(boolean tr);
89.36 +
89.37 + @BrwsrTest public void isTrueInstanceOfBoolean() {
89.38 + Object t = trueFalse(true);
89.39 + assert t instanceof Boolean : "Should be boolean: " + t;
89.40 + assert ((boolean)t) : "and is true";
89.41 + }
89.42 +
89.43 + @BrwsrTest public void isFalseInstanceOfBoolean() {
89.44 + Object t = trueFalse(false);
89.45 + assert t instanceof Boolean : "Should be boolean: " + t;
89.46 + assert !((boolean)t) : "and is false: " + t;
89.47 + }
89.48 +
89.49 + @Factory public static Object[] create() {
89.50 + return VMTest.create(BooleanTest.class);
89.51 + }
89.52 +}
90.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
90.2 +++ b/rt/emul/brwsrtest/src/test/java/org/apidesign/bck2brwsr/brwsrtest/DoubleBitsTest.java Fri May 17 10:27:41 2013 +0200
90.3 @@ -0,0 +1,42 @@
90.4 +/**
90.5 + * Back 2 Browser Bytecode Translator
90.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
90.7 + *
90.8 + * This program is free software: you can redistribute it and/or modify
90.9 + * it under the terms of the GNU General Public License as published by
90.10 + * the Free Software Foundation, version 2 of the License.
90.11 + *
90.12 + * This program is distributed in the hope that it will be useful,
90.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
90.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
90.15 + * GNU General Public License for more details.
90.16 + *
90.17 + * You should have received a copy of the GNU General Public License
90.18 + * along with this program. Look for COPYING file in the top folder.
90.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
90.20 + */
90.21 +package org.apidesign.bck2brwsr.brwsrtest;
90.22 +
90.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
90.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
90.25 +import org.testng.annotations.Factory;
90.26 +
90.27 +/**
90.28 + *
90.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
90.30 + */
90.31 +public class DoubleBitsTest {
90.32 +
90.33 + @Compare public String doubleToBits() {
90.34 + long val = Double.doubleToLongBits(333.456);
90.35 + return Long.toString(val);
90.36 + }
90.37 +
90.38 + @Compare public int floatToBits() {
90.39 + return Float.floatToIntBits(333.456f);
90.40 + }
90.41 +
90.42 + @Factory public static Object[] create() {
90.43 + return VMTest.create(DoubleBitsTest.class);
90.44 + }
90.45 +}
91.1 --- a/rt/emul/compact/pom.xml Tue Mar 05 07:57:16 2013 +0100
91.2 +++ b/rt/emul/compact/pom.xml Fri May 17 10:27:41 2013 +0200
91.3 @@ -4,11 +4,11 @@
91.4 <parent>
91.5 <groupId>org.apidesign.bck2brwsr</groupId>
91.6 <artifactId>emul.pom</artifactId>
91.7 - <version>0.4-SNAPSHOT</version>
91.8 + <version>0.8-SNAPSHOT</version>
91.9 </parent>
91.10 <groupId>org.apidesign.bck2brwsr</groupId>
91.11 <artifactId>emul</artifactId>
91.12 - <version>0.4-SNAPSHOT</version>
91.13 + <version>0.8-SNAPSHOT</version>
91.14 <name>Bck2Brwsr API Profile</name>
91.15 <url>http://maven.apache.org</url>
91.16 <properties>
91.17 @@ -28,6 +28,18 @@
91.18 <scope>test</scope>
91.19 </dependency>
91.20 <dependency>
91.21 + <groupId>${project.groupId}</groupId>
91.22 + <artifactId>launcher.http</artifactId>
91.23 + <version>${project.version}</version>
91.24 + <scope>test</scope>
91.25 + <exclusions>
91.26 + <exclusion>
91.27 + <groupId>com.oracle</groupId>
91.28 + <artifactId>javafx</artifactId>
91.29 + </exclusion>
91.30 + </exclusions>
91.31 + </dependency>
91.32 + <dependency>
91.33 <groupId>org.netbeans.api</groupId>
91.34 <artifactId>org-openide-util-lookup</artifactId>
91.35 <scope>test</scope>
91.36 @@ -47,6 +59,15 @@
91.37 <target>1.7</target>
91.38 </configuration>
91.39 </plugin>
91.40 + <plugin>
91.41 + <groupId>org.apache.maven.plugins</groupId>
91.42 + <artifactId>maven-javadoc-plugin</artifactId>
91.43 + <configuration>
91.44 + <excludePackageNames>org.apidesign.bck2brwsr.emul.*</excludePackageNames>
91.45 + <skip>false</skip>
91.46 + <includeDependencySources>true</includeDependencySources>
91.47 + </configuration>
91.48 + </plugin>
91.49 <plugin>
91.50 <artifactId>maven-assembly-plugin</artifactId>
91.51 <version>2.4</version>
92.1 --- a/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ZipCompatibilityTest.java Tue Mar 05 07:57:16 2013 +0100
92.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ZipCompatibilityTest.java Fri May 17 10:27:41 2013 +0200
92.3 @@ -30,7 +30,7 @@
92.4 public class ZipCompatibilityTest {
92.5 @Compare
92.6 public String testDemoStaticCalculator() throws IOException {
92.7 - InputStream is = getClass().getResourceAsStream("demo.static.calculator-0.3-SNAPSHOT.jar");
92.8 + InputStream is = getClass().getResourceAsStream("demo.static.calculator-TEST.jar");
92.9 ZipArchive zip = ZipArchive.createZip(is);
92.10 final String ts = zip.toString();
92.11 return ts.substring(0, 4096) + ts.hashCode();
93.1 --- a/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ZipVsJzLibTest.java Tue Mar 05 07:57:16 2013 +0100
93.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ZipVsJzLibTest.java Fri May 17 10:27:41 2013 +0200
93.3 @@ -27,10 +27,10 @@
93.4 */
93.5 public class ZipVsJzLibTest {
93.6 @Test public void r() throws IOException {
93.7 - InputStream is = getClass().getResourceAsStream("demo.static.calculator-0.3-SNAPSHOT.jar");
93.8 + InputStream is = getClass().getResourceAsStream("demo.static.calculator-TEST.jar");
93.9 ZipArchive zip = ZipArchive.createZip(is);
93.10
93.11 - is = getClass().getResourceAsStream("demo.static.calculator-0.3-SNAPSHOT.jar");
93.12 + is = getClass().getResourceAsStream("demo.static.calculator-TEST.jar");
93.13 ZipArchive real = ZipArchive.createReal(is);
93.14
93.15 real.assertEquals(zip, "Are they the same?");
94.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
94.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/AssertionTest.java Fri May 17 10:27:41 2013 +0200
94.3 @@ -0,0 +1,43 @@
94.4 +/**
94.5 + * Back 2 Browser Bytecode Translator
94.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
94.7 + *
94.8 + * This program is free software: you can redistribute it and/or modify
94.9 + * it under the terms of the GNU General Public License as published by
94.10 + * the Free Software Foundation, version 2 of the License.
94.11 + *
94.12 + * This program is distributed in the hope that it will be useful,
94.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
94.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
94.15 + * GNU General Public License for more details.
94.16 + *
94.17 + * You should have received a copy of the GNU General Public License
94.18 + * along with this program. Look for COPYING file in the top folder.
94.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
94.20 + */
94.21 +package org.apidesign.bck2brwsr.tck;
94.22 +
94.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
94.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
94.25 +import org.testng.annotations.Factory;
94.26 +
94.27 +/**
94.28 + *
94.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
94.30 + */
94.31 +public class AssertionTest {
94.32 +
94.33 + @Compare public Object checkAssert() throws ClassNotFoundException {
94.34 + try {
94.35 + assert false : "Is assertion status on?";
94.36 + return null;
94.37 + } catch (AssertionError ex) {
94.38 + return ex.getClass().getName();
94.39 + }
94.40 + }
94.41 +
94.42 + @Factory
94.43 + public static Object[] create() {
94.44 + return VMTest.create(AssertionTest.class);
94.45 + }
94.46 +}
95.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
95.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/BrwsrCheckTest.java Fri May 17 10:27:41 2013 +0200
95.3 @@ -0,0 +1,57 @@
95.4 +/**
95.5 + * Back 2 Browser Bytecode Translator
95.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
95.7 + *
95.8 + * This program is free software: you can redistribute it and/or modify
95.9 + * it under the terms of the GNU General Public License as published by
95.10 + * the Free Software Foundation, version 2 of the License.
95.11 + *
95.12 + * This program is distributed in the hope that it will be useful,
95.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
95.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
95.15 + * GNU General Public License for more details.
95.16 + *
95.17 + * You should have received a copy of the GNU General Public License
95.18 + * along with this program. Look for COPYING file in the top folder.
95.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
95.20 + */
95.21 +package org.apidesign.bck2brwsr.tck;
95.22 +
95.23 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
95.24 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
95.25 +import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
95.26 +import org.apidesign.bck2brwsr.vmtest.VMTest;
95.27 +import org.testng.annotations.Factory;
95.28 +
95.29 +/**
95.30 + *
95.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
95.32 + */
95.33 +public class BrwsrCheckTest {
95.34 +
95.35 + @BrwsrTest public void assertWindowObjectIsDefined() {
95.36 + assert window() != null : "No window object found!";
95.37 + }
95.38 +
95.39 +
95.40 +
95.41 +
95.42 + @HtmlFragment("<h1 id='hello'>\n"
95.43 + + "Hello!\n"
95.44 + + "</h1>\n")
95.45 + @BrwsrTest public void accessProvidedFragment() {
95.46 + assert getElementById("hello") != null : "Element with 'hello' ID found";
95.47 + }
95.48 +
95.49 + @Factory
95.50 + public static Object[] create() {
95.51 + return VMTest.create(BrwsrCheckTest.class);
95.52 + }
95.53 +
95.54 +
95.55 + @JavaScriptBody(args = {}, body = "return window;")
95.56 + private static native Object window();
95.57 +
95.58 + @JavaScriptBody(args = { "id" }, body = "return window.document.getElementById(id);")
95.59 + private static native Object getElementById(String id);
95.60 +}
96.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
96.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ByteArithmeticTest.java Fri May 17 10:27:41 2013 +0200
96.3 @@ -0,0 +1,147 @@
96.4 +/**
96.5 + * Back 2 Browser Bytecode Translator
96.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
96.7 + *
96.8 + * This program is free software: you can redistribute it and/or modify
96.9 + * it under the terms of the GNU General Public License as published by
96.10 + * the Free Software Foundation, version 2 of the License.
96.11 + *
96.12 + * This program is distributed in the hope that it will be useful,
96.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
96.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
96.15 + * GNU General Public License for more details.
96.16 + *
96.17 + * You should have received a copy of the GNU General Public License
96.18 + * along with this program. Look for COPYING file in the top folder.
96.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
96.20 + */
96.21 +package org.apidesign.bck2brwsr.tck;
96.22 +
96.23 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
96.24 +import org.apidesign.bck2brwsr.vmtest.Compare;
96.25 +import org.apidesign.bck2brwsr.vmtest.VMTest;
96.26 +import org.testng.annotations.Factory;
96.27 +
96.28 +/**
96.29 + *
96.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
96.31 + */
96.32 +public class ByteArithmeticTest {
96.33 +
96.34 + private static byte add(byte x, byte y) {
96.35 + return (byte)(x + y);
96.36 + }
96.37 +
96.38 + private static byte sub(byte x, byte y) {
96.39 + return (byte)(x - y);
96.40 + }
96.41 +
96.42 + private static byte mul(byte x, byte y) {
96.43 + return (byte)(x * y);
96.44 + }
96.45 +
96.46 + private static byte div(byte x, byte y) {
96.47 + return (byte)(x / y);
96.48 + }
96.49 +
96.50 + private static byte mod(byte x, byte y) {
96.51 + return (byte)(x % y);
96.52 + }
96.53 +
96.54 + @Compare public byte conversion() {
96.55 + return (byte)123456;
96.56 + }
96.57 +
96.58 + @Compare public byte addOverflow() {
96.59 + return add(Byte.MAX_VALUE, (byte)1);
96.60 + }
96.61 +
96.62 + @Compare public byte subUnderflow() {
96.63 + return sub(Byte.MIN_VALUE, (byte)1);
96.64 + }
96.65 +
96.66 + @Compare public byte addMaxByteAndMaxByte() {
96.67 + return add(Byte.MAX_VALUE, Byte.MAX_VALUE);
96.68 + }
96.69 +
96.70 + @Compare public byte subMinByteAndMinByte() {
96.71 + return sub(Byte.MIN_VALUE, Byte.MIN_VALUE);
96.72 + }
96.73 +
96.74 + @Compare public byte multiplyMaxByte() {
96.75 + return mul(Byte.MAX_VALUE, (byte)2);
96.76 + }
96.77 +
96.78 + @Compare public byte multiplyMaxByteAndMaxByte() {
96.79 + return mul(Byte.MAX_VALUE, Byte.MAX_VALUE);
96.80 + }
96.81 +
96.82 + @Compare public byte multiplyMinByte() {
96.83 + return mul(Byte.MIN_VALUE, (byte)2);
96.84 + }
96.85 +
96.86 + @Compare public byte multiplyMinByteAndMinByte() {
96.87 + return mul(Byte.MIN_VALUE, Byte.MIN_VALUE);
96.88 + }
96.89 +
96.90 + @Compare public byte multiplyPrecision() {
96.91 + return mul((byte)17638, (byte)1103);
96.92 + }
96.93 +
96.94 + @Compare public byte division() {
96.95 + return div((byte)1, (byte)2);
96.96 + }
96.97 +
96.98 + @Compare public byte divisionReminder() {
96.99 + return mod((byte)1, (byte)2);
96.100 + }
96.101 +
96.102 + private static int readShort(byte[] byteCodes, int offset) {
96.103 + int signed = byteCodes[offset];
96.104 + byte b0 = (byte)signed;
96.105 + return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
96.106 + }
96.107 +
96.108 + private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
96.109 + return readShort(byteCodes, offsetInstruction + 1);
96.110 + }
96.111 +
96.112 + @Compare public int readIntArgs255and156() {
96.113 + final byte[] arr = new byte[] { (byte)0, (byte)255, (byte)156 };
96.114 +
96.115 + assert arr[1] == -1 : "First byte: " + arr[1];
96.116 + assert arr[2] == -100 : "Second byte: " + arr[2];
96.117 + final int ret = readShortArg(arr, 0);
96.118 + assert ret < 65000: "Value: " + ret;
96.119 + return ret;
96.120 + }
96.121 +
96.122 + @JavaScriptBody(args = { "arr" }, body = "arr[1] = 255; arr[2] = 156; return arr;")
96.123 + private static byte[] fill255and156(byte[] arr) {
96.124 + arr[1] = (byte)255;
96.125 + arr[2] = (byte)156;
96.126 + return arr;
96.127 + }
96.128 +
96.129 + @Compare public int readIntArgs255and156JSArray() {
96.130 + final byte[] arr = fill255and156(new byte[] { 0, 0, 0 });
96.131 +
96.132 + final int ret = readShortArg(arr, 0);
96.133 + assert ret < 65000: "Value: " + ret;
96.134 + return ret;
96.135 + }
96.136 +
96.137 + @Compare public int readIntArgsMinus1andMinus100() {
96.138 + final byte[] arr = new byte[] { (byte)0, (byte)-1, (byte)-100 };
96.139 +
96.140 + assert arr[1] == -1 : "First byte: " + arr[1];
96.141 + assert arr[2] == -100 : "Second byte: " + arr[2];
96.142 +
96.143 + return readShortArg(arr, 0);
96.144 + }
96.145 +
96.146 + @Factory
96.147 + public static Object[] create() {
96.148 + return VMTest.create(ByteArithmeticTest.class);
96.149 + }
96.150 +}
97.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
97.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CloneTest.java Fri May 17 10:27:41 2013 +0200
97.3 @@ -0,0 +1,73 @@
97.4 +/**
97.5 + * Back 2 Browser Bytecode Translator
97.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
97.7 + *
97.8 + * This program is free software: you can redistribute it and/or modify
97.9 + * it under the terms of the GNU General Public License as published by
97.10 + * the Free Software Foundation, version 2 of the License.
97.11 + *
97.12 + * This program is distributed in the hope that it will be useful,
97.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
97.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
97.15 + * GNU General Public License for more details.
97.16 + *
97.17 + * You should have received a copy of the GNU General Public License
97.18 + * along with this program. Look for COPYING file in the top folder.
97.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
97.20 + */
97.21 +package org.apidesign.bck2brwsr.tck;
97.22 +
97.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
97.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
97.25 +import org.testng.annotations.Factory;
97.26 +
97.27 +/**
97.28 + *
97.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
97.30 + */
97.31 +public class CloneTest {
97.32 + private int value;
97.33 +
97.34 + @Compare
97.35 + public Object notSupported() throws CloneNotSupportedException {
97.36 + return this.clone();
97.37 + }
97.38 +
97.39 + @Compare public String sameClass() throws CloneNotSupportedException {
97.40 + return new Clnbl().clone().getClass().getName();
97.41 + }
97.42 +
97.43 + @Compare public boolean differentInstance() throws CloneNotSupportedException {
97.44 + Clnbl orig = new Clnbl();
97.45 + return orig == orig.clone();
97.46 + }
97.47 +
97.48 + @Compare public int sameReference() throws CloneNotSupportedException {
97.49 + CloneTest self = this;
97.50 + Clnbl orig = new Clnbl();
97.51 + self.value = 33;
97.52 + orig.ref = self;
97.53 + return ((Clnbl)orig.clone()).ref.value;
97.54 + }
97.55 +
97.56 + @Compare public int sameValue() throws CloneNotSupportedException {
97.57 + Clnbl orig = new Clnbl();
97.58 + orig.value = 10;
97.59 + return ((Clnbl)orig.clone()).value;
97.60 + }
97.61 +
97.62 + @Factory
97.63 + public static Object[] create() {
97.64 + return VMTest.create(CloneTest.class);
97.65 + }
97.66 +
97.67 + public static final class Clnbl implements Cloneable {
97.68 + public CloneTest ref;
97.69 + private int value;
97.70 +
97.71 + @Override
97.72 + public Object clone() throws CloneNotSupportedException {
97.73 + return super.clone();
97.74 + }
97.75 + }
97.76 +}
98.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
98.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareByteArrayTest.java Fri May 17 10:27:41 2013 +0200
98.3 @@ -0,0 +1,103 @@
98.4 +/**
98.5 + * Back 2 Browser Bytecode Translator
98.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
98.7 + *
98.8 + * This program is free software: you can redistribute it and/or modify
98.9 + * it under the terms of the GNU General Public License as published by
98.10 + * the Free Software Foundation, version 2 of the License.
98.11 + *
98.12 + * This program is distributed in the hope that it will be useful,
98.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
98.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
98.15 + * GNU General Public License for more details.
98.16 + *
98.17 + * You should have received a copy of the GNU General Public License
98.18 + * along with this program. Look for COPYING file in the top folder.
98.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
98.20 + */
98.21 +package org.apidesign.bck2brwsr.tck;
98.22 +
98.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
98.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
98.25 +import org.testng.annotations.Factory;
98.26 +
98.27 +/**
98.28 + *
98.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
98.30 + */
98.31 +public class CompareByteArrayTest {
98.32 + @Compare public int byteArraySum() {
98.33 + byte[] arr = createArray();
98.34 + return sumByteArr(arr);
98.35 + }
98.36 +
98.37 + @Compare public int countZeros() {
98.38 + int zeros = 0;
98.39 + for (Byte b : createArray()) {
98.40 + if (b == 0) {
98.41 + zeros++;
98.42 + }
98.43 + }
98.44 + return zeros;
98.45 + }
98.46 +
98.47 + private static int sumByteArr(byte[] arr) {
98.48 + int sum = 0;
98.49 + for (int i = 0; i < arr.length; i++) {
98.50 + sum += arr[i];
98.51 + }
98.52 + return sum;
98.53 + }
98.54 +
98.55 + @Compare public String noOutOfBounds() {
98.56 + return atIndex(1);
98.57 + }
98.58 +
98.59 + @Compare public String outOfBounds() {
98.60 + return atIndex(5);
98.61 + }
98.62 +
98.63 + @Compare public String outOfBoundsMinus() {
98.64 + return atIndex(-1);
98.65 + }
98.66 +
98.67 + @Compare public String toOfBounds() {
98.68 + return toIndex(5);
98.69 + }
98.70 +
98.71 + @Compare public String toOfBoundsMinus() {
98.72 + return toIndex(-1);
98.73 + }
98.74 +
98.75 + @Compare public int multiArrayLength() {
98.76 + int[][] arr = new int[1][0];
98.77 + return arr[0].length;
98.78 + }
98.79 +
98.80 + @Compare public int multiObjectArrayLength() {
98.81 + Object[][] arr = new Object[1][0];
98.82 + return arr[0].length;
98.83 + }
98.84 +
98.85 + private static final int[] arr = { 0, 1, 2 };
98.86 + public static String atIndex(int at) {
98.87 + return "at@" + arr[at];
98.88 + }
98.89 + public static String toIndex(int at) {
98.90 + arr[at] = 10;
98.91 + return "ok";
98.92 + }
98.93 +
98.94 +
98.95 + @Factory
98.96 + public static Object[] create() {
98.97 + return VMTest.create(CompareByteArrayTest.class);
98.98 + }
98.99 +
98.100 + private byte[] createArray() {
98.101 + byte[] arr = new byte[10];
98.102 + arr[5] = 3;
98.103 + arr[7] = 8;
98.104 + return arr;
98.105 + }
98.106 +}
99.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
99.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareHashTest.java Fri May 17 10:27:41 2013 +0200
99.3 @@ -0,0 +1,50 @@
99.4 +/**
99.5 + * Back 2 Browser Bytecode Translator
99.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
99.7 + *
99.8 + * This program is free software: you can redistribute it and/or modify
99.9 + * it under the terms of the GNU General Public License as published by
99.10 + * the Free Software Foundation, version 2 of the License.
99.11 + *
99.12 + * This program is distributed in the hope that it will be useful,
99.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
99.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
99.15 + * GNU General Public License for more details.
99.16 + *
99.17 + * You should have received a copy of the GNU General Public License
99.18 + * along with this program. Look for COPYING file in the top folder.
99.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
99.20 + */
99.21 +package org.apidesign.bck2brwsr.tck;
99.22 +
99.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
99.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
99.25 +import org.testng.annotations.Factory;
99.26 +
99.27 +/**
99.28 + *
99.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
99.30 + */
99.31 +public class CompareHashTest {
99.32 + @Compare public int hashOfString() {
99.33 + return "Ahoj".hashCode();
99.34 + }
99.35 +
99.36 + @Compare public int hashRemainsYieldsZero() {
99.37 + Object o = new Object();
99.38 + return o.hashCode() - o.hashCode();
99.39 + }
99.40 +
99.41 + @Compare public int initializeInStatic() {
99.42 + return StaticUse.NON_NULL.hashCode() - StaticUse.NON_NULL.hashCode();
99.43 + }
99.44 +
99.45 + @Compare public int hashOfInt() {
99.46 + return Integer.valueOf(Integer.MAX_VALUE).hashCode();
99.47 + }
99.48 +
99.49 + @Factory
99.50 + public static Object[] create() {
99.51 + return VMTest.create(CompareHashTest.class);
99.52 + }
99.53 +}
100.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
100.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareIntArrayTest.java Fri May 17 10:27:41 2013 +0200
100.3 @@ -0,0 +1,63 @@
100.4 +/**
100.5 + * Back 2 Browser Bytecode Translator
100.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
100.7 + *
100.8 + * This program is free software: you can redistribute it and/or modify
100.9 + * it under the terms of the GNU General Public License as published by
100.10 + * the Free Software Foundation, version 2 of the License.
100.11 + *
100.12 + * This program is distributed in the hope that it will be useful,
100.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
100.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
100.15 + * GNU General Public License for more details.
100.16 + *
100.17 + * You should have received a copy of the GNU General Public License
100.18 + * along with this program. Look for COPYING file in the top folder.
100.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
100.20 + */
100.21 +package org.apidesign.bck2brwsr.tck;
100.22 +
100.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
100.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
100.25 +import org.testng.annotations.Factory;
100.26 +
100.27 +/**
100.28 + *
100.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
100.30 + */
100.31 +public class CompareIntArrayTest {
100.32 + @Compare public int integerArraySum() {
100.33 + int[] arr = createArray();
100.34 + return sumIntArr(arr);
100.35 + }
100.36 +
100.37 + @Compare public int countZeros() {
100.38 + int zeros = 0;
100.39 + for (Integer i : createArray()) {
100.40 + if (i == 0) {
100.41 + zeros++;
100.42 + }
100.43 + }
100.44 + return zeros;
100.45 + }
100.46 +
100.47 + private static int sumIntArr(int[] arr) {
100.48 + int sum = 0;
100.49 + for (int i = 0; i < arr.length; i++) {
100.50 + sum += arr[i];
100.51 + }
100.52 + return sum;
100.53 + }
100.54 +
100.55 + @Factory
100.56 + public static Object[] create() {
100.57 + return VMTest.create(CompareIntArrayTest.class);
100.58 + }
100.59 +
100.60 + private int[] createArray() {
100.61 + int[] arr = new int[10];
100.62 + arr[5] = 3;
100.63 + arr[7] = 8;
100.64 + return arr;
100.65 + }
100.66 +}
101.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
101.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java Fri May 17 10:27:41 2013 +0200
101.3 @@ -0,0 +1,174 @@
101.4 +/**
101.5 + * Back 2 Browser Bytecode Translator
101.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
101.7 + *
101.8 + * This program is free software: you can redistribute it and/or modify
101.9 + * it under the terms of the GNU General Public License as published by
101.10 + * the Free Software Foundation, version 2 of the License.
101.11 + *
101.12 + * This program is distributed in the hope that it will be useful,
101.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
101.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
101.15 + * GNU General Public License for more details.
101.16 + *
101.17 + * You should have received a copy of the GNU General Public License
101.18 + * along with this program. Look for COPYING file in the top folder.
101.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
101.20 + */
101.21 +package org.apidesign.bck2brwsr.tck;
101.22 +
101.23 +import java.io.UnsupportedEncodingException;
101.24 +import java.net.MalformedURLException;
101.25 +import java.net.URL;
101.26 +import org.apidesign.bck2brwsr.vmtest.Compare;
101.27 +import org.apidesign.bck2brwsr.vmtest.VMTest;
101.28 +import org.testng.annotations.Factory;
101.29 +
101.30 +/**
101.31 + *
101.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
101.33 + */
101.34 +public class CompareStringsTest {
101.35 + @Compare public String firstChar() {
101.36 + return "" + ("Hello".toCharArray()[0]);
101.37 + }
101.38 +
101.39 + @Compare public String classCast() {
101.40 + Object o = firstChar();
101.41 + return String.class.cast(o);
101.42 + }
101.43 +
101.44 + @Compare public String classCastThrown() {
101.45 + Object o = null;
101.46 + return String.class.cast(o);
101.47 + }
101.48 +
101.49 + @Compare public boolean equalToNull() {
101.50 + return "Ahoj".equals(null);
101.51 + }
101.52 +
101.53 + @Compare public int highByteLenght() {
101.54 + byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
101.55 + return new String(arr, 0).length();
101.56 + }
101.57 +
101.58 + @Compare public String highByte() {
101.59 + byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
101.60 + StringBuilder sb = new StringBuilder();
101.61 + sb.append("pref:");
101.62 + sb.append(new String(arr, 0));
101.63 + return sb.toString();
101.64 + }
101.65 +
101.66 + @Compare public static Object compareURLs() throws MalformedURLException {
101.67 + return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString();
101.68 + }
101.69 +
101.70 + @Compare public String deleteLastTwoCharacters() {
101.71 + StringBuilder sb = new StringBuilder();
101.72 + sb.append("453.0");
101.73 + if (sb.toString().endsWith(".0")) {
101.74 + final int l = sb.length();
101.75 + sb.delete(l - 2, l);
101.76 + }
101.77 + return sb.toString().toString();
101.78 + }
101.79 +
101.80 + @Compare public String nameOfStringClass() throws Exception {
101.81 + return Class.forName("java.lang.String").getName();
101.82 + }
101.83 + @Compare public String nameOfArrayClass() throws Exception {
101.84 + return Class.forName("org.apidesign.bck2brwsr.tck.CompareHashTest").getName();
101.85 + }
101.86 +
101.87 + @Compare public String lowerHello() {
101.88 + return "HeLlO".toLowerCase();
101.89 + }
101.90 +
101.91 + @Compare public String lowerA() {
101.92 + return String.valueOf(Character.toLowerCase('A')).toString();
101.93 + }
101.94 + @Compare public String upperHello() {
101.95 + return "hello".toUpperCase();
101.96 + }
101.97 +
101.98 + @Compare public String upperA() {
101.99 + return String.valueOf(Character.toUpperCase('a')).toString();
101.100 + }
101.101 +
101.102 + @Compare public boolean matchRegExp() throws Exception {
101.103 + return "58038503".matches("\\d*");
101.104 + }
101.105 +
101.106 + @Compare public boolean doesNotMatchRegExp() throws Exception {
101.107 + return "58038503GH".matches("\\d*");
101.108 + }
101.109 +
101.110 + @Compare public boolean doesNotMatchRegExpFully() throws Exception {
101.111 + return "Hello".matches("Hell");
101.112 + }
101.113 +
101.114 + @Compare public String emptyCharArray() {
101.115 + char[] arr = new char[10];
101.116 + return new String(arr);
101.117 + }
101.118 +
101.119 + @Compare public String variousCharacterTests() throws Exception {
101.120 + StringBuilder sb = new StringBuilder();
101.121 +
101.122 + sb.append(Character.isUpperCase('a'));
101.123 + sb.append(Character.isUpperCase('A'));
101.124 + sb.append(Character.isLowerCase('a'));
101.125 + sb.append(Character.isLowerCase('A'));
101.126 +
101.127 + sb.append(Character.isLetter('A'));
101.128 + sb.append(Character.isLetterOrDigit('9'));
101.129 + sb.append(Character.isLetterOrDigit('A'));
101.130 + sb.append(Character.isLetter('0'));
101.131 +
101.132 + return sb.toString().toString();
101.133 + }
101.134 +
101.135 + @Compare
101.136 + public String nullFieldInitialized() {
101.137 + NullField nf = new NullField();
101.138 + return ("" + nf.name).toString();
101.139 + }
101.140 + @Compare
101.141 + public String toUTFString() throws UnsupportedEncodingException {
101.142 + byte[] arr = {
101.143 + (byte) -59, (byte) -67, (byte) 108, (byte) 117, (byte) -59, (byte) -91,
101.144 + (byte) 111, (byte) 117, (byte) -60, (byte) -115, (byte) 107, (byte) -61,
101.145 + (byte) -67, (byte) 32, (byte) 107, (byte) -59, (byte) -81, (byte) -59,
101.146 + (byte) -120
101.147 + };
101.148 + return new String(arr, "utf-8");
101.149 + }
101.150 +
101.151 + @Compare
101.152 + public int stringToBytesLenght() throws UnsupportedEncodingException {
101.153 + return "\u017dlu\u0165ou\u010dk\u00fd k\u016f\u0148".getBytes("utf8").length;
101.154 + }
101.155 +
101.156 + @Compare public String replaceSeq() {
101.157 + return "Hello World.".replace(".", "!");
101.158 + }
101.159 + @Compare public String replaceSeqAll() {
101.160 + return "Hello World! Hello World.".replace("World", "Jarda");
101.161 + }
101.162 + @Compare public String replaceSeqAA() {
101.163 + String res = "aaa".replace("aa", "b");
101.164 + assert res.equals("ba") : "Expecting ba: " + res;
101.165 + return res;
101.166 + }
101.167 +
101.168 + @Factory
101.169 + public static Object[] create() {
101.170 + return VMTest.create(CompareStringsTest.class);
101.171 + }
101.172 +
101.173 + private static final class NullField {
101.174 +
101.175 + String name;
101.176 + }
101.177 +}
102.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
102.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/DoubleTest.java Fri May 17 10:27:41 2013 +0200
102.3 @@ -0,0 +1,65 @@
102.4 +/**
102.5 + * Back 2 Browser Bytecode Translator
102.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
102.7 + *
102.8 + * This program is free software: you can redistribute it and/or modify
102.9 + * it under the terms of the GNU General Public License as published by
102.10 + * the Free Software Foundation, version 2 of the License.
102.11 + *
102.12 + * This program is distributed in the hope that it will be useful,
102.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
102.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
102.15 + * GNU General Public License for more details.
102.16 + *
102.17 + * You should have received a copy of the GNU General Public License
102.18 + * along with this program. Look for COPYING file in the top folder.
102.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
102.20 + */
102.21 +package org.apidesign.bck2brwsr.tck;
102.22 +
102.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
102.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
102.25 +import org.testng.annotations.Factory;
102.26 +
102.27 +/**
102.28 + *
102.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
102.30 + */
102.31 +public class DoubleTest {
102.32 + @Compare public String integerToString() {
102.33 + return toStr(1);
102.34 + }
102.35 +
102.36 + @Compare public String integerAndHalfToString() {
102.37 + return toStr(1.5);
102.38 + }
102.39 +
102.40 + @Compare public double longToAndBack() {
102.41 + return Double.parseDouble(toStr(Long.MAX_VALUE / 10));
102.42 + }
102.43 +
102.44 + @Compare public String negativeIntToString() {
102.45 + return toStr(-10);
102.46 + }
102.47 +
102.48 + @Compare public String negativeIntAndHalfToString() {
102.49 + return toStr(-10.5);
102.50 + }
102.51 +
102.52 + @Compare public double negativeLongAndBack() {
102.53 + return Double.parseDouble(toStr(Long.MIN_VALUE / 10));
102.54 + }
102.55 +
102.56 + @Compare public double canParseExp() {
102.57 + return Double.parseDouble(toStr(1.7976931348623157e+308));
102.58 + }
102.59 +
102.60 + private static String toStr(double d) {
102.61 + return Double.toString(d);
102.62 + }
102.63 +
102.64 + @Factory
102.65 + public static Object[] create() {
102.66 + return VMTest.create(DoubleTest.class);
102.67 + }
102.68 +}
103.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
103.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java Fri May 17 10:27:41 2013 +0200
103.3 @@ -0,0 +1,106 @@
103.4 +/**
103.5 + * Back 2 Browser Bytecode Translator
103.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
103.7 + *
103.8 + * This program is free software: you can redistribute it and/or modify
103.9 + * it under the terms of the GNU General Public License as published by
103.10 + * the Free Software Foundation, version 2 of the License.
103.11 + *
103.12 + * This program is distributed in the hope that it will be useful,
103.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
103.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
103.15 + * GNU General Public License for more details.
103.16 + *
103.17 + * You should have received a copy of the GNU General Public License
103.18 + * along with this program. Look for COPYING file in the top folder.
103.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
103.20 + */
103.21 +package org.apidesign.bck2brwsr.tck;
103.22 +
103.23 +import java.io.InputStream;
103.24 +import java.net.URL;
103.25 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
103.26 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
103.27 +import org.apidesign.bck2brwsr.vmtest.Http;
103.28 +import org.apidesign.bck2brwsr.vmtest.VMTest;
103.29 +import org.testng.annotations.Factory;
103.30 +
103.31 +/**
103.32 + *
103.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
103.34 + */
103.35 +public class HttpResourceTest {
103.36 +
103.37 + @Http({
103.38 + @Http.Resource(path = "/xhr", content = "Hello Brwsr!", mimeType = "text/plain")
103.39 + })
103.40 + @BrwsrTest
103.41 +
103.42 +
103.43 + public String testReadContentViaXHR() throws Exception {
103.44 + String msg = read("/xhr");
103.45 + assert "Hello Brwsr!".equals(msg) : "The message was " + msg;
103.46 + return msg;
103.47 + }
103.48 +
103.49 + @Http({
103.50 + @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
103.51 + })
103.52 + @BrwsrTest
103.53 + public String testReadContentViaURL() throws Exception {
103.54 + URL url = new URL("http:/url");
103.55 + String msg = (String) url.getContent();
103.56 + assert "Hello via URL!".equals(msg) : "The message was " + msg;
103.57 + return msg;
103.58 + }
103.59 + @Http({
103.60 + @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
103.61 + })
103.62 + @BrwsrTest
103.63 + public String testReadContentViaURLWithStringParam() throws Exception {
103.64 + URL url = new URL("http:/url");
103.65 + String msg = (String) url.getContent(new Class[] { String.class });
103.66 + assert "Hello via URL!".equals(msg) : "The message was " + msg;
103.67 + return msg;
103.68 + }
103.69 +
103.70 + @Http({
103.71 + @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
103.72 + })
103.73 + @BrwsrTest
103.74 + public void testReadByte() throws Exception {
103.75 + URL url = new URL("http:/bytes");
103.76 + final Object res = url.getContent(new Class[] { byte[].class });
103.77 + assert res instanceof byte[] : "Expecting byte[]: " + res;
103.78 + byte[] arr = (byte[]) res;
103.79 + assert arr.length == 1 : "One byte " + arr.length;
103.80 + assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
103.81 + }
103.82 +
103.83 + @Http({
103.84 + @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
103.85 + })
103.86 + @BrwsrTest
103.87 + public void testReadByteViaInputStream() throws Exception {
103.88 + URL url = new URL("http:/bytes");
103.89 + InputStream is = url.openStream();
103.90 + byte[] arr = new byte[10];
103.91 + int len = is.read(arr);
103.92 + assert len == 1 : "One byte " + len;
103.93 + assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
103.94 + }
103.95 +
103.96 + @JavaScriptBody(args = { "url" }, body =
103.97 + "var req = new XMLHttpRequest();\n"
103.98 + + "req.open('GET', url, false);\n"
103.99 + + "req.send();\n"
103.100 + + "return req.responseText;"
103.101 + )
103.102 + private static native String read(String url);
103.103 +
103.104 +
103.105 + @Factory
103.106 + public static Object[] create() {
103.107 + return VMTest.create(HttpResourceTest.class);
103.108 + }
103.109 +}
104.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
104.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/InheritanceA.java Fri May 17 10:27:41 2013 +0200
104.3 @@ -0,0 +1,34 @@
104.4 +/**
104.5 + * Back 2 Browser Bytecode Translator
104.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
104.7 + *
104.8 + * This program is free software: you can redistribute it and/or modify
104.9 + * it under the terms of the GNU General Public License as published by
104.10 + * the Free Software Foundation, version 2 of the License.
104.11 + *
104.12 + * This program is distributed in the hope that it will be useful,
104.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
104.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
104.15 + * GNU General Public License for more details.
104.16 + *
104.17 + * You should have received a copy of the GNU General Public License
104.18 + * along with this program. Look for COPYING file in the top folder.
104.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
104.20 + */
104.21 +package org.apidesign.bck2brwsr.tck;
104.22 +
104.23 +/**
104.24 + *
104.25 + * @author Jaroslav Tulach <jtulach@netbeans.org>
104.26 + */
104.27 +public class InheritanceA {
104.28 + private String name;
104.29 +
104.30 + public void setA(String n) {
104.31 + this.name = n;
104.32 + }
104.33 +
104.34 + public String getA() {
104.35 + return name;
104.36 + }
104.37 +}
105.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
105.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/InheritanceB.java Fri May 17 10:27:41 2013 +0200
105.3 @@ -0,0 +1,34 @@
105.4 +/**
105.5 + * Back 2 Browser Bytecode Translator
105.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
105.7 + *
105.8 + * This program is free software: you can redistribute it and/or modify
105.9 + * it under the terms of the GNU General Public License as published by
105.10 + * the Free Software Foundation, version 2 of the License.
105.11 + *
105.12 + * This program is distributed in the hope that it will be useful,
105.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
105.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
105.15 + * GNU General Public License for more details.
105.16 + *
105.17 + * You should have received a copy of the GNU General Public License
105.18 + * along with this program. Look for COPYING file in the top folder.
105.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
105.20 + */
105.21 +package org.apidesign.bck2brwsr.tck;
105.22 +
105.23 +/**
105.24 + *
105.25 + * @author Jaroslav Tulach <jtulach@netbeans.org>
105.26 + */
105.27 +public class InheritanceB extends InheritanceA {
105.28 + private String name;
105.29 +
105.30 + public void setB(String n) {
105.31 + this.name = n;
105.32 + }
105.33 +
105.34 + public String getB() {
105.35 + return name;
105.36 + }
105.37 +}
106.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
106.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/InheritanceTest.java Fri May 17 10:27:41 2013 +0200
106.3 @@ -0,0 +1,41 @@
106.4 +/**
106.5 + * Back 2 Browser Bytecode Translator
106.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
106.7 + *
106.8 + * This program is free software: you can redistribute it and/or modify
106.9 + * it under the terms of the GNU General Public License as published by
106.10 + * the Free Software Foundation, version 2 of the License.
106.11 + *
106.12 + * This program is distributed in the hope that it will be useful,
106.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
106.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
106.15 + * GNU General Public License for more details.
106.16 + *
106.17 + * You should have received a copy of the GNU General Public License
106.18 + * along with this program. Look for COPYING file in the top folder.
106.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
106.20 + */
106.21 +package org.apidesign.bck2brwsr.tck;
106.22 +
106.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
106.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
106.25 +import org.testng.annotations.Factory;
106.26 +
106.27 +/**
106.28 + *
106.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
106.30 + */
106.31 +public class InheritanceTest {
106.32 +
106.33 + @Compare public String checkFieldsIndependent() throws ClassNotFoundException {
106.34 + InheritanceB ib = new InheritanceB();
106.35 + ib.setA("A");
106.36 + ib.setB("B");
106.37 + return "A: " + ib.getA() + " B: " + ib.getB();
106.38 + }
106.39 +
106.40 + @Factory
106.41 + public static Object[] create() {
106.42 + return VMTest.create(InheritanceTest.class);
106.43 + }
106.44 +}
107.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
107.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/IntegerArithmeticTest.java Fri May 17 10:27:41 2013 +0200
107.3 @@ -0,0 +1,166 @@
107.4 +/**
107.5 + * Back 2 Browser Bytecode Translator
107.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
107.7 + *
107.8 + * This program is free software: you can redistribute it and/or modify
107.9 + * it under the terms of the GNU General Public License as published by
107.10 + * the Free Software Foundation, version 2 of the License.
107.11 + *
107.12 + * This program is distributed in the hope that it will be useful,
107.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
107.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
107.15 + * GNU General Public License for more details.
107.16 + *
107.17 + * You should have received a copy of the GNU General Public License
107.18 + * along with this program. Look for COPYING file in the top folder.
107.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
107.20 + */
107.21 +package org.apidesign.bck2brwsr.tck;
107.22 +
107.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
107.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
107.25 +import org.testng.annotations.Factory;
107.26 +
107.27 +/**
107.28 + *
107.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
107.30 + */
107.31 +public class IntegerArithmeticTest {
107.32 +
107.33 + private static int add(int x, int y) {
107.34 + return x + y;
107.35 + }
107.36 +
107.37 + private static int sub(int x, int y) {
107.38 + return x - y;
107.39 + }
107.40 +
107.41 + private static int mul(int x, int y) {
107.42 + return x * y;
107.43 + }
107.44 +
107.45 + private static int div(int x, int y) {
107.46 + return x / y;
107.47 + }
107.48 +
107.49 + private static int mod(int x, int y) {
107.50 + return x % y;
107.51 + }
107.52 +
107.53 + private static int neg(int x) {
107.54 + return (-x);
107.55 + }
107.56 +
107.57 + private static float fadd(float x, float y) {
107.58 + return x + y;
107.59 + }
107.60 +
107.61 + private static double dadd(double x, double y) {
107.62 + return x + y;
107.63 + }
107.64 +
107.65 + @Compare public int addOverflow() {
107.66 + return add(Integer.MAX_VALUE, 1);
107.67 + }
107.68 +
107.69 + @Compare public int subUnderflow() {
107.70 + return sub(Integer.MIN_VALUE, 1);
107.71 + }
107.72 +
107.73 + @Compare public int addMaxIntAndMaxInt() {
107.74 + return add(Integer.MAX_VALUE, Integer.MAX_VALUE);
107.75 + }
107.76 +
107.77 + @Compare public int subMinIntAndMinInt() {
107.78 + return sub(Integer.MIN_VALUE, Integer.MIN_VALUE);
107.79 + }
107.80 +
107.81 + @Compare public int multiplyMaxInt() {
107.82 + return mul(Integer.MAX_VALUE, 2);
107.83 + }
107.84 +
107.85 + @Compare public int multiplyMaxIntAndMaxInt() {
107.86 + return mul(Integer.MAX_VALUE, Integer.MAX_VALUE);
107.87 + }
107.88 +
107.89 + @Compare public int multiplyMinInt() {
107.90 + return mul(Integer.MIN_VALUE, 2);
107.91 + }
107.92 +
107.93 + @Compare public int multiplyMinIntAndMinInt() {
107.94 + return mul(Integer.MIN_VALUE, Integer.MIN_VALUE);
107.95 + }
107.96 +
107.97 + @Compare public int multiplyPrecision() {
107.98 + return mul(119106029, 1103515245);
107.99 + }
107.100 +
107.101 + @Compare public int division() {
107.102 + return div(1, 2);
107.103 + }
107.104 +
107.105 + @Compare public int divisionReminder() {
107.106 + return mod(1, 2);
107.107 + }
107.108 +
107.109 + @Compare public int negativeDivision() {
107.110 + return div(-7, 3);
107.111 + }
107.112 +
107.113 + @Compare public int negativeDivisionReminder() {
107.114 + return mod(-7, 3);
107.115 + }
107.116 +
107.117 + @Compare public int conversionFromFloat() {
107.118 + return (int) fadd(-2, -0.6f);
107.119 + }
107.120 +
107.121 + @Compare public int conversionFromDouble() {
107.122 + return (int) dadd(-2, -0.6);
107.123 + }
107.124 +
107.125 + @Compare public boolean divByZeroThrowsArithmeticException() {
107.126 + try {
107.127 + div(1, 0);
107.128 + return false;
107.129 + } catch (final ArithmeticException e) {
107.130 + return true;
107.131 + }
107.132 + }
107.133 +
107.134 + @Compare public boolean modByZeroThrowsArithmeticException() {
107.135 + try {
107.136 + mod(1, 0);
107.137 + return false;
107.138 + } catch (final ArithmeticException e) {
107.139 + return true;
107.140 + }
107.141 + }
107.142 +
107.143 + @Compare public int negate() {
107.144 + return neg(123456);
107.145 + }
107.146 +
107.147 + @Compare public int negateMaxInt() {
107.148 + return neg(Integer.MAX_VALUE);
107.149 + }
107.150 +
107.151 + @Compare public int negateMinInt() {
107.152 + return neg(Integer.MIN_VALUE);
107.153 + }
107.154 +
107.155 + @Compare public int sumTwoDimensions() {
107.156 + int[][] matrix = createMatrix(4, 3);
107.157 + matrix[0][0] += 10;
107.158 + return matrix[0][0];
107.159 + }
107.160 +
107.161 + static int[][] createMatrix(int x, int y) {
107.162 + return new int[x][y];
107.163 + }
107.164 +
107.165 + @Factory
107.166 + public static Object[] create() {
107.167 + return VMTest.create(IntegerArithmeticTest.class);
107.168 + }
107.169 +}
108.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
108.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java Fri May 17 10:27:41 2013 +0200
108.3 @@ -0,0 +1,376 @@
108.4 +/**
108.5 + * Back 2 Browser Bytecode Translator
108.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
108.7 + *
108.8 + * This program is free software: you can redistribute it and/or modify
108.9 + * it under the terms of the GNU General Public License as published by
108.10 + * the Free Software Foundation, version 2 of the License.
108.11 + *
108.12 + * This program is distributed in the hope that it will be useful,
108.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
108.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
108.15 + * GNU General Public License for more details.
108.16 + *
108.17 + * You should have received a copy of the GNU General Public License
108.18 + * along with this program. Look for COPYING file in the top folder.
108.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
108.20 + */
108.21 +package org.apidesign.bck2brwsr.tck;
108.22 +
108.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
108.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
108.25 +import org.testng.annotations.Factory;
108.26 +
108.27 +/**
108.28 + *
108.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
108.30 + */
108.31 +public class LongArithmeticTest {
108.32 +
108.33 + private static long add(long x, long y) {
108.34 + return (x + y);
108.35 + }
108.36 +
108.37 + private static long sub(long x, long y) {
108.38 + return (x - y);
108.39 + }
108.40 +
108.41 + private static long mul(long x, long y) {
108.42 + return (x * y);
108.43 + }
108.44 +
108.45 + private static long div(long x, long y) {
108.46 + return (x / y);
108.47 + }
108.48 +
108.49 + private static long mod(long x, long y) {
108.50 + return (x % y);
108.51 + }
108.52 +
108.53 + private static long neg(long x) {
108.54 + return (-x);
108.55 + }
108.56 +
108.57 + private static long shl(long x, int b) {
108.58 + return (x << b);
108.59 + }
108.60 +
108.61 + private static long shr(long x, int b) {
108.62 + return (x >> b);
108.63 + }
108.64 +
108.65 + private static long ushr(long x, int b) {
108.66 + return (x >>> b);
108.67 + }
108.68 +
108.69 + private static long and(long x, long y) {
108.70 + return (x & y);
108.71 + }
108.72 +
108.73 + private static long or(long x, long y) {
108.74 + return (x | y);
108.75 + }
108.76 +
108.77 + private static long xor(long x, long y) {
108.78 + return (x ^ y);
108.79 + }
108.80 +
108.81 + private static float fadd(float x, float y) {
108.82 + return x + y;
108.83 + }
108.84 +
108.85 + private static double dadd(double x, double y) {
108.86 + return x + y;
108.87 + }
108.88 +
108.89 + public static int compare(long x, long y, int zero) {
108.90 + final int xyResult = compareL(x, y, zero);
108.91 + final int yxResult = compareL(y, x, zero);
108.92 +
108.93 + return ((xyResult + yxResult) == 0) ? xyResult : -2;
108.94 + }
108.95 +
108.96 + private static int compareL(long x, long y, int zero) {
108.97 + int result = -2;
108.98 + int trueCount = 0;
108.99 +
108.100 + x += zero;
108.101 + if (x == y) {
108.102 + result = 0;
108.103 + ++trueCount;
108.104 + }
108.105 +
108.106 + x += zero;
108.107 + if (x < y) {
108.108 + result = -1;
108.109 + ++trueCount;
108.110 + }
108.111 +
108.112 + x += zero;
108.113 + if (x > y) {
108.114 + result = 1;
108.115 + ++trueCount;
108.116 + }
108.117 +
108.118 + return (trueCount == 1) ? result : -2;
108.119 + }
108.120 +
108.121 + @Compare public long conversion() {
108.122 + return Long.MAX_VALUE;
108.123 + }
108.124 +
108.125 + @Compare public long negate1() {
108.126 + return neg(0x00fa37d7763e0ca1l);
108.127 + }
108.128 +
108.129 + @Compare public long negate2() {
108.130 + return neg(0x80fa37d7763e0ca1l);
108.131 + }
108.132 +
108.133 + @Compare public long negate3() {
108.134 + return neg(0xfffffffffffffeddl);
108.135 + }
108.136 +
108.137 + @Compare public long addOverflow() {
108.138 + return add(Long.MAX_VALUE, 1l);
108.139 + }
108.140 +
108.141 + @Compare public long subUnderflow() {
108.142 + return sub(Long.MIN_VALUE, 1l);
108.143 + }
108.144 +
108.145 + @Compare public long addMaxLongAndMaxLong() {
108.146 + return add(Long.MAX_VALUE, Long.MAX_VALUE);
108.147 + }
108.148 +
108.149 + @Compare public long subMinLongAndMinLong() {
108.150 + return sub(Long.MIN_VALUE, Long.MIN_VALUE);
108.151 + }
108.152 +
108.153 + @Compare public long subMinLongAndMaxLong() {
108.154 + return sub(Long.MIN_VALUE, Long.MAX_VALUE);
108.155 + }
108.156 +
108.157 + @Compare public long multiplyMaxLong() {
108.158 + return mul(Long.MAX_VALUE, 2l);
108.159 + }
108.160 +
108.161 + @Compare public long multiplyMaxLongAndMaxLong() {
108.162 + return mul(Long.MAX_VALUE, Long.MAX_VALUE);
108.163 + }
108.164 +
108.165 + @Compare public long multiplyMinLong() {
108.166 + return mul(Long.MIN_VALUE, 2l);
108.167 + }
108.168 +
108.169 + @Compare public long multiplyMinLongAndMinLong() {
108.170 + return mul(Long.MIN_VALUE, Long.MIN_VALUE);
108.171 + }
108.172 +
108.173 + @Compare public long multiplyPrecision() {
108.174 + return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
108.175 + }
108.176 +
108.177 + @Compare public long divideSmallPositiveNumbers() {
108.178 + return div(0xabcdef, 0x123);
108.179 + }
108.180 +
108.181 + @Compare public long divideSmallNegativeNumbers() {
108.182 + return div(-0xabcdef, -0x123);
108.183 + }
108.184 +
108.185 + @Compare public long divideSmallMixedNumbers() {
108.186 + return div(0xabcdef, -0x123);
108.187 + }
108.188 +
108.189 + @Compare public long dividePositiveNumbersOneDigitDenom() {
108.190 + return div(0xabcdef0102ffffl, 0x654);
108.191 + }
108.192 +
108.193 + @Compare public long divideNegativeNumbersOneDigitDenom() {
108.194 + return div(-0xabcdef0102ffffl, -0x654);
108.195 + }
108.196 +
108.197 + @Compare public long divideMixedNumbersOneDigitDenom() {
108.198 + return div(-0xabcdef0102ffffl, 0x654);
108.199 + }
108.200 +
108.201 + @Compare public long dividePositiveNumbersMultiDigitDenom() {
108.202 + return div(0x7ffefc003322aabbl, 0x89ab1000l);
108.203 + }
108.204 +
108.205 + @Compare public long divideNegativeNumbersMultiDigitDenom() {
108.206 + return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
108.207 + }
108.208 +
108.209 + @Compare public long divideMixedNumbersMultiDigitDenom() {
108.210 + return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
108.211 + }
108.212 +
108.213 + @Compare public long divideWithOverflow() {
108.214 + return div(0x8000fffe0000l, 0x8000ffffl);
108.215 + }
108.216 +
108.217 + @Compare public long divideWithCorrection() {
108.218 + return div(0x7fff800000000000l, 0x800000000001l);
108.219 + }
108.220 +
108.221 + @Compare public long moduloSmallPositiveNumbers() {
108.222 + return mod(0xabcdef, 0x123);
108.223 + }
108.224 +
108.225 + @Compare public long moduloSmallNegativeNumbers() {
108.226 + return mod(-0xabcdef, -0x123);
108.227 + }
108.228 +
108.229 + @Compare public long moduloSmallMixedNumbers() {
108.230 + return mod(0xabcdef, -0x123);
108.231 + }
108.232 +
108.233 + @Compare public long moduloPositiveNumbersOneDigitDenom() {
108.234 + return mod(0xabcdef0102ffffl, 0x654);
108.235 + }
108.236 +
108.237 + @Compare public long moduloNegativeNumbersOneDigitDenom() {
108.238 + return mod(-0xabcdef0102ffffl, -0x654);
108.239 + }
108.240 +
108.241 + @Compare public long moduloMixedNumbersOneDigitDenom() {
108.242 + return mod(-0xabcdef0102ffffl, 0x654);
108.243 + }
108.244 +
108.245 + @Compare public long moduloPositiveNumbersMultiDigitDenom() {
108.246 + return mod(0x7ffefc003322aabbl, 0x89ab1000l);
108.247 + }
108.248 +
108.249 + @Compare public long moduloNegativeNumbersMultiDigitDenom() {
108.250 + return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
108.251 + }
108.252 +
108.253 + @Compare public long moduloMixedNumbersMultiDigitDenom() {
108.254 + return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
108.255 + }
108.256 +
108.257 + @Compare public long moduloWithOverflow() {
108.258 + return mod(0x8000fffe0000l, 0x8000ffffl);
108.259 + }
108.260 +
108.261 + @Compare public long moduloWithCorrection() {
108.262 + return mod(0x7fff800000000000l, 0x800000000001l);
108.263 + }
108.264 +
108.265 + @Compare public long conversionFromFloatPositive() {
108.266 + return (long) fadd(2, 0.6f);
108.267 + }
108.268 +
108.269 + @Compare public long conversionFromFloatNegative() {
108.270 + return (long) fadd(-2, -0.6f);
108.271 + }
108.272 +
108.273 + @Compare public long conversionFromDoublePositive() {
108.274 + return (long) dadd(0x20ffff0000L, 0.6);
108.275 + }
108.276 +
108.277 + @Compare public long conversionFromDoubleNegative() {
108.278 + return (long) dadd(-0x20ffff0000L, -0.6);
108.279 + }
108.280 +
108.281 + @Compare public boolean divByZeroThrowsArithmeticException() {
108.282 + try {
108.283 + div(1, 0);
108.284 + return false;
108.285 + } catch (final ArithmeticException e) {
108.286 + return true;
108.287 + }
108.288 + }
108.289 +
108.290 + @Compare public boolean modByZeroThrowsArithmeticException() {
108.291 + try {
108.292 + mod(1, 0);
108.293 + return false;
108.294 + } catch (final ArithmeticException e) {
108.295 + return true;
108.296 + }
108.297 + }
108.298 +
108.299 + @Compare public long shiftL1() {
108.300 + return shl(0x00fa37d7763e0ca1l, 5);
108.301 + }
108.302 +
108.303 + @Compare public long shiftL2() {
108.304 + return shl(0x00fa37d7763e0ca1l, 32);
108.305 + }
108.306 +
108.307 + @Compare public long shiftL3() {
108.308 + return shl(0x00fa37d7763e0ca1l, 45);
108.309 + }
108.310 +
108.311 + @Compare public long shiftR1() {
108.312 + return shr(0x00fa37d7763e0ca1l, 5);
108.313 + }
108.314 +
108.315 + @Compare public long shiftR2() {
108.316 + return shr(0x00fa37d7763e0ca1l, 32);
108.317 + }
108.318 +
108.319 + @Compare public long shiftR3() {
108.320 + return shr(0x00fa37d7763e0ca1l, 45);
108.321 + }
108.322 +
108.323 + @Compare public long uShiftR1() {
108.324 + return ushr(0x00fa37d7763e0ca1l, 5);
108.325 + }
108.326 +
108.327 + @Compare public long uShiftR2() {
108.328 + return ushr(0x00fa37d7763e0ca1l, 45);
108.329 + }
108.330 +
108.331 + @Compare public long uShiftR3() {
108.332 + return ushr(0xf0fa37d7763e0ca1l, 5);
108.333 + }
108.334 +
108.335 + @Compare public long uShiftR4() {
108.336 + return ushr(0xf0fa37d7763e0ca1l, 45);
108.337 + }
108.338 +
108.339 + @Compare public long and1() {
108.340 + return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
108.341 + }
108.342 +
108.343 + @Compare public long or1() {
108.344 + return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
108.345 + }
108.346 +
108.347 + @Compare public long xor1() {
108.348 + return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
108.349 + }
108.350 +
108.351 + @Compare public long xor2() {
108.352 + return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
108.353 + }
108.354 +
108.355 + @Compare public long xor3() {
108.356 + return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
108.357 + }
108.358 +
108.359 + @Compare public int compareSameNumbers() {
108.360 + return compare(0x0000000000000000l, 0x0000000000000000l, 0);
108.361 + }
108.362 +
108.363 + @Compare public int comparePositiveNumbers() {
108.364 + return compare(0x0000000000200000l, 0x0000000010000000l, 0);
108.365 + }
108.366 +
108.367 + @Compare public int compareNegativeNumbers() {
108.368 + return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
108.369 + }
108.370 +
108.371 + @Compare public int compareMixedNumbers() {
108.372 + return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
108.373 + }
108.374 +
108.375 + @Factory
108.376 + public static Object[] create() {
108.377 + return VMTest.create(LongArithmeticTest.class);
108.378 + }
108.379 +}
109.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
109.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionArrayTest.java Fri May 17 10:27:41 2013 +0200
109.3 @@ -0,0 +1,161 @@
109.4 +/**
109.5 + * Back 2 Browser Bytecode Translator
109.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
109.7 + *
109.8 + * This program is free software: you can redistribute it and/or modify
109.9 + * it under the terms of the GNU General Public License as published by
109.10 + * the Free Software Foundation, version 2 of the License.
109.11 + *
109.12 + * This program is distributed in the hope that it will be useful,
109.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
109.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
109.15 + * GNU General Public License for more details.
109.16 + *
109.17 + * You should have received a copy of the GNU General Public License
109.18 + * along with this program. Look for COPYING file in the top folder.
109.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
109.20 + */
109.21 +package org.apidesign.bck2brwsr.tck;
109.22 +
109.23 +import java.lang.reflect.Array;
109.24 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
109.25 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
109.26 +import org.apidesign.bck2brwsr.vmtest.Compare;
109.27 +import org.apidesign.bck2brwsr.vmtest.VMTest;
109.28 +import org.testng.annotations.Factory;
109.29 +
109.30 +/**
109.31 + *
109.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
109.33 + */
109.34 +public class ReflectionArrayTest {
109.35 + @Compare public int lengthOfStringArray() {
109.36 + String[] arr = (String[]) Array.newInstance(String.class, 10);
109.37 + return arr.length;
109.38 + }
109.39 +
109.40 + @Compare public int reflectiveLengthOfStringArray() {
109.41 + Object arr = Array.newInstance(String.class, 10);
109.42 + return Array.getLength(arr);
109.43 + }
109.44 +
109.45 + @Compare public int reflectiveLengthOneNonArray() {
109.46 + Object arr = "non-array";
109.47 + return Array.getLength(arr);
109.48 + }
109.49 +
109.50 + @Compare public String compTypeOfStringArray() {
109.51 + String[] arr = (String[]) Array.newInstance(String.class, 10);
109.52 + return arr.getClass().getComponentType().getName();
109.53 + }
109.54 +
109.55 + @Compare public Object negativeArrayExcp() {
109.56 + return Array.newInstance(String.class, -5);
109.57 + }
109.58 +
109.59 + @Compare public int lengthOfIntArray() {
109.60 + int[] arr = (int[]) Array.newInstance(Integer.TYPE, 10);
109.61 + return arr.length;
109.62 + }
109.63 +
109.64 + @Compare public int reflectiveLengthOfIntArray() {
109.65 + Object arr = Array.newInstance(Integer.TYPE, 10);
109.66 + return Array.getLength(arr);
109.67 + }
109.68 +
109.69 + @Compare public String compTypeOfIntArray() {
109.70 + int[] arr = (int[]) Array.newInstance(int.class, 10);
109.71 + return arr.getClass().getComponentType().getName();
109.72 + }
109.73 +
109.74 + @Compare public Object intNegativeArrayExcp() {
109.75 + return Array.newInstance(int.class, -5);
109.76 + }
109.77 +
109.78 + @Compare public Integer verifyAutobox() {
109.79 + int[] arr = (int[]) Array.newInstance(int.class, 5);
109.80 + return (Integer) Array.get(arr, 0);
109.81 + }
109.82 + @Compare public String verifyObjectArray() {
109.83 + String[] arr = (String[]) Array.newInstance(String.class, 5);
109.84 + Array.set(arr, 0, "Hello");
109.85 + return (String) Array.get(arr, 0);
109.86 + }
109.87 + @Compare public int verifyInt() {
109.88 + int[] arr = (int[]) Array.newInstance(int.class, 5);
109.89 + return Array.getInt(arr, 0);
109.90 + }
109.91 + @Compare public long verifyConvertToLong() {
109.92 + int[] arr = (int[]) Array.newInstance(int.class, 5);
109.93 + return Array.getLong(arr, 0);
109.94 + }
109.95 +
109.96 + @Compare public Object verifySetIntToObject() {
109.97 + try {
109.98 + Object[] arr = (Object[]) Array.newInstance(Object.class, 5);
109.99 + Array.setInt(arr, 0, 10);
109.100 + return Array.get(arr, 0);
109.101 + } catch (Exception exception) {
109.102 + return exception.getClass().getName();
109.103 + }
109.104 + }
109.105 + @Compare public long verifySetShort() {
109.106 + int[] arr = (int[]) Array.newInstance(int.class, 5);
109.107 + Array.setShort(arr, 0, (short)10);
109.108 + return Array.getLong(arr, 0);
109.109 + }
109.110 + @Compare public long verifyCantSetLong() {
109.111 + int[] arr = (int[]) Array.newInstance(int.class, 5);
109.112 + Array.setLong(arr, 0, 10);
109.113 + return Array.getLong(arr, 0);
109.114 + }
109.115 + @Compare public float verifyLongToFloat() {
109.116 + Object arr = Array.newInstance(float.class, 5);
109.117 + Array.setLong(arr, 0, 10);
109.118 + return Array.getFloat(arr, 0);
109.119 + }
109.120 +
109.121 + @Compare public double verifyConvertToDouble() {
109.122 + int[] arr = (int[]) Array.newInstance(int.class, 5);
109.123 + return Array.getDouble(arr, 0);
109.124 + }
109.125 +
109.126 + @Compare public int multiIntArray() {
109.127 + int[][][] arr = (int[][][]) Array.newInstance(int.class, 3, 3, 3);
109.128 + return arr[0][1][2] + 5 + arr[2][2][0];
109.129 + }
109.130 +
109.131 + @Compare public String multiIntArrayCompType() {
109.132 + return Array.newInstance(int.class, 3, 3, 3).getClass().getName();
109.133 + }
109.134 +
109.135 + @JavaScriptBody(args = {}, body = "return [1, 2];")
109.136 + private static native Object crtarr();
109.137 +
109.138 + @JavaScriptBody(args = {}, body = "return new Object();")
109.139 + private static native Object newobj();
109.140 +
109.141 + @BrwsrTest
109.142 + public static void toStringArray() {
109.143 + final Object arr = crtarr();
109.144 + final Object real = new Object[2];
109.145 + assert arr instanceof Object[] : "Any array is Java array: " + arr;
109.146 + assert arr.getClass() == real.getClass() : "Same classes " + arr + " and " + real.getClass();
109.147 + final String str = arr.toString();
109.148 + assert str != null;
109.149 + assert str.startsWith("[Ljava.lang.Object;@") : str;
109.150 + }
109.151 +
109.152 + @BrwsrTest
109.153 + public static void objectToString() {
109.154 + String s = newobj().toString();
109.155 + assert s != null : "Some string computed";
109.156 + assert s.startsWith("java.lang.Object@") : "Regular object toString(): " + s;
109.157 + }
109.158 +
109.159 +
109.160 + @Factory
109.161 + public static Object[] create() {
109.162 + return VMTest.create(ReflectionArrayTest.class);
109.163 + }
109.164 +}
110.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
110.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java Fri May 17 10:27:41 2013 +0200
110.3 @@ -0,0 +1,272 @@
110.4 +/**
110.5 + * Back 2 Browser Bytecode Translator
110.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
110.7 + *
110.8 + * This program is free software: you can redistribute it and/or modify
110.9 + * it under the terms of the GNU General Public License as published by
110.10 + * the Free Software Foundation, version 2 of the License.
110.11 + *
110.12 + * This program is distributed in the hope that it will be useful,
110.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
110.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
110.15 + * GNU General Public License for more details.
110.16 + *
110.17 + * You should have received a copy of the GNU General Public License
110.18 + * along with this program. Look for COPYING file in the top folder.
110.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
110.20 + */
110.21 +package org.apidesign.bck2brwsr.tck;
110.22 +
110.23 +import java.lang.annotation.Retention;
110.24 +import java.lang.annotation.RetentionPolicy;
110.25 +import java.lang.reflect.Method;
110.26 +import java.util.Arrays;
110.27 +import java.util.Collections;
110.28 +import java.util.List;
110.29 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
110.30 +import org.apidesign.bck2brwsr.vmtest.Compare;
110.31 +import org.apidesign.bck2brwsr.vmtest.VMTest;
110.32 +import org.testng.annotations.Factory;
110.33 +
110.34 +/**
110.35 + *
110.36 + * @author Jaroslav Tulach <jtulach@netbeans.org>
110.37 + */
110.38 +public class ReflectionTest {
110.39 + @Compare public boolean nonNullThis() {
110.40 + return this == null;
110.41 + }
110.42 +
110.43 + @Compare public String intType() {
110.44 + return Integer.TYPE.toString();
110.45 + }
110.46 +
110.47 + @Compare public String voidType() throws Exception {
110.48 + return void.class.toString();
110.49 + }
110.50 +
110.51 + @Compare public String longClass() {
110.52 + return long.class.toString();
110.53 + }
110.54 +
110.55 + @Compare public boolean isRunnableInterface() {
110.56 + return Runnable.class.isInterface();
110.57 + }
110.58 +
110.59 + @Compare public boolean isAssignableToPrimitiveType() {
110.60 + return boolean.class.isAssignableFrom(Runnable.class);
110.61 + }
110.62 +
110.63 + @Compare public boolean isAssignableFromPrimitiveType() {
110.64 + return Runnable.class.isAssignableFrom(boolean.class);
110.65 + }
110.66 +
110.67 + @Compare public boolean isAssignableLongFromInt() {
110.68 + return long.class.isAssignableFrom(int.class);
110.69 + }
110.70 +
110.71 + @Compare public boolean isAssignableIntFromLong() {
110.72 + return int.class.isAssignableFrom(long.class);
110.73 + }
110.74 +
110.75 + @Compare public String isRunnableHasRunMethod() throws NoSuchMethodException {
110.76 + return Runnable.class.getMethod("run").getName();
110.77 + }
110.78 +
110.79 + @Compare public String namesOfMethods() {
110.80 + StringBuilder sb = new StringBuilder();
110.81 + String[] arr = new String[20];
110.82 + int i = 0;
110.83 + for (Method m : StaticUse.class.getMethods()) {
110.84 + arr[i++] = m.getName();
110.85 + }
110.86 + for (String s : sort(arr, i)) {
110.87 + sb.append(s).append("\n");
110.88 + }
110.89 + return sb.toString();
110.90 + }
110.91 +
110.92 + @Compare public String namesOfDeclaringClassesOfMethods() {
110.93 + StringBuilder sb = new StringBuilder();
110.94 + String[] arr = new String[20];
110.95 + int i = 0;
110.96 + for (Method m : StaticUse.class.getMethods()) {
110.97 + arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
110.98 + }
110.99 + for (String s : sort(arr, i)) {
110.100 + sb.append(s).append("\n");
110.101 + }
110.102 + return sb.toString();
110.103 + }
110.104 +
110.105 + @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
110.106 + StaticUse.class.getMethod("instanceMethod").invoke(null);
110.107 + return "should not happen";
110.108 + }
110.109 +
110.110 + @Compare public String classCastException() {
110.111 + try {
110.112 + Integer i = (Integer)StaticUseSub.getNonNull();
110.113 + return "" + i.intValue();
110.114 + } catch (ClassCastException ex) {
110.115 + return ex.getClass().getName();
110.116 + }
110.117 + }
110.118 +
110.119 + @Compare public String methodThatThrowsException() throws Exception {
110.120 + StaticUse.class.getMethod("instanceMethod").invoke(new StaticUse());
110.121 + return "should not happen";
110.122 + }
110.123 +
110.124 + @Compare public Object voidReturnType() throws Exception {
110.125 + return StaticUse.class.getMethod("instanceMethod").getReturnType();
110.126 + }
110.127 +
110.128 + @Retention(RetentionPolicy.RUNTIME)
110.129 + @interface Ann {
110.130 + }
110.131 +
110.132 + @Compare public String annoClass() throws Exception {
110.133 + Retention r = Ann.class.getAnnotation(Retention.class);
110.134 + assert r != null : "Annotation is present";
110.135 + assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
110.136 + return r.annotationType().getName();
110.137 + }
110.138 +
110.139 + @Compare public boolean isAnnotation() {
110.140 + return Ann.class.isAnnotation();
110.141 + }
110.142 + @Compare public boolean isNotAnnotation() {
110.143 + return String.class.isAnnotation();
110.144 + }
110.145 + @Compare public boolean isNotAnnotationEnum() {
110.146 + return E.class.isAnnotation();
110.147 + }
110.148 + enum E { A, B };
110.149 + @Compare public boolean isEnum() {
110.150 + return E.A.getClass().isEnum();
110.151 + }
110.152 +
110.153 + @Compare public boolean isNotEnum() {
110.154 + return "".getClass().isEnum();
110.155 + }
110.156 +
110.157 + @Compare public String newInstanceFails() throws InstantiationException {
110.158 + try {
110.159 + return "success: " + StaticUseSub.class.newInstance();
110.160 + } catch (IllegalAccessException ex) {
110.161 + return ex.getClass().getName();
110.162 + }
110.163 + }
110.164 +
110.165 + @Compare public String paramTypes() throws Exception {
110.166 + Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
110.167 + final Class[] pt = plus.getParameterTypes();
110.168 + return pt[0].getName();
110.169 + }
110.170 + @Compare public String paramTypesNotFound() throws Exception {
110.171 + return StaticUse.class.getMethod("plus", int.class, double.class).toString();
110.172 + }
110.173 + @Compare public int methodWithArgs() throws Exception {
110.174 + Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
110.175 + return (Integer)plus.invoke(null, 2, 3);
110.176 + }
110.177 +
110.178 + @Compare public String classGetNameForByte() {
110.179 + return byte.class.getName();
110.180 + }
110.181 + @Compare public String classGetNameForBaseObject() {
110.182 + return newObject().getClass().getName();
110.183 + }
110.184 + @Compare public String classGetNameForJavaObject() {
110.185 + return new Object().getClass().getName();
110.186 + }
110.187 + @Compare public String classGetNameForObjectArray() {
110.188 + return (new Object[3]).getClass().getName();
110.189 + }
110.190 + @Compare public String classGetNameForSimpleIntArray() {
110.191 + return (new int[3]).getClass().getName();
110.192 + }
110.193 + @Compare public boolean sameClassGetNameForSimpleCharArray() {
110.194 + return (new char[3]).getClass() == (new char[34]).getClass();
110.195 + }
110.196 + @Compare public String classGetNameForMultiIntArray() {
110.197 + return (new int[3][4][5][6][7][8][9]).getClass().getName();
110.198 + }
110.199 + @Compare public String classGetNameForMultiIntArrayInner() {
110.200 + final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
110.201 + int[][][][][][] subarr = arr[0];
110.202 + int[][][][][] subsubarr = subarr[0];
110.203 + return subsubarr.getClass().getName();
110.204 + }
110.205 + @Compare public String classGetNameForMultiStringArray() {
110.206 + return (new String[3][4][5][6][7][8][9]).getClass().getName();
110.207 + }
110.208 +
110.209 + @Compare public String classForByte() throws Exception {
110.210 + return Class.forName("[Z").getName();
110.211 + }
110.212 +
110.213 + @Compare public String classForUnknownArray() {
110.214 + try {
110.215 + return Class.forName("[W").getName();
110.216 + } catch (Exception ex) {
110.217 + return ex.getClass().getName();
110.218 + }
110.219 + }
110.220 +
110.221 + @Compare public String classForUnknownDeepArray() {
110.222 + try {
110.223 + return Class.forName("[[[[[W").getName();
110.224 + } catch (Exception ex) {
110.225 + return ex.getClass().getName();
110.226 + }
110.227 + }
110.228 +
110.229 + @Compare public String componentGetNameForObjectArray() {
110.230 + return (new Object[3]).getClass().getComponentType().getName();
110.231 + }
110.232 + @Compare public boolean sameComponentGetNameForObjectArray() {
110.233 + return (new Object[3]).getClass().getComponentType() == Object.class;
110.234 + }
110.235 + @Compare public String componentGetNameForSimpleIntArray() {
110.236 + return (new int[3]).getClass().getComponentType().getName();
110.237 + }
110.238 + @Compare public String componentGetNameForMultiIntArray() {
110.239 + return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
110.240 + }
110.241 + @Compare public String componentGetNameForMultiStringArray() {
110.242 + Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
110.243 + StringBuilder sb = new StringBuilder();
110.244 + for (;;) {
110.245 + sb.append(c.getName()).append("\n");
110.246 + c = c.getComponentType();
110.247 + if (c == null) {
110.248 + break;
110.249 + }
110.250 + }
110.251 + return sb.toString();
110.252 + }
110.253 +
110.254 + @Compare public boolean isArray() {
110.255 + return new Object[0].getClass().isArray();
110.256 + }
110.257 +
110.258 + @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
110.259 + private static String[] sort(String[] arr, int len) {
110.260 + List<String> list = Arrays.asList(arr).subList(0, len);
110.261 + Collections.sort(list);
110.262 + return list.toArray(new String[0]);
110.263 + }
110.264 +
110.265 + @JavaScriptBody(args = {}, body = "return new Object();")
110.266 + private static Object newObject() {
110.267 + return new Object();
110.268 + }
110.269 +
110.270 + @Factory
110.271 + public static Object[] create() {
110.272 + return VMTest.create(ReflectionTest.class);
110.273 + }
110.274 +
110.275 +}
111.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
111.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ResourcesTest.java Fri May 17 10:27:41 2013 +0200
111.3 @@ -0,0 +1,45 @@
111.4 +/**
111.5 + * Back 2 Browser Bytecode Translator
111.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
111.7 + *
111.8 + * This program is free software: you can redistribute it and/or modify
111.9 + * it under the terms of the GNU General Public License as published by
111.10 + * the Free Software Foundation, version 2 of the License.
111.11 + *
111.12 + * This program is distributed in the hope that it will be useful,
111.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
111.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
111.15 + * GNU General Public License for more details.
111.16 + *
111.17 + * You should have received a copy of the GNU General Public License
111.18 + * along with this program. Look for COPYING file in the top folder.
111.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
111.20 + */
111.21 +package org.apidesign.bck2brwsr.tck;
111.22 +
111.23 +import java.io.InputStream;
111.24 +import org.apidesign.bck2brwsr.vmtest.Compare;
111.25 +import org.apidesign.bck2brwsr.vmtest.VMTest;
111.26 +import org.testng.annotations.Factory;
111.27 +
111.28 +/**
111.29 + *
111.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
111.31 + */
111.32 +public class ResourcesTest {
111.33 +
111.34 + @Compare public String readResourceAsStream() throws Exception {
111.35 + InputStream is = getClass().getResourceAsStream("Resources.txt");
111.36 + byte[] b = new byte[30];
111.37 + int len = is.read(b);
111.38 + StringBuilder sb = new StringBuilder();
111.39 + for (int i = 0; i < len; i++) {
111.40 + sb.append((char)b[i]);
111.41 + }
111.42 + return sb.toString();
111.43 + }
111.44 +
111.45 + @Factory public static Object[] create() {
111.46 + return VMTest.create(ResourcesTest.class);
111.47 + }
111.48 +}
112.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
112.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ShortArithmeticTest.java Fri May 17 10:27:41 2013 +0200
112.3 @@ -0,0 +1,102 @@
112.4 +/**
112.5 + * Back 2 Browser Bytecode Translator
112.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
112.7 + *
112.8 + * This program is free software: you can redistribute it and/or modify
112.9 + * it under the terms of the GNU General Public License as published by
112.10 + * the Free Software Foundation, version 2 of the License.
112.11 + *
112.12 + * This program is distributed in the hope that it will be useful,
112.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
112.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
112.15 + * GNU General Public License for more details.
112.16 + *
112.17 + * You should have received a copy of the GNU General Public License
112.18 + * along with this program. Look for COPYING file in the top folder.
112.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
112.20 + */
112.21 +package org.apidesign.bck2brwsr.tck;
112.22 +
112.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
112.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
112.25 +import org.testng.annotations.Factory;
112.26 +
112.27 +/**
112.28 + *
112.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
112.30 + */
112.31 +public class ShortArithmeticTest {
112.32 +
112.33 + private static short add(short x, short y) {
112.34 + return (short)(x + y);
112.35 + }
112.36 +
112.37 + private static short sub(short x, short y) {
112.38 + return (short)(x - y);
112.39 + }
112.40 +
112.41 + private static short mul(short x, short y) {
112.42 + return (short)(x * y);
112.43 + }
112.44 +
112.45 + private static short div(short x, short y) {
112.46 + return (short)(x / y);
112.47 + }
112.48 +
112.49 + private static short mod(short x, short y) {
112.50 + return (short)(x % y);
112.51 + }
112.52 +
112.53 + @Compare public short conversion() {
112.54 + return (short)123456;
112.55 + }
112.56 +
112.57 + @Compare public short addOverflow() {
112.58 + return add(Short.MAX_VALUE, (short)1);
112.59 + }
112.60 +
112.61 + @Compare public short subUnderflow() {
112.62 + return sub(Short.MIN_VALUE, (short)1);
112.63 + }
112.64 +
112.65 + @Compare public short addMaxShortAndMaxShort() {
112.66 + return add(Short.MAX_VALUE, Short.MAX_VALUE);
112.67 + }
112.68 +
112.69 + @Compare public short subMinShortAndMinShort() {
112.70 + return sub(Short.MIN_VALUE, Short.MIN_VALUE);
112.71 + }
112.72 +
112.73 + @Compare public short multiplyMaxShort() {
112.74 + return mul(Short.MAX_VALUE, (short)2);
112.75 + }
112.76 +
112.77 + @Compare public short multiplyMaxShortAndMaxShort() {
112.78 + return mul(Short.MAX_VALUE, Short.MAX_VALUE);
112.79 + }
112.80 +
112.81 + @Compare public short multiplyMinShort() {
112.82 + return mul(Short.MIN_VALUE, (short)2);
112.83 + }
112.84 +
112.85 + @Compare public short multiplyMinShortAndMinShort() {
112.86 + return mul(Short.MIN_VALUE, Short.MIN_VALUE);
112.87 + }
112.88 +
112.89 + @Compare public short multiplyPrecision() {
112.90 + return mul((short)17638, (short)1103);
112.91 + }
112.92 +
112.93 + @Compare public short division() {
112.94 + return div((short)1, (short)2);
112.95 + }
112.96 +
112.97 + @Compare public short divisionReminder() {
112.98 + return mod((short)1, (short)2);
112.99 + }
112.100 +
112.101 + @Factory
112.102 + public static Object[] create() {
112.103 + return VMTest.create(ShortArithmeticTest.class);
112.104 + }
112.105 +}
113.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
113.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/StaticUse.java Fri May 17 10:27:41 2013 +0200
113.3 @@ -0,0 +1,39 @@
113.4 +/**
113.5 + * Back 2 Browser Bytecode Translator
113.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
113.7 + *
113.8 + * This program is free software: you can redistribute it and/or modify
113.9 + * it under the terms of the GNU General Public License as published by
113.10 + * the Free Software Foundation, version 2 of the License.
113.11 + *
113.12 + * This program is distributed in the hope that it will be useful,
113.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
113.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
113.15 + * GNU General Public License for more details.
113.16 + *
113.17 + * You should have received a copy of the GNU General Public License
113.18 + * along with this program. Look for COPYING file in the top folder.
113.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
113.20 + */
113.21 +package org.apidesign.bck2brwsr.tck;
113.22 +
113.23 +class StaticUse {
113.24 + public static final Object NON_NULL = new Object();
113.25 + public static int cnt;
113.26 + static {
113.27 + if (cnt++ != 0) {
113.28 + throw new IllegalStateException("Multiple initialization of a <cinit>");
113.29 + }
113.30 + }
113.31 +
113.32 + StaticUse() {
113.33 + }
113.34 +
113.35 + public void instanceMethod() {
113.36 + throw new IllegalStateException();
113.37 + }
113.38 +
113.39 + public static int plus(int a, int b) {
113.40 + return a + b;
113.41 + }
113.42 +}
114.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
114.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/StaticUseSub.java Fri May 17 10:27:41 2013 +0200
114.3 @@ -0,0 +1,30 @@
114.4 +/**
114.5 + * Back 2 Browser Bytecode Translator
114.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
114.7 + *
114.8 + * This program is free software: you can redistribute it and/or modify
114.9 + * it under the terms of the GNU General Public License as published by
114.10 + * the Free Software Foundation, version 2 of the License.
114.11 + *
114.12 + * This program is distributed in the hope that it will be useful,
114.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
114.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
114.15 + * GNU General Public License for more details.
114.16 + *
114.17 + * You should have received a copy of the GNU General Public License
114.18 + * along with this program. Look for COPYING file in the top folder.
114.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
114.20 + */
114.21 +package org.apidesign.bck2brwsr.tck;
114.22 +
114.23 +public class StaticUseSub extends StaticUse {
114.24 + private StaticUseSub() {
114.25 + }
114.26 +
114.27 + public static Object getNonNull() {
114.28 + return NON_NULL;
114.29 + }
114.30 + static Object getNull() {
114.31 + return null;
114.32 + }
114.33 +}
115.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
115.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/StaticUseSubTest.java Fri May 17 10:27:41 2013 +0200
115.3 @@ -0,0 +1,45 @@
115.4 +/**
115.5 + * Back 2 Browser Bytecode Translator
115.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
115.7 + *
115.8 + * This program is free software: you can redistribute it and/or modify
115.9 + * it under the terms of the GNU General Public License as published by
115.10 + * the Free Software Foundation, version 2 of the License.
115.11 + *
115.12 + * This program is distributed in the hope that it will be useful,
115.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
115.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
115.15 + * GNU General Public License for more details.
115.16 + *
115.17 + * You should have received a copy of the GNU General Public License
115.18 + * along with this program. Look for COPYING file in the top folder.
115.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
115.20 + */
115.21 +package org.apidesign.bck2brwsr.tck;
115.22 +
115.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
115.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
115.25 +import org.testng.annotations.Factory;
115.26 +
115.27 +/**
115.28 + *
115.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
115.30 + */
115.31 +public class StaticUseSubTest {
115.32 + @Compare public String staticFieldInitializationInSuperClass() throws Exception {
115.33 + Object ret = StaticUseSub.getNonNull();
115.34 + return ret.getClass().getName();
115.35 + }
115.36 +
115.37 + @Compare public String isNullPointerTheSame() throws Exception {
115.38 + try {
115.39 + return StaticUseSub.getNull().getClass().toString();
115.40 + } catch (NullPointerException ex) {
115.41 + return ex.getClass().getName();
115.42 + }
115.43 + }
115.44 +
115.45 + @Factory public static Object[] create() {
115.46 + return VMTest.create(StaticUseSubTest.class);
115.47 + }
115.48 +}
116.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
116.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/CRC32Test.java Fri May 17 10:27:41 2013 +0200
116.3 @@ -0,0 +1,41 @@
116.4 +/**
116.5 + * Back 2 Browser Bytecode Translator
116.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
116.7 + *
116.8 + * This program is free software: you can redistribute it and/or modify
116.9 + * it under the terms of the GNU General Public License as published by
116.10 + * the Free Software Foundation, version 2 of the License.
116.11 + *
116.12 + * This program is distributed in the hope that it will be useful,
116.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
116.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
116.15 + * GNU General Public License for more details.
116.16 + *
116.17 + * You should have received a copy of the GNU General Public License
116.18 + * along with this program. Look for COPYING file in the top folder.
116.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
116.20 + */
116.21 +package org.apidesign.bck2brwsr.vmtest.impl;
116.22 +
116.23 +import java.io.UnsupportedEncodingException;
116.24 +import java.util.zip.CRC32;
116.25 +import org.apidesign.bck2brwsr.vmtest.Compare;
116.26 +import org.apidesign.bck2brwsr.vmtest.VMTest;
116.27 +import org.testng.annotations.Factory;
116.28 +
116.29 +/**
116.30 + *
116.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
116.32 + */
116.33 +public class CRC32Test {
116.34 +
116.35 + @Compare public long crc1() throws UnsupportedEncodingException {
116.36 + CRC32 crc = new CRC32();
116.37 + crc.update("Hello World!".getBytes("UTF-8"));
116.38 + return crc.getValue();
116.39 + }
116.40 +
116.41 + @Factory public static Object[] create() {
116.42 + return VMTest.create(CRC32Test.class);
116.43 + }
116.44 +}
117.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
117.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/CallMeTwiceTest.java Fri May 17 10:27:41 2013 +0200
117.3 @@ -0,0 +1,43 @@
117.4 +/**
117.5 + * Back 2 Browser Bytecode Translator
117.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
117.7 + *
117.8 + * This program is free software: you can redistribute it and/or modify
117.9 + * it under the terms of the GNU General Public License as published by
117.10 + * the Free Software Foundation, version 2 of the License.
117.11 + *
117.12 + * This program is distributed in the hope that it will be useful,
117.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
117.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
117.15 + * GNU General Public License for more details.
117.16 + *
117.17 + * You should have received a copy of the GNU General Public License
117.18 + * along with this program. Look for COPYING file in the top folder.
117.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
117.20 + */
117.21 +package org.apidesign.bck2brwsr.vmtest.impl;
117.22 +
117.23 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
117.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
117.25 +import org.testng.annotations.Factory;
117.26 +
117.27 +/**
117.28 + *
117.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
117.30 + */
117.31 +public class CallMeTwiceTest {
117.32 + int cnt;
117.33 +
117.34 + @BrwsrTest public void callMeTwice() throws InterruptedException {
117.35 + if (cnt++ == 0) {
117.36 + throw new InterruptedException();
117.37 + }
117.38 + int prevCnt = cnt;
117.39 + cnt = 0;
117.40 + assert prevCnt == 2 : "We need to receive two calls " + prevCnt;
117.41 + }
117.42 +
117.43 + @Factory public static Object[] create() {
117.44 + return VMTest.create(CallMeTwiceTest.class);
117.45 + }
117.46 +}
118.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
118.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipEntryTest.java Fri May 17 10:27:41 2013 +0200
118.3 @@ -0,0 +1,67 @@
118.4 +/**
118.5 + * Back 2 Browser Bytecode Translator
118.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
118.7 + *
118.8 + * This program is free software: you can redistribute it and/or modify
118.9 + * it under the terms of the GNU General Public License as published by
118.10 + * the Free Software Foundation, version 2 of the License.
118.11 + *
118.12 + * This program is distributed in the hope that it will be useful,
118.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
118.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
118.15 + * GNU General Public License for more details.
118.16 + *
118.17 + * You should have received a copy of the GNU General Public License
118.18 + * along with this program. Look for COPYING file in the top folder.
118.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
118.20 + */
118.21 +package org.apidesign.bck2brwsr.vmtest.impl;
118.22 +
118.23 +import java.io.ByteArrayInputStream;
118.24 +import java.io.IOException;
118.25 +import java.io.InputStream;
118.26 +import org.apidesign.bck2brwsr.emul.zip.FastJar;
118.27 +import org.testng.annotations.Test;
118.28 +import static org.testng.Assert.*;
118.29 +
118.30 +/**
118.31 + *
118.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
118.33 + */
118.34 +@GenerateZip(name = "five.zip", contents = {
118.35 + "1.txt", "one",
118.36 + "2.txt", "duo",
118.37 + "3.txt", "three",
118.38 + "4.txt", "four",
118.39 + "5.txt", "five"
118.40 +})
118.41 +public class ZipEntryTest {
118.42 + @Test
118.43 + public void readEntriesEffectively() throws IOException {
118.44 + InputStream is = ZipEntryTest.class.getResourceAsStream("five.zip");
118.45 + byte[] arr = new byte[is.available()];
118.46 + int len = is.read(arr);
118.47 + assertEquals(len, arr.length, "Read fully");
118.48 +
118.49 + FastJar fj = new FastJar(arr);
118.50 + FastJar.Entry[] entrs = fj.list();
118.51 +
118.52 + assertEquals(5, entrs.length, "Five entries");
118.53 +
118.54 + for (int i = 1; i <= 5; i++) {
118.55 + FastJar.Entry en = entrs[i - 1];
118.56 + assertEquals(en.name, i + ".txt");
118.57 +// assertEquals(cis.cnt, 0, "Content of the file should be skipped, not read");
118.58 + }
118.59 +
118.60 + assertContent("three", fj.getInputStream(entrs[3 - 1]), "read OK");
118.61 + assertContent("five", fj.getInputStream(entrs[5 - 1]), "read OK");
118.62 + }
118.63 +
118.64 + private static void assertContent(String exp, InputStream is, String msg) throws IOException {
118.65 + byte[] arr = new byte[512];
118.66 + int len = is.read(arr);
118.67 + String s = new String(arr, 0, len);
118.68 + assertEquals(exp, s, msg);
118.69 + }
118.70 +}
119.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
119.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java Fri May 17 10:27:41 2013 +0200
119.3 @@ -0,0 +1,108 @@
119.4 +/**
119.5 + * Back 2 Browser Bytecode Translator
119.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
119.7 + *
119.8 + * This program is free software: you can redistribute it and/or modify
119.9 + * it under the terms of the GNU General Public License as published by
119.10 + * the Free Software Foundation, version 2 of the License.
119.11 + *
119.12 + * This program is distributed in the hope that it will be useful,
119.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
119.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
119.15 + * GNU General Public License for more details.
119.16 + *
119.17 + * You should have received a copy of the GNU General Public License
119.18 + * along with this program. Look for COPYING file in the top folder.
119.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
119.20 + */
119.21 +package org.apidesign.bck2brwsr.vmtest.impl;
119.22 +
119.23 +import java.io.IOException;
119.24 +import java.io.InputStream;
119.25 +import java.util.Objects;
119.26 +import java.util.zip.ZipEntry;
119.27 +import java.util.zip.ZipInputStream;
119.28 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
119.29 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
119.30 +import org.apidesign.bck2brwsr.vmtest.Compare;
119.31 +import org.apidesign.bck2brwsr.vmtest.Http;
119.32 +import org.apidesign.bck2brwsr.vmtest.VMTest;
119.33 +import org.testng.annotations.Factory;
119.34 +
119.35 +/**
119.36 + *
119.37 + * @author Jaroslav Tulach <jtulach@netbeans.org>
119.38 + */
119.39 +@GenerateZip(name = "readAnEntry.zip", contents = {
119.40 + "my/main/file.txt", "Hello World!"
119.41 +})
119.42 +public class ZipFileTest {
119.43 +
119.44 + @Compare public String readAnEntry() throws IOException {
119.45 + InputStream is = ZipFileTest.class.getResourceAsStream("readAnEntry.zip");
119.46 + ZipInputStream zip = new ZipInputStream(is);
119.47 + ZipEntry entry = zip.getNextEntry();
119.48 + assertEquals(entry.getName(), "my/main/file.txt", "Correct entry");
119.49 +
119.50 + byte[] arr = new byte[4096];
119.51 + int len = zip.read(arr);
119.52 +
119.53 + assertEquals(zip.getNextEntry(), null, "No next entry");
119.54 +
119.55 + final String ret = new String(arr, 0, len, "UTF-8");
119.56 + return ret;
119.57 + }
119.58 +
119.59 + @JavaScriptBody(args = { "res", "path" }, body =
119.60 + "var myvm = bck2brwsr.apply(null, path);\n"
119.61 + + "var cls = myvm.loadClass('java.lang.String');\n"
119.62 + + "return cls.getClass__Ljava_lang_Class_2().getResourceAsStream__Ljava_io_InputStream_2Ljava_lang_String_2(res);\n"
119.63 + )
119.64 + private static native Object loadVMResource(String res, String...path);
119.65 +
119.66 + @Http({
119.67 + @Http.Resource(path = "/readAnEntry.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip")
119.68 + })
119.69 + @BrwsrTest public void canVmLoadResourceFromZip() throws IOException {
119.70 + Object res = loadVMResource("/my/main/file.txt", "/readAnEntry.jar");
119.71 + assert res instanceof InputStream : "Got array of bytes: " + res;
119.72 + InputStream is = (InputStream)res;
119.73 +
119.74 + byte[] arr = new byte[4096];
119.75 + int len = is.read(arr);
119.76 +
119.77 + final String ret = new String(arr, 0, len, "UTF-8");
119.78 +
119.79 + assertEquals(ret, "Hello World!", "Can read the bytes");
119.80 + }
119.81 +
119.82 + @GenerateZip(name = "cpattr.zip", contents = {
119.83 + "META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n"
119.84 + + "Created-By: hand\n"
119.85 + + "Class-Path: realJar.jar\n\n\n"
119.86 + })
119.87 + @Http({
119.88 + @Http.Resource(path = "/readComplexEntry.jar", mimeType = "x-application/zip", content = "", resource="cpattr.zip"),
119.89 + @Http.Resource(path = "/realJar.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip"),
119.90 + })
119.91 + @BrwsrTest public void understandsClassPathAttr() throws IOException {
119.92 + Object res = loadVMResource("/my/main/file.txt", "/readComplexEntry.jar");
119.93 + assert res instanceof InputStream : "Got array of bytes: " + res;
119.94 + InputStream is = (InputStream)res;
119.95 +
119.96 + byte[] arr = new byte[4096];
119.97 + int len = is.read(arr);
119.98 +
119.99 + final String ret = new String(arr, 0, len, "UTF-8");
119.100 +
119.101 + assertEquals(ret, "Hello World!", "Can read the bytes from secondary JAR");
119.102 + }
119.103 +
119.104 + private static void assertEquals(Object real, Object exp, String msg) {
119.105 + assert Objects.equals(exp, real) : msg + " exp: " + exp + " real: " + real;
119.106 + }
119.107 +
119.108 + @Factory public static Object[] create() {
119.109 + return VMTest.create(ZipFileTest.class);
119.110 + }
119.111 +}
120.1 Binary file rt/emul/compact/src/test/resources/org/apidesign/bck2brwsr/compact/tck/demo.static.calculator-0.3-SNAPSHOT.jar has changed
121.1 Binary file rt/emul/compact/src/test/resources/org/apidesign/bck2brwsr/compact/tck/demo.static.calculator-TEST.jar has changed
122.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
122.2 +++ b/rt/emul/compact/src/test/resources/org/apidesign/bck2brwsr/tck/0xfe Fri May 17 10:27:41 2013 +0200
122.3 @@ -0,0 +1,1 @@
122.4 +þ
122.5 \ No newline at end of file
123.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
123.2 +++ b/rt/emul/compact/src/test/resources/org/apidesign/bck2brwsr/tck/Resources.txt Fri May 17 10:27:41 2013 +0200
123.3 @@ -0,0 +1,1 @@
123.4 +Ahoj
124.1 --- a/rt/emul/mini/pom.xml Tue Mar 05 07:57:16 2013 +0100
124.2 +++ b/rt/emul/mini/pom.xml Fri May 17 10:27:41 2013 +0200
124.3 @@ -4,11 +4,11 @@
124.4 <parent>
124.5 <groupId>org.apidesign.bck2brwsr</groupId>
124.6 <artifactId>emul.pom</artifactId>
124.7 - <version>0.4-SNAPSHOT</version>
124.8 + <version>0.8-SNAPSHOT</version>
124.9 </parent>
124.10 <groupId>org.apidesign.bck2brwsr</groupId>
124.11 <artifactId>emul.mini</artifactId>
124.12 - <version>0.4-SNAPSHOT</version>
124.13 + <version>0.8-SNAPSHOT</version>
124.14 <name>Minimal API Profile</name>
124.15 <url>http://maven.apache.org</url>
124.16 <properties>
124.17 @@ -18,7 +18,7 @@
124.18 <dependency>
124.19 <groupId>org.apidesign.bck2brwsr</groupId>
124.20 <artifactId>core</artifactId>
124.21 - <version>0.4-SNAPSHOT</version>
124.22 + <version>0.8-SNAPSHOT</version>
124.23 <type>jar</type>
124.24 </dependency>
124.25 <dependency>
124.26 @@ -42,6 +42,28 @@
124.27 <target>1.7</target>
124.28 </configuration>
124.29 </plugin>
124.30 + <plugin>
124.31 + <groupId>org.apache.maven.plugins</groupId>
124.32 + <artifactId>maven-javadoc-plugin</artifactId>
124.33 + <configuration>
124.34 + <excludePackageNames>org.apidesign.bck2brwsr.emul.*</excludePackageNames>
124.35 + <skip>false</skip>
124.36 + </configuration>
124.37 + </plugin>
124.38 + <plugin>
124.39 + <groupId>org.apache.maven.plugins</groupId>
124.40 + <artifactId>maven-source-plugin</artifactId>
124.41 + <version>2.2.1</version>
124.42 + <executions>
124.43 + <execution>
124.44 + <id>prepare-sources</id>
124.45 + <goals>
124.46 + <goal>jar</goal>
124.47 + </goals>
124.48 + <phase>package</phase>
124.49 + </execution>
124.50 + </executions>
124.51 + </plugin>
124.52 </plugins>
124.53 </build>
124.54 </project>
125.1 --- a/rt/emul/mini/src/main/java/java/lang/AbstractStringBuilder.java Tue Mar 05 07:57:16 2013 +0100
125.2 +++ b/rt/emul/mini/src/main/java/java/lang/AbstractStringBuilder.java Fri May 17 10:27:41 2013 +0200
125.3 @@ -126,7 +126,7 @@
125.4 throw new OutOfMemoryError();
125.5 newCapacity = Integer.MAX_VALUE;
125.6 }
125.7 - value = copyOf(value, newCapacity);
125.8 + value = System.expandArray(value, newCapacity);
125.9 }
125.10
125.11 /**
126.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
126.2 +++ b/rt/emul/mini/src/main/java/java/lang/ArithmeticException.java Fri May 17 10:27:41 2013 +0200
126.3 @@ -0,0 +1,61 @@
126.4 +/*
126.5 + * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
126.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
126.7 + *
126.8 + * This code is free software; you can redistribute it and/or modify it
126.9 + * under the terms of the GNU General Public License version 2 only, as
126.10 + * published by the Free Software Foundation. Oracle designates this
126.11 + * particular file as subject to the "Classpath" exception as provided
126.12 + * by Oracle in the LICENSE file that accompanied this code.
126.13 + *
126.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
126.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
126.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
126.17 + * version 2 for more details (a copy is included in the LICENSE file that
126.18 + * accompanied this code).
126.19 + *
126.20 + * You should have received a copy of the GNU General Public License version
126.21 + * 2 along with this work; if not, write to the Free Software Foundation,
126.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
126.23 + *
126.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
126.25 + * or visit www.oracle.com if you need additional information or have any
126.26 + * questions.
126.27 + */
126.28 +
126.29 +package java.lang;
126.30 +
126.31 +/**
126.32 + * Thrown when an exceptional arithmetic condition has occurred. For
126.33 + * example, an integer "divide by zero" throws an
126.34 + * instance of this class.
126.35 + *
126.36 + * {@code ArithmeticException} objects may be constructed by the
126.37 + * virtual machine as if {@linkplain Throwable#Throwable(String,
126.38 + * Throwable, boolean, boolean) suppression were disabled and/or the
126.39 + * stack trace was not writable}.
126.40 + *
126.41 + * @author unascribed
126.42 + * @since JDK1.0
126.43 + */
126.44 +public class ArithmeticException extends RuntimeException {
126.45 + private static final long serialVersionUID = 2256477558314496007L;
126.46 +
126.47 + /**
126.48 + * Constructs an {@code ArithmeticException} with no detail
126.49 + * message.
126.50 + */
126.51 + public ArithmeticException() {
126.52 + super();
126.53 + }
126.54 +
126.55 + /**
126.56 + * Constructs an {@code ArithmeticException} with the specified
126.57 + * detail message.
126.58 + *
126.59 + * @param s the detail message.
126.60 + */
126.61 + public ArithmeticException(String s) {
126.62 + super(s);
126.63 + }
126.64 +}
127.1 --- a/rt/emul/mini/src/main/java/java/lang/Boolean.java Tue Mar 05 07:57:16 2013 +0100
127.2 +++ b/rt/emul/mini/src/main/java/java/lang/Boolean.java Fri May 17 10:27:41 2013 +0200
127.3 @@ -26,6 +26,7 @@
127.4 package java.lang;
127.5
127.6 import org.apidesign.bck2brwsr.core.JavaScriptBody;
127.7 +import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
127.8
127.9 /**
127.10 * The Boolean class wraps a value of the primitive type
127.11 @@ -42,6 +43,7 @@
127.12 * @author Arthur van Hoff
127.13 * @since JDK1.0
127.14 */
127.15 +@JavaScriptPrototype(container = "Boolean.prototype", prototype = "new Boolean")
127.16 public final class Boolean implements java.io.Serializable,
127.17 Comparable<Boolean>
127.18 {
127.19 @@ -129,6 +131,7 @@
127.20 *
127.21 * @return the primitive {@code boolean} value of this object.
127.22 */
127.23 + @JavaScriptBody(args = {}, body = "return this.valueOf();")
127.24 public boolean booleanValue() {
127.25 return value;
127.26 }
127.27 @@ -287,7 +290,7 @@
127.28 }
127.29 @JavaScriptBody(args = { }, body =
127.30 "vm.java_lang_Boolean(false)" +
127.31 - ".valueOf = function() { return this._value(); };"
127.32 + ".valueOf = function() { return this._value() ? true : false; };"
127.33 )
127.34 private native static void initValueOf();
127.35 }
128.1 --- a/rt/emul/mini/src/main/java/java/lang/Class.java Tue Mar 05 07:57:16 2013 +0100
128.2 +++ b/rt/emul/mini/src/main/java/java/lang/Class.java Fri May 17 10:27:41 2013 +0200
128.3 @@ -402,8 +402,15 @@
128.4 }
128.5 return cmpType != null && getComponentType().isAssignableFrom(cmpType);
128.6 }
128.7 - String prop = "$instOf_" + getName().replace('.', '_');
128.8 - return hasCnstrProperty(cls, prop);
128.9 + if (isPrimitive()) {
128.10 + return false;
128.11 + } else {
128.12 + if (cls.isPrimitive()) {
128.13 + return false;
128.14 + }
128.15 + String prop = "$instOf_" + getName().replace('.', '_');
128.16 + return hasCnstrProperty(cls, prop);
128.17 + }
128.18 }
128.19
128.20 @JavaScriptBody(args = { "who", "prop" }, body =
128.21 @@ -1245,6 +1252,7 @@
128.22 }
128.23
128.24 @JavaScriptBody(args = { "sig" }, body =
128.25 + "if (!sig) sig = '[Ljava/lang/Object;';\n" +
128.26 "var c = Array[sig];\n" +
128.27 "if (c) return c;\n" +
128.28 "c = vm.java_lang_Class(true);\n" +
129.1 --- a/rt/emul/mini/src/main/java/java/lang/Double.java Tue Mar 05 07:57:16 2013 +0100
129.2 +++ b/rt/emul/mini/src/main/java/java/lang/Double.java Fri May 17 10:27:41 2013 +0200
129.3 @@ -190,12 +190,12 @@
129.4 * @param d the {@code double} to be converted.
129.5 * @return a string representation of the argument.
129.6 */
129.7 - @JavaScriptBody(args="d", body="var r = d.toString();"
129.8 - + "if (isFinite(d) && (r.indexOf('.') === -1)) r = r + '.0';"
129.9 - + "return r;")
129.10 - public static String toString(double d) {
129.11 - throw new UnsupportedOperationException();
129.12 - }
129.13 + @JavaScriptBody(args="d", body="var f = Math.floor(d);\n" +
129.14 + "var r = d.toString();" +
129.15 + "if (f === d && isFinite(d) && r.indexOf('e') === -1) return r + '.0';\n" +
129.16 + " else return r;"
129.17 + )
129.18 + public static native String toString(double d);
129.19
129.20 /**
129.21 * Returns a hexadecimal string representation of the
129.22 @@ -810,15 +810,17 @@
129.23 * @return the bits that represent the floating-point number.
129.24 */
129.25 public static long doubleToLongBits(double value) {
129.26 - throw new UnsupportedOperationException();
129.27 -// long result = doubleToRawLongBits(value);
129.28 -// // Check for NaN based on values of bit fields, maximum
129.29 -// // exponent and nonzero significand.
129.30 -// if ( ((result & DoubleConsts.EXP_BIT_MASK) ==
129.31 -// DoubleConsts.EXP_BIT_MASK) &&
129.32 -// (result & DoubleConsts.SIGNIF_BIT_MASK) != 0L)
129.33 -// result = 0x7ff8000000000000L;
129.34 -// return result;
129.35 + final long EXP_BIT_MASK = 9218868437227405312L;
129.36 + final long SIGNIF_BIT_MASK = 4503599627370495L;
129.37 +
129.38 + long result = doubleToRawLongBits(value);
129.39 + // Check for NaN based on values of bit fields, maximum
129.40 + // exponent and nonzero significand.
129.41 + if ( ((result & EXP_BIT_MASK) ==
129.42 + EXP_BIT_MASK) &&
129.43 + (result & SIGNIF_BIT_MASK) != 0L)
129.44 + result = 0x7ff8000000000000L;
129.45 + return result;
129.46 }
129.47
129.48 /**
129.49 @@ -857,7 +859,21 @@
129.50 * @return the bits that represent the floating-point number.
129.51 * @since 1.3
129.52 */
129.53 - public static native long doubleToRawLongBits(double value);
129.54 + public static long doubleToRawLongBits(double value) {
129.55 + int[] arr = { 0, 0 };
129.56 + doubleToRawLongBits(value, arr);
129.57 + long l = arr[1];
129.58 + return (l << 32) | arr[0];
129.59 + }
129.60 +
129.61 + @JavaScriptBody(args = { "value", "arr" }, body = ""
129.62 + + "var a = new ArrayBuffer(8);"
129.63 + + "new Float64Array(a)[0] = value;"
129.64 + + "var out = new Int32Array(a);"
129.65 + + "arr[0] = out[0];"
129.66 + + "arr[1] = out[1];"
129.67 + )
129.68 + private static native void doubleToRawLongBits(double value, int[] arr);
129.69
129.70 /**
129.71 * Returns the {@code double} value corresponding to a given
130.1 --- a/rt/emul/mini/src/main/java/java/lang/Enum.java Tue Mar 05 07:57:16 2013 +0100
130.2 +++ b/rt/emul/mini/src/main/java/java/lang/Enum.java Fri May 17 10:27:41 2013 +0200
130.3 @@ -27,6 +27,7 @@
130.4
130.5 import java.io.Serializable;
130.6 import java.io.IOException;
130.7 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
130.8
130.9 /**
130.10 * This is the common base class of all Java language enumeration types.
130.11 @@ -225,15 +226,17 @@
130.12 */
130.13 public static <T extends Enum<T>> T valueOf(Class<T> enumType,
130.14 String name) {
130.15 - throw new UnsupportedOperationException();
130.16 -// T result = enumType.enumConstantDirectory().get(name);
130.17 -// if (result != null)
130.18 -// return result;
130.19 -// if (name == null)
130.20 -// throw new NullPointerException("Name is null");
130.21 -// throw new IllegalArgumentException(
130.22 -// "No enum constant " + enumType.getCanonicalName() + "." + name);
130.23 + for (Object o : values(enumType)) {
130.24 + T t = enumType.cast(o);
130.25 + if (name.equals(((Enum)t).name)) {
130.26 + return t;
130.27 + }
130.28 + }
130.29 + throw new IllegalArgumentException();
130.30 }
130.31 +
130.32 + @JavaScriptBody(args = { "enumType" }, body = "return enumType.cnstr.$VALUES;")
130.33 + private static native Object[] values(Class<?> enumType);
130.34
130.35 /**
130.36 * enum classes cannot have finalize methods.
131.1 --- a/rt/emul/mini/src/main/java/java/lang/Float.java Tue Mar 05 07:57:16 2013 +0100
131.2 +++ b/rt/emul/mini/src/main/java/java/lang/Float.java Fri May 17 10:27:41 2013 +0200
131.3 @@ -710,15 +710,17 @@
131.4 * @return the bits that represent the floating-point number.
131.5 */
131.6 public static int floatToIntBits(float value) {
131.7 - throw new UnsupportedOperationException();
131.8 -// int result = floatToRawIntBits(value);
131.9 -// // Check for NaN based on values of bit fields, maximum
131.10 -// // exponent and nonzero significand.
131.11 -// if ( ((result & FloatConsts.EXP_BIT_MASK) ==
131.12 -// FloatConsts.EXP_BIT_MASK) &&
131.13 -// (result & FloatConsts.SIGNIF_BIT_MASK) != 0)
131.14 -// result = 0x7fc00000;
131.15 -// return result;
131.16 + final int EXP_BIT_MASK = 2139095040;
131.17 + final int SIGNIF_BIT_MASK = 8388607;
131.18 +
131.19 + int result = floatToRawIntBits(value);
131.20 + // Check for NaN based on values of bit fields, maximum
131.21 + // exponent and nonzero significand.
131.22 + if ( ((result & EXP_BIT_MASK) ==
131.23 + EXP_BIT_MASK) &&
131.24 + (result & SIGNIF_BIT_MASK) != 0)
131.25 + result = 0x7fc00000;
131.26 + return result;
131.27 }
131.28
131.29 /**
131.30 @@ -756,6 +758,11 @@
131.31 * @return the bits that represent the floating-point number.
131.32 * @since 1.3
131.33 */
131.34 + @JavaScriptBody(args = { "value" }, body = ""
131.35 + + "var a = new ArrayBuffer(4);"
131.36 + + "new Float32Array(a)[0] = value;"
131.37 + + "return new Int32Array(a)[0];"
131.38 + )
131.39 public static native int floatToRawIntBits(float value);
131.40
131.41 /**
132.1 --- a/rt/emul/mini/src/main/java/java/lang/String.java Tue Mar 05 07:57:16 2013 +0100
132.2 +++ b/rt/emul/mini/src/main/java/java/lang/String.java Fri May 17 10:27:41 2013 +0200
132.3 @@ -2220,9 +2220,19 @@
132.4 * <code>replacement</code> is <code>null</code>.
132.5 * @since 1.5
132.6 */
132.7 - public String replace(CharSequence target, CharSequence replacement) {
132.8 - throw new UnsupportedOperationException("This one should be supported, but without dep on rest of regexp");
132.9 - }
132.10 + @JavaScriptBody(args = { "target", "replacement" }, body =
132.11 + "var s = this.toString();\n"
132.12 + + "target = target.toString();\n"
132.13 + + "replacement = replacement.toString();\n"
132.14 + + "for (;;) {\n"
132.15 + + " var ret = s.replace(target, replacement);\n"
132.16 + + " if (ret === s) {\n"
132.17 + + " return ret;\n"
132.18 + + " }\n"
132.19 + + " s = ret;\n"
132.20 + + "}"
132.21 + )
132.22 + public native String replace(CharSequence target, CharSequence replacement);
132.23
132.24 /**
132.25 * Splits this string around matches of the given
133.1 --- a/rt/emul/mini/src/main/java/java/lang/Throwable.java Tue Mar 05 07:57:16 2013 +0100
133.2 +++ b/rt/emul/mini/src/main/java/java/lang/Throwable.java Fri May 17 10:27:41 2013 +0200
133.3 @@ -1085,4 +1085,22 @@
133.4 // else
133.5 // return suppressedExceptions.toArray(EMPTY_THROWABLE_ARRAY);
133.6 }
133.7 +
133.8 + private static Object bck2BrwsrCnvrt(Object o) {
133.9 + if (o instanceof Throwable) {
133.10 + return o;
133.11 + }
133.12 + final String msg = msg(o);
133.13 + if (msg == null || msg.startsWith("TypeError")) {
133.14 + return new NullPointerException(msg);
133.15 + }
133.16 + return new Throwable(msg);
133.17 + }
133.18 +
133.19 + @JavaScriptBody(args = { "o" }, body = "return o ? o.toString() : null;")
133.20 + private static native String msg(Object o);
133.21 +
133.22 + @JavaScriptOnly(name = "bck2BrwsrCnvrt", value = "c.bck2BrwsrCnvrt__Ljava_lang_Object_2Ljava_lang_Object_2")
133.23 + private static void bck2BrwsrCnvrtVM() {
133.24 + }
133.25 }
134.1 --- a/rt/emul/mini/src/main/java/java/lang/reflect/Array.java Tue Mar 05 07:57:16 2013 +0100
134.2 +++ b/rt/emul/mini/src/main/java/java/lang/reflect/Array.java Fri May 17 10:27:41 2013 +0200
134.3 @@ -636,9 +636,9 @@
134.4 + "arr.jvmName = sig;\n"
134.5 + "return arr;"
134.6 )
134.7 - private static native Object newArray(boolean primitive, String sig, int length);
134.8 + static native Object newArray(boolean primitive, String sig, int length);
134.9
134.10 - private static Object multiNewArray(String sig, int[] dims, int index)
134.11 + static Object multiNewArray(String sig, int[] dims, int index)
134.12 throws IllegalArgumentException, NegativeArraySizeException {
134.13 if (dims.length == index + 1) {
134.14 return newArray(sig.length() == 2, sig, dims[index]);
135.1 --- a/rt/emul/mini/src/main/java/java/lang/reflect/Method.java Tue Mar 05 07:57:16 2013 +0100
135.2 +++ b/rt/emul/mini/src/main/java/java/lang/reflect/Method.java Fri May 17 10:27:41 2013 +0200
135.3 @@ -501,8 +501,8 @@
135.4 throws IllegalAccessException, IllegalArgumentException,
135.5 InvocationTargetException
135.6 {
135.7 - final boolean isStatic = (getModifiers() & Modifier.STATIC) == 0;
135.8 - if (isStatic && obj == null) {
135.9 + final boolean nonStatic = (getModifiers() & Modifier.STATIC) == 0;
135.10 + if (nonStatic && obj == null) {
135.11 throw new NullPointerException();
135.12 }
135.13 Class[] types = getParameterTypes();
135.14 @@ -517,7 +517,7 @@
135.15 }
135.16 }
135.17 }
135.18 - Object res = invoke0(isStatic, this, obj, args);
135.19 + Object res = invokeTry(nonStatic, this, obj, args);
135.20 if (getReturnType().isPrimitive()) {
135.21 res = fromPrimitive(getReturnType(), res);
135.22 }
135.23 @@ -536,6 +536,15 @@
135.24 + "return method._data().apply(self, p);\n"
135.25 )
135.26 private static native Object invoke0(boolean isStatic, Method m, Object self, Object[] args);
135.27 +
135.28 + private static Object invokeTry(boolean isStatic, Method m, Object self, Object[] args)
135.29 + throws InvocationTargetException {
135.30 + try {
135.31 + return invoke0(isStatic, m, self, args);
135.32 + } catch (Throwable ex) {
135.33 + throw new InvocationTargetException(ex, ex.getMessage());
135.34 + }
135.35 + }
135.36
135.37 static Object fromPrimitive(Class<?> type, Object o) {
135.38 if (type == Integer.TYPE) {
136.1 --- a/rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/System.java Tue Mar 05 07:57:16 2013 +0100
136.2 +++ b/rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/System.java Fri May 17 10:27:41 2013 +0200
136.3 @@ -54,6 +54,11 @@
136.4 )
136.5 public static native byte[] expandArray(byte[] arr, int expectedSize);
136.6
136.7 + @JavaScriptBody(args = { "arr", "expectedSize" }, body =
136.8 + "while (expectedSize-- > arr.length) { arr.push(0); }; return arr;"
136.9 + )
136.10 + public static native char[] expandArray(char[] arr, int expectedSize);
136.11 +
136.12 @JavaScriptBody(args = {}, body = "return new Date().getTime();")
136.13 private static native double currentTimeMillisDouble();
136.14
137.1 --- a/rt/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js Tue Mar 05 07:57:16 2013 +0100
137.2 +++ b/rt/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js Fri May 17 10:27:41 2013 +0200
137.3 @@ -1,226 +1,244 @@
137.4 // empty line needed here
137.5 -Number.prototype.add32 = function(x) { return (this + x) | 0; };
137.6 -Number.prototype.sub32 = function(x) { return (this - x) | 0; };
137.7 -Number.prototype.mul32 = function(x) {
137.8 - return (((this * (x >> 16)) << 16) + this * (x & 0xFFFF)) | 0;
137.9 -};
137.10 -Number.prototype.neg32 = function() { return (-this) | 0; };
137.11
137.12 -Number.prototype.toInt8 = function() { return (this << 24) >> 24; };
137.13 -Number.prototype.toInt16 = function() { return (this << 16) >> 16; };
137.14 +(function(numberPrototype) {
137.15 + numberPrototype.add32 = function(x) {
137.16 + return (this + x) | 0;
137.17 + };
137.18 + numberPrototype.sub32 = function(x) {
137.19 + return (this - x) | 0;
137.20 + };
137.21 + numberPrototype.mul32 = function(x) {
137.22 + return (((this * (x >> 16)) << 16) + this * (x & 0xFFFF)) | 0;
137.23 + };
137.24 + numberPrototype.neg32 = function() {
137.25 + return (-this) | 0;
137.26 + };
137.27
137.28 -var __m32 = 0xFFFFFFFF;
137.29 + numberPrototype.toInt8 = function() {
137.30 + return (this << 24) >> 24;
137.31 + };
137.32 + numberPrototype.toInt16 = function() {
137.33 + return (this << 16) >> 16;
137.34 + };
137.35
137.36 -Number.prototype.next32 = function(low) {
137.37 - if (this === 0) {
137.38 - return low;
137.39 - }
137.40 - var l = new Number(low);
137.41 - l.hi = this | 0;
137.42 - return l;
137.43 -};
137.44 + var __m32 = 0xFFFFFFFF;
137.45
137.46 -Number.prototype.high32 = function() {
137.47 - return this.hi ? this.hi : (Math.floor(this / (__m32+1))) | 0;
137.48 -};
137.49 -Number.prototype.toInt32 = function() { return this | 0; };
137.50 -Number.prototype.toFP = function() {
137.51 - return this.hi ? this.hi * (__m32+1) + this : this;
137.52 -};
137.53 -Number.prototype.toLong = function() {
137.54 - var hi = (this / (__m32+1)) | 0;
137.55 - var low = (this % (__m32+1)) | 0;
137.56 - if (low < 0) {
137.57 - low += __m32+1;
137.58 - }
137.59 -
137.60 - if (this < 0) {
137.61 - hi -= 1;
137.62 - }
137.63 + numberPrototype.next32 = function(low) {
137.64 + if (this === 0) {
137.65 + return low;
137.66 + }
137.67 + var l = new Number(low);
137.68 + l.hi = this | 0;
137.69 + return l;
137.70 + };
137.71
137.72 - return hi.next32(low);
137.73 -};
137.74 + numberPrototype.high32 = function() {
137.75 + return this.hi ? this.hi : (Math.floor(this / (__m32 + 1))) | 0;
137.76 + };
137.77 + numberPrototype.toInt32 = function() {
137.78 + return this | 0;
137.79 + };
137.80 + numberPrototype.toFP = function() {
137.81 + return this.hi ? this.hi * (__m32 + 1) + this : this;
137.82 + };
137.83 + numberPrototype.toLong = function() {
137.84 + var hi = (this / (__m32 + 1)) | 0;
137.85 + var low = (this % (__m32 + 1)) | 0;
137.86 + if (low < 0) {
137.87 + low += __m32 + 1;
137.88 + }
137.89
137.90 -Number.prototype.toExactString = function() {
137.91 - if (this.hi) {
137.92 - // check for Long.MIN_VALUE
137.93 - if ((this.hi == (0x80000000 | 0)) && (this == 0)) {
137.94 - return '-9223372036854775808';
137.95 + if (this < 0) {
137.96 + hi -= 1;
137.97 }
137.98 - var res = 0;
137.99 - var a = [ 6,9,2,7,6,9,4,9,2,4 ];
137.100 - var s = '';
137.101 - var digit;
137.102 - var neg = this.hi < 0;
137.103 - if (neg) {
137.104 - var x = this.neg64();
137.105 - var hi = x.hi;
137.106 - var low = x;
137.107 - } else {
137.108 - var hi = this.hi;
137.109 - var low = this;
137.110 - }
137.111 - for (var i = 0; i < a.length; i++) {
137.112 - res += hi * a[i];
137.113 - var low_digit = low % 10;
137.114 - digit = (res % 10) + low_digit;
137.115
137.116 - low = Math.floor(low / 10);
137.117 - res = Math.floor(res / 10);
137.118 -
137.119 - if (digit >= 10) {
137.120 - digit -= 10;
137.121 - res++;
137.122 - }
137.123 - s = String(digit).concat(s);
137.124 - }
137.125 - s = String(res).concat(s).replace(/^0+/, '');
137.126 - return (neg ? '-' : '').concat(s);
137.127 - }
137.128 - return String(this);
137.129 -};
137.130 -
137.131 -Number.prototype.add64 = function(x) {
137.132 - var low = this + x;
137.133 - carry = 0;
137.134 - if (low > __m32) {
137.135 - carry = 1;
137.136 - low -= (__m32+1);
137.137 - }
137.138 - var hi = (this.high32() + x.high32() + carry) | 0;
137.139 - return hi.next32(low);
137.140 -};
137.141 -
137.142 -Number.prototype.sub64 = function(x) {
137.143 - var low = this - x;
137.144 - carry = 0;
137.145 - if (low < 0) {
137.146 - carry = 1;
137.147 - low += (__m32+1);
137.148 - }
137.149 - var hi = (this.high32() - x.high32() - carry) | 0;
137.150 - return hi.next32(low);
137.151 -};
137.152 -
137.153 -Number.prototype.mul64 = function(x) {
137.154 - var low = this.mul32(x);
137.155 - low += (low < 0) ? (__m32+1) : 0;
137.156 - // first count upper 32 bits of (this.low * x.low)
137.157 - var hi_hi = 0;
137.158 - var hi_low = 0;
137.159 - var m = 1;
137.160 - for (var i = 0; i < 32; i++) {
137.161 - if (x & m) {
137.162 - hi_hi += this >>> 16;
137.163 - hi_low += this & 0xFFFF
137.164 - }
137.165 - hi_low >>= 1;
137.166 - hi_low += (hi_hi & 1) ? 0x8000 : 0;
137.167 - hi_hi >>= 1;
137.168 - m <<= 1;
137.169 - }
137.170 - var hi = (hi_hi << 16) + hi_low;
137.171 -
137.172 - var m1 = this.high32().mul32(x);
137.173 - var m2 = this.mul32(x.high32());
137.174 - hi = hi.add32(m1).add32(m2);
137.175 -
137.176 - return hi.next32(low);
137.177 -};
137.178 -
137.179 -Number.prototype.and64 = function(x) {
137.180 - var low = this & x;
137.181 - low += (low < 0) ? (__m32+1) : 0;
137.182 - if (this.hi && x.hi) {
137.183 - var hi = this.hi & x.hi;
137.184 return hi.next32(low);
137.185 };
137.186 - return low;
137.187 -};
137.188
137.189 -Number.prototype.or64 = function(x) {
137.190 - var low = this | x;
137.191 - low += (low < 0) ? (__m32+1) : 0;
137.192 - if (this.hi || x.hi) {
137.193 - var hi = this.hi | x.hi;
137.194 + numberPrototype.toExactString = function() {
137.195 + if (this.hi) {
137.196 + // check for Long.MIN_VALUE
137.197 + if ((this.hi == (0x80000000 | 0)) && (this == 0)) {
137.198 + return '-9223372036854775808';
137.199 + }
137.200 + var res = 0;
137.201 + var a = [6, 9, 2, 7, 6, 9, 4, 9, 2, 4];
137.202 + var s = '';
137.203 + var digit;
137.204 + var neg = this.hi < 0;
137.205 + if (neg) {
137.206 + var x = this.neg64();
137.207 + var hi = x.hi;
137.208 + var low = x;
137.209 + } else {
137.210 + var hi = this.hi;
137.211 + var low = this;
137.212 + }
137.213 + for (var i = 0; i < a.length; i++) {
137.214 + res += hi * a[i];
137.215 + var low_digit = low % 10;
137.216 + digit = (res % 10) + low_digit;
137.217 +
137.218 + low = Math.floor(low / 10);
137.219 + res = Math.floor(res / 10);
137.220 +
137.221 + if (digit >= 10) {
137.222 + digit -= 10;
137.223 + res++;
137.224 + }
137.225 + s = String(digit).concat(s);
137.226 + }
137.227 + s = String(res).concat(s).replace(/^0+/, '');
137.228 + return (neg ? '-' : '').concat(s);
137.229 + }
137.230 + return String(this);
137.231 + };
137.232 +
137.233 + numberPrototype.add64 = function(x) {
137.234 + var low = this + x;
137.235 + carry = 0;
137.236 + if (low > __m32) {
137.237 + carry = 1;
137.238 + low -= (__m32 + 1);
137.239 + }
137.240 + var hi = (this.high32() + x.high32() + carry) | 0;
137.241 return hi.next32(low);
137.242 };
137.243 - return low;
137.244 -};
137.245
137.246 -Number.prototype.xor64 = function(x) {
137.247 - var low = this ^ x;
137.248 - low += (low < 0) ? (__m32+1) : 0;
137.249 - if (this.hi || x.hi) {
137.250 - var hi = this.hi ^ x.hi;
137.251 + numberPrototype.sub64 = function(x) {
137.252 + var low = this - x;
137.253 + carry = 0;
137.254 + if (low < 0) {
137.255 + carry = 1;
137.256 + low += (__m32 + 1);
137.257 + }
137.258 + var hi = (this.high32() - x.high32() - carry) | 0;
137.259 return hi.next32(low);
137.260 };
137.261 - return low;
137.262 -};
137.263
137.264 -Number.prototype.shl64 = function(x) {
137.265 - if (x >= 32) {
137.266 - var hi = this << (x - 32);
137.267 - return hi.next32(0);
137.268 - } else {
137.269 - var hi = this.high32() << x;
137.270 - var low_reminder = this >> (32 - x);
137.271 - hi |= low_reminder;
137.272 - var low = this << x;
137.273 - low += (low < 0) ? (__m32+1) : 0;
137.274 + numberPrototype.mul64 = function(x) {
137.275 + var low = this.mul32(x);
137.276 + low += (low < 0) ? (__m32 + 1) : 0;
137.277 + // first count upper 32 bits of (this.low * x.low)
137.278 + var hi_hi = 0;
137.279 + var hi_low = 0;
137.280 + var m = 1;
137.281 + for (var i = 0; i < 32; i++) {
137.282 + if (x & m) {
137.283 + hi_hi += this >>> 16;
137.284 + hi_low += this & 0xFFFF
137.285 + }
137.286 + hi_low >>= 1;
137.287 + hi_low += (hi_hi & 1) ? 0x8000 : 0;
137.288 + hi_hi >>= 1;
137.289 + m <<= 1;
137.290 + }
137.291 + var hi = (hi_hi << 16) + hi_low;
137.292 +
137.293 + var m1 = this.high32().mul32(x);
137.294 + var m2 = this.mul32(x.high32());
137.295 + hi = hi.add32(m1).add32(m2);
137.296 +
137.297 return hi.next32(low);
137.298 - }
137.299 -};
137.300 + };
137.301
137.302 -Number.prototype.shr64 = function(x) {
137.303 - if (x >= 32) {
137.304 - var low = this.high32() >> (x - 32);
137.305 - low += (low < 0) ? (__m32+1) : 0;
137.306 + numberPrototype.and64 = function(x) {
137.307 + var low = this & x;
137.308 + low += (low < 0) ? (__m32 + 1) : 0;
137.309 + if (this.hi && x.hi) {
137.310 + var hi = this.hi & x.hi;
137.311 + return hi.next32(low);
137.312 + }
137.313 + ;
137.314 return low;
137.315 - } else {
137.316 - var low = this >> x;
137.317 - var hi_reminder = this.high32() << (32 - x);
137.318 - low |= hi_reminder;
137.319 - low += (low < 0) ? (__m32+1) : 0;
137.320 - var hi = this.high32() >> x;
137.321 - return hi.next32(low);
137.322 - }
137.323 -};
137.324 + };
137.325
137.326 -Number.prototype.ushr64 = function(x) {
137.327 - if (x >= 32) {
137.328 - var low = this.high32() >>> (x - 32);
137.329 - low += (low < 0) ? (__m32+1) : 0;
137.330 + numberPrototype.or64 = function(x) {
137.331 + var low = this | x;
137.332 + low += (low < 0) ? (__m32 + 1) : 0;
137.333 + if (this.hi || x.hi) {
137.334 + var hi = this.hi | x.hi;
137.335 + return hi.next32(low);
137.336 + }
137.337 + ;
137.338 return low;
137.339 - } else {
137.340 - var low = this >>> x;
137.341 - var hi_reminder = this.high32() << (32 - x);
137.342 - low |= hi_reminder;
137.343 - low += (low < 0) ? (__m32+1) : 0;
137.344 - var hi = this.high32() >>> x;
137.345 - return hi.next32(low);
137.346 - }
137.347 -};
137.348 + };
137.349
137.350 -Number.prototype.compare64 = function(x) {
137.351 - if (this.high32() === x.high32()) {
137.352 - return (this < x) ? -1 : ((this > x) ? 1 : 0);
137.353 - }
137.354 - return (this.high32() < x.high32()) ? -1 : 1;
137.355 -};
137.356 + numberPrototype.xor64 = function(x) {
137.357 + var low = this ^ x;
137.358 + low += (low < 0) ? (__m32 + 1) : 0;
137.359 + if (this.hi || x.hi) {
137.360 + var hi = this.hi ^ x.hi;
137.361 + return hi.next32(low);
137.362 + }
137.363 + ;
137.364 + return low;
137.365 + };
137.366
137.367 -Number.prototype.neg64 = function() {
137.368 - var hi = this.high32();
137.369 - var low = this;
137.370 - if ((hi === 0) && (low < 0)) { return -low; }
137.371 - hi = ~hi;
137.372 - low = ~low;
137.373 - low += (low < 0) ? (__m32+1) : 0;
137.374 - var ret = hi.next32(low);
137.375 - return ret.add64(1);
137.376 -};
137.377 + numberPrototype.shl64 = function(x) {
137.378 + if (x >= 32) {
137.379 + var hi = this << (x - 32);
137.380 + return hi.next32(0);
137.381 + } else {
137.382 + var hi = this.high32() << x;
137.383 + var low_reminder = this >> (32 - x);
137.384 + hi |= low_reminder;
137.385 + var low = this << x;
137.386 + low += (low < 0) ? (__m32 + 1) : 0;
137.387 + return hi.next32(low);
137.388 + }
137.389 + };
137.390
137.391 -(function(numberPrototype) {
137.392 + numberPrototype.shr64 = function(x) {
137.393 + if (x >= 32) {
137.394 + var low = this.high32() >> (x - 32);
137.395 + low += (low < 0) ? (__m32 + 1) : 0;
137.396 + return low;
137.397 + } else {
137.398 + var low = this >> x;
137.399 + var hi_reminder = this.high32() << (32 - x);
137.400 + low |= hi_reminder;
137.401 + low += (low < 0) ? (__m32 + 1) : 0;
137.402 + var hi = this.high32() >> x;
137.403 + return hi.next32(low);
137.404 + }
137.405 + };
137.406 +
137.407 + numberPrototype.ushr64 = function(x) {
137.408 + if (x >= 32) {
137.409 + var low = this.high32() >>> (x - 32);
137.410 + low += (low < 0) ? (__m32 + 1) : 0;
137.411 + return low;
137.412 + } else {
137.413 + var low = this >>> x;
137.414 + var hi_reminder = this.high32() << (32 - x);
137.415 + low |= hi_reminder;
137.416 + low += (low < 0) ? (__m32 + 1) : 0;
137.417 + var hi = this.high32() >>> x;
137.418 + return hi.next32(low);
137.419 + }
137.420 + };
137.421 +
137.422 + numberPrototype.compare64 = function(x) {
137.423 + if (this.high32() === x.high32()) {
137.424 + return (this < x) ? -1 : ((this > x) ? 1 : 0);
137.425 + }
137.426 + return (this.high32() < x.high32()) ? -1 : 1;
137.427 + };
137.428 +
137.429 + numberPrototype.neg64 = function() {
137.430 + var hi = this.high32();
137.431 + var low = this;
137.432 + if ((hi === 0) && (low < 0)) {
137.433 + return -low;
137.434 + }
137.435 + hi = ~hi;
137.436 + low = ~low;
137.437 + low += (low < 0) ? (__m32 + 1) : 0;
137.438 + var ret = hi.next32(low);
137.439 + return ret.add64(1);
137.440 + };
137.441 +
137.442 function __handleDivByZero() {
137.443 var exception = new vm.java_lang_ArithmeticException;
137.444 vm.java_lang_ArithmeticException(false).constructor
137.445 @@ -551,3 +569,5 @@
137.446 return negateResult ? result.neg64() : result;
137.447 }
137.448 })(Number.prototype);
137.449 +
137.450 +vm.java_lang_Number(false);
138.1 --- a/rt/emul/pom.xml Tue Mar 05 07:57:16 2013 +0100
138.2 +++ b/rt/emul/pom.xml Fri May 17 10:27:41 2013 +0200
138.3 @@ -4,15 +4,16 @@
138.4 <parent>
138.5 <groupId>org.apidesign.bck2brwsr</groupId>
138.6 <artifactId>rt</artifactId>
138.7 - <version>0.4-SNAPSHOT</version>
138.8 + <version>0.8-SNAPSHOT</version>
138.9 </parent>
138.10 <groupId>org.apidesign.bck2brwsr</groupId>
138.11 <artifactId>emul.pom</artifactId>
138.12 - <version>0.4-SNAPSHOT</version>
138.13 + <version>0.8-SNAPSHOT</version>
138.14 <packaging>pom</packaging>
138.15 <name>Emulation of Core Libraries</name>
138.16 <modules>
138.17 <module>mini</module>
138.18 <module>compact</module>
138.19 + <module>brwsrtest</module>
138.20 </modules>
138.21 </project>
139.1 --- a/rt/javap/pom.xml Tue Mar 05 07:57:16 2013 +0100
139.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
139.3 @@ -1,40 +0,0 @@
139.4 -<?xml version="1.0"?>
139.5 -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
139.6 - <modelVersion>4.0.0</modelVersion>
139.7 - <parent>
139.8 - <groupId>org.apidesign.bck2brwsr</groupId>
139.9 - <artifactId>rt</artifactId>
139.10 - <version>0.4-SNAPSHOT</version>
139.11 - </parent>
139.12 - <groupId>org.apidesign.bck2brwsr</groupId>
139.13 - <artifactId>javap</artifactId>
139.14 - <version>0.4-SNAPSHOT</version>
139.15 - <name>ByteCode Parser</name>
139.16 - <url>http://maven.apache.org</url>
139.17 - <properties>
139.18 - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
139.19 - </properties>
139.20 - <build>
139.21 - <plugins>
139.22 - <plugin>
139.23 - <groupId>org.apache.maven.plugins</groupId>
139.24 - <artifactId>maven-compiler-plugin</artifactId>
139.25 - <version>2.5.1</version>
139.26 - <configuration>
139.27 - <compilerArguments>
139.28 - <!--<bootclasspath>non-existing</bootclasspath>-->
139.29 - </compilerArguments>
139.30 - <source>1.6</source>
139.31 - <target>1.6</target>
139.32 - </configuration>
139.33 - </plugin>
139.34 - </plugins>
139.35 - </build>
139.36 - <dependencies>
139.37 - <dependency>
139.38 - <groupId>org.apidesign.bck2brwsr</groupId>
139.39 - <artifactId>emul.mini</artifactId>
139.40 - <version>0.4-SNAPSHOT</version>
139.41 - </dependency>
139.42 - </dependencies>
139.43 -</project>
140.1 --- a/rt/javap/src/main/java/org/apidesign/javap/AnnotationParser.java Tue Mar 05 07:57:16 2013 +0100
140.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
140.3 @@ -1,145 +0,0 @@
140.4 -/*
140.5 - * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
140.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
140.7 - *
140.8 - * This code is free software; you can redistribute it and/or modify it
140.9 - * under the terms of the GNU General Public License version 2 only, as
140.10 - * published by the Free Software Foundation. Oracle designates this
140.11 - * particular file as subject to the "Classpath" exception as provided
140.12 - * by Oracle in the LICENSE file that accompanied this code.
140.13 - *
140.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
140.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
140.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
140.17 - * version 2 for more details (a copy is included in the LICENSE file that
140.18 - * accompanied this code).
140.19 - *
140.20 - * You should have received a copy of the GNU General Public License version
140.21 - * 2 along with this work; if not, write to the Free Software Foundation,
140.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
140.23 - *
140.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
140.25 - * or visit www.oracle.com if you need additional information or have any
140.26 - * questions.
140.27 - */
140.28 -package org.apidesign.javap;
140.29 -
140.30 -import java.io.ByteArrayInputStream;
140.31 -import java.io.DataInputStream;
140.32 -import java.io.IOException;
140.33 -
140.34 -/** An abstract parser for annotation definitions. Analyses the bytes and
140.35 - * performs some callbacks to the overriden parser methods.
140.36 - *
140.37 - * @author Jaroslav Tulach <jtulach@netbeans.org>
140.38 - */
140.39 -public class AnnotationParser {
140.40 - private final boolean textual;
140.41 - private final boolean iterateArray;
140.42 -
140.43 - protected AnnotationParser(boolean textual, boolean iterateArray) {
140.44 - this.textual = textual;
140.45 - this.iterateArray = iterateArray;
140.46 - }
140.47 -
140.48 - protected void visitAnnotationStart(String type, boolean top) throws IOException {
140.49 - }
140.50 -
140.51 - protected void visitAnnotationEnd(String type, boolean top) throws IOException {
140.52 - }
140.53 -
140.54 - protected void visitValueStart(String attrName, char type) throws IOException {
140.55 - }
140.56 -
140.57 - protected void visitValueEnd(String attrName, char type) throws IOException {
140.58 - }
140.59 -
140.60 -
140.61 - protected void visitAttr(
140.62 - String annoType, String attr, String attrType, String value
140.63 - ) throws IOException {
140.64 - }
140.65 -
140.66 - protected void visitEnumAttr(
140.67 - String annoType, String attr, String attrType, String value
140.68 - ) throws IOException {
140.69 - visitAttr(annoType, attr, attrType, value);
140.70 - }
140.71 -
140.72 - /** Initialize the parsing with constant pool from <code>cd</code>.
140.73 - *
140.74 - * @param attr the attribute defining annotations
140.75 - * @param cd constant pool
140.76 - * @throws IOException in case I/O fails
140.77 - */
140.78 - public final void parse(byte[] attr, ClassData cd) throws IOException {
140.79 - ByteArrayInputStream is = new ByteArrayInputStream(attr);
140.80 - DataInputStream dis = new DataInputStream(is);
140.81 - try {
140.82 - read(dis, cd);
140.83 - } finally {
140.84 - is.close();
140.85 - }
140.86 - }
140.87 -
140.88 - private void read(DataInputStream dis, ClassData cd) throws IOException {
140.89 - int cnt = dis.readUnsignedShort();
140.90 - for (int i = 0; i < cnt; i++) {
140.91 - readAnno(dis, cd, true);
140.92 - }
140.93 - }
140.94 -
140.95 - private void readAnno(DataInputStream dis, ClassData cd, boolean top) throws IOException {
140.96 - int type = dis.readUnsignedShort();
140.97 - String typeName = cd.StringValue(type);
140.98 - visitAnnotationStart(typeName, top);
140.99 - int cnt = dis.readUnsignedShort();
140.100 - for (int i = 0; i < cnt; i++) {
140.101 - String attrName = cd.StringValue(dis.readUnsignedShort());
140.102 - readValue(dis, cd, typeName, attrName);
140.103 - }
140.104 - visitAnnotationEnd(typeName, top);
140.105 - if (cnt == 0) {
140.106 - visitAttr(typeName, null, null, null);
140.107 - }
140.108 - }
140.109 -
140.110 - private void readValue(
140.111 - DataInputStream dis, ClassData cd, String typeName, String attrName
140.112 - ) throws IOException {
140.113 - char type = (char)dis.readByte();
140.114 - visitValueStart(attrName, type);
140.115 - if (type == '@') {
140.116 - readAnno(dis, cd, false);
140.117 - } else if ("CFJZsSIDB".indexOf(type) >= 0) { // NOI18N
140.118 - int primitive = dis.readUnsignedShort();
140.119 - String val = cd.stringValue(primitive, textual);
140.120 - String attrType;
140.121 - if (type == 's') {
140.122 - attrType = "Ljava_lang_String_2";
140.123 - if (textual) {
140.124 - val = '"' + val + '"';
140.125 - }
140.126 - } else {
140.127 - attrType = "" + type;
140.128 - }
140.129 - visitAttr(typeName, attrName, attrType, val);
140.130 - } else if (type == 'c') {
140.131 - int cls = dis.readUnsignedShort();
140.132 - } else if (type == '[') {
140.133 - int cnt = dis.readUnsignedShort();
140.134 - for (int i = 0; i < cnt; i++) {
140.135 - readValue(dis, cd, typeName, iterateArray ? attrName : null);
140.136 - }
140.137 - } else if (type == 'e') {
140.138 - int enumT = dis.readUnsignedShort();
140.139 - String attrType = cd.stringValue(enumT, textual);
140.140 - int enumN = dis.readUnsignedShort();
140.141 - String val = cd.stringValue(enumN, textual);
140.142 - visitEnumAttr(typeName, attrName, attrType, val);
140.143 - } else {
140.144 - throw new IOException("Unknown type " + type);
140.145 - }
140.146 - visitValueEnd(attrName, type);
140.147 - }
140.148 -}
141.1 --- a/rt/javap/src/main/java/org/apidesign/javap/AttrData.java Tue Mar 05 07:57:16 2013 +0100
141.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
141.3 @@ -1,77 +0,0 @@
141.4 -/*
141.5 - * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
141.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
141.7 - *
141.8 - * This code is free software; you can redistribute it and/or modify it
141.9 - * under the terms of the GNU General Public License version 2 only, as
141.10 - * published by the Free Software Foundation. Oracle designates this
141.11 - * particular file as subject to the "Classpath" exception as provided
141.12 - * by Oracle in the LICENSE file that accompanied this code.
141.13 - *
141.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
141.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
141.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
141.17 - * version 2 for more details (a copy is included in the LICENSE file that
141.18 - * accompanied this code).
141.19 - *
141.20 - * You should have received a copy of the GNU General Public License version
141.21 - * 2 along with this work; if not, write to the Free Software Foundation,
141.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
141.23 - *
141.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
141.25 - * or visit www.oracle.com if you need additional information or have any
141.26 - * questions.
141.27 - */
141.28 -
141.29 -
141.30 -
141.31 -package org.apidesign.javap;
141.32 -
141.33 -import java.io.*;
141.34 -
141.35 -/**
141.36 - * Reads and stores attribute information.
141.37 - *
141.38 - * @author Sucheta Dambalkar (Adopted code from jdis)
141.39 - */
141.40 -class AttrData {
141.41 - ClassData cls;
141.42 - int name_cpx;
141.43 - int datalen;
141.44 - byte data[];
141.45 -
141.46 - public AttrData (ClassData cls) {
141.47 - this.cls=cls;
141.48 - }
141.49 -
141.50 - /**
141.51 - * Reads unknown attribute.
141.52 - */
141.53 - public void read(int name_cpx, DataInputStream in) throws IOException {
141.54 - this.name_cpx=name_cpx;
141.55 - datalen=in.readInt();
141.56 - data=new byte[datalen];
141.57 - in.readFully(data);
141.58 - }
141.59 -
141.60 - /**
141.61 - * Reads just the name of known attribute.
141.62 - */
141.63 - public void read(int name_cpx){
141.64 - this.name_cpx=name_cpx;
141.65 - }
141.66 -
141.67 - /**
141.68 - * Returns attribute name.
141.69 - */
141.70 - public String getAttrName(){
141.71 - return cls.getString(name_cpx);
141.72 - }
141.73 -
141.74 - /**
141.75 - * Returns attribute data.
141.76 - */
141.77 - public byte[] getData(){
141.78 - return data;
141.79 - }
141.80 -}
142.1 --- a/rt/javap/src/main/java/org/apidesign/javap/CPX.java Tue Mar 05 07:57:16 2013 +0100
142.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
142.3 @@ -1,40 +0,0 @@
142.4 -/*
142.5 - * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
142.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
142.7 - *
142.8 - * This code is free software; you can redistribute it and/or modify it
142.9 - * under the terms of the GNU General Public License version 2 only, as
142.10 - * published by the Free Software Foundation. Oracle designates this
142.11 - * particular file as subject to the "Classpath" exception as provided
142.12 - * by Oracle in the LICENSE file that accompanied this code.
142.13 - *
142.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
142.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
142.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
142.17 - * version 2 for more details (a copy is included in the LICENSE file that
142.18 - * accompanied this code).
142.19 - *
142.20 - * You should have received a copy of the GNU General Public License version
142.21 - * 2 along with this work; if not, write to the Free Software Foundation,
142.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
142.23 - *
142.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
142.25 - * or visit www.oracle.com if you need additional information or have any
142.26 - * questions.
142.27 - */
142.28 -
142.29 -
142.30 -package org.apidesign.javap;
142.31 -
142.32 -/**
142.33 - * Stores constant pool entry information with one field.
142.34 - *
142.35 - * @author Sucheta Dambalkar (Adopted code from jdis)
142.36 - */
142.37 -class CPX {
142.38 - int cpx;
142.39 -
142.40 - CPX (int cpx) {
142.41 - this.cpx=cpx;
142.42 - }
142.43 -}
143.1 --- a/rt/javap/src/main/java/org/apidesign/javap/CPX2.java Tue Mar 05 07:57:16 2013 +0100
143.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
143.3 @@ -1,41 +0,0 @@
143.4 -/*
143.5 - * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
143.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
143.7 - *
143.8 - * This code is free software; you can redistribute it and/or modify it
143.9 - * under the terms of the GNU General Public License version 2 only, as
143.10 - * published by the Free Software Foundation. Oracle designates this
143.11 - * particular file as subject to the "Classpath" exception as provided
143.12 - * by Oracle in the LICENSE file that accompanied this code.
143.13 - *
143.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
143.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
143.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
143.17 - * version 2 for more details (a copy is included in the LICENSE file that
143.18 - * accompanied this code).
143.19 - *
143.20 - * You should have received a copy of the GNU General Public License version
143.21 - * 2 along with this work; if not, write to the Free Software Foundation,
143.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
143.23 - *
143.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
143.25 - * or visit www.oracle.com if you need additional information or have any
143.26 - * questions.
143.27 - */
143.28 -
143.29 -
143.30 -package org.apidesign.javap;
143.31 -
143.32 -/**
143.33 - * Stores constant pool entry information with two fields.
143.34 - *
143.35 - * @author Sucheta Dambalkar (Adopted code from jdis)
143.36 - */
143.37 -class CPX2 {
143.38 - int cpx1,cpx2;
143.39 -
143.40 - CPX2 (int cpx1, int cpx2) {
143.41 - this.cpx1=cpx1;
143.42 - this.cpx2=cpx2;
143.43 - }
143.44 -}
144.1 --- a/rt/javap/src/main/java/org/apidesign/javap/ClassData.java Tue Mar 05 07:57:16 2013 +0100
144.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
144.3 @@ -1,713 +0,0 @@
144.4 -/*
144.5 - * Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved.
144.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
144.7 - *
144.8 - * This code is free software; you can redistribute it and/or modify it
144.9 - * under the terms of the GNU General Public License version 2 only, as
144.10 - * published by the Free Software Foundation. Oracle designates this
144.11 - * particular file as subject to the "Classpath" exception as provided
144.12 - * by Oracle in the LICENSE file that accompanied this code.
144.13 - *
144.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
144.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
144.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
144.17 - * version 2 for more details (a copy is included in the LICENSE file that
144.18 - * accompanied this code).
144.19 - *
144.20 - * You should have received a copy of the GNU General Public License version
144.21 - * 2 along with this work; if not, write to the Free Software Foundation,
144.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
144.23 - *
144.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
144.25 - * or visit www.oracle.com if you need additional information or have any
144.26 - * questions.
144.27 - */
144.28 -
144.29 -
144.30 -package org.apidesign.javap;
144.31 -
144.32 -import java.io.*;
144.33 -
144.34 -/**
144.35 - * Central data repository of the Java Disassembler.
144.36 - * Stores all the information in java class file.
144.37 - *
144.38 - * @author Sucheta Dambalkar (Adopted code from jdis)
144.39 - */
144.40 -public final class ClassData implements RuntimeConstants {
144.41 -
144.42 - private int magic;
144.43 - private int minor_version;
144.44 - private int major_version;
144.45 - private int cpool_count;
144.46 - private Object cpool[];
144.47 - private int access;
144.48 - private int this_class = 0;;
144.49 - private int super_class;
144.50 - private int interfaces_count;
144.51 - private int[] interfaces = new int[0];;
144.52 - private int fields_count;
144.53 - private FieldData[] fields;
144.54 - private int methods_count;
144.55 - private MethodData[] methods;
144.56 - private InnerClassData[] innerClasses;
144.57 - private int attributes_count;
144.58 - private AttrData[] attrs;
144.59 - private String classname;
144.60 - private String superclassname;
144.61 - private int source_cpx=0;
144.62 - private byte tags[];
144.63 - private Hashtable indexHashAscii = new Hashtable();
144.64 - private String pkgPrefix="";
144.65 - private int pkgPrefixLen=0;
144.66 -
144.67 - /**
144.68 - * Read classfile to disassemble.
144.69 - */
144.70 - public ClassData(InputStream infile) throws IOException {
144.71 - this.read(new DataInputStream(infile));
144.72 - }
144.73 -
144.74 - /**
144.75 - * Reads and stores class file information.
144.76 - */
144.77 - public void read(DataInputStream in) throws IOException {
144.78 - // Read the header
144.79 - magic = in.readInt();
144.80 - if (magic != JAVA_MAGIC) {
144.81 - throw new ClassFormatError("wrong magic: " +
144.82 - toHex(magic) + ", expected " +
144.83 - toHex(JAVA_MAGIC));
144.84 - }
144.85 - minor_version = in.readShort();
144.86 - major_version = in.readShort();
144.87 - if (major_version != JAVA_VERSION) {
144.88 - }
144.89 -
144.90 - // Read the constant pool
144.91 - readCP(in);
144.92 - access = in.readUnsignedShort();
144.93 - this_class = in.readUnsignedShort();
144.94 - super_class = in.readUnsignedShort();
144.95 -
144.96 - //Read interfaces.
144.97 - interfaces_count = in.readUnsignedShort();
144.98 - if(interfaces_count > 0){
144.99 - interfaces = new int[interfaces_count];
144.100 - }
144.101 - for (int i = 0; i < interfaces_count; i++) {
144.102 - interfaces[i]=in.readShort();
144.103 - }
144.104 -
144.105 - // Read the fields
144.106 - readFields(in);
144.107 -
144.108 - // Read the methods
144.109 - readMethods(in);
144.110 -
144.111 - // Read the attributes
144.112 - attributes_count = in.readUnsignedShort();
144.113 - attrs=new AttrData[attributes_count];
144.114 - for (int k = 0; k < attributes_count; k++) {
144.115 - int name_cpx=in.readUnsignedShort();
144.116 - if (getTag(name_cpx)==CONSTANT_UTF8
144.117 - && getString(name_cpx).equals("SourceFile")
144.118 - ){ if (in.readInt()!=2)
144.119 - throw new ClassFormatError("invalid attr length");
144.120 - source_cpx=in.readUnsignedShort();
144.121 - AttrData attr=new AttrData(this);
144.122 - attr.read(name_cpx);
144.123 - attrs[k]=attr;
144.124 -
144.125 - } else if (getTag(name_cpx)==CONSTANT_UTF8
144.126 - && getString(name_cpx).equals("InnerClasses")
144.127 - ){ int length=in.readInt();
144.128 - int num=in.readUnsignedShort();
144.129 - if (2+num*8 != length)
144.130 - throw new ClassFormatError("invalid attr length");
144.131 - innerClasses=new InnerClassData[num];
144.132 - for (int j = 0; j < num; j++) {
144.133 - InnerClassData innerClass=new InnerClassData(this);
144.134 - innerClass.read(in);
144.135 - innerClasses[j]=innerClass;
144.136 - }
144.137 - AttrData attr=new AttrData(this);
144.138 - attr.read(name_cpx);
144.139 - attrs[k]=attr;
144.140 - } else {
144.141 - AttrData attr=new AttrData(this);
144.142 - attr.read(name_cpx, in);
144.143 - attrs[k]=attr;
144.144 - }
144.145 - }
144.146 - in.close();
144.147 - } // end ClassData.read()
144.148 -
144.149 - /**
144.150 - * Reads and stores constant pool info.
144.151 - */
144.152 - void readCP(DataInputStream in) throws IOException {
144.153 - cpool_count = in.readUnsignedShort();
144.154 - tags = new byte[cpool_count];
144.155 - cpool = new Object[cpool_count];
144.156 - for (int i = 1; i < cpool_count; i++) {
144.157 - byte tag = in.readByte();
144.158 -
144.159 - switch(tags[i] = tag) {
144.160 - case CONSTANT_UTF8:
144.161 - String str=in.readUTF();
144.162 - indexHashAscii.put(cpool[i] = str, new Integer(i));
144.163 - break;
144.164 - case CONSTANT_INTEGER:
144.165 - cpool[i] = new Integer(in.readInt());
144.166 - break;
144.167 - case CONSTANT_FLOAT:
144.168 - cpool[i] = new Float(in.readFloat());
144.169 - break;
144.170 - case CONSTANT_LONG:
144.171 - cpool[i++] = new Long(in.readLong());
144.172 - break;
144.173 - case CONSTANT_DOUBLE:
144.174 - cpool[i++] = new Double(in.readDouble());
144.175 - break;
144.176 - case CONSTANT_CLASS:
144.177 - case CONSTANT_STRING:
144.178 - cpool[i] = new CPX(in.readUnsignedShort());
144.179 - break;
144.180 -
144.181 - case CONSTANT_FIELD:
144.182 - case CONSTANT_METHOD:
144.183 - case CONSTANT_INTERFACEMETHOD:
144.184 - case CONSTANT_NAMEANDTYPE:
144.185 - cpool[i] = new CPX2(in.readUnsignedShort(), in.readUnsignedShort());
144.186 - break;
144.187 -
144.188 - case 0:
144.189 - default:
144.190 - throw new ClassFormatError("invalid constant type: " + (int)tags[i]);
144.191 - }
144.192 - }
144.193 - }
144.194 -
144.195 - /**
144.196 - * Reads and strores field info.
144.197 - */
144.198 - protected void readFields(DataInputStream in) throws IOException {
144.199 - int fields_count = in.readUnsignedShort();
144.200 - fields=new FieldData[fields_count];
144.201 - for (int k = 0; k < fields_count; k++) {
144.202 - FieldData field=new FieldData(this);
144.203 - field.read(in);
144.204 - fields[k]=field;
144.205 - }
144.206 - }
144.207 -
144.208 - /**
144.209 - * Reads and strores Method info.
144.210 - */
144.211 - protected void readMethods(DataInputStream in) throws IOException {
144.212 - int methods_count = in.readUnsignedShort();
144.213 - methods=new MethodData[methods_count];
144.214 - for (int k = 0; k < methods_count ; k++) {
144.215 - MethodData method=new MethodData(this);
144.216 - method.read(in);
144.217 - methods[k]=method;
144.218 - }
144.219 - }
144.220 -
144.221 - /**
144.222 - * get a string
144.223 - */
144.224 - public String getString(int n) {
144.225 - if (n == 0) {
144.226 - return null;
144.227 - } else {
144.228 - return (String)cpool[n];
144.229 - }
144.230 - }
144.231 -
144.232 - /**
144.233 - * get the type of constant given an index
144.234 - */
144.235 - public byte getTag(int n) {
144.236 - try{
144.237 - return tags[n];
144.238 - } catch (ArrayIndexOutOfBoundsException e) {
144.239 - return (byte)100;
144.240 - }
144.241 - }
144.242 -
144.243 - static final String hexString="0123456789ABCDEF";
144.244 -
144.245 - public static char hexTable[]=hexString.toCharArray();
144.246 -
144.247 - static String toHex(long val, int width) {
144.248 - StringBuffer s = new StringBuffer();
144.249 - for (int i=width-1; i>=0; i--)
144.250 - s.append(hexTable[((int)(val>>(4*i)))&0xF]);
144.251 - return "0x"+s.toString();
144.252 - }
144.253 -
144.254 - static String toHex(long val) {
144.255 - int width;
144.256 - for (width=16; width>0; width--) {
144.257 - if ((val>>(width-1)*4)!=0) break;
144.258 - }
144.259 - return toHex(val, width);
144.260 - }
144.261 -
144.262 - static String toHex(int val) {
144.263 - int width;
144.264 - for (width=8; width>0; width--) {
144.265 - if ((val>>(width-1)*4)!=0) break;
144.266 - }
144.267 - return toHex(val, width);
144.268 - }
144.269 -
144.270 - /**
144.271 - * Returns the name of this class.
144.272 - */
144.273 - public String getClassName() {
144.274 - String res=null;
144.275 - if (this_class==0) {
144.276 - return res;
144.277 - }
144.278 - int tcpx;
144.279 - try {
144.280 - if (tags[this_class]!=CONSTANT_CLASS) {
144.281 - return res; //"<CP["+cpx+"] is not a Class> ";
144.282 - }
144.283 - tcpx=((CPX)cpool[this_class]).cpx;
144.284 - } catch (ArrayIndexOutOfBoundsException e) {
144.285 - return res; // "#"+cpx+"// invalid constant pool index";
144.286 - } catch (Throwable e) {
144.287 - return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
144.288 - }
144.289 -
144.290 - try {
144.291 - return (String)(cpool[tcpx]);
144.292 - } catch (ArrayIndexOutOfBoundsException e) {
144.293 - return res; // "class #"+scpx+"// invalid constant pool index";
144.294 - } catch (ClassCastException e) {
144.295 - return res; // "class #"+scpx+"// invalid constant pool reference";
144.296 - } catch (Throwable e) {
144.297 - return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
144.298 - }
144.299 -
144.300 - }
144.301 -
144.302 - /**
144.303 - * Returns the name of class at perticular index.
144.304 - */
144.305 - public String getClassName(int cpx) {
144.306 - String res="#"+cpx;
144.307 - if (cpx==0) {
144.308 - return res;
144.309 - }
144.310 - int scpx;
144.311 - try {
144.312 - if (tags[cpx]!=CONSTANT_CLASS) {
144.313 - return res; //"<CP["+cpx+"] is not a Class> ";
144.314 - }
144.315 - scpx=((CPX)cpool[cpx]).cpx;
144.316 - } catch (ArrayIndexOutOfBoundsException e) {
144.317 - return res; // "#"+cpx+"// invalid constant pool index";
144.318 - } catch (Throwable e) {
144.319 - return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
144.320 - }
144.321 - res="#"+scpx;
144.322 - try {
144.323 - return (String)(cpool[scpx]);
144.324 - } catch (ArrayIndexOutOfBoundsException e) {
144.325 - return res; // "class #"+scpx+"// invalid constant pool index";
144.326 - } catch (ClassCastException e) {
144.327 - return res; // "class #"+scpx+"// invalid constant pool reference";
144.328 - } catch (Throwable e) {
144.329 - return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
144.330 - }
144.331 - }
144.332 -
144.333 - public int getAccessFlags() {
144.334 - return access;
144.335 - }
144.336 -
144.337 - /**
144.338 - * Returns true if it is a class
144.339 - */
144.340 - public boolean isClass() {
144.341 - if((access & ACC_INTERFACE) == 0) return true;
144.342 - return false;
144.343 - }
144.344 -
144.345 - /**
144.346 - * Returns true if it is a interface.
144.347 - */
144.348 - public boolean isInterface(){
144.349 - if((access & ACC_INTERFACE) != 0) return true;
144.350 - return false;
144.351 - }
144.352 -
144.353 - /**
144.354 - * Returns true if this member is public, false otherwise.
144.355 - */
144.356 - public boolean isPublic(){
144.357 - return (access & ACC_PUBLIC) != 0;
144.358 - }
144.359 -
144.360 - /**
144.361 - * Returns the access of this class or interface.
144.362 - */
144.363 - public String[] getAccess(){
144.364 - Vector v = new Vector();
144.365 - if ((access & ACC_PUBLIC) !=0) v.addElement("public");
144.366 - if ((access & ACC_FINAL) !=0) v.addElement("final");
144.367 - if ((access & ACC_ABSTRACT) !=0) v.addElement("abstract");
144.368 - String[] accflags = new String[v.size()];
144.369 - v.copyInto(accflags);
144.370 - return accflags;
144.371 - }
144.372 -
144.373 - /**
144.374 - * Returns list of innerclasses.
144.375 - */
144.376 - public InnerClassData[] getInnerClasses(){
144.377 - return innerClasses;
144.378 - }
144.379 -
144.380 - /**
144.381 - * Returns list of attributes.
144.382 - */
144.383 - final AttrData[] getAttributes(){
144.384 - return attrs;
144.385 - }
144.386 -
144.387 - public byte[] findAnnotationData(boolean classRetention) {
144.388 - String n = classRetention ?
144.389 - "RuntimeInvisibleAnnotations" : // NOI18N
144.390 - "RuntimeVisibleAnnotations"; // NOI18N
144.391 - return findAttr(n, attrs);
144.392 - }
144.393 -
144.394 - /**
144.395 - * Returns true if superbit is set.
144.396 - */
144.397 - public boolean isSuperSet(){
144.398 - if ((access & ACC_SUPER) !=0) return true;
144.399 - return false;
144.400 - }
144.401 -
144.402 - /**
144.403 - * Returns super class name.
144.404 - */
144.405 - public String getSuperClassName(){
144.406 - String res=null;
144.407 - if (super_class==0) {
144.408 - return res;
144.409 - }
144.410 - int scpx;
144.411 - try {
144.412 - if (tags[super_class]!=CONSTANT_CLASS) {
144.413 - return res; //"<CP["+cpx+"] is not a Class> ";
144.414 - }
144.415 - scpx=((CPX)cpool[super_class]).cpx;
144.416 - } catch (ArrayIndexOutOfBoundsException e) {
144.417 - return res; // "#"+cpx+"// invalid constant pool index";
144.418 - } catch (Throwable e) {
144.419 - return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
144.420 - }
144.421 -
144.422 - try {
144.423 - return (String)(cpool[scpx]);
144.424 - } catch (ArrayIndexOutOfBoundsException e) {
144.425 - return res; // "class #"+scpx+"// invalid constant pool index";
144.426 - } catch (ClassCastException e) {
144.427 - return res; // "class #"+scpx+"// invalid constant pool reference";
144.428 - } catch (Throwable e) {
144.429 - return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
144.430 - }
144.431 - }
144.432 -
144.433 - /**
144.434 - * Returns list of super interfaces.
144.435 - */
144.436 - public String[] getSuperInterfaces(){
144.437 - String interfacenames[] = new String[interfaces.length];
144.438 - int interfacecpx = -1;
144.439 - for(int i = 0; i < interfaces.length; i++){
144.440 - interfacecpx=((CPX)cpool[interfaces[i]]).cpx;
144.441 - interfacenames[i] = (String)(cpool[interfacecpx]);
144.442 - }
144.443 - return interfacenames;
144.444 - }
144.445 -
144.446 - /**
144.447 - * Returns string at prticular constant pool index.
144.448 - */
144.449 - public String getStringValue(int cpoolx) {
144.450 - try {
144.451 - return ((String)cpool[cpoolx]);
144.452 - } catch (ArrayIndexOutOfBoundsException e) {
144.453 - return "//invalid constant pool index:"+cpoolx;
144.454 - } catch (ClassCastException e) {
144.455 - return "//invalid constant pool ref:"+cpoolx;
144.456 - }
144.457 - }
144.458 -
144.459 - /**
144.460 - * Returns list of field info.
144.461 - */
144.462 - public FieldData[] getFields(){
144.463 - return fields;
144.464 - }
144.465 -
144.466 - /**
144.467 - * Returns list of method info.
144.468 - */
144.469 - public MethodData[] getMethods(){
144.470 - return methods;
144.471 - }
144.472 -
144.473 - /**
144.474 - * Returns constant pool entry at that index.
144.475 - */
144.476 - public CPX2 getCpoolEntry(int cpx){
144.477 - return ((CPX2)(cpool[cpx]));
144.478 - }
144.479 -
144.480 - public Object getCpoolEntryobj(int cpx){
144.481 - return (cpool[cpx]);
144.482 - }
144.483 -
144.484 - /**
144.485 - * Returns index of this class.
144.486 - */
144.487 - public int getthis_cpx(){
144.488 - return this_class;
144.489 - }
144.490 -
144.491 - /**
144.492 - * Returns string at that index.
144.493 - */
144.494 - public String StringValue(int cpx) {
144.495 - return stringValue(cpx, false);
144.496 - }
144.497 - public String stringValue(int cpx, boolean textual) {
144.498 - return stringValue(cpx, textual, null);
144.499 - }
144.500 - public String stringValue(int cpx, String[] classRefs) {
144.501 - return stringValue(cpx, true, classRefs);
144.502 - }
144.503 - private String stringValue(int cpx, boolean textual, String[] refs) {
144.504 - if (cpx==0) return "#0";
144.505 - int tag;
144.506 - Object x;
144.507 - String suffix="";
144.508 - try {
144.509 - tag=tags[cpx];
144.510 - x=cpool[cpx];
144.511 - } catch (IndexOutOfBoundsException e) {
144.512 - return "<Incorrect CP index:"+cpx+">";
144.513 - }
144.514 -
144.515 - if (x==null) return "<NULL>";
144.516 - switch (tag) {
144.517 - case CONSTANT_UTF8: {
144.518 - if (!textual) {
144.519 - return (String)x;
144.520 - }
144.521 - StringBuilder sb=new StringBuilder();
144.522 - String s=(String)x;
144.523 - for (int k=0; k<s.length(); k++) {
144.524 - char c=s.charAt(k);
144.525 - switch (c) {
144.526 - case '\\': sb.append('\\').append('\\'); break;
144.527 - case '\t': sb.append('\\').append('t'); break;
144.528 - case '\n': sb.append('\\').append('n'); break;
144.529 - case '\r': sb.append('\\').append('r'); break;
144.530 - case '\"': sb.append('\\').append('\"'); break;
144.531 - default: sb.append(c);
144.532 - }
144.533 - }
144.534 - return sb.toString();
144.535 - }
144.536 - case CONSTANT_DOUBLE: {
144.537 - Double d=(Double)x;
144.538 - String sd=d.toString();
144.539 - if (textual) {
144.540 - return sd;
144.541 - }
144.542 - return sd+"d";
144.543 - }
144.544 - case CONSTANT_FLOAT: {
144.545 - Float f=(Float)x;
144.546 - String sf=(f).toString();
144.547 - if (textual) {
144.548 - return sf;
144.549 - }
144.550 - return sf+"f";
144.551 - }
144.552 - case CONSTANT_LONG: {
144.553 - Long ln = (Long)x;
144.554 - if (textual) {
144.555 - return ln.toString();
144.556 - }
144.557 - return ln.toString()+'l';
144.558 - }
144.559 - case CONSTANT_INTEGER: {
144.560 - Integer in = (Integer)x;
144.561 - return in.toString();
144.562 - }
144.563 - case CONSTANT_CLASS:
144.564 - String jn = getClassName(cpx);
144.565 - if (textual) {
144.566 - if (refs != null) {
144.567 - refs[0] = jn;
144.568 - }
144.569 - return jn;
144.570 - }
144.571 - return javaName(jn);
144.572 - case CONSTANT_STRING:
144.573 - String sv = stringValue(((CPX)x).cpx, textual);
144.574 - if (textual) {
144.575 - return '"' + sv + '"';
144.576 - } else {
144.577 - return sv;
144.578 - }
144.579 - case CONSTANT_FIELD:
144.580 - case CONSTANT_METHOD:
144.581 - case CONSTANT_INTERFACEMETHOD:
144.582 - //return getShortClassName(((CPX2)x).cpx1)+"."+StringValue(((CPX2)x).cpx2);
144.583 - return javaName(getClassName(((CPX2)x).cpx1))+"."+StringValue(((CPX2)x).cpx2);
144.584 -
144.585 - case CONSTANT_NAMEANDTYPE:
144.586 - return getName(((CPX2)x).cpx1)+":"+StringValue(((CPX2)x).cpx2);
144.587 - default:
144.588 - return "UnknownTag"; //TBD
144.589 - }
144.590 - }
144.591 -
144.592 - /**
144.593 - * Returns resolved java type name.
144.594 - */
144.595 - public String javaName(String name) {
144.596 - if( name==null) return "null";
144.597 - int len=name.length();
144.598 - if (len==0) return "\"\"";
144.599 - int cc='/';
144.600 - fullname: { // xxx/yyy/zzz
144.601 - int cp;
144.602 - for (int k=0; k<len; k += Character.charCount(cp)) {
144.603 - cp=name.codePointAt(k);
144.604 - if (cc=='/') {
144.605 - if (!isJavaIdentifierStart(cp)) break fullname;
144.606 - } else if (cp!='/') {
144.607 - if (!isJavaIdentifierPart(cp)) break fullname;
144.608 - }
144.609 - cc=cp;
144.610 - }
144.611 - return name;
144.612 - }
144.613 - return "\""+name+"\"";
144.614 - }
144.615 -
144.616 - public String getName(int cpx) {
144.617 - String res;
144.618 - try {
144.619 - return javaName((String)cpool[cpx]); //.replace('/','.');
144.620 - } catch (ArrayIndexOutOfBoundsException e) {
144.621 - return "<invalid constant pool index:"+cpx+">";
144.622 - } catch (ClassCastException e) {
144.623 - return "<invalid constant pool ref:"+cpx+">";
144.624 - }
144.625 - }
144.626 -
144.627 - /**
144.628 - * Returns unqualified class name.
144.629 - */
144.630 - public String getShortClassName(int cpx) {
144.631 - String classname=javaName(getClassName(cpx));
144.632 - pkgPrefixLen=classname.lastIndexOf("/")+1;
144.633 - if (pkgPrefixLen!=0) {
144.634 - pkgPrefix=classname.substring(0,pkgPrefixLen);
144.635 - if (classname.startsWith(pkgPrefix)) {
144.636 - return classname.substring(pkgPrefixLen);
144.637 - }
144.638 - }
144.639 - return classname;
144.640 - }
144.641 -
144.642 - /**
144.643 - * Returns source file name.
144.644 - */
144.645 - public String getSourceName(){
144.646 - return getName(source_cpx);
144.647 - }
144.648 -
144.649 - /**
144.650 - * Returns package name.
144.651 - */
144.652 - public String getPkgName(){
144.653 - String classname=getClassName(this_class);
144.654 - pkgPrefixLen=classname.lastIndexOf("/")+1;
144.655 - if (pkgPrefixLen!=0) {
144.656 - pkgPrefix=classname.substring(0,pkgPrefixLen);
144.657 - return("package "+pkgPrefix.substring(0,pkgPrefixLen-1)+";\n");
144.658 - }else return null;
144.659 - }
144.660 -
144.661 - /**
144.662 - * Returns total constant pool entry count.
144.663 - */
144.664 - public int getCpoolCount(){
144.665 - return cpool_count;
144.666 - }
144.667 -
144.668 - /**
144.669 - * Returns minor version of class file.
144.670 - */
144.671 - public int getMinor_version(){
144.672 - return minor_version;
144.673 - }
144.674 -
144.675 - /**
144.676 - * Returns major version of class file.
144.677 - */
144.678 - public int getMajor_version(){
144.679 - return major_version;
144.680 - }
144.681 -
144.682 - private boolean isJavaIdentifierStart(int cp) {
144.683 - return ('a' <= cp && cp <= 'z') || ('A' <= cp && cp <= 'Z');
144.684 - }
144.685 -
144.686 - private boolean isJavaIdentifierPart(int cp) {
144.687 - return isJavaIdentifierStart(cp) || ('0' <= cp && cp <= '9');
144.688 - }
144.689 -
144.690 - public String[] getNameAndType(int indx) {
144.691 - return getNameAndType(indx, 0, new String[2]);
144.692 - }
144.693 -
144.694 - private String[] getNameAndType(int indx, int at, String[] arr) {
144.695 - CPX2 c2 = getCpoolEntry(indx);
144.696 - arr[at] = StringValue(c2.cpx1);
144.697 - arr[at + 1] = StringValue(c2.cpx2);
144.698 - return arr;
144.699 - }
144.700 -
144.701 - public String[] getFieldInfoName(int indx) {
144.702 - CPX2 c2 = getCpoolEntry(indx);
144.703 - String[] arr = new String[3];
144.704 - arr[0] = getClassName(c2.cpx1);
144.705 - return getNameAndType(c2.cpx2, 1, arr);
144.706 - }
144.707 -
144.708 - static byte[] findAttr(String n, AttrData[] attrs) {
144.709 - for (AttrData ad : attrs) {
144.710 - if (n.equals(ad.getAttrName())) {
144.711 - return ad.getData();
144.712 - }
144.713 - }
144.714 - return null;
144.715 - }
144.716 -}
145.1 --- a/rt/javap/src/main/java/org/apidesign/javap/Constants.java Tue Mar 05 07:57:16 2013 +0100
145.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
145.3 @@ -1,372 +0,0 @@
145.4 -/*
145.5 - * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
145.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
145.7 - *
145.8 - * This code is free software; you can redistribute it and/or modify it
145.9 - * under the terms of the GNU General Public License version 2 only, as
145.10 - * published by the Free Software Foundation. Oracle designates this
145.11 - * particular file as subject to the "Classpath" exception as provided
145.12 - * by Oracle in the LICENSE file that accompanied this code.
145.13 - *
145.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
145.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
145.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
145.17 - * version 2 for more details (a copy is included in the LICENSE file that
145.18 - * accompanied this code).
145.19 - *
145.20 - * You should have received a copy of the GNU General Public License version
145.21 - * 2 along with this work; if not, write to the Free Software Foundation,
145.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
145.23 - *
145.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
145.25 - * or visit www.oracle.com if you need additional information or have any
145.26 - * questions.
145.27 - */
145.28 -
145.29 -
145.30 -
145.31 -package org.apidesign.javap;
145.32 -
145.33 -/**
145.34 - * This interface defines constant that are used
145.35 - * throughout the compiler. It inherits from RuntimeConstants,
145.36 - * which is an autogenerated class that contains contstants
145.37 - * defined in the interpreter.
145.38 - */
145.39 -
145.40 -public
145.41 -interface Constants extends RuntimeConstants {
145.42 -
145.43 - /**
145.44 - * End of input
145.45 - */
145.46 - public static final int EOF = -1;
145.47 -
145.48 - /*
145.49 - * Flags
145.50 - */
145.51 - public static final int F_VERBOSE = 1 << 0;
145.52 - public static final int F_DUMP = 1 << 1;
145.53 - public static final int F_WARNINGS = 1 << 2;
145.54 - public static final int F_DEBUG = 1 << 3;
145.55 - public static final int F_OPTIMIZE = 1 << 4;
145.56 - public static final int F_DEPENDENCIES = 1 << 5;
145.57 -
145.58 - /*
145.59 - * Type codes
145.60 - */
145.61 - public static final int TC_BOOLEAN = 0;
145.62 - public static final int TC_BYTE = 1;
145.63 - public static final int TC_CHAR = 2;
145.64 - public static final int TC_SHORT = 3;
145.65 - public static final int TC_INT = 4;
145.66 - public static final int TC_LONG = 5;
145.67 - public static final int TC_FLOAT = 6;
145.68 - public static final int TC_DOUBLE = 7;
145.69 - public static final int TC_NULL = 8;
145.70 - public static final int TC_ARRAY = 9;
145.71 - public static final int TC_CLASS = 10;
145.72 - public static final int TC_VOID = 11;
145.73 - public static final int TC_METHOD = 12;
145.74 - public static final int TC_ERROR = 13;
145.75 -
145.76 - /*
145.77 - * Type Masks
145.78 - */
145.79 - public static final int TM_NULL = 1 << TC_NULL;
145.80 - public static final int TM_VOID = 1 << TC_VOID;
145.81 - public static final int TM_BOOLEAN = 1 << TC_BOOLEAN;
145.82 - public static final int TM_BYTE = 1 << TC_BYTE;
145.83 - public static final int TM_CHAR = 1 << TC_CHAR;
145.84 - public static final int TM_SHORT = 1 << TC_SHORT;
145.85 - public static final int TM_INT = 1 << TC_INT;
145.86 - public static final int TM_LONG = 1 << TC_LONG;
145.87 - public static final int TM_FLOAT = 1 << TC_FLOAT;
145.88 - public static final int TM_DOUBLE = 1 << TC_DOUBLE;
145.89 - public static final int TM_ARRAY = 1 << TC_ARRAY;
145.90 - public static final int TM_CLASS = 1 << TC_CLASS;
145.91 - public static final int TM_METHOD = 1 << TC_METHOD;
145.92 - public static final int TM_ERROR = 1 << TC_ERROR;
145.93 -
145.94 - public static final int TM_INT32 = TM_BYTE | TM_SHORT | TM_CHAR | TM_INT;
145.95 - public static final int TM_NUM32 = TM_INT32 | TM_FLOAT;
145.96 - public static final int TM_NUM64 = TM_LONG | TM_DOUBLE;
145.97 - public static final int TM_INTEGER = TM_INT32 | TM_LONG;
145.98 - public static final int TM_REAL = TM_FLOAT | TM_DOUBLE;
145.99 - public static final int TM_NUMBER = TM_INTEGER | TM_REAL;
145.100 - public static final int TM_REFERENCE = TM_ARRAY | TM_CLASS | TM_NULL;
145.101 -
145.102 - /*
145.103 - * Class status
145.104 - */
145.105 - public static final int CS_UNDEFINED = 0;
145.106 - public static final int CS_UNDECIDED = 1;
145.107 - public static final int CS_BINARY = 2;
145.108 - public static final int CS_SOURCE = 3;
145.109 - public static final int CS_PARSED = 4;
145.110 - public static final int CS_COMPILED = 5;
145.111 - public static final int CS_NOTFOUND = 6;
145.112 -
145.113 - /*
145.114 - * Attributes
145.115 - */
145.116 - public static final int ATT_ALL = -1;
145.117 - public static final int ATT_CODE = 1;
145.118 -
145.119 - /*
145.120 - * Number of bits used in file offsets
145.121 - */
145.122 - public static final int OFFSETBITS = 19;
145.123 - public static final int MAXFILESIZE = (1 << OFFSETBITS) - 1;
145.124 - public static final int MAXLINENUMBER = (1 << (32 - OFFSETBITS)) - 1;
145.125 -
145.126 - /*
145.127 - * Operators
145.128 - */
145.129 - public final int COMMA = 0;
145.130 - public final int ASSIGN = 1;
145.131 -
145.132 - public final int ASGMUL = 2;
145.133 - public final int ASGDIV = 3;
145.134 - public final int ASGREM = 4;
145.135 - public final int ASGADD = 5;
145.136 - public final int ASGSUB = 6;
145.137 - public final int ASGLSHIFT = 7;
145.138 - public final int ASGRSHIFT = 8;
145.139 - public final int ASGURSHIFT = 9;
145.140 - public final int ASGBITAND = 10;
145.141 - public final int ASGBITOR = 11;
145.142 - public final int ASGBITXOR = 12;
145.143 -
145.144 - public final int COND = 13;
145.145 - public final int OR = 14;
145.146 - public final int AND = 15;
145.147 - public final int BITOR = 16;
145.148 - public final int BITXOR = 17;
145.149 - public final int BITAND = 18;
145.150 - public final int NE = 19;
145.151 - public final int EQ = 20;
145.152 - public final int GE = 21;
145.153 - public final int GT = 22;
145.154 - public final int LE = 23;
145.155 - public final int LT = 24;
145.156 - public final int INSTANCEOF = 25;
145.157 - public final int LSHIFT = 26;
145.158 - public final int RSHIFT = 27;
145.159 - public final int URSHIFT = 28;
145.160 - public final int ADD = 29;
145.161 - public final int SUB = 30;
145.162 - public final int DIV = 31;
145.163 - public final int REM = 32;
145.164 - public final int MUL = 33;
145.165 - public final int CAST = 34; // (x)y
145.166 - public final int POS = 35; // +x
145.167 - public final int NEG = 36; // -x
145.168 - public final int NOT = 37;
145.169 - public final int BITNOT = 38;
145.170 - public final int PREINC = 39; // ++x
145.171 - public final int PREDEC = 40; // --x
145.172 - public final int NEWARRAY = 41;
145.173 - public final int NEWINSTANCE = 42;
145.174 - public final int NEWFROMNAME = 43;
145.175 - public final int POSTINC = 44; // x++
145.176 - public final int POSTDEC = 45; // x--
145.177 - public final int FIELD = 46;
145.178 - public final int METHOD = 47; // x(y)
145.179 - public final int ARRAYACCESS = 48; // x[y]
145.180 - public final int NEW = 49;
145.181 - public final int INC = 50;
145.182 - public final int DEC = 51;
145.183 -
145.184 - public final int CONVERT = 55; // implicit conversion
145.185 - public final int EXPR = 56; // (x)
145.186 - public final int ARRAY = 57; // {x, y, ...}
145.187 - public final int GOTO = 58;
145.188 -
145.189 - /*
145.190 - * Value tokens
145.191 - */
145.192 - public final int IDENT = 60;
145.193 - public final int BOOLEANVAL = 61;
145.194 - public final int BYTEVAL = 62;
145.195 - public final int CHARVAL = 63;
145.196 - public final int SHORTVAL = 64;
145.197 - public final int INTVAL = 65;
145.198 - public final int LONGVAL = 66;
145.199 - public final int FLOATVAL = 67;
145.200 - public final int DOUBLEVAL = 68;
145.201 - public final int STRINGVAL = 69;
145.202 -
145.203 - /*
145.204 - * Type keywords
145.205 - */
145.206 - public final int BYTE = 70;
145.207 - public final int CHAR = 71;
145.208 - public final int SHORT = 72;
145.209 - public final int INT = 73;
145.210 - public final int LONG = 74;
145.211 - public final int FLOAT = 75;
145.212 - public final int DOUBLE = 76;
145.213 - public final int VOID = 77;
145.214 - public final int BOOLEAN = 78;
145.215 -
145.216 - /*
145.217 - * Expression keywords
145.218 - */
145.219 - public final int TRUE = 80;
145.220 - public final int FALSE = 81;
145.221 - public final int THIS = 82;
145.222 - public final int SUPER = 83;
145.223 - public final int NULL = 84;
145.224 -
145.225 - /*
145.226 - * Statement keywords
145.227 - */
145.228 - public final int IF = 90;
145.229 - public final int ELSE = 91;
145.230 - public final int FOR = 92;
145.231 - public final int WHILE = 93;
145.232 - public final int DO = 94;
145.233 - public final int SWITCH = 95;
145.234 - public final int CASE = 96;
145.235 - public final int DEFAULT = 97;
145.236 - public final int BREAK = 98;
145.237 - public final int CONTINUE = 99;
145.238 - public final int RETURN = 100;
145.239 - public final int TRY = 101;
145.240 - public final int CATCH = 102;
145.241 - public final int FINALLY = 103;
145.242 - public final int THROW = 104;
145.243 - public final int STAT = 105;
145.244 - public final int EXPRESSION = 106;
145.245 - public final int DECLARATION = 107;
145.246 - public final int VARDECLARATION = 108;
145.247 -
145.248 - /*
145.249 - * Declaration keywords
145.250 - */
145.251 - public final int IMPORT = 110;
145.252 - public final int CLASS = 111;
145.253 - public final int EXTENDS = 112;
145.254 - public final int IMPLEMENTS = 113;
145.255 - public final int INTERFACE = 114;
145.256 - public final int PACKAGE = 115;
145.257 -
145.258 - /*
145.259 - * Modifier keywords
145.260 - */
145.261 - public final int PRIVATE = 120;
145.262 - public final int PUBLIC = 121;
145.263 - public final int PROTECTED = 122;
145.264 - public final int CONST = 123;
145.265 - public final int STATIC = 124;
145.266 - public final int TRANSIENT = 125;
145.267 - public final int SYNCHRONIZED = 126;
145.268 - public final int NATIVE = 127;
145.269 - public final int FINAL = 128;
145.270 - public final int VOLATILE = 129;
145.271 - public final int ABSTRACT = 130;
145.272 - public final int STRICT = 165;
145.273 -
145.274 - /*
145.275 - * Punctuation
145.276 - */
145.277 - public final int SEMICOLON = 135;
145.278 - public final int COLON = 136;
145.279 - public final int QUESTIONMARK = 137;
145.280 - public final int LBRACE = 138;
145.281 - public final int RBRACE = 139;
145.282 - public final int LPAREN = 140;
145.283 - public final int RPAREN = 141;
145.284 - public final int LSQBRACKET = 142;
145.285 - public final int RSQBRACKET = 143;
145.286 - public final int THROWS = 144;
145.287 -
145.288 - /*
145.289 - * Special tokens
145.290 - */
145.291 - public final int ERROR = 145; // an error
145.292 - public final int COMMENT = 146; // not used anymore.
145.293 - public final int TYPE = 147;
145.294 - public final int LENGTH = 148;
145.295 - public final int INLINERETURN = 149;
145.296 - public final int INLINEMETHOD = 150;
145.297 - public final int INLINENEWINSTANCE = 151;
145.298 -
145.299 - /*
145.300 - * Added for jasm
145.301 - */
145.302 - public final int METHODREF = 152;
145.303 - public final int FIELDREF = 153;
145.304 - public final int STACK = 154;
145.305 - public final int LOCAL = 155;
145.306 - public final int CPINDEX = 156;
145.307 - public final int CPNAME = 157;
145.308 - public final int SIGN = 158;
145.309 - public final int BITS = 159;
145.310 - public final int INF = 160;
145.311 - public final int NAN = 161;
145.312 - public final int INNERCLASS = 162;
145.313 - public final int OF = 163;
145.314 - public final int SYNTHETIC = 164;
145.315 -// last used=165;
145.316 -
145.317 - /*
145.318 - * Operator precedence
145.319 - */
145.320 - public static final int opPrecedence[] = {
145.321 - 10, 11, 11, 11, 11, 11, 11, 11, 11, 11,
145.322 - 11, 11, 11, 12, 13, 14, 15, 16, 17, 18,
145.323 - 18, 19, 19, 19, 19, 19, 20, 20, 20, 21,
145.324 - 21, 22, 22, 22, 23, 24, 24, 24, 24, 24,
145.325 - 24, 25, 25, 26, 26, 26, 26, 26, 26
145.326 - };
145.327 -
145.328 - /*
145.329 - * Operator names
145.330 - */
145.331 - public static final String opNames[] = {
145.332 - ",", "=", "*=", "/=", "%=",
145.333 - "+=", "-=", "<<=", ">>=", "<<<=",
145.334 - "&=", "|=", "^=", "?:", "||",
145.335 - "&&", "|", "^", "&", "!=",
145.336 - "==", ">=", ">", "<=", "<",
145.337 - "instanceof", "<<", ">>", "<<<", "+",
145.338 - "-", "/", "%", "*", "cast",
145.339 - "+", "-", "!", "~", "++",
145.340 - "--", "new", "new", "new", "++",
145.341 - "--", "field", "method", "[]", "new",
145.342 - "++", "--", null, null, null,
145.343 -
145.344 - "convert", "expr", "array", "goto", null,
145.345 -
145.346 - "Identifier", "Boolean", "Byte", "Char", "Short",
145.347 - "Integer", "Long", "Float", "Double", "String",
145.348 -
145.349 - "byte", "char", "short", "int", "long",
145.350 - "float", "double", "void", "boolean", null,
145.351 -
145.352 - "true", "false", "this", "super", "null",
145.353 - null, null, null, null, null,
145.354 -
145.355 - "if", "else", "for", "while", "do",
145.356 - "switch", "case", "default", "break", "continue",
145.357 - "return", "try", "catch", "finally", "throw",
145.358 - "stat", "expression", "declaration", "declaration", null,
145.359 -
145.360 - "import", "class", "extends", "implements", "interface",
145.361 - "package", null, null, null, null,
145.362 -
145.363 - "private", "public", "protected", "const", "static",
145.364 - "transient", "synchronized", "native", "final", "volatile",
145.365 - "abstract", null, null, null, null,
145.366 -
145.367 - ";", ":", "?", "{", "}",
145.368 - "(", ")", "[", "]", "throws",
145.369 - "error", "comment", "type", "length", "inline-return",
145.370 - "inline-method", "inline-new",
145.371 - "method", "field", "stack", "locals", "CPINDEX", "CPName", "SIGN",
145.372 - "bits", "INF", "NaN", "InnerClass", "of", "synthetic"
145.373 - };
145.374 -
145.375 -}
146.1 --- a/rt/javap/src/main/java/org/apidesign/javap/FieldData.java Tue Mar 05 07:57:16 2013 +0100
146.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
146.3 @@ -1,168 +0,0 @@
146.4 -/*
146.5 - * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
146.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
146.7 - *
146.8 - * This code is free software; you can redistribute it and/or modify it
146.9 - * under the terms of the GNU General Public License version 2 only, as
146.10 - * published by the Free Software Foundation. Oracle designates this
146.11 - * particular file as subject to the "Classpath" exception as provided
146.12 - * by Oracle in the LICENSE file that accompanied this code.
146.13 - *
146.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
146.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
146.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
146.17 - * version 2 for more details (a copy is included in the LICENSE file that
146.18 - * accompanied this code).
146.19 - *
146.20 - * You should have received a copy of the GNU General Public License version
146.21 - * 2 along with this work; if not, write to the Free Software Foundation,
146.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
146.23 - *
146.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
146.25 - * or visit www.oracle.com if you need additional information or have any
146.26 - * questions.
146.27 - */
146.28 -
146.29 -
146.30 -package org.apidesign.javap;
146.31 -
146.32 -import java.io.*;
146.33 -
146.34 -/**
146.35 - * Strores field data informastion.
146.36 - *
146.37 - * @author Sucheta Dambalkar (Adopted code from jdis)
146.38 - */
146.39 -
146.40 -public class FieldData implements RuntimeConstants {
146.41 -
146.42 - ClassData cls;
146.43 - int access;
146.44 - int name_index;
146.45 - int descriptor_index;
146.46 - int attributes_count;
146.47 - int value_cpx=0;
146.48 - boolean isSynthetic=false;
146.49 - boolean isDeprecated=false;
146.50 - Vector attrs;
146.51 -
146.52 - public FieldData(ClassData cls){
146.53 - this.cls=cls;
146.54 - }
146.55 -
146.56 - /**
146.57 - * Read and store field info.
146.58 - */
146.59 - public void read(DataInputStream in) throws IOException {
146.60 - access = in.readUnsignedShort();
146.61 - name_index = in.readUnsignedShort();
146.62 - descriptor_index = in.readUnsignedShort();
146.63 - // Read the attributes
146.64 - int attributes_count = in.readUnsignedShort();
146.65 - attrs=new Vector(attributes_count);
146.66 - for (int i = 0; i < attributes_count; i++) {
146.67 - int attr_name_index=in.readUnsignedShort();
146.68 - if (cls.getTag(attr_name_index)!=CONSTANT_UTF8) continue;
146.69 - String attr_name=cls.getString(attr_name_index);
146.70 - if (attr_name.equals("ConstantValue")){
146.71 - if (in.readInt()!=2)
146.72 - throw new ClassFormatError("invalid ConstantValue attr length");
146.73 - value_cpx=in.readUnsignedShort();
146.74 - AttrData attr=new AttrData(cls);
146.75 - attr.read(attr_name_index);
146.76 - attrs.addElement(attr);
146.77 - } else if (attr_name.equals("Synthetic")){
146.78 - if (in.readInt()!=0)
146.79 - throw new ClassFormatError("invalid Synthetic attr length");
146.80 - isSynthetic=true;
146.81 - AttrData attr=new AttrData(cls);
146.82 - attr.read(attr_name_index);
146.83 - attrs.addElement(attr);
146.84 - } else if (attr_name.equals("Deprecated")){
146.85 - if (in.readInt()!=0)
146.86 - throw new ClassFormatError("invalid Synthetic attr length");
146.87 - isDeprecated = true;
146.88 - AttrData attr=new AttrData(cls);
146.89 - attr.read(attr_name_index);
146.90 - attrs.addElement(attr);
146.91 - } else {
146.92 - AttrData attr=new AttrData(cls);
146.93 - attr.read(attr_name_index, in);
146.94 - attrs.addElement(attr);
146.95 - }
146.96 - }
146.97 -
146.98 - } // end read
146.99 -
146.100 - public boolean isStatic() {
146.101 - return (access & ACC_STATIC) != 0;
146.102 - }
146.103 -
146.104 - /**
146.105 - * Returns access of a field.
146.106 - */
146.107 - public String[] getAccess(){
146.108 - Vector v = new Vector();
146.109 - if ((access & ACC_PUBLIC) !=0) v.addElement("public");
146.110 - if ((access & ACC_PRIVATE) !=0) v.addElement("private");
146.111 - if ((access & ACC_PROTECTED) !=0) v.addElement("protected");
146.112 - if ((access & ACC_STATIC) !=0) v.addElement("static");
146.113 - if ((access & ACC_FINAL) !=0) v.addElement("final");
146.114 - if ((access & ACC_VOLATILE) !=0) v.addElement("volatile");
146.115 - if ((access & ACC_TRANSIENT) !=0) v.addElement("transient");
146.116 - String[] accflags = new String[v.size()];
146.117 - v.copyInto(accflags);
146.118 - return accflags;
146.119 - }
146.120 -
146.121 - /**
146.122 - * Returns name of a field.
146.123 - */
146.124 - public String getName(){
146.125 - return cls.getStringValue(name_index);
146.126 - }
146.127 -
146.128 - /**
146.129 - * Returns internal signature of a field
146.130 - */
146.131 - public String getInternalSig(){
146.132 - return cls.getStringValue(descriptor_index);
146.133 - }
146.134 -
146.135 - /**
146.136 - * Returns true if field is synthetic.
146.137 - */
146.138 - public boolean isSynthetic(){
146.139 - return isSynthetic;
146.140 - }
146.141 -
146.142 - /**
146.143 - * Returns true if field is deprecated.
146.144 - */
146.145 - public boolean isDeprecated(){
146.146 - return isDeprecated;
146.147 - }
146.148 -
146.149 - /**
146.150 - * Returns index of constant value in cpool.
146.151 - */
146.152 - public int getConstantValueIndex(){
146.153 - return (value_cpx);
146.154 - }
146.155 -
146.156 - /**
146.157 - * Returns list of attributes of field.
146.158 - */
146.159 - public Vector getAttributes(){
146.160 - return attrs;
146.161 - }
146.162 -
146.163 - public byte[] findAnnotationData(boolean classRetention) {
146.164 - String n = classRetention ?
146.165 - "RuntimeInvisibleAnnotations" : // NOI18N
146.166 - "RuntimeVisibleAnnotations"; // NOI18N
146.167 - AttrData[] arr = new AttrData[attrs.size()];
146.168 - attrs.copyInto(arr);
146.169 - return ClassData.findAttr(n, arr);
146.170 - }
146.171 -}
147.1 --- a/rt/javap/src/main/java/org/apidesign/javap/Hashtable.java Tue Mar 05 07:57:16 2013 +0100
147.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
147.3 @@ -1,94 +0,0 @@
147.4 -/**
147.5 - * Back 2 Browser Bytecode Translator
147.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
147.7 - *
147.8 - * This program is free software: you can redistribute it and/or modify
147.9 - * it under the terms of the GNU General Public License as published by
147.10 - * the Free Software Foundation, version 2 of the License.
147.11 - *
147.12 - * This program is distributed in the hope that it will be useful,
147.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
147.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
147.15 - * GNU General Public License for more details.
147.16 - *
147.17 - * You should have received a copy of the GNU General Public License
147.18 - * along with this program. Look for COPYING file in the top folder.
147.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
147.20 - */
147.21 -package org.apidesign.javap;
147.22 -
147.23 -/** A JavaScript optimized replacement for Hashtable.
147.24 - *
147.25 - * @author Jaroslav Tulach <jtulach@netbeans.org>
147.26 - */
147.27 -final class Hashtable {
147.28 - private Object[] keys;
147.29 - private Object[] values;
147.30 -
147.31 - Hashtable(int i) {
147.32 - this();
147.33 - }
147.34 -
147.35 - Hashtable(int i, double d) {
147.36 - this();
147.37 - }
147.38 -
147.39 - Hashtable() {
147.40 - }
147.41 -
147.42 - synchronized void put(Object key, Object val) {
147.43 - int[] where = { -1, -1 };
147.44 - Object found = get(key, where);
147.45 - if (where[0] != -1) {
147.46 - // key exists
147.47 - values[where[0]] = val;
147.48 - } else {
147.49 - if (where[1] != -1) {
147.50 - // null found
147.51 - keys[where[1]] = key;
147.52 - values[where[1]] = val;
147.53 - } else {
147.54 - if (keys == null) {
147.55 - keys = new Object[11];
147.56 - values = new Object[11];
147.57 - keys[0] = key;
147.58 - values[0] = val;
147.59 - } else {
147.60 - Object[] newKeys = new Object[keys.length * 2];
147.61 - Object[] newValues = new Object[values.length * 2];
147.62 - for (int i = 0; i < keys.length; i++) {
147.63 - newKeys[i] = keys[i];
147.64 - newValues[i] = values[i];
147.65 - }
147.66 - newKeys[keys.length] = key;
147.67 - newValues[keys.length] = val;
147.68 - keys = newKeys;
147.69 - values = newValues;
147.70 - }
147.71 - }
147.72 - }
147.73 - }
147.74 -
147.75 - Object get(Object key) {
147.76 - return get(key, null);
147.77 - }
147.78 - private synchronized Object get(Object key, int[] foundAndNull) {
147.79 - if (keys == null) {
147.80 - return null;
147.81 - }
147.82 - for (int i = 0; i < keys.length; i++) {
147.83 - if (keys[i] == null) {
147.84 - if (foundAndNull != null) {
147.85 - foundAndNull[1] = i;
147.86 - }
147.87 - } else if (keys[i].equals(key)) {
147.88 - if (foundAndNull != null) {
147.89 - foundAndNull[0] = i;
147.90 - }
147.91 - return values[i];
147.92 - }
147.93 - }
147.94 - return null;
147.95 - }
147.96 -
147.97 -}
148.1 --- a/rt/javap/src/main/java/org/apidesign/javap/InnerClassData.java Tue Mar 05 07:57:16 2013 +0100
148.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
148.3 @@ -1,75 +0,0 @@
148.4 -/*
148.5 - * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
148.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
148.7 - *
148.8 - * This code is free software; you can redistribute it and/or modify it
148.9 - * under the terms of the GNU General Public License version 2 only, as
148.10 - * published by the Free Software Foundation. Oracle designates this
148.11 - * particular file as subject to the "Classpath" exception as provided
148.12 - * by Oracle in the LICENSE file that accompanied this code.
148.13 - *
148.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
148.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
148.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
148.17 - * version 2 for more details (a copy is included in the LICENSE file that
148.18 - * accompanied this code).
148.19 - *
148.20 - * You should have received a copy of the GNU General Public License version
148.21 - * 2 along with this work; if not, write to the Free Software Foundation,
148.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
148.23 - *
148.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
148.25 - * or visit www.oracle.com if you need additional information or have any
148.26 - * questions.
148.27 - */
148.28 -
148.29 -
148.30 -package org.apidesign.javap;
148.31 -
148.32 -import java.io.*;
148.33 -import java.util.*;
148.34 -
148.35 -/**
148.36 - * Strores InnerClass data informastion.
148.37 - *
148.38 - * @author Sucheta Dambalkar (Adopted code from jdis)
148.39 - */
148.40 -class InnerClassData implements RuntimeConstants {
148.41 - ClassData cls;
148.42 -
148.43 -
148.44 - int inner_class_info_index
148.45 - ,outer_class_info_index
148.46 - ,inner_name_index
148.47 - ,access
148.48 - ;
148.49 -
148.50 - public InnerClassData(ClassData cls) {
148.51 - this.cls=cls;
148.52 -
148.53 - }
148.54 -
148.55 - /**
148.56 - * Read Innerclass attribute data.
148.57 - */
148.58 - public void read(DataInputStream in) throws IOException {
148.59 - inner_class_info_index = in.readUnsignedShort();
148.60 - outer_class_info_index = in.readUnsignedShort();
148.61 - inner_name_index = in.readUnsignedShort();
148.62 - access = in.readUnsignedShort();
148.63 - } // end read
148.64 -
148.65 - /**
148.66 - * Returns the access of this class or interface.
148.67 - */
148.68 - public String[] getAccess(){
148.69 - Vector v = new Vector();
148.70 - if ((access & ACC_PUBLIC) !=0) v.addElement("public");
148.71 - if ((access & ACC_FINAL) !=0) v.addElement("final");
148.72 - if ((access & ACC_ABSTRACT) !=0) v.addElement("abstract");
148.73 - String[] accflags = new String[v.size()];
148.74 - v.copyInto(accflags);
148.75 - return accflags;
148.76 - }
148.77 -
148.78 -} // end InnerClassData
149.1 --- a/rt/javap/src/main/java/org/apidesign/javap/LineNumData.java Tue Mar 05 07:57:16 2013 +0100
149.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
149.3 @@ -1,50 +0,0 @@
149.4 -/*
149.5 - * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
149.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
149.7 - *
149.8 - * This code is free software; you can redistribute it and/or modify it
149.9 - * under the terms of the GNU General Public License version 2 only, as
149.10 - * published by the Free Software Foundation. Oracle designates this
149.11 - * particular file as subject to the "Classpath" exception as provided
149.12 - * by Oracle in the LICENSE file that accompanied this code.
149.13 - *
149.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
149.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
149.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
149.17 - * version 2 for more details (a copy is included in the LICENSE file that
149.18 - * accompanied this code).
149.19 - *
149.20 - * You should have received a copy of the GNU General Public License version
149.21 - * 2 along with this work; if not, write to the Free Software Foundation,
149.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
149.23 - *
149.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
149.25 - * or visit www.oracle.com if you need additional information or have any
149.26 - * questions.
149.27 - */
149.28 -
149.29 -
149.30 -package org.apidesign.javap;
149.31 -
149.32 -import java.util.*;
149.33 -import java.io.*;
149.34 -
149.35 -/**
149.36 - * Strores LineNumberTable data information.
149.37 - *
149.38 - * @author Sucheta Dambalkar (Adopted code from jdis)
149.39 - */
149.40 -class LineNumData {
149.41 - short start_pc, line_number;
149.42 -
149.43 - public LineNumData() {}
149.44 -
149.45 - /**
149.46 - * Read LineNumberTable attribute.
149.47 - */
149.48 - public LineNumData(DataInputStream in) throws IOException {
149.49 - start_pc = in.readShort();
149.50 - line_number=in.readShort();
149.51 -
149.52 - }
149.53 -}
150.1 --- a/rt/javap/src/main/java/org/apidesign/javap/LocVarData.java Tue Mar 05 07:57:16 2013 +0100
150.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
150.3 @@ -1,54 +0,0 @@
150.4 -/*
150.5 - * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
150.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
150.7 - *
150.8 - * This code is free software; you can redistribute it and/or modify it
150.9 - * under the terms of the GNU General Public License version 2 only, as
150.10 - * published by the Free Software Foundation. Oracle designates this
150.11 - * particular file as subject to the "Classpath" exception as provided
150.12 - * by Oracle in the LICENSE file that accompanied this code.
150.13 - *
150.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
150.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
150.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
150.17 - * version 2 for more details (a copy is included in the LICENSE file that
150.18 - * accompanied this code).
150.19 - *
150.20 - * You should have received a copy of the GNU General Public License version
150.21 - * 2 along with this work; if not, write to the Free Software Foundation,
150.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
150.23 - *
150.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
150.25 - * or visit www.oracle.com if you need additional information or have any
150.26 - * questions.
150.27 - */
150.28 -
150.29 -
150.30 -package org.apidesign.javap;
150.31 -
150.32 -import java.util.*;
150.33 -import java.io.*;
150.34 -
150.35 -/**
150.36 - * Strores LocalVariableTable data information.
150.37 - *
150.38 - * @author Sucheta Dambalkar (Adopted code from jdis)
150.39 - */
150.40 -class LocVarData {
150.41 - short start_pc, length, name_cpx, sig_cpx, slot;
150.42 -
150.43 - public LocVarData() {
150.44 - }
150.45 -
150.46 - /**
150.47 - * Read LocalVariableTable attribute.
150.48 - */
150.49 - public LocVarData(DataInputStream in) throws IOException {
150.50 - start_pc = in.readShort();
150.51 - length=in.readShort();
150.52 - name_cpx=in.readShort();
150.53 - sig_cpx=in.readShort();
150.54 - slot=in.readShort();
150.55 -
150.56 - }
150.57 -}
151.1 --- a/rt/javap/src/main/java/org/apidesign/javap/MethodData.java Tue Mar 05 07:57:16 2013 +0100
151.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
151.3 @@ -1,394 +0,0 @@
151.4 -/*
151.5 - * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved.
151.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
151.7 - *
151.8 - * This code is free software; you can redistribute it and/or modify it
151.9 - * under the terms of the GNU General Public License version 2 only, as
151.10 - * published by the Free Software Foundation. Oracle designates this
151.11 - * particular file as subject to the "Classpath" exception as provided
151.12 - * by Oracle in the LICENSE file that accompanied this code.
151.13 - *
151.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
151.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
151.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
151.17 - * version 2 for more details (a copy is included in the LICENSE file that
151.18 - * accompanied this code).
151.19 - *
151.20 - * You should have received a copy of the GNU General Public License version
151.21 - * 2 along with this work; if not, write to the Free Software Foundation,
151.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
151.23 - *
151.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
151.25 - * or visit www.oracle.com if you need additional information or have any
151.26 - * questions.
151.27 - */
151.28 -
151.29 -package org.apidesign.javap;
151.30 -
151.31 -
151.32 -import java.io.DataInputStream;
151.33 -import java.io.IOException;
151.34 -import static org.apidesign.javap.RuntimeConstants.*;
151.35 -
151.36 -/**
151.37 - * Strores method data informastion.
151.38 - *
151.39 - * @author Sucheta Dambalkar (Adopted code from jdis)
151.40 - */
151.41 -public class MethodData {
151.42 -
151.43 - ClassData cls;
151.44 - int access;
151.45 - int name_index;
151.46 - int descriptor_index;
151.47 - int attributes_count;
151.48 - byte[] code;
151.49 - Vector exception_table = new Vector(0);
151.50 - Vector lin_num_tb = new Vector(0);
151.51 - Vector loc_var_tb = new Vector(0);
151.52 - StackMapTableData[] stackMapTable;
151.53 - StackMapData[] stackMap;
151.54 - int[] exc_index_table=null;
151.55 - Vector attrs=new Vector(0);
151.56 - Vector code_attrs=new Vector(0);
151.57 - int max_stack, max_locals;
151.58 - boolean isSynthetic=false;
151.59 - boolean isDeprecated=false;
151.60 -
151.61 - public MethodData(ClassData cls){
151.62 - this.cls=cls;
151.63 - }
151.64 -
151.65 - /**
151.66 - * Read method info.
151.67 - */
151.68 - public void read(DataInputStream in) throws IOException {
151.69 - access = in.readUnsignedShort();
151.70 - name_index=in.readUnsignedShort();
151.71 - descriptor_index =in.readUnsignedShort();
151.72 - int attributes_count = in.readUnsignedShort();
151.73 - for (int i = 0; i < attributes_count; i++) {
151.74 - int attr_name_index=in.readUnsignedShort();
151.75 -
151.76 - readAttr: {
151.77 - if (cls.getTag(attr_name_index)==CONSTANT_UTF8) {
151.78 - String attr_name=cls.getString(attr_name_index);
151.79 - if ( attr_name.equals("Code")){
151.80 - readCode (in);
151.81 - AttrData attr=new AttrData(cls);
151.82 - attr.read(attr_name_index);
151.83 - attrs.addElement(attr);
151.84 - break readAttr;
151.85 - } else if ( attr_name.equals("Exceptions")){
151.86 - readExceptions(in);
151.87 - AttrData attr=new AttrData(cls);
151.88 - attr.read(attr_name_index);
151.89 - attrs.addElement(attr);
151.90 - break readAttr;
151.91 - } else if (attr_name.equals("Synthetic")){
151.92 - if (in.readInt()!=0)
151.93 - throw new ClassFormatError("invalid Synthetic attr length");
151.94 - isSynthetic=true;
151.95 - AttrData attr=new AttrData(cls);
151.96 - attr.read(attr_name_index);
151.97 - attrs.addElement(attr);
151.98 - break readAttr;
151.99 - } else if (attr_name.equals("Deprecated")){
151.100 - if (in.readInt()!=0)
151.101 - throw new ClassFormatError("invalid Synthetic attr length");
151.102 - isDeprecated = true;
151.103 - AttrData attr=new AttrData(cls);
151.104 - attr.read(attr_name_index);
151.105 - attrs.addElement(attr);
151.106 - break readAttr;
151.107 - }
151.108 - }
151.109 - AttrData attr=new AttrData(cls);
151.110 - attr.read(attr_name_index, in);
151.111 - attrs.addElement(attr);
151.112 - }
151.113 - }
151.114 - }
151.115 -
151.116 - /**
151.117 - * Read code attribute info.
151.118 - */
151.119 - public void readCode(DataInputStream in) throws IOException {
151.120 -
151.121 - int attr_length = in.readInt();
151.122 - max_stack=in.readUnsignedShort();
151.123 - max_locals=in.readUnsignedShort();
151.124 - int codelen=in.readInt();
151.125 -
151.126 - code=new byte[codelen];
151.127 - int totalread = 0;
151.128 - while(totalread < codelen){
151.129 - totalread += in.read(code, totalread, codelen-totalread);
151.130 - }
151.131 - // in.read(code, 0, codelen);
151.132 - int clen = 0;
151.133 - readExceptionTable(in);
151.134 - int code_attributes_count = in.readUnsignedShort();
151.135 -
151.136 - for (int k = 0 ; k < code_attributes_count ; k++) {
151.137 - int table_name_index=in.readUnsignedShort();
151.138 - int table_name_tag=cls.getTag(table_name_index);
151.139 - AttrData attr=new AttrData(cls);
151.140 - if (table_name_tag==CONSTANT_UTF8) {
151.141 - String table_name_tstr=cls.getString(table_name_index);
151.142 - if (table_name_tstr.equals("LineNumberTable")) {
151.143 - readLineNumTable(in);
151.144 - attr.read(table_name_index);
151.145 - } else if (table_name_tstr.equals("LocalVariableTable")) {
151.146 - readLocVarTable(in);
151.147 - attr.read(table_name_index);
151.148 - } else if (table_name_tstr.equals("StackMapTable")) {
151.149 - readStackMapTable(in);
151.150 - attr.read(table_name_index);
151.151 - } else if (table_name_tstr.equals("StackMap")) {
151.152 - readStackMap(in);
151.153 - attr.read(table_name_index);
151.154 - } else {
151.155 - attr.read(table_name_index, in);
151.156 - }
151.157 - code_attrs.addElement(attr);
151.158 - continue;
151.159 - }
151.160 -
151.161 - attr.read(table_name_index, in);
151.162 - code_attrs.addElement(attr);
151.163 - }
151.164 - }
151.165 -
151.166 - /**
151.167 - * Read exception table info.
151.168 - */
151.169 - void readExceptionTable (DataInputStream in) throws IOException {
151.170 - int exception_table_len=in.readUnsignedShort();
151.171 - exception_table=new Vector(exception_table_len);
151.172 - for (int l = 0; l < exception_table_len; l++) {
151.173 - exception_table.addElement(new TrapData(in, l));
151.174 - }
151.175 - }
151.176 -
151.177 - /**
151.178 - * Read LineNumberTable attribute info.
151.179 - */
151.180 - void readLineNumTable (DataInputStream in) throws IOException {
151.181 - int attr_len = in.readInt(); // attr_length
151.182 - int lin_num_tb_len = in.readUnsignedShort();
151.183 - lin_num_tb=new Vector(lin_num_tb_len);
151.184 - for (int l = 0; l < lin_num_tb_len; l++) {
151.185 - lin_num_tb.addElement(new LineNumData(in));
151.186 - }
151.187 - }
151.188 -
151.189 - /**
151.190 - * Read LocalVariableTable attribute info.
151.191 - */
151.192 - void readLocVarTable (DataInputStream in) throws IOException {
151.193 - int attr_len=in.readInt(); // attr_length
151.194 - int loc_var_tb_len = in.readUnsignedShort();
151.195 - loc_var_tb = new Vector(loc_var_tb_len);
151.196 - for (int l = 0; l < loc_var_tb_len; l++) {
151.197 - loc_var_tb.addElement(new LocVarData(in));
151.198 - }
151.199 - }
151.200 -
151.201 - /**
151.202 - * Read Exception attribute info.
151.203 - */
151.204 - public void readExceptions(DataInputStream in) throws IOException {
151.205 - int attr_len=in.readInt(); // attr_length in prog
151.206 - int num_exceptions = in.readUnsignedShort();
151.207 - exc_index_table=new int[num_exceptions];
151.208 - for (int l = 0; l < num_exceptions; l++) {
151.209 - int exc=in.readShort();
151.210 - exc_index_table[l]=exc;
151.211 - }
151.212 - }
151.213 -
151.214 - /**
151.215 - * Read StackMapTable attribute info.
151.216 - */
151.217 - void readStackMapTable(DataInputStream in) throws IOException {
151.218 - int attr_len = in.readInt(); //attr_length
151.219 - int stack_map_tb_len = in.readUnsignedShort();
151.220 - stackMapTable = new StackMapTableData[stack_map_tb_len];
151.221 - for (int i=0; i<stack_map_tb_len; i++) {
151.222 - stackMapTable[i] = StackMapTableData.getInstance(in, this);
151.223 - }
151.224 - }
151.225 -
151.226 - /**
151.227 - * Read StackMap attribute info.
151.228 - */
151.229 - void readStackMap(DataInputStream in) throws IOException {
151.230 - int attr_len = in.readInt(); //attr_length
151.231 - int stack_map_len = in.readUnsignedShort();
151.232 - stackMap = new StackMapData[stack_map_len];
151.233 - for (int i = 0; i<stack_map_len; i++) {
151.234 - stackMap[i] = new StackMapData(in, this);
151.235 - }
151.236 - }
151.237 -
151.238 - /**
151.239 - * Return access of the method.
151.240 - */
151.241 - public int getAccess(){
151.242 - return access;
151.243 - }
151.244 -
151.245 - /**
151.246 - * Return name of the method.
151.247 - */
151.248 - public String getName(){
151.249 - return cls.getStringValue(name_index);
151.250 - }
151.251 -
151.252 - /**
151.253 - * Return internal siganature of the method.
151.254 - */
151.255 - public String getInternalSig(){
151.256 - return cls.getStringValue(descriptor_index);
151.257 - }
151.258 -
151.259 - /**
151.260 - * Return code attribute data of a method.
151.261 - */
151.262 - public byte[] getCode(){
151.263 - return code;
151.264 - }
151.265 -
151.266 - /**
151.267 - * Return LineNumberTable size.
151.268 - */
151.269 - public int getnumlines(){
151.270 - return lin_num_tb.size();
151.271 - }
151.272 -
151.273 - /**
151.274 - * Return LineNumberTable
151.275 - */
151.276 - public Vector getlin_num_tb(){
151.277 - return lin_num_tb;
151.278 - }
151.279 -
151.280 - /**
151.281 - * Return LocalVariableTable size.
151.282 - */
151.283 - public int getloc_var_tbsize(){
151.284 - return loc_var_tb.size();
151.285 - }
151.286 -
151.287 -
151.288 - /**
151.289 - * Return LocalVariableTable.
151.290 - */
151.291 - public Vector getloc_var_tb(){
151.292 - return loc_var_tb;
151.293 - }
151.294 -
151.295 - /**
151.296 - * Return StackMap.
151.297 - */
151.298 - public StackMapData[] getStackMap() {
151.299 - return stackMap;
151.300 - }
151.301 -
151.302 - /**
151.303 - * Return StackMapTable.
151.304 - */
151.305 - public StackMapTableData[] getStackMapTable() {
151.306 - return stackMapTable;
151.307 - }
151.308 -
151.309 - public StackMapIterator createStackMapIterator() {
151.310 - return new StackMapIterator(this);
151.311 - }
151.312 -
151.313 - /**
151.314 - * Return true if method is static
151.315 - */
151.316 - public boolean isStatic(){
151.317 - if ((access & ACC_STATIC) !=0) return true;
151.318 - return false;
151.319 - }
151.320 -
151.321 -
151.322 - /**
151.323 - * Return max depth of operand stack.
151.324 - */
151.325 - public int getMaxStack(){
151.326 - return max_stack;
151.327 - }
151.328 -
151.329 -
151.330 - /**
151.331 - * Return number of local variables.
151.332 - */
151.333 - public int getMaxLocals(){
151.334 - return max_locals;
151.335 - }
151.336 -
151.337 -
151.338 - /**
151.339 - * Return exception index table in Exception attribute.
151.340 - */
151.341 - public int []get_exc_index_table(){
151.342 - return exc_index_table;
151.343 - }
151.344 -
151.345 -
151.346 - /**
151.347 - * Return exception table in code attributre.
151.348 - */
151.349 - public TrapDataIterator getTrapDataIterator(){
151.350 - return new TrapDataIterator(exception_table);
151.351 - }
151.352 -
151.353 -
151.354 - /**
151.355 - * Return method attributes.
151.356 - */
151.357 - public Vector getAttributes(){
151.358 - return attrs;
151.359 - }
151.360 -
151.361 -
151.362 - /**
151.363 - * Return code attributes.
151.364 - */
151.365 - public Vector getCodeAttributes(){
151.366 - return code_attrs;
151.367 - }
151.368 -
151.369 -
151.370 - /**
151.371 - * Return true if method id synthetic.
151.372 - */
151.373 - public boolean isSynthetic(){
151.374 - return isSynthetic;
151.375 - }
151.376 -
151.377 -
151.378 - /**
151.379 - * Return true if method is deprecated.
151.380 - */
151.381 - public boolean isDeprecated(){
151.382 - return isDeprecated;
151.383 - }
151.384 -
151.385 - public byte[] findAnnotationData(boolean classRetention) {
151.386 - String n = classRetention ?
151.387 - "RuntimeInvisibleAnnotations" : // NOI18N
151.388 - "RuntimeVisibleAnnotations"; // NOI18N
151.389 - AttrData[] arr = new AttrData[attrs.size()];
151.390 - attrs.copyInto(arr);
151.391 - return ClassData.findAttr(n, arr);
151.392 - }
151.393 -
151.394 - public boolean isConstructor() {
151.395 - return "<init>".equals(getName());
151.396 - }
151.397 -}
152.1 --- a/rt/javap/src/main/java/org/apidesign/javap/RuntimeConstants.java Tue Mar 05 07:57:16 2013 +0100
152.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
152.3 @@ -1,787 +0,0 @@
152.4 -/*
152.5 - * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved.
152.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
152.7 - *
152.8 - * This code is free software; you can redistribute it and/or modify it
152.9 - * under the terms of the GNU General Public License version 2 only, as
152.10 - * published by the Free Software Foundation. Oracle designates this
152.11 - * particular file as subject to the "Classpath" exception as provided
152.12 - * by Oracle in the LICENSE file that accompanied this code.
152.13 - *
152.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
152.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
152.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
152.17 - * version 2 for more details (a copy is included in the LICENSE file that
152.18 - * accompanied this code).
152.19 - *
152.20 - * You should have received a copy of the GNU General Public License version
152.21 - * 2 along with this work; if not, write to the Free Software Foundation,
152.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
152.23 - *
152.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
152.25 - * or visit www.oracle.com if you need additional information or have any
152.26 - * questions.
152.27 - */
152.28 -
152.29 -
152.30 -package org.apidesign.javap;
152.31 -
152.32 -public interface RuntimeConstants {
152.33 -
152.34 - /* Signature Characters */
152.35 - public static final char SIGC_VOID = 'V';
152.36 - public static final String SIG_VOID = "V";
152.37 - public static final char SIGC_BOOLEAN = 'Z';
152.38 - public static final String SIG_BOOLEAN = "Z";
152.39 - public static final char SIGC_BYTE = 'B';
152.40 - public static final String SIG_BYTE = "B";
152.41 - public static final char SIGC_CHAR = 'C';
152.42 - public static final String SIG_CHAR = "C";
152.43 - public static final char SIGC_SHORT = 'S';
152.44 - public static final String SIG_SHORT = "S";
152.45 - public static final char SIGC_INT = 'I';
152.46 - public static final String SIG_INT = "I";
152.47 - public static final char SIGC_LONG = 'J';
152.48 - public static final String SIG_LONG = "J";
152.49 - public static final char SIGC_FLOAT = 'F';
152.50 - public static final String SIG_FLOAT = "F";
152.51 - public static final char SIGC_DOUBLE = 'D';
152.52 - public static final String SIG_DOUBLE = "D";
152.53 - public static final char SIGC_ARRAY = '[';
152.54 - public static final String SIG_ARRAY = "[";
152.55 - public static final char SIGC_CLASS = 'L';
152.56 - public static final String SIG_CLASS = "L";
152.57 - public static final char SIGC_METHOD = '(';
152.58 - public static final String SIG_METHOD = "(";
152.59 - public static final char SIGC_ENDCLASS = ';';
152.60 - public static final String SIG_ENDCLASS = ";";
152.61 - public static final char SIGC_ENDMETHOD = ')';
152.62 - public static final String SIG_ENDMETHOD = ")";
152.63 - public static final char SIGC_PACKAGE = '/';
152.64 - public static final String SIG_PACKAGE = "/";
152.65 -
152.66 - /* Class File Constants */
152.67 - public static final int JAVA_MAGIC = 0xcafebabe;
152.68 - public static final int JAVA_VERSION = 45;
152.69 - public static final int JAVA_MINOR_VERSION = 3;
152.70 -
152.71 - /* Constant table */
152.72 - public static final int CONSTANT_UTF8 = 1;
152.73 - public static final int CONSTANT_UNICODE = 2;
152.74 - public static final int CONSTANT_INTEGER = 3;
152.75 - public static final int CONSTANT_FLOAT = 4;
152.76 - public static final int CONSTANT_LONG = 5;
152.77 - public static final int CONSTANT_DOUBLE = 6;
152.78 - public static final int CONSTANT_CLASS = 7;
152.79 - public static final int CONSTANT_STRING = 8;
152.80 - public static final int CONSTANT_FIELD = 9;
152.81 - public static final int CONSTANT_METHOD = 10;
152.82 - public static final int CONSTANT_INTERFACEMETHOD = 11;
152.83 - public static final int CONSTANT_NAMEANDTYPE = 12;
152.84 -
152.85 - /* Access Flags */
152.86 - public static final int ACC_PUBLIC = 0x00000001;
152.87 - public static final int ACC_PRIVATE = 0x00000002;
152.88 - public static final int ACC_PROTECTED = 0x00000004;
152.89 - public static final int ACC_STATIC = 0x00000008;
152.90 - public static final int ACC_FINAL = 0x00000010;
152.91 - public static final int ACC_SYNCHRONIZED = 0x00000020;
152.92 - public static final int ACC_SUPER = 0x00000020;
152.93 - public static final int ACC_VOLATILE = 0x00000040;
152.94 - public static final int ACC_TRANSIENT = 0x00000080;
152.95 - public static final int ACC_NATIVE = 0x00000100;
152.96 - public static final int ACC_INTERFACE = 0x00000200;
152.97 - public static final int ACC_ABSTRACT = 0x00000400;
152.98 - public static final int ACC_STRICT = 0x00000800;
152.99 - public static final int ACC_EXPLICIT = 0x00001000;
152.100 - public static final int ACC_SYNTHETIC = 0x00010000; // actually, this is an attribute
152.101 -
152.102 - /* Type codes */
152.103 - public static final int T_CLASS = 0x00000002;
152.104 - public static final int T_BOOLEAN = 0x00000004;
152.105 - public static final int T_CHAR = 0x00000005;
152.106 - public static final int T_FLOAT = 0x00000006;
152.107 - public static final int T_DOUBLE = 0x00000007;
152.108 - public static final int T_BYTE = 0x00000008;
152.109 - public static final int T_SHORT = 0x00000009;
152.110 - public static final int T_INT = 0x0000000a;
152.111 - public static final int T_LONG = 0x0000000b;
152.112 -
152.113 - /* Type codes for StackMap attribute */
152.114 - public static final int ITEM_Bogus =0; // an unknown or uninitialized value
152.115 - public static final int ITEM_Integer =1; // a 32-bit integer
152.116 - public static final int ITEM_Float =2; // not used
152.117 - public static final int ITEM_Double =3; // not used
152.118 - public static final int ITEM_Long =4; // a 64-bit integer
152.119 - public static final int ITEM_Null =5; // the type of null
152.120 - public static final int ITEM_InitObject =6; // "this" in constructor
152.121 - public static final int ITEM_Object =7; // followed by 2-byte index of class name
152.122 - public static final int ITEM_NewObject =8; // followed by 2-byte ref to "new"
152.123 -
152.124 - /* Constants used in StackMapTable attribute */
152.125 - public static final int SAME_FRAME_BOUND = 64;
152.126 - public static final int SAME_LOCALS_1_STACK_ITEM_BOUND = 128;
152.127 - public static final int SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247;
152.128 - public static final int SAME_FRAME_EXTENDED = 251;
152.129 - public static final int FULL_FRAME = 255;
152.130 -
152.131 - /* Opcodes */
152.132 - public static final int opc_dead = -2;
152.133 - public static final int opc_label = -1;
152.134 - public static final int opc_nop = 0;
152.135 - public static final int opc_aconst_null = 1;
152.136 - public static final int opc_iconst_m1 = 2;
152.137 - public static final int opc_iconst_0 = 3;
152.138 - public static final int opc_iconst_1 = 4;
152.139 - public static final int opc_iconst_2 = 5;
152.140 - public static final int opc_iconst_3 = 6;
152.141 - public static final int opc_iconst_4 = 7;
152.142 - public static final int opc_iconst_5 = 8;
152.143 - public static final int opc_lconst_0 = 9;
152.144 - public static final int opc_lconst_1 = 10;
152.145 - public static final int opc_fconst_0 = 11;
152.146 - public static final int opc_fconst_1 = 12;
152.147 - public static final int opc_fconst_2 = 13;
152.148 - public static final int opc_dconst_0 = 14;
152.149 - public static final int opc_dconst_1 = 15;
152.150 - public static final int opc_bipush = 16;
152.151 - public static final int opc_sipush = 17;
152.152 - public static final int opc_ldc = 18;
152.153 - public static final int opc_ldc_w = 19;
152.154 - public static final int opc_ldc2_w = 20;
152.155 - public static final int opc_iload = 21;
152.156 - public static final int opc_lload = 22;
152.157 - public static final int opc_fload = 23;
152.158 - public static final int opc_dload = 24;
152.159 - public static final int opc_aload = 25;
152.160 - public static final int opc_iload_0 = 26;
152.161 - public static final int opc_iload_1 = 27;
152.162 - public static final int opc_iload_2 = 28;
152.163 - public static final int opc_iload_3 = 29;
152.164 - public static final int opc_lload_0 = 30;
152.165 - public static final int opc_lload_1 = 31;
152.166 - public static final int opc_lload_2 = 32;
152.167 - public static final int opc_lload_3 = 33;
152.168 - public static final int opc_fload_0 = 34;
152.169 - public static final int opc_fload_1 = 35;
152.170 - public static final int opc_fload_2 = 36;
152.171 - public static final int opc_fload_3 = 37;
152.172 - public static final int opc_dload_0 = 38;
152.173 - public static final int opc_dload_1 = 39;
152.174 - public static final int opc_dload_2 = 40;
152.175 - public static final int opc_dload_3 = 41;
152.176 - public static final int opc_aload_0 = 42;
152.177 - public static final int opc_aload_1 = 43;
152.178 - public static final int opc_aload_2 = 44;
152.179 - public static final int opc_aload_3 = 45;
152.180 - public static final int opc_iaload = 46;
152.181 - public static final int opc_laload = 47;
152.182 - public static final int opc_faload = 48;
152.183 - public static final int opc_daload = 49;
152.184 - public static final int opc_aaload = 50;
152.185 - public static final int opc_baload = 51;
152.186 - public static final int opc_caload = 52;
152.187 - public static final int opc_saload = 53;
152.188 - public static final int opc_istore = 54;
152.189 - public static final int opc_lstore = 55;
152.190 - public static final int opc_fstore = 56;
152.191 - public static final int opc_dstore = 57;
152.192 - public static final int opc_astore = 58;
152.193 - public static final int opc_istore_0 = 59;
152.194 - public static final int opc_istore_1 = 60;
152.195 - public static final int opc_istore_2 = 61;
152.196 - public static final int opc_istore_3 = 62;
152.197 - public static final int opc_lstore_0 = 63;
152.198 - public static final int opc_lstore_1 = 64;
152.199 - public static final int opc_lstore_2 = 65;
152.200 - public static final int opc_lstore_3 = 66;
152.201 - public static final int opc_fstore_0 = 67;
152.202 - public static final int opc_fstore_1 = 68;
152.203 - public static final int opc_fstore_2 = 69;
152.204 - public static final int opc_fstore_3 = 70;
152.205 - public static final int opc_dstore_0 = 71;
152.206 - public static final int opc_dstore_1 = 72;
152.207 - public static final int opc_dstore_2 = 73;
152.208 - public static final int opc_dstore_3 = 74;
152.209 - public static final int opc_astore_0 = 75;
152.210 - public static final int opc_astore_1 = 76;
152.211 - public static final int opc_astore_2 = 77;
152.212 - public static final int opc_astore_3 = 78;
152.213 - public static final int opc_iastore = 79;
152.214 - public static final int opc_lastore = 80;
152.215 - public static final int opc_fastore = 81;
152.216 - public static final int opc_dastore = 82;
152.217 - public static final int opc_aastore = 83;
152.218 - public static final int opc_bastore = 84;
152.219 - public static final int opc_castore = 85;
152.220 - public static final int opc_sastore = 86;
152.221 - public static final int opc_pop = 87;
152.222 - public static final int opc_pop2 = 88;
152.223 - public static final int opc_dup = 89;
152.224 - public static final int opc_dup_x1 = 90;
152.225 - public static final int opc_dup_x2 = 91;
152.226 - public static final int opc_dup2 = 92;
152.227 - public static final int opc_dup2_x1 = 93;
152.228 - public static final int opc_dup2_x2 = 94;
152.229 - public static final int opc_swap = 95;
152.230 - public static final int opc_iadd = 96;
152.231 - public static final int opc_ladd = 97;
152.232 - public static final int opc_fadd = 98;
152.233 - public static final int opc_dadd = 99;
152.234 - public static final int opc_isub = 100;
152.235 - public static final int opc_lsub = 101;
152.236 - public static final int opc_fsub = 102;
152.237 - public static final int opc_dsub = 103;
152.238 - public static final int opc_imul = 104;
152.239 - public static final int opc_lmul = 105;
152.240 - public static final int opc_fmul = 106;
152.241 - public static final int opc_dmul = 107;
152.242 - public static final int opc_idiv = 108;
152.243 - public static final int opc_ldiv = 109;
152.244 - public static final int opc_fdiv = 110;
152.245 - public static final int opc_ddiv = 111;
152.246 - public static final int opc_irem = 112;
152.247 - public static final int opc_lrem = 113;
152.248 - public static final int opc_frem = 114;
152.249 - public static final int opc_drem = 115;
152.250 - public static final int opc_ineg = 116;
152.251 - public static final int opc_lneg = 117;
152.252 - public static final int opc_fneg = 118;
152.253 - public static final int opc_dneg = 119;
152.254 - public static final int opc_ishl = 120;
152.255 - public static final int opc_lshl = 121;
152.256 - public static final int opc_ishr = 122;
152.257 - public static final int opc_lshr = 123;
152.258 - public static final int opc_iushr = 124;
152.259 - public static final int opc_lushr = 125;
152.260 - public static final int opc_iand = 126;
152.261 - public static final int opc_land = 127;
152.262 - public static final int opc_ior = 128;
152.263 - public static final int opc_lor = 129;
152.264 - public static final int opc_ixor = 130;
152.265 - public static final int opc_lxor = 131;
152.266 - public static final int opc_iinc = 132;
152.267 - public static final int opc_i2l = 133;
152.268 - public static final int opc_i2f = 134;
152.269 - public static final int opc_i2d = 135;
152.270 - public static final int opc_l2i = 136;
152.271 - public static final int opc_l2f = 137;
152.272 - public static final int opc_l2d = 138;
152.273 - public static final int opc_f2i = 139;
152.274 - public static final int opc_f2l = 140;
152.275 - public static final int opc_f2d = 141;
152.276 - public static final int opc_d2i = 142;
152.277 - public static final int opc_d2l = 143;
152.278 - public static final int opc_d2f = 144;
152.279 - public static final int opc_i2b = 145;
152.280 - public static final int opc_int2byte = 145;
152.281 - public static final int opc_i2c = 146;
152.282 - public static final int opc_int2char = 146;
152.283 - public static final int opc_i2s = 147;
152.284 - public static final int opc_int2short = 147;
152.285 - public static final int opc_lcmp = 148;
152.286 - public static final int opc_fcmpl = 149;
152.287 - public static final int opc_fcmpg = 150;
152.288 - public static final int opc_dcmpl = 151;
152.289 - public static final int opc_dcmpg = 152;
152.290 - public static final int opc_ifeq = 153;
152.291 - public static final int opc_ifne = 154;
152.292 - public static final int opc_iflt = 155;
152.293 - public static final int opc_ifge = 156;
152.294 - public static final int opc_ifgt = 157;
152.295 - public static final int opc_ifle = 158;
152.296 - public static final int opc_if_icmpeq = 159;
152.297 - public static final int opc_if_icmpne = 160;
152.298 - public static final int opc_if_icmplt = 161;
152.299 - public static final int opc_if_icmpge = 162;
152.300 - public static final int opc_if_icmpgt = 163;
152.301 - public static final int opc_if_icmple = 164;
152.302 - public static final int opc_if_acmpeq = 165;
152.303 - public static final int opc_if_acmpne = 166;
152.304 - public static final int opc_goto = 167;
152.305 - public static final int opc_jsr = 168;
152.306 - public static final int opc_ret = 169;
152.307 - public static final int opc_tableswitch = 170;
152.308 - public static final int opc_lookupswitch = 171;
152.309 - public static final int opc_ireturn = 172;
152.310 - public static final int opc_lreturn = 173;
152.311 - public static final int opc_freturn = 174;
152.312 - public static final int opc_dreturn = 175;
152.313 - public static final int opc_areturn = 176;
152.314 - public static final int opc_return = 177;
152.315 - public static final int opc_getstatic = 178;
152.316 - public static final int opc_putstatic = 179;
152.317 - public static final int opc_getfield = 180;
152.318 - public static final int opc_putfield = 181;
152.319 - public static final int opc_invokevirtual = 182;
152.320 - public static final int opc_invokenonvirtual = 183;
152.321 - public static final int opc_invokespecial = 183;
152.322 - public static final int opc_invokestatic = 184;
152.323 - public static final int opc_invokeinterface = 185;
152.324 -// public static final int opc_xxxunusedxxx = 186;
152.325 - public static final int opc_new = 187;
152.326 - public static final int opc_newarray = 188;
152.327 - public static final int opc_anewarray = 189;
152.328 - public static final int opc_arraylength = 190;
152.329 - public static final int opc_athrow = 191;
152.330 - public static final int opc_checkcast = 192;
152.331 - public static final int opc_instanceof = 193;
152.332 - public static final int opc_monitorenter = 194;
152.333 - public static final int opc_monitorexit = 195;
152.334 - public static final int opc_wide = 196;
152.335 - public static final int opc_multianewarray = 197;
152.336 - public static final int opc_ifnull = 198;
152.337 - public static final int opc_ifnonnull = 199;
152.338 - public static final int opc_goto_w = 200;
152.339 - public static final int opc_jsr_w = 201;
152.340 - /* Pseudo-instructions */
152.341 - public static final int opc_bytecode = 203;
152.342 - public static final int opc_try = 204;
152.343 - public static final int opc_endtry = 205;
152.344 - public static final int opc_catch = 206;
152.345 - public static final int opc_var = 207;
152.346 - public static final int opc_endvar = 208;
152.347 - public static final int opc_localsmap = 209;
152.348 - public static final int opc_stackmap = 210;
152.349 - /* PicoJava prefixes */
152.350 - public static final int opc_nonpriv = 254;
152.351 - public static final int opc_priv = 255;
152.352 -
152.353 - /* Wide instructions */
152.354 - public static final int opc_iload_w = (opc_wide<<8)|opc_iload;
152.355 - public static final int opc_lload_w = (opc_wide<<8)|opc_lload;
152.356 - public static final int opc_fload_w = (opc_wide<<8)|opc_fload;
152.357 - public static final int opc_dload_w = (opc_wide<<8)|opc_dload;
152.358 - public static final int opc_aload_w = (opc_wide<<8)|opc_aload;
152.359 - public static final int opc_istore_w = (opc_wide<<8)|opc_istore;
152.360 - public static final int opc_lstore_w = (opc_wide<<8)|opc_lstore;
152.361 - public static final int opc_fstore_w = (opc_wide<<8)|opc_fstore;
152.362 - public static final int opc_dstore_w = (opc_wide<<8)|opc_dstore;
152.363 - public static final int opc_astore_w = (opc_wide<<8)|opc_astore;
152.364 - public static final int opc_ret_w = (opc_wide<<8)|opc_ret;
152.365 - public static final int opc_iinc_w = (opc_wide<<8)|opc_iinc;
152.366 -
152.367 - /* Opcode Names */
152.368 - public static final String opcNamesTab[] = {
152.369 - "nop",
152.370 - "aconst_null",
152.371 - "iconst_m1",
152.372 - "iconst_0",
152.373 - "iconst_1",
152.374 - "iconst_2",
152.375 - "iconst_3",
152.376 - "iconst_4",
152.377 - "iconst_5",
152.378 - "lconst_0",
152.379 - "lconst_1",
152.380 - "fconst_0",
152.381 - "fconst_1",
152.382 - "fconst_2",
152.383 - "dconst_0",
152.384 - "dconst_1",
152.385 - "bipush",
152.386 - "sipush",
152.387 - "ldc",
152.388 - "ldc_w",
152.389 - "ldc2_w",
152.390 - "iload",
152.391 - "lload",
152.392 - "fload",
152.393 - "dload",
152.394 - "aload",
152.395 - "iload_0",
152.396 - "iload_1",
152.397 - "iload_2",
152.398 - "iload_3",
152.399 - "lload_0",
152.400 - "lload_1",
152.401 - "lload_2",
152.402 - "lload_3",
152.403 - "fload_0",
152.404 - "fload_1",
152.405 - "fload_2",
152.406 - "fload_3",
152.407 - "dload_0",
152.408 - "dload_1",
152.409 - "dload_2",
152.410 - "dload_3",
152.411 - "aload_0",
152.412 - "aload_1",
152.413 - "aload_2",
152.414 - "aload_3",
152.415 - "iaload",
152.416 - "laload",
152.417 - "faload",
152.418 - "daload",
152.419 - "aaload",
152.420 - "baload",
152.421 - "caload",
152.422 - "saload",
152.423 - "istore",
152.424 - "lstore",
152.425 - "fstore",
152.426 - "dstore",
152.427 - "astore",
152.428 - "istore_0",
152.429 - "istore_1",
152.430 - "istore_2",
152.431 - "istore_3",
152.432 - "lstore_0",
152.433 - "lstore_1",
152.434 - "lstore_2",
152.435 - "lstore_3",
152.436 - "fstore_0",
152.437 - "fstore_1",
152.438 - "fstore_2",
152.439 - "fstore_3",
152.440 - "dstore_0",
152.441 - "dstore_1",
152.442 - "dstore_2",
152.443 - "dstore_3",
152.444 - "astore_0",
152.445 - "astore_1",
152.446 - "astore_2",
152.447 - "astore_3",
152.448 - "iastore",
152.449 - "lastore",
152.450 - "fastore",
152.451 - "dastore",
152.452 - "aastore",
152.453 - "bastore",
152.454 - "castore",
152.455 - "sastore",
152.456 - "pop",
152.457 - "pop2",
152.458 - "dup",
152.459 - "dup_x1",
152.460 - "dup_x2",
152.461 - "dup2",
152.462 - "dup2_x1",
152.463 - "dup2_x2",
152.464 - "swap",
152.465 - "iadd",
152.466 - "ladd",
152.467 - "fadd",
152.468 - "dadd",
152.469 - "isub",
152.470 - "lsub",
152.471 - "fsub",
152.472 - "dsub",
152.473 - "imul",
152.474 - "lmul",
152.475 - "fmul",
152.476 - "dmul",
152.477 - "idiv",
152.478 - "ldiv",
152.479 - "fdiv",
152.480 - "ddiv",
152.481 - "irem",
152.482 - "lrem",
152.483 - "frem",
152.484 - "drem",
152.485 - "ineg",
152.486 - "lneg",
152.487 - "fneg",
152.488 - "dneg",
152.489 - "ishl",
152.490 - "lshl",
152.491 - "ishr",
152.492 - "lshr",
152.493 - "iushr",
152.494 - "lushr",
152.495 - "iand",
152.496 - "land",
152.497 - "ior",
152.498 - "lor",
152.499 - "ixor",
152.500 - "lxor",
152.501 - "iinc",
152.502 - "i2l",
152.503 - "i2f",
152.504 - "i2d",
152.505 - "l2i",
152.506 - "l2f",
152.507 - "l2d",
152.508 - "f2i",
152.509 - "f2l",
152.510 - "f2d",
152.511 - "d2i",
152.512 - "d2l",
152.513 - "d2f",
152.514 - "i2b",
152.515 - "i2c",
152.516 - "i2s",
152.517 - "lcmp",
152.518 - "fcmpl",
152.519 - "fcmpg",
152.520 - "dcmpl",
152.521 - "dcmpg",
152.522 - "ifeq",
152.523 - "ifne",
152.524 - "iflt",
152.525 - "ifge",
152.526 - "ifgt",
152.527 - "ifle",
152.528 - "if_icmpeq",
152.529 - "if_icmpne",
152.530 - "if_icmplt",
152.531 - "if_icmpge",
152.532 - "if_icmpgt",
152.533 - "if_icmple",
152.534 - "if_acmpeq",
152.535 - "if_acmpne",
152.536 - "goto",
152.537 - "jsr",
152.538 - "ret",
152.539 - "tableswitch",
152.540 - "lookupswitch",
152.541 - "ireturn",
152.542 - "lreturn",
152.543 - "freturn",
152.544 - "dreturn",
152.545 - "areturn",
152.546 - "return",
152.547 - "getstatic",
152.548 - "putstatic",
152.549 - "getfield",
152.550 - "putfield",
152.551 - "invokevirtual",
152.552 - "invokespecial", // was "invokenonvirtual",
152.553 - "invokestatic",
152.554 - "invokeinterface",
152.555 - "bytecode 186", //"xxxunusedxxx",
152.556 - "new",
152.557 - "newarray",
152.558 - "anewarray",
152.559 - "arraylength",
152.560 - "athrow",
152.561 - "checkcast",
152.562 - "instanceof",
152.563 - "monitorenter",
152.564 - "monitorexit",
152.565 - null, // "wide",
152.566 - "multianewarray",
152.567 - "ifnull",
152.568 - "ifnonnull",
152.569 - "goto_w",
152.570 - "jsr_w",
152.571 - "bytecode 202", // "breakpoint",
152.572 - "bytecode",
152.573 - "try",
152.574 - "endtry",
152.575 - "catch",
152.576 - "var",
152.577 - "endvar",
152.578 - "locals_map",
152.579 - "stack_map"
152.580 - };
152.581 -
152.582 - /* Opcode Lengths */
152.583 - public static final int opcLengthsTab[] = {
152.584 - 1,
152.585 - 1,
152.586 - 1,
152.587 - 1,
152.588 - 1,
152.589 - 1,
152.590 - 1,
152.591 - 1,
152.592 - 1,
152.593 - 1,
152.594 - 1,
152.595 - 1,
152.596 - 1,
152.597 - 1,
152.598 - 1,
152.599 - 1,
152.600 - 2,
152.601 - 3,
152.602 - 2,
152.603 - 3,
152.604 - 3,
152.605 - 2,
152.606 - 2,
152.607 - 2,
152.608 - 2,
152.609 - 2,
152.610 - 1,
152.611 - 1,
152.612 - 1,
152.613 - 1,
152.614 - 1,
152.615 - 1,
152.616 - 1,
152.617 - 1,
152.618 - 1,
152.619 - 1,
152.620 - 1,
152.621 - 1,
152.622 - 1,
152.623 - 1,
152.624 - 1,
152.625 - 1,
152.626 - 1,
152.627 - 1,
152.628 - 1,
152.629 - 1,
152.630 - 1,
152.631 - 1,
152.632 - 1,
152.633 - 1,
152.634 - 1,
152.635 - 1,
152.636 - 1,
152.637 - 1,
152.638 - 2,
152.639 - 2,
152.640 - 2,
152.641 - 2,
152.642 - 2,
152.643 - 1,
152.644 - 1,
152.645 - 1,
152.646 - 1,
152.647 - 1,
152.648 - 1,
152.649 - 1,
152.650 - 1,
152.651 - 1,
152.652 - 1,
152.653 - 1,
152.654 - 1,
152.655 - 1,
152.656 - 1,
152.657 - 1,
152.658 - 1,
152.659 - 1,
152.660 - 1,
152.661 - 1,
152.662 - 1,
152.663 - 1,
152.664 - 1,
152.665 - 1,
152.666 - 1,
152.667 - 1,
152.668 - 1,
152.669 - 1,
152.670 - 1,
152.671 - 1,
152.672 - 1,
152.673 - 1,
152.674 - 1,
152.675 - 1,
152.676 - 1,
152.677 - 1,
152.678 - 1,
152.679 - 1,
152.680 - 1,
152.681 - 1,
152.682 - 1,
152.683 - 1,
152.684 - 1,
152.685 - 1,
152.686 - 1,
152.687 - 1,
152.688 - 1,
152.689 - 1,
152.690 - 1,
152.691 - 1,
152.692 - 1,
152.693 - 1,
152.694 - 1,
152.695 - 1,
152.696 - 1,
152.697 - 1,
152.698 - 1,
152.699 - 1,
152.700 - 1,
152.701 - 1,
152.702 - 1,
152.703 - 1,
152.704 - 1,
152.705 - 1,
152.706 - 1,
152.707 - 1,
152.708 - 1,
152.709 - 1,
152.710 - 1,
152.711 - 1,
152.712 - 1,
152.713 - 1,
152.714 - 1,
152.715 - 1,
152.716 - 3,
152.717 - 1,
152.718 - 1,
152.719 - 1,
152.720 - 1,
152.721 - 1,
152.722 - 1,
152.723 - 1,
152.724 - 1,
152.725 - 1,
152.726 - 1,
152.727 - 1,
152.728 - 1,
152.729 - 1,
152.730 - 1,
152.731 - 1,
152.732 - 1,
152.733 - 1,
152.734 - 1,
152.735 - 1,
152.736 - 1,
152.737 - 3,
152.738 - 3,
152.739 - 3,
152.740 - 3,
152.741 - 3,
152.742 - 3,
152.743 - 3,
152.744 - 3,
152.745 - 3,
152.746 - 3,
152.747 - 3,
152.748 - 3,
152.749 - 3,
152.750 - 3,
152.751 - 3,
152.752 - 3,
152.753 - 2,
152.754 - 99,
152.755 - 99,
152.756 - 1,
152.757 - 1,
152.758 - 1,
152.759 - 1,
152.760 - 1,
152.761 - 1,
152.762 - 3,
152.763 - 3,
152.764 - 3,
152.765 - 3,
152.766 - 3,
152.767 - 3,
152.768 - 3,
152.769 - 5,
152.770 - 0,
152.771 - 3,
152.772 - 2,
152.773 - 3,
152.774 - 1,
152.775 - 1,
152.776 - 3,
152.777 - 3,
152.778 - 1,
152.779 - 1,
152.780 - 0, // wide
152.781 - 4,
152.782 - 3,
152.783 - 3,
152.784 - 5,
152.785 - 5,
152.786 - 1,
152.787 - 1, 0, 0, 0, 0, 0 // pseudo
152.788 - };
152.789 -
152.790 -}
153.1 --- a/rt/javap/src/main/java/org/apidesign/javap/StackMapData.java Tue Mar 05 07:57:16 2013 +0100
153.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
153.3 @@ -1,71 +0,0 @@
153.4 -/*
153.5 - * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
153.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
153.7 - *
153.8 - * This code is free software; you can redistribute it and/or modify it
153.9 - * under the terms of the GNU General Public License version 2 only, as
153.10 - * published by the Free Software Foundation. Oracle designates this
153.11 - * particular file as subject to the "Classpath" exception as provided
153.12 - * by Oracle in the LICENSE file that accompanied this code.
153.13 - *
153.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
153.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
153.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
153.17 - * version 2 for more details (a copy is included in the LICENSE file that
153.18 - * accompanied this code).
153.19 - *
153.20 - * You should have received a copy of the GNU General Public License version
153.21 - * 2 along with this work; if not, write to the Free Software Foundation,
153.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
153.23 - *
153.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
153.25 - * or visit www.oracle.com if you need additional information or have any
153.26 - * questions.
153.27 - */
153.28 -
153.29 -
153.30 -package org.apidesign.javap;
153.31 -
153.32 -import java.util.*;
153.33 -import java.io.*;
153.34 -
153.35 -import static org.apidesign.javap.RuntimeConstants.*;
153.36 -
153.37 -/* represents one entry of StackMap attribute
153.38 - */
153.39 -class StackMapData {
153.40 - final int offset;
153.41 - final int[] locals;
153.42 - final int[] stack;
153.43 -
153.44 - StackMapData(int offset, int[] locals, int[] stack) {
153.45 - this.offset = offset;
153.46 - this.locals = locals;
153.47 - this.stack = stack;
153.48 - }
153.49 -
153.50 - StackMapData(DataInputStream in, MethodData method) throws IOException {
153.51 - offset = in.readUnsignedShort();
153.52 - int local_size = in.readUnsignedShort();
153.53 - locals = readTypeArray(in, local_size, method);
153.54 - int stack_size = in.readUnsignedShort();
153.55 - stack = readTypeArray(in, stack_size, method);
153.56 - }
153.57 -
153.58 - static final int[] readTypeArray(DataInputStream in, int length, MethodData method) throws IOException {
153.59 - int[] types = new int[length];
153.60 - for (int i=0; i<length; i++) {
153.61 - types[i] = readType(in, method);
153.62 - }
153.63 - return types;
153.64 - }
153.65 -
153.66 - static final int readType(DataInputStream in, MethodData method) throws IOException {
153.67 - int type = in.readUnsignedByte();
153.68 - if (type == ITEM_Object || type == ITEM_NewObject) {
153.69 - type = type | (in.readUnsignedShort()<<8);
153.70 - }
153.71 - return type;
153.72 - }
153.73 -
153.74 -}
154.1 --- a/rt/javap/src/main/java/org/apidesign/javap/StackMapIterator.java Tue Mar 05 07:57:16 2013 +0100
154.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
154.3 @@ -1,179 +0,0 @@
154.4 -/*
154.5 - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
154.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
154.7 - *
154.8 - * This code is free software; you can redistribute it and/or modify it
154.9 - * under the terms of the GNU General Public License version 2 only, as
154.10 - * published by the Free Software Foundation. Oracle designates this
154.11 - * particular file as subject to the "Classpath" exception as provided
154.12 - * by Oracle in the LICENSE file that accompanied this code.
154.13 - *
154.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
154.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
154.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
154.17 - * version 2 for more details (a copy is included in the LICENSE file that
154.18 - * accompanied this code).
154.19 - *
154.20 - * You should have received a copy of the GNU General Public License version
154.21 - * 2 along with this work; if not, write to the Free Software Foundation,
154.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
154.23 - *
154.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
154.25 - * or visit www.oracle.com if you need additional information or have any
154.26 - * questions.
154.27 - */
154.28 -
154.29 -package org.apidesign.javap;
154.30 -
154.31 -import static org.apidesign.javap.RuntimeConstants.ITEM_Integer;
154.32 -import static org.apidesign.javap.RuntimeConstants.ITEM_Float;
154.33 -import static org.apidesign.javap.RuntimeConstants.ITEM_Double;
154.34 -import static org.apidesign.javap.RuntimeConstants.ITEM_Long;
154.35 -import static org.apidesign.javap.RuntimeConstants.ITEM_Object;
154.36 -
154.37 -public final class StackMapIterator {
154.38 - private final StackMapTableData[] stackMapTable;
154.39 - private final TypeArray argTypes;
154.40 - private final TypeArray localTypes;
154.41 - private final TypeArray stackTypes;
154.42 -
154.43 - private int nextFrameIndex;
154.44 - private int lastFrameByteCodeOffset;
154.45 -
154.46 - private int byteCodeOffset;
154.47 -
154.48 - StackMapIterator(final MethodData methodData) {
154.49 - this(methodData.getStackMapTable(),
154.50 - methodData.getInternalSig(),
154.51 - methodData.isStatic());
154.52 - }
154.53 -
154.54 - StackMapIterator(final StackMapTableData[] stackMapTable,
154.55 - final String methodSignature,
154.56 - final boolean isStaticMethod) {
154.57 - this.stackMapTable = (stackMapTable != null)
154.58 - ? stackMapTable
154.59 - : new StackMapTableData[0];
154.60 -
154.61 - argTypes = getArgTypes(methodSignature, isStaticMethod);
154.62 - localTypes = new TypeArray();
154.63 - stackTypes = new TypeArray();
154.64 -
154.65 - localTypes.addAll(argTypes);
154.66 -
154.67 - lastFrameByteCodeOffset = -1;
154.68 - advanceBy(0);
154.69 - }
154.70 -
154.71 - public String getFrameAsString() {
154.72 - return (nextFrameIndex == 0)
154.73 - ? StackMapTableData.toString("INITIAL", 0, null, null)
154.74 - : stackMapTable[nextFrameIndex - 1].toString();
154.75 - }
154.76 -
154.77 - public int getFrameIndex() {
154.78 - return nextFrameIndex;
154.79 - }
154.80 -
154.81 - public TypeArray getFrameStack() {
154.82 - return stackTypes;
154.83 - }
154.84 -
154.85 - public TypeArray getFrameLocals() {
154.86 - return localTypes;
154.87 - }
154.88 -
154.89 - public TypeArray getArguments() {
154.90 - return argTypes;
154.91 - }
154.92 -
154.93 - public void advanceBy(final int numByteCodes) {
154.94 - if (numByteCodes < 0) {
154.95 - throw new IllegalStateException("Forward only iterator");
154.96 - }
154.97 -
154.98 - byteCodeOffset += numByteCodes;
154.99 - while ((nextFrameIndex < stackMapTable.length)
154.100 - && ((byteCodeOffset - lastFrameByteCodeOffset)
154.101 - >= (stackMapTable[nextFrameIndex].offsetDelta
154.102 - + 1))) {
154.103 - final StackMapTableData nextFrame = stackMapTable[nextFrameIndex];
154.104 -
154.105 - lastFrameByteCodeOffset += nextFrame.offsetDelta + 1;
154.106 - nextFrame.applyTo(localTypes, stackTypes);
154.107 -
154.108 - ++nextFrameIndex;
154.109 - }
154.110 - }
154.111 -
154.112 - public void advanceTo(final int nextByteCodeOffset) {
154.113 - advanceBy(nextByteCodeOffset - byteCodeOffset);
154.114 - }
154.115 -
154.116 - private static TypeArray getArgTypes(final String methodSignature,
154.117 - final boolean isStaticMethod) {
154.118 - final TypeArray argTypes = new TypeArray();
154.119 -
154.120 - if (!isStaticMethod) {
154.121 - argTypes.add(ITEM_Object);
154.122 - }
154.123 -
154.124 - if (methodSignature.charAt(0) != '(') {
154.125 - throw new IllegalArgumentException("Invalid method signature");
154.126 - }
154.127 -
154.128 - final int length = methodSignature.length();
154.129 - boolean skipType = false;
154.130 - int argType;
154.131 - for (int i = 1; i < length; ++i) {
154.132 - switch (methodSignature.charAt(i)) {
154.133 - case 'B':
154.134 - case 'C':
154.135 - case 'S':
154.136 - case 'Z':
154.137 - case 'I':
154.138 - argType = ITEM_Integer;
154.139 - break;
154.140 - case 'J':
154.141 - argType = ITEM_Long;
154.142 - break;
154.143 - case 'F':
154.144 - argType = ITEM_Float;
154.145 - break;
154.146 - case 'D':
154.147 - argType = ITEM_Double;
154.148 - break;
154.149 - case 'L': {
154.150 - i = methodSignature.indexOf(';', i + 1);
154.151 - if (i == -1) {
154.152 - throw new IllegalArgumentException(
154.153 - "Invalid method signature");
154.154 - }
154.155 - argType = ITEM_Object;
154.156 - break;
154.157 - }
154.158 - case ')':
154.159 - // not interested in the return value type
154.160 - return argTypes;
154.161 - case '[':
154.162 - if (!skipType) {
154.163 - argTypes.add(ITEM_Object);
154.164 - skipType = true;
154.165 - }
154.166 - continue;
154.167 -
154.168 - default:
154.169 - throw new IllegalArgumentException(
154.170 - "Invalid method signature");
154.171 - }
154.172 -
154.173 - if (!skipType) {
154.174 - argTypes.add(argType);
154.175 - } else {
154.176 - skipType = false;
154.177 - }
154.178 - }
154.179 -
154.180 - return argTypes;
154.181 - }
154.182 -}
155.1 --- a/rt/javap/src/main/java/org/apidesign/javap/StackMapTableData.java Tue Mar 05 07:57:16 2013 +0100
155.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
155.3 @@ -1,223 +0,0 @@
155.4 -/*
155.5 - * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
155.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
155.7 - *
155.8 - * This code is free software; you can redistribute it and/or modify it
155.9 - * under the terms of the GNU General Public License version 2 only, as
155.10 - * published by the Free Software Foundation. Oracle designates this
155.11 - * particular file as subject to the "Classpath" exception as provided
155.12 - * by Oracle in the LICENSE file that accompanied this code.
155.13 - *
155.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
155.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
155.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
155.17 - * version 2 for more details (a copy is included in the LICENSE file that
155.18 - * accompanied this code).
155.19 - *
155.20 - * You should have received a copy of the GNU General Public License version
155.21 - * 2 along with this work; if not, write to the Free Software Foundation,
155.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
155.23 - *
155.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
155.25 - * or visit www.oracle.com if you need additional information or have any
155.26 - * questions.
155.27 - */
155.28 -
155.29 -
155.30 -package org.apidesign.javap;
155.31 -
155.32 -import java.io.*;
155.33 -
155.34 -import static org.apidesign.javap.RuntimeConstants.*;
155.35 -
155.36 -/* represents one entry of StackMapTable attribute
155.37 - */
155.38 -abstract class StackMapTableData {
155.39 - final int frameType;
155.40 - int offsetDelta;
155.41 -
155.42 - StackMapTableData(int frameType) {
155.43 - this.frameType = frameType;
155.44 - }
155.45 -
155.46 - abstract void applyTo(TypeArray localTypes, TypeArray stackTypes);
155.47 -
155.48 - protected static String toString(
155.49 - final String frameType,
155.50 - final int offset,
155.51 - final int[] localTypes,
155.52 - final int[] stackTypes) {
155.53 - final StringBuilder sb = new StringBuilder(frameType);
155.54 -
155.55 - sb.append("(off: +").append(offset);
155.56 - if (localTypes != null) {
155.57 - sb.append(", locals: ");
155.58 - appendTypes(sb, localTypes);
155.59 - }
155.60 - if (stackTypes != null) {
155.61 - sb.append(", stack: ");
155.62 - appendTypes(sb, stackTypes);
155.63 - }
155.64 - sb.append(')');
155.65 -
155.66 - return sb.toString();
155.67 - }
155.68 -
155.69 - private static void appendTypes(final StringBuilder sb, final int[] types) {
155.70 - sb.append('[');
155.71 - if (types.length > 0) {
155.72 - sb.append(TypeArray.typeString(types[0]));
155.73 - for (int i = 1; i < types.length; ++i) {
155.74 - sb.append(", ");
155.75 - sb.append(TypeArray.typeString(types[i]));
155.76 - }
155.77 - }
155.78 - sb.append(']');
155.79 - }
155.80 -
155.81 - static class SameFrame extends StackMapTableData {
155.82 - SameFrame(int frameType, int offsetDelta) {
155.83 - super(frameType);
155.84 - this.offsetDelta = offsetDelta;
155.85 - }
155.86 -
155.87 - @Override
155.88 - void applyTo(TypeArray localTypes, TypeArray stackTypes) {
155.89 - stackTypes.clear();
155.90 - }
155.91 -
155.92 - @Override
155.93 - public String toString() {
155.94 - return toString("SAME" + ((frameType == SAME_FRAME_EXTENDED)
155.95 - ? "_FRAME_EXTENDED" : ""),
155.96 - offsetDelta,
155.97 - null, null);
155.98 - }
155.99 - }
155.100 -
155.101 - static class SameLocals1StackItem extends StackMapTableData {
155.102 - final int[] stack;
155.103 - SameLocals1StackItem(int frameType, int offsetDelta, int[] stack) {
155.104 - super(frameType);
155.105 - this.offsetDelta = offsetDelta;
155.106 - this.stack = stack;
155.107 - }
155.108 -
155.109 - @Override
155.110 - void applyTo(TypeArray localTypes, TypeArray stackTypes) {
155.111 - stackTypes.setAll(stack);
155.112 - }
155.113 -
155.114 - @Override
155.115 - public String toString() {
155.116 - return toString(
155.117 - "SAME_LOCALS_1_STACK_ITEM"
155.118 - + ((frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED)
155.119 - ? "_EXTENDED" : ""),
155.120 - offsetDelta,
155.121 - null, stack);
155.122 - }
155.123 - }
155.124 -
155.125 - static class ChopFrame extends StackMapTableData {
155.126 - ChopFrame(int frameType, int offsetDelta) {
155.127 - super(frameType);
155.128 - this.offsetDelta = offsetDelta;
155.129 - }
155.130 -
155.131 - @Override
155.132 - void applyTo(TypeArray localTypes, TypeArray stackTypes) {
155.133 - localTypes.setSize(localTypes.getSize()
155.134 - - (SAME_FRAME_EXTENDED - frameType));
155.135 - stackTypes.clear();
155.136 - }
155.137 -
155.138 - @Override
155.139 - public String toString() {
155.140 - return toString("CHOP", offsetDelta, null, null);
155.141 - }
155.142 - }
155.143 -
155.144 - static class AppendFrame extends StackMapTableData {
155.145 - final int[] locals;
155.146 - AppendFrame(int frameType, int offsetDelta, int[] locals) {
155.147 - super(frameType);
155.148 - this.offsetDelta = offsetDelta;
155.149 - this.locals = locals;
155.150 - }
155.151 -
155.152 - @Override
155.153 - void applyTo(TypeArray localTypes, TypeArray stackTypes) {
155.154 - localTypes.addAll(locals);
155.155 - stackTypes.clear();
155.156 - }
155.157 -
155.158 - @Override
155.159 - public String toString() {
155.160 - return toString("APPEND", offsetDelta, locals, null);
155.161 - }
155.162 - }
155.163 -
155.164 - static class FullFrame extends StackMapTableData {
155.165 - final int[] locals;
155.166 - final int[] stack;
155.167 - FullFrame(int offsetDelta, int[] locals, int[] stack) {
155.168 - super(FULL_FRAME);
155.169 - this.offsetDelta = offsetDelta;
155.170 - this.locals = locals;
155.171 - this.stack = stack;
155.172 - }
155.173 -
155.174 - @Override
155.175 - void applyTo(TypeArray localTypes, TypeArray stackTypes) {
155.176 - localTypes.setAll(locals);
155.177 - stackTypes.setAll(stack);
155.178 - }
155.179 -
155.180 - @Override
155.181 - public String toString() {
155.182 - return toString("FULL", offsetDelta, locals, stack);
155.183 - }
155.184 - }
155.185 -
155.186 - static StackMapTableData getInstance(DataInputStream in, MethodData method)
155.187 - throws IOException {
155.188 - int frameType = in.readUnsignedByte();
155.189 -
155.190 - if (frameType < SAME_FRAME_BOUND) {
155.191 - // same_frame
155.192 - return new SameFrame(frameType, frameType);
155.193 - } else if (SAME_FRAME_BOUND <= frameType && frameType < SAME_LOCALS_1_STACK_ITEM_BOUND) {
155.194 - // same_locals_1_stack_item_frame
155.195 - // read additional single stack element
155.196 - return new SameLocals1StackItem(frameType,
155.197 - (frameType - SAME_FRAME_BOUND),
155.198 - StackMapData.readTypeArray(in, 1, method));
155.199 - } else if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
155.200 - // same_locals_1_stack_item_extended
155.201 - return new SameLocals1StackItem(frameType,
155.202 - in.readUnsignedShort(),
155.203 - StackMapData.readTypeArray(in, 1, method));
155.204 - } else if (SAME_LOCALS_1_STACK_ITEM_EXTENDED < frameType && frameType < SAME_FRAME_EXTENDED) {
155.205 - // chop_frame or same_frame_extended
155.206 - return new ChopFrame(frameType, in.readUnsignedShort());
155.207 - } else if (frameType == SAME_FRAME_EXTENDED) {
155.208 - // chop_frame or same_frame_extended
155.209 - return new SameFrame(frameType, in.readUnsignedShort());
155.210 - } else if (SAME_FRAME_EXTENDED < frameType && frameType < FULL_FRAME) {
155.211 - // append_frame
155.212 - return new AppendFrame(frameType, in.readUnsignedShort(),
155.213 - StackMapData.readTypeArray(in, frameType - SAME_FRAME_EXTENDED, method));
155.214 - } else if (frameType == FULL_FRAME) {
155.215 - // full_frame
155.216 - int offsetDelta = in.readUnsignedShort();
155.217 - int locals_size = in.readUnsignedShort();
155.218 - int[] locals = StackMapData.readTypeArray(in, locals_size, method);
155.219 - int stack_size = in.readUnsignedShort();
155.220 - int[] stack = StackMapData.readTypeArray(in, stack_size, method);
155.221 - return new FullFrame(offsetDelta, locals, stack);
155.222 - } else {
155.223 - throw new ClassFormatError("unrecognized frame_type in StackMapTable");
155.224 - }
155.225 - }
155.226 -}
156.1 --- a/rt/javap/src/main/java/org/apidesign/javap/TrapData.java Tue Mar 05 07:57:16 2013 +0100
156.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
156.3 @@ -1,62 +0,0 @@
156.4 -/*
156.5 - * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
156.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
156.7 - *
156.8 - * This code is free software; you can redistribute it and/or modify it
156.9 - * under the terms of the GNU General Public License version 2 only, as
156.10 - * published by the Free Software Foundation. Oracle designates this
156.11 - * particular file as subject to the "Classpath" exception as provided
156.12 - * by Oracle in the LICENSE file that accompanied this code.
156.13 - *
156.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
156.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
156.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
156.17 - * version 2 for more details (a copy is included in the LICENSE file that
156.18 - * accompanied this code).
156.19 - *
156.20 - * You should have received a copy of the GNU General Public License version
156.21 - * 2 along with this work; if not, write to the Free Software Foundation,
156.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
156.23 - *
156.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
156.25 - * or visit www.oracle.com if you need additional information or have any
156.26 - * questions.
156.27 - */
156.28 -
156.29 -
156.30 -package org.apidesign.javap;
156.31 -
156.32 -import java.io.*;
156.33 -
156.34 -/**
156.35 - * Stores exception table data in code attribute.
156.36 - *
156.37 - * @author Sucheta Dambalkar (Adopted code from jdis)
156.38 - */
156.39 -public final class TrapData {
156.40 - public final short start_pc;
156.41 - public final short end_pc;
156.42 - public final short handler_pc;
156.43 - public final short catch_cpx;
156.44 - final int num;
156.45 -
156.46 -
156.47 - /**
156.48 - * Read and store exception table data in code attribute.
156.49 - */
156.50 - TrapData(DataInputStream in, int num) throws IOException {
156.51 - this.num=num;
156.52 - start_pc = in.readShort();
156.53 - end_pc=in.readShort();
156.54 - handler_pc=in.readShort();
156.55 - catch_cpx=in.readShort();
156.56 - }
156.57 -
156.58 - /**
156.59 - * returns recommended identifier
156.60 - */
156.61 - public String ident() {
156.62 - return "t"+num;
156.63 - }
156.64 -
156.65 -}
157.1 --- a/rt/javap/src/main/java/org/apidesign/javap/TrapDataIterator.java Tue Mar 05 07:57:16 2013 +0100
157.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
157.3 @@ -1,114 +0,0 @@
157.4 -/**
157.5 - * Back 2 Browser Bytecode Translator
157.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
157.7 - *
157.8 - * This program is free software: you can redistribute it and/or modify
157.9 - * it under the terms of the GNU General Public License as published by
157.10 - * the Free Software Foundation, version 2 of the License.
157.11 - *
157.12 - * This program is distributed in the hope that it will be useful,
157.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
157.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
157.15 - * GNU General Public License for more details.
157.16 - *
157.17 - * You should have received a copy of the GNU General Public License
157.18 - * along with this program. Look for COPYING file in the top folder.
157.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
157.20 - */
157.21 -package org.apidesign.javap;
157.22 -
157.23 -/**
157.24 - *
157.25 - * @author Jaroslav Tulach <jtulach@netbeans.org>
157.26 - */
157.27 -public final class TrapDataIterator {
157.28 - private final Hashtable exStart = new Hashtable();
157.29 - private final Hashtable exStop = new Hashtable();
157.30 - private TrapData[] current = new TrapData[10];
157.31 - private int currentCount;
157.32 -
157.33 - TrapDataIterator(Vector exceptionTable) {
157.34 - for (int i=0 ; i < exceptionTable.size(); i++) {
157.35 - final TrapData td = (TrapData)exceptionTable.elementAt(i);
157.36 - put(exStart, td.start_pc, td);
157.37 - put(exStop, td.end_pc, td);
157.38 - }
157.39 - }
157.40 -
157.41 - private static void put(Hashtable h, short key, TrapData td) {
157.42 - Short s = Short.valueOf((short)key);
157.43 - Vector v = (Vector) h.get(s);
157.44 - if (v == null) {
157.45 - v = new Vector(1);
157.46 - h.put(s, v);
157.47 - }
157.48 - v.add(td);
157.49 - }
157.50 -
157.51 - private boolean processAll(Hashtable h, Short key, boolean add) {
157.52 - boolean change = false;
157.53 - Vector v = (Vector)h.get(key);
157.54 - if (v != null) {
157.55 - int s = v.size();
157.56 - for (int i = 0; i < s; i++) {
157.57 - TrapData td = (TrapData)v.elementAt(i);
157.58 - if (add) {
157.59 - add(td);
157.60 - change = true;
157.61 - } else {
157.62 - remove(td);
157.63 - change = true;
157.64 - }
157.65 - }
157.66 - }
157.67 - return change;
157.68 - }
157.69 -
157.70 - public boolean advanceTo(int i) {
157.71 - Short s = Short.valueOf((short)i);
157.72 - boolean ch1 = processAll(exStart, s, true);
157.73 - boolean ch2 = processAll(exStop, s, false);
157.74 - return ch1 || ch2;
157.75 - }
157.76 -
157.77 - public boolean useTry() {
157.78 - return currentCount > 0;
157.79 - }
157.80 -
157.81 - public TrapData[] current() {
157.82 - TrapData[] copy = new TrapData[currentCount];
157.83 - for (int i = 0; i < currentCount; i++) {
157.84 - copy[i] = current[i];
157.85 - }
157.86 - return copy;
157.87 - }
157.88 -
157.89 - private void add(TrapData e) {
157.90 - if (currentCount == current.length) {
157.91 - TrapData[] data = new TrapData[currentCount * 2];
157.92 - for (int i = 0; i < currentCount; i++) {
157.93 - data[i] = current[i];
157.94 - }
157.95 - current = data;
157.96 - }
157.97 - current[currentCount++] = e;
157.98 - }
157.99 -
157.100 - private void remove(TrapData e) {
157.101 - if (currentCount == 0) {
157.102 - return;
157.103 - }
157.104 - int from = 0;
157.105 - while (from < currentCount) {
157.106 - if (e == current[from++]) {
157.107 - break;
157.108 - }
157.109 - }
157.110 - while (from < currentCount) {
157.111 - current[from - 1] = current[from];
157.112 - current[from] = null;
157.113 - from++;
157.114 - }
157.115 - currentCount--;
157.116 - }
157.117 -}
158.1 --- a/rt/javap/src/main/java/org/apidesign/javap/TypeArray.java Tue Mar 05 07:57:16 2013 +0100
158.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
158.3 @@ -1,186 +0,0 @@
158.4 -/*
158.5 - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
158.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
158.7 - *
158.8 - * This code is free software; you can redistribute it and/or modify it
158.9 - * under the terms of the GNU General Public License version 2 only, as
158.10 - * published by the Free Software Foundation. Oracle designates this
158.11 - * particular file as subject to the "Classpath" exception as provided
158.12 - * by Oracle in the LICENSE file that accompanied this code.
158.13 - *
158.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
158.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
158.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
158.17 - * version 2 for more details (a copy is included in the LICENSE file that
158.18 - * accompanied this code).
158.19 - *
158.20 - * You should have received a copy of the GNU General Public License version
158.21 - * 2 along with this work; if not, write to the Free Software Foundation,
158.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
158.23 - *
158.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
158.25 - * or visit www.oracle.com if you need additional information or have any
158.26 - * questions.
158.27 - */
158.28 -
158.29 -package org.apidesign.javap;
158.30 -
158.31 -import static org.apidesign.javap.RuntimeConstants.ITEM_Bogus;
158.32 -import static org.apidesign.javap.RuntimeConstants.ITEM_Integer;
158.33 -import static org.apidesign.javap.RuntimeConstants.ITEM_Float;
158.34 -import static org.apidesign.javap.RuntimeConstants.ITEM_Double;
158.35 -import static org.apidesign.javap.RuntimeConstants.ITEM_Long;
158.36 -import static org.apidesign.javap.RuntimeConstants.ITEM_Null;
158.37 -import static org.apidesign.javap.RuntimeConstants.ITEM_InitObject;
158.38 -import static org.apidesign.javap.RuntimeConstants.ITEM_Object;
158.39 -import static org.apidesign.javap.RuntimeConstants.ITEM_NewObject;
158.40 -
158.41 -public final class TypeArray {
158.42 - private static final int CAPACITY_INCREMENT = 16;
158.43 -
158.44 - private int[] types;
158.45 - private int size;
158.46 -
158.47 - public TypeArray() {
158.48 - }
158.49 -
158.50 - public TypeArray(final TypeArray initialTypes) {
158.51 - setAll(initialTypes);
158.52 - }
158.53 -
158.54 - public void add(final int newType) {
158.55 - ensureCapacity(size + 1);
158.56 - types[size++] = newType;
158.57 - }
158.58 -
158.59 - public void addAll(final TypeArray newTypes) {
158.60 - addAll(newTypes.types, 0, newTypes.size);
158.61 - }
158.62 -
158.63 - public void addAll(final int[] newTypes) {
158.64 - addAll(newTypes, 0, newTypes.length);
158.65 - }
158.66 -
158.67 - public void addAll(final int[] newTypes,
158.68 - final int offset,
158.69 - final int count) {
158.70 - if (count > 0) {
158.71 - ensureCapacity(size + count);
158.72 - arraycopy(newTypes, offset, types, size, count);
158.73 - size += count;
158.74 - }
158.75 - }
158.76 -
158.77 - public void set(final int index, final int newType) {
158.78 - types[index] = newType;
158.79 - }
158.80 -
158.81 - public void setAll(final TypeArray newTypes) {
158.82 - setAll(newTypes.types, 0, newTypes.size);
158.83 - }
158.84 -
158.85 - public void setAll(final int[] newTypes) {
158.86 - setAll(newTypes, 0, newTypes.length);
158.87 - }
158.88 -
158.89 - public void setAll(final int[] newTypes,
158.90 - final int offset,
158.91 - final int count) {
158.92 - if (count > 0) {
158.93 - ensureCapacity(count);
158.94 - arraycopy(newTypes, offset, types, 0, count);
158.95 - size = count;
158.96 - } else {
158.97 - clear();
158.98 - }
158.99 - }
158.100 -
158.101 - public void setSize(final int newSize) {
158.102 - if (size != newSize) {
158.103 - ensureCapacity(newSize);
158.104 -
158.105 - for (int i = size; i < newSize; ++i) {
158.106 - types[i] = 0;
158.107 - }
158.108 - size = newSize;
158.109 - }
158.110 - }
158.111 -
158.112 - public void clear() {
158.113 - size = 0;
158.114 - }
158.115 -
158.116 - public int getSize() {
158.117 - return size;
158.118 - }
158.119 -
158.120 - public int get(final int index) {
158.121 - return types[index];
158.122 - }
158.123 -
158.124 - public static String typeString(final int type) {
158.125 - switch (type & 0xff) {
158.126 - case ITEM_Bogus:
158.127 - return "_top_";
158.128 - case ITEM_Integer:
158.129 - return "_int_";
158.130 - case ITEM_Float:
158.131 - return "_float_";
158.132 - case ITEM_Double:
158.133 - return "_double_";
158.134 - case ITEM_Long:
158.135 - return "_long_";
158.136 - case ITEM_Null:
158.137 - return "_null_";
158.138 - case ITEM_InitObject: // UninitializedThis
158.139 - return "_init_";
158.140 - case ITEM_Object:
158.141 - return "_object_";
158.142 - case ITEM_NewObject: // Uninitialized
158.143 - return "_new_";
158.144 - default:
158.145 - throw new IllegalArgumentException("Unknown type");
158.146 - }
158.147 - }
158.148 -
158.149 - @Override
158.150 - public String toString() {
158.151 - final StringBuilder sb = new StringBuilder("[");
158.152 - if (size > 0) {
158.153 - sb.append(typeString(types[0]));
158.154 - for (int i = 1; i < size; ++i) {
158.155 - sb.append(", ");
158.156 - sb.append(typeString(types[i]));
158.157 - }
158.158 - }
158.159 -
158.160 - return sb.append(']').toString();
158.161 - }
158.162 -
158.163 - private void ensureCapacity(final int minCapacity) {
158.164 - if ((minCapacity == 0)
158.165 - || (types != null) && (minCapacity <= types.length)) {
158.166 - return;
158.167 - }
158.168 -
158.169 - final int newCapacity =
158.170 - ((minCapacity + CAPACITY_INCREMENT - 1) / CAPACITY_INCREMENT)
158.171 - * CAPACITY_INCREMENT;
158.172 - final int[] newTypes = new int[newCapacity];
158.173 -
158.174 - if (size > 0) {
158.175 - arraycopy(types, 0, newTypes, 0, size);
158.176 - }
158.177 -
158.178 - types = newTypes;
158.179 - }
158.180 -
158.181 - // no System.arraycopy
158.182 - private void arraycopy(final int[] src, final int srcPos,
158.183 - final int[] dest, final int destPos,
158.184 - final int length) {
158.185 - for (int i = 0; i < length; ++i) {
158.186 - dest[destPos + i] = src[srcPos + i];
158.187 - }
158.188 - }
158.189 -}
159.1 --- a/rt/javap/src/main/java/org/apidesign/javap/Vector.java Tue Mar 05 07:57:16 2013 +0100
159.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
159.3 @@ -1,89 +0,0 @@
159.4 -/**
159.5 - * Back 2 Browser Bytecode Translator
159.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
159.7 - *
159.8 - * This program is free software: you can redistribute it and/or modify
159.9 - * it under the terms of the GNU General Public License as published by
159.10 - * the Free Software Foundation, version 2 of the License.
159.11 - *
159.12 - * This program is distributed in the hope that it will be useful,
159.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
159.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
159.15 - * GNU General Public License for more details.
159.16 - *
159.17 - * You should have received a copy of the GNU General Public License
159.18 - * along with this program. Look for COPYING file in the top folder.
159.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
159.20 - */
159.21 -package org.apidesign.javap;
159.22 -
159.23 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
159.24 -import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
159.25 -
159.26 -/** A JavaScript ready replacement for java.util.Vector
159.27 - *
159.28 - * @author Jaroslav Tulach <jtulach@netbeans.org>
159.29 - */
159.30 -@JavaScriptPrototype(prototype = "new Array" )
159.31 -final class Vector {
159.32 - private Object[] arr;
159.33 -
159.34 - Vector() {
159.35 - }
159.36 -
159.37 - Vector(int i) {
159.38 - }
159.39 -
159.40 - void add(Object objectType) {
159.41 - addElement(objectType);
159.42 - }
159.43 - @JavaScriptBody(args = { "obj" }, body =
159.44 - "this.push(obj);"
159.45 - )
159.46 - void addElement(Object obj) {
159.47 - final int s = size();
159.48 - setSize(s + 1);
159.49 - setElementAt(obj, s);
159.50 - }
159.51 -
159.52 - @JavaScriptBody(args = { }, body =
159.53 - "return this.length;"
159.54 - )
159.55 - int size() {
159.56 - return arr == null ? 0 : arr.length;
159.57 - }
159.58 -
159.59 - @JavaScriptBody(args = { "newArr" }, body =
159.60 - "for (var i = 0; i < this.length; i++) {\n"
159.61 - + " newArr[i] = this[i];\n"
159.62 - + "}\n")
159.63 - void copyInto(Object[] newArr) {
159.64 - if (arr == null) {
159.65 - return;
159.66 - }
159.67 - int min = Math.min(newArr.length, arr.length);
159.68 - for (int i = 0; i < min; i++) {
159.69 - newArr[i] = arr[i];
159.70 - }
159.71 - }
159.72 -
159.73 - @JavaScriptBody(args = { "index" }, body =
159.74 - "return this[index];"
159.75 - )
159.76 - Object elementAt(int index) {
159.77 - return arr[index];
159.78 - }
159.79 -
159.80 - private void setSize(int len) {
159.81 - Object[] newArr = new Object[len];
159.82 - copyInto(newArr);
159.83 - arr = newArr;
159.84 - }
159.85 -
159.86 - @JavaScriptBody(args = { "val", "index" }, body =
159.87 - "this[index] = val;"
159.88 - )
159.89 - void setElementAt(Object val, int index) {
159.90 - arr[index] = val;
159.91 - }
159.92 -}
160.1 --- a/rt/launcher/pom.xml Tue Mar 05 07:57:16 2013 +0100
160.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
160.3 @@ -1,56 +0,0 @@
160.4 -<?xml version="1.0"?>
160.5 -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
160.6 - <modelVersion>4.0.0</modelVersion>
160.7 - <parent>
160.8 - <groupId>org.apidesign.bck2brwsr</groupId>
160.9 - <artifactId>rt</artifactId>
160.10 - <version>0.4-SNAPSHOT</version>
160.11 - </parent>
160.12 - <groupId>org.apidesign.bck2brwsr</groupId>
160.13 - <artifactId>launcher</artifactId>
160.14 - <version>0.4-SNAPSHOT</version>
160.15 - <name>Bck2Brwsr Launcher</name>
160.16 - <url>http://maven.apache.org</url>
160.17 - <build>
160.18 - <plugins>
160.19 - <plugin>
160.20 - <groupId>org.apache.maven.plugins</groupId>
160.21 - <artifactId>maven-compiler-plugin</artifactId>
160.22 - <version>2.3.2</version>
160.23 - <configuration>
160.24 - <source>1.7</source>
160.25 - <target>1.7</target>
160.26 - </configuration>
160.27 - </plugin>
160.28 - <plugin>
160.29 - <groupId>org.apache.maven.plugins</groupId>
160.30 - <artifactId>maven-javadoc-plugin</artifactId>
160.31 - <configuration>
160.32 - <excludePackageNames>org.apidesign.bck2brwsr.launcher.impl</excludePackageNames>
160.33 - <skip>false</skip>
160.34 - </configuration>
160.35 - </plugin>
160.36 - </plugins>
160.37 - </build>
160.38 - <properties>
160.39 - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
160.40 - </properties>
160.41 - <dependencies>
160.42 - <dependency>
160.43 - <groupId>junit</groupId>
160.44 - <artifactId>junit</artifactId>
160.45 - <version>3.8.1</version>
160.46 - <scope>test</scope>
160.47 - </dependency>
160.48 - <dependency>
160.49 - <groupId>org.glassfish.grizzly</groupId>
160.50 - <artifactId>grizzly-http-server</artifactId>
160.51 - <version>2.2.19</version>
160.52 - </dependency>
160.53 - <dependency>
160.54 - <groupId>${project.groupId}</groupId>
160.55 - <artifactId>vm4brwsr</artifactId>
160.56 - <version>${project.version}</version>
160.57 - </dependency>
160.58 - </dependencies>
160.59 -</project>
161.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Tue Mar 05 07:57:16 2013 +0100
161.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
161.3 @@ -1,570 +0,0 @@
161.4 -/**
161.5 - * Back 2 Browser Bytecode Translator
161.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
161.7 - *
161.8 - * This program is free software: you can redistribute it and/or modify
161.9 - * it under the terms of the GNU General Public License as published by
161.10 - * the Free Software Foundation, version 2 of the License.
161.11 - *
161.12 - * This program is distributed in the hope that it will be useful,
161.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
161.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
161.15 - * GNU General Public License for more details.
161.16 - *
161.17 - * You should have received a copy of the GNU General Public License
161.18 - * along with this program. Look for COPYING file in the top folder.
161.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
161.20 - */
161.21 -package org.apidesign.bck2brwsr.launcher;
161.22 -
161.23 -import java.io.Closeable;
161.24 -import java.io.File;
161.25 -import java.io.IOException;
161.26 -import java.io.InputStream;
161.27 -import java.io.InterruptedIOException;
161.28 -import java.io.OutputStream;
161.29 -import java.io.UnsupportedEncodingException;
161.30 -import java.io.Writer;
161.31 -import java.net.URI;
161.32 -import java.net.URISyntaxException;
161.33 -import java.net.URL;
161.34 -import java.util.ArrayList;
161.35 -import java.util.Arrays;
161.36 -import java.util.Enumeration;
161.37 -import java.util.LinkedHashSet;
161.38 -import java.util.List;
161.39 -import java.util.Set;
161.40 -import java.util.concurrent.BlockingQueue;
161.41 -import java.util.concurrent.CountDownLatch;
161.42 -import java.util.concurrent.LinkedBlockingQueue;
161.43 -import java.util.concurrent.TimeUnit;
161.44 -import java.util.logging.Level;
161.45 -import java.util.logging.Logger;
161.46 -import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource;
161.47 -import org.apidesign.vm4brwsr.Bck2Brwsr;
161.48 -import org.glassfish.grizzly.PortRange;
161.49 -import org.glassfish.grizzly.http.server.HttpHandler;
161.50 -import org.glassfish.grizzly.http.server.HttpServer;
161.51 -import org.glassfish.grizzly.http.server.NetworkListener;
161.52 -import org.glassfish.grizzly.http.server.Request;
161.53 -import org.glassfish.grizzly.http.server.Response;
161.54 -import org.glassfish.grizzly.http.server.ServerConfiguration;
161.55 -import org.glassfish.grizzly.http.util.HttpStatus;
161.56 -
161.57 -/**
161.58 - * Lightweight server to launch Bck2Brwsr applications and tests.
161.59 - * Supports execution in native browser as well as Java's internal
161.60 - * execution engine.
161.61 - */
161.62 -final class Bck2BrwsrLauncher extends Launcher implements Closeable {
161.63 - private static final Logger LOG = Logger.getLogger(Bck2BrwsrLauncher.class.getName());
161.64 - private static final InvocationContext END = new InvocationContext(null, null, null);
161.65 - private final Set<ClassLoader> loaders = new LinkedHashSet<>();
161.66 - private final BlockingQueue<InvocationContext> methods = new LinkedBlockingQueue<>();
161.67 - private long timeOut;
161.68 - private final Res resources = new Res();
161.69 - private final String cmd;
161.70 - private Object[] brwsr;
161.71 - private HttpServer server;
161.72 - private CountDownLatch wait;
161.73 -
161.74 - public Bck2BrwsrLauncher(String cmd) {
161.75 - this.cmd = cmd;
161.76 - }
161.77 -
161.78 - @Override
161.79 - InvocationContext runMethod(InvocationContext c) throws IOException {
161.80 - loaders.add(c.clazz.getClassLoader());
161.81 - methods.add(c);
161.82 - try {
161.83 - c.await(timeOut);
161.84 - } catch (InterruptedException ex) {
161.85 - throw new IOException(ex);
161.86 - }
161.87 - return c;
161.88 - }
161.89 -
161.90 - public void setTimeout(long ms) {
161.91 - timeOut = ms;
161.92 - }
161.93 -
161.94 - public void addClassLoader(ClassLoader url) {
161.95 - this.loaders.add(url);
161.96 - }
161.97 -
161.98 - public void showURL(String startpage) throws IOException {
161.99 - if (!startpage.startsWith("/")) {
161.100 - startpage = "/" + startpage;
161.101 - }
161.102 - HttpServer s = initServer(".", true);
161.103 - int last = startpage.lastIndexOf('/');
161.104 - String simpleName = startpage.substring(last);
161.105 - s.getServerConfiguration().addHttpHandler(new Page(resources, startpage), simpleName);
161.106 - s.getServerConfiguration().addHttpHandler(new Page(resources, null), "/");
161.107 - try {
161.108 - launchServerAndBrwsr(s, simpleName);
161.109 - } catch (URISyntaxException | InterruptedException ex) {
161.110 - throw new IOException(ex);
161.111 - }
161.112 - }
161.113 -
161.114 - void showDirectory(File dir, String startpage) throws IOException {
161.115 - if (!startpage.startsWith("/")) {
161.116 - startpage = "/" + startpage;
161.117 - }
161.118 - HttpServer s = initServer(dir.getPath(), false);
161.119 - try {
161.120 - launchServerAndBrwsr(s, startpage);
161.121 - } catch (URISyntaxException | InterruptedException ex) {
161.122 - throw new IOException(ex);
161.123 - }
161.124 - }
161.125 -
161.126 - @Override
161.127 - public void initialize() throws IOException {
161.128 - try {
161.129 - executeInBrowser();
161.130 - } catch (InterruptedException ex) {
161.131 - final InterruptedIOException iio = new InterruptedIOException(ex.getMessage());
161.132 - iio.initCause(ex);
161.133 - throw iio;
161.134 - } catch (Exception ex) {
161.135 - if (ex instanceof IOException) {
161.136 - throw (IOException)ex;
161.137 - }
161.138 - if (ex instanceof RuntimeException) {
161.139 - throw (RuntimeException)ex;
161.140 - }
161.141 - throw new IOException(ex);
161.142 - }
161.143 - }
161.144 -
161.145 - private HttpServer initServer(String path, boolean addClasses) throws IOException {
161.146 - HttpServer s = HttpServer.createSimpleServer(path, new PortRange(8080, 65535));
161.147 -
161.148 - final ServerConfiguration conf = s.getServerConfiguration();
161.149 - if (addClasses) {
161.150 - conf.addHttpHandler(new VM(resources), "/bck2brwsr.js");
161.151 - conf.addHttpHandler(new Classes(resources), "/classes/");
161.152 - }
161.153 - return s;
161.154 - }
161.155 -
161.156 - private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
161.157 - wait = new CountDownLatch(1);
161.158 - server = initServer(".", true);
161.159 - final ServerConfiguration conf = server.getServerConfiguration();
161.160 -
161.161 - class DynamicResourceHandler extends HttpHandler {
161.162 - private final InvocationContext ic;
161.163 - public DynamicResourceHandler(InvocationContext ic) {
161.164 - if (ic == null || ic.resources.isEmpty()) {
161.165 - throw new NullPointerException();
161.166 - }
161.167 - this.ic = ic;
161.168 - for (Resource r : ic.resources) {
161.169 - conf.addHttpHandler(this, r.httpPath);
161.170 - }
161.171 - }
161.172 -
161.173 - public void close() {
161.174 - conf.removeHttpHandler(this);
161.175 - }
161.176 -
161.177 - @Override
161.178 - public void service(Request request, Response response) throws Exception {
161.179 - for (Resource r : ic.resources) {
161.180 - if (r.httpPath.equals(request.getRequestURI())) {
161.181 - LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
161.182 - response.setContentType(r.httpType);
161.183 - copyStream(r.httpContent, response.getOutputStream(), null);
161.184 - }
161.185 - }
161.186 - }
161.187 - }
161.188 -
161.189 - conf.addHttpHandler(new Page(resources,
161.190 - "org/apidesign/bck2brwsr/launcher/harness.xhtml"
161.191 - ), "/execute");
161.192 -
161.193 - conf.addHttpHandler(new HttpHandler() {
161.194 - int cnt;
161.195 - List<InvocationContext> cases = new ArrayList<>();
161.196 - DynamicResourceHandler prev;
161.197 - @Override
161.198 - public void service(Request request, Response response) throws Exception {
161.199 - String id = request.getParameter("request");
161.200 - String value = request.getParameter("result");
161.201 - if (value != null && value.indexOf((char)0xC5) != -1) {
161.202 - value = toUTF8(value);
161.203 - }
161.204 -
161.205 -
161.206 - InvocationContext mi = null;
161.207 - int caseNmbr = -1;
161.208 -
161.209 - if (id != null && value != null) {
161.210 - LOG.log(Level.INFO, "Received result for case {0} = {1}", new Object[]{id, value});
161.211 - value = decodeURL(value);
161.212 - int indx = Integer.parseInt(id);
161.213 - cases.get(indx).result(value, null);
161.214 - if (++indx < cases.size()) {
161.215 - mi = cases.get(indx);
161.216 - LOG.log(Level.INFO, "Re-executing case {0}", indx);
161.217 - caseNmbr = indx;
161.218 - }
161.219 - } else {
161.220 - if (!cases.isEmpty()) {
161.221 - LOG.info("Re-executing test cases");
161.222 - mi = cases.get(0);
161.223 - caseNmbr = 0;
161.224 - }
161.225 - }
161.226 -
161.227 - if (prev != null) {
161.228 - prev.close();
161.229 - prev = null;
161.230 - }
161.231 -
161.232 - if (mi == null) {
161.233 - mi = methods.take();
161.234 - caseNmbr = cnt++;
161.235 - }
161.236 - if (mi == END) {
161.237 - response.getWriter().write("");
161.238 - wait.countDown();
161.239 - cnt = 0;
161.240 - LOG.log(Level.INFO, "End of data reached. Exiting.");
161.241 - return;
161.242 - }
161.243 -
161.244 - if (!mi.resources.isEmpty()) {
161.245 - prev = new DynamicResourceHandler(mi);
161.246 - }
161.247 -
161.248 - cases.add(mi);
161.249 - final String cn = mi.clazz.getName();
161.250 - final String mn = mi.methodName;
161.251 - LOG.log(Level.INFO, "Request for {0} case. Sending {1}.{2}", new Object[]{caseNmbr, cn, mn});
161.252 - response.getWriter().write("{"
161.253 - + "className: '" + cn + "', "
161.254 - + "methodName: '" + mn + "', "
161.255 - + "request: " + caseNmbr
161.256 - );
161.257 - if (mi.html != null) {
161.258 - response.getWriter().write(", html: '");
161.259 - response.getWriter().write(encodeJSON(mi.html));
161.260 - response.getWriter().write("'");
161.261 - }
161.262 - response.getWriter().write("}");
161.263 - }
161.264 - }, "/data");
161.265 -
161.266 - this.brwsr = launchServerAndBrwsr(server, "/execute");
161.267 - }
161.268 -
161.269 - private static String encodeJSON(String in) {
161.270 - StringBuilder sb = new StringBuilder();
161.271 - for (int i = 0; i < in.length(); i++) {
161.272 - char ch = in.charAt(i);
161.273 - if (ch < 32 || ch == '\'' || ch == '"') {
161.274 - sb.append("\\u");
161.275 - String hs = "0000" + Integer.toHexString(ch);
161.276 - hs = hs.substring(hs.length() - 4);
161.277 - sb.append(hs);
161.278 - } else {
161.279 - sb.append(ch);
161.280 - }
161.281 - }
161.282 - return sb.toString();
161.283 - }
161.284 -
161.285 - @Override
161.286 - public void shutdown() throws IOException {
161.287 - methods.offer(END);
161.288 - for (;;) {
161.289 - int prev = methods.size();
161.290 - try {
161.291 - if (wait != null && wait.await(timeOut, TimeUnit.MILLISECONDS)) {
161.292 - break;
161.293 - }
161.294 - } catch (InterruptedException ex) {
161.295 - throw new IOException(ex);
161.296 - }
161.297 - if (prev == methods.size()) {
161.298 - LOG.log(
161.299 - Level.WARNING,
161.300 - "Timeout and no test has been executed meanwhile (at {0}). Giving up.",
161.301 - methods.size()
161.302 - );
161.303 - break;
161.304 - }
161.305 - LOG.log(Level.INFO,
161.306 - "Timeout, but tests got from {0} to {1}. Trying again.",
161.307 - new Object[]{prev, methods.size()}
161.308 - );
161.309 - }
161.310 - stopServerAndBrwsr(server, brwsr);
161.311 - }
161.312 -
161.313 - static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
161.314 - for (;;) {
161.315 - int ch = is.read();
161.316 - if (ch == -1) {
161.317 - break;
161.318 - }
161.319 - if (ch == '$' && params.length > 0) {
161.320 - int cnt = is.read() - '0';
161.321 - if (cnt == 'U' - '0') {
161.322 - os.write(baseURL.getBytes("UTF-8"));
161.323 - }
161.324 - if (cnt >= 0 && cnt < params.length) {
161.325 - os.write(params[cnt].getBytes("UTF-8"));
161.326 - }
161.327 - } else {
161.328 - os.write(ch);
161.329 - }
161.330 - }
161.331 - }
161.332 -
161.333 - private Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
161.334 - server.start();
161.335 - NetworkListener listener = server.getListeners().iterator().next();
161.336 - int port = listener.getPort();
161.337 -
161.338 - URI uri = new URI("http://localhost:" + port + page);
161.339 - LOG.log(Level.INFO, "Showing {0}", uri);
161.340 - if (cmd == null) {
161.341 - try {
161.342 - LOG.log(Level.INFO, "Trying Desktop.browse on {0} {2} by {1}", new Object[] {
161.343 - System.getProperty("java.vm.name"),
161.344 - System.getProperty("java.vm.vendor"),
161.345 - System.getProperty("java.vm.version"),
161.346 - });
161.347 - java.awt.Desktop.getDesktop().browse(uri);
161.348 - LOG.log(Level.INFO, "Desktop.browse successfully finished");
161.349 - return null;
161.350 - } catch (UnsupportedOperationException ex) {
161.351 - LOG.log(Level.INFO, "Desktop.browse not supported: {0}", ex.getMessage());
161.352 - LOG.log(Level.FINE, null, ex);
161.353 - }
161.354 - }
161.355 - {
161.356 - String cmdName = cmd == null ? "xdg-open" : cmd;
161.357 - String[] cmdArr = {
161.358 - cmdName, uri.toString()
161.359 - };
161.360 - LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmdArr));
161.361 - final Process process = Runtime.getRuntime().exec(cmdArr);
161.362 - return new Object[] { process, null };
161.363 - }
161.364 - }
161.365 - private static String toUTF8(String value) throws UnsupportedEncodingException {
161.366 - byte[] arr = new byte[value.length()];
161.367 - for (int i = 0; i < arr.length; i++) {
161.368 - arr[i] = (byte)value.charAt(i);
161.369 - }
161.370 - return new String(arr, "UTF-8");
161.371 - }
161.372 -
161.373 - private static String decodeURL(String s) {
161.374 - for (;;) {
161.375 - int pos = s.indexOf('%');
161.376 - if (pos == -1) {
161.377 - return s;
161.378 - }
161.379 - int i = Integer.parseInt(s.substring(pos + 1, pos + 2), 16);
161.380 - s = s.substring(0, pos) + (char)i + s.substring(pos + 2);
161.381 - }
161.382 - }
161.383 -
161.384 - private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException {
161.385 - if (brwsr == null) {
161.386 - return;
161.387 - }
161.388 - Process process = (Process)brwsr[0];
161.389 -
161.390 - server.stop();
161.391 - InputStream stdout = process.getInputStream();
161.392 - InputStream stderr = process.getErrorStream();
161.393 - drain("StdOut", stdout);
161.394 - drain("StdErr", stderr);
161.395 - process.destroy();
161.396 - int res;
161.397 - try {
161.398 - res = process.waitFor();
161.399 - } catch (InterruptedException ex) {
161.400 - throw new IOException(ex);
161.401 - }
161.402 - LOG.log(Level.INFO, "Exit code: {0}", res);
161.403 -
161.404 - deleteTree((File)brwsr[1]);
161.405 - }
161.406 -
161.407 - private static void drain(String name, InputStream is) throws IOException {
161.408 - int av = is.available();
161.409 - if (av > 0) {
161.410 - StringBuilder sb = new StringBuilder();
161.411 - sb.append("v== ").append(name).append(" ==v\n");
161.412 - while (av-- > 0) {
161.413 - sb.append((char)is.read());
161.414 - }
161.415 - sb.append("\n^== ").append(name).append(" ==^");
161.416 - LOG.log(Level.INFO, sb.toString());
161.417 - }
161.418 - }
161.419 -
161.420 - private void deleteTree(File file) {
161.421 - if (file == null) {
161.422 - return;
161.423 - }
161.424 - File[] arr = file.listFiles();
161.425 - if (arr != null) {
161.426 - for (File s : arr) {
161.427 - deleteTree(s);
161.428 - }
161.429 - }
161.430 - file.delete();
161.431 - }
161.432 -
161.433 - @Override
161.434 - public void close() throws IOException {
161.435 - shutdown();
161.436 - }
161.437 -
161.438 - private class Res implements Bck2Brwsr.Resources {
161.439 - @Override
161.440 - public InputStream get(String resource) throws IOException {
161.441 - for (ClassLoader l : loaders) {
161.442 - URL u = null;
161.443 - Enumeration<URL> en = l.getResources(resource);
161.444 - while (en.hasMoreElements()) {
161.445 - u = en.nextElement();
161.446 - }
161.447 - if (u != null) {
161.448 - return u.openStream();
161.449 - }
161.450 - }
161.451 - throw new IOException("Can't find " + resource);
161.452 - }
161.453 - }
161.454 -
161.455 - private static class Page extends HttpHandler {
161.456 - private final String resource;
161.457 - private final String[] args;
161.458 - private final Res res;
161.459 -
161.460 - public Page(Res res, String resource, String... args) {
161.461 - this.res = res;
161.462 - this.resource = resource;
161.463 - this.args = args.length == 0 ? new String[] { "$0" } : args;
161.464 - }
161.465 -
161.466 - @Override
161.467 - public void service(Request request, Response response) throws Exception {
161.468 - String r = resource;
161.469 - if (r == null) {
161.470 - r = request.getHttpHandlerPath();
161.471 - }
161.472 - if (r.startsWith("/")) {
161.473 - r = r.substring(1);
161.474 - }
161.475 - String[] replace = {};
161.476 - if (r.endsWith(".html")) {
161.477 - response.setContentType("text/html");
161.478 - LOG.info("Content type text/html");
161.479 - replace = args;
161.480 - }
161.481 - if (r.endsWith(".xhtml")) {
161.482 - response.setContentType("application/xhtml+xml");
161.483 - LOG.info("Content type application/xhtml+xml");
161.484 - replace = args;
161.485 - }
161.486 - OutputStream os = response.getOutputStream();
161.487 - try (InputStream is = res.get(r)) {
161.488 - copyStream(is, os, request.getRequestURL().toString(), replace);
161.489 - } catch (IOException ex) {
161.490 - response.setDetailMessage(ex.getLocalizedMessage());
161.491 - response.setError();
161.492 - response.setStatus(404);
161.493 - }
161.494 - }
161.495 - }
161.496 -
161.497 - private static class VM extends HttpHandler {
161.498 - private final String bck2brwsr;
161.499 -
161.500 - public VM(Res loader) throws IOException {
161.501 - StringBuilder sb = new StringBuilder();
161.502 - Bck2Brwsr.generate(sb, loader);
161.503 - sb.append(
161.504 - "(function WrapperVM(global) {"
161.505 - + " function ldCls(res) {\n"
161.506 - + " var request = new XMLHttpRequest();\n"
161.507 - + " request.open('GET', '/classes/' + res, false);\n"
161.508 - + " request.send();\n"
161.509 - + " if (request.status !== 200) return null;\n"
161.510 - + " var arr = eval('(' + request.responseText + ')');\n"
161.511 - + " return arr;\n"
161.512 - + " }\n"
161.513 - + " var prevvm = global.bck2brwsr;\n"
161.514 - + " global.bck2brwsr = function() {\n"
161.515 - + " var args = Array.prototype.slice.apply(arguments);\n"
161.516 - + " args.unshift(ldCls);\n"
161.517 - + " return prevvm.apply(null, args);\n"
161.518 - + " };\n"
161.519 - + "})(this);\n"
161.520 - );
161.521 - this.bck2brwsr = sb.toString();
161.522 - }
161.523 -
161.524 - @Override
161.525 - public void service(Request request, Response response) throws Exception {
161.526 - response.setCharacterEncoding("UTF-8");
161.527 - response.setContentType("text/javascript");
161.528 - response.getWriter().write(bck2brwsr);
161.529 - }
161.530 - }
161.531 -
161.532 - private static class Classes extends HttpHandler {
161.533 - private final Res loader;
161.534 -
161.535 - public Classes(Res loader) {
161.536 - this.loader = loader;
161.537 - }
161.538 -
161.539 - @Override
161.540 - public void service(Request request, Response response) throws Exception {
161.541 - String res = request.getHttpHandlerPath();
161.542 - if (res.startsWith("/")) {
161.543 - res = res.substring(1);
161.544 - }
161.545 - try (InputStream is = loader.get(res)) {
161.546 - response.setContentType("text/javascript");
161.547 - Writer w = response.getWriter();
161.548 - w.append("[");
161.549 - for (int i = 0;; i++) {
161.550 - int b = is.read();
161.551 - if (b == -1) {
161.552 - break;
161.553 - }
161.554 - if (i > 0) {
161.555 - w.append(", ");
161.556 - }
161.557 - if (i % 20 == 0) {
161.558 - w.write("\n");
161.559 - }
161.560 - if (b > 127) {
161.561 - b = b - 256;
161.562 - }
161.563 - w.append(Integer.toString(b));
161.564 - }
161.565 - w.append("\n]");
161.566 - } catch (IOException ex) {
161.567 - response.setStatus(HttpStatus.NOT_FOUND_404);
161.568 - response.setError();
161.569 - response.setDetailMessage(ex.getMessage());
161.570 - }
161.571 - }
161.572 - }
161.573 -}
162.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Tue Mar 05 07:57:16 2013 +0100
162.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
162.3 @@ -1,110 +0,0 @@
162.4 -/**
162.5 - * Back 2 Browser Bytecode Translator
162.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
162.7 - *
162.8 - * This program is free software: you can redistribute it and/or modify
162.9 - * it under the terms of the GNU General Public License as published by
162.10 - * the Free Software Foundation, version 2 of the License.
162.11 - *
162.12 - * This program is distributed in the hope that it will be useful,
162.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
162.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
162.15 - * GNU General Public License for more details.
162.16 - *
162.17 - * You should have received a copy of the GNU General Public License
162.18 - * along with this program. Look for COPYING file in the top folder.
162.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
162.20 - */
162.21 -package org.apidesign.bck2brwsr.launcher;
162.22 -
162.23 -import java.io.IOException;
162.24 -import java.io.InputStream;
162.25 -import java.util.ArrayList;
162.26 -import java.util.List;
162.27 -import java.util.concurrent.CountDownLatch;
162.28 -import java.util.concurrent.TimeUnit;
162.29 -
162.30 -/** Represents individual method invocation, its context and its result.
162.31 - *
162.32 - * @author Jaroslav Tulach <jtulach@netbeans.org>
162.33 - */
162.34 -public final class InvocationContext {
162.35 - final CountDownLatch wait = new CountDownLatch(1);
162.36 - final Class<?> clazz;
162.37 - final String methodName;
162.38 - private final Launcher launcher;
162.39 - private String result;
162.40 - private Throwable exception;
162.41 - String html;
162.42 - final List<Resource> resources = new ArrayList<>();
162.43 -
162.44 - InvocationContext(Launcher launcher, Class<?> clazz, String methodName) {
162.45 - this.launcher = launcher;
162.46 - this.clazz = clazz;
162.47 - this.methodName = methodName;
162.48 - }
162.49 -
162.50 - /** An HTML fragment to be available for the execution. Useful primarily when
162.51 - * executing in a browser via {@link Launcher#createBrowser(java.lang.String)}.
162.52 - * @param html the html fragment
162.53 - */
162.54 - public void setHtmlFragment(String html) {
162.55 - this.html = html;
162.56 - }
162.57 -
162.58 - /** HTTP resource to be available during execution. An invocation may
162.59 - * perform an HTTP query and obtain a resource relative to the page.
162.60 - */
162.61 - public void addHttpResource(String relativePath, String mimeType, InputStream content) {
162.62 - if (relativePath == null || mimeType == null || content == null) {
162.63 - throw new NullPointerException();
162.64 - }
162.65 - resources.add(new Resource(content, mimeType, relativePath));
162.66 - }
162.67 -
162.68 - /** Invokes the associated method.
162.69 - * @return the textual result of the invocation
162.70 - */
162.71 - public String invoke() throws IOException {
162.72 - launcher.runMethod(this);
162.73 - return toString();
162.74 - }
162.75 -
162.76 - /** Obtains textual result of the invocation.
162.77 - * @return text representing the exception or result value
162.78 - */
162.79 - @Override
162.80 - public String toString() {
162.81 - if (exception != null) {
162.82 - return exception.toString();
162.83 - }
162.84 - return result;
162.85 - }
162.86 -
162.87 - /**
162.88 - * @param timeOut
162.89 - * @throws InterruptedException
162.90 - */
162.91 - void await(long timeOut) throws InterruptedException {
162.92 - wait.await(timeOut, TimeUnit.MILLISECONDS);
162.93 - }
162.94 -
162.95 - void result(String r, Throwable e) {
162.96 - this.result = r;
162.97 - this.exception = e;
162.98 - wait.countDown();
162.99 - }
162.100 -
162.101 -
162.102 - static final class Resource {
162.103 - final InputStream httpContent;
162.104 - final String httpType;
162.105 - final String httpPath;
162.106 -
162.107 - Resource(InputStream httpContent, String httpType, String httpPath) {
162.108 - this.httpContent = httpContent;
162.109 - this.httpType = httpType;
162.110 - this.httpPath = httpPath;
162.111 - }
162.112 - }
162.113 -}
163.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java Tue Mar 05 07:57:16 2013 +0100
163.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
163.3 @@ -1,132 +0,0 @@
163.4 -/**
163.5 - * Back 2 Browser Bytecode Translator
163.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
163.7 - *
163.8 - * This program is free software: you can redistribute it and/or modify
163.9 - * it under the terms of the GNU General Public License as published by
163.10 - * the Free Software Foundation, version 2 of the License.
163.11 - *
163.12 - * This program is distributed in the hope that it will be useful,
163.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
163.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
163.15 - * GNU General Public License for more details.
163.16 - *
163.17 - * You should have received a copy of the GNU General Public License
163.18 - * along with this program. Look for COPYING file in the top folder.
163.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
163.20 - */
163.21 -package org.apidesign.bck2brwsr.launcher;
163.22 -
163.23 -import org.apidesign.bck2brwsr.launcher.impl.Console;
163.24 -import java.io.IOException;
163.25 -import java.io.InputStream;
163.26 -import java.net.URL;
163.27 -import java.util.Enumeration;
163.28 -import java.util.LinkedHashSet;
163.29 -import java.util.Set;
163.30 -import java.util.logging.Level;
163.31 -import java.util.logging.Logger;
163.32 -import javax.script.Invocable;
163.33 -import javax.script.ScriptEngine;
163.34 -import javax.script.ScriptEngineManager;
163.35 -import javax.script.ScriptException;
163.36 -import org.apidesign.vm4brwsr.Bck2Brwsr;
163.37 -
163.38 -/**
163.39 - * Tests execution in Java's internal scripting engine.
163.40 - */
163.41 -final class JSLauncher extends Launcher {
163.42 - private static final Logger LOG = Logger.getLogger(JSLauncher.class.getName());
163.43 - private Set<ClassLoader> loaders = new LinkedHashSet<>();
163.44 - private final Res resources = new Res();
163.45 - private Invocable code;
163.46 - private StringBuilder codeSeq;
163.47 - private Object console;
163.48 -
163.49 -
163.50 - @Override InvocationContext runMethod(InvocationContext mi) {
163.51 - loaders.add(mi.clazz.getClassLoader());
163.52 - try {
163.53 - long time = System.currentTimeMillis();
163.54 - LOG.log(Level.FINE, "Invoking {0}.{1}", new Object[]{mi.clazz.getName(), mi.methodName});
163.55 - String res = code.invokeMethod(
163.56 - console,
163.57 - "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
163.58 - mi.clazz.getName(), mi.methodName).toString();
163.59 - time = System.currentTimeMillis() - time;
163.60 - LOG.log(Level.FINE, "Resut of {0}.{1} = {2} in {3} ms", new Object[]{mi.clazz.getName(), mi.methodName, res, time});
163.61 - mi.result(res, null);
163.62 - } catch (ScriptException | NoSuchMethodException ex) {
163.63 - mi.result(null, ex);
163.64 - }
163.65 - return mi;
163.66 - }
163.67 -
163.68 - public void addClassLoader(ClassLoader url) {
163.69 - this.loaders.add(url);
163.70 - }
163.71 -
163.72 - @Override
163.73 - public void initialize() throws IOException {
163.74 - try {
163.75 - initRhino();
163.76 - } catch (Exception ex) {
163.77 - if (ex instanceof IOException) {
163.78 - throw (IOException)ex;
163.79 - }
163.80 - if (ex instanceof RuntimeException) {
163.81 - throw (RuntimeException)ex;
163.82 - }
163.83 - throw new IOException(ex);
163.84 - }
163.85 - }
163.86 -
163.87 - private void initRhino() throws IOException, ScriptException, NoSuchMethodException {
163.88 - StringBuilder sb = new StringBuilder();
163.89 - Bck2Brwsr.generate(sb, new Res());
163.90 -
163.91 - ScriptEngineManager sem = new ScriptEngineManager();
163.92 - ScriptEngine mach = sem.getEngineByExtension("js");
163.93 -
163.94 - sb.append(
163.95 - "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.impl.Console.read);"
163.96 - + "\nfunction initVM() { return vm; };"
163.97 - + "\n");
163.98 -
163.99 - Object res = mach.eval(sb.toString());
163.100 - if (!(mach instanceof Invocable)) {
163.101 - throw new IOException("It is invocable object: " + res);
163.102 - }
163.103 - code = (Invocable) mach;
163.104 - codeSeq = sb;
163.105 -
163.106 - Object vm = code.invokeFunction("initVM");
163.107 - console = code.invokeMethod(vm, "loadClass", Console.class.getName());
163.108 - }
163.109 -
163.110 - @Override
163.111 - public void shutdown() throws IOException {
163.112 - }
163.113 -
163.114 - @Override
163.115 - public String toString() {
163.116 - return codeSeq.toString();
163.117 - }
163.118 -
163.119 - private class Res implements Bck2Brwsr.Resources {
163.120 - @Override
163.121 - public InputStream get(String resource) throws IOException {
163.122 - for (ClassLoader l : loaders) {
163.123 - URL u = null;
163.124 - Enumeration<URL> en = l.getResources(resource);
163.125 - while (en.hasMoreElements()) {
163.126 - u = en.nextElement();
163.127 - }
163.128 - if (u != null) {
163.129 - return u.openStream();
163.130 - }
163.131 - }
163.132 - throw new IOException("Can't find " + resource);
163.133 - }
163.134 - }
163.135 -}
164.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java Tue Mar 05 07:57:16 2013 +0100
164.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
164.3 @@ -1,117 +0,0 @@
164.4 -/**
164.5 - * Back 2 Browser Bytecode Translator
164.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
164.7 - *
164.8 - * This program is free software: you can redistribute it and/or modify
164.9 - * it under the terms of the GNU General Public License as published by
164.10 - * the Free Software Foundation, version 2 of the License.
164.11 - *
164.12 - * This program is distributed in the hope that it will be useful,
164.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
164.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
164.15 - * GNU General Public License for more details.
164.16 - *
164.17 - * You should have received a copy of the GNU General Public License
164.18 - * along with this program. Look for COPYING file in the top folder.
164.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
164.20 - */
164.21 -package org.apidesign.bck2brwsr.launcher;
164.22 -
164.23 -import java.io.Closeable;
164.24 -import java.io.File;
164.25 -import java.io.IOException;
164.26 -import java.net.URLClassLoader;
164.27 -import org.apidesign.vm4brwsr.Bck2Brwsr;
164.28 -
164.29 -/** An abstraction for executing tests in a Bck2Brwsr virtual machine.
164.30 - * Either in {@linkm Launcher#createJavaScript JavaScript engine},
164.31 - * or in {@linkm Launcher#createBrowser external browser}.
164.32 - * <p>
164.33 - * There also are methods to {@link #showDir(java.io.File, java.lang.String) display pages}
164.34 - * in an external browser served by internal HTTP server.
164.35 - *
164.36 - * @author Jaroslav Tulach <jtulach@netbeans.org>
164.37 - */
164.38 -public abstract class Launcher {
164.39 -
164.40 - Launcher() {
164.41 - }
164.42 -
164.43 - /** Initializes the launcher. This may mean starting a web browser or
164.44 - * initializing execution engine.
164.45 - * @throws IOException if something goes wrong
164.46 - */
164.47 - public abstract void initialize() throws IOException;
164.48 -
164.49 - /** Shuts down the launcher.
164.50 - * @throws IOException if something goes wrong
164.51 - */
164.52 - public abstract void shutdown() throws IOException;
164.53 -
164.54 -
164.55 - /** Builds an invocation context. The context can later be customized
164.56 - * and {@link InvocationContext#invoke() invoked}.
164.57 - *
164.58 - * @param clazz the class to execute method from
164.59 - * @param method the method to execute
164.60 - * @return the context pointing to the selected method
164.61 - */
164.62 - public InvocationContext createInvocation(Class<?> clazz, String method) {
164.63 - return new InvocationContext(this, clazz, method);
164.64 - }
164.65 -
164.66 -
164.67 - /** Creates launcher that uses internal JavaScript engine (Rhino).
164.68 - * @return the launcher
164.69 - */
164.70 - public static Launcher createJavaScript() {
164.71 - final JSLauncher l = new JSLauncher();
164.72 - l.addClassLoader(Bck2Brwsr.class.getClassLoader());
164.73 - return l;
164.74 - }
164.75 -
164.76 - /** Creates launcher that is using external browser.
164.77 - *
164.78 - * @param cmd <code>null</code> to use <code>java.awt.Desktop</code> to show the launcher
164.79 - * or a string to execute in an external process (with a parameter to the URL)
164.80 - * @return launcher executing in external browser.
164.81 - */
164.82 - public static Launcher createBrowser(String cmd) {
164.83 - final Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(cmd);
164.84 - l.addClassLoader(Bck2Brwsr.class.getClassLoader());
164.85 - l.setTimeout(180000);
164.86 - return l;
164.87 - }
164.88 -
164.89 - /** Starts an HTTP server which provides access to classes and resources
164.90 - * available in the <code>classes</code> URL and shows a start page
164.91 - * available as {@link ClassLoader#getResource(java.lang.String)} from the
164.92 - * provide classloader. Opens a browser with URL showing the start page.
164.93 - *
164.94 - * @param classes classloader offering access to classes and resources
164.95 - * @param startpage page to show in the browser
164.96 - * @return interface that allows one to stop the server
164.97 - * @throws IOException if something goes wrong
164.98 - */
164.99 - public static Closeable showURL(URLClassLoader classes, String startpage) throws IOException {
164.100 - Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(null);
164.101 - l.addClassLoader(classes);
164.102 - l.showURL(startpage);
164.103 - return l;
164.104 - }
164.105 - /** Starts an HTTP server which provides access to certain directory.
164.106 - * The <code>startpage</code> should be relative location inside the root
164.107 - * directory. Opens a browser with URL showing the start page.
164.108 - *
164.109 - * @param directory the root directory on disk
164.110 - * @param startpage relative path from the root to the page
164.111 - * @exception IOException if something goes wrong.
164.112 - */
164.113 - public static Closeable showDir(File directory, String startpage) throws IOException {
164.114 - Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(null);
164.115 - l.showDirectory(directory, startpage);
164.116 - return l;
164.117 - }
164.118 -
164.119 - abstract InvocationContext runMethod(InvocationContext c) throws IOException;
164.120 -}
165.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Tue Mar 05 07:57:16 2013 +0100
165.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
165.3 @@ -1,260 +0,0 @@
165.4 -/**
165.5 - * Back 2 Browser Bytecode Translator
165.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
165.7 - *
165.8 - * This program is free software: you can redistribute it and/or modify
165.9 - * it under the terms of the GNU General Public License as published by
165.10 - * the Free Software Foundation, version 2 of the License.
165.11 - *
165.12 - * This program is distributed in the hope that it will be useful,
165.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
165.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
165.15 - * GNU General Public License for more details.
165.16 - *
165.17 - * You should have received a copy of the GNU General Public License
165.18 - * along with this program. Look for COPYING file in the top folder.
165.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
165.20 - */
165.21 -package org.apidesign.bck2brwsr.launcher.impl;
165.22 -
165.23 -import java.io.IOException;
165.24 -import java.io.InputStream;
165.25 -import java.io.UnsupportedEncodingException;
165.26 -import java.lang.reflect.InvocationTargetException;
165.27 -import java.lang.reflect.Method;
165.28 -import java.lang.reflect.Modifier;
165.29 -import java.net.URL;
165.30 -import java.util.Enumeration;
165.31 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
165.32 -
165.33 -/**
165.34 - *
165.35 - * @author Jaroslav Tulach <jtulach@netbeans.org>
165.36 - */
165.37 -public class Console {
165.38 - private Console() {
165.39 - }
165.40 - static {
165.41 - turnAssetionStatusOn();
165.42 - }
165.43 -
165.44 - @JavaScriptBody(args = {"id", "attr"}, body =
165.45 - "return window.document.getElementById(id)[attr].toString();")
165.46 - private static native Object getAttr(String id, String attr);
165.47 -
165.48 - @JavaScriptBody(args = {"id", "attr", "value"}, body =
165.49 - "window.document.getElementById(id)[attr] = value;")
165.50 - private static native void setAttr(String id, String attr, Object value);
165.51 -
165.52 - @JavaScriptBody(args = {}, body = "return; window.close();")
165.53 - private static native void closeWindow();
165.54 -
165.55 - private static void log(String newText) {
165.56 - String id = "bck2brwsr.result";
165.57 - String attr = "value";
165.58 - setAttr(id, attr, getAttr(id, attr) + "\n" + newText);
165.59 - setAttr(id, "scrollTop", getAttr(id, "scrollHeight"));
165.60 - }
165.61 -
165.62 - public static void execute() throws Exception {
165.63 - String clazz = (String) getAttr("clazz", "value");
165.64 - String method = (String) getAttr("method", "value");
165.65 - Object res = invokeMethod(clazz, method);
165.66 - setAttr("bck2brwsr.result", "value", res);
165.67 - }
165.68 -
165.69 - @JavaScriptBody(args = { "url", "callback", "arr" }, body = ""
165.70 - + "var request = new XMLHttpRequest();\n"
165.71 - + "request.open('GET', url, true);\n"
165.72 - + "request.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');\n"
165.73 - + "request.onreadystatechange = function() {\n"
165.74 - + " if (this.readyState!==4) return;\n"
165.75 - + " arr[0] = this.responseText;\n"
165.76 - + " callback.run__V();\n"
165.77 - + "};"
165.78 - + "request.send();"
165.79 - )
165.80 - private static native void loadText(String url, Runnable callback, String[] arr) throws IOException;
165.81 -
165.82 - public static void harness(String url) throws IOException {
165.83 - log("Connecting to " + url);
165.84 - Request r = new Request(url);
165.85 - }
165.86 -
165.87 - private static class Request implements Runnable {
165.88 - private final String[] arr = { null };
165.89 - private final String url;
165.90 -
165.91 - private Request(String url) throws IOException {
165.92 - this.url = url;
165.93 - loadText(url, this, arr);
165.94 - }
165.95 -
165.96 - @Override
165.97 - public void run() {
165.98 - try {
165.99 - String data = arr[0];
165.100 - log("\nGot \"" + data + "\"");
165.101 -
165.102 - if (data == null) {
165.103 - log("Some error exiting");
165.104 - closeWindow();
165.105 - return;
165.106 - }
165.107 -
165.108 - if (data.isEmpty()) {
165.109 - log("No data, exiting");
165.110 - closeWindow();
165.111 - return;
165.112 - }
165.113 -
165.114 - Case c = Case.parseData(data);
165.115 - if (c.getHtmlFragment() != null) {
165.116 - setAttr("bck2brwsr.fragment", "innerHTML", c.getHtmlFragment());
165.117 - }
165.118 - log("Invoking " + c.getClassName() + '.' + c.getMethodName() + " as request: " + c.getRequestId());
165.119 -
165.120 - Object result = invokeMethod(c.getClassName(), c.getMethodName());
165.121 -
165.122 - setAttr("bck2brwsr.fragment", "innerHTML", "");
165.123 - log("Result: " + result);
165.124 -
165.125 - result = encodeURL("" + result);
165.126 -
165.127 - log("Sending back: " + url + "?request=" + c.getRequestId() + "&result=" + result);
165.128 - String u = url + "?request=" + c.getRequestId() + "&result=" + result;
165.129 -
165.130 - loadText(u, this, arr);
165.131 -
165.132 - } catch (Exception ex) {
165.133 - log(ex.getClass().getName() + ":" + ex.getMessage());
165.134 - }
165.135 - }
165.136 - }
165.137 -
165.138 - private static String encodeURL(String r) throws UnsupportedEncodingException {
165.139 - final String SPECIAL = "%$&+,/:;=?@";
165.140 - StringBuilder sb = new StringBuilder();
165.141 - byte[] utf8 = r.getBytes("UTF-8");
165.142 - for (int i = 0; i < utf8.length; i++) {
165.143 - int ch = utf8[i] & 0xff;
165.144 - if (ch < 32 || ch > 127 || SPECIAL.indexOf(ch) >= 0) {
165.145 - final String numbers = "0" + Integer.toHexString(ch);
165.146 - sb.append("%").append(numbers.substring(numbers.length() - 2));
165.147 - } else {
165.148 - if (ch == 32) {
165.149 - sb.append("+");
165.150 - } else {
165.151 - sb.append((char)ch);
165.152 - }
165.153 - }
165.154 - }
165.155 - return sb.toString();
165.156 - }
165.157 -
165.158 - static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {
165.159 - final Object r = invokeMethod(clazz, method);
165.160 - return r == null ? "null" : r.toString().toString();
165.161 - }
165.162 -
165.163 - /** Helper method that inspects the classpath and loads given resource
165.164 - * (usually a class file). Used while running tests in Rhino.
165.165 - *
165.166 - * @param name resource name to find
165.167 - * @return the array of bytes in the given resource
165.168 - * @throws IOException I/O in case something goes wrong
165.169 - */
165.170 - public static byte[] read(String name) throws IOException {
165.171 - URL u = null;
165.172 - Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
165.173 - while (en.hasMoreElements()) {
165.174 - u = en.nextElement();
165.175 - }
165.176 - if (u == null) {
165.177 - throw new IOException("Can't find " + name);
165.178 - }
165.179 - try (InputStream is = u.openStream()) {
165.180 - byte[] arr;
165.181 - arr = new byte[is.available()];
165.182 - int offset = 0;
165.183 - while (offset < arr.length) {
165.184 - int len = is.read(arr, offset, arr.length - offset);
165.185 - if (len == -1) {
165.186 - throw new IOException("Can't read " + name);
165.187 - }
165.188 - offset += len;
165.189 - }
165.190 - return arr;
165.191 - }
165.192 - }
165.193 -
165.194 - private static Object invokeMethod(String clazz, String method)
165.195 - throws ClassNotFoundException, InvocationTargetException,
165.196 - SecurityException, IllegalAccessException, IllegalArgumentException,
165.197 - InstantiationException {
165.198 - Method found = null;
165.199 - Class<?> c = Class.forName(clazz);
165.200 - for (Method m : c.getMethods()) {
165.201 - if (m.getName().equals(method)) {
165.202 - found = m;
165.203 - }
165.204 - }
165.205 - Object res;
165.206 - if (found != null) {
165.207 - try {
165.208 - if ((found.getModifiers() & Modifier.STATIC) != 0) {
165.209 - res = found.invoke(null);
165.210 - } else {
165.211 - res = found.invoke(c.newInstance());
165.212 - }
165.213 - } catch (Throwable ex) {
165.214 - res = ex.getClass().getName() + ":" + ex.getMessage();
165.215 - }
165.216 - } else {
165.217 - res = "Can't find method " + method + " in " + clazz;
165.218 - }
165.219 - return res;
165.220 - }
165.221 -
165.222 - @JavaScriptBody(args = {}, body = "vm.desiredAssertionStatus = true;")
165.223 - private static void turnAssetionStatusOn() {
165.224 - }
165.225 -
165.226 - private static final class Case {
165.227 - private final Object data;
165.228 -
165.229 - private Case(Object data) {
165.230 - this.data = data;
165.231 - }
165.232 -
165.233 - public static Case parseData(String s) {
165.234 - return new Case(toJSON(s));
165.235 - }
165.236 -
165.237 - public String getMethodName() {
165.238 - return value("methodName", data);
165.239 - }
165.240 -
165.241 - public String getClassName() {
165.242 - return value("className", data);
165.243 - }
165.244 -
165.245 - public String getRequestId() {
165.246 - return value("request", data);
165.247 - }
165.248 -
165.249 - public String getHtmlFragment() {
165.250 - return value("html", data);
165.251 - }
165.252 -
165.253 - @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
165.254 - private static native Object toJSON(String s);
165.255 -
165.256 - @JavaScriptBody(args = {"p", "d"}, body =
165.257 - "var v = d[p];\n"
165.258 - + "if (typeof v === 'undefined') return null;\n"
165.259 - + "return v.toString();"
165.260 - )
165.261 - private static native String value(String p, Object d);
165.262 - }
165.263 -}
166.1 --- a/rt/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml Tue Mar 05 07:57:16 2013 +0100
166.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
166.3 @@ -1,43 +0,0 @@
166.4 -<?xml version="1.0" encoding="UTF-8"?>
166.5 -<!--
166.6 -
166.7 - Back 2 Browser Bytecode Translator
166.8 - Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
166.9 -
166.10 - This program is free software: you can redistribute it and/or modify
166.11 - it under the terms of the GNU General Public License as published by
166.12 - the Free Software Foundation, version 2 of the License.
166.13 -
166.14 - This program is distributed in the hope that it will be useful,
166.15 - but WITHOUT ANY WARRANTY; without even the implied warranty of
166.16 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
166.17 - GNU General Public License for more details.
166.18 -
166.19 - You should have received a copy of the GNU General Public License
166.20 - along with this program. Look for COPYING file in the top folder.
166.21 - If not, see http://opensource.org/licenses/GPL-2.0.
166.22 -
166.23 --->
166.24 -<!DOCTYPE html>
166.25 -<html xmlns="http://www.w3.org/1999/xhtml">
166.26 - <head>
166.27 - <title>Bck2Brwsr Harness</title>
166.28 - </head>
166.29 - <body>
166.30 - <script src="/bck2brwsr.js"></script>
166.31 - <script>
166.32 - var vm = bck2brwsr();
166.33 - </script>
166.34 -
166.35 - <h1>Bck2Brwsr Execution Harness</h1>
166.36 -
166.37 - <textarea id="bck2brwsr.result" rows="25" style="width: 100%;" disabled="">
166.38 - </textarea>
166.39 -
166.40 - <div id="bck2brwsr.fragment"/>
166.41 -
166.42 - <script type="text/javascript">
166.43 - vm.loadClass('org.apidesign.bck2brwsr.launcher.impl.Console').harness__VLjava_lang_String_2('$U/../data');
166.44 - </script>
166.45 - </body>
166.46 -</html>
167.1 --- a/rt/mojo/pom.xml Tue Mar 05 07:57:16 2013 +0100
167.2 +++ b/rt/mojo/pom.xml Fri May 17 10:27:41 2013 +0200
167.3 @@ -4,14 +4,14 @@
167.4 <parent>
167.5 <groupId>org.apidesign.bck2brwsr</groupId>
167.6 <artifactId>rt</artifactId>
167.7 - <version>0.4-SNAPSHOT</version>
167.8 + <version>0.8-SNAPSHOT</version>
167.9 </parent>
167.10 <groupId>org.apidesign.bck2brwsr</groupId>
167.11 - <artifactId>mojo</artifactId>
167.12 - <version>0.4-SNAPSHOT</version>
167.13 + <artifactId>bck2brwsr-maven-plugin</artifactId>
167.14 + <version>0.8-SNAPSHOT</version>
167.15 <packaging>maven-plugin</packaging>
167.16 - <name>Bck2Brwsr Maven Project</name>
167.17 - <url>http://maven.apache.org</url>
167.18 + <name>Bck2Brwsr Maven Plugin</name>
167.19 + <url>http://bck2brwsr.apidesign.org/</url>
167.20 <build>
167.21 <plugins>
167.22 <plugin>
167.23 @@ -62,7 +62,7 @@
167.24 <dependency>
167.25 <groupId>${project.groupId}</groupId>
167.26 <artifactId>vm4brwsr</artifactId>
167.27 - <version>0.4-SNAPSHOT</version>
167.28 + <version>${project.version}</version>
167.29 <exclusions>
167.30 <exclusion>
167.31 <artifactId>emul.mini</artifactId>
167.32 @@ -81,5 +81,15 @@
167.33 <artifactId>launcher</artifactId>
167.34 <version>${project.version}</version>
167.35 </dependency>
167.36 + <dependency>
167.37 + <groupId>${project.groupId}</groupId>
167.38 + <artifactId>launcher.http</artifactId>
167.39 + <version>${project.version}</version>
167.40 + </dependency>
167.41 + <dependency>
167.42 + <groupId>${project.groupId}</groupId>
167.43 + <artifactId>launcher.fx</artifactId>
167.44 + <version>${project.version}</version>
167.45 + </dependency>
167.46 </dependencies>
167.47 </project>
168.1 --- a/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrswrMojo.java Tue Mar 05 07:57:16 2013 +0100
168.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
168.3 @@ -1,99 +0,0 @@
168.4 -/**
168.5 - * Back 2 Browser Bytecode Translator
168.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
168.7 - *
168.8 - * This program is free software: you can redistribute it and/or modify
168.9 - * it under the terms of the GNU General Public License as published by
168.10 - * the Free Software Foundation, version 2 of the License.
168.11 - *
168.12 - * This program is distributed in the hope that it will be useful,
168.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
168.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
168.15 - * GNU General Public License for more details.
168.16 - *
168.17 - * You should have received a copy of the GNU General Public License
168.18 - * along with this program. Look for COPYING file in the top folder.
168.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
168.20 - */
168.21 -package org.apidesign.bck2brwsr.mojo;
168.22 -
168.23 -import java.io.Closeable;
168.24 -import org.apache.maven.plugin.AbstractMojo;
168.25 -
168.26 -import java.io.File;
168.27 -import java.io.IOException;
168.28 -import java.net.MalformedURLException;
168.29 -import java.net.URL;
168.30 -import java.net.URLClassLoader;
168.31 -import java.util.ArrayList;
168.32 -import java.util.Collection;
168.33 -import java.util.List;
168.34 -import org.apache.maven.artifact.Artifact;
168.35 -import org.apache.maven.plugin.MojoExecutionException;
168.36 -import org.apache.maven.plugins.annotations.LifecyclePhase;
168.37 -import org.apache.maven.plugins.annotations.Mojo;
168.38 -import org.apache.maven.plugins.annotations.Parameter;
168.39 -import org.apache.maven.project.MavenProject;
168.40 -import org.apidesign.bck2brwsr.launcher.Launcher;
168.41 -
168.42 -/** Executes given HTML page in a browser. */
168.43 -@Mojo(name="brwsr", defaultPhase=LifecyclePhase.NONE)
168.44 -public class BrswrMojo extends AbstractMojo {
168.45 - public BrswrMojo() {
168.46 - }
168.47 - /** Resource to show as initial page */
168.48 - @Parameter
168.49 - private String startpage;
168.50 -
168.51 - @Parameter(defaultValue="${project}")
168.52 - private MavenProject prj;
168.53 -
168.54 - /** Root of the class files */
168.55 - @Parameter(defaultValue="${project.build.directory}/classes")
168.56 - private File classes;
168.57 -
168.58 - /** Root of all pages, and files, etc. */
168.59 - @Parameter
168.60 - private File directory;
168.61 -
168.62 - @Override
168.63 - public void execute() throws MojoExecutionException {
168.64 - if (startpage == null) {
168.65 - throw new MojoExecutionException("You have to provide a start page");
168.66 - }
168.67 -
168.68 - try {
168.69 - Closeable httpServer;
168.70 - if (directory != null) {
168.71 - httpServer = Launcher.showDir(directory, startpage);
168.72 - } else {
168.73 - URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
168.74 - try {
168.75 - httpServer = Launcher.showURL(url, startpage());
168.76 - } catch (Exception ex) {
168.77 - throw new MojoExecutionException("Can't open " + startpage(), ex);
168.78 - }
168.79 - }
168.80 - System.in.read();
168.81 - httpServer.close();
168.82 - } catch (IOException ex) {
168.83 - throw new MojoExecutionException("Can't show the browser", ex);
168.84 - }
168.85 - }
168.86 -
168.87 - private String startpage() {
168.88 - return startpage;
168.89 - }
168.90 -
168.91 - private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
168.92 - List<URL> arr = new ArrayList<URL>();
168.93 - arr.add(root.toURI().toURL());
168.94 - for (Artifact a : deps) {
168.95 - final File f = a.getFile();
168.96 - if (f != null) {
168.97 - arr.add(f.toURI().toURL());
168.98 - }
168.99 - }
168.100 - return new URLClassLoader(arr.toArray(new URL[0]), BrswrMojo.class.getClassLoader());
168.101 - }
168.102 -}
169.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
169.2 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrwsrMojo.java Fri May 17 10:27:41 2013 +0200
169.3 @@ -0,0 +1,108 @@
169.4 +/**
169.5 + * Back 2 Browser Bytecode Translator
169.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
169.7 + *
169.8 + * This program is free software: you can redistribute it and/or modify
169.9 + * it under the terms of the GNU General Public License as published by
169.10 + * the Free Software Foundation, version 2 of the License.
169.11 + *
169.12 + * This program is distributed in the hope that it will be useful,
169.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
169.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
169.15 + * GNU General Public License for more details.
169.16 + *
169.17 + * You should have received a copy of the GNU General Public License
169.18 + * along with this program. Look for COPYING file in the top folder.
169.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
169.20 + */
169.21 +package org.apidesign.bck2brwsr.mojo;
169.22 +
169.23 +import java.io.Closeable;
169.24 +import org.apache.maven.plugin.AbstractMojo;
169.25 +
169.26 +import java.io.File;
169.27 +import java.io.IOException;
169.28 +import java.net.MalformedURLException;
169.29 +import java.net.URL;
169.30 +import java.net.URLClassLoader;
169.31 +import java.util.ArrayList;
169.32 +import java.util.Collection;
169.33 +import java.util.List;
169.34 +import org.apache.maven.artifact.Artifact;
169.35 +import org.apache.maven.plugin.MojoExecutionException;
169.36 +import org.apache.maven.plugins.annotations.LifecyclePhase;
169.37 +import org.apache.maven.plugins.annotations.Mojo;
169.38 +import org.apache.maven.plugins.annotations.Parameter;
169.39 +import org.apache.maven.project.MavenProject;
169.40 +import org.apidesign.bck2brwsr.launcher.Launcher;
169.41 +
169.42 +/** Executes given HTML page in a browser. */
169.43 +@Mojo(name="brwsr", defaultPhase=LifecyclePhase.NONE)
169.44 +public class BrwsrMojo extends AbstractMojo {
169.45 + public BrwsrMojo() {
169.46 + }
169.47 +
169.48 + /** The identification of a launcher to use. Known values <code>fxbrwsr</code>,
169.49 + * <code>bck2brwsr</code>, or
169.50 + * name of an external process to execute.
169.51 + */
169.52 + @Parameter
169.53 + private String launcher;
169.54 +
169.55 +
169.56 + /** Resource to show as initial page */
169.57 + @Parameter
169.58 + private String startpage;
169.59 +
169.60 + @Parameter(defaultValue="${project}")
169.61 + private MavenProject prj;
169.62 +
169.63 + /** Root of the class files */
169.64 + @Parameter(defaultValue="${project.build.directory}/classes")
169.65 + private File classes;
169.66 +
169.67 + /** Root of all pages, and files, etc. */
169.68 + @Parameter
169.69 + private File directory;
169.70 +
169.71 + @Override
169.72 + public void execute() throws MojoExecutionException {
169.73 + if (startpage == null) {
169.74 + throw new MojoExecutionException("You have to provide a start page");
169.75 + }
169.76 +
169.77 + try {
169.78 + Closeable httpServer;
169.79 + if (directory != null) {
169.80 + httpServer = Launcher.showDir(directory, startpage);
169.81 + } else {
169.82 + URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
169.83 + try {
169.84 + httpServer = Launcher.showURL(launcher, url, startpage());
169.85 + } catch (Exception ex) {
169.86 + throw new MojoExecutionException("Can't open " + startpage(), ex);
169.87 + }
169.88 + }
169.89 + System.in.read();
169.90 + httpServer.close();
169.91 + } catch (IOException ex) {
169.92 + throw new MojoExecutionException("Can't show the browser", ex);
169.93 + }
169.94 + }
169.95 +
169.96 + private String startpage() {
169.97 + return startpage;
169.98 + }
169.99 +
169.100 + private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
169.101 + List<URL> arr = new ArrayList<URL>();
169.102 + arr.add(root.toURI().toURL());
169.103 + for (Artifact a : deps) {
169.104 + final File f = a.getFile();
169.105 + if (f != null) {
169.106 + arr.add(f.toURI().toURL());
169.107 + }
169.108 + }
169.109 + return new URLClassLoader(arr.toArray(new URL[0]), BrwsrMojo.class.getClassLoader());
169.110 + }
169.111 +}
170.1 --- a/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java Tue Mar 05 07:57:16 2013 +0100
170.2 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java Fri May 17 10:27:41 2013 +0200
170.3 @@ -35,6 +35,7 @@
170.4 import org.apache.maven.plugins.annotations.Parameter;
170.5 import org.apache.maven.project.MavenProject;
170.6 import org.apidesign.vm4brwsr.Bck2Brwsr;
170.7 +import org.apidesign.vm4brwsr.ObfuscationLevel;
170.8
170.9 /** Compiles classes into JavaScript. */
170.10 @Mojo(name="j2js", defaultPhase=LifecyclePhase.PROCESS_CLASSES)
170.11 @@ -44,15 +45,27 @@
170.12 /** Root of the class files */
170.13 @Parameter(defaultValue="${project.build.directory}/classes")
170.14 private File classes;
170.15 - /** File to generate. Defaults bootjava.js in the first non-empty
170.16 - package under the classes directory */
170.17 + /** JavaScript file to generate */
170.18 @Parameter
170.19 private File javascript;
170.20 +
170.21 + /** Additional classes that should be pre-compiled into the javascript
170.22 + * file. By default compiles all classes found under <code>classes</code>
170.23 + * directory and their transitive closure.
170.24 + */
170.25 + @Parameter
170.26 + private List<String> compileclasses;
170.27
170.28 @Parameter(defaultValue="${project}")
170.29 private MavenProject prj;
170.30 -
170.31 -
170.32 +
170.33 + /**
170.34 + * The obfuscation level for the generated JavaScript file.
170.35 + *
170.36 + * @since 0.5
170.37 + */
170.38 + @Parameter(defaultValue="NONE")
170.39 + private ObfuscationLevel obfuscation;
170.40
170.41 @Override
170.42 public void execute() throws MojoExecutionException {
170.43 @@ -60,13 +73,14 @@
170.44 throw new MojoExecutionException("Can't find " + classes);
170.45 }
170.46
170.47 - if (javascript == null) {
170.48 - javascript = new File(findNonEmptyFolder(classes), "bootjava.js");
170.49 - }
170.50 -
170.51 List<String> arr = new ArrayList<String>();
170.52 long newest = collectAllClasses("", classes, arr);
170.53
170.54 + if (compileclasses != null) {
170.55 + arr.retainAll(compileclasses);
170.56 + arr.addAll(compileclasses);
170.57 + }
170.58 +
170.59 if (javascript.lastModified() > newest) {
170.60 return;
170.61 }
170.62 @@ -74,24 +88,17 @@
170.63 try {
170.64 URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
170.65 FileWriter w = new FileWriter(javascript);
170.66 - Bck2Brwsr.generate(w, url, arr.toArray(new String[0]));
170.67 + Bck2Brwsr.newCompiler().
170.68 + obfuscation(obfuscation).
170.69 + resources(url).
170.70 + addRootClasses(arr.toArray(new String[0])).
170.71 + generate(w);
170.72 w.close();
170.73 } catch (IOException ex) {
170.74 throw new MojoExecutionException("Can't compile", ex);
170.75 }
170.76 }
170.77
170.78 - private static File findNonEmptyFolder(File dir) throws MojoExecutionException {
170.79 - if (!dir.isDirectory()) {
170.80 - throw new MojoExecutionException("Not a directory " + dir);
170.81 - }
170.82 - File[] arr = dir.listFiles();
170.83 - if (arr.length == 1 && arr[0].isDirectory()) {
170.84 - return findNonEmptyFolder(arr[0]);
170.85 - }
170.86 - return dir;
170.87 - }
170.88 -
170.89 private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
170.90 File[] files = toCheck.listFiles();
170.91 if (files != null) {
170.92 @@ -104,7 +111,8 @@
170.93 }
170.94 return newest;
170.95 } else if (toCheck.getName().endsWith(".class")) {
170.96 - arr.add(prefix.substring(0, prefix.length() - 7));
170.97 + final String cls = prefix.substring(0, prefix.length() - 7);
170.98 + arr.add(cls);
170.99 return toCheck.lastModified();
170.100 } else {
170.101 return 0L;
170.102 @@ -115,7 +123,9 @@
170.103 List<URL> arr = new ArrayList<URL>();
170.104 arr.add(root.toURI().toURL());
170.105 for (Artifact a : deps) {
170.106 - arr.add(a.getFile().toURI().toURL());
170.107 + if (a.getFile() != null) {
170.108 + arr.add(a.getFile().toURI().toURL());
170.109 + }
170.110 }
170.111 return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
170.112 }
171.1 --- a/rt/mojo/src/main/resources/META-INF/maven/archetype-metadata.xml Tue Mar 05 07:57:16 2013 +0100
171.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
171.3 @@ -1,55 +0,0 @@
171.4 -<?xml version="1.0" encoding="UTF-8"?>
171.5 -<!--
171.6 -
171.7 - Back 2 Browser Bytecode Translator
171.8 - Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
171.9 -
171.10 - This program is free software: you can redistribute it and/or modify
171.11 - it under the terms of the GNU General Public License as published by
171.12 - the Free Software Foundation, version 2 of the License.
171.13 -
171.14 - This program is distributed in the hope that it will be useful,
171.15 - but WITHOUT ANY WARRANTY; without even the implied warranty of
171.16 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
171.17 - GNU General Public License for more details.
171.18 -
171.19 - You should have received a copy of the GNU General Public License
171.20 - along with this program. Look for COPYING file in the top folder.
171.21 - If not, see http://opensource.org/licenses/GPL-2.0.
171.22 -
171.23 --->
171.24 -<archetype-descriptor name="bck2brwsr">
171.25 - <fileSets>
171.26 - <fileSet filtered="true" packaged="true">
171.27 - <directory>src/main/java</directory>
171.28 - <includes>
171.29 - <include>**/App.java</include>
171.30 - </includes>
171.31 - </fileSet>
171.32 - <fileSet filtered="true" packaged="true">
171.33 - <directory>src/main/resources</directory>
171.34 - <includes>
171.35 - <include>**/*.xhtml</include>
171.36 - <include>**/*.html</include>
171.37 - </includes>
171.38 - </fileSet>
171.39 - <fileSet filtered="true" packaged="true">
171.40 - <directory>src/test/java</directory>
171.41 - <includes>
171.42 - <include>**/*Test.java</include>
171.43 - </includes>
171.44 - </fileSet>
171.45 - <fileSet filtered="false" packaged="false">
171.46 - <directory></directory>
171.47 - <includes>
171.48 - <include>nbactions.xml</include>
171.49 - </includes>
171.50 - </fileSet>
171.51 - <fileSet filtered="true" packaged="false">
171.52 - <directory></directory>
171.53 - <includes>
171.54 - <include>bck2brwsr-assembly.xml</include>
171.55 - </includes>
171.56 - </fileSet>
171.57 - </fileSets>
171.58 -</archetype-descriptor>
171.59 \ No newline at end of file
172.1 --- a/rt/mojo/src/main/resources/archetype-resources/bck2brwsr-assembly.xml Tue Mar 05 07:57:16 2013 +0100
172.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
172.3 @@ -1,61 +0,0 @@
172.4 -<?xml version="1.0"?>
172.5 -<!--
172.6 -
172.7 - Back 2 Browser Bytecode Translator
172.8 - Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
172.9 -
172.10 - This program is free software: you can redistribute it and/or modify
172.11 - it under the terms of the GNU General Public License as published by
172.12 - the Free Software Foundation, version 2 of the License.
172.13 -
172.14 - This program is distributed in the hope that it will be useful,
172.15 - but WITHOUT ANY WARRANTY; without even the implied warranty of
172.16 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
172.17 - GNU General Public License for more details.
172.18 -
172.19 - You should have received a copy of the GNU General Public License
172.20 - along with this program. Look for COPYING file in the top folder.
172.21 - If not, see http://opensource.org/licenses/GPL-2.0.
172.22 -
172.23 --->
172.24 -<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
172.25 - xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
172.26 -
172.27 - <id>bck2brwsr</id>
172.28 - <formats>
172.29 - <format>zip</format>
172.30 - </formats>
172.31 - <baseDirectory>public_html</baseDirectory>
172.32 - <dependencySets>
172.33 - <dependencySet>
172.34 - <useProjectArtifact>false</useProjectArtifact>
172.35 - <scope>runtime</scope>
172.36 - <outputDirectory>lib</outputDirectory>
172.37 - <includes>
172.38 - <include>*:jar</include>
172.39 - <include>*:rt</include>
172.40 - </includes>
172.41 - </dependencySet>
172.42 - <dependencySet>
172.43 - <useProjectArtifact>false</useProjectArtifact>
172.44 - <scope>provided</scope>
172.45 - <includes>
172.46 - <include>*:js</include>
172.47 - </includes>
172.48 - <unpack>true</unpack>
172.49 - <outputDirectory>/</outputDirectory>
172.50 - </dependencySet>
172.51 - </dependencySets>
172.52 - <files>
172.53 - <file>
172.54 - <source>${project.build.directory}/${project.build.finalName}.jar</source>
172.55 - <outputDirectory>/</outputDirectory>
172.56 - </file>
172.57 - <file>
172.58 - <source>${project.build.directory}/classes/${package.replace('.','/')}/index.html</source>
172.59 - <outputDirectory>/</outputDirectory>
172.60 - <destName>index.html</destName>
172.61 - </file>
172.62 - </files>
172.63 -
172.64 -</assembly>
172.65 \ No newline at end of file
173.1 --- a/rt/mojo/src/main/resources/archetype-resources/nbactions.xml Tue Mar 05 07:57:16 2013 +0100
173.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
173.3 @@ -1,10 +0,0 @@
173.4 -<?xml version="1.0" encoding="UTF-8"?>
173.5 -<actions>
173.6 - <action>
173.7 - <actionName>run</actionName>
173.8 - <goals>
173.9 - <goal>process-classes</goal>
173.10 - <goal>org.apidesign.bck2brwsr:mojo:0.3-SNAPSHOT:brwsr</goal>
173.11 - </goals>
173.12 - </action>
173.13 -</actions>
174.1 --- a/rt/mojo/src/main/resources/archetype-resources/pom.xml Tue Mar 05 07:57:16 2013 +0100
174.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
174.3 @@ -1,135 +0,0 @@
174.4 -<?xml version="1.0"?>
174.5 -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
174.6 - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
174.7 - <modelVersion>4.0.0</modelVersion>
174.8 -
174.9 - <groupId>${groupId}</groupId>
174.10 - <artifactId>${artifactId}</artifactId>
174.11 - <version>${version}</version>
174.12 - <packaging>jar</packaging>
174.13 -
174.14 - <name>${artifactId}</name>
174.15 -
174.16 - <repositories>
174.17 - <repository>
174.18 - <id>java.net</id>
174.19 - <name>Java.net</name>
174.20 - <url>https://maven.java.net/content/repositories/snapshots/</url>
174.21 - <snapshots>
174.22 - <enabled>true</enabled>
174.23 - </snapshots>
174.24 - </repository>
174.25 - <repository>
174.26 - <id>netbeans</id>
174.27 - <name>NetBeans</name>
174.28 - <url>http://bits.netbeans.org/maven2/</url>
174.29 - </repository>
174.30 - </repositories>
174.31 - <pluginRepositories>
174.32 - <pluginRepository>
174.33 - <id>java.net</id>
174.34 - <name>Local Maven repository of releases</name>
174.35 - <url>https://maven.java.net/content/repositories/snapshots/</url>
174.36 - <snapshots>
174.37 - <enabled>true</enabled>
174.38 - </snapshots>
174.39 - </pluginRepository>
174.40 - </pluginRepositories>
174.41 -
174.42 - <properties>
174.43 - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
174.44 - </properties>
174.45 - <build>
174.46 - <plugins>
174.47 - <plugin>
174.48 - <groupId>org.apidesign.bck2brwsr</groupId>
174.49 - <artifactId>mojo</artifactId>
174.50 - <version>0.3-SNAPSHOT</version>
174.51 - <executions>
174.52 - <execution>
174.53 - <goals>
174.54 - <goal>brwsr</goal>
174.55 - </goals>
174.56 - </execution>
174.57 - </executions>
174.58 - <configuration>
174.59 - <startpage>${package.replace('.','/')}/index.html</startpage>
174.60 - </configuration>
174.61 - </plugin>
174.62 - <plugin>
174.63 - <groupId>org.apache.maven.plugins</groupId>
174.64 - <artifactId>maven-compiler-plugin</artifactId>
174.65 - <version>2.3.2</version>
174.66 - <configuration>
174.67 - <source>1.7</source>
174.68 - <target>1.7</target>
174.69 - </configuration>
174.70 - </plugin>
174.71 - <plugin>
174.72 - <groupId>org.apache.maven.plugins</groupId>
174.73 - <artifactId>maven-jar-plugin</artifactId>
174.74 - <version>2.4</version>
174.75 - <configuration>
174.76 - <archive>
174.77 - <manifest>
174.78 - <addClasspath>true</addClasspath>
174.79 - <classpathPrefix>lib/</classpathPrefix>
174.80 - </manifest>
174.81 - </archive>
174.82 - </configuration>
174.83 - </plugin>
174.84 - <plugin>
174.85 - <artifactId>maven-assembly-plugin</artifactId>
174.86 - <version>2.4</version>
174.87 - <executions>
174.88 - <execution>
174.89 - <id>distro-assembly</id>
174.90 - <phase>package</phase>
174.91 - <goals>
174.92 - <goal>single</goal>
174.93 - </goals>
174.94 - <configuration>
174.95 - <descriptors>
174.96 - <descriptor>bck2brwsr-assembly.xml</descriptor>
174.97 - </descriptors>
174.98 - </configuration>
174.99 - </execution>
174.100 - </executions>
174.101 - </plugin>
174.102 - </plugins>
174.103 - </build>
174.104 -
174.105 - <dependencies>
174.106 - <dependency>
174.107 - <groupId>org.apidesign.bck2brwsr</groupId>
174.108 - <artifactId>emul</artifactId>
174.109 - <version>0.3-SNAPSHOT</version>
174.110 - <classifier>rt</classifier>
174.111 - </dependency>
174.112 - <dependency>
174.113 - <groupId>org.apidesign.bck2brwsr</groupId>
174.114 - <artifactId>javaquery.api</artifactId>
174.115 - <version>0.3-SNAPSHOT</version>
174.116 - </dependency>
174.117 - <dependency>
174.118 - <groupId>org.testng</groupId>
174.119 - <artifactId>testng</artifactId>
174.120 - <version>6.5.2</version>
174.121 - <scope>test</scope>
174.122 - </dependency>
174.123 - <dependency>
174.124 - <groupId>org.apidesign.bck2brwsr</groupId>
174.125 - <artifactId>vm4brwsr</artifactId>
174.126 - <classifier>js</classifier>
174.127 - <type>zip</type>
174.128 - <version>0.3-SNAPSHOT</version>
174.129 - <scope>provided</scope>
174.130 - </dependency>
174.131 - <dependency>
174.132 - <groupId>org.apidesign.bck2brwsr</groupId>
174.133 - <artifactId>vmtest</artifactId>
174.134 - <version>0.3-SNAPSHOT</version>
174.135 - <scope>test</scope>
174.136 - </dependency>
174.137 - </dependencies>
174.138 -</project>
175.1 --- a/rt/mojo/src/main/resources/archetype-resources/src/main/java/App.java Tue Mar 05 07:57:16 2013 +0100
175.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
175.3 @@ -1,34 +0,0 @@
175.4 -package ${package};
175.5 -
175.6 -import org.apidesign.bck2brwsr.htmlpage.api.*;
175.7 -import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
175.8 -import org.apidesign.bck2brwsr.htmlpage.api.Page;
175.9 -import org.apidesign.bck2brwsr.htmlpage.api.Property;
175.10 -import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
175.11 -
175.12 -/** Edit the index.xhtml file. Use 'id' to name certain HTML elements.
175.13 - * Use this class to define behavior of the elements.
175.14 - */
175.15 -@Page(xhtml="index.html", className="Index", properties={
175.16 - @Property(name="name", type=String.class)
175.17 -})
175.18 -public class App {
175.19 - static {
175.20 - Index model = new Index();
175.21 - model.setName("World");
175.22 - model.applyBindings();
175.23 - }
175.24 -
175.25 - @On(event = CLICK, id="hello")
175.26 - static void hello(Index m) {
175.27 - GraphicsContext g = m.CANVAS.getContext();
175.28 - g.clearRect(0, 0, 1000, 1000);
175.29 - g.setFont("italic 40px Calibri");
175.30 - g.fillText(m.getHelloMessage(), 10, 40);
175.31 - }
175.32 -
175.33 - @ComputedProperty
175.34 - static String helloMessage(String name) {
175.35 - return "Hello " + name + "!";
175.36 - }
175.37 -}
176.1 --- a/rt/mojo/src/main/resources/archetype-resources/src/main/resources/index.html Tue Mar 05 07:57:16 2013 +0100
176.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
176.3 @@ -1,22 +0,0 @@
176.4 -<?xml version="1.0" encoding="UTF-8"?>
176.5 -<!DOCTYPE html>
176.6 -<html xmlns="http://www.w3.org/1999/xhtml">
176.7 - <head>
176.8 - <title>Bck2Brwsr's Hello World</title>
176.9 - </head>
176.10 - <body>
176.11 - <h1 data-bind="text: helloMessage">Loading Bck2Brwsr's Hello World...</h1>
176.12 - Your name: <input id='input' data-bind="value: name, valueUpdate: 'afterkeydown'"></input>
176.13 - <button id="hello">Say Hello!</button>
176.14 - <p>
176.15 - <canvas id="canvas" width="300" height="50">
176.16 - </canvas>
176.17 - </p>
176.18 -
176.19 - <script src="bck2brwsr.js"></script>
176.20 - <script type="text/javascript">
176.21 - var vm = bck2brwsr('${artifactId}-${version}.jar');
176.22 - vm.loadClass('${package}.App');
176.23 - </script>
176.24 - </body>
176.25 -</html>
177.1 --- a/rt/mojo/src/main/resources/archetype-resources/src/test/java/AppTest.java Tue Mar 05 07:57:16 2013 +0100
177.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
177.3 @@ -1,26 +0,0 @@
177.4 -package ${package};
177.5 -
177.6 -import static org.testng.Assert.*;
177.7 -import org.testng.annotations.BeforeMethod;
177.8 -import org.testng.annotations.Test;
177.9 -
177.10 -/** Demonstrating POJO testing of HTML page model. Runs in good old HotSpot
177.11 - * as it does not reference any HTML elements or browser functionality. Just
177.12 - * operates on the page model.
177.13 - *
177.14 - * @author Jaroslav Tulach <jtulach@netbeans.org>
177.15 - */
177.16 -public class AppTest {
177.17 - private Index model;
177.18 -
177.19 -
177.20 - @BeforeMethod
177.21 - public void initModel() {
177.22 - model = new Index().applyBindings();
177.23 - }
177.24 -
177.25 - @Test public void testHelloMessage() {
177.26 - model.setName("Joe");
177.27 - assertEquals(model.getHelloMessage(), "Hello Joe!", "Cleared after pressing +");
177.28 - }
177.29 -}
178.1 --- a/rt/mojo/src/main/resources/archetype-resources/src/test/java/InconsistencyTest.java Tue Mar 05 07:57:16 2013 +0100
178.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
178.3 @@ -1,40 +0,0 @@
178.4 -package ${package};
178.5 -
178.6 -import org.apidesign.bck2brwsr.vmtest.Compare;
178.7 -import org.apidesign.bck2brwsr.vmtest.VMTest;
178.8 -import org.testng.annotations.Factory;
178.9 -
178.10 -/** Bck2brwsr cares about compatibility with real Java. Whatever API is
178.11 - * supported by bck2brwsr, it needs to behave the same way as when running
178.12 - * in HotSpot VM.
178.13 - * <p>
178.14 - * There can be bugs, however. To help us fix them, we kindly ask you to
178.15 - * write an "inconsistency" test. A test that compares behavior of the API
178.16 - * between real VM and bck2brwsr VM. This class is skeleton of such test.
178.17 - *
178.18 - * @author Jaroslav Tulach <jtulach@netbeans.org>
178.19 - */
178.20 -public class InconsistencyTest {
178.21 - /** A method to demonstrate inconsistency between bck2brwsr and HotSpot.
178.22 - * Make calls to an API that behaves strangely, return some result at
178.23 - * the end. No need to use any <code>assert</code>.
178.24 - *
178.25 - * @return value to compare between HotSpot and bck2brwsr
178.26 - */
178.27 - @Compare
178.28 - public int checkStringHashCode() throws Exception {
178.29 - return "Is string hashCode the same?".hashCode();
178.30 - }
178.31 -
178.32 - /** Factory method that creates a three tests for each method annotated with
178.33 - * {@link org.apidesign.bck2brwsr.vmtest.Compare}. One executes the code in
178.34 - * HotSpot, one in Rhino and the last one compares the results.
178.35 - *
178.36 - * @see org.apidesign.bck2brwsr.vmtest.VMTest
178.37 - */
178.38 - @Factory
178.39 - public static Object[] create() {
178.40 - return VMTest.create(InconsistencyTest.class);
178.41 - }
178.42 -
178.43 -}
179.1 --- a/rt/mojo/src/main/resources/archetype-resources/src/test/java/IntegrationTest.java Tue Mar 05 07:57:16 2013 +0100
179.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
179.3 @@ -1,46 +0,0 @@
179.4 -package ${package};
179.5 -
179.6 -import org.apidesign.bck2brwsr.htmlpage.api.OnEvent;
179.7 -import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
179.8 -import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
179.9 -import org.apidesign.bck2brwsr.vmtest.VMTest;
179.10 -import org.testng.annotations.Factory;
179.11 -
179.12 -/** Sometimes it is useful to run tests inside of the real browser.
179.13 - * To do that just annotate your method with {@link org.apidesign.bck2brwsr.vmtest.BrwsrTest}
179.14 - * and that is it. If your code references elements on the HTML page,
179.15 - * you can pass in an {@link org.apidesign.bck2brwsr.vmtest.HtmlFragment} which
179.16 - * will be made available on the page before your test starts.
179.17 - *
179.18 - * @author Jaroslav Tulach <jtulach@netbeans.org>
179.19 - */
179.20 -public class IntegrationTest {
179.21 -
179.22 - /** Write to testing code here. Use <code>assert</code> (but not TestNG's
179.23 - * Assert, as TestNG is not compiled with target 1.6 yet).
179.24 - */
179.25 - @HtmlFragment(
179.26 - "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
179.27 - "Your name: <input id='input' data-bind=\"value: name, valueUpdate: 'afterkeydown'\"></input>\n" +
179.28 - "<button id=\"hello\">Say Hello!</button>\n" +
179.29 - "<p>\n" +
179.30 - " <canvas id=\"canvas\" width=\"300\" height=\"50\"></canvas>\n" +
179.31 - "</p>\n"
179.32 - )
179.33 - @BrwsrTest
179.34 - public void modifyValueAssertChangeInModel() {
179.35 - Index m = new Index();
179.36 - m.setName("Joe Hacker");
179.37 - m.applyBindings();
179.38 - assert "Joe Hacker".equals(m.INPUT.getValue()) : "Value is really Joe Hacker: " + m.INPUT.getValue();
179.39 - m.INPUT.setValue("Happy Joe");
179.40 - m.triggerEvent(m.INPUT, OnEvent.CHANGE);
179.41 - assert "Happy Joe".equals(m.getName()) : "Name property updated to Happy Joe: " + m.getName();
179.42 - }
179.43 -
179.44 - @Factory
179.45 - public static Object[] create() {
179.46 - return VMTest.create(IntegrationTest.class);
179.47 - }
179.48 -
179.49 -}
180.1 --- a/rt/pom.xml Tue Mar 05 07:57:16 2013 +0100
180.2 +++ b/rt/pom.xml Fri May 17 10:27:41 2013 +0200
180.3 @@ -3,21 +3,20 @@
180.4 <modelVersion>4.0.0</modelVersion>
180.5 <groupId>org.apidesign.bck2brwsr</groupId>
180.6 <artifactId>rt</artifactId>
180.7 - <version>0.4-SNAPSHOT</version>
180.8 + <version>0.8-SNAPSHOT</version>
180.9 <packaging>pom</packaging>
180.10 <name>Bck2Brwsr Runtime</name>
180.11 <parent>
180.12 <groupId>org.apidesign</groupId>
180.13 <artifactId>bck2brwsr</artifactId>
180.14 - <version>0.4-SNAPSHOT</version>
180.15 + <version>0.8-SNAPSHOT</version>
180.16 </parent>
180.17 <modules>
180.18 <module>core</module>
180.19 <module>emul</module>
180.20 - <module>javap</module>
180.21 - <module>launcher</module>
180.22 + <module>archetype</module>
180.23 <module>mojo</module>
180.24 <module>vm</module>
180.25 <module>vmtest</module>
180.26 </modules>
180.27 -</project>
180.28 \ No newline at end of file
180.29 +</project>
181.1 --- a/rt/vm/pom.xml Tue Mar 05 07:57:16 2013 +0100
181.2 +++ b/rt/vm/pom.xml Fri May 17 10:27:41 2013 +0200
181.3 @@ -3,12 +3,12 @@
181.4 <parent>
181.5 <groupId>org.apidesign.bck2brwsr</groupId>
181.6 <artifactId>rt</artifactId>
181.7 - <version>0.4-SNAPSHOT</version>
181.8 + <version>0.8-SNAPSHOT</version>
181.9 </parent>
181.10
181.11 <groupId>org.apidesign.bck2brwsr</groupId>
181.12 <artifactId>vm4brwsr</artifactId>
181.13 - <version>0.4-SNAPSHOT</version>
181.14 + <version>0.8-SNAPSHOT</version>
181.15 <packaging>jar</packaging>
181.16
181.17 <name>Virtual Machine for Browser</name>
181.18 @@ -43,6 +43,7 @@
181.19 <scm>
181.20 <connection>scm:hg:http://source.apidesign.org/hg/bck2brwsr</connection>
181.21 <url>http://source.apidesign.org/hg/bck2brwsr</url>
181.22 + <tag>HEAD</tag>
181.23 </scm>
181.24 <build>
181.25 <plugins>
181.26 @@ -82,18 +83,24 @@
181.27 <execution>
181.28 <id>generate-js</id>
181.29 <phase>process-classes</phase>
181.30 + <configuration>
181.31 + <executable>java</executable>
181.32 + <arguments>
181.33 + <argument>-Dskip.if.exists=true</argument>
181.34 + <argument>-cp</argument>
181.35 + <classpath />
181.36 + <argument>org.apidesign.vm4brwsr.Main</argument>
181.37 + <argument>--obfuscatelevel</argument>
181.38 + <argument>MINIMAL</argument>
181.39 + <argument>${project.build.directory}/bck2brwsr.js</argument>
181.40 + <argument>org/apidesign/vm4brwsr/Bck2Brwsr</argument>
181.41 + </arguments>
181.42 + </configuration>
181.43 <goals>
181.44 - <goal>java</goal>
181.45 + <goal>exec</goal>
181.46 </goals>
181.47 </execution>
181.48 </executions>
181.49 - <configuration>
181.50 - <mainClass>org.apidesign.vm4brwsr.Main</mainClass>
181.51 - <arguments>
181.52 - <argument>${project.build.directory}/bck2brwsr.js</argument>
181.53 - <argument>org/apidesign/vm4brwsr/Bck2Brwsr</argument>
181.54 - </arguments>
181.55 - </configuration>
181.56 </plugin>
181.57 <plugin>
181.58 <artifactId>maven-assembly-plugin</artifactId>
181.59 @@ -140,10 +147,10 @@
181.60 <scope>compile</scope>
181.61 </dependency>
181.62 <dependency>
181.63 - <groupId>${project.groupId}</groupId>
181.64 - <artifactId>javap</artifactId>
181.65 - <version>${project.version}</version>
181.66 + <groupId>com.google.javascript</groupId>
181.67 + <artifactId>closure-compiler</artifactId>
181.68 + <version>r2388</version>
181.69 <scope>compile</scope>
181.70 - </dependency>
181.71 + </dependency>
181.72 </dependencies>
181.73 </project>
182.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Tue Mar 05 07:57:16 2013 +0100
182.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java Fri May 17 10:27:41 2013 +0200
182.3 @@ -54,46 +54,127 @@
182.4 * @author Jaroslav Tulach <jtulach@netbeans.org>
182.5 */
182.6 public final class Bck2Brwsr {
182.7 - private Bck2Brwsr() {
182.8 + private final ObfuscationLevel level;
182.9 + private final StringArray classes;
182.10 + private final Resources res;
182.11 +
182.12 + private Bck2Brwsr(ObfuscationLevel level, StringArray classes, Resources resources) {
182.13 + this.level = level;
182.14 + this.classes = classes;
182.15 + this.res = resources;
182.16 }
182.17
182.18 - /** Generates virtual machine from bytes served by a <code>resources</code>
182.19 + /** Helper method to generate virtual machine from bytes served by a <code>resources</code>
182.20 * provider.
182.21 - *
182.22 + *
182.23 * @param out the output to write the generated JavaScript to
182.24 * @param resources provider of class files to use
182.25 * @param classes additional classes to include in the generated script
182.26 * @throws IOException I/O exception can be thrown when something goes wrong
182.27 */
182.28 public static void generate(Appendable out, Resources resources, String... classes) throws IOException {
182.29 - StringArray arr = StringArray.asList(classes);
182.30 - arr.add(VM.class.getName().replace('.', '/'));
182.31 - VM.compile(resources, out, arr);
182.32 + newCompiler().resources(resources).addRootClasses(classes).generate(out);
182.33 }
182.34 -
182.35 - /** Generates virtual machine from bytes served by a class loader.
182.36 - *
182.37 +
182.38 + /** Helper method to generate virtual machine from bytes served by a class loader.
182.39 + *
182.40 * @param out the output to write the generated JavaScript to
182.41 * @param loader class loader to load needed classes from
182.42 * @param classes additional classes to include in the generated script
182.43 * @throws IOException I/O exception can be thrown when something goes wrong
182.44 */
182.45 - public static void generate(Appendable out, final ClassLoader loader, String... classes) throws IOException {
182.46 - class R implements Resources {
182.47 - @Override
182.48 - public InputStream get(String name) throws IOException {
182.49 - Enumeration<URL> en = loader.getResources(name);
182.50 - URL u = null;
182.51 - while (en.hasMoreElements()) {
182.52 - u = en.nextElement();
182.53 - }
182.54 - if (u == null) {
182.55 - throw new IOException("Can't find " + name);
182.56 - }
182.57 - return u.openStream();
182.58 + public static void generate(Appendable out, ClassLoader loader, String... classes) throws IOException {
182.59 + newCompiler().resources(loader).addRootClasses(classes).generate(out);
182.60 + }
182.61 +
182.62 + /** Creates new instance of Bck2Brwsr compiler which is ready to generate
182.63 + * empty Bck2Brwsr virtual machine. The instance can be further
182.64 + * configured by calling chain of methods. For example:
182.65 + * <pre>
182.66 + * {@link #createCompiler()}.{@link #resources(org.apidesign.vm4brwsr.Bck2Brwsr.Resources) resources(loader)}.{@link #addRootClasses(java.lang.String[]) addRootClasses("your/Clazz")}.{@link #generate(java.lang.Appendable) generate(out)};
182.67 + * </pre>
182.68 + *
182.69 + * @return new instance of the Bck2Brwsr compiler
182.70 + * @since 0.5
182.71 + */
182.72 + public static Bck2Brwsr newCompiler() {
182.73 + StringArray arr = StringArray.asList(VM.class.getName().replace('.', '/'));
182.74 + return new Bck2Brwsr(ObfuscationLevel.NONE, arr, null);
182.75 + }
182.76 +
182.77 + /** Creates new instance of the Bck2Brwsr compiler which inherits
182.78 + * all values from <code>this</code> instance and adds additional classes
182.79 + * to the list of those that should be compiled by the {@link #generate(java.lang.Appendable)}
182.80 + * method.
182.81 + *
182.82 + * @param classes the classes to add to the compilation
182.83 + * @return new instance of the compiler
182.84 + */
182.85 + public Bck2Brwsr addRootClasses(String... classes) {
182.86 + if (classes.length == 0) {
182.87 + return this;
182.88 + } else {
182.89 + return new Bck2Brwsr(level, this.classes.addAndNew(classes), res);
182.90 + }
182.91 + }
182.92 +
182.93 + /** Changes the obfuscation level for the compiler by creating new instance
182.94 + * which inherits all values from <code>this</code> and adjust the level
182.95 + * of obfuscation.
182.96 + *
182.97 + * @param level the new level of obfuscation
182.98 + * @return new instance of the compiler with changed level of obfuscation
182.99 + * @since 0.5
182.100 + */
182.101 + public Bck2Brwsr obfuscation(ObfuscationLevel level) {
182.102 + return new Bck2Brwsr(level, classes, res);
182.103 + }
182.104 +
182.105 + /** A way to change the provider of additional resources (classes) for the
182.106 + * compiler.
182.107 + *
182.108 + * @param res the implementation of resources provider
182.109 + * @return new instance of the compiler with all values remaining the same, just
182.110 + * with different resources provider
182.111 + * @since 0.5
182.112 + */
182.113 + public Bck2Brwsr resources(Resources res) {
182.114 + return new Bck2Brwsr(level, classes, res);
182.115 + }
182.116 +
182.117 + /** A way to change the provider of additional resources (classes) for the
182.118 + * compiler by specifying classloader to use for loading them.
182.119 + *
182.120 + * @param loader class loader to load the resources from
182.121 + * @return new instance of the compiler with all values being the same, just
182.122 + * different resources provider
182.123 + * @since 0.5
182.124 + */
182.125 + public Bck2Brwsr resources(final ClassLoader loader) {
182.126 + return resources(new LdrRsrcs(loader));
182.127 + }
182.128 +
182.129 + /** Generates virtual machine based on previous configuration of the
182.130 + * compiler.
182.131 + *
182.132 + * @param out the output to write the generated JavaScript to
182.133 + * @since 0.5
182.134 + */
182.135 + public void generate(Appendable out) throws IOException {
182.136 + Resources r = res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader());
182.137 + if (level != ObfuscationLevel.NONE) {
182.138 + try {
182.139 + ClosureWrapper.produceTo(out, level, r, classes);
182.140 + return;
182.141 + } catch (IOException ex) {
182.142 + throw ex;
182.143 + } catch (Throwable ex) {
182.144 + out.append("/* Failed to obfuscate: " + ex.getMessage()
182.145 + + " */\n");
182.146 }
182.147 }
182.148 - generate(out, new R(), classes);
182.149 +
182.150 + VM.compile(r, out, classes);
182.151 }
182.152
182.153 /** Provider of resources (classes and other files). The
183.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
183.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeParser.java Fri May 17 10:27:41 2013 +0200
183.3 @@ -0,0 +1,3461 @@
183.4 +/*
183.5 + * Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved.
183.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
183.7 + *
183.8 + * This code is free software; you can redistribute it and/or modify it
183.9 + * under the terms of the GNU General Public License version 2 only, as
183.10 + * published by the Free Software Foundation. Oracle designates this
183.11 + * particular file as subject to the "Classpath" exception as provided
183.12 + * by Oracle in the LICENSE file that accompanied this code.
183.13 + *
183.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
183.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
183.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
183.17 + * version 2 for more details (a copy is included in the LICENSE file that
183.18 + * accompanied this code).
183.19 + *
183.20 + * You should have received a copy of the GNU General Public License version
183.21 + * 2 along with this work; if not, write to the Free Software Foundation,
183.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
183.23 + *
183.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
183.25 + * or visit www.oracle.com if you need additional information or have any
183.26 + * questions.
183.27 + */
183.28 +package org.apidesign.vm4brwsr;
183.29 +
183.30 +import java.io.ByteArrayInputStream;
183.31 +import java.io.DataInputStream;
183.32 +import java.io.IOException;
183.33 +import java.io.InputStream;
183.34 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
183.35 +import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
183.36 +
183.37 +/** This is a byte code parser heavily based on original code of JavaP utility.
183.38 + * As such I decided to keep the original Oracle's GPLv2 header.
183.39 + *
183.40 + * @author Jaroslav Tulach <jtulach@netbeans.org>
183.41 + */
183.42 +final class ByteCodeParser {
183.43 + private ByteCodeParser() {
183.44 + }
183.45 + /* Signature Characters */
183.46 + public static final char SIGC_VOID = 'V';
183.47 + public static final String SIG_VOID = "V";
183.48 + public static final char SIGC_BOOLEAN = 'Z';
183.49 + public static final String SIG_BOOLEAN = "Z";
183.50 + public static final char SIGC_BYTE = 'B';
183.51 + public static final String SIG_BYTE = "B";
183.52 + public static final char SIGC_CHAR = 'C';
183.53 + public static final String SIG_CHAR = "C";
183.54 + public static final char SIGC_SHORT = 'S';
183.55 + public static final String SIG_SHORT = "S";
183.56 + public static final char SIGC_INT = 'I';
183.57 + public static final String SIG_INT = "I";
183.58 + public static final char SIGC_LONG = 'J';
183.59 + public static final String SIG_LONG = "J";
183.60 + public static final char SIGC_FLOAT = 'F';
183.61 + public static final String SIG_FLOAT = "F";
183.62 + public static final char SIGC_DOUBLE = 'D';
183.63 + public static final String SIG_DOUBLE = "D";
183.64 + public static final char SIGC_ARRAY = '[';
183.65 + public static final String SIG_ARRAY = "[";
183.66 + public static final char SIGC_CLASS = 'L';
183.67 + public static final String SIG_CLASS = "L";
183.68 + public static final char SIGC_METHOD = '(';
183.69 + public static final String SIG_METHOD = "(";
183.70 + public static final char SIGC_ENDCLASS = ';';
183.71 + public static final String SIG_ENDCLASS = ";";
183.72 + public static final char SIGC_ENDMETHOD = ')';
183.73 + public static final String SIG_ENDMETHOD = ")";
183.74 + public static final char SIGC_PACKAGE = '/';
183.75 + public static final String SIG_PACKAGE = "/";
183.76 +
183.77 + /* Class File Constants */
183.78 + public static final int JAVA_MAGIC = 0xcafebabe;
183.79 + public static final int JAVA_VERSION = 45;
183.80 + public static final int JAVA_MINOR_VERSION = 3;
183.81 +
183.82 + /* Constant table */
183.83 + public static final int CONSTANT_UTF8 = 1;
183.84 + public static final int CONSTANT_UNICODE = 2;
183.85 + public static final int CONSTANT_INTEGER = 3;
183.86 + public static final int CONSTANT_FLOAT = 4;
183.87 + public static final int CONSTANT_LONG = 5;
183.88 + public static final int CONSTANT_DOUBLE = 6;
183.89 + public static final int CONSTANT_CLASS = 7;
183.90 + public static final int CONSTANT_STRING = 8;
183.91 + public static final int CONSTANT_FIELD = 9;
183.92 + public static final int CONSTANT_METHOD = 10;
183.93 + public static final int CONSTANT_INTERFACEMETHOD = 11;
183.94 + public static final int CONSTANT_NAMEANDTYPE = 12;
183.95 +
183.96 + /* Access Flags */
183.97 + public static final int ACC_PUBLIC = 0x00000001;
183.98 + public static final int ACC_PRIVATE = 0x00000002;
183.99 + public static final int ACC_PROTECTED = 0x00000004;
183.100 + public static final int ACC_STATIC = 0x00000008;
183.101 + public static final int ACC_FINAL = 0x00000010;
183.102 + public static final int ACC_SYNCHRONIZED = 0x00000020;
183.103 + public static final int ACC_SUPER = 0x00000020;
183.104 + public static final int ACC_VOLATILE = 0x00000040;
183.105 + public static final int ACC_TRANSIENT = 0x00000080;
183.106 + public static final int ACC_NATIVE = 0x00000100;
183.107 + public static final int ACC_INTERFACE = 0x00000200;
183.108 + public static final int ACC_ABSTRACT = 0x00000400;
183.109 + public static final int ACC_STRICT = 0x00000800;
183.110 + public static final int ACC_EXPLICIT = 0x00001000;
183.111 + public static final int ACC_SYNTHETIC = 0x00010000; // actually, this is an attribute
183.112 +
183.113 + /* Type codes */
183.114 + public static final int T_CLASS = 0x00000002;
183.115 + public static final int T_BOOLEAN = 0x00000004;
183.116 + public static final int T_CHAR = 0x00000005;
183.117 + public static final int T_FLOAT = 0x00000006;
183.118 + public static final int T_DOUBLE = 0x00000007;
183.119 + public static final int T_BYTE = 0x00000008;
183.120 + public static final int T_SHORT = 0x00000009;
183.121 + public static final int T_INT = 0x0000000a;
183.122 + public static final int T_LONG = 0x0000000b;
183.123 +
183.124 + /* Type codes for StackMap attribute */
183.125 + public static final int ITEM_Bogus =0; // an unknown or uninitialized value
183.126 + public static final int ITEM_Integer =1; // a 32-bit integer
183.127 + public static final int ITEM_Float =2; // not used
183.128 + public static final int ITEM_Double =3; // not used
183.129 + public static final int ITEM_Long =4; // a 64-bit integer
183.130 + public static final int ITEM_Null =5; // the type of null
183.131 + public static final int ITEM_InitObject =6; // "this" in constructor
183.132 + public static final int ITEM_Object =7; // followed by 2-byte index of class name
183.133 + public static final int ITEM_NewObject =8; // followed by 2-byte ref to "new"
183.134 +
183.135 + /* Constants used in StackMapTable attribute */
183.136 + public static final int SAME_FRAME_BOUND = 64;
183.137 + public static final int SAME_LOCALS_1_STACK_ITEM_BOUND = 128;
183.138 + public static final int SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247;
183.139 + public static final int SAME_FRAME_EXTENDED = 251;
183.140 + public static final int FULL_FRAME = 255;
183.141 +
183.142 + /* Opcodes */
183.143 + public static final int opc_dead = -2;
183.144 + public static final int opc_label = -1;
183.145 + public static final int opc_nop = 0;
183.146 + public static final int opc_aconst_null = 1;
183.147 + public static final int opc_iconst_m1 = 2;
183.148 + public static final int opc_iconst_0 = 3;
183.149 + public static final int opc_iconst_1 = 4;
183.150 + public static final int opc_iconst_2 = 5;
183.151 + public static final int opc_iconst_3 = 6;
183.152 + public static final int opc_iconst_4 = 7;
183.153 + public static final int opc_iconst_5 = 8;
183.154 + public static final int opc_lconst_0 = 9;
183.155 + public static final int opc_lconst_1 = 10;
183.156 + public static final int opc_fconst_0 = 11;
183.157 + public static final int opc_fconst_1 = 12;
183.158 + public static final int opc_fconst_2 = 13;
183.159 + public static final int opc_dconst_0 = 14;
183.160 + public static final int opc_dconst_1 = 15;
183.161 + public static final int opc_bipush = 16;
183.162 + public static final int opc_sipush = 17;
183.163 + public static final int opc_ldc = 18;
183.164 + public static final int opc_ldc_w = 19;
183.165 + public static final int opc_ldc2_w = 20;
183.166 + public static final int opc_iload = 21;
183.167 + public static final int opc_lload = 22;
183.168 + public static final int opc_fload = 23;
183.169 + public static final int opc_dload = 24;
183.170 + public static final int opc_aload = 25;
183.171 + public static final int opc_iload_0 = 26;
183.172 + public static final int opc_iload_1 = 27;
183.173 + public static final int opc_iload_2 = 28;
183.174 + public static final int opc_iload_3 = 29;
183.175 + public static final int opc_lload_0 = 30;
183.176 + public static final int opc_lload_1 = 31;
183.177 + public static final int opc_lload_2 = 32;
183.178 + public static final int opc_lload_3 = 33;
183.179 + public static final int opc_fload_0 = 34;
183.180 + public static final int opc_fload_1 = 35;
183.181 + public static final int opc_fload_2 = 36;
183.182 + public static final int opc_fload_3 = 37;
183.183 + public static final int opc_dload_0 = 38;
183.184 + public static final int opc_dload_1 = 39;
183.185 + public static final int opc_dload_2 = 40;
183.186 + public static final int opc_dload_3 = 41;
183.187 + public static final int opc_aload_0 = 42;
183.188 + public static final int opc_aload_1 = 43;
183.189 + public static final int opc_aload_2 = 44;
183.190 + public static final int opc_aload_3 = 45;
183.191 + public static final int opc_iaload = 46;
183.192 + public static final int opc_laload = 47;
183.193 + public static final int opc_faload = 48;
183.194 + public static final int opc_daload = 49;
183.195 + public static final int opc_aaload = 50;
183.196 + public static final int opc_baload = 51;
183.197 + public static final int opc_caload = 52;
183.198 + public static final int opc_saload = 53;
183.199 + public static final int opc_istore = 54;
183.200 + public static final int opc_lstore = 55;
183.201 + public static final int opc_fstore = 56;
183.202 + public static final int opc_dstore = 57;
183.203 + public static final int opc_astore = 58;
183.204 + public static final int opc_istore_0 = 59;
183.205 + public static final int opc_istore_1 = 60;
183.206 + public static final int opc_istore_2 = 61;
183.207 + public static final int opc_istore_3 = 62;
183.208 + public static final int opc_lstore_0 = 63;
183.209 + public static final int opc_lstore_1 = 64;
183.210 + public static final int opc_lstore_2 = 65;
183.211 + public static final int opc_lstore_3 = 66;
183.212 + public static final int opc_fstore_0 = 67;
183.213 + public static final int opc_fstore_1 = 68;
183.214 + public static final int opc_fstore_2 = 69;
183.215 + public static final int opc_fstore_3 = 70;
183.216 + public static final int opc_dstore_0 = 71;
183.217 + public static final int opc_dstore_1 = 72;
183.218 + public static final int opc_dstore_2 = 73;
183.219 + public static final int opc_dstore_3 = 74;
183.220 + public static final int opc_astore_0 = 75;
183.221 + public static final int opc_astore_1 = 76;
183.222 + public static final int opc_astore_2 = 77;
183.223 + public static final int opc_astore_3 = 78;
183.224 + public static final int opc_iastore = 79;
183.225 + public static final int opc_lastore = 80;
183.226 + public static final int opc_fastore = 81;
183.227 + public static final int opc_dastore = 82;
183.228 + public static final int opc_aastore = 83;
183.229 + public static final int opc_bastore = 84;
183.230 + public static final int opc_castore = 85;
183.231 + public static final int opc_sastore = 86;
183.232 + public static final int opc_pop = 87;
183.233 + public static final int opc_pop2 = 88;
183.234 + public static final int opc_dup = 89;
183.235 + public static final int opc_dup_x1 = 90;
183.236 + public static final int opc_dup_x2 = 91;
183.237 + public static final int opc_dup2 = 92;
183.238 + public static final int opc_dup2_x1 = 93;
183.239 + public static final int opc_dup2_x2 = 94;
183.240 + public static final int opc_swap = 95;
183.241 + public static final int opc_iadd = 96;
183.242 + public static final int opc_ladd = 97;
183.243 + public static final int opc_fadd = 98;
183.244 + public static final int opc_dadd = 99;
183.245 + public static final int opc_isub = 100;
183.246 + public static final int opc_lsub = 101;
183.247 + public static final int opc_fsub = 102;
183.248 + public static final int opc_dsub = 103;
183.249 + public static final int opc_imul = 104;
183.250 + public static final int opc_lmul = 105;
183.251 + public static final int opc_fmul = 106;
183.252 + public static final int opc_dmul = 107;
183.253 + public static final int opc_idiv = 108;
183.254 + public static final int opc_ldiv = 109;
183.255 + public static final int opc_fdiv = 110;
183.256 + public static final int opc_ddiv = 111;
183.257 + public static final int opc_irem = 112;
183.258 + public static final int opc_lrem = 113;
183.259 + public static final int opc_frem = 114;
183.260 + public static final int opc_drem = 115;
183.261 + public static final int opc_ineg = 116;
183.262 + public static final int opc_lneg = 117;
183.263 + public static final int opc_fneg = 118;
183.264 + public static final int opc_dneg = 119;
183.265 + public static final int opc_ishl = 120;
183.266 + public static final int opc_lshl = 121;
183.267 + public static final int opc_ishr = 122;
183.268 + public static final int opc_lshr = 123;
183.269 + public static final int opc_iushr = 124;
183.270 + public static final int opc_lushr = 125;
183.271 + public static final int opc_iand = 126;
183.272 + public static final int opc_land = 127;
183.273 + public static final int opc_ior = 128;
183.274 + public static final int opc_lor = 129;
183.275 + public static final int opc_ixor = 130;
183.276 + public static final int opc_lxor = 131;
183.277 + public static final int opc_iinc = 132;
183.278 + public static final int opc_i2l = 133;
183.279 + public static final int opc_i2f = 134;
183.280 + public static final int opc_i2d = 135;
183.281 + public static final int opc_l2i = 136;
183.282 + public static final int opc_l2f = 137;
183.283 + public static final int opc_l2d = 138;
183.284 + public static final int opc_f2i = 139;
183.285 + public static final int opc_f2l = 140;
183.286 + public static final int opc_f2d = 141;
183.287 + public static final int opc_d2i = 142;
183.288 + public static final int opc_d2l = 143;
183.289 + public static final int opc_d2f = 144;
183.290 + public static final int opc_i2b = 145;
183.291 + public static final int opc_int2byte = 145;
183.292 + public static final int opc_i2c = 146;
183.293 + public static final int opc_int2char = 146;
183.294 + public static final int opc_i2s = 147;
183.295 + public static final int opc_int2short = 147;
183.296 + public static final int opc_lcmp = 148;
183.297 + public static final int opc_fcmpl = 149;
183.298 + public static final int opc_fcmpg = 150;
183.299 + public static final int opc_dcmpl = 151;
183.300 + public static final int opc_dcmpg = 152;
183.301 + public static final int opc_ifeq = 153;
183.302 + public static final int opc_ifne = 154;
183.303 + public static final int opc_iflt = 155;
183.304 + public static final int opc_ifge = 156;
183.305 + public static final int opc_ifgt = 157;
183.306 + public static final int opc_ifle = 158;
183.307 + public static final int opc_if_icmpeq = 159;
183.308 + public static final int opc_if_icmpne = 160;
183.309 + public static final int opc_if_icmplt = 161;
183.310 + public static final int opc_if_icmpge = 162;
183.311 + public static final int opc_if_icmpgt = 163;
183.312 + public static final int opc_if_icmple = 164;
183.313 + public static final int opc_if_acmpeq = 165;
183.314 + public static final int opc_if_acmpne = 166;
183.315 + public static final int opc_goto = 167;
183.316 + public static final int opc_jsr = 168;
183.317 + public static final int opc_ret = 169;
183.318 + public static final int opc_tableswitch = 170;
183.319 + public static final int opc_lookupswitch = 171;
183.320 + public static final int opc_ireturn = 172;
183.321 + public static final int opc_lreturn = 173;
183.322 + public static final int opc_freturn = 174;
183.323 + public static final int opc_dreturn = 175;
183.324 + public static final int opc_areturn = 176;
183.325 + public static final int opc_return = 177;
183.326 + public static final int opc_getstatic = 178;
183.327 + public static final int opc_putstatic = 179;
183.328 + public static final int opc_getfield = 180;
183.329 + public static final int opc_putfield = 181;
183.330 + public static final int opc_invokevirtual = 182;
183.331 + public static final int opc_invokenonvirtual = 183;
183.332 + public static final int opc_invokespecial = 183;
183.333 + public static final int opc_invokestatic = 184;
183.334 + public static final int opc_invokeinterface = 185;
183.335 +// public static final int opc_xxxunusedxxx = 186;
183.336 + public static final int opc_new = 187;
183.337 + public static final int opc_newarray = 188;
183.338 + public static final int opc_anewarray = 189;
183.339 + public static final int opc_arraylength = 190;
183.340 + public static final int opc_athrow = 191;
183.341 + public static final int opc_checkcast = 192;
183.342 + public static final int opc_instanceof = 193;
183.343 + public static final int opc_monitorenter = 194;
183.344 + public static final int opc_monitorexit = 195;
183.345 + public static final int opc_wide = 196;
183.346 + public static final int opc_multianewarray = 197;
183.347 + public static final int opc_ifnull = 198;
183.348 + public static final int opc_ifnonnull = 199;
183.349 + public static final int opc_goto_w = 200;
183.350 + public static final int opc_jsr_w = 201;
183.351 + /* Pseudo-instructions */
183.352 + public static final int opc_bytecode = 203;
183.353 + public static final int opc_try = 204;
183.354 + public static final int opc_endtry = 205;
183.355 + public static final int opc_catch = 206;
183.356 + public static final int opc_var = 207;
183.357 + public static final int opc_endvar = 208;
183.358 + public static final int opc_localsmap = 209;
183.359 + public static final int opc_stackmap = 210;
183.360 + /* PicoJava prefixes */
183.361 + public static final int opc_nonpriv = 254;
183.362 + public static final int opc_priv = 255;
183.363 +
183.364 + /* Wide instructions */
183.365 + public static final int opc_iload_w = (opc_wide<<8)|opc_iload;
183.366 + public static final int opc_lload_w = (opc_wide<<8)|opc_lload;
183.367 + public static final int opc_fload_w = (opc_wide<<8)|opc_fload;
183.368 + public static final int opc_dload_w = (opc_wide<<8)|opc_dload;
183.369 + public static final int opc_aload_w = (opc_wide<<8)|opc_aload;
183.370 + public static final int opc_istore_w = (opc_wide<<8)|opc_istore;
183.371 + public static final int opc_lstore_w = (opc_wide<<8)|opc_lstore;
183.372 + public static final int opc_fstore_w = (opc_wide<<8)|opc_fstore;
183.373 + public static final int opc_dstore_w = (opc_wide<<8)|opc_dstore;
183.374 + public static final int opc_astore_w = (opc_wide<<8)|opc_astore;
183.375 + public static final int opc_ret_w = (opc_wide<<8)|opc_ret;
183.376 + public static final int opc_iinc_w = (opc_wide<<8)|opc_iinc;
183.377 +
183.378 + /* Opcode Names */
183.379 + public static final String opcNamesTab[] = {
183.380 + "nop",
183.381 + "aconst_null",
183.382 + "iconst_m1",
183.383 + "iconst_0",
183.384 + "iconst_1",
183.385 + "iconst_2",
183.386 + "iconst_3",
183.387 + "iconst_4",
183.388 + "iconst_5",
183.389 + "lconst_0",
183.390 + "lconst_1",
183.391 + "fconst_0",
183.392 + "fconst_1",
183.393 + "fconst_2",
183.394 + "dconst_0",
183.395 + "dconst_1",
183.396 + "bipush",
183.397 + "sipush",
183.398 + "ldc",
183.399 + "ldc_w",
183.400 + "ldc2_w",
183.401 + "iload",
183.402 + "lload",
183.403 + "fload",
183.404 + "dload",
183.405 + "aload",
183.406 + "iload_0",
183.407 + "iload_1",
183.408 + "iload_2",
183.409 + "iload_3",
183.410 + "lload_0",
183.411 + "lload_1",
183.412 + "lload_2",
183.413 + "lload_3",
183.414 + "fload_0",
183.415 + "fload_1",
183.416 + "fload_2",
183.417 + "fload_3",
183.418 + "dload_0",
183.419 + "dload_1",
183.420 + "dload_2",
183.421 + "dload_3",
183.422 + "aload_0",
183.423 + "aload_1",
183.424 + "aload_2",
183.425 + "aload_3",
183.426 + "iaload",
183.427 + "laload",
183.428 + "faload",
183.429 + "daload",
183.430 + "aaload",
183.431 + "baload",
183.432 + "caload",
183.433 + "saload",
183.434 + "istore",
183.435 + "lstore",
183.436 + "fstore",
183.437 + "dstore",
183.438 + "astore",
183.439 + "istore_0",
183.440 + "istore_1",
183.441 + "istore_2",
183.442 + "istore_3",
183.443 + "lstore_0",
183.444 + "lstore_1",
183.445 + "lstore_2",
183.446 + "lstore_3",
183.447 + "fstore_0",
183.448 + "fstore_1",
183.449 + "fstore_2",
183.450 + "fstore_3",
183.451 + "dstore_0",
183.452 + "dstore_1",
183.453 + "dstore_2",
183.454 + "dstore_3",
183.455 + "astore_0",
183.456 + "astore_1",
183.457 + "astore_2",
183.458 + "astore_3",
183.459 + "iastore",
183.460 + "lastore",
183.461 + "fastore",
183.462 + "dastore",
183.463 + "aastore",
183.464 + "bastore",
183.465 + "castore",
183.466 + "sastore",
183.467 + "pop",
183.468 + "pop2",
183.469 + "dup",
183.470 + "dup_x1",
183.471 + "dup_x2",
183.472 + "dup2",
183.473 + "dup2_x1",
183.474 + "dup2_x2",
183.475 + "swap",
183.476 + "iadd",
183.477 + "ladd",
183.478 + "fadd",
183.479 + "dadd",
183.480 + "isub",
183.481 + "lsub",
183.482 + "fsub",
183.483 + "dsub",
183.484 + "imul",
183.485 + "lmul",
183.486 + "fmul",
183.487 + "dmul",
183.488 + "idiv",
183.489 + "ldiv",
183.490 + "fdiv",
183.491 + "ddiv",
183.492 + "irem",
183.493 + "lrem",
183.494 + "frem",
183.495 + "drem",
183.496 + "ineg",
183.497 + "lneg",
183.498 + "fneg",
183.499 + "dneg",
183.500 + "ishl",
183.501 + "lshl",
183.502 + "ishr",
183.503 + "lshr",
183.504 + "iushr",
183.505 + "lushr",
183.506 + "iand",
183.507 + "land",
183.508 + "ior",
183.509 + "lor",
183.510 + "ixor",
183.511 + "lxor",
183.512 + "iinc",
183.513 + "i2l",
183.514 + "i2f",
183.515 + "i2d",
183.516 + "l2i",
183.517 + "l2f",
183.518 + "l2d",
183.519 + "f2i",
183.520 + "f2l",
183.521 + "f2d",
183.522 + "d2i",
183.523 + "d2l",
183.524 + "d2f",
183.525 + "i2b",
183.526 + "i2c",
183.527 + "i2s",
183.528 + "lcmp",
183.529 + "fcmpl",
183.530 + "fcmpg",
183.531 + "dcmpl",
183.532 + "dcmpg",
183.533 + "ifeq",
183.534 + "ifne",
183.535 + "iflt",
183.536 + "ifge",
183.537 + "ifgt",
183.538 + "ifle",
183.539 + "if_icmpeq",
183.540 + "if_icmpne",
183.541 + "if_icmplt",
183.542 + "if_icmpge",
183.543 + "if_icmpgt",
183.544 + "if_icmple",
183.545 + "if_acmpeq",
183.546 + "if_acmpne",
183.547 + "goto",
183.548 + "jsr",
183.549 + "ret",
183.550 + "tableswitch",
183.551 + "lookupswitch",
183.552 + "ireturn",
183.553 + "lreturn",
183.554 + "freturn",
183.555 + "dreturn",
183.556 + "areturn",
183.557 + "return",
183.558 + "getstatic",
183.559 + "putstatic",
183.560 + "getfield",
183.561 + "putfield",
183.562 + "invokevirtual",
183.563 + "invokespecial", // was "invokenonvirtual",
183.564 + "invokestatic",
183.565 + "invokeinterface",
183.566 + "bytecode 186", //"xxxunusedxxx",
183.567 + "new",
183.568 + "newarray",
183.569 + "anewarray",
183.570 + "arraylength",
183.571 + "athrow",
183.572 + "checkcast",
183.573 + "instanceof",
183.574 + "monitorenter",
183.575 + "monitorexit",
183.576 + null, // "wide",
183.577 + "multianewarray",
183.578 + "ifnull",
183.579 + "ifnonnull",
183.580 + "goto_w",
183.581 + "jsr_w",
183.582 + "bytecode 202", // "breakpoint",
183.583 + "bytecode",
183.584 + "try",
183.585 + "endtry",
183.586 + "catch",
183.587 + "var",
183.588 + "endvar",
183.589 + "locals_map",
183.590 + "stack_map"
183.591 + };
183.592 +
183.593 + /* Opcode Lengths */
183.594 + public static final int opcLengthsTab[] = {
183.595 + 1,
183.596 + 1,
183.597 + 1,
183.598 + 1,
183.599 + 1,
183.600 + 1,
183.601 + 1,
183.602 + 1,
183.603 + 1,
183.604 + 1,
183.605 + 1,
183.606 + 1,
183.607 + 1,
183.608 + 1,
183.609 + 1,
183.610 + 1,
183.611 + 2,
183.612 + 3,
183.613 + 2,
183.614 + 3,
183.615 + 3,
183.616 + 2,
183.617 + 2,
183.618 + 2,
183.619 + 2,
183.620 + 2,
183.621 + 1,
183.622 + 1,
183.623 + 1,
183.624 + 1,
183.625 + 1,
183.626 + 1,
183.627 + 1,
183.628 + 1,
183.629 + 1,
183.630 + 1,
183.631 + 1,
183.632 + 1,
183.633 + 1,
183.634 + 1,
183.635 + 1,
183.636 + 1,
183.637 + 1,
183.638 + 1,
183.639 + 1,
183.640 + 1,
183.641 + 1,
183.642 + 1,
183.643 + 1,
183.644 + 1,
183.645 + 1,
183.646 + 1,
183.647 + 1,
183.648 + 1,
183.649 + 2,
183.650 + 2,
183.651 + 2,
183.652 + 2,
183.653 + 2,
183.654 + 1,
183.655 + 1,
183.656 + 1,
183.657 + 1,
183.658 + 1,
183.659 + 1,
183.660 + 1,
183.661 + 1,
183.662 + 1,
183.663 + 1,
183.664 + 1,
183.665 + 1,
183.666 + 1,
183.667 + 1,
183.668 + 1,
183.669 + 1,
183.670 + 1,
183.671 + 1,
183.672 + 1,
183.673 + 1,
183.674 + 1,
183.675 + 1,
183.676 + 1,
183.677 + 1,
183.678 + 1,
183.679 + 1,
183.680 + 1,
183.681 + 1,
183.682 + 1,
183.683 + 1,
183.684 + 1,
183.685 + 1,
183.686 + 1,
183.687 + 1,
183.688 + 1,
183.689 + 1,
183.690 + 1,
183.691 + 1,
183.692 + 1,
183.693 + 1,
183.694 + 1,
183.695 + 1,
183.696 + 1,
183.697 + 1,
183.698 + 1,
183.699 + 1,
183.700 + 1,
183.701 + 1,
183.702 + 1,
183.703 + 1,
183.704 + 1,
183.705 + 1,
183.706 + 1,
183.707 + 1,
183.708 + 1,
183.709 + 1,
183.710 + 1,
183.711 + 1,
183.712 + 1,
183.713 + 1,
183.714 + 1,
183.715 + 1,
183.716 + 1,
183.717 + 1,
183.718 + 1,
183.719 + 1,
183.720 + 1,
183.721 + 1,
183.722 + 1,
183.723 + 1,
183.724 + 1,
183.725 + 1,
183.726 + 1,
183.727 + 3,
183.728 + 1,
183.729 + 1,
183.730 + 1,
183.731 + 1,
183.732 + 1,
183.733 + 1,
183.734 + 1,
183.735 + 1,
183.736 + 1,
183.737 + 1,
183.738 + 1,
183.739 + 1,
183.740 + 1,
183.741 + 1,
183.742 + 1,
183.743 + 1,
183.744 + 1,
183.745 + 1,
183.746 + 1,
183.747 + 1,
183.748 + 3,
183.749 + 3,
183.750 + 3,
183.751 + 3,
183.752 + 3,
183.753 + 3,
183.754 + 3,
183.755 + 3,
183.756 + 3,
183.757 + 3,
183.758 + 3,
183.759 + 3,
183.760 + 3,
183.761 + 3,
183.762 + 3,
183.763 + 3,
183.764 + 2,
183.765 + 99,
183.766 + 99,
183.767 + 1,
183.768 + 1,
183.769 + 1,
183.770 + 1,
183.771 + 1,
183.772 + 1,
183.773 + 3,
183.774 + 3,
183.775 + 3,
183.776 + 3,
183.777 + 3,
183.778 + 3,
183.779 + 3,
183.780 + 5,
183.781 + 0,
183.782 + 3,
183.783 + 2,
183.784 + 3,
183.785 + 1,
183.786 + 1,
183.787 + 3,
183.788 + 3,
183.789 + 1,
183.790 + 1,
183.791 + 0, // wide
183.792 + 4,
183.793 + 3,
183.794 + 3,
183.795 + 5,
183.796 + 5,
183.797 + 1,
183.798 + 1, 0, 0, 0, 0, 0 // pseudo
183.799 + };
183.800 +
183.801 + /**
183.802 + * End of input
183.803 + */
183.804 + public static final int EOF = -1;
183.805 +
183.806 + /*
183.807 + * Flags
183.808 + */
183.809 + public static final int F_VERBOSE = 1 << 0;
183.810 + public static final int F_DUMP = 1 << 1;
183.811 + public static final int F_WARNINGS = 1 << 2;
183.812 + public static final int F_DEBUG = 1 << 3;
183.813 + public static final int F_OPTIMIZE = 1 << 4;
183.814 + public static final int F_DEPENDENCIES = 1 << 5;
183.815 +
183.816 + /*
183.817 + * Type codes
183.818 + */
183.819 + public static final int TC_BOOLEAN = 0;
183.820 + public static final int TC_BYTE = 1;
183.821 + public static final int TC_CHAR = 2;
183.822 + public static final int TC_SHORT = 3;
183.823 + public static final int TC_INT = 4;
183.824 + public static final int TC_LONG = 5;
183.825 + public static final int TC_FLOAT = 6;
183.826 + public static final int TC_DOUBLE = 7;
183.827 + public static final int TC_NULL = 8;
183.828 + public static final int TC_ARRAY = 9;
183.829 + public static final int TC_CLASS = 10;
183.830 + public static final int TC_VOID = 11;
183.831 + public static final int TC_METHOD = 12;
183.832 + public static final int TC_ERROR = 13;
183.833 +
183.834 + /*
183.835 + * Type Masks
183.836 + */
183.837 + public static final int TM_NULL = 1 << TC_NULL;
183.838 + public static final int TM_VOID = 1 << TC_VOID;
183.839 + public static final int TM_BOOLEAN = 1 << TC_BOOLEAN;
183.840 + public static final int TM_BYTE = 1 << TC_BYTE;
183.841 + public static final int TM_CHAR = 1 << TC_CHAR;
183.842 + public static final int TM_SHORT = 1 << TC_SHORT;
183.843 + public static final int TM_INT = 1 << TC_INT;
183.844 + public static final int TM_LONG = 1 << TC_LONG;
183.845 + public static final int TM_FLOAT = 1 << TC_FLOAT;
183.846 + public static final int TM_DOUBLE = 1 << TC_DOUBLE;
183.847 + public static final int TM_ARRAY = 1 << TC_ARRAY;
183.848 + public static final int TM_CLASS = 1 << TC_CLASS;
183.849 + public static final int TM_METHOD = 1 << TC_METHOD;
183.850 + public static final int TM_ERROR = 1 << TC_ERROR;
183.851 +
183.852 + public static final int TM_INT32 = TM_BYTE | TM_SHORT | TM_CHAR | TM_INT;
183.853 + public static final int TM_NUM32 = TM_INT32 | TM_FLOAT;
183.854 + public static final int TM_NUM64 = TM_LONG | TM_DOUBLE;
183.855 + public static final int TM_INTEGER = TM_INT32 | TM_LONG;
183.856 + public static final int TM_REAL = TM_FLOAT | TM_DOUBLE;
183.857 + public static final int TM_NUMBER = TM_INTEGER | TM_REAL;
183.858 + public static final int TM_REFERENCE = TM_ARRAY | TM_CLASS | TM_NULL;
183.859 +
183.860 + /*
183.861 + * Class status
183.862 + */
183.863 + public static final int CS_UNDEFINED = 0;
183.864 + public static final int CS_UNDECIDED = 1;
183.865 + public static final int CS_BINARY = 2;
183.866 + public static final int CS_SOURCE = 3;
183.867 + public static final int CS_PARSED = 4;
183.868 + public static final int CS_COMPILED = 5;
183.869 + public static final int CS_NOTFOUND = 6;
183.870 +
183.871 + /*
183.872 + * Attributes
183.873 + */
183.874 + public static final int ATT_ALL = -1;
183.875 + public static final int ATT_CODE = 1;
183.876 +
183.877 + /*
183.878 + * Number of bits used in file offsets
183.879 + */
183.880 + public static final int OFFSETBITS = 19;
183.881 + public static final int MAXFILESIZE = (1 << OFFSETBITS) - 1;
183.882 + public static final int MAXLINENUMBER = (1 << (32 - OFFSETBITS)) - 1;
183.883 +
183.884 + /*
183.885 + * Operators
183.886 + */
183.887 + public final int COMMA = 0;
183.888 + public final int ASSIGN = 1;
183.889 +
183.890 + public final int ASGMUL = 2;
183.891 + public final int ASGDIV = 3;
183.892 + public final int ASGREM = 4;
183.893 + public final int ASGADD = 5;
183.894 + public final int ASGSUB = 6;
183.895 + public final int ASGLSHIFT = 7;
183.896 + public final int ASGRSHIFT = 8;
183.897 + public final int ASGURSHIFT = 9;
183.898 + public final int ASGBITAND = 10;
183.899 + public final int ASGBITOR = 11;
183.900 + public final int ASGBITXOR = 12;
183.901 +
183.902 + public final int COND = 13;
183.903 + public final int OR = 14;
183.904 + public final int AND = 15;
183.905 + public final int BITOR = 16;
183.906 + public final int BITXOR = 17;
183.907 + public final int BITAND = 18;
183.908 + public final int NE = 19;
183.909 + public final int EQ = 20;
183.910 + public final int GE = 21;
183.911 + public final int GT = 22;
183.912 + public final int LE = 23;
183.913 + public final int LT = 24;
183.914 + public final int INSTANCEOF = 25;
183.915 + public final int LSHIFT = 26;
183.916 + public final int RSHIFT = 27;
183.917 + public final int URSHIFT = 28;
183.918 + public final int ADD = 29;
183.919 + public final int SUB = 30;
183.920 + public final int DIV = 31;
183.921 + public final int REM = 32;
183.922 + public final int MUL = 33;
183.923 + public final int CAST = 34; // (x)y
183.924 + public final int POS = 35; // +x
183.925 + public final int NEG = 36; // -x
183.926 + public final int NOT = 37;
183.927 + public final int BITNOT = 38;
183.928 + public final int PREINC = 39; // ++x
183.929 + public final int PREDEC = 40; // --x
183.930 + public final int NEWARRAY = 41;
183.931 + public final int NEWINSTANCE = 42;
183.932 + public final int NEWFROMNAME = 43;
183.933 + public final int POSTINC = 44; // x++
183.934 + public final int POSTDEC = 45; // x--
183.935 + public final int FIELD = 46;
183.936 + public final int METHOD = 47; // x(y)
183.937 + public final int ARRAYACCESS = 48; // x[y]
183.938 + public final int NEW = 49;
183.939 + public final int INC = 50;
183.940 + public final int DEC = 51;
183.941 +
183.942 + public final int CONVERT = 55; // implicit conversion
183.943 + public final int EXPR = 56; // (x)
183.944 + public final int ARRAY = 57; // {x, y, ...}
183.945 + public final int GOTO = 58;
183.946 +
183.947 + /*
183.948 + * Value tokens
183.949 + */
183.950 + public final int IDENT = 60;
183.951 + public final int BOOLEANVAL = 61;
183.952 + public final int BYTEVAL = 62;
183.953 + public final int CHARVAL = 63;
183.954 + public final int SHORTVAL = 64;
183.955 + public final int INTVAL = 65;
183.956 + public final int LONGVAL = 66;
183.957 + public final int FLOATVAL = 67;
183.958 + public final int DOUBLEVAL = 68;
183.959 + public final int STRINGVAL = 69;
183.960 +
183.961 + /*
183.962 + * Type keywords
183.963 + */
183.964 + public final int BYTE = 70;
183.965 + public final int CHAR = 71;
183.966 + public final int SHORT = 72;
183.967 + public final int INT = 73;
183.968 + public final int LONG = 74;
183.969 + public final int FLOAT = 75;
183.970 + public final int DOUBLE = 76;
183.971 + public final int VOID = 77;
183.972 + public final int BOOLEAN = 78;
183.973 +
183.974 + /*
183.975 + * Expression keywords
183.976 + */
183.977 + public final int TRUE = 80;
183.978 + public final int FALSE = 81;
183.979 + public final int THIS = 82;
183.980 + public final int SUPER = 83;
183.981 + public final int NULL = 84;
183.982 +
183.983 + /*
183.984 + * Statement keywords
183.985 + */
183.986 + public final int IF = 90;
183.987 + public final int ELSE = 91;
183.988 + public final int FOR = 92;
183.989 + public final int WHILE = 93;
183.990 + public final int DO = 94;
183.991 + public final int SWITCH = 95;
183.992 + public final int CASE = 96;
183.993 + public final int DEFAULT = 97;
183.994 + public final int BREAK = 98;
183.995 + public final int CONTINUE = 99;
183.996 + public final int RETURN = 100;
183.997 + public final int TRY = 101;
183.998 + public final int CATCH = 102;
183.999 + public final int FINALLY = 103;
183.1000 + public final int THROW = 104;
183.1001 + public final int STAT = 105;
183.1002 + public final int EXPRESSION = 106;
183.1003 + public final int DECLARATION = 107;
183.1004 + public final int VARDECLARATION = 108;
183.1005 +
183.1006 + /*
183.1007 + * Declaration keywords
183.1008 + */
183.1009 + public final int IMPORT = 110;
183.1010 + public final int CLASS = 111;
183.1011 + public final int EXTENDS = 112;
183.1012 + public final int IMPLEMENTS = 113;
183.1013 + public final int INTERFACE = 114;
183.1014 + public final int PACKAGE = 115;
183.1015 +
183.1016 + /*
183.1017 + * Modifier keywords
183.1018 + */
183.1019 + public final int PRIVATE = 120;
183.1020 + public final int PUBLIC = 121;
183.1021 + public final int PROTECTED = 122;
183.1022 + public final int CONST = 123;
183.1023 + public final int STATIC = 124;
183.1024 + public final int TRANSIENT = 125;
183.1025 + public final int SYNCHRONIZED = 126;
183.1026 + public final int NATIVE = 127;
183.1027 + public final int FINAL = 128;
183.1028 + public final int VOLATILE = 129;
183.1029 + public final int ABSTRACT = 130;
183.1030 + public final int STRICT = 165;
183.1031 +
183.1032 + /*
183.1033 + * Punctuation
183.1034 + */
183.1035 + public final int SEMICOLON = 135;
183.1036 + public final int COLON = 136;
183.1037 + public final int QUESTIONMARK = 137;
183.1038 + public final int LBRACE = 138;
183.1039 + public final int RBRACE = 139;
183.1040 + public final int LPAREN = 140;
183.1041 + public final int RPAREN = 141;
183.1042 + public final int LSQBRACKET = 142;
183.1043 + public final int RSQBRACKET = 143;
183.1044 + public final int THROWS = 144;
183.1045 +
183.1046 + /*
183.1047 + * Special tokens
183.1048 + */
183.1049 + public final int ERROR = 145; // an error
183.1050 + public final int COMMENT = 146; // not used anymore.
183.1051 + public final int TYPE = 147;
183.1052 + public final int LENGTH = 148;
183.1053 + public final int INLINERETURN = 149;
183.1054 + public final int INLINEMETHOD = 150;
183.1055 + public final int INLINENEWINSTANCE = 151;
183.1056 +
183.1057 + /*
183.1058 + * Added for jasm
183.1059 + */
183.1060 + public final int METHODREF = 152;
183.1061 + public final int FIELDREF = 153;
183.1062 + public final int STACK = 154;
183.1063 + public final int LOCAL = 155;
183.1064 + public final int CPINDEX = 156;
183.1065 + public final int CPNAME = 157;
183.1066 + public final int SIGN = 158;
183.1067 + public final int BITS = 159;
183.1068 + public final int INF = 160;
183.1069 + public final int NAN = 161;
183.1070 + public final int INNERCLASS = 162;
183.1071 + public final int OF = 163;
183.1072 + public final int SYNTHETIC = 164;
183.1073 +// last used=165;
183.1074 +
183.1075 + /*
183.1076 + * Operator precedence
183.1077 + */
183.1078 + public static final int opPrecedence[] = {
183.1079 + 10, 11, 11, 11, 11, 11, 11, 11, 11, 11,
183.1080 + 11, 11, 11, 12, 13, 14, 15, 16, 17, 18,
183.1081 + 18, 19, 19, 19, 19, 19, 20, 20, 20, 21,
183.1082 + 21, 22, 22, 22, 23, 24, 24, 24, 24, 24,
183.1083 + 24, 25, 25, 26, 26, 26, 26, 26, 26
183.1084 + };
183.1085 +
183.1086 + /*
183.1087 + * Operator names
183.1088 + */
183.1089 + public static final String opNames[] = {
183.1090 + ",", "=", "*=", "/=", "%=",
183.1091 + "+=", "-=", "<<=", ">>=", "<<<=",
183.1092 + "&=", "|=", "^=", "?:", "||",
183.1093 + "&&", "|", "^", "&", "!=",
183.1094 + "==", ">=", ">", "<=", "<",
183.1095 + "instanceof", "<<", ">>", "<<<", "+",
183.1096 + "-", "/", "%", "*", "cast",
183.1097 + "+", "-", "!", "~", "++",
183.1098 + "--", "new", "new", "new", "++",
183.1099 + "--", "field", "method", "[]", "new",
183.1100 + "++", "--", null, null, null,
183.1101 +
183.1102 + "convert", "expr", "array", "goto", null,
183.1103 +
183.1104 + "Identifier", "Boolean", "Byte", "Char", "Short",
183.1105 + "Integer", "Long", "Float", "Double", "String",
183.1106 +
183.1107 + "byte", "char", "short", "int", "long",
183.1108 + "float", "double", "void", "boolean", null,
183.1109 +
183.1110 + "true", "false", "this", "super", "null",
183.1111 + null, null, null, null, null,
183.1112 +
183.1113 + "if", "else", "for", "while", "do",
183.1114 + "switch", "case", "default", "break", "continue",
183.1115 + "return", "try", "catch", "finally", "throw",
183.1116 + "stat", "expression", "declaration", "declaration", null,
183.1117 +
183.1118 + "import", "class", "extends", "implements", "interface",
183.1119 + "package", null, null, null, null,
183.1120 +
183.1121 + "private", "public", "protected", "const", "static",
183.1122 + "transient", "synchronized", "native", "final", "volatile",
183.1123 + "abstract", null, null, null, null,
183.1124 +
183.1125 + ";", ":", "?", "{", "}",
183.1126 + "(", ")", "[", "]", "throws",
183.1127 + "error", "comment", "type", "length", "inline-return",
183.1128 + "inline-method", "inline-new",
183.1129 + "method", "field", "stack", "locals", "CPINDEX", "CPName", "SIGN",
183.1130 + "bits", "INF", "NaN", "InnerClass", "of", "synthetic"
183.1131 + };
183.1132 +
183.1133 + static class AnnotationParser {
183.1134 +
183.1135 + private final boolean textual;
183.1136 + private final boolean iterateArray;
183.1137 +
183.1138 + protected AnnotationParser(boolean textual, boolean iterateArray) {
183.1139 + this.textual = textual;
183.1140 + this.iterateArray = iterateArray;
183.1141 + }
183.1142 +
183.1143 + protected void visitAnnotationStart(String type, boolean top) throws IOException {
183.1144 + }
183.1145 +
183.1146 + protected void visitAnnotationEnd(String type, boolean top) throws IOException {
183.1147 + }
183.1148 +
183.1149 + protected void visitValueStart(String attrName, char type) throws IOException {
183.1150 + }
183.1151 +
183.1152 + protected void visitValueEnd(String attrName, char type) throws IOException {
183.1153 + }
183.1154 +
183.1155 + protected void visitAttr(
183.1156 + String annoType, String attr, String attrType, String value) throws IOException {
183.1157 + }
183.1158 +
183.1159 + protected void visitEnumAttr(
183.1160 + String annoType, String attr, String attrType, String value) throws IOException {
183.1161 + visitAttr(annoType, attr, attrType, value);
183.1162 + }
183.1163 +
183.1164 + /**
183.1165 + * Initialize the parsing with constant pool from
183.1166 + * <code>cd</code>.
183.1167 + *
183.1168 + * @param attr the attribute defining annotations
183.1169 + * @param cd constant pool
183.1170 + * @throws IOException in case I/O fails
183.1171 + */
183.1172 + public final void parse(byte[] attr, ClassData cd) throws IOException {
183.1173 + ByteArrayInputStream is = new ByteArrayInputStream(attr);
183.1174 + DataInputStream dis = new DataInputStream(is);
183.1175 + try {
183.1176 + read(dis, cd);
183.1177 + } finally {
183.1178 + is.close();
183.1179 + }
183.1180 + }
183.1181 +
183.1182 + private void read(DataInputStream dis, ClassData cd) throws IOException {
183.1183 + int cnt = dis.readUnsignedShort();
183.1184 + for (int i = 0; i < cnt; i++) {
183.1185 + readAnno(dis, cd, true);
183.1186 + }
183.1187 + }
183.1188 +
183.1189 + private void readAnno(DataInputStream dis, ClassData cd, boolean top) throws IOException {
183.1190 + int type = dis.readUnsignedShort();
183.1191 + String typeName = cd.StringValue(type);
183.1192 + visitAnnotationStart(typeName, top);
183.1193 + int cnt = dis.readUnsignedShort();
183.1194 + for (int i = 0; i < cnt; i++) {
183.1195 + String attrName = cd.StringValue(dis.readUnsignedShort());
183.1196 + readValue(dis, cd, typeName, attrName);
183.1197 + }
183.1198 + visitAnnotationEnd(typeName, top);
183.1199 + if (cnt == 0) {
183.1200 + visitAttr(typeName, null, null, null);
183.1201 + }
183.1202 + }
183.1203 +
183.1204 + private void readValue(
183.1205 + DataInputStream dis, ClassData cd, String typeName, String attrName) throws IOException {
183.1206 + char type = (char) dis.readByte();
183.1207 + visitValueStart(attrName, type);
183.1208 + if (type == '@') {
183.1209 + readAnno(dis, cd, false);
183.1210 + } else if ("CFJZsSIDB".indexOf(type) >= 0) { // NOI18N
183.1211 + int primitive = dis.readUnsignedShort();
183.1212 + String val = cd.stringValue(primitive, textual);
183.1213 + String attrType;
183.1214 + if (type == 's') {
183.1215 + attrType = "Ljava_lang_String_2";
183.1216 + if (textual) {
183.1217 + val = '"' + val + '"';
183.1218 + }
183.1219 + } else {
183.1220 + attrType = "" + type;
183.1221 + }
183.1222 + visitAttr(typeName, attrName, attrType, val);
183.1223 + } else if (type == 'c') {
183.1224 + int cls = dis.readUnsignedShort();
183.1225 + } else if (type == '[') {
183.1226 + int cnt = dis.readUnsignedShort();
183.1227 + for (int i = 0; i < cnt; i++) {
183.1228 + readValue(dis, cd, typeName, iterateArray ? attrName : null);
183.1229 + }
183.1230 + } else if (type == 'e') {
183.1231 + int enumT = dis.readUnsignedShort();
183.1232 + String attrType = cd.stringValue(enumT, textual);
183.1233 + int enumN = dis.readUnsignedShort();
183.1234 + String val = cd.stringValue(enumN, textual);
183.1235 + visitEnumAttr(typeName, attrName, attrType, val);
183.1236 + } else {
183.1237 + throw new IOException("Unknown type " + type);
183.1238 + }
183.1239 + visitValueEnd(attrName, type);
183.1240 + }
183.1241 + }
183.1242 +
183.1243 + /**
183.1244 + * Reads and stores attribute information.
183.1245 + *
183.1246 + * @author Sucheta Dambalkar (Adopted code from jdis)
183.1247 + */
183.1248 + private static class AttrData {
183.1249 +
183.1250 + ClassData cls;
183.1251 + int name_cpx;
183.1252 + int datalen;
183.1253 + byte data[];
183.1254 +
183.1255 + public AttrData(ClassData cls) {
183.1256 + this.cls = cls;
183.1257 + }
183.1258 +
183.1259 + /**
183.1260 + * Reads unknown attribute.
183.1261 + */
183.1262 + public void read(int name_cpx, DataInputStream in) throws IOException {
183.1263 + this.name_cpx = name_cpx;
183.1264 + datalen = in.readInt();
183.1265 + data = new byte[datalen];
183.1266 + in.readFully(data);
183.1267 + }
183.1268 +
183.1269 + /**
183.1270 + * Reads just the name of known attribute.
183.1271 + */
183.1272 + public void read(int name_cpx) {
183.1273 + this.name_cpx = name_cpx;
183.1274 + }
183.1275 +
183.1276 + /**
183.1277 + * Returns attribute name.
183.1278 + */
183.1279 + public String getAttrName() {
183.1280 + return cls.getString(name_cpx);
183.1281 + }
183.1282 +
183.1283 + /**
183.1284 + * Returns attribute data.
183.1285 + */
183.1286 + public byte[] getData() {
183.1287 + return data;
183.1288 + }
183.1289 + }
183.1290 +
183.1291 + /**
183.1292 + * Stores constant pool entry information with one field.
183.1293 + *
183.1294 + * @author Sucheta Dambalkar (Adopted code from jdis)
183.1295 + */
183.1296 + private static class CPX {
183.1297 +
183.1298 + int cpx;
183.1299 +
183.1300 + CPX(int cpx) {
183.1301 + this.cpx = cpx;
183.1302 + }
183.1303 + }
183.1304 +
183.1305 + /**
183.1306 + * Stores constant pool entry information with two fields.
183.1307 + *
183.1308 + * @author Sucheta Dambalkar (Adopted code from jdis)
183.1309 + */
183.1310 + private static class CPX2 {
183.1311 +
183.1312 + int cpx1, cpx2;
183.1313 +
183.1314 + CPX2(int cpx1, int cpx2) {
183.1315 + this.cpx1 = cpx1;
183.1316 + this.cpx2 = cpx2;
183.1317 + }
183.1318 + }
183.1319 +
183.1320 + /**
183.1321 + * Central data repository of the Java Disassembler. Stores all the
183.1322 + * information in java class file.
183.1323 + *
183.1324 + * @author Sucheta Dambalkar (Adopted code from jdis)
183.1325 + */
183.1326 + static final class ClassData {
183.1327 +
183.1328 + private int magic;
183.1329 + private int minor_version;
183.1330 + private int major_version;
183.1331 + private int cpool_count;
183.1332 + private Object cpool[];
183.1333 + private int access;
183.1334 + private int this_class = 0;
183.1335 + private int super_class;
183.1336 + private int interfaces_count;
183.1337 + private int[] interfaces = new int[0];
183.1338 + private int fields_count;
183.1339 + private FieldData[] fields;
183.1340 + private int methods_count;
183.1341 + private MethodData[] methods;
183.1342 + private InnerClassData[] innerClasses;
183.1343 + private int attributes_count;
183.1344 + private AttrData[] attrs;
183.1345 + private String classname;
183.1346 + private String superclassname;
183.1347 + private int source_cpx = 0;
183.1348 + private byte tags[];
183.1349 + private Hashtable indexHashAscii = new Hashtable();
183.1350 + private String pkgPrefix = "";
183.1351 + private int pkgPrefixLen = 0;
183.1352 +
183.1353 + /**
183.1354 + * Read classfile to disassemble.
183.1355 + */
183.1356 + public ClassData(InputStream infile) throws IOException {
183.1357 + this.read(new DataInputStream(infile));
183.1358 + }
183.1359 +
183.1360 + /**
183.1361 + * Reads and stores class file information.
183.1362 + */
183.1363 + public void read(DataInputStream in) throws IOException {
183.1364 + // Read the header
183.1365 + magic = in.readInt();
183.1366 + if (magic != JAVA_MAGIC) {
183.1367 + throw new ClassFormatError("wrong magic: "
183.1368 + + toHex(magic) + ", expected "
183.1369 + + toHex(JAVA_MAGIC));
183.1370 + }
183.1371 + minor_version = in.readShort();
183.1372 + major_version = in.readShort();
183.1373 + if (major_version != JAVA_VERSION) {
183.1374 + }
183.1375 +
183.1376 + // Read the constant pool
183.1377 + readCP(in);
183.1378 + access = in.readUnsignedShort();
183.1379 + this_class = in.readUnsignedShort();
183.1380 + super_class = in.readUnsignedShort();
183.1381 +
183.1382 + //Read interfaces.
183.1383 + interfaces_count = in.readUnsignedShort();
183.1384 + if (interfaces_count > 0) {
183.1385 + interfaces = new int[interfaces_count];
183.1386 + }
183.1387 + for (int i = 0; i < interfaces_count; i++) {
183.1388 + interfaces[i] = in.readShort();
183.1389 + }
183.1390 +
183.1391 + // Read the fields
183.1392 + readFields(in);
183.1393 +
183.1394 + // Read the methods
183.1395 + readMethods(in);
183.1396 +
183.1397 + // Read the attributes
183.1398 + attributes_count = in.readUnsignedShort();
183.1399 + attrs = new AttrData[attributes_count];
183.1400 + for (int k = 0; k < attributes_count; k++) {
183.1401 + int name_cpx = in.readUnsignedShort();
183.1402 + if (getTag(name_cpx) == CONSTANT_UTF8
183.1403 + && getString(name_cpx).equals("SourceFile")) {
183.1404 + if (in.readInt() != 2) {
183.1405 + throw new ClassFormatError("invalid attr length");
183.1406 + }
183.1407 + source_cpx = in.readUnsignedShort();
183.1408 + AttrData attr = new AttrData(this);
183.1409 + attr.read(name_cpx);
183.1410 + attrs[k] = attr;
183.1411 +
183.1412 + } else if (getTag(name_cpx) == CONSTANT_UTF8
183.1413 + && getString(name_cpx).equals("InnerClasses")) {
183.1414 + int length = in.readInt();
183.1415 + int num = in.readUnsignedShort();
183.1416 + if (2 + num * 8 != length) {
183.1417 + throw new ClassFormatError("invalid attr length");
183.1418 + }
183.1419 + innerClasses = new InnerClassData[num];
183.1420 + for (int j = 0; j < num; j++) {
183.1421 + InnerClassData innerClass = new InnerClassData(this);
183.1422 + innerClass.read(in);
183.1423 + innerClasses[j] = innerClass;
183.1424 + }
183.1425 + AttrData attr = new AttrData(this);
183.1426 + attr.read(name_cpx);
183.1427 + attrs[k] = attr;
183.1428 + } else {
183.1429 + AttrData attr = new AttrData(this);
183.1430 + attr.read(name_cpx, in);
183.1431 + attrs[k] = attr;
183.1432 + }
183.1433 + }
183.1434 + in.close();
183.1435 + } // end ClassData.read()
183.1436 +
183.1437 + /**
183.1438 + * Reads and stores constant pool info.
183.1439 + */
183.1440 + void readCP(DataInputStream in) throws IOException {
183.1441 + cpool_count = in.readUnsignedShort();
183.1442 + tags = new byte[cpool_count];
183.1443 + cpool = new Object[cpool_count];
183.1444 + for (int i = 1; i < cpool_count; i++) {
183.1445 + byte tag = in.readByte();
183.1446 +
183.1447 + switch (tags[i] = tag) {
183.1448 + case CONSTANT_UTF8:
183.1449 + String str = in.readUTF();
183.1450 + indexHashAscii.put(cpool[i] = str, new Integer(i));
183.1451 + break;
183.1452 + case CONSTANT_INTEGER:
183.1453 + cpool[i] = new Integer(in.readInt());
183.1454 + break;
183.1455 + case CONSTANT_FLOAT:
183.1456 + cpool[i] = new Float(in.readFloat());
183.1457 + break;
183.1458 + case CONSTANT_LONG:
183.1459 + cpool[i++] = new Long(in.readLong());
183.1460 + break;
183.1461 + case CONSTANT_DOUBLE:
183.1462 + cpool[i++] = new Double(in.readDouble());
183.1463 + break;
183.1464 + case CONSTANT_CLASS:
183.1465 + case CONSTANT_STRING:
183.1466 + cpool[i] = new CPX(in.readUnsignedShort());
183.1467 + break;
183.1468 +
183.1469 + case CONSTANT_FIELD:
183.1470 + case CONSTANT_METHOD:
183.1471 + case CONSTANT_INTERFACEMETHOD:
183.1472 + case CONSTANT_NAMEANDTYPE:
183.1473 + cpool[i] = new CPX2(in.readUnsignedShort(), in.readUnsignedShort());
183.1474 + break;
183.1475 +
183.1476 + case 0:
183.1477 + default:
183.1478 + throw new ClassFormatError("invalid constant type: " + (int) tags[i]);
183.1479 + }
183.1480 + }
183.1481 + }
183.1482 +
183.1483 + /**
183.1484 + * Reads and strores field info.
183.1485 + */
183.1486 + protected void readFields(DataInputStream in) throws IOException {
183.1487 + int fields_count = in.readUnsignedShort();
183.1488 + fields = new FieldData[fields_count];
183.1489 + for (int k = 0; k < fields_count; k++) {
183.1490 + FieldData field = new FieldData(this);
183.1491 + field.read(in);
183.1492 + fields[k] = field;
183.1493 + }
183.1494 + }
183.1495 +
183.1496 + /**
183.1497 + * Reads and strores Method info.
183.1498 + */
183.1499 + protected void readMethods(DataInputStream in) throws IOException {
183.1500 + int methods_count = in.readUnsignedShort();
183.1501 + methods = new MethodData[methods_count];
183.1502 + for (int k = 0; k < methods_count; k++) {
183.1503 + MethodData method = new MethodData(this);
183.1504 + method.read(in);
183.1505 + methods[k] = method;
183.1506 + }
183.1507 + }
183.1508 +
183.1509 + /**
183.1510 + * get a string
183.1511 + */
183.1512 + public String getString(int n) {
183.1513 + if (n == 0) {
183.1514 + return null;
183.1515 + } else {
183.1516 + return (String) cpool[n];
183.1517 + }
183.1518 + }
183.1519 +
183.1520 + /**
183.1521 + * get the type of constant given an index
183.1522 + */
183.1523 + public byte getTag(int n) {
183.1524 + try {
183.1525 + return tags[n];
183.1526 + } catch (ArrayIndexOutOfBoundsException e) {
183.1527 + return (byte) 100;
183.1528 + }
183.1529 + }
183.1530 + static final String hexString = "0123456789ABCDEF";
183.1531 + public static char hexTable[] = hexString.toCharArray();
183.1532 +
183.1533 + static String toHex(long val, int width) {
183.1534 + StringBuffer s = new StringBuffer();
183.1535 + for (int i = width - 1; i >= 0; i--) {
183.1536 + s.append(hexTable[((int) (val >> (4 * i))) & 0xF]);
183.1537 + }
183.1538 + return "0x" + s.toString();
183.1539 + }
183.1540 +
183.1541 + static String toHex(long val) {
183.1542 + int width;
183.1543 + for (width = 16; width > 0; width--) {
183.1544 + if ((val >> (width - 1) * 4) != 0) {
183.1545 + break;
183.1546 + }
183.1547 + }
183.1548 + return toHex(val, width);
183.1549 + }
183.1550 +
183.1551 + static String toHex(int val) {
183.1552 + int width;
183.1553 + for (width = 8; width > 0; width--) {
183.1554 + if ((val >> (width - 1) * 4) != 0) {
183.1555 + break;
183.1556 + }
183.1557 + }
183.1558 + return toHex(val, width);
183.1559 + }
183.1560 +
183.1561 + /**
183.1562 + * Returns the name of this class.
183.1563 + */
183.1564 + public String getClassName() {
183.1565 + String res = null;
183.1566 + if (this_class == 0) {
183.1567 + return res;
183.1568 + }
183.1569 + int tcpx;
183.1570 + try {
183.1571 + if (tags[this_class] != CONSTANT_CLASS) {
183.1572 + return res; //"<CP["+cpx+"] is not a Class> ";
183.1573 + }
183.1574 + tcpx = ((CPX) cpool[this_class]).cpx;
183.1575 + } catch (ArrayIndexOutOfBoundsException e) {
183.1576 + return res; // "#"+cpx+"// invalid constant pool index";
183.1577 + } catch (Throwable e) {
183.1578 + return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
183.1579 + }
183.1580 +
183.1581 + try {
183.1582 + return (String) (cpool[tcpx]);
183.1583 + } catch (ArrayIndexOutOfBoundsException e) {
183.1584 + return res; // "class #"+scpx+"// invalid constant pool index";
183.1585 + } catch (ClassCastException e) {
183.1586 + return res; // "class #"+scpx+"// invalid constant pool reference";
183.1587 + } catch (Throwable e) {
183.1588 + return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
183.1589 + }
183.1590 +
183.1591 + }
183.1592 +
183.1593 + /**
183.1594 + * Returns the name of class at perticular index.
183.1595 + */
183.1596 + public String getClassName(int cpx) {
183.1597 + String res = "#" + cpx;
183.1598 + if (cpx == 0) {
183.1599 + return res;
183.1600 + }
183.1601 + int scpx;
183.1602 + try {
183.1603 + if (tags[cpx] != CONSTANT_CLASS) {
183.1604 + return res; //"<CP["+cpx+"] is not a Class> ";
183.1605 + }
183.1606 + scpx = ((CPX) cpool[cpx]).cpx;
183.1607 + } catch (ArrayIndexOutOfBoundsException e) {
183.1608 + return res; // "#"+cpx+"// invalid constant pool index";
183.1609 + } catch (Throwable e) {
183.1610 + return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
183.1611 + }
183.1612 + res = "#" + scpx;
183.1613 + try {
183.1614 + return (String) (cpool[scpx]);
183.1615 + } catch (ArrayIndexOutOfBoundsException e) {
183.1616 + return res; // "class #"+scpx+"// invalid constant pool index";
183.1617 + } catch (ClassCastException e) {
183.1618 + return res; // "class #"+scpx+"// invalid constant pool reference";
183.1619 + } catch (Throwable e) {
183.1620 + return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
183.1621 + }
183.1622 + }
183.1623 +
183.1624 + public int getAccessFlags() {
183.1625 + return access;
183.1626 + }
183.1627 +
183.1628 + /**
183.1629 + * Returns true if it is a class
183.1630 + */
183.1631 + public boolean isClass() {
183.1632 + if ((access & ACC_INTERFACE) == 0) {
183.1633 + return true;
183.1634 + }
183.1635 + return false;
183.1636 + }
183.1637 +
183.1638 + /**
183.1639 + * Returns true if it is a interface.
183.1640 + */
183.1641 + public boolean isInterface() {
183.1642 + if ((access & ACC_INTERFACE) != 0) {
183.1643 + return true;
183.1644 + }
183.1645 + return false;
183.1646 + }
183.1647 +
183.1648 + /**
183.1649 + * Returns true if this member is public, false otherwise.
183.1650 + */
183.1651 + public boolean isPublic() {
183.1652 + return (access & ACC_PUBLIC) != 0;
183.1653 + }
183.1654 +
183.1655 + /**
183.1656 + * Returns the access of this class or interface.
183.1657 + */
183.1658 + public String[] getAccess() {
183.1659 + Vector v = new Vector();
183.1660 + if ((access & ACC_PUBLIC) != 0) {
183.1661 + v.addElement("public");
183.1662 + }
183.1663 + if ((access & ACC_FINAL) != 0) {
183.1664 + v.addElement("final");
183.1665 + }
183.1666 + if ((access & ACC_ABSTRACT) != 0) {
183.1667 + v.addElement("abstract");
183.1668 + }
183.1669 + String[] accflags = new String[v.size()];
183.1670 + v.copyInto(accflags);
183.1671 + return accflags;
183.1672 + }
183.1673 +
183.1674 + /**
183.1675 + * Returns list of innerclasses.
183.1676 + */
183.1677 + public InnerClassData[] getInnerClasses() {
183.1678 + return innerClasses;
183.1679 + }
183.1680 +
183.1681 + /**
183.1682 + * Returns list of attributes.
183.1683 + */
183.1684 + final AttrData[] getAttributes() {
183.1685 + return attrs;
183.1686 + }
183.1687 +
183.1688 + public byte[] findAnnotationData(boolean classRetention) {
183.1689 + String n = classRetention
183.1690 + ? "RuntimeInvisibleAnnotations" : // NOI18N
183.1691 + "RuntimeVisibleAnnotations"; // NOI18N
183.1692 + return findAttr(n, attrs);
183.1693 + }
183.1694 +
183.1695 + /**
183.1696 + * Returns true if superbit is set.
183.1697 + */
183.1698 + public boolean isSuperSet() {
183.1699 + if ((access & ACC_SUPER) != 0) {
183.1700 + return true;
183.1701 + }
183.1702 + return false;
183.1703 + }
183.1704 +
183.1705 + /**
183.1706 + * Returns super class name.
183.1707 + */
183.1708 + public String getSuperClassName() {
183.1709 + String res = null;
183.1710 + if (super_class == 0) {
183.1711 + return res;
183.1712 + }
183.1713 + int scpx;
183.1714 + try {
183.1715 + if (tags[super_class] != CONSTANT_CLASS) {
183.1716 + return res; //"<CP["+cpx+"] is not a Class> ";
183.1717 + }
183.1718 + scpx = ((CPX) cpool[super_class]).cpx;
183.1719 + } catch (ArrayIndexOutOfBoundsException e) {
183.1720 + return res; // "#"+cpx+"// invalid constant pool index";
183.1721 + } catch (Throwable e) {
183.1722 + return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
183.1723 + }
183.1724 +
183.1725 + try {
183.1726 + return (String) (cpool[scpx]);
183.1727 + } catch (ArrayIndexOutOfBoundsException e) {
183.1728 + return res; // "class #"+scpx+"// invalid constant pool index";
183.1729 + } catch (ClassCastException e) {
183.1730 + return res; // "class #"+scpx+"// invalid constant pool reference";
183.1731 + } catch (Throwable e) {
183.1732 + return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
183.1733 + }
183.1734 + }
183.1735 +
183.1736 + /**
183.1737 + * Returns list of super interfaces.
183.1738 + */
183.1739 + public String[] getSuperInterfaces() {
183.1740 + String interfacenames[] = new String[interfaces.length];
183.1741 + int interfacecpx = -1;
183.1742 + for (int i = 0; i < interfaces.length; i++) {
183.1743 + interfacecpx = ((CPX) cpool[interfaces[i]]).cpx;
183.1744 + interfacenames[i] = (String) (cpool[interfacecpx]);
183.1745 + }
183.1746 + return interfacenames;
183.1747 + }
183.1748 +
183.1749 + /**
183.1750 + * Returns string at prticular constant pool index.
183.1751 + */
183.1752 + public String getStringValue(int cpoolx) {
183.1753 + try {
183.1754 + return ((String) cpool[cpoolx]);
183.1755 + } catch (ArrayIndexOutOfBoundsException e) {
183.1756 + return "//invalid constant pool index:" + cpoolx;
183.1757 + } catch (ClassCastException e) {
183.1758 + return "//invalid constant pool ref:" + cpoolx;
183.1759 + }
183.1760 + }
183.1761 +
183.1762 + /**
183.1763 + * Returns list of field info.
183.1764 + */
183.1765 + public FieldData[] getFields() {
183.1766 + return fields;
183.1767 + }
183.1768 +
183.1769 + /**
183.1770 + * Returns list of method info.
183.1771 + */
183.1772 + public MethodData[] getMethods() {
183.1773 + return methods;
183.1774 + }
183.1775 +
183.1776 + /**
183.1777 + * Returns constant pool entry at that index.
183.1778 + */
183.1779 + public CPX2 getCpoolEntry(int cpx) {
183.1780 + return ((CPX2) (cpool[cpx]));
183.1781 + }
183.1782 +
183.1783 + public Object getCpoolEntryobj(int cpx) {
183.1784 + return (cpool[cpx]);
183.1785 + }
183.1786 +
183.1787 + /**
183.1788 + * Returns index of this class.
183.1789 + */
183.1790 + public int getthis_cpx() {
183.1791 + return this_class;
183.1792 + }
183.1793 +
183.1794 + /**
183.1795 + * Returns string at that index.
183.1796 + */
183.1797 + public String StringValue(int cpx) {
183.1798 + return stringValue(cpx, false);
183.1799 + }
183.1800 +
183.1801 + public String stringValue(int cpx, boolean textual) {
183.1802 + return stringValue(cpx, textual, null);
183.1803 + }
183.1804 +
183.1805 + public String stringValue(int cpx, String[] classRefs) {
183.1806 + return stringValue(cpx, true, classRefs);
183.1807 + }
183.1808 +
183.1809 + private String stringValue(int cpx, boolean textual, String[] refs) {
183.1810 + if (cpx == 0) {
183.1811 + return "#0";
183.1812 + }
183.1813 + int tag;
183.1814 + Object x;
183.1815 + String suffix = "";
183.1816 + try {
183.1817 + tag = tags[cpx];
183.1818 + x = cpool[cpx];
183.1819 + } catch (IndexOutOfBoundsException e) {
183.1820 + return "<Incorrect CP index:" + cpx + ">";
183.1821 + }
183.1822 +
183.1823 + if (x == null) {
183.1824 + return "<NULL>";
183.1825 + }
183.1826 + switch (tag) {
183.1827 + case CONSTANT_UTF8: {
183.1828 + if (!textual) {
183.1829 + return (String) x;
183.1830 + }
183.1831 + StringBuilder sb = new StringBuilder();
183.1832 + String s = (String) x;
183.1833 + for (int k = 0; k < s.length(); k++) {
183.1834 + char c = s.charAt(k);
183.1835 + switch (c) {
183.1836 + case '\\':
183.1837 + sb.append('\\').append('\\');
183.1838 + break;
183.1839 + case '\t':
183.1840 + sb.append('\\').append('t');
183.1841 + break;
183.1842 + case '\n':
183.1843 + sb.append('\\').append('n');
183.1844 + break;
183.1845 + case '\r':
183.1846 + sb.append('\\').append('r');
183.1847 + break;
183.1848 + case '\"':
183.1849 + sb.append('\\').append('\"');
183.1850 + break;
183.1851 + default:
183.1852 + sb.append(c);
183.1853 + }
183.1854 + }
183.1855 + return sb.toString();
183.1856 + }
183.1857 + case CONSTANT_DOUBLE: {
183.1858 + Double d = (Double) x;
183.1859 + String sd = d.toString();
183.1860 + if (textual) {
183.1861 + return sd;
183.1862 + }
183.1863 + return sd + "d";
183.1864 + }
183.1865 + case CONSTANT_FLOAT: {
183.1866 + Float f = (Float) x;
183.1867 + String sf = (f).toString();
183.1868 + if (textual) {
183.1869 + return sf;
183.1870 + }
183.1871 + return sf + "f";
183.1872 + }
183.1873 + case CONSTANT_LONG: {
183.1874 + Long ln = (Long) x;
183.1875 + if (textual) {
183.1876 + return ln.toString();
183.1877 + }
183.1878 + return ln.toString() + 'l';
183.1879 + }
183.1880 + case CONSTANT_INTEGER: {
183.1881 + Integer in = (Integer) x;
183.1882 + return in.toString();
183.1883 + }
183.1884 + case CONSTANT_CLASS:
183.1885 + String jn = getClassName(cpx);
183.1886 + if (textual) {
183.1887 + if (refs != null) {
183.1888 + refs[0] = jn;
183.1889 + }
183.1890 + return jn;
183.1891 + }
183.1892 + return javaName(jn);
183.1893 + case CONSTANT_STRING:
183.1894 + String sv = stringValue(((CPX) x).cpx, textual);
183.1895 + if (textual) {
183.1896 + return '"' + sv + '"';
183.1897 + } else {
183.1898 + return sv;
183.1899 + }
183.1900 + case CONSTANT_FIELD:
183.1901 + case CONSTANT_METHOD:
183.1902 + case CONSTANT_INTERFACEMETHOD:
183.1903 + //return getShortClassName(((CPX2)x).cpx1)+"."+StringValue(((CPX2)x).cpx2);
183.1904 + return javaName(getClassName(((CPX2) x).cpx1)) + "." + StringValue(((CPX2) x).cpx2);
183.1905 +
183.1906 + case CONSTANT_NAMEANDTYPE:
183.1907 + return getName(((CPX2) x).cpx1) + ":" + StringValue(((CPX2) x).cpx2);
183.1908 + default:
183.1909 + return "UnknownTag"; //TBD
183.1910 + }
183.1911 + }
183.1912 +
183.1913 + /**
183.1914 + * Returns resolved java type name.
183.1915 + */
183.1916 + public String javaName(String name) {
183.1917 + if (name == null) {
183.1918 + return "null";
183.1919 + }
183.1920 + int len = name.length();
183.1921 + if (len == 0) {
183.1922 + return "\"\"";
183.1923 + }
183.1924 + int cc = '/';
183.1925 + fullname:
183.1926 + { // xxx/yyy/zzz
183.1927 + int cp;
183.1928 + for (int k = 0; k < len; k += Character.charCount(cp)) {
183.1929 + cp = name.codePointAt(k);
183.1930 + if (cc == '/') {
183.1931 + if (!isJavaIdentifierStart(cp)) {
183.1932 + break fullname;
183.1933 + }
183.1934 + } else if (cp != '/') {
183.1935 + if (!isJavaIdentifierPart(cp)) {
183.1936 + break fullname;
183.1937 + }
183.1938 + }
183.1939 + cc = cp;
183.1940 + }
183.1941 + return name;
183.1942 + }
183.1943 + return "\"" + name + "\"";
183.1944 + }
183.1945 +
183.1946 + public String getName(int cpx) {
183.1947 + String res;
183.1948 + try {
183.1949 + return javaName((String) cpool[cpx]); //.replace('/','.');
183.1950 + } catch (ArrayIndexOutOfBoundsException e) {
183.1951 + return "<invalid constant pool index:" + cpx + ">";
183.1952 + } catch (ClassCastException e) {
183.1953 + return "<invalid constant pool ref:" + cpx + ">";
183.1954 + }
183.1955 + }
183.1956 +
183.1957 + /**
183.1958 + * Returns unqualified class name.
183.1959 + */
183.1960 + public String getShortClassName(int cpx) {
183.1961 + String classname = javaName(getClassName(cpx));
183.1962 + pkgPrefixLen = classname.lastIndexOf("/") + 1;
183.1963 + if (pkgPrefixLen != 0) {
183.1964 + pkgPrefix = classname.substring(0, pkgPrefixLen);
183.1965 + if (classname.startsWith(pkgPrefix)) {
183.1966 + return classname.substring(pkgPrefixLen);
183.1967 + }
183.1968 + }
183.1969 + return classname;
183.1970 + }
183.1971 +
183.1972 + /**
183.1973 + * Returns source file name.
183.1974 + */
183.1975 + public String getSourceName() {
183.1976 + return getName(source_cpx);
183.1977 + }
183.1978 +
183.1979 + /**
183.1980 + * Returns package name.
183.1981 + */
183.1982 + public String getPkgName() {
183.1983 + String classname = getClassName(this_class);
183.1984 + pkgPrefixLen = classname.lastIndexOf("/") + 1;
183.1985 + if (pkgPrefixLen != 0) {
183.1986 + pkgPrefix = classname.substring(0, pkgPrefixLen);
183.1987 + return ("package " + pkgPrefix.substring(0, pkgPrefixLen - 1) + ";\n");
183.1988 + } else {
183.1989 + return null;
183.1990 + }
183.1991 + }
183.1992 +
183.1993 + /**
183.1994 + * Returns total constant pool entry count.
183.1995 + */
183.1996 + public int getCpoolCount() {
183.1997 + return cpool_count;
183.1998 + }
183.1999 +
183.2000 + /**
183.2001 + * Returns minor version of class file.
183.2002 + */
183.2003 + public int getMinor_version() {
183.2004 + return minor_version;
183.2005 + }
183.2006 +
183.2007 + /**
183.2008 + * Returns major version of class file.
183.2009 + */
183.2010 + public int getMajor_version() {
183.2011 + return major_version;
183.2012 + }
183.2013 +
183.2014 + private boolean isJavaIdentifierStart(int cp) {
183.2015 + return ('a' <= cp && cp <= 'z') || ('A' <= cp && cp <= 'Z');
183.2016 + }
183.2017 +
183.2018 + private boolean isJavaIdentifierPart(int cp) {
183.2019 + return isJavaIdentifierStart(cp) || ('0' <= cp && cp <= '9');
183.2020 + }
183.2021 +
183.2022 + public String[] getNameAndType(int indx) {
183.2023 + return getNameAndType(indx, 0, new String[2]);
183.2024 + }
183.2025 +
183.2026 + private String[] getNameAndType(int indx, int at, String[] arr) {
183.2027 + CPX2 c2 = getCpoolEntry(indx);
183.2028 + arr[at] = StringValue(c2.cpx1);
183.2029 + arr[at + 1] = StringValue(c2.cpx2);
183.2030 + return arr;
183.2031 + }
183.2032 +
183.2033 + public String[] getFieldInfoName(int indx) {
183.2034 + CPX2 c2 = getCpoolEntry(indx);
183.2035 + String[] arr = new String[3];
183.2036 + arr[0] = getClassName(c2.cpx1);
183.2037 + return getNameAndType(c2.cpx2, 1, arr);
183.2038 + }
183.2039 +
183.2040 + static byte[] findAttr(String n, AttrData[] attrs) {
183.2041 + for (AttrData ad : attrs) {
183.2042 + if (n.equals(ad.getAttrName())) {
183.2043 + return ad.getData();
183.2044 + }
183.2045 + }
183.2046 + return null;
183.2047 + }
183.2048 + }
183.2049 +
183.2050 + /**
183.2051 + * Strores field data informastion.
183.2052 + *
183.2053 + * @author Sucheta Dambalkar (Adopted code from jdis)
183.2054 + */
183.2055 + static class FieldData {
183.2056 +
183.2057 + ClassData cls;
183.2058 + int access;
183.2059 + int name_index;
183.2060 + int descriptor_index;
183.2061 + int attributes_count;
183.2062 + int value_cpx = 0;
183.2063 + boolean isSynthetic = false;
183.2064 + boolean isDeprecated = false;
183.2065 + Vector attrs;
183.2066 +
183.2067 + public FieldData(ClassData cls) {
183.2068 + this.cls = cls;
183.2069 + }
183.2070 +
183.2071 + /**
183.2072 + * Read and store field info.
183.2073 + */
183.2074 + public void read(DataInputStream in) throws IOException {
183.2075 + access = in.readUnsignedShort();
183.2076 + name_index = in.readUnsignedShort();
183.2077 + descriptor_index = in.readUnsignedShort();
183.2078 + // Read the attributes
183.2079 + int attributes_count = in.readUnsignedShort();
183.2080 + attrs = new Vector(attributes_count);
183.2081 + for (int i = 0; i < attributes_count; i++) {
183.2082 + int attr_name_index = in.readUnsignedShort();
183.2083 + if (cls.getTag(attr_name_index) != CONSTANT_UTF8) {
183.2084 + continue;
183.2085 + }
183.2086 + String attr_name = cls.getString(attr_name_index);
183.2087 + if (attr_name.equals("ConstantValue")) {
183.2088 + if (in.readInt() != 2) {
183.2089 + throw new ClassFormatError("invalid ConstantValue attr length");
183.2090 + }
183.2091 + value_cpx = in.readUnsignedShort();
183.2092 + AttrData attr = new AttrData(cls);
183.2093 + attr.read(attr_name_index);
183.2094 + attrs.addElement(attr);
183.2095 + } else if (attr_name.equals("Synthetic")) {
183.2096 + if (in.readInt() != 0) {
183.2097 + throw new ClassFormatError("invalid Synthetic attr length");
183.2098 + }
183.2099 + isSynthetic = true;
183.2100 + AttrData attr = new AttrData(cls);
183.2101 + attr.read(attr_name_index);
183.2102 + attrs.addElement(attr);
183.2103 + } else if (attr_name.equals("Deprecated")) {
183.2104 + if (in.readInt() != 0) {
183.2105 + throw new ClassFormatError("invalid Synthetic attr length");
183.2106 + }
183.2107 + isDeprecated = true;
183.2108 + AttrData attr = new AttrData(cls);
183.2109 + attr.read(attr_name_index);
183.2110 + attrs.addElement(attr);
183.2111 + } else {
183.2112 + AttrData attr = new AttrData(cls);
183.2113 + attr.read(attr_name_index, in);
183.2114 + attrs.addElement(attr);
183.2115 + }
183.2116 + }
183.2117 +
183.2118 + } // end read
183.2119 +
183.2120 + public boolean isStatic() {
183.2121 + return (access & ACC_STATIC) != 0;
183.2122 + }
183.2123 +
183.2124 + /**
183.2125 + * Returns access of a field.
183.2126 + */
183.2127 + public String[] getAccess() {
183.2128 + Vector v = new Vector();
183.2129 + if ((access & ACC_PUBLIC) != 0) {
183.2130 + v.addElement("public");
183.2131 + }
183.2132 + if ((access & ACC_PRIVATE) != 0) {
183.2133 + v.addElement("private");
183.2134 + }
183.2135 + if ((access & ACC_PROTECTED) != 0) {
183.2136 + v.addElement("protected");
183.2137 + }
183.2138 + if ((access & ACC_STATIC) != 0) {
183.2139 + v.addElement("static");
183.2140 + }
183.2141 + if ((access & ACC_FINAL) != 0) {
183.2142 + v.addElement("final");
183.2143 + }
183.2144 + if ((access & ACC_VOLATILE) != 0) {
183.2145 + v.addElement("volatile");
183.2146 + }
183.2147 + if ((access & ACC_TRANSIENT) != 0) {
183.2148 + v.addElement("transient");
183.2149 + }
183.2150 + String[] accflags = new String[v.size()];
183.2151 + v.copyInto(accflags);
183.2152 + return accflags;
183.2153 + }
183.2154 +
183.2155 + /**
183.2156 + * Returns name of a field.
183.2157 + */
183.2158 + public String getName() {
183.2159 + return cls.getStringValue(name_index);
183.2160 + }
183.2161 +
183.2162 + /**
183.2163 + * Returns internal signature of a field
183.2164 + */
183.2165 + public String getInternalSig() {
183.2166 + return cls.getStringValue(descriptor_index);
183.2167 + }
183.2168 +
183.2169 + /**
183.2170 + * Returns true if field is synthetic.
183.2171 + */
183.2172 + public boolean isSynthetic() {
183.2173 + return isSynthetic;
183.2174 + }
183.2175 +
183.2176 + /**
183.2177 + * Returns true if field is deprecated.
183.2178 + */
183.2179 + public boolean isDeprecated() {
183.2180 + return isDeprecated;
183.2181 + }
183.2182 +
183.2183 + /**
183.2184 + * Returns index of constant value in cpool.
183.2185 + */
183.2186 + public int getConstantValueIndex() {
183.2187 + return (value_cpx);
183.2188 + }
183.2189 +
183.2190 + /**
183.2191 + * Returns list of attributes of field.
183.2192 + */
183.2193 + public Vector getAttributes() {
183.2194 + return attrs;
183.2195 + }
183.2196 +
183.2197 + public byte[] findAnnotationData(boolean classRetention) {
183.2198 + String n = classRetention
183.2199 + ? "RuntimeInvisibleAnnotations" : // NOI18N
183.2200 + "RuntimeVisibleAnnotations"; // NOI18N
183.2201 + AttrData[] arr = new AttrData[attrs.size()];
183.2202 + attrs.copyInto(arr);
183.2203 + return ClassData.findAttr(n, arr);
183.2204 + }
183.2205 + }
183.2206 +
183.2207 + /**
183.2208 + * A JavaScript optimized replacement for Hashtable.
183.2209 + *
183.2210 + * @author Jaroslav Tulach <jtulach@netbeans.org>
183.2211 + */
183.2212 + private static final class Hashtable {
183.2213 +
183.2214 + private Object[] keys;
183.2215 + private Object[] values;
183.2216 +
183.2217 + Hashtable(int i) {
183.2218 + this();
183.2219 + }
183.2220 +
183.2221 + Hashtable(int i, double d) {
183.2222 + this();
183.2223 + }
183.2224 +
183.2225 + Hashtable() {
183.2226 + }
183.2227 +
183.2228 + synchronized void put(Object key, Object val) {
183.2229 + int[] where = {-1, -1};
183.2230 + Object found = get(key, where);
183.2231 + if (where[0] != -1) {
183.2232 + // key exists
183.2233 + values[where[0]] = val;
183.2234 + } else {
183.2235 + if (where[1] != -1) {
183.2236 + // null found
183.2237 + keys[where[1]] = key;
183.2238 + values[where[1]] = val;
183.2239 + } else {
183.2240 + if (keys == null) {
183.2241 + keys = new Object[11];
183.2242 + values = new Object[11];
183.2243 + keys[0] = key;
183.2244 + values[0] = val;
183.2245 + } else {
183.2246 + Object[] newKeys = new Object[keys.length * 2];
183.2247 + Object[] newValues = new Object[values.length * 2];
183.2248 + for (int i = 0; i < keys.length; i++) {
183.2249 + newKeys[i] = keys[i];
183.2250 + newValues[i] = values[i];
183.2251 + }
183.2252 + newKeys[keys.length] = key;
183.2253 + newValues[keys.length] = val;
183.2254 + keys = newKeys;
183.2255 + values = newValues;
183.2256 + }
183.2257 + }
183.2258 + }
183.2259 + }
183.2260 +
183.2261 + Object get(Object key) {
183.2262 + return get(key, null);
183.2263 + }
183.2264 +
183.2265 + private synchronized Object get(Object key, int[] foundAndNull) {
183.2266 + if (keys == null) {
183.2267 + return null;
183.2268 + }
183.2269 + for (int i = 0; i < keys.length; i++) {
183.2270 + if (keys[i] == null) {
183.2271 + if (foundAndNull != null) {
183.2272 + foundAndNull[1] = i;
183.2273 + }
183.2274 + } else if (keys[i].equals(key)) {
183.2275 + if (foundAndNull != null) {
183.2276 + foundAndNull[0] = i;
183.2277 + }
183.2278 + return values[i];
183.2279 + }
183.2280 + }
183.2281 + return null;
183.2282 + }
183.2283 + }
183.2284 +
183.2285 + /**
183.2286 + * Strores InnerClass data informastion.
183.2287 + *
183.2288 + * @author Sucheta Dambalkar (Adopted code from jdis)
183.2289 + */
183.2290 + private static class InnerClassData {
183.2291 +
183.2292 + ClassData cls;
183.2293 + int inner_class_info_index, outer_class_info_index, inner_name_index, access;
183.2294 +
183.2295 + public InnerClassData(ClassData cls) {
183.2296 + this.cls = cls;
183.2297 +
183.2298 + }
183.2299 +
183.2300 + /**
183.2301 + * Read Innerclass attribute data.
183.2302 + */
183.2303 + public void read(DataInputStream in) throws IOException {
183.2304 + inner_class_info_index = in.readUnsignedShort();
183.2305 + outer_class_info_index = in.readUnsignedShort();
183.2306 + inner_name_index = in.readUnsignedShort();
183.2307 + access = in.readUnsignedShort();
183.2308 + } // end read
183.2309 +
183.2310 + /**
183.2311 + * Returns the access of this class or interface.
183.2312 + */
183.2313 + public String[] getAccess() {
183.2314 + Vector v = new Vector();
183.2315 + if ((access & ACC_PUBLIC) != 0) {
183.2316 + v.addElement("public");
183.2317 + }
183.2318 + if ((access & ACC_FINAL) != 0) {
183.2319 + v.addElement("final");
183.2320 + }
183.2321 + if ((access & ACC_ABSTRACT) != 0) {
183.2322 + v.addElement("abstract");
183.2323 + }
183.2324 + String[] accflags = new String[v.size()];
183.2325 + v.copyInto(accflags);
183.2326 + return accflags;
183.2327 + }
183.2328 + } // end InnerClassData
183.2329 +
183.2330 + /**
183.2331 + * Strores LineNumberTable data information.
183.2332 + *
183.2333 + * @author Sucheta Dambalkar (Adopted code from jdis)
183.2334 + */
183.2335 + private static class LineNumData {
183.2336 +
183.2337 + short start_pc, line_number;
183.2338 +
183.2339 + public LineNumData() {
183.2340 + }
183.2341 +
183.2342 + /**
183.2343 + * Read LineNumberTable attribute.
183.2344 + */
183.2345 + public LineNumData(DataInputStream in) throws IOException {
183.2346 + start_pc = in.readShort();
183.2347 + line_number = in.readShort();
183.2348 +
183.2349 + }
183.2350 + }
183.2351 +
183.2352 + /**
183.2353 + * Strores LocalVariableTable data information.
183.2354 + *
183.2355 + * @author Sucheta Dambalkar (Adopted code from jdis)
183.2356 + */
183.2357 + private static class LocVarData {
183.2358 +
183.2359 + short start_pc, length, name_cpx, sig_cpx, slot;
183.2360 +
183.2361 + public LocVarData() {
183.2362 + }
183.2363 +
183.2364 + /**
183.2365 + * Read LocalVariableTable attribute.
183.2366 + */
183.2367 + public LocVarData(DataInputStream in) throws IOException {
183.2368 + start_pc = in.readShort();
183.2369 + length = in.readShort();
183.2370 + name_cpx = in.readShort();
183.2371 + sig_cpx = in.readShort();
183.2372 + slot = in.readShort();
183.2373 +
183.2374 + }
183.2375 + }
183.2376 + /**
183.2377 + * Strores method data informastion.
183.2378 + *
183.2379 + * @author Sucheta Dambalkar (Adopted code from jdis)
183.2380 + */
183.2381 + static class MethodData {
183.2382 +
183.2383 + ClassData cls;
183.2384 + int access;
183.2385 + int name_index;
183.2386 + int descriptor_index;
183.2387 + int attributes_count;
183.2388 + byte[] code;
183.2389 + Vector exception_table = new Vector(0);
183.2390 + Vector lin_num_tb = new Vector(0);
183.2391 + Vector loc_var_tb = new Vector(0);
183.2392 + StackMapTableData[] stackMapTable;
183.2393 + StackMapData[] stackMap;
183.2394 + int[] exc_index_table = null;
183.2395 + Vector attrs = new Vector(0);
183.2396 + Vector code_attrs = new Vector(0);
183.2397 + int max_stack, max_locals;
183.2398 + boolean isSynthetic = false;
183.2399 + boolean isDeprecated = false;
183.2400 +
183.2401 + public MethodData(ClassData cls) {
183.2402 + this.cls = cls;
183.2403 + }
183.2404 +
183.2405 + /**
183.2406 + * Read method info.
183.2407 + */
183.2408 + public void read(DataInputStream in) throws IOException {
183.2409 + access = in.readUnsignedShort();
183.2410 + name_index = in.readUnsignedShort();
183.2411 + descriptor_index = in.readUnsignedShort();
183.2412 + int attributes_count = in.readUnsignedShort();
183.2413 + for (int i = 0; i < attributes_count; i++) {
183.2414 + int attr_name_index = in.readUnsignedShort();
183.2415 +
183.2416 + readAttr:
183.2417 + {
183.2418 + if (cls.getTag(attr_name_index) == CONSTANT_UTF8) {
183.2419 + String attr_name = cls.getString(attr_name_index);
183.2420 + if (attr_name.equals("Code")) {
183.2421 + readCode(in);
183.2422 + AttrData attr = new AttrData(cls);
183.2423 + attr.read(attr_name_index);
183.2424 + attrs.addElement(attr);
183.2425 + break readAttr;
183.2426 + } else if (attr_name.equals("Exceptions")) {
183.2427 + readExceptions(in);
183.2428 + AttrData attr = new AttrData(cls);
183.2429 + attr.read(attr_name_index);
183.2430 + attrs.addElement(attr);
183.2431 + break readAttr;
183.2432 + } else if (attr_name.equals("Synthetic")) {
183.2433 + if (in.readInt() != 0) {
183.2434 + throw new ClassFormatError("invalid Synthetic attr length");
183.2435 + }
183.2436 + isSynthetic = true;
183.2437 + AttrData attr = new AttrData(cls);
183.2438 + attr.read(attr_name_index);
183.2439 + attrs.addElement(attr);
183.2440 + break readAttr;
183.2441 + } else if (attr_name.equals("Deprecated")) {
183.2442 + if (in.readInt() != 0) {
183.2443 + throw new ClassFormatError("invalid Synthetic attr length");
183.2444 + }
183.2445 + isDeprecated = true;
183.2446 + AttrData attr = new AttrData(cls);
183.2447 + attr.read(attr_name_index);
183.2448 + attrs.addElement(attr);
183.2449 + break readAttr;
183.2450 + }
183.2451 + }
183.2452 + AttrData attr = new AttrData(cls);
183.2453 + attr.read(attr_name_index, in);
183.2454 + attrs.addElement(attr);
183.2455 + }
183.2456 + }
183.2457 + }
183.2458 +
183.2459 + /**
183.2460 + * Read code attribute info.
183.2461 + */
183.2462 + public void readCode(DataInputStream in) throws IOException {
183.2463 +
183.2464 + int attr_length = in.readInt();
183.2465 + max_stack = in.readUnsignedShort();
183.2466 + max_locals = in.readUnsignedShort();
183.2467 + int codelen = in.readInt();
183.2468 +
183.2469 + code = new byte[codelen];
183.2470 + int totalread = 0;
183.2471 + while (totalread < codelen) {
183.2472 + totalread += in.read(code, totalread, codelen - totalread);
183.2473 + }
183.2474 + // in.read(code, 0, codelen);
183.2475 + int clen = 0;
183.2476 + readExceptionTable(in);
183.2477 + int code_attributes_count = in.readUnsignedShort();
183.2478 +
183.2479 + for (int k = 0; k < code_attributes_count; k++) {
183.2480 + int table_name_index = in.readUnsignedShort();
183.2481 + int table_name_tag = cls.getTag(table_name_index);
183.2482 + AttrData attr = new AttrData(cls);
183.2483 + if (table_name_tag == CONSTANT_UTF8) {
183.2484 + String table_name_tstr = cls.getString(table_name_index);
183.2485 + if (table_name_tstr.equals("LineNumberTable")) {
183.2486 + readLineNumTable(in);
183.2487 + attr.read(table_name_index);
183.2488 + } else if (table_name_tstr.equals("LocalVariableTable")) {
183.2489 + readLocVarTable(in);
183.2490 + attr.read(table_name_index);
183.2491 + } else if (table_name_tstr.equals("StackMapTable")) {
183.2492 + readStackMapTable(in);
183.2493 + attr.read(table_name_index);
183.2494 + } else if (table_name_tstr.equals("StackMap")) {
183.2495 + readStackMap(in);
183.2496 + attr.read(table_name_index);
183.2497 + } else {
183.2498 + attr.read(table_name_index, in);
183.2499 + }
183.2500 + code_attrs.addElement(attr);
183.2501 + continue;
183.2502 + }
183.2503 +
183.2504 + attr.read(table_name_index, in);
183.2505 + code_attrs.addElement(attr);
183.2506 + }
183.2507 + }
183.2508 +
183.2509 + /**
183.2510 + * Read exception table info.
183.2511 + */
183.2512 + void readExceptionTable(DataInputStream in) throws IOException {
183.2513 + int exception_table_len = in.readUnsignedShort();
183.2514 + exception_table = new Vector(exception_table_len);
183.2515 + for (int l = 0; l < exception_table_len; l++) {
183.2516 + exception_table.addElement(new TrapData(in, l));
183.2517 + }
183.2518 + }
183.2519 +
183.2520 + /**
183.2521 + * Read LineNumberTable attribute info.
183.2522 + */
183.2523 + void readLineNumTable(DataInputStream in) throws IOException {
183.2524 + int attr_len = in.readInt(); // attr_length
183.2525 + int lin_num_tb_len = in.readUnsignedShort();
183.2526 + lin_num_tb = new Vector(lin_num_tb_len);
183.2527 + for (int l = 0; l < lin_num_tb_len; l++) {
183.2528 + lin_num_tb.addElement(new LineNumData(in));
183.2529 + }
183.2530 + }
183.2531 +
183.2532 + /**
183.2533 + * Read LocalVariableTable attribute info.
183.2534 + */
183.2535 + void readLocVarTable(DataInputStream in) throws IOException {
183.2536 + int attr_len = in.readInt(); // attr_length
183.2537 + int loc_var_tb_len = in.readUnsignedShort();
183.2538 + loc_var_tb = new Vector(loc_var_tb_len);
183.2539 + for (int l = 0; l < loc_var_tb_len; l++) {
183.2540 + loc_var_tb.addElement(new LocVarData(in));
183.2541 + }
183.2542 + }
183.2543 +
183.2544 + /**
183.2545 + * Read Exception attribute info.
183.2546 + */
183.2547 + public void readExceptions(DataInputStream in) throws IOException {
183.2548 + int attr_len = in.readInt(); // attr_length in prog
183.2549 + int num_exceptions = in.readUnsignedShort();
183.2550 + exc_index_table = new int[num_exceptions];
183.2551 + for (int l = 0; l < num_exceptions; l++) {
183.2552 + int exc = in.readShort();
183.2553 + exc_index_table[l] = exc;
183.2554 + }
183.2555 + }
183.2556 +
183.2557 + /**
183.2558 + * Read StackMapTable attribute info.
183.2559 + */
183.2560 + void readStackMapTable(DataInputStream in) throws IOException {
183.2561 + int attr_len = in.readInt(); //attr_length
183.2562 + int stack_map_tb_len = in.readUnsignedShort();
183.2563 + stackMapTable = new StackMapTableData[stack_map_tb_len];
183.2564 + for (int i = 0; i < stack_map_tb_len; i++) {
183.2565 + stackMapTable[i] = StackMapTableData.getInstance(in, this);
183.2566 + }
183.2567 + }
183.2568 +
183.2569 + /**
183.2570 + * Read StackMap attribute info.
183.2571 + */
183.2572 + void readStackMap(DataInputStream in) throws IOException {
183.2573 + int attr_len = in.readInt(); //attr_length
183.2574 + int stack_map_len = in.readUnsignedShort();
183.2575 + stackMap = new StackMapData[stack_map_len];
183.2576 + for (int i = 0; i < stack_map_len; i++) {
183.2577 + stackMap[i] = new StackMapData(in, this);
183.2578 + }
183.2579 + }
183.2580 +
183.2581 + /**
183.2582 + * Return access of the method.
183.2583 + */
183.2584 + public int getAccess() {
183.2585 + return access;
183.2586 + }
183.2587 +
183.2588 + /**
183.2589 + * Return name of the method.
183.2590 + */
183.2591 + public String getName() {
183.2592 + return cls.getStringValue(name_index);
183.2593 + }
183.2594 +
183.2595 + /**
183.2596 + * Return internal siganature of the method.
183.2597 + */
183.2598 + public String getInternalSig() {
183.2599 + return cls.getStringValue(descriptor_index);
183.2600 + }
183.2601 +
183.2602 + /**
183.2603 + * Return code attribute data of a method.
183.2604 + */
183.2605 + public byte[] getCode() {
183.2606 + return code;
183.2607 + }
183.2608 +
183.2609 + /**
183.2610 + * Return LineNumberTable size.
183.2611 + */
183.2612 + public int getnumlines() {
183.2613 + return lin_num_tb.size();
183.2614 + }
183.2615 +
183.2616 + /**
183.2617 + * Return LineNumberTable
183.2618 + */
183.2619 + public Vector getlin_num_tb() {
183.2620 + return lin_num_tb;
183.2621 + }
183.2622 +
183.2623 + /**
183.2624 + * Return LocalVariableTable size.
183.2625 + */
183.2626 + public int getloc_var_tbsize() {
183.2627 + return loc_var_tb.size();
183.2628 + }
183.2629 +
183.2630 + /**
183.2631 + * Return LocalVariableTable.
183.2632 + */
183.2633 + public Vector getloc_var_tb() {
183.2634 + return loc_var_tb;
183.2635 + }
183.2636 +
183.2637 + /**
183.2638 + * Return StackMap.
183.2639 + */
183.2640 + public StackMapData[] getStackMap() {
183.2641 + return stackMap;
183.2642 + }
183.2643 +
183.2644 + /**
183.2645 + * Return StackMapTable.
183.2646 + */
183.2647 + public StackMapTableData[] getStackMapTable() {
183.2648 + return stackMapTable;
183.2649 + }
183.2650 +
183.2651 + public StackMapIterator createStackMapIterator() {
183.2652 + return new StackMapIterator(this);
183.2653 + }
183.2654 +
183.2655 + /**
183.2656 + * Return true if method is static
183.2657 + */
183.2658 + public boolean isStatic() {
183.2659 + if ((access & ACC_STATIC) != 0) {
183.2660 + return true;
183.2661 + }
183.2662 + return false;
183.2663 + }
183.2664 +
183.2665 + /**
183.2666 + * Return max depth of operand stack.
183.2667 + */
183.2668 + public int getMaxStack() {
183.2669 + return max_stack;
183.2670 + }
183.2671 +
183.2672 + /**
183.2673 + * Return number of local variables.
183.2674 + */
183.2675 + public int getMaxLocals() {
183.2676 + return max_locals;
183.2677 + }
183.2678 +
183.2679 + /**
183.2680 + * Return exception index table in Exception attribute.
183.2681 + */
183.2682 + public int[] get_exc_index_table() {
183.2683 + return exc_index_table;
183.2684 + }
183.2685 +
183.2686 + /**
183.2687 + * Return exception table in code attributre.
183.2688 + */
183.2689 + public TrapDataIterator getTrapDataIterator() {
183.2690 + return new TrapDataIterator(exception_table);
183.2691 + }
183.2692 +
183.2693 + /**
183.2694 + * Return method attributes.
183.2695 + */
183.2696 + public Vector getAttributes() {
183.2697 + return attrs;
183.2698 + }
183.2699 +
183.2700 + /**
183.2701 + * Return code attributes.
183.2702 + */
183.2703 + public Vector getCodeAttributes() {
183.2704 + return code_attrs;
183.2705 + }
183.2706 +
183.2707 + /**
183.2708 + * Return true if method id synthetic.
183.2709 + */
183.2710 + public boolean isSynthetic() {
183.2711 + return isSynthetic;
183.2712 + }
183.2713 +
183.2714 + /**
183.2715 + * Return true if method is deprecated.
183.2716 + */
183.2717 + public boolean isDeprecated() {
183.2718 + return isDeprecated;
183.2719 + }
183.2720 +
183.2721 + public byte[] findAnnotationData(boolean classRetention) {
183.2722 + String n = classRetention
183.2723 + ? "RuntimeInvisibleAnnotations" : // NOI18N
183.2724 + "RuntimeVisibleAnnotations"; // NOI18N
183.2725 + AttrData[] arr = new AttrData[attrs.size()];
183.2726 + attrs.copyInto(arr);
183.2727 + return ClassData.findAttr(n, arr);
183.2728 + }
183.2729 +
183.2730 + public boolean isConstructor() {
183.2731 + return "<init>".equals(getName());
183.2732 + }
183.2733 + }
183.2734 +
183.2735 + /* represents one entry of StackMap attribute
183.2736 + */
183.2737 + private static class StackMapData {
183.2738 +
183.2739 + final int offset;
183.2740 + final int[] locals;
183.2741 + final int[] stack;
183.2742 +
183.2743 + StackMapData(int offset, int[] locals, int[] stack) {
183.2744 + this.offset = offset;
183.2745 + this.locals = locals;
183.2746 + this.stack = stack;
183.2747 + }
183.2748 +
183.2749 + StackMapData(DataInputStream in, MethodData method) throws IOException {
183.2750 + offset = in.readUnsignedShort();
183.2751 + int local_size = in.readUnsignedShort();
183.2752 + locals = readTypeArray(in, local_size, method);
183.2753 + int stack_size = in.readUnsignedShort();
183.2754 + stack = readTypeArray(in, stack_size, method);
183.2755 + }
183.2756 +
183.2757 + static final int[] readTypeArray(DataInputStream in, int length, MethodData method) throws IOException {
183.2758 + int[] types = new int[length];
183.2759 + for (int i = 0; i < length; i++) {
183.2760 + types[i] = readType(in, method);
183.2761 + }
183.2762 + return types;
183.2763 + }
183.2764 +
183.2765 + static final int readType(DataInputStream in, MethodData method) throws IOException {
183.2766 + int type = in.readUnsignedByte();
183.2767 + if (type == ITEM_Object || type == ITEM_NewObject) {
183.2768 + type = type | (in.readUnsignedShort() << 8);
183.2769 + }
183.2770 + return type;
183.2771 + }
183.2772 + }
183.2773 +
183.2774 + static final class StackMapIterator {
183.2775 +
183.2776 + private final StackMapTableData[] stackMapTable;
183.2777 + private final TypeArray argTypes;
183.2778 + private final TypeArray localTypes;
183.2779 + private final TypeArray stackTypes;
183.2780 + private int nextFrameIndex;
183.2781 + private int lastFrameByteCodeOffset;
183.2782 + private int byteCodeOffset;
183.2783 +
183.2784 + StackMapIterator(final MethodData methodData) {
183.2785 + this(methodData.getStackMapTable(),
183.2786 + methodData.getInternalSig(),
183.2787 + methodData.isStatic());
183.2788 + }
183.2789 +
183.2790 + StackMapIterator(final StackMapTableData[] stackMapTable,
183.2791 + final String methodSignature,
183.2792 + final boolean isStaticMethod) {
183.2793 + this.stackMapTable = (stackMapTable != null)
183.2794 + ? stackMapTable
183.2795 + : new StackMapTableData[0];
183.2796 +
183.2797 + argTypes = getArgTypes(methodSignature, isStaticMethod);
183.2798 + localTypes = new TypeArray();
183.2799 + stackTypes = new TypeArray();
183.2800 +
183.2801 + localTypes.addAll(argTypes);
183.2802 +
183.2803 + lastFrameByteCodeOffset = -1;
183.2804 + advanceBy(0);
183.2805 + }
183.2806 +
183.2807 + public String getFrameAsString() {
183.2808 + return (nextFrameIndex == 0)
183.2809 + ? StackMapTableData.toString("INITIAL", 0, null, null)
183.2810 + : stackMapTable[nextFrameIndex - 1].toString();
183.2811 + }
183.2812 +
183.2813 + public int getFrameIndex() {
183.2814 + return nextFrameIndex;
183.2815 + }
183.2816 +
183.2817 + public TypeArray getFrameStack() {
183.2818 + return stackTypes;
183.2819 + }
183.2820 +
183.2821 + public TypeArray getFrameLocals() {
183.2822 + return localTypes;
183.2823 + }
183.2824 +
183.2825 + public TypeArray getArguments() {
183.2826 + return argTypes;
183.2827 + }
183.2828 +
183.2829 + public void advanceBy(final int numByteCodes) {
183.2830 + if (numByteCodes < 0) {
183.2831 + throw new IllegalStateException("Forward only iterator");
183.2832 + }
183.2833 +
183.2834 + byteCodeOffset += numByteCodes;
183.2835 + while ((nextFrameIndex < stackMapTable.length)
183.2836 + && ((byteCodeOffset - lastFrameByteCodeOffset)
183.2837 + >= (stackMapTable[nextFrameIndex].offsetDelta
183.2838 + + 1))) {
183.2839 + final StackMapTableData nextFrame = stackMapTable[nextFrameIndex];
183.2840 +
183.2841 + lastFrameByteCodeOffset += nextFrame.offsetDelta + 1;
183.2842 + nextFrame.applyTo(localTypes, stackTypes);
183.2843 +
183.2844 + ++nextFrameIndex;
183.2845 + }
183.2846 + }
183.2847 +
183.2848 + public void advanceTo(final int nextByteCodeOffset) {
183.2849 + advanceBy(nextByteCodeOffset - byteCodeOffset);
183.2850 + }
183.2851 +
183.2852 + private static TypeArray getArgTypes(final String methodSignature,
183.2853 + final boolean isStaticMethod) {
183.2854 + final TypeArray argTypes = new TypeArray();
183.2855 +
183.2856 + if (!isStaticMethod) {
183.2857 + argTypes.add(ITEM_Object);
183.2858 + }
183.2859 +
183.2860 + if (methodSignature.charAt(0) != '(') {
183.2861 + throw new IllegalArgumentException("Invalid method signature");
183.2862 + }
183.2863 +
183.2864 + final int length = methodSignature.length();
183.2865 + boolean skipType = false;
183.2866 + int argType;
183.2867 + for (int i = 1; i < length; ++i) {
183.2868 + switch (methodSignature.charAt(i)) {
183.2869 + case 'B':
183.2870 + case 'C':
183.2871 + case 'S':
183.2872 + case 'Z':
183.2873 + case 'I':
183.2874 + argType = ITEM_Integer;
183.2875 + break;
183.2876 + case 'J':
183.2877 + argType = ITEM_Long;
183.2878 + break;
183.2879 + case 'F':
183.2880 + argType = ITEM_Float;
183.2881 + break;
183.2882 + case 'D':
183.2883 + argType = ITEM_Double;
183.2884 + break;
183.2885 + case 'L': {
183.2886 + i = methodSignature.indexOf(';', i + 1);
183.2887 + if (i == -1) {
183.2888 + throw new IllegalArgumentException(
183.2889 + "Invalid method signature");
183.2890 + }
183.2891 + argType = ITEM_Object;
183.2892 + break;
183.2893 + }
183.2894 + case ')':
183.2895 + // not interested in the return value type
183.2896 + return argTypes;
183.2897 + case '[':
183.2898 + if (!skipType) {
183.2899 + argTypes.add(ITEM_Object);
183.2900 + skipType = true;
183.2901 + }
183.2902 + continue;
183.2903 +
183.2904 + default:
183.2905 + throw new IllegalArgumentException(
183.2906 + "Invalid method signature");
183.2907 + }
183.2908 +
183.2909 + if (!skipType) {
183.2910 + argTypes.add(argType);
183.2911 + } else {
183.2912 + skipType = false;
183.2913 + }
183.2914 + }
183.2915 +
183.2916 + return argTypes;
183.2917 + }
183.2918 + }
183.2919 + /* represents one entry of StackMapTable attribute
183.2920 + */
183.2921 +
183.2922 + private static abstract class StackMapTableData {
183.2923 +
183.2924 + final int frameType;
183.2925 + int offsetDelta;
183.2926 +
183.2927 + StackMapTableData(int frameType) {
183.2928 + this.frameType = frameType;
183.2929 + }
183.2930 +
183.2931 + abstract void applyTo(TypeArray localTypes, TypeArray stackTypes);
183.2932 +
183.2933 + protected static String toString(
183.2934 + final String frameType,
183.2935 + final int offset,
183.2936 + final int[] localTypes,
183.2937 + final int[] stackTypes) {
183.2938 + final StringBuilder sb = new StringBuilder(frameType);
183.2939 +
183.2940 + sb.append("(off: +").append(offset);
183.2941 + if (localTypes != null) {
183.2942 + sb.append(", locals: ");
183.2943 + appendTypes(sb, localTypes);
183.2944 + }
183.2945 + if (stackTypes != null) {
183.2946 + sb.append(", stack: ");
183.2947 + appendTypes(sb, stackTypes);
183.2948 + }
183.2949 + sb.append(')');
183.2950 +
183.2951 + return sb.toString();
183.2952 + }
183.2953 +
183.2954 + private static void appendTypes(final StringBuilder sb, final int[] types) {
183.2955 + sb.append('[');
183.2956 + if (types.length > 0) {
183.2957 + sb.append(TypeArray.typeString(types[0]));
183.2958 + for (int i = 1; i < types.length; ++i) {
183.2959 + sb.append(", ");
183.2960 + sb.append(TypeArray.typeString(types[i]));
183.2961 + }
183.2962 + }
183.2963 + sb.append(']');
183.2964 + }
183.2965 +
183.2966 + private static class SameFrame extends StackMapTableData {
183.2967 +
183.2968 + SameFrame(int frameType, int offsetDelta) {
183.2969 + super(frameType);
183.2970 + this.offsetDelta = offsetDelta;
183.2971 + }
183.2972 +
183.2973 + @Override
183.2974 + void applyTo(TypeArray localTypes, TypeArray stackTypes) {
183.2975 + stackTypes.clear();
183.2976 + }
183.2977 +
183.2978 + @Override
183.2979 + public String toString() {
183.2980 + return toString("SAME" + ((frameType == SAME_FRAME_EXTENDED)
183.2981 + ? "_FRAME_EXTENDED" : ""),
183.2982 + offsetDelta,
183.2983 + null, null);
183.2984 + }
183.2985 + }
183.2986 +
183.2987 + private static class SameLocals1StackItem extends StackMapTableData {
183.2988 +
183.2989 + final int[] stack;
183.2990 +
183.2991 + SameLocals1StackItem(int frameType, int offsetDelta, int[] stack) {
183.2992 + super(frameType);
183.2993 + this.offsetDelta = offsetDelta;
183.2994 + this.stack = stack;
183.2995 + }
183.2996 +
183.2997 + @Override
183.2998 + void applyTo(TypeArray localTypes, TypeArray stackTypes) {
183.2999 + stackTypes.setAll(stack);
183.3000 + }
183.3001 +
183.3002 + @Override
183.3003 + public String toString() {
183.3004 + return toString(
183.3005 + "SAME_LOCALS_1_STACK_ITEM"
183.3006 + + ((frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED)
183.3007 + ? "_EXTENDED" : ""),
183.3008 + offsetDelta,
183.3009 + null, stack);
183.3010 + }
183.3011 + }
183.3012 +
183.3013 + private static class ChopFrame extends StackMapTableData {
183.3014 +
183.3015 + ChopFrame(int frameType, int offsetDelta) {
183.3016 + super(frameType);
183.3017 + this.offsetDelta = offsetDelta;
183.3018 + }
183.3019 +
183.3020 + @Override
183.3021 + void applyTo(TypeArray localTypes, TypeArray stackTypes) {
183.3022 + localTypes.setSize(localTypes.getSize()
183.3023 + - (SAME_FRAME_EXTENDED - frameType));
183.3024 + stackTypes.clear();
183.3025 + }
183.3026 +
183.3027 + @Override
183.3028 + public String toString() {
183.3029 + return toString("CHOP", offsetDelta, null, null);
183.3030 + }
183.3031 + }
183.3032 +
183.3033 + private static class AppendFrame extends StackMapTableData {
183.3034 +
183.3035 + final int[] locals;
183.3036 +
183.3037 + AppendFrame(int frameType, int offsetDelta, int[] locals) {
183.3038 + super(frameType);
183.3039 + this.offsetDelta = offsetDelta;
183.3040 + this.locals = locals;
183.3041 + }
183.3042 +
183.3043 + @Override
183.3044 + void applyTo(TypeArray localTypes, TypeArray stackTypes) {
183.3045 + localTypes.addAll(locals);
183.3046 + stackTypes.clear();
183.3047 + }
183.3048 +
183.3049 + @Override
183.3050 + public String toString() {
183.3051 + return toString("APPEND", offsetDelta, locals, null);
183.3052 + }
183.3053 + }
183.3054 +
183.3055 + private static class FullFrame extends StackMapTableData {
183.3056 +
183.3057 + final int[] locals;
183.3058 + final int[] stack;
183.3059 +
183.3060 + FullFrame(int offsetDelta, int[] locals, int[] stack) {
183.3061 + super(FULL_FRAME);
183.3062 + this.offsetDelta = offsetDelta;
183.3063 + this.locals = locals;
183.3064 + this.stack = stack;
183.3065 + }
183.3066 +
183.3067 + @Override
183.3068 + void applyTo(TypeArray localTypes, TypeArray stackTypes) {
183.3069 + localTypes.setAll(locals);
183.3070 + stackTypes.setAll(stack);
183.3071 + }
183.3072 +
183.3073 + @Override
183.3074 + public String toString() {
183.3075 + return toString("FULL", offsetDelta, locals, stack);
183.3076 + }
183.3077 + }
183.3078 +
183.3079 + static StackMapTableData getInstance(DataInputStream in, MethodData method)
183.3080 + throws IOException {
183.3081 + int frameType = in.readUnsignedByte();
183.3082 +
183.3083 + if (frameType < SAME_FRAME_BOUND) {
183.3084 + // same_frame
183.3085 + return new SameFrame(frameType, frameType);
183.3086 + } else if (SAME_FRAME_BOUND <= frameType && frameType < SAME_LOCALS_1_STACK_ITEM_BOUND) {
183.3087 + // same_locals_1_stack_item_frame
183.3088 + // read additional single stack element
183.3089 + return new SameLocals1StackItem(frameType,
183.3090 + (frameType - SAME_FRAME_BOUND),
183.3091 + StackMapData.readTypeArray(in, 1, method));
183.3092 + } else if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
183.3093 + // same_locals_1_stack_item_extended
183.3094 + return new SameLocals1StackItem(frameType,
183.3095 + in.readUnsignedShort(),
183.3096 + StackMapData.readTypeArray(in, 1, method));
183.3097 + } else if (SAME_LOCALS_1_STACK_ITEM_EXTENDED < frameType && frameType < SAME_FRAME_EXTENDED) {
183.3098 + // chop_frame or same_frame_extended
183.3099 + return new ChopFrame(frameType, in.readUnsignedShort());
183.3100 + } else if (frameType == SAME_FRAME_EXTENDED) {
183.3101 + // chop_frame or same_frame_extended
183.3102 + return new SameFrame(frameType, in.readUnsignedShort());
183.3103 + } else if (SAME_FRAME_EXTENDED < frameType && frameType < FULL_FRAME) {
183.3104 + // append_frame
183.3105 + return new AppendFrame(frameType, in.readUnsignedShort(),
183.3106 + StackMapData.readTypeArray(in, frameType - SAME_FRAME_EXTENDED, method));
183.3107 + } else if (frameType == FULL_FRAME) {
183.3108 + // full_frame
183.3109 + int offsetDelta = in.readUnsignedShort();
183.3110 + int locals_size = in.readUnsignedShort();
183.3111 + int[] locals = StackMapData.readTypeArray(in, locals_size, method);
183.3112 + int stack_size = in.readUnsignedShort();
183.3113 + int[] stack = StackMapData.readTypeArray(in, stack_size, method);
183.3114 + return new FullFrame(offsetDelta, locals, stack);
183.3115 + } else {
183.3116 + throw new ClassFormatError("unrecognized frame_type in StackMapTable");
183.3117 + }
183.3118 + }
183.3119 + }
183.3120 +
183.3121 + /**
183.3122 + * Stores exception table data in code attribute.
183.3123 + *
183.3124 + * @author Sucheta Dambalkar (Adopted code from jdis)
183.3125 + */
183.3126 + static final class TrapData {
183.3127 +
183.3128 + public final short start_pc;
183.3129 + public final short end_pc;
183.3130 + public final short handler_pc;
183.3131 + public final short catch_cpx;
183.3132 + final int num;
183.3133 +
183.3134 + /**
183.3135 + * Read and store exception table data in code attribute.
183.3136 + */
183.3137 + TrapData(DataInputStream in, int num) throws IOException {
183.3138 + this.num = num;
183.3139 + start_pc = in.readShort();
183.3140 + end_pc = in.readShort();
183.3141 + handler_pc = in.readShort();
183.3142 + catch_cpx = in.readShort();
183.3143 + }
183.3144 +
183.3145 + /**
183.3146 + * returns recommended identifier
183.3147 + */
183.3148 + public String ident() {
183.3149 + return "t" + num;
183.3150 + }
183.3151 + }
183.3152 + /**
183.3153 + *
183.3154 + * @author Jaroslav Tulach <jtulach@netbeans.org>
183.3155 + */
183.3156 + static final class TrapDataIterator {
183.3157 +
183.3158 + private final Hashtable exStart = new Hashtable();
183.3159 + private final Hashtable exStop = new Hashtable();
183.3160 + private TrapData[] current = new TrapData[10];
183.3161 + private int currentCount;
183.3162 +
183.3163 + TrapDataIterator(Vector exceptionTable) {
183.3164 + for (int i = 0; i < exceptionTable.size(); i++) {
183.3165 + final TrapData td = (TrapData) exceptionTable.elementAt(i);
183.3166 + put(exStart, td.start_pc, td);
183.3167 + put(exStop, td.end_pc, td);
183.3168 + }
183.3169 + }
183.3170 +
183.3171 + private static void put(Hashtable h, short key, TrapData td) {
183.3172 + Short s = Short.valueOf((short) key);
183.3173 + Vector v = (Vector) h.get(s);
183.3174 + if (v == null) {
183.3175 + v = new Vector(1);
183.3176 + h.put(s, v);
183.3177 + }
183.3178 + v.add(td);
183.3179 + }
183.3180 +
183.3181 + private boolean processAll(Hashtable h, Short key, boolean add) {
183.3182 + boolean change = false;
183.3183 + Vector v = (Vector) h.get(key);
183.3184 + if (v != null) {
183.3185 + int s = v.size();
183.3186 + for (int i = 0; i < s; i++) {
183.3187 + TrapData td = (TrapData) v.elementAt(i);
183.3188 + if (add) {
183.3189 + add(td);
183.3190 + change = true;
183.3191 + } else {
183.3192 + remove(td);
183.3193 + change = true;
183.3194 + }
183.3195 + }
183.3196 + }
183.3197 + return change;
183.3198 + }
183.3199 +
183.3200 + public boolean advanceTo(int i) {
183.3201 + Short s = Short.valueOf((short) i);
183.3202 + boolean ch1 = processAll(exStart, s, true);
183.3203 + boolean ch2 = processAll(exStop, s, false);
183.3204 + return ch1 || ch2;
183.3205 + }
183.3206 +
183.3207 + public boolean useTry() {
183.3208 + return currentCount > 0;
183.3209 + }
183.3210 +
183.3211 + public TrapData[] current() {
183.3212 + TrapData[] copy = new TrapData[currentCount];
183.3213 + for (int i = 0; i < currentCount; i++) {
183.3214 + copy[i] = current[i];
183.3215 + }
183.3216 + return copy;
183.3217 + }
183.3218 +
183.3219 + private void add(TrapData e) {
183.3220 + if (currentCount == current.length) {
183.3221 + TrapData[] data = new TrapData[currentCount * 2];
183.3222 + for (int i = 0; i < currentCount; i++) {
183.3223 + data[i] = current[i];
183.3224 + }
183.3225 + current = data;
183.3226 + }
183.3227 + current[currentCount++] = e;
183.3228 + }
183.3229 +
183.3230 + private void remove(TrapData e) {
183.3231 + if (currentCount == 0) {
183.3232 + return;
183.3233 + }
183.3234 + int from = 0;
183.3235 + while (from < currentCount) {
183.3236 + if (e == current[from++]) {
183.3237 + break;
183.3238 + }
183.3239 + }
183.3240 + while (from < currentCount) {
183.3241 + current[from - 1] = current[from];
183.3242 + current[from] = null;
183.3243 + from++;
183.3244 + }
183.3245 + currentCount--;
183.3246 + }
183.3247 + }
183.3248 + static final class TypeArray {
183.3249 +
183.3250 + private static final int CAPACITY_INCREMENT = 16;
183.3251 + private int[] types;
183.3252 + private int size;
183.3253 +
183.3254 + public TypeArray() {
183.3255 + }
183.3256 +
183.3257 + public TypeArray(final TypeArray initialTypes) {
183.3258 + setAll(initialTypes);
183.3259 + }
183.3260 +
183.3261 + public void add(final int newType) {
183.3262 + ensureCapacity(size + 1);
183.3263 + types[size++] = newType;
183.3264 + }
183.3265 +
183.3266 + public void addAll(final TypeArray newTypes) {
183.3267 + addAll(newTypes.types, 0, newTypes.size);
183.3268 + }
183.3269 +
183.3270 + public void addAll(final int[] newTypes) {
183.3271 + addAll(newTypes, 0, newTypes.length);
183.3272 + }
183.3273 +
183.3274 + public void addAll(final int[] newTypes,
183.3275 + final int offset,
183.3276 + final int count) {
183.3277 + if (count > 0) {
183.3278 + ensureCapacity(size + count);
183.3279 + arraycopy(newTypes, offset, types, size, count);
183.3280 + size += count;
183.3281 + }
183.3282 + }
183.3283 +
183.3284 + public void set(final int index, final int newType) {
183.3285 + types[index] = newType;
183.3286 + }
183.3287 +
183.3288 + public void setAll(final TypeArray newTypes) {
183.3289 + setAll(newTypes.types, 0, newTypes.size);
183.3290 + }
183.3291 +
183.3292 + public void setAll(final int[] newTypes) {
183.3293 + setAll(newTypes, 0, newTypes.length);
183.3294 + }
183.3295 +
183.3296 + public void setAll(final int[] newTypes,
183.3297 + final int offset,
183.3298 + final int count) {
183.3299 + if (count > 0) {
183.3300 + ensureCapacity(count);
183.3301 + arraycopy(newTypes, offset, types, 0, count);
183.3302 + size = count;
183.3303 + } else {
183.3304 + clear();
183.3305 + }
183.3306 + }
183.3307 +
183.3308 + public void setSize(final int newSize) {
183.3309 + if (size != newSize) {
183.3310 + ensureCapacity(newSize);
183.3311 +
183.3312 + for (int i = size; i < newSize; ++i) {
183.3313 + types[i] = 0;
183.3314 + }
183.3315 + size = newSize;
183.3316 + }
183.3317 + }
183.3318 +
183.3319 + public void clear() {
183.3320 + size = 0;
183.3321 + }
183.3322 +
183.3323 + public int getSize() {
183.3324 + return size;
183.3325 + }
183.3326 +
183.3327 + public int get(final int index) {
183.3328 + return types[index];
183.3329 + }
183.3330 +
183.3331 + public static String typeString(final int type) {
183.3332 + switch (type & 0xff) {
183.3333 + case ITEM_Bogus:
183.3334 + return "_top_";
183.3335 + case ITEM_Integer:
183.3336 + return "_int_";
183.3337 + case ITEM_Float:
183.3338 + return "_float_";
183.3339 + case ITEM_Double:
183.3340 + return "_double_";
183.3341 + case ITEM_Long:
183.3342 + return "_long_";
183.3343 + case ITEM_Null:
183.3344 + return "_null_";
183.3345 + case ITEM_InitObject: // UninitializedThis
183.3346 + return "_init_";
183.3347 + case ITEM_Object:
183.3348 + return "_object_";
183.3349 + case ITEM_NewObject: // Uninitialized
183.3350 + return "_new_";
183.3351 + default:
183.3352 + throw new IllegalArgumentException("Unknown type");
183.3353 + }
183.3354 + }
183.3355 +
183.3356 + @Override
183.3357 + public String toString() {
183.3358 + final StringBuilder sb = new StringBuilder("[");
183.3359 + if (size > 0) {
183.3360 + sb.append(typeString(types[0]));
183.3361 + for (int i = 1; i < size; ++i) {
183.3362 + sb.append(", ");
183.3363 + sb.append(typeString(types[i]));
183.3364 + }
183.3365 + }
183.3366 +
183.3367 + return sb.append(']').toString();
183.3368 + }
183.3369 +
183.3370 + private void ensureCapacity(final int minCapacity) {
183.3371 + if ((minCapacity == 0)
183.3372 + || (types != null) && (minCapacity <= types.length)) {
183.3373 + return;
183.3374 + }
183.3375 +
183.3376 + final int newCapacity =
183.3377 + ((minCapacity + CAPACITY_INCREMENT - 1) / CAPACITY_INCREMENT)
183.3378 + * CAPACITY_INCREMENT;
183.3379 + final int[] newTypes = new int[newCapacity];
183.3380 +
183.3381 + if (size > 0) {
183.3382 + arraycopy(types, 0, newTypes, 0, size);
183.3383 + }
183.3384 +
183.3385 + types = newTypes;
183.3386 + }
183.3387 +
183.3388 + // no System.arraycopy
183.3389 + private void arraycopy(final int[] src, final int srcPos,
183.3390 + final int[] dest, final int destPos,
183.3391 + final int length) {
183.3392 + for (int i = 0; i < length; ++i) {
183.3393 + dest[destPos + i] = src[srcPos + i];
183.3394 + }
183.3395 + }
183.3396 + }
183.3397 + /**
183.3398 + * A JavaScript ready replacement for java.util.Vector
183.3399 + *
183.3400 + * @author Jaroslav Tulach <jtulach@netbeans.org>
183.3401 + */
183.3402 + @JavaScriptPrototype(prototype = "new Array")
183.3403 + private static final class Vector {
183.3404 +
183.3405 + private Object[] arr;
183.3406 +
183.3407 + Vector() {
183.3408 + }
183.3409 +
183.3410 + Vector(int i) {
183.3411 + }
183.3412 +
183.3413 + void add(Object objectType) {
183.3414 + addElement(objectType);
183.3415 + }
183.3416 +
183.3417 + @JavaScriptBody(args = {"obj"}, body =
183.3418 + "this.push(obj);")
183.3419 + void addElement(Object obj) {
183.3420 + final int s = size();
183.3421 + setSize(s + 1);
183.3422 + setElementAt(obj, s);
183.3423 + }
183.3424 +
183.3425 + @JavaScriptBody(args = {}, body =
183.3426 + "return this.length;")
183.3427 + int size() {
183.3428 + return arr == null ? 0 : arr.length;
183.3429 + }
183.3430 +
183.3431 + @JavaScriptBody(args = {"newArr"}, body =
183.3432 + "for (var i = 0; i < this.length; i++) {\n"
183.3433 + + " newArr[i] = this[i];\n"
183.3434 + + "}\n")
183.3435 + void copyInto(Object[] newArr) {
183.3436 + if (arr == null) {
183.3437 + return;
183.3438 + }
183.3439 + int min = Math.min(newArr.length, arr.length);
183.3440 + for (int i = 0; i < min; i++) {
183.3441 + newArr[i] = arr[i];
183.3442 + }
183.3443 + }
183.3444 +
183.3445 + @JavaScriptBody(args = {"index"}, body =
183.3446 + "return this[index];")
183.3447 + Object elementAt(int index) {
183.3448 + return arr[index];
183.3449 + }
183.3450 +
183.3451 + private void setSize(int len) {
183.3452 + Object[] newArr = new Object[len];
183.3453 + copyInto(newArr);
183.3454 + arr = newArr;
183.3455 + }
183.3456 +
183.3457 + @JavaScriptBody(args = {"val", "index"}, body =
183.3458 + "this[index] = val;")
183.3459 + void setElementAt(Object val, int index) {
183.3460 + arr[index] = val;
183.3461 + }
183.3462 + }
183.3463 +
183.3464 +}
184.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Tue Mar 05 07:57:16 2013 +0100
184.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Fri May 17 10:27:41 2013 +0200
184.3 @@ -19,15 +19,7 @@
184.4
184.5 import java.io.IOException;
184.6 import java.io.InputStream;
184.7 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
184.8 -import org.apidesign.javap.AnnotationParser;
184.9 -import org.apidesign.javap.ClassData;
184.10 -import org.apidesign.javap.FieldData;
184.11 -import org.apidesign.javap.MethodData;
184.12 -import org.apidesign.javap.StackMapIterator;
184.13 -import static org.apidesign.javap.RuntimeConstants.*;
184.14 -import org.apidesign.javap.TrapData;
184.15 -import org.apidesign.javap.TrapDataIterator;
184.16 +import static org.apidesign.vm4brwsr.ByteCodeParser.*;
184.17
184.18 /** Translator of the code inside class files to JavaScript.
184.19 *
184.20 @@ -36,9 +28,16 @@
184.21 abstract class ByteCodeToJavaScript {
184.22 private ClassData jc;
184.23 final Appendable out;
184.24 + final ObfuscationDelegate obfuscationDelegate;
184.25
184.26 protected ByteCodeToJavaScript(Appendable out) {
184.27 + this(out, ObfuscationDelegate.NULL);
184.28 + }
184.29 +
184.30 + protected ByteCodeToJavaScript(
184.31 + Appendable out, ObfuscationDelegate obfuscationDelegate) {
184.32 this.out = out;
184.33 + this.obfuscationDelegate = obfuscationDelegate;
184.34 }
184.35
184.36 /* Collects additional required resources.
184.37 @@ -66,7 +65,9 @@
184.38 /* protected */ String accessClass(String classOperation) {
184.39 return classOperation;
184.40 }
184.41 -
184.42 +
184.43 + abstract String getVMObject();
184.44 +
184.45 /** Prints out a debug message.
184.46 *
184.47 * @param msg the message
184.48 @@ -100,7 +101,9 @@
184.49 "resource", "processByteCode"
184.50 );
184.51 if (arr != null) {
184.52 - requireScript(arr[0]);
184.53 + if (!arr[0].isEmpty()) {
184.54 + requireScript(arr[0]);
184.55 + }
184.56 if ("0".equals(arr[1])) {
184.57 return null;
184.58 }
184.59 @@ -112,7 +115,8 @@
184.60 StringArray toInitilize = new StringArray();
184.61 final String className = className(jc);
184.62 out.append("\n\n").append(assignClass(className));
184.63 - out.append("function CLS() {");
184.64 + out.append("function ").append(className).append("() {");
184.65 + out.append("\n var CLS = ").append(className).append(';');
184.66 out.append("\n if (!CLS.$class) {");
184.67 if (proto == null) {
184.68 String sc = jc.getSuperClassName(); // with _
184.69 @@ -144,6 +148,8 @@
184.70 append(className).append('_').append(v.getName())
184.71 .append("; };");
184.72 }
184.73 +
184.74 + obfuscationDelegate.exportField(out, "c", "_" + v.getName(), v);
184.75 }
184.76 for (MethodData m : jc.getMethods()) {
184.77 byte[] onlyArr = m.findAnnotationData(true);
184.78 @@ -158,33 +164,39 @@
184.79 }
184.80 continue;
184.81 }
184.82 - String prefix;
184.83 + String destObject;
184.84 String mn;
184.85 + out.append("\n ");
184.86 if (m.isStatic()) {
184.87 - prefix = "\n c.";
184.88 - mn = generateStaticMethod(prefix, m, toInitilize);
184.89 + destObject = "c";
184.90 + mn = generateStaticMethod(destObject, m, toInitilize);
184.91 } else {
184.92 if (m.isConstructor()) {
184.93 - prefix = "\n CLS.";
184.94 - mn = generateInstanceMethod(prefix, m);
184.95 + destObject = "CLS";
184.96 + mn = generateInstanceMethod(destObject, m);
184.97 } else {
184.98 - prefix = "\n c.";
184.99 - mn = generateInstanceMethod(prefix, m);
184.100 + destObject = "c";
184.101 + mn = generateInstanceMethod(destObject, m);
184.102 }
184.103 }
184.104 + obfuscationDelegate.exportMethod(out, destObject, mn, m);
184.105 byte[] runAnno = m.findAnnotationData(false);
184.106 if (runAnno != null) {
184.107 - out.append(prefix).append(mn).append(".anno = {");
184.108 + out.append("\n ").append(destObject).append(".").append(mn).append(".anno = {");
184.109 generateAnno(jc, out, runAnno);
184.110 out.append("\n };");
184.111 }
184.112 - out.append(prefix).append(mn).append(".access = " + m.getAccess()).append(";");
184.113 - out.append(prefix).append(mn).append(".cls = CLS;");
184.114 + out.append("\n ").append(destObject).append(".").append(mn).append(".access = " + m.getAccess()).append(";");
184.115 + out.append("\n ").append(destObject).append(".").append(mn).append(".cls = CLS;");
184.116 }
184.117 out.append("\n c.constructor = CLS;");
184.118 - out.append("\n c.$instOf_").append(className).append(" = true;");
184.119 + String instOfName = "$instOf_" + className;
184.120 + out.append("\n c.").append(instOfName).append(" = true;");
184.121 + obfuscationDelegate.exportJSProperty(out, "c", instOfName);
184.122 for (String superInterface : jc.getSuperInterfaces()) {
184.123 - out.append("\n c.$instOf_").append(superInterface.replace('/', '_')).append(" = true;");
184.124 + instOfName = "$instOf_" + superInterface.replace('/', '_');
184.125 + out.append("\n c.").append(instOfName).append(" = true;");
184.126 + obfuscationDelegate.exportJSProperty(out, "c", instOfName);
184.127 }
184.128 out.append("\n CLS.$class = 'temp';");
184.129 out.append("\n CLS.$class = ");
184.130 @@ -199,6 +211,9 @@
184.131 generateAnno(jc, out, classAnno);
184.132 out.append("\n };");
184.133 }
184.134 + for (String init : toInitilize.toArray()) {
184.135 + out.append("\n ").append(init).append("();");
184.136 + }
184.137 out.append("\n }");
184.138 out.append("\n if (arguments.length === 0) {");
184.139 out.append("\n if (!(this instanceof CLS)) {");
184.140 @@ -227,14 +242,17 @@
184.141 out.append("\n }");
184.142 out.append("\n return arguments[0] ? new CLS() : CLS.prototype;");
184.143 out.append("\n};");
184.144 - StringBuilder sb = new StringBuilder();
184.145 - for (String init : toInitilize.toArray()) {
184.146 - sb.append("\n").append(init).append("();");
184.147 - }
184.148 - return sb.toString();
184.149 +
184.150 + obfuscationDelegate.exportClass(out, getVMObject(), className, jc);
184.151 +
184.152 +// StringBuilder sb = new StringBuilder();
184.153 +// for (String init : toInitilize.toArray()) {
184.154 +// sb.append("\n").append(init).append("();");
184.155 +// }
184.156 + return "";
184.157 }
184.158 - private String generateStaticMethod(String prefix, MethodData m, StringArray toInitilize) throws IOException {
184.159 - String jsb = javaScriptBody(prefix, m, true);
184.160 + private String generateStaticMethod(String destObject, MethodData m, StringArray toInitilize) throws IOException {
184.161 + String jsb = javaScriptBody(destObject, m, true);
184.162 if (jsb != null) {
184.163 return jsb;
184.164 }
184.165 @@ -242,28 +260,28 @@
184.166 if (mn.equals("class__V")) {
184.167 toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
184.168 }
184.169 - generateMethod(prefix, mn, m);
184.170 + generateMethod(destObject, mn, m);
184.171 return mn;
184.172 }
184.173
184.174 - private String generateInstanceMethod(String prefix, MethodData m) throws IOException {
184.175 - String jsb = javaScriptBody(prefix, m, false);
184.176 + private String generateInstanceMethod(String destObject, MethodData m) throws IOException {
184.177 + String jsb = javaScriptBody(destObject, m, false);
184.178 if (jsb != null) {
184.179 return jsb;
184.180 }
184.181 final String mn = findMethodName(m, new StringBuilder());
184.182 - generateMethod(prefix, mn, m);
184.183 + generateMethod(destObject, mn, m);
184.184 return mn;
184.185 }
184.186
184.187 - private void generateMethod(String prefix, String name, MethodData m)
184.188 + private void generateMethod(String destObject, String name, MethodData m)
184.189 throws IOException {
184.190 final StackMapIterator stackMapIterator = m.createStackMapIterator();
184.191 TrapDataIterator trap = m.getTrapDataIterator();
184.192 final LocalsMapper lmapper =
184.193 new LocalsMapper(stackMapIterator.getArguments());
184.194
184.195 - out.append(prefix).append(name).append(" = function(");
184.196 + out.append(destObject).append(".").append(name).append(" = function(");
184.197 lmapper.outputArguments(out, m.isStatic());
184.198 out.append(") {").append("\n");
184.199
184.200 @@ -286,22 +304,36 @@
184.201 TrapData[] previousTrap = null;
184.202 boolean wide = false;
184.203
184.204 - out.append("\n var gt = 0;\n for(;;) switch(gt) {\n");
184.205 + out.append("\n var gt = 0;\n");
184.206 + int openBraces = 0;
184.207 + int topMostLabel = 0;
184.208 for (int i = 0; i < byteCodes.length; i++) {
184.209 int prev = i;
184.210 stackMapIterator.advanceTo(i);
184.211 boolean changeInCatch = trap.advanceTo(i);
184.212 if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
184.213 if (previousTrap != null) {
184.214 - generateCatch(previousTrap);
184.215 + generateCatch(previousTrap, i, topMostLabel);
184.216 previousTrap = null;
184.217 }
184.218 }
184.219 if (lastStackFrame != stackMapIterator.getFrameIndex()) {
184.220 + if (i != 0) {
184.221 + out.append(" }\n");
184.222 + }
184.223 + if (openBraces > 64) {
184.224 + for (int c = 0; c < 64; c++) {
184.225 + out.append("break;}\n");
184.226 + }
184.227 + openBraces = 1;
184.228 + topMostLabel = i;
184.229 + }
184.230 +
184.231 lastStackFrame = stackMapIterator.getFrameIndex();
184.232 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
184.233 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
184.234 - out.append(" case " + i).append(": ");
184.235 + out.append(" X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
184.236 + openBraces++;
184.237 changeInCatch = true;
184.238 } else {
184.239 debug(" /* " + i + " */ ");
184.240 @@ -773,7 +805,7 @@
184.241 }
184.242 case opc_ldc_w:
184.243 case opc_ldc2_w: {
184.244 - int indx = readIntArg(byteCodes, i);
184.245 + int indx = readUShortArg(byteCodes, i);
184.246 i += 2;
184.247 String v = encodeConstant(indx);
184.248 int type = VarType.fromConstantType(jc.getTag(indx));
184.249 @@ -804,133 +836,104 @@
184.250 break;
184.251 case opc_if_acmpeq:
184.252 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
184.253 - "===");
184.254 + "===", topMostLabel);
184.255 break;
184.256 case opc_if_acmpne:
184.257 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
184.258 - "!==");
184.259 + "!==", topMostLabel);
184.260 break;
184.261 case opc_if_icmpeq:
184.262 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
184.263 - "==");
184.264 + "==", topMostLabel);
184.265 break;
184.266 case opc_ifeq: {
184.267 - int indx = i + readIntArg(byteCodes, i);
184.268 - emit(out, "if (@1 == 0) { gt = @2; continue; }",
184.269 - smapper.popI(), Integer.toString(indx));
184.270 + int indx = i + readShortArg(byteCodes, i);
184.271 + emitIf(out, "if (@1 == 0) ",
184.272 + smapper.popI(), i, indx, topMostLabel);
184.273 i += 2;
184.274 break;
184.275 }
184.276 case opc_ifne: {
184.277 - int indx = i + readIntArg(byteCodes, i);
184.278 - emit(out, "if (@1 != 0) { gt = @2; continue; }",
184.279 - smapper.popI(), Integer.toString(indx));
184.280 + int indx = i + readShortArg(byteCodes, i);
184.281 + emitIf(out, "if (@1 != 0) ",
184.282 + smapper.popI(), i, indx, topMostLabel);
184.283 i += 2;
184.284 break;
184.285 }
184.286 case opc_iflt: {
184.287 - int indx = i + readIntArg(byteCodes, i);
184.288 - emit(out, "if (@1 < 0) { gt = @2; continue; }",
184.289 - smapper.popI(), Integer.toString(indx));
184.290 + int indx = i + readShortArg(byteCodes, i);
184.291 + emitIf(out, "if (@1 < 0) ",
184.292 + smapper.popI(), i, indx, topMostLabel);
184.293 i += 2;
184.294 break;
184.295 }
184.296 case opc_ifle: {
184.297 - int indx = i + readIntArg(byteCodes, i);
184.298 - emit(out, "if (@1 <= 0) { gt = @2; continue; }",
184.299 - smapper.popI(), Integer.toString(indx));
184.300 + int indx = i + readShortArg(byteCodes, i);
184.301 + emitIf(out, "if (@1 <= 0) ",
184.302 + smapper.popI(), i, indx, topMostLabel);
184.303 i += 2;
184.304 break;
184.305 }
184.306 case opc_ifgt: {
184.307 - int indx = i + readIntArg(byteCodes, i);
184.308 - emit(out, "if (@1 > 0) { gt = @2; continue; }",
184.309 - smapper.popI(), Integer.toString(indx));
184.310 + int indx = i + readShortArg(byteCodes, i);
184.311 + emitIf(out, "if (@1 > 0) ",
184.312 + smapper.popI(), i, indx, topMostLabel);
184.313 i += 2;
184.314 break;
184.315 }
184.316 case opc_ifge: {
184.317 - int indx = i + readIntArg(byteCodes, i);
184.318 - emit(out, "if (@1 >= 0) { gt = @2; continue; }",
184.319 - smapper.popI(), Integer.toString(indx));
184.320 + int indx = i + readShortArg(byteCodes, i);
184.321 + emitIf(out, "if (@1 >= 0) ",
184.322 + smapper.popI(), i, indx, topMostLabel);
184.323 i += 2;
184.324 break;
184.325 }
184.326 case opc_ifnonnull: {
184.327 - int indx = i + readIntArg(byteCodes, i);
184.328 - emit(out, "if (@1 !== null) { gt = @2; continue; }",
184.329 - smapper.popA(), Integer.toString(indx));
184.330 + int indx = i + readShortArg(byteCodes, i);
184.331 + emitIf(out, "if (@1 !== null) ",
184.332 + smapper.popA(), i, indx, topMostLabel);
184.333 i += 2;
184.334 break;
184.335 }
184.336 case opc_ifnull: {
184.337 - int indx = i + readIntArg(byteCodes, i);
184.338 - emit(out, "if (@1 === null) { gt = @2; continue; }",
184.339 - smapper.popA(), Integer.toString(indx));
184.340 + int indx = i + readShortArg(byteCodes, i);
184.341 + emitIf(out, "if (@1 === null) ",
184.342 + smapper.popA(), i, indx, topMostLabel);
184.343 i += 2;
184.344 break;
184.345 }
184.346 case opc_if_icmpne:
184.347 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
184.348 - "!=");
184.349 + "!=", topMostLabel);
184.350 break;
184.351 case opc_if_icmplt:
184.352 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
184.353 - "<");
184.354 + "<", topMostLabel);
184.355 break;
184.356 case opc_if_icmple:
184.357 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
184.358 - "<=");
184.359 + "<=", topMostLabel);
184.360 break;
184.361 case opc_if_icmpgt:
184.362 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
184.363 - ">");
184.364 + ">", topMostLabel);
184.365 break;
184.366 case opc_if_icmpge:
184.367 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
184.368 - ">=");
184.369 + ">=", topMostLabel);
184.370 break;
184.371 case opc_goto: {
184.372 - int indx = i + readIntArg(byteCodes, i);
184.373 - emit(out, "gt = @1; continue;", Integer.toString(indx));
184.374 + int indx = i + readShortArg(byteCodes, i);
184.375 + goTo(out, i, indx, topMostLabel);
184.376 i += 2;
184.377 break;
184.378 }
184.379 case opc_lookupswitch: {
184.380 - int table = i / 4 * 4 + 4;
184.381 - int dflt = i + readInt4(byteCodes, table);
184.382 - table += 4;
184.383 - int n = readInt4(byteCodes, table);
184.384 - table += 4;
184.385 - out.append("switch (").append(smapper.popI()).append(") {\n");
184.386 - while (n-- > 0) {
184.387 - int cnstnt = readInt4(byteCodes, table);
184.388 - table += 4;
184.389 - int offset = i + readInt4(byteCodes, table);
184.390 - table += 4;
184.391 - out.append(" case " + cnstnt).append(": gt = " + offset).append("; continue;\n");
184.392 - }
184.393 - out.append(" default: gt = " + dflt).append("; continue;\n}");
184.394 - i = table - 1;
184.395 + i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
184.396 break;
184.397 }
184.398 case opc_tableswitch: {
184.399 - int table = i / 4 * 4 + 4;
184.400 - int dflt = i + readInt4(byteCodes, table);
184.401 - table += 4;
184.402 - int low = readInt4(byteCodes, table);
184.403 - table += 4;
184.404 - int high = readInt4(byteCodes, table);
184.405 - table += 4;
184.406 - out.append("switch (").append(smapper.popI()).append(") {\n");
184.407 - while (low <= high) {
184.408 - int offset = i + readInt4(byteCodes, table);
184.409 - table += 4;
184.410 - out.append(" case " + low).append(": gt = " + offset).append("; continue;\n");
184.411 - low++;
184.412 - }
184.413 - out.append(" default: gt = " + dflt).append("; continue;\n}");
184.414 - i = table - 1;
184.415 + i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
184.416 break;
184.417 }
184.418 case opc_invokeinterface: {
184.419 @@ -947,7 +950,7 @@
184.420 i = invokeStaticMethod(byteCodes, i, smapper, true);
184.421 break;
184.422 case opc_new: {
184.423 - int indx = readIntArg(byteCodes, i);
184.424 + int indx = readUShortArg(byteCodes, i);
184.425 String ci = jc.getClassName(indx);
184.426 emit(out, "var @1 = new @2;",
184.427 smapper.pushA(), accessClass(ci.replace('/', '_')));
184.428 @@ -957,50 +960,18 @@
184.429 }
184.430 case opc_newarray:
184.431 int atype = readUByte(byteCodes, ++i);
184.432 - String jvmType;
184.433 - switch (atype) {
184.434 - case 4: jvmType = "[Z"; break;
184.435 - case 5: jvmType = "[C"; break;
184.436 - case 6: jvmType = "[F"; break;
184.437 - case 7: jvmType = "[D"; break;
184.438 - case 8: jvmType = "[B"; break;
184.439 - case 9: jvmType = "[S"; break;
184.440 - case 10: jvmType = "[I"; break;
184.441 - case 11: jvmType = "[J"; break;
184.442 - default: throw new IllegalStateException("Array type: " + atype);
184.443 - }
184.444 - emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
184.445 - smapper.popI(), smapper.pushA(), jvmType);
184.446 + generateNewArray(atype, smapper);
184.447 break;
184.448 case opc_anewarray: {
184.449 - int type = readIntArg(byteCodes, i);
184.450 + int type = readUShortArg(byteCodes, i);
184.451 i += 2;
184.452 - String typeName = jc.getClassName(type);
184.453 - if (typeName.startsWith("[")) {
184.454 - typeName = "[" + typeName;
184.455 - } else {
184.456 - typeName = "[L" + typeName + ";";
184.457 - }
184.458 - emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
184.459 - smapper.popI(), smapper.pushA(), typeName);
184.460 + generateANewArray(type, smapper);
184.461 break;
184.462 }
184.463 case opc_multianewarray: {
184.464 - int type = readIntArg(byteCodes, i);
184.465 + int type = readUShortArg(byteCodes, i);
184.466 i += 2;
184.467 - String typeName = jc.getClassName(type);
184.468 - int dim = readUByte(byteCodes, ++i);
184.469 - StringBuilder dims = new StringBuilder();
184.470 - dims.append('[');
184.471 - for (int d = 0; d < dim; d++) {
184.472 - if (d != 0) {
184.473 - dims.insert(1, ",");
184.474 - }
184.475 - dims.insert(1, smapper.popI());
184.476 - }
184.477 - dims.append(']');
184.478 - emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
184.479 - dims.toString(), smapper.pushA(), typeName);
184.480 + i = generateMultiANewArray(type, byteCodes, i, smapper);
184.481 break;
184.482 }
184.483 case opc_arraylength:
184.484 @@ -1214,11 +1185,11 @@
184.485 case opc_sipush:
184.486 emit(out, "var @1 = @2;",
184.487 smapper.pushI(),
184.488 - Integer.toString(readIntArg(byteCodes, i)));
184.489 + Integer.toString(readShortArg(byteCodes, i)));
184.490 i += 2;
184.491 break;
184.492 case opc_getfield: {
184.493 - int indx = readIntArg(byteCodes, i);
184.494 + int indx = readUShortArg(byteCodes, i);
184.495 String[] fi = jc.getFieldInfoName(indx);
184.496 final int type = VarType.fromFieldType(fi[2].charAt(0));
184.497 final String mangleClass = mangleSig(fi[0]);
184.498 @@ -1231,7 +1202,7 @@
184.499 break;
184.500 }
184.501 case opc_putfield: {
184.502 - int indx = readIntArg(byteCodes, i);
184.503 + int indx = readUShortArg(byteCodes, i);
184.504 String[] fi = jc.getFieldInfoName(indx);
184.505 final int type = VarType.fromFieldType(fi[2].charAt(0));
184.506 final String mangleClass = mangleSig(fi[0]);
184.507 @@ -1245,7 +1216,7 @@
184.508 break;
184.509 }
184.510 case opc_getstatic: {
184.511 - int indx = readIntArg(byteCodes, i);
184.512 + int indx = readUShortArg(byteCodes, i);
184.513 String[] fi = jc.getFieldInfoName(indx);
184.514 final int type = VarType.fromFieldType(fi[2].charAt(0));
184.515 emit(out, "var @1 = @2(false)._@3();",
184.516 @@ -1256,7 +1227,7 @@
184.517 break;
184.518 }
184.519 case opc_putstatic: {
184.520 - int indx = readIntArg(byteCodes, i);
184.521 + int indx = readUShortArg(byteCodes, i);
184.522 String[] fi = jc.getFieldInfoName(indx);
184.523 final int type = VarType.fromFieldType(fi[2].charAt(0));
184.524 emit(out, "@1(false)._@2(@3);",
184.525 @@ -1267,33 +1238,14 @@
184.526 break;
184.527 }
184.528 case opc_checkcast: {
184.529 - int indx = readIntArg(byteCodes, i);
184.530 - final String type = jc.getClassName(indx);
184.531 - if (!type.startsWith("[")) {
184.532 - emit(out,
184.533 - "if (@1 !== null && !@1.$instOf_@2) throw {};",
184.534 - smapper.getA(0), type.replace('/', '_'));
184.535 - } else {
184.536 - emit(out, "vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@2').cast__Ljava_lang_Object_2Ljava_lang_Object_2(@1);",
184.537 - smapper.getA(0), type
184.538 - );
184.539 - }
184.540 + int indx = readUShortArg(byteCodes, i);
184.541 + generateCheckcast(indx, smapper);
184.542 i += 2;
184.543 break;
184.544 }
184.545 case opc_instanceof: {
184.546 - int indx = readIntArg(byteCodes, i);
184.547 - final String type = jc.getClassName(indx);
184.548 - if (!type.startsWith("[")) {
184.549 - emit(out, "var @2 = @1 != null && @1.$instOf_@3 ? 1 : 0;",
184.550 - smapper.popA(), smapper.pushI(),
184.551 - type.replace('/', '_'));
184.552 - } else {
184.553 - emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
184.554 - smapper.popA(), smapper.pushI(),
184.555 - type
184.556 - );
184.557 - }
184.558 + int indx = readUShortArg(byteCodes, i);
184.559 + generateInstanceOf(indx, smapper);
184.560 i += 2;
184.561 break;
184.562 }
184.563 @@ -1329,37 +1281,29 @@
184.564 }
184.565 }
184.566 if (debug(" //")) {
184.567 - for (int j = prev; j <= i; j++) {
184.568 - out.append(" ");
184.569 - final int cc = readUByte(byteCodes, j);
184.570 - out.append(Integer.toString(cc));
184.571 - }
184.572 + generateByteCodeComment(prev, i, byteCodes);
184.573 }
184.574 out.append("\n");
184.575 }
184.576 if (previousTrap != null) {
184.577 - generateCatch(previousTrap);
184.578 + generateCatch(previousTrap, byteCodes.length, topMostLabel);
184.579 }
184.580 - out.append(" }\n");
184.581 - out.append("};");
184.582 + out.append("\n }\n");
184.583 + while (openBraces-- > 0) {
184.584 + out.append('}');
184.585 + }
184.586 + out.append("\n};");
184.587 }
184.588
184.589 - private int generateIf(byte[] byteCodes, int i,
184.590 - final Variable v2, final Variable v1,
184.591 - final String test) throws IOException {
184.592 - int indx = i + readIntArg(byteCodes, i);
184.593 + private int generateIf(byte[] byteCodes, int i, final Variable v2, final Variable v1, final String test, int topMostLabel) throws IOException {
184.594 + int indx = i + readShortArg(byteCodes, i);
184.595 out.append("if (").append(v1)
184.596 .append(' ').append(test).append(' ')
184.597 - .append(v2).append(") { gt = " + indx)
184.598 - .append("; continue; }");
184.599 + .append(v2).append(") ");
184.600 + goTo(out, i, indx, topMostLabel);
184.601 return i + 2;
184.602 }
184.603 -
184.604 - private int readIntArg(byte[] byteCodes, int offsetInstruction) {
184.605 - final int indxHi = byteCodes[offsetInstruction + 1] << 8;
184.606 - final int indxLo = byteCodes[offsetInstruction + 2];
184.607 - return (indxHi & 0xffffff00) | (indxLo & 0xff);
184.608 - }
184.609 +
184.610 private int readInt4(byte[] byteCodes, int offset) {
184.611 final int d = byteCodes[offset + 0] << 24;
184.612 final int c = byteCodes[offset + 1] << 16;
184.613 @@ -1367,18 +1311,25 @@
184.614 final int a = byteCodes[offset + 3];
184.615 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
184.616 }
184.617 - private int readUByte(byte[] byteCodes, int offset) {
184.618 + private static int readUByte(byte[] byteCodes, int offset) {
184.619 return byteCodes[offset] & 0xff;
184.620 }
184.621
184.622 - private int readUShort(byte[] byteCodes, int offset) {
184.623 + private static int readUShort(byte[] byteCodes, int offset) {
184.624 return ((byteCodes[offset] & 0xff) << 8)
184.625 | (byteCodes[offset + 1] & 0xff);
184.626 }
184.627 + private static int readUShortArg(byte[] byteCodes, int offsetInstruction) {
184.628 + return readUShort(byteCodes, offsetInstruction + 1);
184.629 + }
184.630
184.631 - private int readShort(byte[] byteCodes, int offset) {
184.632 - return (byteCodes[offset] << 8)
184.633 - | (byteCodes[offset + 1] & 0xff);
184.634 + private static int readShort(byte[] byteCodes, int offset) {
184.635 + int signed = byteCodes[offset];
184.636 + byte b0 = (byte)signed;
184.637 + return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
184.638 + }
184.639 + private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
184.640 + return readShort(byteCodes, offsetInstruction + 1);
184.641 }
184.642
184.643 private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
184.644 @@ -1506,7 +1457,7 @@
184.645
184.646 private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
184.647 throws IOException {
184.648 - int methodIndex = readIntArg(byteCodes, i);
184.649 + int methodIndex = readUShortArg(byteCodes, i);
184.650 String[] mi = jc.getFieldInfoName(methodIndex);
184.651 char[] returnType = { 'V' };
184.652 StringBuilder cnt = new StringBuilder();
184.653 @@ -1551,7 +1502,7 @@
184.654 }
184.655 private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
184.656 throws IOException {
184.657 - int methodIndex = readIntArg(byteCodes, i);
184.658 + int methodIndex = readUShortArg(byteCodes, i);
184.659 String[] mi = jc.getFieldInfoName(methodIndex);
184.660 char[] returnType = { 'V' };
184.661 StringBuilder cnt = new StringBuilder();
184.662 @@ -1618,7 +1569,7 @@
184.663 return s;
184.664 }
184.665
184.666 - private String javaScriptBody(String prefix, MethodData m, boolean isStatic) throws IOException {
184.667 + private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
184.668 byte[] arr = m.findAnnotationData(true);
184.669 if (arr == null) {
184.670 return null;
184.671 @@ -1653,7 +1604,7 @@
184.672 }
184.673 StringBuilder cnt = new StringBuilder();
184.674 final String mn = findMethodName(m, cnt);
184.675 - out.append(prefix).append(mn);
184.676 + out.append(destObject).append(".").append(mn);
184.677 out.append(" = function(");
184.678 String space = "";
184.679 int index = 0;
184.680 @@ -1826,7 +1777,7 @@
184.681 out.append(format, processed, length);
184.682 }
184.683
184.684 - private void generateCatch(TrapData[] traps) throws IOException {
184.685 + private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
184.686 out.append("} catch (e) {\n");
184.687 int finallyPC = -1;
184.688 for (TrapData e : traps) {
184.689 @@ -1836,19 +1787,11 @@
184.690 if (e.catch_cpx != 0) { //not finally
184.691 final String classInternalName = jc.getClassName(e.catch_cpx);
184.692 addReference(classInternalName);
184.693 - if ("java/lang/Throwable".equals(classInternalName)) {
184.694 - out.append("if (e.$instOf_java_lang_Throwable) {");
184.695 - out.append(" var stA0 = e;");
184.696 - out.append("} else {");
184.697 - out.append(" var stA0 = vm.java_lang_Throwable(true);");
184.698 - out.append(" vm.java_lang_Throwable.cons__VLjava_lang_String_2.call(stA0, e.toString());");
184.699 - out.append("}");
184.700 - out.append("gt=" + e.handler_pc + "; continue;");
184.701 - } else {
184.702 - out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {");
184.703 - out.append("gt=" + e.handler_pc + "; var stA0 = e; continue;");
184.704 - out.append("}\n");
184.705 - }
184.706 + out.append("e = vm.java_lang_Throwable(false).bck2BrwsrCnvrt(e);");
184.707 + out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {");
184.708 + out.append("var stA0 = e;");
184.709 + goTo(out, current, e.handler_pc, topMostLabel);
184.710 + out.append("}\n");
184.711 } else {
184.712 finallyPC = e.handler_pc;
184.713 }
184.714 @@ -1856,8 +1799,152 @@
184.715 if (finallyPC == -1) {
184.716 out.append("throw e;");
184.717 } else {
184.718 - out.append("gt=" + finallyPC + "; var stA0 = e; continue;");
184.719 + out.append("var stA0 = e;");
184.720 + goTo(out, current, finallyPC, topMostLabel);
184.721 }
184.722 out.append("\n}");
184.723 }
184.724 +
184.725 + private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
184.726 + if (to < current) {
184.727 + if (canBack < to) {
184.728 + out.append("{ gt = 0; continue X_" + to + "; }");
184.729 + } else {
184.730 + out.append("{ gt = " + to + "; continue X_0; }");
184.731 + }
184.732 + } else {
184.733 + out.append("{ gt = " + to + "; break IF; }");
184.734 + }
184.735 + }
184.736 +
184.737 + private static void emitIf(
184.738 + Appendable out, String pattern, Variable param,
184.739 + int current, int to, int canBack
184.740 + ) throws IOException {
184.741 + emit(out, pattern, param);
184.742 + goTo(out, current, to, canBack);
184.743 + }
184.744 +
184.745 + private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
184.746 + String jvmType;
184.747 + switch (atype) {
184.748 + case 4: jvmType = "[Z"; break;
184.749 + case 5: jvmType = "[C"; break;
184.750 + case 6: jvmType = "[F"; break;
184.751 + case 7: jvmType = "[D"; break;
184.752 + case 8: jvmType = "[B"; break;
184.753 + case 9: jvmType = "[S"; break;
184.754 + case 10: jvmType = "[I"; break;
184.755 + case 11: jvmType = "[J"; break;
184.756 + default: throw new IllegalStateException("Array type: " + atype);
184.757 + }
184.758 + emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
184.759 + smapper.popI(), smapper.pushA(), jvmType);
184.760 + }
184.761 +
184.762 + private void generateANewArray(int type, final StackMapper smapper) throws IOException {
184.763 + String typeName = jc.getClassName(type);
184.764 + if (typeName.startsWith("[")) {
184.765 + typeName = "[" + typeName;
184.766 + } else {
184.767 + typeName = "[L" + typeName + ";";
184.768 + }
184.769 + emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
184.770 + smapper.popI(), smapper.pushA(), typeName);
184.771 + }
184.772 +
184.773 + private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
184.774 + String typeName = jc.getClassName(type);
184.775 + int dim = readUByte(byteCodes, ++i);
184.776 + StringBuilder dims = new StringBuilder();
184.777 + dims.append('[');
184.778 + for (int d = 0; d < dim; d++) {
184.779 + if (d != 0) {
184.780 + dims.insert(1, ",");
184.781 + }
184.782 + dims.insert(1, smapper.popI());
184.783 + }
184.784 + dims.append(']');
184.785 + emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
184.786 + dims.toString(), smapper.pushA(), typeName);
184.787 + return i;
184.788 + }
184.789 +
184.790 + private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
184.791 + int table = i / 4 * 4 + 4;
184.792 + int dflt = i + readInt4(byteCodes, table);
184.793 + table += 4;
184.794 + int low = readInt4(byteCodes, table);
184.795 + table += 4;
184.796 + int high = readInt4(byteCodes, table);
184.797 + table += 4;
184.798 + out.append("switch (").append(smapper.popI()).append(") {\n");
184.799 + while (low <= high) {
184.800 + int offset = i + readInt4(byteCodes, table);
184.801 + table += 4;
184.802 + out.append(" case " + low).append(":"); goTo(out, i, offset, topMostLabel); out.append('\n');
184.803 + low++;
184.804 + }
184.805 + out.append(" default: ");
184.806 + goTo(out, i, dflt, topMostLabel);
184.807 + out.append("\n}");
184.808 + i = table - 1;
184.809 + return i;
184.810 + }
184.811 +
184.812 + private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
184.813 + int table = i / 4 * 4 + 4;
184.814 + int dflt = i + readInt4(byteCodes, table);
184.815 + table += 4;
184.816 + int n = readInt4(byteCodes, table);
184.817 + table += 4;
184.818 + out.append("switch (").append(smapper.popI()).append(") {\n");
184.819 + while (n-- > 0) {
184.820 + int cnstnt = readInt4(byteCodes, table);
184.821 + table += 4;
184.822 + int offset = i + readInt4(byteCodes, table);
184.823 + table += 4;
184.824 + out.append(" case " + cnstnt).append(": "); goTo(out, i, offset, topMostLabel); out.append('\n');
184.825 + }
184.826 + out.append(" default: ");
184.827 + goTo(out, i, dflt, topMostLabel);
184.828 + out.append("\n}");
184.829 + i = table - 1;
184.830 + return i;
184.831 + }
184.832 +
184.833 + private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
184.834 + final String type = jc.getClassName(indx);
184.835 + if (!type.startsWith("[")) {
184.836 + emit(out, "var @2 = @1 != null && @1.$instOf_@3 ? 1 : 0;",
184.837 + smapper.popA(), smapper.pushI(),
184.838 + type.replace('/', '_'));
184.839 + } else {
184.840 + emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
184.841 + smapper.popA(), smapper.pushI(),
184.842 + type
184.843 + );
184.844 + }
184.845 + }
184.846 +
184.847 + private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
184.848 + final String type = jc.getClassName(indx);
184.849 + if (!type.startsWith("[")) {
184.850 + emit(out,
184.851 + "if (@1 !== null && !@1.$instOf_@2) throw vm.java_lang_ClassCastException(true);",
184.852 + smapper.getA(0), type.replace('/', '_'));
184.853 + } else {
184.854 + emit(out, "vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@2').cast__Ljava_lang_Object_2Ljava_lang_Object_2(@1);",
184.855 + smapper.getA(0), type
184.856 + );
184.857 + }
184.858 + }
184.859 +
184.860 + private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
184.861 + for (int j = prev; j <= i; j++) {
184.862 + out.append(" ");
184.863 + final int cc = readUByte(byteCodes, j);
184.864 + out.append(Integer.toString(cc));
184.865 + }
184.866 + }
184.867 }
185.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
185.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClosureWrapper.java Fri May 17 10:27:41 2013 +0200
185.3 @@ -0,0 +1,334 @@
185.4 +/**
185.5 + * Back 2 Browser Bytecode Translator
185.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
185.7 + *
185.8 + * This program is free software: you can redistribute it and/or modify
185.9 + * it under the terms of the GNU General Public License as published by
185.10 + * the Free Software Foundation, version 2 of the License.
185.11 + *
185.12 + * This program is distributed in the hope that it will be useful,
185.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
185.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
185.15 + * GNU General Public License for more details.
185.16 + *
185.17 + * You should have received a copy of the GNU General Public License
185.18 + * along with this program. Look for COPYING file in the top folder.
185.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
185.20 + */
185.21 +package org.apidesign.vm4brwsr;
185.22 +
185.23 +import com.google.javascript.jscomp.CommandLineRunner;
185.24 +import com.google.javascript.jscomp.SourceFile;
185.25 +import java.io.IOException;
185.26 +import java.io.OutputStream;
185.27 +import java.io.PrintStream;
185.28 +import java.util.ArrayList;
185.29 +import java.util.Arrays;
185.30 +import java.util.Collection;
185.31 +import java.util.Collections;
185.32 +import java.util.List;
185.33 +import org.apidesign.bck2brwsr.core.ExtraJavaScript;
185.34 +import org.apidesign.vm4brwsr.ByteCodeParser.ClassData;
185.35 +import org.apidesign.vm4brwsr.ByteCodeParser.FieldData;
185.36 +import org.apidesign.vm4brwsr.ByteCodeParser.MethodData;
185.37 +
185.38 +/**
185.39 + *
185.40 + * @author Jaroslav Tulach <jtulach@netbeans.org>
185.41 + */
185.42 +@ExtraJavaScript(processByteCode = false, resource="")
185.43 +final class ClosureWrapper extends CommandLineRunner {
185.44 + private static final String[] ARGS = { "--compilation_level", "SIMPLE_OPTIMIZATIONS", "--js", "bck2brwsr-raw.js" /*, "--debug", "--formatting", "PRETTY_PRINT" */ };
185.45 +
185.46 + private final ClosuresObfuscationDelegate obfuscationDelegate;
185.47 + private final Bck2Brwsr.Resources res;
185.48 + private final StringArray classes;
185.49 +
185.50 + private String compiledCode;
185.51 + private String externsCode;
185.52 +
185.53 + private ClosureWrapper(Appendable out,
185.54 + String compilationLevel,
185.55 + ClosuresObfuscationDelegate obfuscationDelegate,
185.56 + Bck2Brwsr.Resources res, StringArray classes) {
185.57 + super(
185.58 + generateArguments(compilationLevel),
185.59 + new PrintStream(new APS(out)), System.err
185.60 + );
185.61 + this.obfuscationDelegate = obfuscationDelegate;
185.62 + this.res = res;
185.63 + this.classes = classes;
185.64 + }
185.65 +
185.66 + @Override
185.67 + protected List<SourceFile> createInputs(List<String> files, boolean allowStdIn) throws FlagUsageException, IOException {
185.68 + if (files.size() != 1 || !"bck2brwsr-raw.js".equals(files.get(0))) {
185.69 + throw new IOException("Unexpected files: " + files);
185.70 + }
185.71 + return Collections.nCopies(
185.72 + 1,
185.73 + SourceFile.fromGenerator(
185.74 + "bck2brwsr-raw.js",
185.75 + new SourceFile.Generator() {
185.76 + @Override
185.77 + public String getCode() {
185.78 + return getCompiledCode();
185.79 + }
185.80 + }));
185.81 + }
185.82 +
185.83 +
185.84 + @Override
185.85 + protected List<SourceFile> createExterns()
185.86 + throws FlagUsageException, IOException {
185.87 + final List<SourceFile> externsFiles =
185.88 + new ArrayList<SourceFile>(super.createExterns());
185.89 +
185.90 + externsFiles.add(
185.91 + SourceFile.fromGenerator(
185.92 + "bck2brwsr_externs.js",
185.93 + new SourceFile.Generator() {
185.94 + @Override
185.95 + public String getCode() {
185.96 + return getExternsCode();
185.97 + }
185.98 + }));
185.99 + return externsFiles;
185.100 + }
185.101 +
185.102 + private String getCompiledCode() {
185.103 + if (compiledCode == null) {
185.104 + StringBuilder sb = new StringBuilder();
185.105 + try {
185.106 + VM.compile(res, sb, classes, obfuscationDelegate);
185.107 + compiledCode = sb.toString();
185.108 + } catch (IOException ex) {
185.109 + compiledCode = ex.getMessage();
185.110 + }
185.111 + }
185.112 + return compiledCode;
185.113 + }
185.114 +
185.115 + private String getExternsCode() {
185.116 + if (externsCode == null) {
185.117 + // need compiled code at this point
185.118 + getCompiledCode();
185.119 +
185.120 + final StringBuilder sb = new StringBuilder("function RAW() {};\n");
185.121 + for (final String extern: obfuscationDelegate.getExterns()) {
185.122 + sb.append("RAW.prototype.").append(extern).append(";\n");
185.123 + }
185.124 + externsCode = sb.toString();
185.125 + }
185.126 + return externsCode;
185.127 + }
185.128 +
185.129 + private static final class APS extends OutputStream {
185.130 + private final Appendable out;
185.131 +
185.132 + public APS(Appendable out) {
185.133 + this.out = out;
185.134 + }
185.135 + @Override
185.136 + public void write(int b) throws IOException {
185.137 + out.append((char)b);
185.138 + }
185.139 + }
185.140 +
185.141 + private static String[] generateArguments(String compilationLevel) {
185.142 + String[] finalArgs = ARGS.clone();
185.143 + finalArgs[1] = compilationLevel;
185.144 +
185.145 + return finalArgs;
185.146 + }
185.147 +
185.148 + static int produceTo(Appendable w, ObfuscationLevel obfuscationLevel, Bck2Brwsr.Resources resources, StringArray arr) throws IOException {
185.149 + ClosureWrapper cw = create(w, obfuscationLevel, resources, arr);
185.150 + try {
185.151 + return cw.doRun();
185.152 + } catch (FlagUsageException ex) {
185.153 + throw new IOException(ex);
185.154 + }
185.155 + }
185.156 +
185.157 + private static ClosureWrapper create(Appendable w,
185.158 + ObfuscationLevel obfuscationLevel,
185.159 + Bck2Brwsr.Resources resources,
185.160 + StringArray arr) {
185.161 + switch (obfuscationLevel) {
185.162 + case MINIMAL:
185.163 + return new ClosureWrapper(w, "SIMPLE_OPTIMIZATIONS",
185.164 + new SimpleObfuscationDelegate(),
185.165 + resources, arr);
185.166 +/*
185.167 + case MEDIUM:
185.168 + return new ClosureWrapper(w, "ADVANCED_OPTIMIZATIONS",
185.169 + new MediumObfuscationDelegate(),
185.170 + resources, arr);
185.171 +*/
185.172 + case FULL:
185.173 + return new ClosureWrapper(w, "ADVANCED_OPTIMIZATIONS",
185.174 + new FullObfuscationDelegate(),
185.175 + resources, arr);
185.176 + default:
185.177 + throw new IllegalArgumentException(
185.178 + "Unsupported level: " + obfuscationLevel);
185.179 + }
185.180 + }
185.181 +
185.182 + private static abstract class ClosuresObfuscationDelegate
185.183 + extends ObfuscationDelegate {
185.184 + public abstract Collection<String> getExterns();
185.185 + }
185.186 +
185.187 + private static final class SimpleObfuscationDelegate
185.188 + extends ClosuresObfuscationDelegate {
185.189 + @Override
185.190 + public void exportJSProperty(Appendable out,
185.191 + String destObject,
185.192 + String propertyName) throws IOException {
185.193 + }
185.194 +
185.195 + @Override
185.196 + public void exportClass(Appendable out,
185.197 + String destObject,
185.198 + String mangledName,
185.199 + ClassData classData) throws IOException {
185.200 + }
185.201 +
185.202 + @Override
185.203 + public void exportMethod(Appendable out,
185.204 + String destObject,
185.205 + String mangledName,
185.206 + MethodData methodData) throws IOException {
185.207 + }
185.208 +
185.209 + @Override
185.210 + public void exportField(Appendable out,
185.211 + String destObject,
185.212 + String mangledName,
185.213 + FieldData fieldData) throws IOException {
185.214 + }
185.215 +
185.216 + @Override
185.217 + public Collection<String> getExterns() {
185.218 + return Collections.EMPTY_LIST;
185.219 + }
185.220 + }
185.221 +
185.222 + private static abstract class AdvancedObfuscationDelegate
185.223 + extends ClosuresObfuscationDelegate {
185.224 + private static final String[] INITIAL_EXTERNS = {
185.225 + "bck2brwsr",
185.226 + "$class",
185.227 + "anno",
185.228 + "array",
185.229 + "access",
185.230 + "cls",
185.231 + "vm",
185.232 + "loadClass",
185.233 + "loadBytes",
185.234 + "jvmName",
185.235 + "primitive",
185.236 + "superclass",
185.237 + "cnstr",
185.238 + "add32",
185.239 + "sub32",
185.240 + "mul32",
185.241 + "neg32",
185.242 + "toInt8",
185.243 + "toInt16",
185.244 + "next32",
185.245 + "high32",
185.246 + "toInt32",
185.247 + "toFP",
185.248 + "toLong",
185.249 + "toExactString",
185.250 + "add64",
185.251 + "sub64",
185.252 + "mul64",
185.253 + "and64",
185.254 + "or64",
185.255 + "xor64",
185.256 + "shl64",
185.257 + "shr64",
185.258 + "ushr64",
185.259 + "compare64",
185.260 + "neg64",
185.261 + "div32",
185.262 + "mod32",
185.263 + "div64",
185.264 + "mod64",
185.265 + "at",
185.266 + "getClass__Ljava_lang_Class_2",
185.267 + "clone__Ljava_lang_Object_2"
185.268 + };
185.269 +
185.270 + private final Collection<String> externs;
185.271 +
185.272 + protected AdvancedObfuscationDelegate() {
185.273 + externs = new ArrayList<String>(Arrays.asList(INITIAL_EXTERNS));
185.274 + }
185.275 +
185.276 + @Override
185.277 + public void exportClass(Appendable out,
185.278 + String destObject,
185.279 + String mangledName,
185.280 + ClassData classData) throws IOException {
185.281 + exportJSProperty(out, destObject, mangledName);
185.282 + }
185.283 +
185.284 + @Override
185.285 + public void exportMethod(Appendable out,
185.286 + String destObject,
185.287 + String mangledName,
185.288 + MethodData methodData) throws IOException {
185.289 + if ((methodData.access & ByteCodeParser.ACC_PRIVATE) == 0) {
185.290 + exportJSProperty(out, destObject, mangledName);
185.291 + }
185.292 + }
185.293 +
185.294 + @Override
185.295 + public void exportField(Appendable out,
185.296 + String destObject,
185.297 + String mangledName,
185.298 + FieldData fieldData) throws IOException {
185.299 + if ((fieldData.access & ByteCodeParser.ACC_PRIVATE) == 0) {
185.300 + exportJSProperty(out, destObject, mangledName);
185.301 + }
185.302 + }
185.303 +
185.304 + @Override
185.305 + public Collection<String> getExterns() {
185.306 + return externs;
185.307 + }
185.308 +
185.309 + protected void addExtern(String extern) {
185.310 + externs.add(extern);
185.311 + }
185.312 + }
185.313 +
185.314 + private static final class MediumObfuscationDelegate
185.315 + extends AdvancedObfuscationDelegate {
185.316 + @Override
185.317 + public void exportJSProperty(Appendable out,
185.318 + String destObject,
185.319 + String propertyName) {
185.320 + addExtern(propertyName);
185.321 + }
185.322 + }
185.323 +
185.324 + private static final class FullObfuscationDelegate
185.325 + extends AdvancedObfuscationDelegate {
185.326 + @Override
185.327 + public void exportJSProperty(Appendable out,
185.328 + String destObject,
185.329 + String propertyName) throws IOException {
185.330 + out.append("\n").append(destObject).append("['")
185.331 + .append(propertyName)
185.332 + .append("'] = ")
185.333 + .append(destObject).append(".").append(propertyName)
185.334 + .append(";\n");
185.335 + }
185.336 + }
185.337 +}
186.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
186.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/LdrRsrcs.java Fri May 17 10:27:41 2013 +0200
186.3 @@ -0,0 +1,48 @@
186.4 +/**
186.5 + * Back 2 Browser Bytecode Translator
186.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
186.7 + *
186.8 + * This program is free software: you can redistribute it and/or modify
186.9 + * it under the terms of the GNU General Public License as published by
186.10 + * the Free Software Foundation, version 2 of the License.
186.11 + *
186.12 + * This program is distributed in the hope that it will be useful,
186.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
186.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
186.15 + * GNU General Public License for more details.
186.16 + *
186.17 + * You should have received a copy of the GNU General Public License
186.18 + * along with this program. Look for COPYING file in the top folder.
186.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
186.20 + */
186.21 +package org.apidesign.vm4brwsr;
186.22 +
186.23 +import java.io.IOException;
186.24 +import java.io.InputStream;
186.25 +import java.net.URL;
186.26 +import java.util.Enumeration;
186.27 +
186.28 +/** Implementation of Resources that delegates to some class loader.
186.29 + *
186.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
186.31 + */
186.32 +final class LdrRsrcs implements Bck2Brwsr.Resources {
186.33 + private final ClassLoader loader;
186.34 +
186.35 + LdrRsrcs(ClassLoader loader) {
186.36 + this.loader = loader;
186.37 + }
186.38 +
186.39 + @Override
186.40 + public InputStream get(String name) throws IOException {
186.41 + Enumeration<URL> en = loader.getResources(name);
186.42 + URL u = null;
186.43 + while (en.hasMoreElements()) {
186.44 + u = en.nextElement();
186.45 + }
186.46 + if (u == null) {
186.47 + throw new IOException("Can't find " + name);
186.48 + }
186.49 + return u.openStream();
186.50 + }
186.51 +}
187.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/LocalsMapper.java Tue Mar 05 07:57:16 2013 +0100
187.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/LocalsMapper.java Fri May 17 10:27:41 2013 +0200
187.3 @@ -18,8 +18,7 @@
187.4 package org.apidesign.vm4brwsr;
187.5
187.6 import java.io.IOException;
187.7 -import org.apidesign.javap.RuntimeConstants;
187.8 -import org.apidesign.javap.TypeArray;
187.9 +import org.apidesign.vm4brwsr.ByteCodeParser.TypeArray;
187.10
187.11 final class LocalsMapper {
187.12 private final TypeArray argTypeRecords;
187.13 @@ -113,7 +112,7 @@
187.14 final int srcSize = stackMapTypes.getSize();
187.15 for (int i = 0, dstIndex = 0; i < srcSize; ++i) {
187.16 final int smType = stackMapTypes.get(i);
187.17 - if (smType == RuntimeConstants.ITEM_Bogus) {
187.18 + if (smType == ByteCodeParser.ITEM_Bogus) {
187.19 ++dstIndex;
187.20 continue;
187.21 }
188.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Main.java Tue Mar 05 07:57:16 2013 +0100
188.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Main.java Fri May 17 10:27:41 2013 +0200
188.3 @@ -18,6 +18,7 @@
188.4 package org.apidesign.vm4brwsr;
188.5
188.6 import java.io.BufferedWriter;
188.7 +import java.io.File;
188.8 import java.io.FileWriter;
188.9 import java.io.IOException;
188.10 import java.io.Writer;
188.11 @@ -31,20 +32,69 @@
188.12 private Main() {}
188.13
188.14 public static void main(String... args) throws IOException {
188.15 + final String obfuscate = "--obfuscatelevel";
188.16 +
188.17 if (args.length < 2) {
188.18 System.err.println("Bck2Brwsr Translator from Java(tm) to JavaScript, (c) Jaroslav Tulach 2012");
188.19 - System.err.println("Usage: java -cp ... -jar ... <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
188.20 - return;
188.21 + System.err.println("Usage: java -cp ... -jar ... [");
188.22 + System.err.print(obfuscate);
188.23 + System.err.print(" [");
188.24 + boolean first = true;
188.25 + for (ObfuscationLevel l : ObfuscationLevel.values()) {
188.26 + if (!first) {
188.27 + System.err.print('|');
188.28 + }
188.29 + System.err.print(l.name());
188.30 + first = false;
188.31 + }
188.32 +
188.33 + System.err.println("] <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
188.34 + System.exit(9);
188.35 }
188.36
188.37 - Writer w = new BufferedWriter(new FileWriter(args[0]));
188.38 - StringArray classes = StringArray.asList(args);
188.39 - classes.delete(0);
188.40 - try {
188.41 - Bck2Brwsr.generate(w, Main.class.getClassLoader(),
188.42 - classes.toArray());
188.43 - } finally {
188.44 - w.close();
188.45 + ObfuscationLevel obfLevel = ObfuscationLevel.NONE;
188.46 + StringArray classes = new StringArray();
188.47 + String generateTo = null;
188.48 + for (int i = 0; i < args.length; i++) {
188.49 + if (obfuscate.equals(args[i])) { // NOI18N
188.50 + i++;
188.51 + try {
188.52 + obfLevel = ObfuscationLevel.valueOf(args[i]);
188.53 + } catch (Exception e) {
188.54 + System.err.print(obfuscate);
188.55 + System.err.print(" parameter needs to be followed by one of ");
188.56 + boolean first = true;
188.57 + for (ObfuscationLevel l : ObfuscationLevel.values()) {
188.58 + if (!first) {
188.59 + System.err.print(", ");
188.60 + }
188.61 + System.err.print(l.name());
188.62 + first = false;
188.63 + }
188.64 + System.err.println();
188.65 + System.exit(1);
188.66 + }
188.67 + continue;
188.68 + }
188.69 + if (generateTo == null) {
188.70 + generateTo = args[i];
188.71 + } else {
188.72 + classes = classes.addAndNew(args[i]);
188.73 + }
188.74 + }
188.75 +
188.76 + File gt = new File(generateTo);
188.77 + if (Boolean.getBoolean("skip.if.exists") && gt.isFile()) {
188.78 + System.err.println("Skipping as " + gt + " exists.");
188.79 + System.exit(0);
188.80 + }
188.81 +
188.82 + try (Writer w = new BufferedWriter(new FileWriter(gt))) {
188.83 + Bck2Brwsr.newCompiler().
188.84 + obfuscation(obfLevel).
188.85 + addRootClasses(classes.toArray()).
188.86 + resources(Main.class.getClassLoader()).
188.87 + generate(w);
188.88 }
188.89 }
188.90 }
189.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
189.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ObfuscationDelegate.java Fri May 17 10:27:41 2013 +0200
189.3 @@ -0,0 +1,75 @@
189.4 +/**
189.5 + * Back 2 Browser Bytecode Translator
189.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
189.7 + *
189.8 + * This program is free software: you can redistribute it and/or modify
189.9 + * it under the terms of the GNU General Public License as published by
189.10 + * the Free Software Foundation, version 2 of the License.
189.11 + *
189.12 + * This program is distributed in the hope that it will be useful,
189.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
189.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
189.15 + * GNU General Public License for more details.
189.16 + *
189.17 + * You should have received a copy of the GNU General Public License
189.18 + * along with this program. Look for COPYING file in the top folder.
189.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
189.20 + */
189.21 +package org.apidesign.vm4brwsr;
189.22 +
189.23 +import java.io.IOException;
189.24 +import org.apidesign.vm4brwsr.ByteCodeParser.ClassData;
189.25 +import org.apidesign.vm4brwsr.ByteCodeParser.FieldData;
189.26 +import org.apidesign.vm4brwsr.ByteCodeParser.MethodData;
189.27 +
189.28 +abstract class ObfuscationDelegate {
189.29 + static ObfuscationDelegate NULL =
189.30 + new ObfuscationDelegate() {
189.31 + @Override
189.32 + public void exportJSProperty(Appendable out,
189.33 + String destObject,
189.34 + String propertyName)
189.35 + throws IOException {
189.36 + }
189.37 +
189.38 + @Override
189.39 + public void exportClass(Appendable out,
189.40 + String destObject,
189.41 + String mangledName,
189.42 + ClassData classData)
189.43 + throws IOException {
189.44 + }
189.45 +
189.46 + @Override
189.47 + public void exportMethod(Appendable out,
189.48 + String destObject,
189.49 + String mangledName,
189.50 + MethodData methodData)
189.51 + throws IOException {
189.52 + }
189.53 +
189.54 + @Override
189.55 + public void exportField(Appendable out,
189.56 + String destObject,
189.57 + String mangledName,
189.58 + FieldData fieldData)
189.59 + throws IOException {
189.60 + }
189.61 + };
189.62 +
189.63 + public abstract void exportJSProperty(
189.64 + Appendable out, String destObject, String propertyName)
189.65 + throws IOException;
189.66 +
189.67 + public abstract void exportClass(
189.68 + Appendable out, String destObject, String mangledName,
189.69 + ClassData classData) throws IOException;
189.70 +
189.71 + public abstract void exportMethod(
189.72 + Appendable out, String destObject, String mangledName,
189.73 + MethodData methodData) throws IOException;
189.74 +
189.75 + public abstract void exportField(
189.76 + Appendable out, String destObject, String mangledName,
189.77 + FieldData fieldData) throws IOException;
189.78 +}
190.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
190.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ObfuscationLevel.java Fri May 17 10:27:41 2013 +0200
190.3 @@ -0,0 +1,41 @@
190.4 +/**
190.5 + * Back 2 Browser Bytecode Translator
190.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
190.7 + *
190.8 + * This program is free software: you can redistribute it and/or modify
190.9 + * it under the terms of the GNU General Public License as published by
190.10 + * the Free Software Foundation, version 2 of the License.
190.11 + *
190.12 + * This program is distributed in the hope that it will be useful,
190.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
190.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
190.15 + * GNU General Public License for more details.
190.16 + *
190.17 + * You should have received a copy of the GNU General Public License
190.18 + * along with this program. Look for COPYING file in the top folder.
190.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
190.20 + */
190.21 +package org.apidesign.vm4brwsr;
190.22 +
190.23 +/**
190.24 + * Defines obfuscation level of produced JavaScript files.
190.25 + *
190.26 + * @since 0.5
190.27 + */
190.28 +public enum ObfuscationLevel {
190.29 + /** Generated JavaScript is (sort of) human readable. Useful for debugging.
190.30 + * Dynamic capabilities of the virtual machine work on all classes.
190.31 + */
190.32 + NONE,
190.33 + /** White spaces are removed. Names of external symbols remain unchanged.
190.34 + * Dynamic capabilities of the virtual machine work on all classes.
190.35 + */
190.36 + MINIMAL,
190.37 +// temporarily commented out before merge. not well defined yet:
190.38 +// MEDIUM,
190.39 + /** Aggressive obfuscation of everything. Compact, unreadable "one-liner".
190.40 + * One cannot load classes dynamically. Useful mostly for static compilation
190.41 + * of self contained application.
190.42 + */
190.43 + FULL
190.44 +}
191.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/StackMapper.java Tue Mar 05 07:57:16 2013 +0100
191.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/StackMapper.java Fri May 17 10:27:41 2013 +0200
191.3 @@ -17,7 +17,7 @@
191.4 */
191.5 package org.apidesign.vm4brwsr;
191.6
191.7 -import org.apidesign.javap.TypeArray;
191.8 +import org.apidesign.vm4brwsr.ByteCodeParser.TypeArray;
191.9
191.10 final class StackMapper {
191.11 private final TypeArray stackTypeIndexPairs;
192.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java Tue Mar 05 07:57:16 2013 +0100
192.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java Fri May 17 10:27:41 2013 +0200
192.3 @@ -43,6 +43,25 @@
192.4 }
192.5 arr[arr.length - 1] = s;
192.6 }
192.7 +
192.8 + StringArray addAndNew(String... values) {
192.9 + int j;
192.10 + String[] tmp;
192.11 + if (arr == null) {
192.12 + tmp = new String[values.length];
192.13 + j = 0;
192.14 + } else {
192.15 + tmp = new String[arr.length + values.length];
192.16 + for (int i = 0; i < arr.length; i++) {
192.17 + tmp[i] = arr[i];
192.18 + }
192.19 + j = arr.length;
192.20 + }
192.21 + for (int i = 0; i < values.length;) {
192.22 + tmp[j++] = values[i++];
192.23 + }
192.24 + return new StringArray(tmp);
192.25 + }
192.26
192.27 public String[] toArray() {
192.28 return arr == null ? new String[0] : arr;
192.29 @@ -93,5 +112,4 @@
192.30 }
192.31 return -1;
192.32 }
192.33 -
192.34 }
193.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Tue Mar 05 07:57:16 2013 +0100
193.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Fri May 17 10:27:41 2013 +0200
193.3 @@ -28,7 +28,11 @@
193.4 public VM(Appendable out) {
193.5 super(out);
193.6 }
193.7 -
193.8 +
193.9 + private VM(Appendable out, ObfuscationDelegate obfuscationDelegate) {
193.10 + super(out, obfuscationDelegate);
193.11 + }
193.12 +
193.13 static {
193.14 // uses VMLazy to load dynamic classes
193.15 boolean assertsOn = false;
193.16 @@ -47,6 +51,12 @@
193.17 static void compile(Bck2Brwsr.Resources l, Appendable out, StringArray names) throws IOException {
193.18 new VM(out).doCompile(l, names);
193.19 }
193.20 +
193.21 + static void compile(Bck2Brwsr.Resources l, Appendable out, StringArray names,
193.22 + ObfuscationDelegate obfuscationDelegate) throws IOException {
193.23 + new VM(out, obfuscationDelegate).doCompile(l, names);
193.24 + }
193.25 +
193.26 protected void doCompile(Bck2Brwsr.Resources l, StringArray names) throws IOException {
193.27 out.append("(function VM(global) {var fillInVMSkeleton = function(vm) {");
193.28 StringArray processed = new StringArray();
193.29 @@ -110,7 +120,10 @@
193.30 for (String ic : toInit.toArray()) {
193.31 int indx = processed.indexOf(ic);
193.32 if (indx >= 0) {
193.33 - out.append(initCode.toArray()[indx]).append("\n");
193.34 + final String theCode = initCode.toArray()[indx];
193.35 + if (!theCode.isEmpty()) {
193.36 + out.append(theCode).append("\n");
193.37 + }
193.38 initCode.toArray()[indx] = "";
193.39 }
193.40 }
193.41 @@ -227,4 +240,9 @@
193.42 String accessClass(String className) {
193.43 return "vm." + className;
193.44 }
193.45 +
193.46 + @Override
193.47 + String getVMObject() {
193.48 + return "vm";
193.49 + }
193.50 }
194.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Tue Mar 05 07:57:16 2013 +0100
194.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Fri May 17 10:27:41 2013 +0200
194.3 @@ -56,7 +56,7 @@
194.4 throw new ClassNotFoundException(name);
194.5 }
194.6 // beingDefined(loader, name);
194.7 - StringBuilder out = new StringBuilder();
194.8 + StringBuilder out = new StringBuilder(65535);
194.9 out.append("var loader = arguments[0];\n");
194.10 out.append("var vm = loader.vm;\n");
194.11 int prelude = out.length();
194.12 @@ -151,5 +151,10 @@
194.13 String accessClass(String classOperation) {
194.14 return "vm." + classOperation;
194.15 }
194.16 +
194.17 + @Override
194.18 + String getVMObject() {
194.19 + return "vm";
194.20 + }
194.21 }
194.22 }
195.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VarType.java Tue Mar 05 07:57:16 2013 +0100
195.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VarType.java Fri May 17 10:27:41 2013 +0200
195.3 @@ -17,8 +17,6 @@
195.4 */
195.5 package org.apidesign.vm4brwsr;
195.6
195.7 -import org.apidesign.javap.RuntimeConstants;
195.8 -
195.9 final class VarType {
195.10 public static final int INTEGER = 0;
195.11 public static final int LONG = 1;
195.12 @@ -37,21 +35,21 @@
195.13
195.14 public static int fromStackMapType(final int smType) {
195.15 switch (smType & 0xff) {
195.16 - case RuntimeConstants.ITEM_Integer:
195.17 + case ByteCodeParser.ITEM_Integer:
195.18 return VarType.INTEGER;
195.19 - case RuntimeConstants.ITEM_Float:
195.20 + case ByteCodeParser.ITEM_Float:
195.21 return VarType.FLOAT;
195.22 - case RuntimeConstants.ITEM_Double:
195.23 + case ByteCodeParser.ITEM_Double:
195.24 return VarType.DOUBLE;
195.25 - case RuntimeConstants.ITEM_Long:
195.26 + case ByteCodeParser.ITEM_Long:
195.27 return VarType.LONG;
195.28 - case RuntimeConstants.ITEM_Null:
195.29 - case RuntimeConstants.ITEM_InitObject:
195.30 - case RuntimeConstants.ITEM_Object:
195.31 - case RuntimeConstants.ITEM_NewObject:
195.32 + case ByteCodeParser.ITEM_Null:
195.33 + case ByteCodeParser.ITEM_InitObject:
195.34 + case ByteCodeParser.ITEM_Object:
195.35 + case ByteCodeParser.ITEM_NewObject:
195.36 return VarType.REFERENCE;
195.37
195.38 - case RuntimeConstants.ITEM_Bogus:
195.39 + case ByteCodeParser.ITEM_Bogus:
195.40 /* unclear how to handle for now */
195.41 default:
195.42 throw new IllegalStateException("Unhandled stack map type");
195.43 @@ -60,25 +58,25 @@
195.44
195.45 public static int fromConstantType(final byte constantTag) {
195.46 switch (constantTag) {
195.47 - case RuntimeConstants.CONSTANT_INTEGER:
195.48 + case ByteCodeParser.CONSTANT_INTEGER:
195.49 return VarType.INTEGER;
195.50 - case RuntimeConstants.CONSTANT_FLOAT:
195.51 + case ByteCodeParser.CONSTANT_FLOAT:
195.52 return VarType.FLOAT;
195.53 - case RuntimeConstants.CONSTANT_LONG:
195.54 + case ByteCodeParser.CONSTANT_LONG:
195.55 return VarType.LONG;
195.56 - case RuntimeConstants.CONSTANT_DOUBLE:
195.57 + case ByteCodeParser.CONSTANT_DOUBLE:
195.58 return VarType.DOUBLE;
195.59
195.60 - case RuntimeConstants.CONSTANT_CLASS:
195.61 - case RuntimeConstants.CONSTANT_UTF8:
195.62 - case RuntimeConstants.CONSTANT_UNICODE:
195.63 - case RuntimeConstants.CONSTANT_STRING:
195.64 + case ByteCodeParser.CONSTANT_CLASS:
195.65 + case ByteCodeParser.CONSTANT_UTF8:
195.66 + case ByteCodeParser.CONSTANT_UNICODE:
195.67 + case ByteCodeParser.CONSTANT_STRING:
195.68 return VarType.REFERENCE;
195.69
195.70 - case RuntimeConstants.CONSTANT_FIELD:
195.71 - case RuntimeConstants.CONSTANT_METHOD:
195.72 - case RuntimeConstants.CONSTANT_INTERFACEMETHOD:
195.73 - case RuntimeConstants.CONSTANT_NAMEANDTYPE:
195.74 + case ByteCodeParser.CONSTANT_FIELD:
195.75 + case ByteCodeParser.CONSTANT_METHOD:
195.76 + case ByteCodeParser.CONSTANT_INTERFACEMETHOD:
195.77 + case ByteCodeParser.CONSTANT_NAMEANDTYPE:
195.78 /* unclear how to handle for now */
195.79 default:
195.80 throw new IllegalStateException("Unhandled constant tag");
196.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java Tue Mar 05 07:57:16 2013 +0100
196.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java Fri May 17 10:27:41 2013 +0200
196.3 @@ -207,5 +207,12 @@
196.4 true
196.5 );
196.6 }
196.7 +
196.8 + @Test public void valueOfEnum() throws Exception {
196.9 + assertExec("can get value of enum", Classes.class,
196.10 + "valueEnum__Ljava_lang_String_2Ljava_lang_String_2",
196.11 + "TWO", "TWO"
196.12 + );
196.13 + }
196.14
196.15 }
197.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Classes.java Tue Mar 05 07:57:16 2013 +0100
197.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Classes.java Fri May 17 10:27:41 2013 +0200
197.3 @@ -230,4 +230,7 @@
197.4 return Application.class.isAssignableFrom(MyApplication.class);
197.5 }
197.6
197.7 + public static String valueEnum(String v) {
197.8 + return ClassesMarker.E.valueOf(v).toString();
197.9 + }
197.10 }
198.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Tue Mar 05 07:57:16 2013 +0100
198.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Fri May 17 10:27:41 2013 +0200
198.3 @@ -194,14 +194,14 @@
198.4
198.5 @Test public void valueOfLongBooleanTrue() throws Exception {
198.6 assertExec("Can we call JavaScripts valueOf on Boolean?",
198.7 - Numbers.class, "seven__DI",
198.8 - Double.valueOf(1), 31
198.9 + Numbers.class, "bseven__ZI",
198.10 + true, 31
198.11 );
198.12 }
198.13 @Test public void valueOfLongBooleanFalse() throws Exception {
198.14 assertExec("Can we call JavaScripts valueOf on Boolean?",
198.15 - Numbers.class, "seven__DI",
198.16 - Double.valueOf(0), 30
198.17 + Numbers.class, "bseven__ZI",
198.18 + false, 30
198.19 );
198.20 }
198.21
199.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Tue Mar 05 07:57:16 2013 +0100
199.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Fri May 17 10:27:41 2013 +0200
199.3 @@ -84,10 +84,20 @@
199.4 default: throw new IllegalStateException();
199.5 }
199.6 }
199.7 + static boolean bseven(int todo) {
199.8 + switch (todo) {
199.9 + case 30: return bvalueOf(Boolean.FALSE);
199.10 + case 31: return bvalueOf(Boolean.TRUE);
199.11 + default: throw new IllegalStateException();
199.12 + }
199.13 + }
199.14
199.15 @JavaScriptBody(args = {}, body = "return 7;")
199.16 private static native Number sevenNew();
199.17 +
199.18 + @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
199.19 + private static native double valueOf(Object o);
199.20
199.21 @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
199.22 - private static native double valueOf(Object o);
199.23 + private static native boolean bvalueOf(Object o);
199.24 }
200.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/StringSample.java Tue Mar 05 07:57:16 2013 +0100
200.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/StringSample.java Fri May 17 10:27:41 2013 +0200
200.3 @@ -18,6 +18,7 @@
200.4 package org.apidesign.vm4brwsr;
200.5
200.6 import java.io.UnsupportedEncodingException;
200.7 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
200.8
200.9 /**
200.10 *
200.11 @@ -129,4 +130,20 @@
200.12 public String toString() {
200.13 return HELLO + cnt;
200.14 }
200.15 +
200.16 + @JavaScriptBody(args = {}, body = "return [1, 2];")
200.17 + private static native Object crtarr();
200.18 + @JavaScriptBody(args = { "o" }, body = "return o.toString();")
200.19 + private static native String toStrng(Object o);
200.20 +
200.21 + public static String toStringArray(boolean fakeArr, boolean toString) {
200.22 + final Object arr = fakeArr ? crtarr() : new Object[2];
200.23 + final String whole = toString ? arr.toString() : toStrng(arr);
200.24 + int zav = whole.indexOf('@');
200.25 + if (zav <= 0) {
200.26 + zav = whole.length();
200.27 + }
200.28 + return whole.substring(0, zav).toString().toString();
200.29 + }
200.30 +
200.31 }
201.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java Tue Mar 05 07:57:16 2013 +0100
201.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java Fri May 17 10:27:41 2013 +0200
201.3 @@ -194,6 +194,34 @@
201.4
201.5 }
201.6
201.7 + @Test public void toStringOnJSArray() throws Exception {
201.8 + String exp = StringSample.toStringArray(false, true);
201.9 +
201.10 + assertExec(
201.11 + "Treated as Java Object array",
201.12 + StringSample.class, "toStringArray__Ljava_lang_String_2ZZ",
201.13 + exp, true, true
201.14 + );
201.15 + }
201.16 +
201.17 + @Test public void toStringOnRealArray() throws Exception {
201.18 + String exp = StringSample.toStringArray(false, true);
201.19 +
201.20 + assertExec(
201.21 + "Is Java Object array",
201.22 + StringSample.class, "toStringArray__Ljava_lang_String_2ZZ",
201.23 + exp, false, true
201.24 + );
201.25 + }
201.26 +
201.27 + @Test public void valueOfOnJSArray() throws Exception {
201.28 + assertExec(
201.29 + "Treated as classical JavaScript array",
201.30 + StringSample.class, "toStringArray__Ljava_lang_String_2ZZ",
201.31 + "1,2", true, false
201.32 + );
201.33 + }
201.34 +
201.35 private static TestVM code;
201.36
201.37 @BeforeClass
202.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Tue Mar 05 07:57:16 2013 +0100
202.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Fri May 17 10:27:41 2013 +0200
202.3 @@ -115,7 +115,7 @@
202.4 if (sb.length() > 2000) {
202.5 sb = dumpJS(sb);
202.6 }
202.7 - fail("Could not evaluate:\n" + sb, ex);
202.8 + fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
202.9 return null;
202.10 }
202.11 }
203.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/VMinVM.java Tue Mar 05 07:57:16 2013 +0100
203.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/VMinVM.java Fri May 17 10:27:41 2013 +0200
203.3 @@ -43,4 +43,9 @@
203.4 @Override
203.5 protected void requireScript(String resourcePath) {
203.6 }
203.7 +
203.8 + @Override
203.9 + String getVMObject() {
203.10 + return "global";
203.11 + }
203.12 }
204.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java Tue Mar 05 07:57:16 2013 +0100
204.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java Fri May 17 10:27:41 2013 +0200
204.3 @@ -40,6 +40,14 @@
204.4 compareCode("org/apidesign/vm4brwsr/Classes.class");
204.5 }
204.6
204.7 + @Test public void compareGeneratedCodeForToolkitClass() throws Exception {
204.8 + String genCode = compareCode("org/apidesign/vm4brwsr/Bck2BrwsrToolkit.class");
204.9 + int indx = genCode.indexOf("gt = 65604");
204.10 + if (indx >= 0) {
204.11 + fail("Goto to an invalid label:\n...." + genCode.substring(indx - 30, indx + 30) + "....");
204.12 + }
204.13 + }
204.14 +
204.15 @BeforeClass
204.16 public static void compileTheCode() throws Exception {
204.17 code = TestVM.compileClass("org/apidesign/vm4brwsr/VMinVM");
204.18 @@ -49,7 +57,7 @@
204.19 code = null;
204.20 }
204.21
204.22 - private void compareCode(final String nm) throws Exception, IOException {
204.23 + private String compareCode(final String nm) throws Exception, IOException {
204.24 byte[] arr = BytesLoader.readClass(nm);
204.25 String ret1 = VMinVM.toJavaScript(arr);
204.26
204.27 @@ -88,5 +96,7 @@
204.28 msg.append(code.toString());
204.29 fail(msg.toString());
204.30 }
204.31 +
204.32 + return ret1;
204.33 }
204.34 }
205.1 Binary file rt/vm/src/test/resources/org/apidesign/vm4brwsr/Bck2BrwsrToolkit.class has changed
206.1 --- a/rt/vmtest/pom.xml Tue Mar 05 07:57:16 2013 +0100
206.2 +++ b/rt/vmtest/pom.xml Fri May 17 10:27:41 2013 +0200
206.3 @@ -4,11 +4,11 @@
206.4 <parent>
206.5 <groupId>org.apidesign.bck2brwsr</groupId>
206.6 <artifactId>rt</artifactId>
206.7 - <version>0.4-SNAPSHOT</version>
206.8 + <version>0.8-SNAPSHOT</version>
206.9 </parent>
206.10 <groupId>org.apidesign.bck2brwsr</groupId>
206.11 <artifactId>vmtest</artifactId>
206.12 - <version>0.4-SNAPSHOT</version>
206.13 + <version>0.8-SNAPSHOT</version>
206.14
206.15 <name>VM Testing APIs</name>
206.16 <url>http://bck2brwsr.apidesign.org</url>
206.17 @@ -50,18 +50,6 @@
206.18 </dependency>
206.19 <dependency>
206.20 <groupId>${project.groupId}</groupId>
206.21 - <artifactId>vm4brwsr</artifactId>
206.22 - <version>${project.version}</version>
206.23 - <type>jar</type>
206.24 - </dependency>
206.25 - <dependency>
206.26 - <groupId>${project.groupId}</groupId>
206.27 - <artifactId>emul.mini</artifactId>
206.28 - <version>${project.version}</version>
206.29 - <scope>test</scope>
206.30 - </dependency>
206.31 - <dependency>
206.32 - <groupId>${project.groupId}</groupId>
206.33 <artifactId>launcher</artifactId>
206.34 <version>${project.version}</version>
206.35 </dependency>
207.1 --- a/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/BrwsrTest.java Tue Mar 05 07:57:16 2013 +0100
207.2 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/BrwsrTest.java Fri May 17 10:27:41 2013 +0200
207.3 @@ -29,6 +29,10 @@
207.4 * The browser to is by default executed via {@link java.awt.Desktop#browse(java.net.URI)},
207.5 * but one can change that by specifying <code>-Dvmtest.brwsrs=firefox,google-chrome</code>
207.6 * property.
207.7 + * <p>
207.8 + * If the annotated method throws {@link InterruptedException}, it will return
207.9 + * the processing to the browser and after 100ms, called again. This is useful
207.10 + * for testing asynchronous communication, etc.
207.11 *
207.12 * @author Jaroslav Tulach <jtulach@netbeans.org>
207.13 */
208.1 --- a/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Http.java Tue Mar 05 07:57:16 2013 +0100
208.2 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Http.java Fri May 17 10:27:41 2013 +0200
208.3 @@ -53,5 +53,10 @@
208.4 String resource() default "";
208.5 /** mime type of the resource */
208.6 String mimeType();
208.7 + /** query parameters. Can be referenced from the {@link #content} as
208.8 + * <code>$0</code>, <code>$1</code>, etc. The values will be extracted
208.9 + * from URL parameters of the request.
208.10 + */
208.11 + String[] parameters() default {};
208.12 }
208.13 }
209.1 --- a/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java Tue Mar 05 07:57:16 2013 +0100
209.2 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java Fri May 17 10:27:41 2013 +0200
209.3 @@ -17,6 +17,9 @@
209.4 */
209.5 package org.apidesign.bck2brwsr.vmtest;
209.6
209.7 +import java.util.ArrayList;
209.8 +import java.util.Arrays;
209.9 +import java.util.List;
209.10 import org.apidesign.bck2brwsr.launcher.Launcher;
209.11 import org.apidesign.bck2brwsr.vmtest.impl.CompareCase;
209.12 import org.testng.annotations.Factory;
209.13 @@ -34,6 +37,9 @@
209.14 * @author Jaroslav Tulach <jtulach@netbeans.org>
209.15 */
209.16 public final class VMTest {
209.17 + private final List<Class> classes = new ArrayList<>();
209.18 + private final List<String> launcher = new ArrayList<>();
209.19 +
209.20 private VMTest() {
209.21 }
209.22
209.23 @@ -47,10 +53,66 @@
209.24 * Each {@link BrwsrTest} annotated method is executed once in {@link Launcher started
209.25 * browser}.
209.26 *
209.27 - * @param clazz the class to inspect
209.28 + * @param clazz the class (or classes) to inspect
209.29 * @return the set of created tests
209.30 */
209.31 - public static Object[] create(Class<?> clazz) {
209.32 - return CompareCase.create(clazz);
209.33 + public static Object[] create(Class clazz) {
209.34 + return newTests().withClasses(clazz).build();
209.35 + }
209.36 +
209.37 + /** Creates new builder for test execution. Continue with methods
209.38 + * like {@link #withClasses(java.lang.Class[])} or {@link #withLaunchers(java.lang.String[])}.
209.39 + * Finish the process by calling {@link #build()}.
209.40 + *
209.41 + * @return new instance of a builder
209.42 + * @since 0.7
209.43 + */
209.44 + public static VMTest newTests() {
209.45 + return new VMTest();
209.46 + }
209.47 +
209.48 + /** Adds class (or classes) to the test execution. The classes are inspected
209.49 + * to contain methods annotated by
209.50 + * {@link Compare} or {@link BrwsrTest}. Appropriate set of TestNG test
209.51 + * cases is then created.
209.52 + * <p>
209.53 + * Each {@link Compare} instance runs the test in different virtual
209.54 + * machine and at the end they compare the results.
209.55 + * <p>
209.56 + * Each {@link BrwsrTest} annotated method is executed once in {@link Launcher started
209.57 + * browser}.
209.58 + *
209.59 + * @param classes one or more classes to inspect
209.60 + * @since 0.7
209.61 + */
209.62 + public final VMTest withClasses(Class... classes) {
209.63 + this.classes.addAll(Arrays.asList(classes));
209.64 + return this;
209.65 + }
209.66 +
209.67 + /** Adds list of launchers that should be used to execute tests defined
209.68 + * by {@link Compare} and {@link BrwsrTest} annotations. This value
209.69 + * can be overrided by using <code>vmtest.brwsrs</code> property.
209.70 + * List of supported launchers is available in the documentation of
209.71 + * {@link Launcher}.
209.72 + *
209.73 + * @param launcher names of one or more launchers to use for the execution
209.74 + * of tests
209.75 + * @since 0.7
209.76 + */
209.77 + public final VMTest withLaunchers(String... launcher) {
209.78 + this.launcher.addAll(Arrays.asList(launcher));
209.79 + return this;
209.80 + }
209.81 +
209.82 + /** Assembles the provided information into the final array of tests.
209.83 + * @return array of TestNG tests
209.84 + * @since 0.7
209.85 + */
209.86 + public final Object[] build() {
209.87 + return CompareCase.create(
209.88 + launcher.toArray(new String[0]),
209.89 + classes.toArray(new Class[0])
209.90 + );
209.91 }
209.92 }
210.1 --- a/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java Tue Mar 05 07:57:16 2013 +0100
210.2 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java Fri May 17 10:27:41 2013 +0200
210.3 @@ -65,17 +65,17 @@
210.4 for (Http.Resource r : http) {
210.5 if (!r.content().isEmpty()) {
210.6 InputStream is = new ByteArrayInputStream(r.content().getBytes("UTF-8"));
210.7 - c.addHttpResource(r.path(), r.mimeType(), is);
210.8 + c.addHttpResource(r.path(), r.mimeType(), r.parameters(), is);
210.9 } else {
210.10 InputStream is = m.getDeclaringClass().getResourceAsStream(r.resource());
210.11 - c.addHttpResource(r.path(), r.mimeType(), is);
210.12 + c.addHttpResource(r.path(), r.mimeType(), r.parameters(), is);
210.13 }
210.14 }
210.15 }
210.16 String res = c.invoke();
210.17 value = res;
210.18 if (fail) {
210.19 - int idx = res.indexOf(':');
210.20 + int idx = res == null ? -1 : res.indexOf(':');
210.21 if (idx >= 0) {
210.22 Class<? extends Throwable> thrwbl = null;
210.23 try {
211.1 --- a/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java Tue Mar 05 07:57:16 2013 +0100
211.2 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java Fri May 17 10:27:41 2013 +0200
211.3 @@ -53,26 +53,25 @@
211.4 * @param clazz the class to inspect
211.5 * @return the set of created tests
211.6 */
211.7 - public static Object[] create(Class<?> clazz) {
211.8 - Method[] arr = clazz.getMethods();
211.9 + public static Object[] create(String[] brwsr, Class[] classes) {
211.10 List<Object> ret = new ArrayList<>();
211.11
211.12 final LaunchSetup l = LaunchSetup.INSTANCE;
211.13 ret.add(l);
211.14
211.15 - String[] brwsr;
211.16 {
211.17 String p = System.getProperty("vmtest.brwsrs");
211.18 if (p != null) {
211.19 brwsr = p.split(",");
211.20 - } else {
211.21 - brwsr = new String[0];
211.22 }
211.23 }
211.24
211.25 - for (Method m : arr) {
211.26 - registerCompareCases(m, l, ret, brwsr);
211.27 - registerBrwsrCases(m, l, ret, brwsr);
211.28 + for (Class clazz : classes) {
211.29 + Method[] arr = clazz.getMethods();
211.30 + for (Method m : arr) {
211.31 + registerCompareCases(m, l, ret, brwsr);
211.32 + registerBrwsrCases(m, l, ret, brwsr);
211.33 + }
211.34 }
211.35 return ret.toArray();
211.36 }
211.37 @@ -83,10 +82,34 @@
211.38 @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
211.39 Object v1 = first.value;
211.40 Object v2 = second.value;
211.41 - if (v1 != null) {
211.42 - v1 = v1.toString();
211.43 + if (v1 instanceof Integer || v1 instanceof Long || v1 instanceof Byte || v1 instanceof Short) {
211.44 + try {
211.45 + v1 = Long.parseLong(v1.toString());
211.46 + } catch (NumberFormatException nfe) {
211.47 + v1 = "Can't parse " + v1.toString();
211.48 + }
211.49 + try {
211.50 + v2 = Long.parseLong(v2.toString());
211.51 + } catch (NumberFormatException nfe) {
211.52 + v2 = "Can't parse " + v2.toString();
211.53 + }
211.54 + } else if (v1 instanceof Number) {
211.55 + try {
211.56 + v1 = Double.parseDouble(v1.toString());
211.57 + } catch (NumberFormatException nfe) {
211.58 + v1 = "Can't parse " + v1.toString();
211.59 + }
211.60 + try {
211.61 + v2 = Double.parseDouble(v2.toString());
211.62 + } catch (NumberFormatException nfe) {
211.63 + v2 = "Can't parse " + v2.toString();
211.64 + }
211.65 } else {
211.66 - v1 = "null";
211.67 + if (v1 != null) {
211.68 + v1 = v1.toString();
211.69 + } else {
211.70 + v1 = "null";
211.71 + }
211.72 }
211.73 try {
211.74 Assert.assertEquals(v2, v1, "Comparing results");
212.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/AssertionTest.java Tue Mar 05 07:57:16 2013 +0100
212.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
212.3 @@ -1,43 +0,0 @@
212.4 -/**
212.5 - * Back 2 Browser Bytecode Translator
212.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
212.7 - *
212.8 - * This program is free software: you can redistribute it and/or modify
212.9 - * it under the terms of the GNU General Public License as published by
212.10 - * the Free Software Foundation, version 2 of the License.
212.11 - *
212.12 - * This program is distributed in the hope that it will be useful,
212.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
212.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
212.15 - * GNU General Public License for more details.
212.16 - *
212.17 - * You should have received a copy of the GNU General Public License
212.18 - * along with this program. Look for COPYING file in the top folder.
212.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
212.20 - */
212.21 -package org.apidesign.bck2brwsr.tck;
212.22 -
212.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
212.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
212.25 -import org.testng.annotations.Factory;
212.26 -
212.27 -/**
212.28 - *
212.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
212.30 - */
212.31 -public class AssertionTest {
212.32 -
212.33 - @Compare public Object checkAssert() throws ClassNotFoundException {
212.34 - try {
212.35 - assert false : "Is assertion status on?";
212.36 - return null;
212.37 - } catch (AssertionError ex) {
212.38 - return ex.getClass().getName();
212.39 - }
212.40 - }
212.41 -
212.42 - @Factory
212.43 - public static Object[] create() {
212.44 - return VMTest.create(AssertionTest.class);
212.45 - }
212.46 -}
213.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/BrwsrCheckTest.java Tue Mar 05 07:57:16 2013 +0100
213.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
213.3 @@ -1,57 +0,0 @@
213.4 -/**
213.5 - * Back 2 Browser Bytecode Translator
213.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
213.7 - *
213.8 - * This program is free software: you can redistribute it and/or modify
213.9 - * it under the terms of the GNU General Public License as published by
213.10 - * the Free Software Foundation, version 2 of the License.
213.11 - *
213.12 - * This program is distributed in the hope that it will be useful,
213.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
213.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
213.15 - * GNU General Public License for more details.
213.16 - *
213.17 - * You should have received a copy of the GNU General Public License
213.18 - * along with this program. Look for COPYING file in the top folder.
213.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
213.20 - */
213.21 -package org.apidesign.bck2brwsr.tck;
213.22 -
213.23 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
213.24 -import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
213.25 -import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
213.26 -import org.apidesign.bck2brwsr.vmtest.VMTest;
213.27 -import org.testng.annotations.Factory;
213.28 -
213.29 -/**
213.30 - *
213.31 - * @author Jaroslav Tulach <jtulach@netbeans.org>
213.32 - */
213.33 -public class BrwsrCheckTest {
213.34 -
213.35 - @BrwsrTest public void assertWindowObjectIsDefined() {
213.36 - assert window() != null : "No window object found!";
213.37 - }
213.38 -
213.39 -
213.40 -
213.41 -
213.42 - @HtmlFragment("<h1 id='hello'>\n"
213.43 - + "Hello!\n"
213.44 - + "</h1>\n")
213.45 - @BrwsrTest public void accessProvidedFragment() {
213.46 - assert getElementById("hello") != null : "Element with 'hello' ID found";
213.47 - }
213.48 -
213.49 - @Factory
213.50 - public static Object[] create() {
213.51 - return VMTest.create(BrwsrCheckTest.class);
213.52 - }
213.53 -
213.54 -
213.55 - @JavaScriptBody(args = {}, body = "return window;")
213.56 - private static native Object window();
213.57 -
213.58 - @JavaScriptBody(args = { "id" }, body = "return window.document.getElementById(id);")
213.59 - private static native Object getElementById(String id);
213.60 -}
214.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ByteArithmeticTest.java Tue Mar 05 07:57:16 2013 +0100
214.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
214.3 @@ -1,102 +0,0 @@
214.4 -/**
214.5 - * Back 2 Browser Bytecode Translator
214.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
214.7 - *
214.8 - * This program is free software: you can redistribute it and/or modify
214.9 - * it under the terms of the GNU General Public License as published by
214.10 - * the Free Software Foundation, version 2 of the License.
214.11 - *
214.12 - * This program is distributed in the hope that it will be useful,
214.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
214.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
214.15 - * GNU General Public License for more details.
214.16 - *
214.17 - * You should have received a copy of the GNU General Public License
214.18 - * along with this program. Look for COPYING file in the top folder.
214.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
214.20 - */
214.21 -package org.apidesign.bck2brwsr.tck;
214.22 -
214.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
214.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
214.25 -import org.testng.annotations.Factory;
214.26 -
214.27 -/**
214.28 - *
214.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
214.30 - */
214.31 -public class ByteArithmeticTest {
214.32 -
214.33 - private static byte add(byte x, byte y) {
214.34 - return (byte)(x + y);
214.35 - }
214.36 -
214.37 - private static byte sub(byte x, byte y) {
214.38 - return (byte)(x - y);
214.39 - }
214.40 -
214.41 - private static byte mul(byte x, byte y) {
214.42 - return (byte)(x * y);
214.43 - }
214.44 -
214.45 - private static byte div(byte x, byte y) {
214.46 - return (byte)(x / y);
214.47 - }
214.48 -
214.49 - private static byte mod(byte x, byte y) {
214.50 - return (byte)(x % y);
214.51 - }
214.52 -
214.53 - @Compare public byte conversion() {
214.54 - return (byte)123456;
214.55 - }
214.56 -
214.57 - @Compare public byte addOverflow() {
214.58 - return add(Byte.MAX_VALUE, (byte)1);
214.59 - }
214.60 -
214.61 - @Compare public byte subUnderflow() {
214.62 - return sub(Byte.MIN_VALUE, (byte)1);
214.63 - }
214.64 -
214.65 - @Compare public byte addMaxByteAndMaxByte() {
214.66 - return add(Byte.MAX_VALUE, Byte.MAX_VALUE);
214.67 - }
214.68 -
214.69 - @Compare public byte subMinByteAndMinByte() {
214.70 - return sub(Byte.MIN_VALUE, Byte.MIN_VALUE);
214.71 - }
214.72 -
214.73 - @Compare public byte multiplyMaxByte() {
214.74 - return mul(Byte.MAX_VALUE, (byte)2);
214.75 - }
214.76 -
214.77 - @Compare public byte multiplyMaxByteAndMaxByte() {
214.78 - return mul(Byte.MAX_VALUE, Byte.MAX_VALUE);
214.79 - }
214.80 -
214.81 - @Compare public byte multiplyMinByte() {
214.82 - return mul(Byte.MIN_VALUE, (byte)2);
214.83 - }
214.84 -
214.85 - @Compare public byte multiplyMinByteAndMinByte() {
214.86 - return mul(Byte.MIN_VALUE, Byte.MIN_VALUE);
214.87 - }
214.88 -
214.89 - @Compare public byte multiplyPrecision() {
214.90 - return mul((byte)17638, (byte)1103);
214.91 - }
214.92 -
214.93 - @Compare public byte division() {
214.94 - return div((byte)1, (byte)2);
214.95 - }
214.96 -
214.97 - @Compare public byte divisionReminder() {
214.98 - return mod((byte)1, (byte)2);
214.99 - }
214.100 -
214.101 - @Factory
214.102 - public static Object[] create() {
214.103 - return VMTest.create(ByteArithmeticTest.class);
214.104 - }
214.105 -}
215.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CloneTest.java Tue Mar 05 07:57:16 2013 +0100
215.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
215.3 @@ -1,73 +0,0 @@
215.4 -/**
215.5 - * Back 2 Browser Bytecode Translator
215.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
215.7 - *
215.8 - * This program is free software: you can redistribute it and/or modify
215.9 - * it under the terms of the GNU General Public License as published by
215.10 - * the Free Software Foundation, version 2 of the License.
215.11 - *
215.12 - * This program is distributed in the hope that it will be useful,
215.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
215.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
215.15 - * GNU General Public License for more details.
215.16 - *
215.17 - * You should have received a copy of the GNU General Public License
215.18 - * along with this program. Look for COPYING file in the top folder.
215.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
215.20 - */
215.21 -package org.apidesign.bck2brwsr.tck;
215.22 -
215.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
215.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
215.25 -import org.testng.annotations.Factory;
215.26 -
215.27 -/**
215.28 - *
215.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
215.30 - */
215.31 -public class CloneTest {
215.32 - private int value;
215.33 -
215.34 - @Compare
215.35 - public Object notSupported() throws CloneNotSupportedException {
215.36 - return this.clone();
215.37 - }
215.38 -
215.39 - @Compare public String sameClass() throws CloneNotSupportedException {
215.40 - return new Clnbl().clone().getClass().getName();
215.41 - }
215.42 -
215.43 - @Compare public boolean differentInstance() throws CloneNotSupportedException {
215.44 - Clnbl orig = new Clnbl();
215.45 - return orig == orig.clone();
215.46 - }
215.47 -
215.48 - @Compare public int sameReference() throws CloneNotSupportedException {
215.49 - CloneTest self = this;
215.50 - Clnbl orig = new Clnbl();
215.51 - self.value = 33;
215.52 - orig.ref = self;
215.53 - return ((Clnbl)orig.clone()).ref.value;
215.54 - }
215.55 -
215.56 - @Compare public int sameValue() throws CloneNotSupportedException {
215.57 - Clnbl orig = new Clnbl();
215.58 - orig.value = 10;
215.59 - return ((Clnbl)orig.clone()).value;
215.60 - }
215.61 -
215.62 - @Factory
215.63 - public static Object[] create() {
215.64 - return VMTest.create(CloneTest.class);
215.65 - }
215.66 -
215.67 - public static final class Clnbl implements Cloneable {
215.68 - public CloneTest ref;
215.69 - private int value;
215.70 -
215.71 - @Override
215.72 - public Object clone() throws CloneNotSupportedException {
215.73 - return super.clone();
215.74 - }
215.75 - }
215.76 -}
216.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareByteArrayTest.java Tue Mar 05 07:57:16 2013 +0100
216.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
216.3 @@ -1,103 +0,0 @@
216.4 -/**
216.5 - * Back 2 Browser Bytecode Translator
216.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
216.7 - *
216.8 - * This program is free software: you can redistribute it and/or modify
216.9 - * it under the terms of the GNU General Public License as published by
216.10 - * the Free Software Foundation, version 2 of the License.
216.11 - *
216.12 - * This program is distributed in the hope that it will be useful,
216.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
216.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
216.15 - * GNU General Public License for more details.
216.16 - *
216.17 - * You should have received a copy of the GNU General Public License
216.18 - * along with this program. Look for COPYING file in the top folder.
216.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
216.20 - */
216.21 -package org.apidesign.bck2brwsr.tck;
216.22 -
216.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
216.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
216.25 -import org.testng.annotations.Factory;
216.26 -
216.27 -/**
216.28 - *
216.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
216.30 - */
216.31 -public class CompareByteArrayTest {
216.32 - @Compare public int byteArraySum() {
216.33 - byte[] arr = createArray();
216.34 - return sumByteArr(arr);
216.35 - }
216.36 -
216.37 - @Compare public int countZeros() {
216.38 - int zeros = 0;
216.39 - for (Byte b : createArray()) {
216.40 - if (b == 0) {
216.41 - zeros++;
216.42 - }
216.43 - }
216.44 - return zeros;
216.45 - }
216.46 -
216.47 - private static int sumByteArr(byte[] arr) {
216.48 - int sum = 0;
216.49 - for (int i = 0; i < arr.length; i++) {
216.50 - sum += arr[i];
216.51 - }
216.52 - return sum;
216.53 - }
216.54 -
216.55 - @Compare public String noOutOfBounds() {
216.56 - return atIndex(1);
216.57 - }
216.58 -
216.59 - @Compare public String outOfBounds() {
216.60 - return atIndex(5);
216.61 - }
216.62 -
216.63 - @Compare public String outOfBoundsMinus() {
216.64 - return atIndex(-1);
216.65 - }
216.66 -
216.67 - @Compare public String toOfBounds() {
216.68 - return toIndex(5);
216.69 - }
216.70 -
216.71 - @Compare public String toOfBoundsMinus() {
216.72 - return toIndex(-1);
216.73 - }
216.74 -
216.75 - @Compare public int multiArrayLength() {
216.76 - int[][] arr = new int[1][0];
216.77 - return arr[0].length;
216.78 - }
216.79 -
216.80 - @Compare public int multiObjectArrayLength() {
216.81 - Object[][] arr = new Object[1][0];
216.82 - return arr[0].length;
216.83 - }
216.84 -
216.85 - private static final int[] arr = { 0, 1, 2 };
216.86 - public static String atIndex(int at) {
216.87 - return "at@" + arr[at];
216.88 - }
216.89 - public static String toIndex(int at) {
216.90 - arr[at] = 10;
216.91 - return "ok";
216.92 - }
216.93 -
216.94 -
216.95 - @Factory
216.96 - public static Object[] create() {
216.97 - return VMTest.create(CompareByteArrayTest.class);
216.98 - }
216.99 -
216.100 - private byte[] createArray() {
216.101 - byte[] arr = new byte[10];
216.102 - arr[5] = 3;
216.103 - arr[7] = 8;
216.104 - return arr;
216.105 - }
216.106 -}
217.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareHashTest.java Tue Mar 05 07:57:16 2013 +0100
217.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
217.3 @@ -1,50 +0,0 @@
217.4 -/**
217.5 - * Back 2 Browser Bytecode Translator
217.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
217.7 - *
217.8 - * This program is free software: you can redistribute it and/or modify
217.9 - * it under the terms of the GNU General Public License as published by
217.10 - * the Free Software Foundation, version 2 of the License.
217.11 - *
217.12 - * This program is distributed in the hope that it will be useful,
217.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
217.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
217.15 - * GNU General Public License for more details.
217.16 - *
217.17 - * You should have received a copy of the GNU General Public License
217.18 - * along with this program. Look for COPYING file in the top folder.
217.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
217.20 - */
217.21 -package org.apidesign.bck2brwsr.tck;
217.22 -
217.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
217.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
217.25 -import org.testng.annotations.Factory;
217.26 -
217.27 -/**
217.28 - *
217.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
217.30 - */
217.31 -public class CompareHashTest {
217.32 - @Compare public int hashOfString() {
217.33 - return "Ahoj".hashCode();
217.34 - }
217.35 -
217.36 - @Compare public int hashRemainsYieldsZero() {
217.37 - Object o = new Object();
217.38 - return o.hashCode() - o.hashCode();
217.39 - }
217.40 -
217.41 - @Compare public int initializeInStatic() {
217.42 - return StaticUse.NON_NULL.hashCode() - StaticUse.NON_NULL.hashCode();
217.43 - }
217.44 -
217.45 - @Compare public int hashOfInt() {
217.46 - return Integer.valueOf(Integer.MAX_VALUE).hashCode();
217.47 - }
217.48 -
217.49 - @Factory
217.50 - public static Object[] create() {
217.51 - return VMTest.create(CompareHashTest.class);
217.52 - }
217.53 -}
218.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareIntArrayTest.java Tue Mar 05 07:57:16 2013 +0100
218.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
218.3 @@ -1,63 +0,0 @@
218.4 -/**
218.5 - * Back 2 Browser Bytecode Translator
218.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
218.7 - *
218.8 - * This program is free software: you can redistribute it and/or modify
218.9 - * it under the terms of the GNU General Public License as published by
218.10 - * the Free Software Foundation, version 2 of the License.
218.11 - *
218.12 - * This program is distributed in the hope that it will be useful,
218.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
218.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
218.15 - * GNU General Public License for more details.
218.16 - *
218.17 - * You should have received a copy of the GNU General Public License
218.18 - * along with this program. Look for COPYING file in the top folder.
218.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
218.20 - */
218.21 -package org.apidesign.bck2brwsr.tck;
218.22 -
218.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
218.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
218.25 -import org.testng.annotations.Factory;
218.26 -
218.27 -/**
218.28 - *
218.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
218.30 - */
218.31 -public class CompareIntArrayTest {
218.32 - @Compare public int integerArraySum() {
218.33 - int[] arr = createArray();
218.34 - return sumIntArr(arr);
218.35 - }
218.36 -
218.37 - @Compare public int countZeros() {
218.38 - int zeros = 0;
218.39 - for (Integer i : createArray()) {
218.40 - if (i == 0) {
218.41 - zeros++;
218.42 - }
218.43 - }
218.44 - return zeros;
218.45 - }
218.46 -
218.47 - private static int sumIntArr(int[] arr) {
218.48 - int sum = 0;
218.49 - for (int i = 0; i < arr.length; i++) {
218.50 - sum += arr[i];
218.51 - }
218.52 - return sum;
218.53 - }
218.54 -
218.55 - @Factory
218.56 - public static Object[] create() {
218.57 - return VMTest.create(CompareIntArrayTest.class);
218.58 - }
218.59 -
218.60 - private int[] createArray() {
218.61 - int[] arr = new int[10];
218.62 - arr[5] = 3;
218.63 - arr[7] = 8;
218.64 - return arr;
218.65 - }
218.66 -}
219.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java Tue Mar 05 07:57:16 2013 +0100
219.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
219.3 @@ -1,162 +0,0 @@
219.4 -/**
219.5 - * Back 2 Browser Bytecode Translator
219.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
219.7 - *
219.8 - * This program is free software: you can redistribute it and/or modify
219.9 - * it under the terms of the GNU General Public License as published by
219.10 - * the Free Software Foundation, version 2 of the License.
219.11 - *
219.12 - * This program is distributed in the hope that it will be useful,
219.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
219.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
219.15 - * GNU General Public License for more details.
219.16 - *
219.17 - * You should have received a copy of the GNU General Public License
219.18 - * along with this program. Look for COPYING file in the top folder.
219.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
219.20 - */
219.21 -package org.apidesign.bck2brwsr.tck;
219.22 -
219.23 -import java.io.UnsupportedEncodingException;
219.24 -import java.net.MalformedURLException;
219.25 -import java.net.URL;
219.26 -import org.apidesign.bck2brwsr.vmtest.Compare;
219.27 -import org.apidesign.bck2brwsr.vmtest.VMTest;
219.28 -import org.testng.annotations.Factory;
219.29 -
219.30 -/**
219.31 - *
219.32 - * @author Jaroslav Tulach <jtulach@netbeans.org>
219.33 - */
219.34 -public class CompareStringsTest {
219.35 - @Compare public String firstChar() {
219.36 - return "" + ("Hello".toCharArray()[0]);
219.37 - }
219.38 -
219.39 - @Compare public String classCast() {
219.40 - Object o = firstChar();
219.41 - return String.class.cast(o);
219.42 - }
219.43 -
219.44 - @Compare public String classCastThrown() {
219.45 - Object o = null;
219.46 - return String.class.cast(o);
219.47 - }
219.48 -
219.49 - @Compare public boolean equalToNull() {
219.50 - return "Ahoj".equals(null);
219.51 - }
219.52 -
219.53 - @Compare public int highByteLenght() {
219.54 - byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
219.55 - return new String(arr, 0).length();
219.56 - }
219.57 -
219.58 - @Compare public String highByte() {
219.59 - byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
219.60 - StringBuilder sb = new StringBuilder();
219.61 - sb.append("pref:");
219.62 - sb.append(new String(arr, 0));
219.63 - return sb.toString();
219.64 - }
219.65 -
219.66 - @Compare public static Object compareURLs() throws MalformedURLException {
219.67 - return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString();
219.68 - }
219.69 -
219.70 - @Compare public String deleteLastTwoCharacters() {
219.71 - StringBuilder sb = new StringBuilder();
219.72 - sb.append("453.0");
219.73 - if (sb.toString().endsWith(".0")) {
219.74 - final int l = sb.length();
219.75 - sb.delete(l - 2, l);
219.76 - }
219.77 - return sb.toString().toString();
219.78 - }
219.79 -
219.80 - @Compare public String nameOfStringClass() throws Exception {
219.81 - return Class.forName("java.lang.String").getName();
219.82 - }
219.83 - @Compare public String nameOfArrayClass() throws Exception {
219.84 - return Class.forName("org.apidesign.bck2brwsr.tck.CompareHashTest").getName();
219.85 - }
219.86 -
219.87 - @Compare public String lowerHello() {
219.88 - return "HeLlO".toLowerCase();
219.89 - }
219.90 -
219.91 - @Compare public String lowerA() {
219.92 - return String.valueOf(Character.toLowerCase('A')).toString();
219.93 - }
219.94 - @Compare public String upperHello() {
219.95 - return "hello".toUpperCase();
219.96 - }
219.97 -
219.98 - @Compare public String upperA() {
219.99 - return String.valueOf(Character.toUpperCase('a')).toString();
219.100 - }
219.101 -
219.102 - @Compare public boolean matchRegExp() throws Exception {
219.103 - return "58038503".matches("\\d*");
219.104 - }
219.105 -
219.106 - @Compare public boolean doesNotMatchRegExp() throws Exception {
219.107 - return "58038503GH".matches("\\d*");
219.108 - }
219.109 -
219.110 - @Compare public boolean doesNotMatchRegExpFully() throws Exception {
219.111 - return "Hello".matches("Hell");
219.112 - }
219.113 -
219.114 - @Compare public String emptyCharArray() {
219.115 - char[] arr = new char[10];
219.116 - return new String(arr);
219.117 - }
219.118 -
219.119 - @Compare public String variousCharacterTests() throws Exception {
219.120 - StringBuilder sb = new StringBuilder();
219.121 -
219.122 - sb.append(Character.isUpperCase('a'));
219.123 - sb.append(Character.isUpperCase('A'));
219.124 - sb.append(Character.isLowerCase('a'));
219.125 - sb.append(Character.isLowerCase('A'));
219.126 -
219.127 - sb.append(Character.isLetter('A'));
219.128 - sb.append(Character.isLetterOrDigit('9'));
219.129 - sb.append(Character.isLetterOrDigit('A'));
219.130 - sb.append(Character.isLetter('0'));
219.131 -
219.132 - return sb.toString().toString();
219.133 - }
219.134 -
219.135 - @Compare
219.136 - public String nullFieldInitialized() {
219.137 - NullField nf = new NullField();
219.138 - return ("" + nf.name).toString();
219.139 - }
219.140 - @Compare
219.141 - public String toUTFString() throws UnsupportedEncodingException {
219.142 - byte[] arr = {
219.143 - (byte) -59, (byte) -67, (byte) 108, (byte) 117, (byte) -59, (byte) -91,
219.144 - (byte) 111, (byte) 117, (byte) -60, (byte) -115, (byte) 107, (byte) -61,
219.145 - (byte) -67, (byte) 32, (byte) 107, (byte) -59, (byte) -81, (byte) -59,
219.146 - (byte) -120
219.147 - };
219.148 - return new String(arr, "utf-8");
219.149 - }
219.150 -
219.151 - @Compare
219.152 - public int stringToBytesLenght() throws UnsupportedEncodingException {
219.153 - return "\u017dlu\u0165ou\u010dk\u00fd k\u016f\u0148".getBytes("utf8").length;
219.154 - }
219.155 -
219.156 - @Factory
219.157 - public static Object[] create() {
219.158 - return VMTest.create(CompareStringsTest.class);
219.159 - }
219.160 -
219.161 - private static final class NullField {
219.162 -
219.163 - String name;
219.164 - }
219.165 -}
220.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java Tue Mar 05 07:57:16 2013 +0100
220.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
220.3 @@ -1,106 +0,0 @@
220.4 -/**
220.5 - * Back 2 Browser Bytecode Translator
220.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
220.7 - *
220.8 - * This program is free software: you can redistribute it and/or modify
220.9 - * it under the terms of the GNU General Public License as published by
220.10 - * the Free Software Foundation, version 2 of the License.
220.11 - *
220.12 - * This program is distributed in the hope that it will be useful,
220.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
220.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
220.15 - * GNU General Public License for more details.
220.16 - *
220.17 - * You should have received a copy of the GNU General Public License
220.18 - * along with this program. Look for COPYING file in the top folder.
220.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
220.20 - */
220.21 -package org.apidesign.bck2brwsr.tck;
220.22 -
220.23 -import java.io.InputStream;
220.24 -import java.net.URL;
220.25 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
220.26 -import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
220.27 -import org.apidesign.bck2brwsr.vmtest.Http;
220.28 -import org.apidesign.bck2brwsr.vmtest.VMTest;
220.29 -import org.testng.annotations.Factory;
220.30 -
220.31 -/**
220.32 - *
220.33 - * @author Jaroslav Tulach <jtulach@netbeans.org>
220.34 - */
220.35 -public class HttpResourceTest {
220.36 -
220.37 - @Http({
220.38 - @Http.Resource(path = "/xhr", content = "Hello Brwsr!", mimeType = "text/plain")
220.39 - })
220.40 - @BrwsrTest
220.41 -
220.42 -
220.43 - public String testReadContentViaXHR() throws Exception {
220.44 - String msg = read("/xhr");
220.45 - assert "Hello Brwsr!".equals(msg) : "The message was " + msg;
220.46 - return msg;
220.47 - }
220.48 -
220.49 - @Http({
220.50 - @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
220.51 - })
220.52 - @BrwsrTest
220.53 - public String testReadContentViaURL() throws Exception {
220.54 - URL url = new URL("http:/url");
220.55 - String msg = (String) url.getContent();
220.56 - assert "Hello via URL!".equals(msg) : "The message was " + msg;
220.57 - return msg;
220.58 - }
220.59 - @Http({
220.60 - @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
220.61 - })
220.62 - @BrwsrTest
220.63 - public String testReadContentViaURLWithStringParam() throws Exception {
220.64 - URL url = new URL("http:/url");
220.65 - String msg = (String) url.getContent(new Class[] { String.class });
220.66 - assert "Hello via URL!".equals(msg) : "The message was " + msg;
220.67 - return msg;
220.68 - }
220.69 -
220.70 - @Http({
220.71 - @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
220.72 - })
220.73 - @BrwsrTest
220.74 - public void testReadByte() throws Exception {
220.75 - URL url = new URL("http:/bytes");
220.76 - final Object res = url.getContent(new Class[] { byte[].class });
220.77 - assert res instanceof byte[] : "Expecting byte[]: " + res;
220.78 - byte[] arr = (byte[]) res;
220.79 - assert arr.length == 1 : "One byte " + arr.length;
220.80 - assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
220.81 - }
220.82 -
220.83 - @Http({
220.84 - @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
220.85 - })
220.86 - @BrwsrTest
220.87 - public void testReadByteViaInputStream() throws Exception {
220.88 - URL url = new URL("http:/bytes");
220.89 - InputStream is = url.openStream();
220.90 - byte[] arr = new byte[10];
220.91 - int len = is.read(arr);
220.92 - assert len == 1 : "One byte " + len;
220.93 - assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
220.94 - }
220.95 -
220.96 - @JavaScriptBody(args = { "url" }, body =
220.97 - "var req = new XMLHttpRequest();\n"
220.98 - + "req.open('GET', url, false);\n"
220.99 - + "req.send();\n"
220.100 - + "return req.responseText;"
220.101 - )
220.102 - private static native String read(String url);
220.103 -
220.104 -
220.105 - @Factory
220.106 - public static Object[] create() {
220.107 - return VMTest.create(HttpResourceTest.class);
220.108 - }
220.109 -}
221.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/InheritanceA.java Tue Mar 05 07:57:16 2013 +0100
221.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
221.3 @@ -1,34 +0,0 @@
221.4 -/**
221.5 - * Back 2 Browser Bytecode Translator
221.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
221.7 - *
221.8 - * This program is free software: you can redistribute it and/or modify
221.9 - * it under the terms of the GNU General Public License as published by
221.10 - * the Free Software Foundation, version 2 of the License.
221.11 - *
221.12 - * This program is distributed in the hope that it will be useful,
221.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
221.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
221.15 - * GNU General Public License for more details.
221.16 - *
221.17 - * You should have received a copy of the GNU General Public License
221.18 - * along with this program. Look for COPYING file in the top folder.
221.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
221.20 - */
221.21 -package org.apidesign.bck2brwsr.tck;
221.22 -
221.23 -/**
221.24 - *
221.25 - * @author Jaroslav Tulach <jtulach@netbeans.org>
221.26 - */
221.27 -public class InheritanceA {
221.28 - private String name;
221.29 -
221.30 - public void setA(String n) {
221.31 - this.name = n;
221.32 - }
221.33 -
221.34 - public String getA() {
221.35 - return name;
221.36 - }
221.37 -}
222.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/InheritanceB.java Tue Mar 05 07:57:16 2013 +0100
222.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
222.3 @@ -1,34 +0,0 @@
222.4 -/**
222.5 - * Back 2 Browser Bytecode Translator
222.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
222.7 - *
222.8 - * This program is free software: you can redistribute it and/or modify
222.9 - * it under the terms of the GNU General Public License as published by
222.10 - * the Free Software Foundation, version 2 of the License.
222.11 - *
222.12 - * This program is distributed in the hope that it will be useful,
222.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
222.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
222.15 - * GNU General Public License for more details.
222.16 - *
222.17 - * You should have received a copy of the GNU General Public License
222.18 - * along with this program. Look for COPYING file in the top folder.
222.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
222.20 - */
222.21 -package org.apidesign.bck2brwsr.tck;
222.22 -
222.23 -/**
222.24 - *
222.25 - * @author Jaroslav Tulach <jtulach@netbeans.org>
222.26 - */
222.27 -public class InheritanceB extends InheritanceA {
222.28 - private String name;
222.29 -
222.30 - public void setB(String n) {
222.31 - this.name = n;
222.32 - }
222.33 -
222.34 - public String getB() {
222.35 - return name;
222.36 - }
222.37 -}
223.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/InheritanceTest.java Tue Mar 05 07:57:16 2013 +0100
223.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
223.3 @@ -1,41 +0,0 @@
223.4 -/**
223.5 - * Back 2 Browser Bytecode Translator
223.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
223.7 - *
223.8 - * This program is free software: you can redistribute it and/or modify
223.9 - * it under the terms of the GNU General Public License as published by
223.10 - * the Free Software Foundation, version 2 of the License.
223.11 - *
223.12 - * This program is distributed in the hope that it will be useful,
223.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
223.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
223.15 - * GNU General Public License for more details.
223.16 - *
223.17 - * You should have received a copy of the GNU General Public License
223.18 - * along with this program. Look for COPYING file in the top folder.
223.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
223.20 - */
223.21 -package org.apidesign.bck2brwsr.tck;
223.22 -
223.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
223.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
223.25 -import org.testng.annotations.Factory;
223.26 -
223.27 -/**
223.28 - *
223.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
223.30 - */
223.31 -public class InheritanceTest {
223.32 -
223.33 - @Compare public String checkFieldsIndependent() throws ClassNotFoundException {
223.34 - InheritanceB ib = new InheritanceB();
223.35 - ib.setA("A");
223.36 - ib.setB("B");
223.37 - return "A: " + ib.getA() + " B: " + ib.getB();
223.38 - }
223.39 -
223.40 - @Factory
223.41 - public static Object[] create() {
223.42 - return VMTest.create(InheritanceTest.class);
223.43 - }
223.44 -}
224.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/IntegerArithmeticTest.java Tue Mar 05 07:57:16 2013 +0100
224.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
224.3 @@ -1,166 +0,0 @@
224.4 -/**
224.5 - * Back 2 Browser Bytecode Translator
224.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
224.7 - *
224.8 - * This program is free software: you can redistribute it and/or modify
224.9 - * it under the terms of the GNU General Public License as published by
224.10 - * the Free Software Foundation, version 2 of the License.
224.11 - *
224.12 - * This program is distributed in the hope that it will be useful,
224.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
224.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
224.15 - * GNU General Public License for more details.
224.16 - *
224.17 - * You should have received a copy of the GNU General Public License
224.18 - * along with this program. Look for COPYING file in the top folder.
224.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
224.20 - */
224.21 -package org.apidesign.bck2brwsr.tck;
224.22 -
224.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
224.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
224.25 -import org.testng.annotations.Factory;
224.26 -
224.27 -/**
224.28 - *
224.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
224.30 - */
224.31 -public class IntegerArithmeticTest {
224.32 -
224.33 - private static int add(int x, int y) {
224.34 - return x + y;
224.35 - }
224.36 -
224.37 - private static int sub(int x, int y) {
224.38 - return x - y;
224.39 - }
224.40 -
224.41 - private static int mul(int x, int y) {
224.42 - return x * y;
224.43 - }
224.44 -
224.45 - private static int div(int x, int y) {
224.46 - return x / y;
224.47 - }
224.48 -
224.49 - private static int mod(int x, int y) {
224.50 - return x % y;
224.51 - }
224.52 -
224.53 - private static int neg(int x) {
224.54 - return (-x);
224.55 - }
224.56 -
224.57 - private static float fadd(float x, float y) {
224.58 - return x + y;
224.59 - }
224.60 -
224.61 - private static double dadd(double x, double y) {
224.62 - return x + y;
224.63 - }
224.64 -
224.65 - @Compare public int addOverflow() {
224.66 - return add(Integer.MAX_VALUE, 1);
224.67 - }
224.68 -
224.69 - @Compare public int subUnderflow() {
224.70 - return sub(Integer.MIN_VALUE, 1);
224.71 - }
224.72 -
224.73 - @Compare public int addMaxIntAndMaxInt() {
224.74 - return add(Integer.MAX_VALUE, Integer.MAX_VALUE);
224.75 - }
224.76 -
224.77 - @Compare public int subMinIntAndMinInt() {
224.78 - return sub(Integer.MIN_VALUE, Integer.MIN_VALUE);
224.79 - }
224.80 -
224.81 - @Compare public int multiplyMaxInt() {
224.82 - return mul(Integer.MAX_VALUE, 2);
224.83 - }
224.84 -
224.85 - @Compare public int multiplyMaxIntAndMaxInt() {
224.86 - return mul(Integer.MAX_VALUE, Integer.MAX_VALUE);
224.87 - }
224.88 -
224.89 - @Compare public int multiplyMinInt() {
224.90 - return mul(Integer.MIN_VALUE, 2);
224.91 - }
224.92 -
224.93 - @Compare public int multiplyMinIntAndMinInt() {
224.94 - return mul(Integer.MIN_VALUE, Integer.MIN_VALUE);
224.95 - }
224.96 -
224.97 - @Compare public int multiplyPrecision() {
224.98 - return mul(119106029, 1103515245);
224.99 - }
224.100 -
224.101 - @Compare public int division() {
224.102 - return div(1, 2);
224.103 - }
224.104 -
224.105 - @Compare public int divisionReminder() {
224.106 - return mod(1, 2);
224.107 - }
224.108 -
224.109 - @Compare public int negativeDivision() {
224.110 - return div(-7, 3);
224.111 - }
224.112 -
224.113 - @Compare public int negativeDivisionReminder() {
224.114 - return mod(-7, 3);
224.115 - }
224.116 -
224.117 - @Compare public int conversionFromFloat() {
224.118 - return (int) fadd(-2, -0.6f);
224.119 - }
224.120 -
224.121 - @Compare public int conversionFromDouble() {
224.122 - return (int) dadd(-2, -0.6);
224.123 - }
224.124 -
224.125 - @Compare public boolean divByZeroThrowsArithmeticException() {
224.126 - try {
224.127 - div(1, 0);
224.128 - return false;
224.129 - } catch (final ArithmeticException e) {
224.130 - return true;
224.131 - }
224.132 - }
224.133 -
224.134 - @Compare public boolean modByZeroThrowsArithmeticException() {
224.135 - try {
224.136 - mod(1, 0);
224.137 - return false;
224.138 - } catch (final ArithmeticException e) {
224.139 - return true;
224.140 - }
224.141 - }
224.142 -
224.143 - @Compare public int negate() {
224.144 - return neg(123456);
224.145 - }
224.146 -
224.147 - @Compare public int negateMaxInt() {
224.148 - return neg(Integer.MAX_VALUE);
224.149 - }
224.150 -
224.151 - @Compare public int negateMinInt() {
224.152 - return neg(Integer.MIN_VALUE);
224.153 - }
224.154 -
224.155 - @Compare public int sumTwoDimensions() {
224.156 - int[][] matrix = createMatrix(4, 3);
224.157 - matrix[0][0] += 10;
224.158 - return matrix[0][0];
224.159 - }
224.160 -
224.161 - static int[][] createMatrix(int x, int y) {
224.162 - return new int[x][y];
224.163 - }
224.164 -
224.165 - @Factory
224.166 - public static Object[] create() {
224.167 - return VMTest.create(IntegerArithmeticTest.class);
224.168 - }
224.169 -}
225.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java Tue Mar 05 07:57:16 2013 +0100
225.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
225.3 @@ -1,376 +0,0 @@
225.4 -/**
225.5 - * Back 2 Browser Bytecode Translator
225.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
225.7 - *
225.8 - * This program is free software: you can redistribute it and/or modify
225.9 - * it under the terms of the GNU General Public License as published by
225.10 - * the Free Software Foundation, version 2 of the License.
225.11 - *
225.12 - * This program is distributed in the hope that it will be useful,
225.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
225.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
225.15 - * GNU General Public License for more details.
225.16 - *
225.17 - * You should have received a copy of the GNU General Public License
225.18 - * along with this program. Look for COPYING file in the top folder.
225.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
225.20 - */
225.21 -package org.apidesign.bck2brwsr.tck;
225.22 -
225.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
225.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
225.25 -import org.testng.annotations.Factory;
225.26 -
225.27 -/**
225.28 - *
225.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
225.30 - */
225.31 -public class LongArithmeticTest {
225.32 -
225.33 - private static long add(long x, long y) {
225.34 - return (x + y);
225.35 - }
225.36 -
225.37 - private static long sub(long x, long y) {
225.38 - return (x - y);
225.39 - }
225.40 -
225.41 - private static long mul(long x, long y) {
225.42 - return (x * y);
225.43 - }
225.44 -
225.45 - private static long div(long x, long y) {
225.46 - return (x / y);
225.47 - }
225.48 -
225.49 - private static long mod(long x, long y) {
225.50 - return (x % y);
225.51 - }
225.52 -
225.53 - private static long neg(long x) {
225.54 - return (-x);
225.55 - }
225.56 -
225.57 - private static long shl(long x, int b) {
225.58 - return (x << b);
225.59 - }
225.60 -
225.61 - private static long shr(long x, int b) {
225.62 - return (x >> b);
225.63 - }
225.64 -
225.65 - private static long ushr(long x, int b) {
225.66 - return (x >>> b);
225.67 - }
225.68 -
225.69 - private static long and(long x, long y) {
225.70 - return (x & y);
225.71 - }
225.72 -
225.73 - private static long or(long x, long y) {
225.74 - return (x | y);
225.75 - }
225.76 -
225.77 - private static long xor(long x, long y) {
225.78 - return (x ^ y);
225.79 - }
225.80 -
225.81 - private static float fadd(float x, float y) {
225.82 - return x + y;
225.83 - }
225.84 -
225.85 - private static double dadd(double x, double y) {
225.86 - return x + y;
225.87 - }
225.88 -
225.89 - public static int compare(long x, long y, int zero) {
225.90 - final int xyResult = compareL(x, y, zero);
225.91 - final int yxResult = compareL(y, x, zero);
225.92 -
225.93 - return ((xyResult + yxResult) == 0) ? xyResult : -2;
225.94 - }
225.95 -
225.96 - private static int compareL(long x, long y, int zero) {
225.97 - int result = -2;
225.98 - int trueCount = 0;
225.99 -
225.100 - x += zero;
225.101 - if (x == y) {
225.102 - result = 0;
225.103 - ++trueCount;
225.104 - }
225.105 -
225.106 - x += zero;
225.107 - if (x < y) {
225.108 - result = -1;
225.109 - ++trueCount;
225.110 - }
225.111 -
225.112 - x += zero;
225.113 - if (x > y) {
225.114 - result = 1;
225.115 - ++trueCount;
225.116 - }
225.117 -
225.118 - return (trueCount == 1) ? result : -2;
225.119 - }
225.120 -
225.121 - @Compare public long conversion() {
225.122 - return Long.MAX_VALUE;
225.123 - }
225.124 -
225.125 - @Compare public long negate1() {
225.126 - return neg(0x00fa37d7763e0ca1l);
225.127 - }
225.128 -
225.129 - @Compare public long negate2() {
225.130 - return neg(0x80fa37d7763e0ca1l);
225.131 - }
225.132 -
225.133 - @Compare public long negate3() {
225.134 - return neg(0xfffffffffffffeddl);
225.135 - }
225.136 -
225.137 - @Compare public long addOverflow() {
225.138 - return add(Long.MAX_VALUE, 1l);
225.139 - }
225.140 -
225.141 - @Compare public long subUnderflow() {
225.142 - return sub(Long.MIN_VALUE, 1l);
225.143 - }
225.144 -
225.145 - @Compare public long addMaxLongAndMaxLong() {
225.146 - return add(Long.MAX_VALUE, Long.MAX_VALUE);
225.147 - }
225.148 -
225.149 - @Compare public long subMinLongAndMinLong() {
225.150 - return sub(Long.MIN_VALUE, Long.MIN_VALUE);
225.151 - }
225.152 -
225.153 - @Compare public long subMinLongAndMaxLong() {
225.154 - return sub(Long.MIN_VALUE, Long.MAX_VALUE);
225.155 - }
225.156 -
225.157 - @Compare public long multiplyMaxLong() {
225.158 - return mul(Long.MAX_VALUE, 2l);
225.159 - }
225.160 -
225.161 - @Compare public long multiplyMaxLongAndMaxLong() {
225.162 - return mul(Long.MAX_VALUE, Long.MAX_VALUE);
225.163 - }
225.164 -
225.165 - @Compare public long multiplyMinLong() {
225.166 - return mul(Long.MIN_VALUE, 2l);
225.167 - }
225.168 -
225.169 - @Compare public long multiplyMinLongAndMinLong() {
225.170 - return mul(Long.MIN_VALUE, Long.MIN_VALUE);
225.171 - }
225.172 -
225.173 - @Compare public long multiplyPrecision() {
225.174 - return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
225.175 - }
225.176 -
225.177 - @Compare public long divideSmallPositiveNumbers() {
225.178 - return div(0xabcdef, 0x123);
225.179 - }
225.180 -
225.181 - @Compare public long divideSmallNegativeNumbers() {
225.182 - return div(-0xabcdef, -0x123);
225.183 - }
225.184 -
225.185 - @Compare public long divideSmallMixedNumbers() {
225.186 - return div(0xabcdef, -0x123);
225.187 - }
225.188 -
225.189 - @Compare public long dividePositiveNumbersOneDigitDenom() {
225.190 - return div(0xabcdef0102ffffl, 0x654);
225.191 - }
225.192 -
225.193 - @Compare public long divideNegativeNumbersOneDigitDenom() {
225.194 - return div(-0xabcdef0102ffffl, -0x654);
225.195 - }
225.196 -
225.197 - @Compare public long divideMixedNumbersOneDigitDenom() {
225.198 - return div(-0xabcdef0102ffffl, 0x654);
225.199 - }
225.200 -
225.201 - @Compare public long dividePositiveNumbersMultiDigitDenom() {
225.202 - return div(0x7ffefc003322aabbl, 0x89ab1000l);
225.203 - }
225.204 -
225.205 - @Compare public long divideNegativeNumbersMultiDigitDenom() {
225.206 - return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
225.207 - }
225.208 -
225.209 - @Compare public long divideMixedNumbersMultiDigitDenom() {
225.210 - return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
225.211 - }
225.212 -
225.213 - @Compare public long divideWithOverflow() {
225.214 - return div(0x8000fffe0000l, 0x8000ffffl);
225.215 - }
225.216 -
225.217 - @Compare public long divideWithCorrection() {
225.218 - return div(0x7fff800000000000l, 0x800000000001l);
225.219 - }
225.220 -
225.221 - @Compare public long moduloSmallPositiveNumbers() {
225.222 - return mod(0xabcdef, 0x123);
225.223 - }
225.224 -
225.225 - @Compare public long moduloSmallNegativeNumbers() {
225.226 - return mod(-0xabcdef, -0x123);
225.227 - }
225.228 -
225.229 - @Compare public long moduloSmallMixedNumbers() {
225.230 - return mod(0xabcdef, -0x123);
225.231 - }
225.232 -
225.233 - @Compare public long moduloPositiveNumbersOneDigitDenom() {
225.234 - return mod(0xabcdef0102ffffl, 0x654);
225.235 - }
225.236 -
225.237 - @Compare public long moduloNegativeNumbersOneDigitDenom() {
225.238 - return mod(-0xabcdef0102ffffl, -0x654);
225.239 - }
225.240 -
225.241 - @Compare public long moduloMixedNumbersOneDigitDenom() {
225.242 - return mod(-0xabcdef0102ffffl, 0x654);
225.243 - }
225.244 -
225.245 - @Compare public long moduloPositiveNumbersMultiDigitDenom() {
225.246 - return mod(0x7ffefc003322aabbl, 0x89ab1000l);
225.247 - }
225.248 -
225.249 - @Compare public long moduloNegativeNumbersMultiDigitDenom() {
225.250 - return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
225.251 - }
225.252 -
225.253 - @Compare public long moduloMixedNumbersMultiDigitDenom() {
225.254 - return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
225.255 - }
225.256 -
225.257 - @Compare public long moduloWithOverflow() {
225.258 - return mod(0x8000fffe0000l, 0x8000ffffl);
225.259 - }
225.260 -
225.261 - @Compare public long moduloWithCorrection() {
225.262 - return mod(0x7fff800000000000l, 0x800000000001l);
225.263 - }
225.264 -
225.265 - @Compare public long conversionFromFloatPositive() {
225.266 - return (long) fadd(2, 0.6f);
225.267 - }
225.268 -
225.269 - @Compare public long conversionFromFloatNegative() {
225.270 - return (long) fadd(-2, -0.6f);
225.271 - }
225.272 -
225.273 - @Compare public long conversionFromDoublePositive() {
225.274 - return (long) dadd(0x20ffff0000L, 0.6);
225.275 - }
225.276 -
225.277 - @Compare public long conversionFromDoubleNegative() {
225.278 - return (long) dadd(-0x20ffff0000L, -0.6);
225.279 - }
225.280 -
225.281 - @Compare public boolean divByZeroThrowsArithmeticException() {
225.282 - try {
225.283 - div(1, 0);
225.284 - return false;
225.285 - } catch (final ArithmeticException e) {
225.286 - return true;
225.287 - }
225.288 - }
225.289 -
225.290 - @Compare public boolean modByZeroThrowsArithmeticException() {
225.291 - try {
225.292 - mod(1, 0);
225.293 - return false;
225.294 - } catch (final ArithmeticException e) {
225.295 - return true;
225.296 - }
225.297 - }
225.298 -
225.299 - @Compare public long shiftL1() {
225.300 - return shl(0x00fa37d7763e0ca1l, 5);
225.301 - }
225.302 -
225.303 - @Compare public long shiftL2() {
225.304 - return shl(0x00fa37d7763e0ca1l, 32);
225.305 - }
225.306 -
225.307 - @Compare public long shiftL3() {
225.308 - return shl(0x00fa37d7763e0ca1l, 45);
225.309 - }
225.310 -
225.311 - @Compare public long shiftR1() {
225.312 - return shr(0x00fa37d7763e0ca1l, 5);
225.313 - }
225.314 -
225.315 - @Compare public long shiftR2() {
225.316 - return shr(0x00fa37d7763e0ca1l, 32);
225.317 - }
225.318 -
225.319 - @Compare public long shiftR3() {
225.320 - return shr(0x00fa37d7763e0ca1l, 45);
225.321 - }
225.322 -
225.323 - @Compare public long uShiftR1() {
225.324 - return ushr(0x00fa37d7763e0ca1l, 5);
225.325 - }
225.326 -
225.327 - @Compare public long uShiftR2() {
225.328 - return ushr(0x00fa37d7763e0ca1l, 45);
225.329 - }
225.330 -
225.331 - @Compare public long uShiftR3() {
225.332 - return ushr(0xf0fa37d7763e0ca1l, 5);
225.333 - }
225.334 -
225.335 - @Compare public long uShiftR4() {
225.336 - return ushr(0xf0fa37d7763e0ca1l, 45);
225.337 - }
225.338 -
225.339 - @Compare public long and1() {
225.340 - return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
225.341 - }
225.342 -
225.343 - @Compare public long or1() {
225.344 - return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
225.345 - }
225.346 -
225.347 - @Compare public long xor1() {
225.348 - return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
225.349 - }
225.350 -
225.351 - @Compare public long xor2() {
225.352 - return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
225.353 - }
225.354 -
225.355 - @Compare public long xor3() {
225.356 - return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
225.357 - }
225.358 -
225.359 - @Compare public int compareSameNumbers() {
225.360 - return compare(0x0000000000000000l, 0x0000000000000000l, 0);
225.361 - }
225.362 -
225.363 - @Compare public int comparePositiveNumbers() {
225.364 - return compare(0x0000000000200000l, 0x0000000010000000l, 0);
225.365 - }
225.366 -
225.367 - @Compare public int compareNegativeNumbers() {
225.368 - return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
225.369 - }
225.370 -
225.371 - @Compare public int compareMixedNumbers() {
225.372 - return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
225.373 - }
225.374 -
225.375 - @Factory
225.376 - public static Object[] create() {
225.377 - return VMTest.create(LongArithmeticTest.class);
225.378 - }
225.379 -}
226.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionArrayTest.java Tue Mar 05 07:57:16 2013 +0100
226.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
226.3 @@ -1,135 +0,0 @@
226.4 -/**
226.5 - * Back 2 Browser Bytecode Translator
226.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
226.7 - *
226.8 - * This program is free software: you can redistribute it and/or modify
226.9 - * it under the terms of the GNU General Public License as published by
226.10 - * the Free Software Foundation, version 2 of the License.
226.11 - *
226.12 - * This program is distributed in the hope that it will be useful,
226.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
226.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
226.15 - * GNU General Public License for more details.
226.16 - *
226.17 - * You should have received a copy of the GNU General Public License
226.18 - * along with this program. Look for COPYING file in the top folder.
226.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
226.20 - */
226.21 -package org.apidesign.bck2brwsr.tck;
226.22 -
226.23 -import java.lang.reflect.Array;
226.24 -import org.apidesign.bck2brwsr.vmtest.Compare;
226.25 -import org.apidesign.bck2brwsr.vmtest.VMTest;
226.26 -import org.testng.annotations.Factory;
226.27 -
226.28 -/**
226.29 - *
226.30 - * @author Jaroslav Tulach <jtulach@netbeans.org>
226.31 - */
226.32 -public class ReflectionArrayTest {
226.33 - @Compare public int lengthOfStringArray() {
226.34 - String[] arr = (String[]) Array.newInstance(String.class, 10);
226.35 - return arr.length;
226.36 - }
226.37 -
226.38 - @Compare public int reflectiveLengthOfStringArray() {
226.39 - Object arr = Array.newInstance(String.class, 10);
226.40 - return Array.getLength(arr);
226.41 - }
226.42 -
226.43 - @Compare public int reflectiveLengthOneNonArray() {
226.44 - Object arr = "non-array";
226.45 - return Array.getLength(arr);
226.46 - }
226.47 -
226.48 - @Compare public String compTypeOfStringArray() {
226.49 - String[] arr = (String[]) Array.newInstance(String.class, 10);
226.50 - return arr.getClass().getComponentType().getName();
226.51 - }
226.52 -
226.53 - @Compare public Object negativeArrayExcp() {
226.54 - return Array.newInstance(String.class, -5);
226.55 - }
226.56 -
226.57 - @Compare public int lengthOfIntArray() {
226.58 - int[] arr = (int[]) Array.newInstance(Integer.TYPE, 10);
226.59 - return arr.length;
226.60 - }
226.61 -
226.62 - @Compare public int reflectiveLengthOfIntArray() {
226.63 - Object arr = Array.newInstance(Integer.TYPE, 10);
226.64 - return Array.getLength(arr);
226.65 - }
226.66 -
226.67 - @Compare public String compTypeOfIntArray() {
226.68 - int[] arr = (int[]) Array.newInstance(int.class, 10);
226.69 - return arr.getClass().getComponentType().getName();
226.70 - }
226.71 -
226.72 - @Compare public Object intNegativeArrayExcp() {
226.73 - return Array.newInstance(int.class, -5);
226.74 - }
226.75 -
226.76 - @Compare public Integer verifyAutobox() {
226.77 - int[] arr = (int[]) Array.newInstance(int.class, 5);
226.78 - return (Integer) Array.get(arr, 0);
226.79 - }
226.80 - @Compare public String verifyObjectArray() {
226.81 - String[] arr = (String[]) Array.newInstance(String.class, 5);
226.82 - Array.set(arr, 0, "Hello");
226.83 - return (String) Array.get(arr, 0);
226.84 - }
226.85 - @Compare public int verifyInt() {
226.86 - int[] arr = (int[]) Array.newInstance(int.class, 5);
226.87 - return Array.getInt(arr, 0);
226.88 - }
226.89 - @Compare public long verifyConvertToLong() {
226.90 - int[] arr = (int[]) Array.newInstance(int.class, 5);
226.91 - return Array.getLong(arr, 0);
226.92 - }
226.93 -
226.94 - @Compare public Object verifySetIntToObject() {
226.95 - try {
226.96 - Object[] arr = (Object[]) Array.newInstance(Object.class, 5);
226.97 - Array.setInt(arr, 0, 10);
226.98 - return Array.get(arr, 0);
226.99 - } catch (Exception exception) {
226.100 - return exception.getClass().getName();
226.101 - }
226.102 - }
226.103 - @Compare public long verifySetShort() {
226.104 - int[] arr = (int[]) Array.newInstance(int.class, 5);
226.105 - Array.setShort(arr, 0, (short)10);
226.106 - return Array.getLong(arr, 0);
226.107 - }
226.108 - @Compare public long verifyCantSetLong() {
226.109 - int[] arr = (int[]) Array.newInstance(int.class, 5);
226.110 - Array.setLong(arr, 0, 10);
226.111 - return Array.getLong(arr, 0);
226.112 - }
226.113 - @Compare public float verifyLongToFloat() {
226.114 - Object arr = Array.newInstance(float.class, 5);
226.115 - Array.setLong(arr, 0, 10);
226.116 - return Array.getFloat(arr, 0);
226.117 - }
226.118 -
226.119 - @Compare public double verifyConvertToDouble() {
226.120 - int[] arr = (int[]) Array.newInstance(int.class, 5);
226.121 - return Array.getDouble(arr, 0);
226.122 - }
226.123 -
226.124 - @Compare public int multiIntArray() {
226.125 - int[][][] arr = (int[][][]) Array.newInstance(int.class, 3, 3, 3);
226.126 - return arr[0][1][2] + 5 + arr[2][2][0];
226.127 - }
226.128 -
226.129 - @Compare public String multiIntArrayCompType() {
226.130 - return Array.newInstance(int.class, 3, 3, 3).getClass().getName();
226.131 - }
226.132 -
226.133 -
226.134 - @Factory
226.135 - public static Object[] create() {
226.136 - return VMTest.create(ReflectionArrayTest.class);
226.137 - }
226.138 -}
227.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java Tue Mar 05 07:57:16 2013 +0100
227.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
227.3 @@ -1,242 +0,0 @@
227.4 -/**
227.5 - * Back 2 Browser Bytecode Translator
227.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
227.7 - *
227.8 - * This program is free software: you can redistribute it and/or modify
227.9 - * it under the terms of the GNU General Public License as published by
227.10 - * the Free Software Foundation, version 2 of the License.
227.11 - *
227.12 - * This program is distributed in the hope that it will be useful,
227.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
227.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
227.15 - * GNU General Public License for more details.
227.16 - *
227.17 - * You should have received a copy of the GNU General Public License
227.18 - * along with this program. Look for COPYING file in the top folder.
227.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
227.20 - */
227.21 -package org.apidesign.bck2brwsr.tck;
227.22 -
227.23 -import java.lang.annotation.Retention;
227.24 -import java.lang.annotation.RetentionPolicy;
227.25 -import java.lang.reflect.Method;
227.26 -import java.util.Arrays;
227.27 -import java.util.Collections;
227.28 -import java.util.List;
227.29 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
227.30 -import org.apidesign.bck2brwsr.vmtest.Compare;
227.31 -import org.apidesign.bck2brwsr.vmtest.VMTest;
227.32 -import org.testng.annotations.Factory;
227.33 -
227.34 -/**
227.35 - *
227.36 - * @author Jaroslav Tulach <jtulach@netbeans.org>
227.37 - */
227.38 -public class ReflectionTest {
227.39 - @Compare public boolean nonNullThis() {
227.40 - return this == null;
227.41 - }
227.42 -
227.43 - @Compare public String intType() {
227.44 - return Integer.TYPE.toString();
227.45 - }
227.46 -
227.47 - @Compare public String voidType() throws Exception {
227.48 - return void.class.toString();
227.49 - }
227.50 -
227.51 - @Compare public String longClass() {
227.52 - return long.class.toString();
227.53 - }
227.54 -
227.55 - @Compare public boolean isRunnableInterface() {
227.56 - return Runnable.class.isInterface();
227.57 - }
227.58 -
227.59 - @Compare public String isRunnableHasRunMethod() throws NoSuchMethodException {
227.60 - return Runnable.class.getMethod("run").getName();
227.61 - }
227.62 -
227.63 - @Compare public String namesOfMethods() {
227.64 - StringBuilder sb = new StringBuilder();
227.65 - String[] arr = new String[20];
227.66 - int i = 0;
227.67 - for (Method m : StaticUse.class.getMethods()) {
227.68 - arr[i++] = m.getName();
227.69 - }
227.70 - for (String s : sort(arr, i)) {
227.71 - sb.append(s).append("\n");
227.72 - }
227.73 - return sb.toString();
227.74 - }
227.75 -
227.76 - @Compare public String namesOfDeclaringClassesOfMethods() {
227.77 - StringBuilder sb = new StringBuilder();
227.78 - String[] arr = new String[20];
227.79 - int i = 0;
227.80 - for (Method m : StaticUse.class.getMethods()) {
227.81 - arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
227.82 - }
227.83 - for (String s : sort(arr, i)) {
227.84 - sb.append(s).append("\n");
227.85 - }
227.86 - return sb.toString();
227.87 - }
227.88 -
227.89 - @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
227.90 - StaticUse.class.getMethod("instanceMethod").invoke(null);
227.91 - return "should not happen";
227.92 - }
227.93 -
227.94 - @Compare public Object voidReturnType() throws Exception {
227.95 - return StaticUse.class.getMethod("instanceMethod").getReturnType();
227.96 - }
227.97 -
227.98 - @Retention(RetentionPolicy.RUNTIME)
227.99 - @interface Ann {
227.100 - }
227.101 -
227.102 - @Compare public String annoClass() throws Exception {
227.103 - Retention r = Ann.class.getAnnotation(Retention.class);
227.104 - assert r != null : "Annotation is present";
227.105 - assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
227.106 - return r.annotationType().getName();
227.107 - }
227.108 -
227.109 - @Compare public boolean isAnnotation() {
227.110 - return Ann.class.isAnnotation();
227.111 - }
227.112 - @Compare public boolean isNotAnnotation() {
227.113 - return String.class.isAnnotation();
227.114 - }
227.115 - @Compare public boolean isNotAnnotationEnum() {
227.116 - return E.class.isAnnotation();
227.117 - }
227.118 - enum E { A, B };
227.119 - @Compare public boolean isEnum() {
227.120 - return E.A.getClass().isEnum();
227.121 - }
227.122 -
227.123 - @Compare public boolean isNotEnum() {
227.124 - return "".getClass().isEnum();
227.125 - }
227.126 -
227.127 - @Compare public String newInstanceFails() throws InstantiationException {
227.128 - try {
227.129 - return "success: " + StaticUseSub.class.newInstance();
227.130 - } catch (IllegalAccessException ex) {
227.131 - return ex.getClass().getName();
227.132 - }
227.133 - }
227.134 -
227.135 - @Compare public String paramTypes() throws Exception {
227.136 - Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
227.137 - final Class[] pt = plus.getParameterTypes();
227.138 - return pt[0].getName();
227.139 - }
227.140 - @Compare public String paramTypesNotFound() throws Exception {
227.141 - return StaticUse.class.getMethod("plus", int.class, double.class).toString();
227.142 - }
227.143 - @Compare public int methodWithArgs() throws Exception {
227.144 - Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
227.145 - return (Integer)plus.invoke(null, 2, 3);
227.146 - }
227.147 -
227.148 - @Compare public String classGetNameForByte() {
227.149 - return byte.class.getName();
227.150 - }
227.151 - @Compare public String classGetNameForBaseObject() {
227.152 - return newObject().getClass().getName();
227.153 - }
227.154 - @Compare public String classGetNameForJavaObject() {
227.155 - return new Object().getClass().getName();
227.156 - }
227.157 - @Compare public String classGetNameForObjectArray() {
227.158 - return (new Object[3]).getClass().getName();
227.159 - }
227.160 - @Compare public String classGetNameForSimpleIntArray() {
227.161 - return (new int[3]).getClass().getName();
227.162 - }
227.163 - @Compare public boolean sameClassGetNameForSimpleCharArray() {
227.164 - return (new char[3]).getClass() == (new char[34]).getClass();
227.165 - }
227.166 - @Compare public String classGetNameForMultiIntArray() {
227.167 - return (new int[3][4][5][6][7][8][9]).getClass().getName();
227.168 - }
227.169 - @Compare public String classGetNameForMultiIntArrayInner() {
227.170 - final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
227.171 - int[][][][][][] subarr = arr[0];
227.172 - int[][][][][] subsubarr = subarr[0];
227.173 - return subsubarr.getClass().getName();
227.174 - }
227.175 - @Compare public String classGetNameForMultiStringArray() {
227.176 - return (new String[3][4][5][6][7][8][9]).getClass().getName();
227.177 - }
227.178 -
227.179 - @Compare public String classForByte() throws Exception {
227.180 - return Class.forName("[Z").getName();
227.181 - }
227.182 -
227.183 - @Compare public String classForUnknownArray() {
227.184 - try {
227.185 - return Class.forName("[W").getName();
227.186 - } catch (Exception ex) {
227.187 - return ex.getClass().getName();
227.188 - }
227.189 - }
227.190 -
227.191 - @Compare public String classForUnknownDeepArray() {
227.192 - try {
227.193 - return Class.forName("[[[[[W").getName();
227.194 - } catch (Exception ex) {
227.195 - return ex.getClass().getName();
227.196 - }
227.197 - }
227.198 -
227.199 - @Compare public String componentGetNameForObjectArray() {
227.200 - return (new Object[3]).getClass().getComponentType().getName();
227.201 - }
227.202 - @Compare public boolean sameComponentGetNameForObjectArray() {
227.203 - return (new Object[3]).getClass().getComponentType() == Object.class;
227.204 - }
227.205 - @Compare public String componentGetNameForSimpleIntArray() {
227.206 - return (new int[3]).getClass().getComponentType().getName();
227.207 - }
227.208 - @Compare public String componentGetNameForMultiIntArray() {
227.209 - return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
227.210 - }
227.211 - @Compare public String componentGetNameForMultiStringArray() {
227.212 - Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
227.213 - StringBuilder sb = new StringBuilder();
227.214 - for (;;) {
227.215 - sb.append(c.getName()).append("\n");
227.216 - c = c.getComponentType();
227.217 - if (c == null) {
227.218 - break;
227.219 - }
227.220 - }
227.221 - return sb.toString();
227.222 - }
227.223 -
227.224 - @Compare public boolean isArray() {
227.225 - return new Object[0].getClass().isArray();
227.226 - }
227.227 -
227.228 - @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
227.229 - private static String[] sort(String[] arr, int len) {
227.230 - List<String> list = Arrays.asList(arr).subList(0, len);
227.231 - Collections.sort(list);
227.232 - return list.toArray(new String[0]);
227.233 - }
227.234 -
227.235 - @JavaScriptBody(args = {}, body = "return new Object();")
227.236 - private static Object newObject() {
227.237 - return new Object();
227.238 - }
227.239 -
227.240 - @Factory
227.241 - public static Object[] create() {
227.242 - return VMTest.create(ReflectionTest.class);
227.243 - }
227.244 -
227.245 -}
228.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ResourcesTest.java Tue Mar 05 07:57:16 2013 +0100
228.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
228.3 @@ -1,45 +0,0 @@
228.4 -/**
228.5 - * Back 2 Browser Bytecode Translator
228.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
228.7 - *
228.8 - * This program is free software: you can redistribute it and/or modify
228.9 - * it under the terms of the GNU General Public License as published by
228.10 - * the Free Software Foundation, version 2 of the License.
228.11 - *
228.12 - * This program is distributed in the hope that it will be useful,
228.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
228.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
228.15 - * GNU General Public License for more details.
228.16 - *
228.17 - * You should have received a copy of the GNU General Public License
228.18 - * along with this program. Look for COPYING file in the top folder.
228.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
228.20 - */
228.21 -package org.apidesign.bck2brwsr.tck;
228.22 -
228.23 -import java.io.InputStream;
228.24 -import org.apidesign.bck2brwsr.vmtest.Compare;
228.25 -import org.apidesign.bck2brwsr.vmtest.VMTest;
228.26 -import org.testng.annotations.Factory;
228.27 -
228.28 -/**
228.29 - *
228.30 - * @author Jaroslav Tulach <jtulach@netbeans.org>
228.31 - */
228.32 -public class ResourcesTest {
228.33 -
228.34 - @Compare public String readResourceAsStream() throws Exception {
228.35 - InputStream is = getClass().getResourceAsStream("Resources.txt");
228.36 - byte[] b = new byte[30];
228.37 - int len = is.read(b);
228.38 - StringBuilder sb = new StringBuilder();
228.39 - for (int i = 0; i < len; i++) {
228.40 - sb.append((char)b[i]);
228.41 - }
228.42 - return sb.toString();
228.43 - }
228.44 -
228.45 - @Factory public static Object[] create() {
228.46 - return VMTest.create(ResourcesTest.class);
228.47 - }
228.48 -}
229.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ShortArithmeticTest.java Tue Mar 05 07:57:16 2013 +0100
229.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
229.3 @@ -1,102 +0,0 @@
229.4 -/**
229.5 - * Back 2 Browser Bytecode Translator
229.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
229.7 - *
229.8 - * This program is free software: you can redistribute it and/or modify
229.9 - * it under the terms of the GNU General Public License as published by
229.10 - * the Free Software Foundation, version 2 of the License.
229.11 - *
229.12 - * This program is distributed in the hope that it will be useful,
229.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
229.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
229.15 - * GNU General Public License for more details.
229.16 - *
229.17 - * You should have received a copy of the GNU General Public License
229.18 - * along with this program. Look for COPYING file in the top folder.
229.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
229.20 - */
229.21 -package org.apidesign.bck2brwsr.tck;
229.22 -
229.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
229.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
229.25 -import org.testng.annotations.Factory;
229.26 -
229.27 -/**
229.28 - *
229.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
229.30 - */
229.31 -public class ShortArithmeticTest {
229.32 -
229.33 - private static short add(short x, short y) {
229.34 - return (short)(x + y);
229.35 - }
229.36 -
229.37 - private static short sub(short x, short y) {
229.38 - return (short)(x - y);
229.39 - }
229.40 -
229.41 - private static short mul(short x, short y) {
229.42 - return (short)(x * y);
229.43 - }
229.44 -
229.45 - private static short div(short x, short y) {
229.46 - return (short)(x / y);
229.47 - }
229.48 -
229.49 - private static short mod(short x, short y) {
229.50 - return (short)(x % y);
229.51 - }
229.52 -
229.53 - @Compare public short conversion() {
229.54 - return (short)123456;
229.55 - }
229.56 -
229.57 - @Compare public short addOverflow() {
229.58 - return add(Short.MAX_VALUE, (short)1);
229.59 - }
229.60 -
229.61 - @Compare public short subUnderflow() {
229.62 - return sub(Short.MIN_VALUE, (short)1);
229.63 - }
229.64 -
229.65 - @Compare public short addMaxShortAndMaxShort() {
229.66 - return add(Short.MAX_VALUE, Short.MAX_VALUE);
229.67 - }
229.68 -
229.69 - @Compare public short subMinShortAndMinShort() {
229.70 - return sub(Short.MIN_VALUE, Short.MIN_VALUE);
229.71 - }
229.72 -
229.73 - @Compare public short multiplyMaxShort() {
229.74 - return mul(Short.MAX_VALUE, (short)2);
229.75 - }
229.76 -
229.77 - @Compare public short multiplyMaxShortAndMaxShort() {
229.78 - return mul(Short.MAX_VALUE, Short.MAX_VALUE);
229.79 - }
229.80 -
229.81 - @Compare public short multiplyMinShort() {
229.82 - return mul(Short.MIN_VALUE, (short)2);
229.83 - }
229.84 -
229.85 - @Compare public short multiplyMinShortAndMinShort() {
229.86 - return mul(Short.MIN_VALUE, Short.MIN_VALUE);
229.87 - }
229.88 -
229.89 - @Compare public short multiplyPrecision() {
229.90 - return mul((short)17638, (short)1103);
229.91 - }
229.92 -
229.93 - @Compare public short division() {
229.94 - return div((short)1, (short)2);
229.95 - }
229.96 -
229.97 - @Compare public short divisionReminder() {
229.98 - return mod((short)1, (short)2);
229.99 - }
229.100 -
229.101 - @Factory
229.102 - public static Object[] create() {
229.103 - return VMTest.create(ShortArithmeticTest.class);
229.104 - }
229.105 -}
230.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/StaticUse.java Tue Mar 05 07:57:16 2013 +0100
230.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
230.3 @@ -1,31 +0,0 @@
230.4 -/**
230.5 - * Back 2 Browser Bytecode Translator
230.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
230.7 - *
230.8 - * This program is free software: you can redistribute it and/or modify
230.9 - * it under the terms of the GNU General Public License as published by
230.10 - * the Free Software Foundation, version 2 of the License.
230.11 - *
230.12 - * This program is distributed in the hope that it will be useful,
230.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
230.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
230.15 - * GNU General Public License for more details.
230.16 - *
230.17 - * You should have received a copy of the GNU General Public License
230.18 - * along with this program. Look for COPYING file in the top folder.
230.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
230.20 - */
230.21 -package org.apidesign.bck2brwsr.tck;
230.22 -
230.23 -class StaticUse {
230.24 - public static final Object NON_NULL = new Object();
230.25 - StaticUse() {
230.26 - }
230.27 -
230.28 - public void instanceMethod() {
230.29 - }
230.30 -
230.31 - public static int plus(int a, int b) {
230.32 - return a + b;
230.33 - }
230.34 -}
231.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/StaticUseSub.java Tue Mar 05 07:57:16 2013 +0100
231.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
231.3 @@ -1,27 +0,0 @@
231.4 -/**
231.5 - * Back 2 Browser Bytecode Translator
231.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
231.7 - *
231.8 - * This program is free software: you can redistribute it and/or modify
231.9 - * it under the terms of the GNU General Public License as published by
231.10 - * the Free Software Foundation, version 2 of the License.
231.11 - *
231.12 - * This program is distributed in the hope that it will be useful,
231.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
231.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
231.15 - * GNU General Public License for more details.
231.16 - *
231.17 - * You should have received a copy of the GNU General Public License
231.18 - * along with this program. Look for COPYING file in the top folder.
231.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
231.20 - */
231.21 -package org.apidesign.bck2brwsr.tck;
231.22 -
231.23 -public class StaticUseSub extends StaticUse {
231.24 - private StaticUseSub() {
231.25 - }
231.26 -
231.27 - public static Object getNonNull() {
231.28 - return NON_NULL;
231.29 - }
231.30 -}
232.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/StaticUseSubTest.java Tue Mar 05 07:57:16 2013 +0100
232.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
232.3 @@ -1,37 +0,0 @@
232.4 -/**
232.5 - * Back 2 Browser Bytecode Translator
232.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
232.7 - *
232.8 - * This program is free software: you can redistribute it and/or modify
232.9 - * it under the terms of the GNU General Public License as published by
232.10 - * the Free Software Foundation, version 2 of the License.
232.11 - *
232.12 - * This program is distributed in the hope that it will be useful,
232.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
232.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
232.15 - * GNU General Public License for more details.
232.16 - *
232.17 - * You should have received a copy of the GNU General Public License
232.18 - * along with this program. Look for COPYING file in the top folder.
232.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
232.20 - */
232.21 -package org.apidesign.bck2brwsr.tck;
232.22 -
232.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
232.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
232.25 -import org.testng.annotations.Factory;
232.26 -
232.27 -/**
232.28 - *
232.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
232.30 - */
232.31 -public class StaticUseSubTest {
232.32 - @Compare public String staticFieldInitializationInSuperClass() throws Exception {
232.33 - Object ret = StaticUseSub.getNonNull();
232.34 - return ret.getClass().getName();
232.35 - }
232.36 -
232.37 - @Factory public static Object[] create() {
232.38 - return VMTest.create(StaticUseSubTest.class);
232.39 - }
232.40 -}
233.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/CRC32Test.java Tue Mar 05 07:57:16 2013 +0100
233.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
233.3 @@ -1,41 +0,0 @@
233.4 -/**
233.5 - * Back 2 Browser Bytecode Translator
233.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
233.7 - *
233.8 - * This program is free software: you can redistribute it and/or modify
233.9 - * it under the terms of the GNU General Public License as published by
233.10 - * the Free Software Foundation, version 2 of the License.
233.11 - *
233.12 - * This program is distributed in the hope that it will be useful,
233.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
233.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
233.15 - * GNU General Public License for more details.
233.16 - *
233.17 - * You should have received a copy of the GNU General Public License
233.18 - * along with this program. Look for COPYING file in the top folder.
233.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
233.20 - */
233.21 -package org.apidesign.bck2brwsr.vmtest.impl;
233.22 -
233.23 -import java.io.UnsupportedEncodingException;
233.24 -import java.util.zip.CRC32;
233.25 -import org.apidesign.bck2brwsr.vmtest.Compare;
233.26 -import org.apidesign.bck2brwsr.vmtest.VMTest;
233.27 -import org.testng.annotations.Factory;
233.28 -
233.29 -/**
233.30 - *
233.31 - * @author Jaroslav Tulach <jtulach@netbeans.org>
233.32 - */
233.33 -public class CRC32Test {
233.34 -
233.35 - @Compare public long crc1() throws UnsupportedEncodingException {
233.36 - CRC32 crc = new CRC32();
233.37 - crc.update("Hello World!".getBytes("UTF-8"));
233.38 - return crc.getValue();
233.39 - }
233.40 -
233.41 - @Factory public static Object[] create() {
233.42 - return VMTest.create(CRC32Test.class);
233.43 - }
233.44 -}
234.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipEntryTest.java Tue Mar 05 07:57:16 2013 +0100
234.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
234.3 @@ -1,67 +0,0 @@
234.4 -/**
234.5 - * Back 2 Browser Bytecode Translator
234.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
234.7 - *
234.8 - * This program is free software: you can redistribute it and/or modify
234.9 - * it under the terms of the GNU General Public License as published by
234.10 - * the Free Software Foundation, version 2 of the License.
234.11 - *
234.12 - * This program is distributed in the hope that it will be useful,
234.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
234.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
234.15 - * GNU General Public License for more details.
234.16 - *
234.17 - * You should have received a copy of the GNU General Public License
234.18 - * along with this program. Look for COPYING file in the top folder.
234.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
234.20 - */
234.21 -package org.apidesign.bck2brwsr.vmtest.impl;
234.22 -
234.23 -import java.io.ByteArrayInputStream;
234.24 -import java.io.IOException;
234.25 -import java.io.InputStream;
234.26 -import org.apidesign.bck2brwsr.emul.zip.FastJar;
234.27 -import org.testng.annotations.Test;
234.28 -import static org.testng.Assert.*;
234.29 -
234.30 -/**
234.31 - *
234.32 - * @author Jaroslav Tulach <jtulach@netbeans.org>
234.33 - */
234.34 -@GenerateZip(name = "five.zip", contents = {
234.35 - "1.txt", "one",
234.36 - "2.txt", "duo",
234.37 - "3.txt", "three",
234.38 - "4.txt", "four",
234.39 - "5.txt", "five"
234.40 -})
234.41 -public class ZipEntryTest {
234.42 - @Test
234.43 - public void readEntriesEffectively() throws IOException {
234.44 - InputStream is = ZipEntryTest.class.getResourceAsStream("five.zip");
234.45 - byte[] arr = new byte[is.available()];
234.46 - int len = is.read(arr);
234.47 - assertEquals(len, arr.length, "Read fully");
234.48 -
234.49 - FastJar fj = new FastJar(arr);
234.50 - FastJar.Entry[] entrs = fj.list();
234.51 -
234.52 - assertEquals(5, entrs.length, "Five entries");
234.53 -
234.54 - for (int i = 1; i <= 5; i++) {
234.55 - FastJar.Entry en = entrs[i - 1];
234.56 - assertEquals(en.name, i + ".txt");
234.57 -// assertEquals(cis.cnt, 0, "Content of the file should be skipped, not read");
234.58 - }
234.59 -
234.60 - assertContent("three", fj.getInputStream(entrs[3 - 1]), "read OK");
234.61 - assertContent("five", fj.getInputStream(entrs[5 - 1]), "read OK");
234.62 - }
234.63 -
234.64 - private static void assertContent(String exp, InputStream is, String msg) throws IOException {
234.65 - byte[] arr = new byte[512];
234.66 - int len = is.read(arr);
234.67 - String s = new String(arr, 0, len);
234.68 - assertEquals(exp, s, msg);
234.69 - }
234.70 -}
235.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java Tue Mar 05 07:57:16 2013 +0100
235.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
235.3 @@ -1,108 +0,0 @@
235.4 -/**
235.5 - * Back 2 Browser Bytecode Translator
235.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
235.7 - *
235.8 - * This program is free software: you can redistribute it and/or modify
235.9 - * it under the terms of the GNU General Public License as published by
235.10 - * the Free Software Foundation, version 2 of the License.
235.11 - *
235.12 - * This program is distributed in the hope that it will be useful,
235.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
235.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
235.15 - * GNU General Public License for more details.
235.16 - *
235.17 - * You should have received a copy of the GNU General Public License
235.18 - * along with this program. Look for COPYING file in the top folder.
235.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
235.20 - */
235.21 -package org.apidesign.bck2brwsr.vmtest.impl;
235.22 -
235.23 -import java.io.IOException;
235.24 -import java.io.InputStream;
235.25 -import java.util.Objects;
235.26 -import java.util.zip.ZipEntry;
235.27 -import java.util.zip.ZipInputStream;
235.28 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
235.29 -import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
235.30 -import org.apidesign.bck2brwsr.vmtest.Compare;
235.31 -import org.apidesign.bck2brwsr.vmtest.Http;
235.32 -import org.apidesign.bck2brwsr.vmtest.VMTest;
235.33 -import org.testng.annotations.Factory;
235.34 -
235.35 -/**
235.36 - *
235.37 - * @author Jaroslav Tulach <jtulach@netbeans.org>
235.38 - */
235.39 -@GenerateZip(name = "readAnEntry.zip", contents = {
235.40 - "my/main/file.txt", "Hello World!"
235.41 -})
235.42 -public class ZipFileTest {
235.43 -
235.44 - @Compare public String readAnEntry() throws IOException {
235.45 - InputStream is = ZipFileTest.class.getResourceAsStream("readAnEntry.zip");
235.46 - ZipInputStream zip = new ZipInputStream(is);
235.47 - ZipEntry entry = zip.getNextEntry();
235.48 - assertEquals(entry.getName(), "my/main/file.txt", "Correct entry");
235.49 -
235.50 - byte[] arr = new byte[4096];
235.51 - int len = zip.read(arr);
235.52 -
235.53 - assertEquals(zip.getNextEntry(), null, "No next entry");
235.54 -
235.55 - final String ret = new String(arr, 0, len, "UTF-8");
235.56 - return ret;
235.57 - }
235.58 -
235.59 - @JavaScriptBody(args = { "res", "path" }, body =
235.60 - "var myvm = bck2brwsr.apply(null, path);\n"
235.61 - + "var cls = myvm.loadClass('java.lang.String');\n"
235.62 - + "return cls.getClass__Ljava_lang_Class_2().getResourceAsStream__Ljava_io_InputStream_2Ljava_lang_String_2(res);\n"
235.63 - )
235.64 - private static native Object loadVMResource(String res, String...path);
235.65 -
235.66 - @Http({
235.67 - @Http.Resource(path = "/readAnEntry.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip")
235.68 - })
235.69 - @BrwsrTest public void canVmLoadResourceFromZip() throws IOException {
235.70 - Object res = loadVMResource("/my/main/file.txt", "/readAnEntry.jar");
235.71 - assert res instanceof InputStream : "Got array of bytes: " + res;
235.72 - InputStream is = (InputStream)res;
235.73 -
235.74 - byte[] arr = new byte[4096];
235.75 - int len = is.read(arr);
235.76 -
235.77 - final String ret = new String(arr, 0, len, "UTF-8");
235.78 -
235.79 - assertEquals(ret, "Hello World!", "Can read the bytes");
235.80 - }
235.81 -
235.82 - @GenerateZip(name = "cpattr.zip", contents = {
235.83 - "META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n"
235.84 - + "Created-By: hand\n"
235.85 - + "Class-Path: realJar.jar\n\n\n"
235.86 - })
235.87 - @Http({
235.88 - @Http.Resource(path = "/readComplexEntry.jar", mimeType = "x-application/zip", content = "", resource="cpattr.zip"),
235.89 - @Http.Resource(path = "/realJar.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip"),
235.90 - })
235.91 - @BrwsrTest public void understandsClassPathAttr() throws IOException {
235.92 - Object res = loadVMResource("/my/main/file.txt", "/readComplexEntry.jar");
235.93 - assert res instanceof InputStream : "Got array of bytes: " + res;
235.94 - InputStream is = (InputStream)res;
235.95 -
235.96 - byte[] arr = new byte[4096];
235.97 - int len = is.read(arr);
235.98 -
235.99 - final String ret = new String(arr, 0, len, "UTF-8");
235.100 -
235.101 - assertEquals(ret, "Hello World!", "Can read the bytes from secondary JAR");
235.102 - }
235.103 -
235.104 - private static void assertEquals(Object real, Object exp, String msg) {
235.105 - assert Objects.equals(exp, real) : msg + " exp: " + exp + " real: " + real;
235.106 - }
235.107 -
235.108 - @Factory public static Object[] create() {
235.109 - return VMTest.create(ZipFileTest.class);
235.110 - }
235.111 -}
236.1 --- a/rt/vmtest/src/test/resources/org/apidesign/bck2brwsr/tck/0xfe Tue Mar 05 07:57:16 2013 +0100
236.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
236.3 @@ -1,1 +0,0 @@
236.4 -þ
236.5 \ No newline at end of file
237.1 --- a/rt/vmtest/src/test/resources/org/apidesign/bck2brwsr/tck/Resources.txt Tue Mar 05 07:57:16 2013 +0100
237.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
237.3 @@ -1,1 +0,0 @@
237.4 -Ahoj