From 1e53a38eb06aa8ff50660fdf7bd8570b27f27c56 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sun, 11 May 2014 03:03:36 +0200 Subject: Bug 923: Remove dependency of CStruct annotation processor _generation_ and generated_class_user due to Java8 issues. Java8's annotation processor as embedded within javac does not allow referencing not-yet existing generated classes in a class source code which will produce these generated classes via the annotation process. Example: +++ import com.jogamp.gluegen.structgen.CStruct; public class Lala { @CStruct(name="RenderingConfig", header="TestStruct01.h") public RenderingConfig config; } +++ Above example illustrates that the type 'RenderingConfig' does not exist at time of processing the annotation. The type will be created via the annotation process itself. Even though we pass '-proc:only', i.e. skip java compilation, Java8's annotation processing via javac fails in such case. We see this as a bug within javac's annotation processing itself! +++ This workaround splits the annotation process and using the class as generated by the former. To allow this to work, CStruct receives a new field 'jname' allowing to enforce the java-name of the structure using a dummy field type like boolean. @CStruct(name="RenderingConfig", jname="RenderingConfig", header="TestStruct01.h") public boolean dummy; Further more CStruct can be annotated on the package, i.e. 'package-info.java', avoiding the dependency problem altogether. To support multiple header files and types, 'CStructs' has been introduced using an array of 'CStruct'. @CStructs({@CStruct(name="RenderingConfig", header="TestStruct01.h"), @CStruct(name="Pixel", header="TestStruct02.h")}) package com.jogamp.gluegen.test.junit.structgen; Tests: - Build w/ Java7 and Java8 - Validated 'major version 50' (Java 6) class files (OK) --- .../test/junit/structgen/BuildStruct01.java | 34 +++++++++++++++ .../gluegen/test/junit/structgen/TestStruct02.h | 14 ++++++ .../test/junit/structgen/TestStructGen01.java | 23 +++++----- .../test/junit/structgen/TestStructGen02.java | 51 ++++++++++++++++++++++ .../gluegen/test/junit/structgen/package-info.java | 15 +++++++ 5 files changed, 124 insertions(+), 13 deletions(-) create mode 100644 src/junit/com/jogamp/gluegen/test/junit/structgen/BuildStruct01.java create mode 100644 src/junit/com/jogamp/gluegen/test/junit/structgen/TestStruct02.h create mode 100644 src/junit/com/jogamp/gluegen/test/junit/structgen/TestStructGen02.java create mode 100644 src/junit/com/jogamp/gluegen/test/junit/structgen/package-info.java (limited to 'src/junit') diff --git a/src/junit/com/jogamp/gluegen/test/junit/structgen/BuildStruct01.java b/src/junit/com/jogamp/gluegen/test/junit/structgen/BuildStruct01.java new file mode 100644 index 0000000..7824e55 --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/structgen/BuildStruct01.java @@ -0,0 +1,34 @@ +package com.jogamp.gluegen.test.junit.structgen; + +import com.jogamp.gluegen.structgen.CStruct; + +/** + * Class simply triggering CStruct annotation processor to generate the types 'RenderingConfig' etc. + *

+ * Due to Java8 issues, see Bug 923, + * using {@link package-info} is more elegant to kick-off the annotation processor. + *

