Skip to content

Commit 7e60e2e

Browse files
author
sebastigurin
committed
1 parent 2f16430 commit 7e60e2e

File tree

5 files changed

+174
-13
lines changed

5 files changed

+174
-13
lines changed

incubator/net.sf.j2s.doc.user-guide/j2s-user-guide/app-java-emulation-api.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,16 @@ TODO
107107
<bridgehead renderas="sect2">ClazzLoader.ignore = function ()</bridgehead>
108108
<simpara>Makes the J2s compiler to ignore (not load) an array of classes. accepts an array of classnames to ignore. Used when we put set some classes as "Abandoned" in the project Java2Script properties page.</simpara>
109109

110+
<bridgehead renderas="sect2">ClazzLoader.jarClasspath</bridgehead>
111+
<simpara>Makes the J2s compiler to ignore (not load) an array of classes. accepts an array of classnames to ignore. Used when we put set some classes as "Abandoned" in the project Java2Script properties page.</simpara>
112+
113+
ClazzLoader.jarClasspath (base + "util/Collections.js", [
114+
"java.util.Collections",
115+
"java.util.Collections.CheckedCollection",
116+
"java.util.Collections.CheckedList",
117+
"java.util.Collections.CheckedListIterator",
118+
"java.util.Collections.CheckedMap",
119+
......
120+
]);
110121
</section>
111122
</appendix>

incubator/net.sf.j2s.doc.user-guide/j2s-user-guide/chap-about-this-document.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<chapter id="chap-about-this-document">
33

44
<title>About this document</title>
5+
6+
<para>This document is currently a <emphasis>WORK in PROGRESS</emphasis>. paragraphs and section contains the work <emphasis>TODO</emphasis> mean that the section is to be written or lacks revision.</para>
57

68
<para>Document format links (TODO)</para>
79

incubator/net.sf.j2s.doc.user-guide/j2s-user-guide/chap-j2s-plugin.xml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,20 @@ supported by HotSpot. Changes to static methods, or changes in class signatures
221221
</bridgehead>
222222

223223
<para>
224-
Java2Script also implements JavaScript's Hotspot technology. Such JavaScript Hotspot technology is not complicate, because JavaScript is
224+
Java2Script also implements JavaScript's Hotspot technology. It is not complicate, because JavaScript is
225225
already a very robust and convenient language to do so. All Java2Script implementation is to clean those classes' declaration inside JavaScript
226226
class inheritance system simulator. And then reload the *.js using Java2Script's classloader. In the implementation, classloader does not change
227227
class or object's prototype when a reloaded class is redefined, it's possible to keep all classes relationships without breaking those already
228228
instantiated instances.
229229
</para>
230230

231-
<para>
232-
To trigger Java2Script class simulator to Hotspot swapping, there is a thread trying to load an
231+
<para><indexterm>
232+
<primary>J2SIHS</primary>
233+
</indexterm>
234+
<indexterm>
235+
<primary>Java2Script Inner Hotspot Server</primary>
236+
</indexterm>
237+
To trigger Java2Script class simulator to Hotspot swapping, there is a thread at the plugin side, trying to load an
233238
updating JavaScript classes list from Java2Script compiler in Eclipse. Its work is simple, just
234239
trying to load http://127.0.0.1:1725/&lt;session>.js. And the server listening on default port
235240
1725 is started by Java2Script compiler, called "Java2Script Inner Hotspot Server (J2SIHS)".
@@ -241,11 +246,10 @@ supported by HotSpot. Changes to static methods, or changes in class signatures
241246
</para>
242247

243248

244-
<para>
245-
By using Hotspot, I think it is much more convenient for me to develop JavaScript RIA in Java codes than before, especially in developing SWT applications.
246-
</para>
247-
249+
<para>By using Hotspot, I think it is much more convenient for me to develop JavaScript RIA in Java codes than
250+
before, especially in developing SWT applications. </para>
248251
</section>
252+
249253
</section>
250254

251255

@@ -267,7 +271,10 @@ By using Hotspot, I think it is much more convenient for me to develop JavaScrip
267271

