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
This commit is contained in:
parent
0ee6f5aa6b
commit
79d1ebd486
|
@ -22,5 +22,6 @@
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
</attributes>
|
</attributes>
|
||||||
</classpathentry>
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
|
||||||
<classpathentry kind="output" path="target/classes"/>
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
|
@ -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<Common> commonClass = Common.class;
|
||||||
|
Class<FisheryRecord> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue