]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Added missing creation of RelatedElementRuleFactory. 01/3101/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Wed, 14 Aug 2019 08:34:15 +0000 (11:34 +0300)
committerReino Ruusu <reino.ruusu@semantum.fi>
Wed, 14 Aug 2019 08:34:15 +0000 (11:34 +0300)
Also checks for missing rule factories.

gitlab #341

Change-Id: Ieb9799910c3c32a7c49dfd9be496bb9afbca0ab5

bundles/org.simantics.objmap2/META-INF/MANIFEST.MF
bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java

index efd050e10959b97df1d4420dde9f0140e7d9dbdd..87e9bbfd5b032aba11aad36ebdcb9b07607cbbea 100644 (file)
@@ -10,7 +10,8 @@ Require-Bundle: org.simantics.db;bundle-version="1.1.0",
  org.simantics.layer0;bundle-version="1.0.0",
  org.apache.log4j;bundle-version="1.2.15",
  org.simantics.db.common;bundle-version="1.1.0",
- org.simantics.structural.ontology;bundle-version="1.1.0"
+ org.simantics.structural.ontology;bundle-version="1.1.0",
+ org.slf4j.api
 Export-Package: org.simantics.objmap.backward,
  org.simantics.objmap.bidirectional,
  org.simantics.objmap.exceptions,
index 871d46bdaebce02c458098fa1b690717fc9ed741..56aa5da205ed21fb0cde31f9e3fd325d18a0a7f3 100644 (file)
@@ -30,6 +30,7 @@ import org.simantics.objmap.graph.annotations.HasSetter;
 import org.simantics.objmap.graph.annotations.OptionalRelatedElements;
 import org.simantics.objmap.graph.annotations.OrderedElementsGet;
 import org.simantics.objmap.graph.annotations.OrderedSetType;
+import org.simantics.objmap.graph.annotations.RelatedElement;
 import org.simantics.objmap.graph.annotations.RelatedElements;
 import org.simantics.objmap.graph.annotations.RelatedElementsGet;
 import org.simantics.objmap.graph.annotations.RelatedGetObj;
@@ -40,6 +41,7 @@ import org.simantics.objmap.graph.annotations.UpdateMethod;
 import org.simantics.objmap.graph.annotations.factories.CompoundRelatedGetSetValueRuleFactory;
 import org.simantics.objmap.graph.annotations.factories.OptionalRelatedElementsRuleFactory;
 import org.simantics.objmap.graph.annotations.factories.OrderedElementsRuleFactory;
+import org.simantics.objmap.graph.annotations.factories.RelatedElementRuleFactory;
 import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory;
 import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory2;
 import org.simantics.objmap.graph.annotations.factories.RelatedGetSetObjRuleFactory;