268272
<para>Fortunately, Java2Script provide with flexible mechanism of library definition. Also as we will see, Java2Script support for defining
269273
how the library must loaded</para>
270-
<para>TODO: talk about j2slib and how swt, junit etc are inside. TODO: talk about .package file and how a lib can be loaded.</para>
274+
<para>TODO: talk about j2slib and how swt, junit etc are inside. TODO: talk about .j2x files and package.js files that le definehow alibrary must be loaded.
275+
Also, try and if works to dev a library, put it in other folder that j2slib, with a pacakge.js and lib.j2z file, and show how oackage.js is evaluated, and how to use it for loading one, concatenating several files, etcx</para>
276+
277+
<para>TODO: p/d: future extension packer can be mention here for intelligent generattion of big .js file .</para>
271278

272279
</section>
273280

incubator/net.sf.j2s.doc.user-guide/j2s-user-guide/chap-java-to-js-translation.xml

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,6 @@ c$.main = Clazz.defineMethod (c$, "main", function (args) {
167167
<code>$c.main = ...</code>
168168
</para>
169169
</callout>
170-
<!-- <callout arearefs="j2snative.jsex1.Clazz.javabody">-->
171-
<!-- <para>This is the main method source body. It is usually very similar to original java code.</para>-->
172-
<!-- </callout>-->
173170
</calloutlist>
174171
</programlistingco>
175172

@@ -407,6 +404,7 @@ classes and give as a context from where to call java classes</para>
407404
<section id="chap-java-to-js-translation-sec-types">
408405
<title>Types</title>
409406

407+
410408
<para>Explain how j2s emulates java types.
411409
ej: string and characters are all javascript strings,
412410
long, int short, double, float are all javascript numbers. arrays are javascript arrays</para>
@@ -422,13 +420,78 @@ classes and give as a context from where to call java classes</para>
422420

423421
<section id="chap-java-to-js-translation-sec-exceptions">
424422
<title>Exceptions</title>
423+
424+
<indexterm> <primary>native exceptions</primary></indexterm>
425+
<indexterm> <primary>Exception</primary></indexterm>
426+
<indexterm> <primary>Throwable</primary></indexterm>
427+
<para>Exceptions are well supported by Java2Script and so exception related statements have a very simple translation. There is only
428+
one special thing we have take care about when programming Java2Script code and is that in Java2Script, besides Java exceptions,
429+
there are also JavaScript native exceptions. For example, as we have seen, using @j2sNative compiler directive we can introduce
430+
native javascript code. And native javascript code
431+
can throw native javascript exceptions, that may have not be related at all with java exceptions (java.lang.Exception class). For example,
432+
the following example will show native javascript code that throws a native DOM exception. </para>
433+
434+
<programlisting>
435+
/**
436+
* native DOM exception siumulation
437+
* @j2sNative
438+
* document.body.appendChild(null);
439+
*/{}
440+
</programlisting>
441+
442+
<para>It is important to understand that these native exceptions, are not java.lang.Exception instances and so,
443+
they cannot be catched with <code>catch(Exception e)</code> expression. So, how we catch native exceptions and how can we diferentiate
444+
them from common java exceptions? The answer is that native exceptions are instance of class java.lang.Throwable (a superclass of
445+
java.lang.Exception) and so, we can catch them using Throwable, as the following example shows: </para>
446+
447+
<programlisting>
448+
try {
449+
/**
450+
* native DOM exception siumulation
451+
* @j2sNative
452+
* document.body.appendChild(null);
453+
*/{}
454+
} catch (Exception e) {
455+
//catching java exceptions
456+
System.out.println("this is NOT printed!!!");
457+
}
458+
catch(Throwable e) {
459+
//catching native exceptions!
460+
System.out.println("this is printed!!!");
461+
}
462+
</programlisting>
463+
464+
<para>The only special case of native exceptions are the null pointer exceptions.
465+
Since both in java and javascript null is the same
466+
object, and in java we spect NullPointerException to be throwed when accessing the null object,
467+
native javascript null pointer exceptions will be objects of the class java.lag.NullPointerException. Let understand this
468+
with a litlle example. In the following listing, we emulate a native null pointer exception and show that is catched with a NullPointerException and not as another native exception with Throwable: </para>
469+
470+
<programlisting>
471+
try {
472+
/**
473+
* This simulates a native null pointer exception
474+
* @j2sNative
475+
* var a = null;
476+
* a.sdf();
477+
*/{}
478+
} catch (NullPointerException e) {
479+
System.out.println("printed!");
480+
}
481+
catch (Throwable e) {
482+
System.out.println("NOT printed!");
483+
}
484+
</programlisting>
425485

426-
<para>Explain exceptions and how to separate native javascript exceptions from java exceptions</para>
427486
</section>
428487

429488

430489

431490

432-
491+
<section><title>Concatenating several .js files into one big .js file </title>
492+
<para>TODO: explain the problen of j2s clazzloader that loads several small files. it is fast to load one big file.</para>
493+
<para>TODO: explain how to concatenate (if a extends b, etc).</para>
494+
<para></para>
495+
</section>
433496

434497
</chapter>

incubator/net.sf.j2s.doc.user-guide/j2s-user-guide/chap-working-with-native-code.xml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
for customizing compiler output code, and specially the directive @j2sNative for including
3131
native javascript in our java classes.</para>
3232

33+
<para>Nevertheless, there are some areas that need to be understand if you want to write
34+
native javascript code and need to integrate that native code with your java code.
35+
36+
37+
</para>
38+
39+
<!-- stasks that are not so tribial to do. Take for example a very common problem: register a mouse click listener -->
3340
<para>this chapter will describe how, call java methods from javascript code, problems with java object+method vs - javascript objectsand javascript functions, care native objects like dom, etc, </para>
3441
<para>TODO</para>
3542

@@ -81,6 +88,77 @@
8188
</section>
8289

8390

91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
<section><title>JavaScript functions</title>
110+
<para>TODO: this sections contains the escense but has a bad english. it should be rewritten more elegantly.</para>
111+
<!-- summary:
112+
explain the problems of javascript functions.
113+
1) emulated in java with runnable, but a runnable is not a function!!
114+
2) problem with function context in java -->
115+
116+
117+
<!-- the language problem -->
118+
<para>The first problem that we face when working with native javascript is that , in javascript, we work with
119+
functions and the java language doesn't support such a concept or nothing similar. In java, the only beahaviour language concepts we have are
120+
instance methods and static methods. Mixing native javascript functions and java code without realizing what we really are doing,
121+
can cause very nasty bugs and problems, and so in this section we will examine possible problems and advices about how to do it right.
122+
</para>
123+
124+
<!-- the function context problem-->
125+
126+
<para>function context:</para>
127+
<para>what is a function context? In javascript, any function can be evaluated using any object as its context. The context of a
128+
function when evaluated is the object referenced by the "this" keyword.</para>
129+
130+
<important><para>Javascript function can be called with an arbitrary context object while Java instance
131+
methods must always have a fixed context. </para></important>
132+
133+
<para>since in java we always have to use an object, the function in java have a fixed context (the this object).
134+
But in javascript this is not true and we must ensure the context. for example, the following example for registering a
135+
mouse click listener in document.body will work, but it is incorrect! :</para>
136+
137+
<programlisting>
138+
Runnable r = new Runnable(){
139+
public void run() {
140+
System.out.println("clicked");
141+
}
142+
};
143+
/**
144+
* @j2sNative
145+
* document.body.onclick=r.run; //WRONG!
146+
*/{}
147+
</programlisting>
148+
149+
<para>Why this is wrong? When an html document is clicked, and a function is registered for listen to clicks with "element.onclick",
150+
the function will be evaluated using the clicked html element as the function context. This means that any reference to "this" in the function body, will
151+
point to the target html element and not where it should be, to the Runnable instance. Let examine an example that fails because of this:</para>
152+
153+
<programlisting></programlisting>
154+
155+
<para>With java static methods there is no problem because they do no context (no this pointer). But it is not useful to work with
156+
static methods for </para>
157+
158+
159+
</section>
160+
161+
84162
<!-- <section><title>j2s-base project for doing javascript in java</title></section>-->
85163

86164
</chapter>

0 commit comments

Comments
 (0)