From 79d1ebd486377d9fdd385ff1f88f8519c82157e3 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Fri, 7 Oct 2016 19:50:21 +0000 Subject: [PATCH] Created Tag and Group annotation. Created junit test to read tagged fields at run time git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@132950 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 1 + .../custom_annotations/Group.java | 16 +++++++ .../custom_annotations/Tag.java | 16 +++++++ .../grsf_publish_ws/JTests.java | 45 +++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 src/main/java/org/gcube/data_catalogue/grsf_publish_ws/custom_annotations/Group.java create mode 100644 src/main/java/org/gcube/data_catalogue/grsf_publish_ws/custom_annotations/Tag.java create mode 100644 src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JTests.java diff --git a/.classpath b/.classpath index 4214a97..7b8885c 100644 --- a/.classpath +++ b/.classpath @@ -22,5 +22,6 @@ + diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/custom_annotations/Group.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/custom_annotations/Group.java new file mode 100644 index 0000000..9686d53 --- /dev/null +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/custom_annotations/Group.java @@ -0,0 +1,16 @@ +package org.gcube.data_catalogue.grsf_publish_ws.custom_annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Group annotation + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface Group { + +} diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/custom_annotations/Tag.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/custom_annotations/Tag.java new file mode 100644 index 0000000..b78346c --- /dev/null +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/custom_annotations/Tag.java @@ -0,0 +1,16 @@ +package org.gcube.data_catalogue.grsf_publish_ws.custom_annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Tag annotation + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface Tag { + +} diff --git a/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JTests.java b/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JTests.java new file mode 100644 index 0000000..8db3819 --- /dev/null +++ b/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JTests.java @@ -0,0 +1,45 @@ +package org.gcube.data_catalogue.grsf_publish_ws; + +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; + +import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Group; +import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag; +import org.gcube.data_catalogue.grsf_publish_ws.json.FisheryRecord; +import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Source; +import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Type; +import org.junit.Test; + +public class JTests { + + @Test + public void test() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, IntrospectionException { + + // Common instance = new Common(); + // instance.setType(Type.Assesment_Unit); + FisheryRecord recordFishery = new FisheryRecord(); + recordFishery.setType(Type.Fishing_Description); + recordFishery.setDatabaseSources(Source.FIRMS); + // Class commonClass = Common.class; + Class fisheryClass = FisheryRecord.class; + + // bottom up, looks up for Tag fields + Class current = fisheryClass; + do{ + // do something with current's fields + System.out.println("Class is " + current.getCanonicalName()); + Field[] fields = current.getDeclaredFields(); + for (Field field : fields) { + if(field.isAnnotationPresent(Tag.class) || field.isAnnotationPresent(Group.class)){ + System.out.println("Annotated field is " + field.getName()); + Object f = new PropertyDescriptor(field.getName(), fisheryClass).getReadMethod().invoke(recordFishery); + System.out.println("Field is " + field.getName() + " and its value for instance is " + f); + } + } + } + while((current = current.getSuperclass())!=null); + } + +}