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); + } + +}