From 0aff72f97a6780aedaab38659dc7950f4307ab33 Mon Sep 17 00:00:00 2001 From: Manuel Carrasco Moñino Date: Mon, 9 Dec 2013 00:29:01 +0100 Subject: JsniBundleGenerator: support for abstract classes --- .../gwt/query/rebind/JsniBundleGenerator.java | 27 ++++++++++++---------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java index 342909de..4c4e56b2 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java @@ -42,31 +42,35 @@ import com.google.gwt.user.rebind.SourceWriter; /** * Generates an implementation of a user-defined interface T that * extends {@link JsniBundle}. - * + * * The generated implementation includes hand-written external js-files into * jsni methods so as those files can take advantage of gwt compiler optimizations. - * + * */ public class JsniBundleGenerator extends Generator { public String generate(TreeLogger logger, GeneratorContext context, String requestedClass) throws UnableToCompleteException { - + TypeOracle oracle = context.getTypeOracle(); JClassType clazz = oracle.findType(requestedClass); String packageName = clazz.getPackage().getName(); String className = clazz.getName().replace('.', '_') + "_Impl"; String fullName = packageName + "." + className; - + PrintWriter pw = context.tryCreate(logger, packageName, className); - - if (pw != null) { + if (pw != null) { ClassSourceFileComposerFactory fact = new ClassSourceFileComposerFactory(packageName, className); - fact.addImplementedInterface(requestedClass); + if (clazz.isInterface() != null) { + fact.addImplementedInterface(requestedClass); + } else { + fact.setSuperclass(requestedClass); + } + SourceWriter sw = fact.createSourceWriter(context, pw); - + if (sw != null) { for (JMethod method : clazz.getMethods()) { LibrarySource librarySource = method.getAnnotation(LibrarySource.class); @@ -92,7 +96,7 @@ public class JsniBundleGenerator extends Generator { // Adjust javascript so as we can introduce it in a JSNI comment block without // breaking java syntax. String jsni = parseJavascriptSource(content); - + pw.println(method.toString().replace("abstract", "native") + "/*-{"); pw.println(prepend); pw.println(jsni); @@ -103,9 +107,8 @@ public class JsniBundleGenerator extends Generator { throw new UnableToCompleteException(); } } - - sw.commit(logger); } + sw.commit(logger); } return fullName; @@ -166,7 +169,7 @@ public class JsniBundleGenerator extends Generator { * * The objective is to replace any 'c' comment-ending occurrence to avoid closing * JSNI comment blocks prematurely. - * + * * A Regexp based parser is not reliable, this approach is better and faster. */ // Note: this comment is intentionally using c++ style to allow writing the '*/' sequence. -- cgit v1.2.3 From 954aad695f838e3f1ecc0b9003df20633dd0466c Mon Sep 17 00:00:00 2001 From: Manuel Carrasco Moñino Date: Mon, 9 Dec 2013 02:59:21 +0100 Subject: Dont fail if method is not anotated --- .../src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java index 4c4e56b2..a1520304 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java @@ -86,7 +86,7 @@ public class JsniBundleGenerator extends Generator { prepend = methodSource.prepend(); postpend = methodSource.postpend(); } else { - return null; + continue; } } try { -- cgit v1.2.3 From 95c5b94fedde8ed6a563645df989c8293288a80f Mon Sep 17 00:00:00 2001 From: Manuel Carrasco Moñino Date: Mon, 9 Dec 2013 04:16:24 +0100 Subject: Allow custom replacers in jsni-bundle anotations --- .../gwt/query/client/builders/JsniBundle.java | 36 +++++++++++++++------- .../gwt/query/rebind/JsniBundleGenerator.java | 7 +++++ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsniBundle.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsniBundle.java index 14b3f1f2..a0a77e2e 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsniBundle.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsniBundle.java @@ -15,7 +15,7 @@ */ package com.google.gwt.query.client.builders; -import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.ElementType.METHOD; import java.lang.annotation.Documented; import java.lang.annotation.Retention; @@ -23,11 +23,11 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * A tag interface that is used in the generation of jsni bundles. - * + * A tag interface that is used in the generation of jsni bundles. + * * A jsni-bundle is a class with jsni methods whose content is taken from * external handwritten javascript files. - * + * * The goals of using this technique are: * - Use pure javascript files so as we can use IDEs for editing, formating etc, * instead of dealing with code in comment blocks. @@ -65,13 +65,13 @@ import java.lang.annotation.Target; * */ public interface JsniBundle { - + /** * Annotation to mark inclusion of third-party libraries. - * + * * The content is wrapped with an inner javascript function to set * the context to the document window instead of the iframe where GWT - * code is run. + * code is run. */ @Target({METHOD}) @Retention(RetentionPolicy.RUNTIME) @@ -92,12 +92,19 @@ public interface JsniBundle { * Fragment of code to include after the external javascript has been * written. */ - String postpend() default "\n}.apply($wnd, [$wnd, $doc, $wnd.console]));"; + String postpend() default "\n}.apply($wnd, [$wnd, $doc, $wnd.console]));"; + + /** + * Regular expression to run over the javascript code to import. + * + * You should add pairs of values with the same syntax than String.replaceAll. + */ + String[] replace() default {}; } - + /** * Annotation to mark inclusion of jsni code writen in external js files. - * + * * The content is not wrapped by default, so the developer has the responsibility * to return the suitable value and to handle correctly parameters. */ @@ -120,6 +127,13 @@ public interface JsniBundle { * Fragment of code to include after the external javascript has been * written. */ - String postpend() default ""; + String postpend() default ""; + + /** + * Regular expression to run over the javascript code to import. + * + * You should add pairs of values with the same syntax than String.replaceAll. + */ + String[] replace() default {}; } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java index a1520304..66ed45a4 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsniBundleGenerator.java @@ -75,16 +75,19 @@ public class JsniBundleGenerator extends Generator { for (JMethod method : clazz.getMethods()) { LibrarySource librarySource = method.getAnnotation(LibrarySource.class); String value, prepend, postpend; + String replace[]; if (librarySource != null) { value = librarySource.value(); prepend = librarySource.prepend(); postpend = librarySource.postpend(); + replace = librarySource.replace(); } else { MethodSource methodSource = method.getAnnotation(MethodSource.class); if (methodSource != null) { value = methodSource.value(); prepend = methodSource.prepend(); postpend = methodSource.postpend(); + replace = methodSource.replace(); } else { continue; } @@ -97,6 +100,10 @@ public class JsniBundleGenerator extends Generator { // breaking java syntax. String jsni = parseJavascriptSource(content); + for (int i = 0; i < replace.length - 1; i += 2) { + jsni = jsni.replaceAll(replace[i], replace[i+1]); + } + pw.println(method.toString().replace("abstract", "native") + "/*-{"); pw.println(prepend); pw.println(jsni); -- cgit v1.2.3