@@ -57,52 +59,60 @@ import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory;
 import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory;
 import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;
 import org.simantics.objmap.graph.rules.factory.IMethodRuleFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 public class MappingSchemas {
-       /**
+    private static final Logger LOGGER = LoggerFactory.getLogger(MappingSchemas.class);
+
+    /**
      * Creates a new SimpleLinkType based on the annotations in the given class.
      * @throws IllegalAccessException 
      * @throws InstantiationException 
      * @see GraphType
      * @see RelatedValue
      */
-       public static SimpleLinkType<Object> fromAnnotations(ReadGraph g, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
-           GraphType graphType = clazz.getAnnotation(GraphType.class);
-           
-           if (graphType != null) {
-                   ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
-                   collectRulesFromAnnotations(g, clazz, rules);
-                   
-                   return new SimpleLinkType<Object>(g.getResource(graphType.value()), clazz, rules);  
-           }
-           DynamicGraphType dynamicType = clazz.getAnnotation(DynamicGraphType.class);
-           if (dynamicType != null) {
-               ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
-                   collectRulesFromAnnotations(g, clazz, rules);
-                   
-                   return new DynamicSimpleLinkType<Object>(g.getResource(dynamicType.value()), clazz, rules);
-           }
-           OrderedSetType orderedSetType = clazz.getAnnotation(OrderedSetType.class);
-           if (orderedSetType != null) {
-               ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
-                   collectRulesFromAnnotations(g, clazz, rules);
-                   
-                   return new OrderedSetSimpleLinkType<Object>(g.getResource(orderedSetType.value()), clazz, rules);
-           }
-           throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations.");
-       }
-       
-       public static void collectRulesFromAnnotations(ReadGraph g, Class<?> clazz, Collection<IBidirectionalMappingRule<Resource, Object>> rules) throws DatabaseException, InstantiationException, IllegalAccessException {
-           Class<?> superclass = clazz.getSuperclass();
-           if(superclass != null)
-               collectRulesFromAnnotations(g, superclass, rules);
-               
+    public static SimpleLinkType<Object> fromAnnotations(ReadGraph g, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
+        GraphType graphType = clazz.getAnnotation(GraphType.class);
+
+        if (graphType != null) {
+            ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
+            collectRulesFromAnnotations(g, clazz, rules);
+
+            return new SimpleLinkType<Object>(g.getResource(graphType.value()), clazz, rules);  
+        }
+        DynamicGraphType dynamicType = clazz.getAnnotation(DynamicGraphType.class);
+        if (dynamicType != null) {
+            ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
+            collectRulesFromAnnotations(g, clazz, rules);
+
+            return new DynamicSimpleLinkType<Object>(g.getResource(dynamicType.value()), clazz, rules);
+        }
+        OrderedSetType orderedSetType = clazz.getAnnotation(OrderedSetType.class);
+        if (orderedSetType != null) {
+            ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
+            collectRulesFromAnnotations(g, clazz, rules);
+
+            return new OrderedSetSimpleLinkType<Object>(g.getResource(orderedSetType.value()), clazz, rules);
+        }
+        throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations.");
+    }
+
+    public static void collectRulesFromAnnotations(ReadGraph g, Class<?> clazz, Collection<IBidirectionalMappingRule<Resource, Object>> rules) throws DatabaseException, InstantiationException, IllegalAccessException {
+        Class<?> superclass = clazz.getSuperclass();
+        if(superclass != null)
+            collectRulesFromAnnotations(g, superclass, rules);
+
         for(Annotation annotation : clazz.getAnnotations()) {
 
-               IsClassRule tag = annotation.annotationType().getAnnotation(IsClassRule.class);
+            IsClassRule tag = annotation.annotationType().getAnnotation(IsClassRule.class);
             if(tag!= null) {
-               rules.add(createClassRule(g, annotation, clazz).create(g, annotation, clazz));
+                final IClassRuleFactory<Resource, Object> ruleFactory = createClassRule(g, annotation, clazz);
+                if (ruleFactory != null)
+                    rules.add(ruleFactory.create(g, annotation, clazz));
+                else
+                    LOGGER.warn("No rule factory found for {}", annotation);
             }
         }
 
@@ -111,9 +121,13 @@ public class MappingSchemas {
 
             for(Annotation annotation : f.getAnnotations()) {
 
-               IsFieldRule tag = annotation.annotationType().getAnnotation(IsFieldRule.class);
+                IsFieldRule tag = annotation.annotationType().getAnnotation(IsFieldRule.class);
                 if(tag != null) {
-                    rules.add(createFieldRule(g, annotation, f).create(g, annotation, f));
+                    final IFieldRuleFactory<Resource, Object> ruleFactory = createFieldRule(g, annotation, f);
+                    if (ruleFactory != null)
+                        rules.add(ruleFactory.create(g, annotation, f));
+                    else
+                        LOGGER.warn("No rule factory found for {}", annotation);
                 }
             }
         }
@@ -122,140 +136,150 @@ public class MappingSchemas {
             m.setAccessible(true);
 
             for(Annotation annotation : m.getAnnotations()) {
-               IsMethodRule tag = 
+                IsMethodRule tag = 
                         annotation.annotationType().getAnnotation(IsMethodRule.class);
                 if(tag != null) {
-                       rules.add(createMethodRule(g, annotation, m).create(g, annotation, m));
+                    final IMethodRuleFactory<Resource, Object> ruleFactory = createMethodRule(g, annotation, m);
+                    if (ruleFactory != null)
+                        rules.add(ruleFactory.create(g, annotation, m));
+                    else
+                        LOGGER.warn("No rule factory found for {}", annotation);
                 }
             }
         }
-        
+
         for (Method m : clazz.getDeclaredMethods()) {
-               m.setAccessible(true);
-               for (Annotation annotation : m.getAnnotations()) {
-                       Class<? extends Annotation> annotationType = annotation.annotationType();
-
-                        IsGetSetRule tag = 
-                         annotationType.getAnnotation(IsGetSetRule.class);
-                        if (tag != null) {
-                                
-                                HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class);
-                                
-                                Class<? extends Annotation> setterAnn = setterAnnType.value();
-                                
-                                Method getter = m;
-                                
-                                IGetSetRuleFactory<Resource,Object> ruleFactory = createGetSetRuleFactory(g, annotation, getter);
-                                
-                                
-                                Method setter = null;
-                                
-                                for (Method m2 : clazz.getDeclaredMethods()) {
-                                        Annotation set = m2.getAnnotation(setterAnn);
-                                        if (set != null && ruleFactory.isSetter(annotation, set))
-                                                setter = m2;
-                                }
-
-                                rules.add(ruleFactory.create(g, annotation, getter, setter));
-                        }
-                
-               }
+            m.setAccessible(true);
+            for (Annotation annotation : m.getAnnotations()) {
+                Class<? extends Annotation> annotationType = annotation.annotationType();
+
+                IsGetSetRule tag = 
+                        annotationType.getAnnotation(IsGetSetRule.class);
+                if (tag != null) {
+
+                    HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class);
+
+                    Class<? extends Annotation> setterAnn = setterAnnType.value();
+
+                    Method getter = m;
+
+                    IGetSetRuleFactory<Resource,Object> ruleFactory = createGetSetRuleFactory(g, annotation, getter);
+                    if (ruleFactory != null) {
+                        Method setter = null;
+    
+                        for (Method m2 : clazz.getDeclaredMethods()) {
+                            Annotation set = m2.getAnnotation(setterAnn);
+                            if (set != null && ruleFactory.isSetter(annotation, set))
+                                setter = m2;
+                        }
+    
+                        rules.add(ruleFactory.create(g, annotation, getter, setter));
+                    }
+                    else {
+                        LOGGER.warn("No rule factory found for {}", annotation);
+                    }
+                }
+
+            }
         }
-        
+
         for (Method m : clazz.getDeclaredMethods()) {
-               m.setAccessible(true);
-               for (Annotation annotation : m.getAnnotations()) {
-                       Class<? extends Annotation> annotationType = annotation.annotationType();
-
-                        IsCollectionRule tag = 
-                         annotationType.getAnnotation(IsCollectionRule.class);
-                        if (tag != null) {
-                                
-                                HasCollectionAdder adderAnnType = annotationType.getAnnotation(HasCollectionAdder.class);
-                                HasCollectionRemover removerAnnType = annotationType.getAnnotation(HasCollectionRemover.class);
-                
-                                Class<? extends Annotation> adderAnn = adderAnnType.value();
-                                Class<? extends Annotation> removerAnn = removerAnnType.value();
-                                
-                                Method getter = m;
-                                
-                                ICollectionRuleFactory<Resource,Object> ruleFactory = createCollectionRuleFactory(g, annotation, getter);
-                                
-                                
-                                Method adder = null;
-                                Method remover = null;
-                                
-                                for (Method m2 : clazz.getDeclaredMethods()) {
-                                        Annotation add = m2.getAnnotation(adderAnn);
-                                        Annotation rem = m2.getAnnotation(removerAnn);
-                                        if (add != null && ruleFactory.isAdder(annotation, add))
-                                                adder = m2;
-                                        if (rem != null && ruleFactory.isRemover(annotation, rem))
-                                                remover = m2;
-                                }
-                                
-                                
-                                
-                                rules.add(ruleFactory.create(g, annotation, getter,adder,remover));
-                        }
-                
-               }
+            m.setAccessible(true);
+            for (Annotation annotation : m.getAnnotations()) {
+                Class<? extends Annotation> annotationType = annotation.annotationType();
+
+                IsCollectionRule tag = 
+                        annotationType.getAnnotation(IsCollectionRule.class);
+                if (tag != null) {
+
+                    HasCollectionAdder adderAnnType = annotationType.getAnnotation(HasCollectionAdder.class);
+                    HasCollectionRemover removerAnnType = annotationType.getAnnotation(HasCollectionRemover.class);
+
+                    Class<? extends Annotation> adderAnn = adderAnnType.value();
+                    Class<? extends Annotation> removerAnn = removerAnnType.value();
+
+                    Method getter = m;
+
+                    ICollectionRuleFactory<Resource,Object> ruleFactory = createCollectionRuleFactory(g, annotation, getter);
+                    if (ruleFactory != null) {
+                        Method adder = null;
+                        Method remover = null;
+    
+                        for (Method m2 : clazz.getDeclaredMethods()) {
+                            Annotation add = m2.getAnnotation(adderAnn);
+                            Annotation rem = m2.getAnnotation(removerAnn);
+                            if (add != null && ruleFactory.isAdder(annotation, add))
+                                adder = m2;
+                            if (rem != null && ruleFactory.isRemover(annotation, rem))
+                                remover = m2;
+                        }
+    
+                        rules.add(ruleFactory.create(g, annotation, getter,adder,remover));
+                    }
+                    else {
+                        LOGGER.warn("No rule factory found for {}", annotation);
+                    }
+                }
+
+            }
         }
     }
