]> uap-core.de Git - rssreader.git/commitdiff
add Var annotation
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Wed, 2 Jul 2025 16:42:58 +0000 (18:42 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Wed, 2 Jul 2025 16:42:58 +0000 (18:42 +0200)
ui-java/src/main/java/de/unixwork/ui/Document.java
ui-java/src/main/java/de/unixwork/ui/Var.java [new file with mode: 0644]

index 47744c4ca66eb0202b60c9d38143fd77e95a9aa8..b1ad20c9dc009245e6c149e0b820318a20342191 100644 (file)
@@ -1,6 +1,7 @@
 package de.unixwork.ui;
 
 import java.lang.foreign.MemorySegment;
+import java.lang.reflect.Field;
 
 public class Document extends Context {
     protected MemorySegment ptr;
@@ -18,6 +19,29 @@ public class Document extends Context {
         } catch (Throwable e) {
             throw new RuntimeException(e);
         }
+
+        // init UI vars
+        Class<?> clazz = this.getClass();
+        for (Field field : clazz.getDeclaredFields()) {
+            Var annotation = field.getAnnotation(Var.class);
+            if (annotation != null) {
+                String name = annotation.name();
+                Class<?> type = field.getType();
+                
+                try {
+                    field.setAccessible(true); // allow access to private fields
+                    if (type == UiString.class) {
+                        field.set(this, this.string(name));
+                    } else if (type == UiInteger.class) {
+                        field.set(this, this.integer(name));
+                    } else if (type == UiList.class) {
+                        field.set(this, this.list(name));
+                    }
+                } catch (Exception e) {
+                    throw new RuntimeException("Failed to initialize field " + field.getName(), e);
+                }
+            }
+        }
     }
 
     public MemorySegment getPtr() {
diff --git a/ui-java/src/main/java/de/unixwork/ui/Var.java b/ui-java/src/main/java/de/unixwork/ui/Var.java
new file mode 100644 (file)
index 0000000..c9fc3b6
--- /dev/null
@@ -0,0 +1,12 @@
+package de.unixwork.ui;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.lang.annotation.ElementType;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface Var {
+    String name();
+}
\ No newline at end of file