@@ -476,13 +476,13 @@ each method invocation, and then the normal method invocation javascript code. <
476476
477477 <formalpara ><title >ASTScriptVisitor</title > <para >
478478 In one side we have an ASTScriptVisitor that is responsible for converting basic java elements to the corresponding javascript code. J2S provides with the default
479- implementation net.sf.j2s.core.astvisitors.ASTScriptVisitor that can be extended for building customized AST script visitors.
479+ implementation net.sf.j2s.core.astvisitors.SWTScriptVisitor that can be extended for building customized AST script visitors.
480480 </para ></formalpara >
481481
482482 <formalpara ><title >DependencyASTVisitor</title > <para >
483483 The other type if visitor is DependencyASTVisitor that is responsible for creating a class dependency trees,
484- in other words an import list for each java file. J2S provides with a default implemenation net.sf.j2s.core.astvisitors.DependencyASTVisitor . It is
485- recommended that the user extends it for defining its own AST dependency visitors. </para ></formalpara >
484+ in other words an import list for each java file. J2S provides with a default implemenation net.sf.j2s.core.astvisitors.SWTDependencyVisitor . It is
485+ recommended that the user extends this class for defining its own AST dependency visitors. </para ></formalpara >
486486
487487 <para >go to the "extensions" tab and add the extension point <code >net.sf.j2s.core.extendedASTScriptVisitor</code >:</para >
488488 <figure >
@@ -532,6 +532,7 @@ package j2s.extension1;
532532
533533import net.sf.j2s.core.astvisitors.ASTScriptVisitor;
534534import net.sf.j2s.core.astvisitors.DependencyASTVisitor;
535+ import net.sf.j2s.core.astvisitors.SWTDependencyASTVisitor;
535536import net.sf.j2s.core.compiler.IExtendedVisitor;
536537
537538public class MethodInvocationComment1 implements IExtendedVisitor {
@@ -571,9 +572,9 @@ package j2s.extension1;
571572
572573import org.eclipse.jdt.core.dom.MethodInvocation;
573574
574- import net.sf.j2s.core.astvisitors.ASTScriptVisitor ;
575+ import net.sf.j2s.core.astvisitors.SWTScriptVisitor ;
575576
576- public class MethodInvocationComment1ScriptVisitor extends ASTScriptVisitor {
577+ public class MethodInvocationComment1ScriptVisitor extends SWTScriptVisitor {
577578 @Override
578579 public boolean visit(MethodInvocation node) {
579580 buffer.append("/* A COMMENT ADDED WITH our J2S compiler extension plugin */\n");
@@ -639,39 +640,130 @@ java.lang.System.out.println ("hee");
639640</programlisting >
640641
641642<para >as you can see, out javascript custom comment was appended just before the method declaration at <code >java.lang.System.out.println ("hee");</code > </para >
643+
642644</section >
643645
644646
645- <programlistingco >
646- <areaspec >
647- <area id =" j2s-compiler-ext-getScriptVisitor2" coords =' 14' />
648- </areaspec >
649- <programlisting >
650647
651- </programlisting >
652- <calloutlist >
653- <callout arearefs =" j2s-compiler-ext-getScriptVisitor1" >
654- <para > </para >
655- </callout >
656- </calloutlist >
657- </programlistingco >
658648
659649
660-
661- <para >TODO</para >
662-
650+
651+
663652
664- </section >
665653
666654 <section ><title >Other example: html attributes with javadoc annotations</title >
667- <para >This second example uses J2S extension mechanism so the user can load html in class fields
668- from javadoc. We usually have to reference html string in our web applications code, and sometimes it is nasty
669- to do that using java or javascript strings. The idea of this plugin is to load a string to a class attribute from javadoc, like this: </para >
655+ <para >In this second example we show how to load string from javadoc comments into java fields.
656+ In web applications, we usually have to reference html, json, xml or other web format strings
657+ in our code and sometimes it is nasty to do it in common java or javascript strings literals. </para >
658+
659+ <para >The following is the java class code that implements ASTScriptVisitor that will detect and load a
660+ string in javadoc after the tag <code >@j2sLoadString</code > .</para >
661+
670662
671663 <programlisting >
664+ package j2s.extension1;
665+
666+ import java.util.Iterator;
667+ import java.util.List;
668+
669+ import net.sf.j2s.core.astvisitors.SWTScriptVisitor;
670+
671+ import org.eclipse.jdt.core.dom.FieldDeclaration;
672+ import org.eclipse.jdt.core.dom.Javadoc;
673+ import org.eclipse.jdt.core.dom.TagElement;
674+ import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
675+
676+ public class MethodInvocationComment1ScriptVisitor extends SWTScriptVisitor {
677+
678+ @Override
679+ public boolean visit(FieldDeclaration node) {
680+ boolean ret = super.visit(node); /* first visit the default visitor implementation */
681+
682+ Javadoc javadoc = node.getJavadoc();
683+ List tags = javadoc.tags();
684+ if(tags!=null&& tags.size()> 0) {
685+ for (Iterator it = tags.iterator(); it.hasNext();) {
686+ Object o = (Object) it.next();
687+ if(o instanceof TagElement) {
688+ TagElement tag = (TagElement) o;
689+ String tagName = tag.getTagName();
690+ if(tagName!=null&& tagName.equals("@j2sLoadString")) { /* annotation found. all the
691+ following javadoc comment until other tag is found will be loaded */
692+ StringBuffer sb = new StringBuffer();
693+ List frags = tag.fragments();
694+ for (Iterator it2 = frags.iterator(); it2.hasNext();) {
695+ sb.append(it2.next()+"\\n");
696+ }
697+ List fieldDeclFrags = node.fragments();
698+ /* now that we have the @j2sLoadString string, we add the javascript statement this.attributeName="str.." for loading the string field.*/
699+ if(fieldDeclFrags!=null&& fieldDeclFrags.size()> 0) {
700+ VariableDeclarationFragment f1 = (VariableDeclarationFragment) fieldDeclFrags.get(0);
701+ String str = sb.toString();
702+ buffer.append("this."+f1.getName()+"=\'");
703+ buffer.append(str);
704+ buffer.append("\';\n");
705+ }
706+ }
707+ }
708+ }
709+ }
710+ return ret;
711+ }
712+ }
713+ </programlisting >
672714
715+ <para >Using this compiler extension, you will be able to code complex strings like json, html, xml, etc inside javadoc.
716+ The following java test program define two fields, htmlCode1 and jsonCode1 which content is loaded from javadoc and not from string literals.
717+ As you can see there is much easier to put
718+ other language code inside javadoc instead using java string, in which you have to quote and concatenate:</para >
719+
720+ <programlisting >
721+ package org.foo;
722+
723+ public class Test1 {
724+
725+ public static void main(String[] args) {
726+ Test1 test1 = new Test1();
727+ System.out.println("loaded html code: "+test1.getHtmlCode1());
728+ System.out.println("loaded json code: "+test1.getJsonCode1());
729+ System.out.println("lot easier isn't it?");
730+ }
731+ /** this field will be loaded with the fololowing string:
732+ * @j2sLoadString
733+ * < h1> this is an < b> html< /b> string loaded from javadoc< /h1>
734+ */
735+ String htmlCode1;
736+
737+ /**
738+ * this other too
739+ * @j2s LoadString
740+ * {p1: [1, 2, 3], p2: {p21: "thi sis a json loaded from javadoc"}}
741+ */
742+ String jsonCode1;
743+
744+ public String getHtmlCode1() {
745+ return htmlCode1;
746+ }
747+
748+ public String getJsonCode1() {
749+ return jsonCode1;
750+ }
751+ }
673752 </programlisting >
674753
675- <para >TODO: finish this</para >
754+
755+ <para >Compiling this using our new compiler extension, the output will be the following when executing this as a Java2Script application:</para >
756+
757+
758+ <programlisting >
759+ loaded html code:
760+ < h1> this is an < b> html< /b> string loaded from javadoc< /h1>
761+
762+ loaded json code: {p1: [1, 2, 3], p2: {p21: "thi sis a json loaded from javadoc"}}
763+
764+ lot easier isn't it?
765+ </programlisting >
676766 </section >
767+ </section >
768+
677769</chapter >
0 commit comments