From 7e995efb6229a54b590a2ca3180b9702cd2c6090 Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Wed, 26 Sep 2018 10:48:19 +0200 Subject: [PATCH] Add methods th the plugin API to access type and inheritance information (#1491) --- src/main/java/io/ebean/plugin/BeanType.java | 30 +++++++++++++++++++ src/main/java/io/ebean/plugin/Property.java | 9 ++++++ .../server/deploy/BeanDescriptor.java | 29 ++++++++++++++++++ .../server/deploy/BeanProperty.java | 3 ++ 4 files changed, 71 insertions(+) diff --git a/src/main/java/io/ebean/plugin/BeanType.java b/src/main/java/io/ebean/plugin/BeanType.java index 2f00cd0af..bb487de35 100644 --- a/src/main/java/io/ebean/plugin/BeanType.java +++ b/src/main/java/io/ebean/plugin/BeanType.java @@ -9,6 +9,10 @@ import io.ebean.event.BeanQueryAdapter; import io.ebeanservice.docstore.api.mapping.DocumentMapping; import java.util.Collection; +import java.util.List; +import java.util.function.Consumer; + +import javax.annotation.Nonnull; /** * Information and methods on BeanDescriptors made available to plugins. @@ -18,6 +22,7 @@ public interface BeanType { /** * Return the short name of the bean type. */ + @Nonnull String getName(); /** @@ -28,11 +33,13 @@ public interface BeanType { /** * Return the full name of the bean type. */ + @Nonnull String getFullName(); /** * Return the class type this BeanDescriptor describes. */ + @Nonnull Class getBeanType(); /** @@ -43,6 +50,7 @@ public interface BeanType { /** * Return all the properties for this bean type. */ + @Nonnull Collection allProperties(); /** @@ -197,6 +205,28 @@ public interface BeanType { */ boolean hasInheritance(); + /** + * Return true if this object is the root level object in its entity + * inheritance. + */ + boolean isInheritanceRoot(); + + /** + * Returns all direct children of this beantype + */ + List> getInheritanceChildren(); + + /** + * Returns the parent in inheritance hiearchy + */ + BeanType getInheritanceParent(); + + /** + * Visit all children recursively + * @param visitor + */ + void visitAllInheritanceChildren(Consumer> visitor); + /** * Return the discriminator column. */ diff --git a/src/main/java/io/ebean/plugin/Property.java b/src/main/java/io/ebean/plugin/Property.java index e3e9d2c3e..20d81d7e5 100644 --- a/src/main/java/io/ebean/plugin/Property.java +++ b/src/main/java/io/ebean/plugin/Property.java @@ -1,5 +1,7 @@ package io.ebean.plugin; +import javax.annotation.Nonnull; + /** * Property of a entity bean that can be read. */ @@ -8,8 +10,15 @@ public interface Property { /** * Return the name of the property. */ + @Nonnull String getName(); + /** + * Return the type of the property. + */ + @Nonnull + Class getPropertyType(); + /** * Return the value of the property on the given bean. */ diff --git a/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptor.java b/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptor.java index 4a0c711d5..f114970f9 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptor.java +++ b/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptor.java @@ -100,6 +100,7 @@ import java.sql.SQLException; import java.sql.Types; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; @@ -107,6 +108,8 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Consumer; +import java.util.stream.Collectors; /** * Describes Beans including their deployment information. @@ -1133,6 +1136,7 @@ public class BeanDescriptor implements BeanType, STreeType { * Return true if this object is the root level object in its entity * inheritance. */ + @Override public boolean isInheritanceRoot() { return inheritInfo == null || inheritInfo.isRoot(); } @@ -3475,4 +3479,29 @@ public class BeanDescriptor implements BeanType, STreeType { public List getUniqueProps() { return propertiesUnique; } + + @Override + public List> getInheritanceChildren() { + if (hasInheritance()) { + return getInheritInfo().getChildren() + .stream() + .map(InheritInfo::desc) + .collect(Collectors.toList()); + } else { + return Collections.emptyList(); + } + } + + @Override + public BeanType getInheritanceParent() { + return getInheritInfo() == null ? null : getInheritInfo().getParent().desc(); + } + + @Override + public void visitAllInheritanceChildren(Consumer> visitor) { + if (hasInheritance()) { + getInheritInfo().visitChildren(info -> visitor.accept(info.desc())); + } + } + } diff --git a/src/main/java/io/ebeaninternal/server/deploy/BeanProperty.java b/src/main/java/io/ebeaninternal/server/deploy/BeanProperty.java index 7473e01aa..8b0650f54 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/BeanProperty.java +++ b/src/main/java/io/ebeaninternal/server/deploy/BeanProperty.java @@ -979,6 +979,7 @@ public class BeanProperty implements ElPropertyValue, Property, STreeProperty { /** * Return the full name of this property. */ + @Override public String getFullBeanName() { return descriptor.getFullName() + "." + name; } @@ -994,6 +995,7 @@ public class BeanProperty implements ElPropertyValue, Property, STreeProperty { /** * Return the scalarType. */ + @Override @SuppressWarnings(value = "unchecked") public ScalarType getScalarType() { return scalarType; @@ -1369,6 +1371,7 @@ public class BeanProperty implements ElPropertyValue, Property, STreeProperty { /** * Return the property type. */ + @Override public Class getPropertyType() { return propertyType; }