+ */ +public class BuildStruct01 { + + // APT is only triggered for fields, + // hence we use unused fields in this unit test! + + // @CStruct(name="RenderingConfig", header="TestStruct01.h") + // MyRenderingConfig config; + + // @CStruct(header="TestStruct01.h") + // MyRenderingConfig config; + + /** + * Java8: We cannot use type 'RenderingConfig' yet (bug?) even if not compiling. + * Hence we force the type-name via 'jname' and use a dummy variable! + */ + @CStruct(name="RenderingConfig", jname="RenderingConfig", header="TestStruct01.h") + boolean dummy1; + + @CStruct(name="Pixel", jname="Pixel", header="TestStruct02.h") + boolean dummy2; + + public static void initSingleton() {} +} diff --git a/src/junit/com/jogamp/gluegen/test/junit/structgen/TestStruct02.h b/src/junit/com/jogamp/gluegen/test/junit/structgen/TestStruct02.h new file mode 100644 index 0000000..64fda78 --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/structgen/TestStruct02.h @@ -0,0 +1,14 @@ +// +// TestStruct02.h +// + +#include "TestStruct01.h" + +typedef struct { + float r, g, b, a; +} Col4f; + +typedef struct { + Col4f color; + Vec3f pos; +} Pixel; diff --git a/src/junit/com/jogamp/gluegen/test/junit/structgen/TestStructGen01.java b/src/junit/com/jogamp/gluegen/test/junit/structgen/TestStructGen01.java index 23b1624..74f0552 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/structgen/TestStructGen01.java +++ b/src/junit/com/jogamp/gluegen/test/junit/structgen/TestStructGen01.java @@ -1,26 +1,23 @@ package com.jogamp.gluegen.test.junit.structgen; -import com.jogamp.gluegen.structgen.CStruct; import com.jogamp.junit.util.JunitTracer; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; - import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class TestStructGen01 extends JunitTracer { - // APT is only triggered for fields, - // hence we use unused fields in this unit test! - - // @CStruct(name="RenderingConfig", header="TestStruct01.h") - // MyRenderingConfig config; - - @CStruct(header="TestStruct01.h") - RenderingConfig config0; - + @BeforeClass + public static void init() { + // Enforce dependency, + // i.e. CStruct annotation processor to generate the types 'RenderingConfig' etc. + BuildStruct01.initSingleton(); + } + @Test public void test01() { RenderingConfig config = RenderingConfig.create(); @@ -46,10 +43,10 @@ public class TestStructGen01 extends JunitTracer { cam_orig.setY(1); cam_orig.setZ(2); } - + public static void main(String args[]) { String tstname = TestStructGen01.class.getName(); org.junit.runner.JUnitCore.main(tstname); } - + } diff --git a/src/junit/com/jogamp/gluegen/test/junit/structgen/TestStructGen02.java b/src/junit/com/jogamp/gluegen/test/junit/structgen/TestStructGen02.java new file mode 100644 index 0000000..a8f0e52 --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/structgen/TestStructGen02.java @@ -0,0 +1,51 @@ +package com.jogamp.gluegen.test.junit.structgen; + +import com.jogamp.junit.util.JunitTracer; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestStructGen02 extends JunitTracer { + + @BeforeClass + public static void init() { + // Enforce dependency, + // i.e. CStruct annotation processor to generate the types 'RenderingConfig' etc. + BuildStruct01.initSingleton(); + } + + @Test + public void test01() { + final Pixel pixel = Pixel.create(); + final Col4f color = pixel.getColor(); + color.setR(1f); + color.setG(2f); + color.setB(3f); + color.setA(4f); + final Vec3f pos = pixel.getPos(); + pos.setX(0.5f); + pos.setY(0.6f); + pos.setZ(0.7f); + + Pixel pixel2 = Pixel.create(pixel.getBuffer()); + final Col4f color2 = pixel2.getColor(); + Assert.assertEquals(color.getR(), color2.getR(), 0.0001f); + Assert.assertEquals(color.getG(), color2.getG(), 0.0001f); + Assert.assertEquals(color.getB(), color2.getB(), 0.0001f); + Assert.assertEquals(color.getA(), color2.getA(), 0.0001f); + final Vec3f pos2 = pixel2.getPos(); + Assert.assertEquals(pos.getX(), pos2.getX(), 0.0001f); + Assert.assertEquals(pos.getY(), pos2.getY(), 0.0001f); + Assert.assertEquals(pos.getZ(), pos2.getZ(), 0.0001f); + } + + public static void main(String args[]) { + String tstname = TestStructGen02.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } + +} diff --git a/src/junit/com/jogamp/gluegen/test/junit/structgen/package-info.java b/src/junit/com/jogamp/gluegen/test/junit/structgen/package-info.java new file mode 100644 index 0000000..d009c5f --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/structgen/package-info.java @@ -0,0 +1,15 @@ +/** + * Package scope generation of {@link CStruct}s + * avoiding Java8 issues w/ annotation processing + * where the generated class is not yet available. + *

+ * See Bug 923. + *

+ * @see BuildStruct01 + */ +@CStructs({@CStruct(name="RenderingConfig", header="TestStruct01.h"), @CStruct(name="Pixel", header="TestStruct02.h")}) +package com.jogamp.gluegen.test.junit.structgen; + +import com.jogamp.gluegen.structgen.CStructs; +import com.jogamp.gluegen.structgen.CStruct; + -- cgit v1.2.3