From 5c6562d93f7b18aab25fd105ccc428918b2264a4 Mon Sep 17 00:00:00 2001 From: Renaud Pawlak Date: Thu, 17 Aug 2017 09:52:11 +0200 Subject: [PATCH] allow @Replace for constructors (fix #339) --- core-lib/es5/pom.xml | 2 +- .../es5/src/main/java/jsweet/lang/Replace.java | 16 ++++++++-------- core-lib/es6/pom.xml | 2 +- .../es6/src/main/java/jsweet/lang/Replace.java | 6 +++--- transpiler/pom.xml | 2 +- .../jsweet/test/transpiler/StructuralTests.java | 1 + .../source/structural/ReplaceAnnotation.java | 11 +++++++++++ 7 files changed, 26 insertions(+), 14 deletions(-) diff --git a/core-lib/es5/pom.xml b/core-lib/es5/pom.xml index 24a8b132..b1088297 100644 --- a/core-lib/es5/pom.xml +++ b/core-lib/es5/pom.xml @@ -5,7 +5,7 @@ jsweet-core JSweet Core Lib JavaScript API for JSweet - 5-20170726 + 5-SNAPSHOT diff --git a/core-lib/es5/src/main/java/jsweet/lang/Replace.java b/core-lib/es5/src/main/java/jsweet/lang/Replace.java index cd4e4a96..bbdac0eb 100644 --- a/core-lib/es5/src/main/java/jsweet/lang/Replace.java +++ b/core-lib/es5/src/main/java/jsweet/lang/Replace.java @@ -23,8 +23,8 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * This annotation allows the programmer to substitute a method body - * implementation by a TypeScript implementation. + * This annotation allows the programmer to substitute a method or constructor + * body implementation by a TypeScript implementation. * *

* The annotation's value contains TypeScript which is generated as is by the @@ -50,14 +50,14 @@ import java.lang.annotation.Target; * @author Renaud Pawlak */ @Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) +@Target({ ElementType.METHOD, ElementType.CONSTRUCTOR }) @Documented public @interface Replace { - /** - * The code that will be generated by the transpiler in place of the - * annotated method body. - */ - java.lang.String value(); + /** + * The code that will be generated by the transpiler in place of the + * annotated method body. + */ + java.lang.String value(); } diff --git a/core-lib/es6/pom.xml b/core-lib/es6/pom.xml index 9c68daad..2a7301b1 100644 --- a/core-lib/es6/pom.xml +++ b/core-lib/es6/pom.xml @@ -5,7 +5,7 @@ jsweet-core JSweet Core Lib JavaScript API for JSweet - 6-20170726 + 6-SNAPSHOT 2.0.0 diff --git a/core-lib/es6/src/main/java/jsweet/lang/Replace.java b/core-lib/es6/src/main/java/jsweet/lang/Replace.java index abc8db14..97cff56f 100644 --- a/core-lib/es6/src/main/java/jsweet/lang/Replace.java +++ b/core-lib/es6/src/main/java/jsweet/lang/Replace.java @@ -23,8 +23,8 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * This annotation allows the programmer to substitute a method body - * implementation by a TypeScript implementation. + * This annotation allows the programmer to substitute a method or constructor + * body implementation by a TypeScript implementation. * *

* The annotation's value contains TypeScript which is generated as is by the @@ -50,7 +50,7 @@ import java.lang.annotation.Target; * @author Renaud Pawlak */ @Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) +@Target({ ElementType.METHOD, ElementType.CONSTRUCTOR }) @Documented public @interface Replace { diff --git a/transpiler/pom.xml b/transpiler/pom.xml index 17b0f621..26b2afe9 100644 --- a/transpiler/pom.xml +++ b/transpiler/pom.xml @@ -217,7 +217,7 @@ org.jsweet jsweet-core - 5-20170726 + 5-SNAPSHOT test true diff --git a/transpiler/src/test/java/org/jsweet/test/transpiler/StructuralTests.java b/transpiler/src/test/java/org/jsweet/test/transpiler/StructuralTests.java index ccf9ba9f..5207ca2a 100644 --- a/transpiler/src/test/java/org/jsweet/test/transpiler/StructuralTests.java +++ b/transpiler/src/test/java/org/jsweet/test/transpiler/StructuralTests.java @@ -362,6 +362,7 @@ public class StructuralTests extends AbstractTest { assertEquals(3, (int) r.get("test2")); assertEquals(1, (int) r.get("test3")); assertEquals(3, (int) r.get("test4")); + assertEquals(3, (int) r.get("test5")); }, getSourceFile(ReplaceAnnotation.class)); createTranspiler(new JSweetFactory()); } diff --git a/transpiler/src/test/java/source/structural/ReplaceAnnotation.java b/transpiler/src/test/java/source/structural/ReplaceAnnotation.java index 64ddf050..45cc1f36 100644 --- a/transpiler/src/test/java/source/structural/ReplaceAnnotation.java +++ b/transpiler/src/test/java/source/structural/ReplaceAnnotation.java @@ -11,10 +11,16 @@ public class ReplaceAnnotation { $export("test2", new ReplaceAnnotation().m2()); $export("test3", new ReplaceAnnotation().m3()); $export("test4", new ReplaceAnnotation().m4()); + $export("test5", new ReplaceAnnotation().m5()); } int i = 1; + int j = 2; + @Replace("this.j = 3;") + public ReplaceAnnotation() { + } + @Replace("return this.i + 1;") public int m1() { return i; @@ -32,4 +38,9 @@ public class ReplaceAnnotation { return i; } + public int m5() { + return j; + } + + }