1 package org.simantics.g3d.objmap.schema;
\r
3 import java.lang.annotation.Annotation;
\r
4 import java.lang.reflect.Field;
\r
5 import java.lang.reflect.Method;
\r
6 import java.util.ArrayList;
\r
7 import java.util.Collection;
\r
9 import org.simantics.db.ReadGraph;
\r
10 import org.simantics.db.exception.DatabaseException;
\r
11 import org.simantics.g3d.objmap.annotations.HasCollectionAdder;
\r
12 import org.simantics.g3d.objmap.annotations.HasCollectionRemover;
\r
13 import org.simantics.g3d.objmap.annotations.HasCollectionRuleFactory;
\r
14 import org.simantics.g3d.objmap.annotations.HasGetSetRuleFactory;
\r
15 import org.simantics.g3d.objmap.annotations.HasSetter;
\r
16 import org.simantics.g3d.objmap.rules.factory.ICollectionRuleFactory;
\r
17 import org.simantics.g3d.objmap.rules.factory.IGetSetRuleFactory;
\r
18 import org.simantics.objmap.ILinkType;
\r
19 import org.simantics.objmap.IMappingRule;
\r
20 import org.simantics.objmap.annotations.GraphType;
\r
21 import org.simantics.objmap.annotations.RelatedValue;
\r
22 import org.simantics.objmap.annotations.meta.HasClassRuleFactory;
\r
23 import org.simantics.objmap.annotations.meta.HasFieldRuleFactory;
\r
24 import org.simantics.objmap.annotations.meta.HasMethodRuleFactory;
\r
25 import org.simantics.objmap.schema.SimpleLinkType;
\r
27 public class MappingSchemas {
\r
29 * Creates a new SimpleLinkType based on the annotations in the given class.
\r
30 * @throws IllegalAccessException
\r
31 * @throws InstantiationException
\r
35 public static SimpleLinkType fromAnnotations(ReadGraph g, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
\r
36 GraphType graphType = clazz.getAnnotation(GraphType.class);
\r
38 ArrayList<IMappingRule> rules = new ArrayList<IMappingRule>();
\r
39 collectRulesFromAnnotations(g, clazz, rules);
\r
41 return new SimpleLinkType(
\r
42 g.getResource(graphType.value()),
\r
46 public static void collectRulesFromAnnotations(ReadGraph g, Class<?> clazz, Collection<IMappingRule> rules) throws DatabaseException, InstantiationException, IllegalAccessException {
\r
47 Class<?> superclass = clazz.getSuperclass();
\r
48 if(superclass != null)
\r
49 collectRulesFromAnnotations(g, superclass, rules);
\r
51 for(Annotation annotation : clazz.getAnnotations()) {
\r
52 HasClassRuleFactory factory =
\r
53 annotation.annotationType().getAnnotation(HasClassRuleFactory.class);
\r
54 if(factory != null) {
\r
55 rules.add(factory.value().newInstance()
\r
56 .create(g, annotation, clazz));
\r
60 for(Field f : clazz.getDeclaredFields()) {
\r
61 f.setAccessible(true);
\r
63 for(Annotation annotation : f.getAnnotations()) {
\r
64 HasFieldRuleFactory factory =
\r
65 annotation.annotationType().getAnnotation(HasFieldRuleFactory.class);
\r
66 if(factory != null) {
\r
67 rules.add(factory.value().newInstance()
\r
68 .create(g, annotation, f));
\r
73 for(Method m : clazz.getDeclaredMethods()) {
\r
74 m.setAccessible(true);
\r
76 for(Annotation annotation : m.getAnnotations()) {
\r
77 HasMethodRuleFactory factory =
\r
78 annotation.annotationType().getAnnotation(HasMethodRuleFactory.class);
\r
79 if(factory != null) {
\r
80 rules.add(factory.value().newInstance().create(g, annotation, m));
\r
85 for (Method m : clazz.getDeclaredMethods()) {
\r
86 m.setAccessible(true);
\r
87 for (Annotation annotation : m.getAnnotations()) {
\r
88 Class<? extends Annotation> annotationType = annotation.annotationType();
\r
89 HasGetSetRuleFactory factory =
\r
90 annotationType.getAnnotation(HasGetSetRuleFactory.class);
\r
91 if (factory != null) {
\r
93 HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class);
\r
95 Class<? extends Annotation> setterAnn = setterAnnType.value();
\r
97 IGetSetRuleFactory ruleFactory = factory.value().newInstance();
\r
100 Method setter = null;
\r
102 for (Method m2 : clazz.getDeclaredMethods()) {
\r
103 Annotation set = m2.getAnnotation(setterAnn);
\r
104 if (set != null && ruleFactory.isSetter(annotation, set))
\r
108 rules.add(ruleFactory.create(g, annotation, getter, setter));
\r
114 for (Method m : clazz.getDeclaredMethods()) {
\r
115 m.setAccessible(true);
\r
116 for (Annotation annotation : m.getAnnotations()) {
\r
117 Class<? extends Annotation> annotationType = annotation.annotationType();
\r
118 HasCollectionRuleFactory factory =
\r
119 annotationType.getAnnotation(HasCollectionRuleFactory.class);
\r
120 if (factory != null) {
\r
122 HasCollectionAdder adderAnnType = annotationType.getAnnotation(HasCollectionAdder.class);
\r
123 HasCollectionRemover removerAnnType = annotationType.getAnnotation(HasCollectionRemover.class);
\r
125 Class<? extends Annotation> adderAnn = adderAnnType.value();
\r
126 Class<? extends Annotation> removerAnn = removerAnnType.value();
\r
128 ICollectionRuleFactory ruleFactory = factory.value().newInstance();
\r
131 Method adder = null;
\r
132 Method remover = null;
\r
134 for (Method m2 : clazz.getDeclaredMethods()) {
\r
135 Annotation add = m2.getAnnotation(adderAnn);
\r
136 Annotation rem = m2.getAnnotation(removerAnn);
\r
137 if (add != null && ruleFactory.isAdder(annotation, add))
\r
139 if (rem != null && ruleFactory.isRemover(annotation, rem))
\r
145 rules.add(ruleFactory.create(g, annotation, getter,adder,remover));
\r
153 * Creates a new SimpleLinkType based on the annotations in the given class.
\r
154 * @throws IllegalAccessException
\r
155 * @throws InstantiationException
\r
157 * @see RelatedValue
\r
159 public static AdaptedLinkType fromAdaptable(ReadGraph g, String type, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
\r
162 return new AdaptedLinkType(g.getResource(type), clazz);
\r