From: Reino Ruusu Date: Wed, 14 Aug 2019 08:34:15 +0000 (+0300) Subject: Added missing creation of RelatedElementRuleFactory. X-Git-Tag: v1.43.0~136^2~116^2~1 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=025dc96495237a1816afc66235309b9306663242;p=simantics%2Fplatform.git Added missing creation of RelatedElementRuleFactory. Also checks for missing rule factories. gitlab #341 Change-Id: Ieb9799910c3c32a7c49dfd9be496bb9afbca0ab5 --- diff --git a/bundles/org.simantics.objmap2/META-INF/MANIFEST.MF b/bundles/org.simantics.objmap2/META-INF/MANIFEST.MF index efd050e10..87e9bbfd5 100644 --- a/bundles/org.simantics.objmap2/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.objmap2/META-INF/MANIFEST.MF @@ -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, diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java index 871d46bda..56aa5da20 100644 --- a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java +++ b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java @@ -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 fromAnnotations(ReadGraph g, Class clazz) throws DatabaseException, InstantiationException, IllegalAccessException { - GraphType graphType = clazz.getAnnotation(GraphType.class); - - if (graphType != null) { - ArrayList> rules = new ArrayList>(); - collectRulesFromAnnotations(g, clazz, rules); - - return new SimpleLinkType(g.getResource(graphType.value()), clazz, rules); - } - DynamicGraphType dynamicType = clazz.getAnnotation(DynamicGraphType.class); - if (dynamicType != null) { - ArrayList> rules = new ArrayList>(); - collectRulesFromAnnotations(g, clazz, rules); - - return new DynamicSimpleLinkType(g.getResource(dynamicType.value()), clazz, rules); - } - OrderedSetType orderedSetType = clazz.getAnnotation(OrderedSetType.class); - if (orderedSetType != null) { - ArrayList> rules = new ArrayList>(); - collectRulesFromAnnotations(g, clazz, rules); - - return new OrderedSetSimpleLinkType(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> rules) throws DatabaseException, InstantiationException, IllegalAccessException { - Class superclass = clazz.getSuperclass(); - if(superclass != null) - collectRulesFromAnnotations(g, superclass, rules); - + public static SimpleLinkType fromAnnotations(ReadGraph g, Class clazz) throws DatabaseException, InstantiationException, IllegalAccessException { + GraphType graphType = clazz.getAnnotation(GraphType.class); + + if (graphType != null) { + ArrayList> rules = new ArrayList>(); + collectRulesFromAnnotations(g, clazz, rules); + + return new SimpleLinkType(g.getResource(graphType.value()), clazz, rules); + } + DynamicGraphType dynamicType = clazz.getAnnotation(DynamicGraphType.class); + if (dynamicType != null) { + ArrayList> rules = new ArrayList>(); + collectRulesFromAnnotations(g, clazz, rules); + + return new DynamicSimpleLinkType(g.getResource(dynamicType.value()), clazz, rules); + } + OrderedSetType orderedSetType = clazz.getAnnotation(OrderedSetType.class); + if (orderedSetType != null) { + ArrayList> rules = new ArrayList>(); + collectRulesFromAnnotations(g, clazz, rules); + + return new OrderedSetSimpleLinkType(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> 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 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 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 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 annotationType = annotation.annotationType(); - - IsGetSetRule tag = - annotationType.getAnnotation(IsGetSetRule.class); - if (tag != null) { - - HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class); - - Class setterAnn = setterAnnType.value(); - - Method getter = m; - - IGetSetRuleFactory 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 annotationType = annotation.annotationType(); + + IsGetSetRule tag = + annotationType.getAnnotation(IsGetSetRule.class); + if (tag != null) { + + HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class); + + Class setterAnn = setterAnnType.value(); + + Method getter = m; + + IGetSetRuleFactory 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 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 adderAnn = adderAnnType.value(); - Class removerAnn = removerAnnType.value(); - - Method getter = m; - - ICollectionRuleFactory 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 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 adderAnn = adderAnnType.value(); + Class removerAnn = removerAnnType.value(); + + Method getter = m; + + ICollectionRuleFactory 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 createClassRule(ReadGraph g, Annotation annotation, Class clazz) { - return null; - } - - public static IFieldRuleFactory createFieldRule(ReadGraph g, Annotation annotation, Field field) { - if (annotation.annotationType().equals(RelatedElements.class)) - return new RelatedElementsRuleFactory(); - if (annotation.annotationType().equals(RelatedValue.class)) - return new RelatedValueRuleFactory(); - if (annotation.annotationType().equals(OptionalRelatedElements.class)) - return new OptionalRelatedElementsRuleFactory(); - if (annotation.annotationType().equals(RelatedOrderedSetElements.class)) - return new RelatedOrderedSetElementsRuleFactory(); - return null; - } - - public static IMethodRuleFactory createMethodRule(ReadGraph g, Annotation annotation, Method m) { - if (annotation.annotationType().equals(UpdateMethod.class)) - return new UpdateMethodFactory(); - return null; - } - - public static IGetSetRuleFactory createGetSetRuleFactory(ReadGraph g, Annotation annotation, Method getter) { - if (annotation.annotationType().equals(RelatedGetValue.class)) - return new RelatedGetSetValueRuleFactory(); - if (annotation.annotationType().equals(RelatedGetObj.class)) - return new RelatedGetSetObjRuleFactory(); - if (annotation.annotationType().equals(CompoundRelatedGetValue.class)) - return new CompoundRelatedGetSetValueRuleFactory(); - return null; - } - - public static ICollectionRuleFactory createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) { - if (annotation.annotationType().equals(RelatedElementsGet.class)) - return new RelatedElementsRuleFactory2(); - if (annotation.annotationType().equals(OrderedElementsGet.class)) - return new OrderedElementsRuleFactory(); - return null; - } - - /** + + public static IClassRuleFactory createClassRule(ReadGraph g, Annotation annotation, Class clazz) { + return null; + } + + public static IFieldRuleFactory createFieldRule(ReadGraph g, Annotation annotation, Field field) { + if (annotation.annotationType().equals(RelatedElement.class)) + return new RelatedElementRuleFactory(); + if (annotation.annotationType().equals(RelatedElements.class)) + return new RelatedElementsRuleFactory(); + if (annotation.annotationType().equals(RelatedValue.class)) + return new RelatedValueRuleFactory(); + if (annotation.annotationType().equals(OptionalRelatedElements.class)) + return new OptionalRelatedElementsRuleFactory(); + if (annotation.annotationType().equals(RelatedOrderedSetElements.class)) + return new RelatedOrderedSetElementsRuleFactory(); + return null; + } + + public static IMethodRuleFactory createMethodRule(ReadGraph g, Annotation annotation, Method m) { + if (annotation.annotationType().equals(UpdateMethod.class)) + return new UpdateMethodFactory(); + return null; + } + + public static IGetSetRuleFactory createGetSetRuleFactory(ReadGraph g, Annotation annotation, Method getter) { + if (annotation.annotationType().equals(RelatedGetValue.class)) + return new RelatedGetSetValueRuleFactory(); + if (annotation.annotationType().equals(RelatedGetObj.class)) + return new RelatedGetSetObjRuleFactory(); + if (annotation.annotationType().equals(CompoundRelatedGetValue.class)) + return new CompoundRelatedGetSetValueRuleFactory(); + return null; + } + + public static ICollectionRuleFactory createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) { + if (annotation.annotationType().equals(RelatedElementsGet.class)) + return new RelatedElementsRuleFactory2(); + if (annotation.annotationType().equals(OrderedElementsGet.class)) + return new OrderedElementsRuleFactory(); + 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 fromAdaptable(ReadGraph g, String type, Class clazz) throws DatabaseException, InstantiationException, IllegalAccessException { - - - return new AdaptedLinkType(g.getResource(type), clazz); - } - - + + public static AdaptedLinkType fromAdaptable(ReadGraph g, String type, Class clazz) throws DatabaseException, InstantiationException, IllegalAccessException { + + + return new AdaptedLinkType(g.getResource(type), clazz); + } + + }