-       
-       public static IClassRuleFactory<Resource, Object> createClassRule(ReadGraph g, Annotation annotation, Class<?> clazz) {
-               return null;
-       }
-       
-       public static IFieldRuleFactory<Resource,Object> createFieldRule(ReadGraph g, Annotation annotation, Field field) {
-               if (annotation.annotationType().equals(RelatedElements.class))
-                       return new RelatedElementsRuleFactory<Object>();
-               if (annotation.annotationType().equals(RelatedValue.class))
-                       return new RelatedValueRuleFactory<Object>();
-               if (annotation.annotationType().equals(OptionalRelatedElements.class))
-                       return new OptionalRelatedElementsRuleFactory<Object>();
-               if (annotation.annotationType().equals(RelatedOrderedSetElements.class))
-                       return new RelatedOrderedSetElementsRuleFactory<Object>();
-               return null;
-       }
-       
-       public static IMethodRuleFactory<Resource, Object> createMethodRule(ReadGraph g, Annotation annotation, Method m) {
-               if (annotation.annotationType().equals(UpdateMethod.class))
-                       return new UpdateMethodFactory<Resource,Object>();
-               return null;
-       }
-       
-       public static IGetSetRuleFactory<Resource,Object> createGetSetRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
-               if (annotation.annotationType().equals(RelatedGetValue.class))
-                       return new RelatedGetSetValueRuleFactory<Object>();
-               if (annotation.annotationType().equals(RelatedGetObj.class))
-                       return new RelatedGetSetObjRuleFactory<Object>();
-               if (annotation.annotationType().equals(CompoundRelatedGetValue.class))
-                       return new CompoundRelatedGetSetValueRuleFactory<Object>();
-               return null;
-       }
-       
-       public static ICollectionRuleFactory<Resource,Object> createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
-               if (annotation.annotationType().equals(RelatedElementsGet.class))
-                       return new RelatedElementsRuleFactory2<Object>();
-               if (annotation.annotationType().equals(OrderedElementsGet.class))
-                       return new OrderedElementsRuleFactory<Object>();
-               return null;
-       }
-       
-       /**
+
+    public static IClassRuleFactory<Resource, Object> createClassRule(ReadGraph g, Annotation annotation, Class<?> clazz) {
+        return null;
+    }
+
+    public static IFieldRuleFactory<Resource,Object> createFieldRule(ReadGraph g, Annotation annotation, Field field) {
+        if (annotation.annotationType().equals(RelatedElement.class))
+            return new RelatedElementRuleFactory<Object>();
+        if (annotation.annotationType().equals(RelatedElements.class))
+            return new RelatedElementsRuleFactory<Object>();
+        if (annotation.annotationType().equals(RelatedValue.class))
+            return new RelatedValueRuleFactory<Object>();
+        if (annotation.annotationType().equals(OptionalRelatedElements.class))
+            return new OptionalRelatedElementsRuleFactory<Object>();
+        if (annotation.annotationType().equals(RelatedOrderedSetElements.class))
+            return new RelatedOrderedSetElementsRuleFactory<Object>();
+        return null;
+    }
+
+    public static IMethodRuleFactory<Resource, Object> createMethodRule(ReadGraph g, Annotation annotation, Method m) {
+        if (annotation.annotationType().equals(UpdateMethod.class))
+            return new UpdateMethodFactory<Resource,Object>();
+        return null;
+    }
+
+    public static IGetSetRuleFactory<Resource,Object> createGetSetRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
+        if (annotation.annotationType().equals(RelatedGetValue.class))
+            return new RelatedGetSetValueRuleFactory<Object>();
+        if (annotation.annotationType().equals(RelatedGetObj.class))
+            return new RelatedGetSetObjRuleFactory<Object>();
+        if (annotation.annotationType().equals(CompoundRelatedGetValue.class))
+            return new CompoundRelatedGetSetValueRuleFactory<Object>();
+        return null;
+    }
+
+    public static ICollectionRuleFactory<Resource,Object> createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
+        if (annotation.annotationType().equals(RelatedElementsGet.class))
+            return new RelatedElementsRuleFactory2<Object>();
+        if (annotation.annotationType().equals(OrderedElementsGet.class))
+            return new OrderedElementsRuleFactory<Object>();
+        return null;
+    }
+
+    /**
      * Creates a new SimpleLinkType based on the annotations in the given class.
      * @throws IllegalAccessException 
      * @throws InstantiationException 
      * @see GraphType
      * @see RelatedValue
      */
-       
-       public static AdaptedLinkType<Object> fromAdaptable(ReadGraph g, String type, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
-           
-           
-           return new AdaptedLinkType<Object>(g.getResource(type), clazz);    
-       }
-       
-       
+
+    public static AdaptedLinkType<Object> fromAdaptable(ReadGraph g, String type, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
+
+
+        return new AdaptedLinkType<Object>(g.getResource(type), clazz);    
+    }
+
+
 }