From 2263d31f46f9ce7e6b34114ecf851217283af5d8 Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Wed, 18 Oct 2017 22:34:57 +0200 Subject: [PATCH] Improved the performance of findAnnotation (#1177) --- .../java/io/ebean/util/AnnotationUtil.java | 68 ++++++++++++------- .../server/cache/DefaultCacheHolder.java | 4 +- .../changelog/DefaultChangeLogRegister.java | 4 +- .../server/core/bootup/BootupClasses.java | 4 +- .../server/deploy/BeanDescriptorManager.java | 4 +- .../deploy/meta/DeployBeanProperty.java | 22 +++--- .../server/deploy/parse/AnnotationBase.java | 65 ++---------------- .../server/deploy/parse/AnnotationClass.java | 33 ++++----- .../server/deploy/parse/AnnotationSql.java | 3 +- .../deploy/parse/DeployCreateProperties.java | 17 ++--- .../server/deploy/parse/DeployInherit.java | 9 +-- .../server/type/DefaultTypeManager.java | 6 +- .../org/tests/basic/TestAnnotationBase.java | 22 +++--- 13 files changed, 116 insertions(+), 145 deletions(-) diff --git a/src/main/java/io/ebean/util/AnnotationUtil.java b/src/main/java/io/ebean/util/AnnotationUtil.java index bad0b6dfa..0f1f2275a 100644 --- a/src/main/java/io/ebean/util/AnnotationUtil.java +++ b/src/main/java/io/ebean/util/AnnotationUtil.java @@ -39,12 +39,21 @@ public class AnnotationUtil { return null; } // check if directly present, if not, start search for meta-annotations. - A ann = annotatedElement.getAnnotation(annotationType); - if (ann != null) { - return ann; - } else { - return findAnnotation(annotatedElement, annotationType, new HashSet<>()); + Annotation[] anns = annotatedElement.getAnnotations(); + if (anns.length == 0) { + return null; // no annotations present, so searching for meta annotations not required } + + // As we need the anns array anyway, we iterate over this instead + // of using annotatedElement.getAnnotation(...) which is synchronized internally + for (Annotation ann : anns) { + if (ann.annotationType() == annotationType) { + return (A) ann; + } + } + + return findAnnotation(anns, annotationType, new HashSet<>()); + } /** @@ -57,21 +66,27 @@ public class AnnotationUtil { if (annotationType == null) { return null; } - // check if directly present, if not, start search for meta-annotations. - A ann = clazz.getAnnotation(annotationType); - if (ann != null) { - return ann; - } else { - while (clazz != null && clazz != Object.class) { - ann = findAnnotation(clazz, annotationType, new HashSet<>()); + + while (clazz != null && clazz != Object.class) { + // check if directly present, if not, start search for meta-annotations. + Annotation[] anns = clazz.getAnnotations(); + if (anns.length != 0) { + for (Annotation ann : anns) { + if (ann.annotationType() == annotationType) { + return (A) ann; + } + } + + A ann = findAnnotation(anns, annotationType, new HashSet<>()); if (ann != null) { return ann; } - // no meta-annotation present at this class - traverse to superclass - clazz = clazz.getSuperclass(); } - return null; + // no meta-annotation present at this class - traverse to superclass + clazz = clazz.getSuperclass(); } + return null; + } /** @@ -107,19 +122,22 @@ public class AnnotationUtil { * annotations have already been visited. */ @SuppressWarnings("unchecked") - private static A findAnnotation(AnnotatedElement annotatedElement, Class annotationType, Set visited) { + private static A findAnnotation(Annotation[] anns, Class annotationType, Set visited) { + - Annotation[] anns = annotatedElement.getAnnotations(); // directly annotatated or inherited - for (Annotation ann : anns) { - if (ann.annotationType() == annotationType) { - return (A) ann; - } - } for (Annotation ann : anns) { if (!isInJavaLangAnnotationPackage(ann) && visited.add(ann)) { - A annotation = findAnnotation(ann.annotationType(), annotationType, visited); - if (annotation != null) { - return annotation; + Annotation[] metaAnns = ann.annotationType().getAnnotations(); + for (Annotation metaAnn : metaAnns) { + if (metaAnn.annotationType() == annotationType) { + return (A) metaAnn; + } + } + if (metaAnns.length > 0) { + A annotation = findAnnotation(metaAnns, annotationType, visited); + if (annotation != null) { + return annotation; + } } } } diff --git a/src/main/java/io/ebeaninternal/server/cache/DefaultCacheHolder.java b/src/main/java/io/ebeaninternal/server/cache/DefaultCacheHolder.java index eb87d393b..1d4f43b74 100644 --- a/src/main/java/io/ebeaninternal/server/cache/DefaultCacheHolder.java +++ b/src/main/java/io/ebeaninternal/server/cache/DefaultCacheHolder.java @@ -7,7 +7,7 @@ import io.ebean.cache.ServerCacheFactory; import io.ebean.cache.ServerCacheOptions; import io.ebean.cache.ServerCacheType; import io.ebean.config.CurrentTenantProvider; -import io.ebeaninternal.server.deploy.parse.AnnotationBase; +import io.ebean.util.AnnotationUtil; import java.util.concurrent.ConcurrentHashMap; @@ -83,7 +83,7 @@ class DefaultCacheHolder { } private ServerCacheOptions getQueryOptions(Class cls) { - CacheQueryTuning tuning = AnnotationBase.findAnnotation(cls, CacheQueryTuning.class); + CacheQueryTuning tuning = AnnotationUtil.findAnnotation(cls, CacheQueryTuning.class); if (tuning != null) { return new ServerCacheOptions(tuning).applyDefaults(queryDefault); } diff --git a/src/main/java/io/ebeaninternal/server/changelog/DefaultChangeLogRegister.java b/src/main/java/io/ebeaninternal/server/changelog/DefaultChangeLogRegister.java index bd9b4210c..eb86e1c11 100644 --- a/src/main/java/io/ebeaninternal/server/changelog/DefaultChangeLogRegister.java +++ b/src/main/java/io/ebeaninternal/server/changelog/DefaultChangeLogRegister.java @@ -5,7 +5,7 @@ import io.ebean.annotation.ChangeLogInsertMode; import io.ebean.event.BeanPersistRequest; import io.ebean.event.changelog.ChangeLogFilter; import io.ebean.event.changelog.ChangeLogRegister; -import io.ebeaninternal.server.deploy.parse.AnnotationBase; +import io.ebean.util.AnnotationUtil; import java.util.Collections; import java.util.HashSet; @@ -52,7 +52,7 @@ public class DefaultChangeLogRegister implements ChangeLogRegister { * Find and return the ChangeLog annotation in the inheritance hierarchy. */ private ChangeLog getChangeLog(Class beanType) { - return AnnotationBase.findAnnotationRecursive(beanType, ChangeLog.class); + return AnnotationUtil.findAnnotationRecursive(beanType, ChangeLog.class); } /** diff --git a/src/main/java/io/ebeaninternal/server/core/bootup/BootupClasses.java b/src/main/java/io/ebeaninternal/server/core/bootup/BootupClasses.java index 5e57ed8e7..46bd95e21 100644 --- a/src/main/java/io/ebeaninternal/server/core/bootup/BootupClasses.java +++ b/src/main/java/io/ebeaninternal/server/core/bootup/BootupClasses.java @@ -16,7 +16,7 @@ import io.ebean.event.changelog.ChangeLogPrepare; import io.ebean.event.changelog.ChangeLogRegister; import io.ebean.event.readaudit.ReadAuditLogger; import io.ebean.event.readaudit.ReadAuditPrepare; -import io.ebeaninternal.server.deploy.parse.AnnotationBase; +import io.ebean.util.AnnotationUtil; import io.ebeaninternal.server.type.ScalarType; import org.avaje.classpath.scanner.ClassFilter; import org.slf4j.Logger; @@ -475,6 +475,6 @@ public class BootupClasses implements ClassFilter { * Returns true if this class has the annotation (or meta annotation). Does not search recursively. */ private boolean has(Class cls, Class ann) { - return AnnotationBase.findAnnotation(cls, ann) != null; + return AnnotationUtil.findAnnotation(cls, ann) != null; } } diff --git a/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorManager.java b/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorManager.java index 9e5a5b528..62d8551e9 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorManager.java +++ b/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorManager.java @@ -19,6 +19,7 @@ import io.ebean.event.changelog.ChangeLogListener; import io.ebean.event.changelog.ChangeLogPrepare; import io.ebean.event.changelog.ChangeLogRegister; import io.ebean.plugin.BeanType; +import io.ebean.util.AnnotationUtil; import io.ebeaninternal.api.ConcurrencyMode; import io.ebeaninternal.api.SpiEbeanServer; import io.ebeaninternal.api.TransactionEventTable; @@ -38,7 +39,6 @@ import io.ebeaninternal.server.deploy.meta.DeployBeanPropertyAssocMany; import io.ebeaninternal.server.deploy.meta.DeployBeanPropertyAssocOne; import io.ebeaninternal.server.deploy.meta.DeployBeanTable; import io.ebeaninternal.server.deploy.meta.DeployTableJoin; -import io.ebeaninternal.server.deploy.parse.AnnotationBase; import io.ebeaninternal.server.deploy.parse.DeployBeanInfo; import io.ebeaninternal.server.deploy.parse.DeployCreateProperties; import io.ebeaninternal.server.deploy.parse.DeployInherit; @@ -1460,7 +1460,7 @@ public class BeanDescriptorManager implements BeanDescriptorMap { */ private boolean isMappedSuperWithNoProperties(Class beanClass) { // do not search recursive here - MappedSuperclass annotation = AnnotationBase.findAnnotation(beanClass, MappedSuperclass.class); + MappedSuperclass annotation = AnnotationUtil.findAnnotation(beanClass, MappedSuperclass.class); if (annotation == null) { return false; } diff --git a/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java b/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java index cc03bb9fd..01d627b44 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java +++ b/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java @@ -14,12 +14,12 @@ import io.ebean.config.ScalarTypeConverter; import io.ebean.config.dbplatform.DbDefaultValue; import io.ebean.config.dbplatform.DbEncrypt; import io.ebean.config.dbplatform.DbEncryptFunction; +import io.ebean.util.AnnotationUtil; import io.ebeaninternal.server.core.InternString; import io.ebeaninternal.server.deploy.BeanProperty; import io.ebeaninternal.server.deploy.DeployDocPropertyOptions; import io.ebeaninternal.server.deploy.DbMigrationInfo; import io.ebeaninternal.server.deploy.generatedproperty.GeneratedProperty; -import io.ebeaninternal.server.deploy.parse.AnnotationBase; import io.ebeaninternal.server.el.ElPropertyValue; import io.ebeaninternal.server.properties.BeanPropertyGetter; import io.ebeaninternal.server.properties.BeanPropertySetter; @@ -255,29 +255,29 @@ public class DeployBeanProperty { if (field == null) { return 0; } - if (AnnotationBase.findAnnotation(field, Id.class) != null) { + if (AnnotationUtil.findAnnotation(field, Id.class) != null) { return ID_ORDER; - } else if (AnnotationBase.findAnnotation(field, EmbeddedId.class) != null) { + } else if (AnnotationUtil.findAnnotation(field, EmbeddedId.class) != null) { return ID_ORDER; } else if (undirectionalShadow) { return UNIDIRECTIONAL_ORDER; } else if (isAuditProperty()) { return AUDITCOLUMN_ORDER; - } else if (AnnotationBase.findAnnotation(field, Version.class) != null) { + } else if (AnnotationUtil.findAnnotation(field, Version.class) != null) { return VERSIONCOLUMN_ORDER; - } else if (AnnotationBase.findAnnotation(field, SoftDelete.class) != null) { + } else if (AnnotationUtil.findAnnotation(field, SoftDelete.class) != null) { return VERSIONCOLUMN_ORDER; } return 0; } private boolean isAuditProperty() { - return (AnnotationBase.findAnnotation(field, WhenCreated.class) != null - || AnnotationBase.findAnnotation(field, WhenModified.class) != null - || AnnotationBase.findAnnotation(field, WhoModified.class) != null - || AnnotationBase.findAnnotation(field, WhoCreated.class) != null - || AnnotationBase.findAnnotation(field, UpdatedTimestamp.class) != null - || AnnotationBase.findAnnotation(field, CreatedTimestamp.class) != null); + return (AnnotationUtil.findAnnotation(field, WhenCreated.class) != null + || AnnotationUtil.findAnnotation(field, WhenModified.class) != null + || AnnotationUtil.findAnnotation(field, WhoModified.class) != null + || AnnotationUtil.findAnnotation(field, WhoCreated.class) != null + || AnnotationUtil.findAnnotation(field, UpdatedTimestamp.class) != null + || AnnotationUtil.findAnnotation(field, CreatedTimestamp.class) != null); } public String getFullBeanName() { diff --git a/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationBase.java b/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationBase.java index eacae2cd3..f86cac704 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationBase.java +++ b/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationBase.java @@ -9,7 +9,6 @@ import io.ebeaninternal.server.deploy.meta.DeployBeanProperty; import javax.persistence.JoinColumn; import javax.persistence.JoinColumns; import java.lang.annotation.Annotation; -import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Set; @@ -83,12 +82,12 @@ public abstract class AnnotationBase { T a = null; Field field = prop.getField(); if (field != null) { - a = findAnnotation(field, annClass); + a = AnnotationUtil.findAnnotation(field, annClass); } if (a == null) { Method method = prop.getReadMethod(); if (method != null) { - a = findAnnotation(method, annClass); + a = AnnotationUtil.findAnnotation(method, annClass); } } return a; @@ -102,14 +101,14 @@ public abstract class AnnotationBase { Set ret = null; Field field = prop.getField(); if (field != null) { - ret = findAnnotations(field, annClass); + ret = AnnotationUtil.findAnnotations(field, annClass); } Method method = prop.getReadMethod(); if (method != null) { if (ret != null) { - ret.addAll(findAnnotations(method, annClass)); + ret.addAll(AnnotationUtil.findAnnotations(method, annClass)); } else { - ret = findAnnotations(method, annClass); + ret = AnnotationUtil.findAnnotations(method, annClass); } } return ret; @@ -125,59 +124,9 @@ public abstract class AnnotationBase { protected T find(DeployBeanProperty prop, Class annClass) { T a = get(prop, annClass); if (a == null) { - a = findAnnotation(prop.getOwningType(), annClass, platform); + a = AnnotationUtil.findAnnotation(prop.getOwningType(), annClass, platform); } return a; } - - /** - * Find a single {@link Annotation} of {@code annotationType} on the supplied {@link AnnotatedElement}. - *

- * Meta-annotations will be searched if the annotation is not directly present on the supplied element. - *

- * Warning: this method operates generically on annotated elements. In other words, this method - * does not execute specialized search algorithms for classes or methods. It only traverses through Annotations! - * It also does not filter out platform dependent annotations! - */ - public static A findAnnotation(AnnotatedElement annotatedElement, Class annotationType) { - return AnnotationUtil.findAnnotation(annotatedElement, annotationType); - } - - /** - * Find a single {@link Annotation} of {@code annotationType} on the supplied class. - *

Meta-annotations will be searched if the annotation is not directly present on - * the supplied element. - *

Note: this method searches for annotations at class & superClass(es)! - */ - public static A findAnnotationRecursive(Class clazz, Class annotationType) { - return AnnotationUtil.findAnnotationRecursive(clazz, annotationType); - } - - /** - * Finds the first annotation of a type for this platform. (if annotation is platform specific, otherwise first - * found annotation is returned) - */ - public static A findAnnotation(AnnotatedElement annotatedElement, Class annotationType, Platform platform) { - return AnnotationUtil.findAnnotation(annotatedElement, annotationType, platform); - } - - /** - * Finds all annotations recusively for a class and its superclasses. - */ - public static Set findAnnotationsRecursive(Class clazz, Class annotationType) { - return AnnotationUtil.findAnnotationsRecursive(clazz, annotationType); - } - - /** - * Find all {@link Annotation}s of {@code annotationType} on the supplied {@link AnnotatedElement}. - *

- * Meta-annotations will be searched if the annotation is not directly present on the supplied element. - *

- * Warning: this method operates generically on annotated elements. In other words, this method - * does not execute specialized search algorithms for classes or methods. It only traverses through Annotations! - */ - public static Set findAnnotations(AnnotatedElement annotatedElement, Class annotationType) { - return AnnotationUtil.findAnnotations(annotatedElement, annotationType); - } - + } diff --git a/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationClass.java b/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationClass.java index fbab35764..aa4902a9b 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationClass.java +++ b/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationClass.java @@ -11,6 +11,7 @@ import io.ebean.annotation.ReadAudit; import io.ebean.annotation.UpdateMode; import io.ebean.annotation.View; import io.ebean.config.TableName; +import io.ebean.util.AnnotationUtil; import io.ebeaninternal.server.deploy.BeanDescriptor.EntityType; import io.ebeaninternal.server.deploy.IndexDefinition; import io.ebeaninternal.server.deploy.InheritInfo; @@ -67,7 +68,7 @@ public class AnnotationClass extends AnnotationParser { public void parseAttributeOverride() { Class cls = descriptor.getBeanType(); - AttributeOverride override = AnnotationBase.findAnnotationRecursive(cls, AttributeOverride.class); + AttributeOverride override = AnnotationUtil.findAnnotationRecursive(cls, AttributeOverride.class); if (override != null) { String propertyName = override.name(); Column column = override.column(); @@ -109,14 +110,14 @@ public class AnnotationClass extends AnnotationParser { private void read(Class cls) { // maybe doc store only so check for this before @Entity - DocStore docStore = AnnotationBase.findAnnotationRecursive(cls, DocStore.class); + DocStore docStore = AnnotationUtil.findAnnotationRecursive(cls, DocStore.class); if (docStore != null) { descriptor.readDocStore(docStore); descriptor.setEntityType(EntityType.DOC); descriptor.setName(cls.getSimpleName()); } - Entity entity = AnnotationBase.findAnnotationRecursive(cls, Entity.class); + Entity entity = AnnotationUtil.findAnnotationRecursive(cls, Entity.class); if (entity != null) { descriptor.setEntityType(EntityType.ORM); if (entity.name().isEmpty()) { @@ -126,27 +127,27 @@ public class AnnotationClass extends AnnotationParser { } } - Embeddable embeddable = AnnotationBase.findAnnotationRecursive(cls, Embeddable.class); + Embeddable embeddable = AnnotationUtil.findAnnotationRecursive(cls, Embeddable.class); if (embeddable != null) { descriptor.setEntityType(EntityType.EMBEDDED); descriptor.setName("Embeddable:" + cls.getSimpleName()); } - Set indices = AnnotationBase.findAnnotationsRecursive(cls, Index.class); + Set indices = AnnotationUtil.findAnnotationsRecursive(cls, Index.class); for (Index index : indices) { descriptor.addIndex(new IndexDefinition(index.columnNames(), index.name(), index.unique())); } - UniqueConstraint uc = AnnotationBase.findAnnotationRecursive(cls, UniqueConstraint.class); + UniqueConstraint uc = AnnotationUtil.findAnnotationRecursive(cls, UniqueConstraint.class); if (uc != null) { descriptor.addIndex(new IndexDefinition(uc.columnNames())); } - View view = AnnotationBase.findAnnotationRecursive(cls, View.class); + View view = AnnotationUtil.findAnnotationRecursive(cls, View.class); if (view != null) { descriptor.setView(view.name(), view.dependentTables()); } - Table table = AnnotationBase.findAnnotationRecursive(cls, Table.class); + Table table = AnnotationUtil.findAnnotationRecursive(cls, Table.class); if (table != null) { UniqueConstraint[] uniqueConstraints = table.uniqueConstraints(); for (UniqueConstraint c : uniqueConstraints) { @@ -154,42 +155,42 @@ public class AnnotationClass extends AnnotationParser { } } - Draftable draftable = AnnotationBase.findAnnotationRecursive(cls, Draftable.class); + Draftable draftable = AnnotationUtil.findAnnotationRecursive(cls, Draftable.class); if (draftable != null) { descriptor.setDraftable(); } - DraftableElement draftableElement = AnnotationBase.findAnnotationRecursive(cls, DraftableElement.class); + DraftableElement draftableElement = AnnotationUtil.findAnnotationRecursive(cls, DraftableElement.class); if (draftableElement != null) { descriptor.setDraftableElement(); } - ReadAudit readAudit = AnnotationBase.findAnnotationRecursive(cls, ReadAudit.class); + ReadAudit readAudit = AnnotationUtil.findAnnotationRecursive(cls, ReadAudit.class); if (readAudit != null) { descriptor.setReadAuditing(); } - History history = AnnotationBase.findAnnotationRecursive(cls, History.class); + History history = AnnotationUtil.findAnnotationRecursive(cls, History.class); if (history != null) { descriptor.setHistorySupport(); } - DbComment comment = AnnotationBase.findAnnotationRecursive(cls, DbComment.class); + DbComment comment = AnnotationUtil.findAnnotationRecursive(cls, DbComment.class); if (comment != null) { descriptor.setDbComment(comment.value()); } - UpdateMode updateMode = AnnotationBase.findAnnotationRecursive(cls, UpdateMode.class); + UpdateMode updateMode = AnnotationUtil.findAnnotationRecursive(cls, UpdateMode.class); if (updateMode != null) { descriptor.setUpdateChangesOnly(updateMode.updateChangesOnly()); } - Cache cache = AnnotationBase.findAnnotationRecursive(cls, Cache.class); + Cache cache = AnnotationUtil.findAnnotationRecursive(cls, Cache.class); if (cache != null && !disableL2Cache) { descriptor.setCache(cache); } - Set namedQueries = AnnotationBase.findAnnotationsRecursive(cls, NamedQuery.class); + Set namedQueries = AnnotationUtil.findAnnotationsRecursive(cls, NamedQuery.class); for (NamedQuery namedQuery : namedQueries) { descriptor.addNamedQuery(namedQuery.name(), namedQuery.query()); } diff --git a/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationSql.java b/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationSql.java index aa0205632..d488e933c 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationSql.java +++ b/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationSql.java @@ -1,6 +1,7 @@ package io.ebeaninternal.server.deploy.parse; import io.ebean.annotation.Sql; +import io.ebean.util.AnnotationUtil; import io.ebeaninternal.server.deploy.BeanDescriptor; /** @@ -15,7 +16,7 @@ public class AnnotationSql extends AnnotationParser { @Override public void parse() { Class cls = descriptor.getBeanType(); - Sql sql = AnnotationBase.findAnnotationRecursive(cls, Sql.class); + Sql sql = AnnotationUtil.findAnnotationRecursive(cls, Sql.class); if (sql != null) { descriptor.setEntityType(BeanDescriptor.EntityType.SQL); } diff --git a/src/main/java/io/ebeaninternal/server/deploy/parse/DeployCreateProperties.java b/src/main/java/io/ebeaninternal/server/deploy/parse/DeployCreateProperties.java index c91864434..e1a96c042 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/parse/DeployCreateProperties.java +++ b/src/main/java/io/ebeaninternal/server/deploy/parse/DeployCreateProperties.java @@ -5,6 +5,7 @@ import io.ebean.annotation.DbHstore; import io.ebean.annotation.DbJson; import io.ebean.annotation.DbJsonB; import io.ebean.annotation.UnmappedJson; +import io.ebean.util.AnnotationUtil; import io.ebeaninternal.server.deploy.DetermineManyType; import io.ebeaninternal.server.deploy.ManyType; import io.ebeaninternal.server.deploy.meta.DeployBeanDescriptor; @@ -221,7 +222,7 @@ public class DeployCreateProperties { Class propertyType = field.getType(); - ManyToOne manyToOne = AnnotationBase.findAnnotation(field, ManyToOne.class); + ManyToOne manyToOne = AnnotationUtil.findAnnotation(field, ManyToOne.class); if (manyToOne != null) { Class tt = manyToOne.targetEntity(); if (!tt.equals(void.class)) { @@ -238,7 +239,7 @@ public class DeployCreateProperties { // List, Set or Map based object Class targetType = determineTargetType(field); if (targetType == null) { - Transient transAnnotation = AnnotationBase.findAnnotation(field, Transient.class); + Transient transAnnotation = AnnotationUtil.findAnnotation(field, Transient.class); if (transAnnotation != null) { // not supporting this field (generic type used) return null; @@ -274,16 +275,16 @@ public class DeployCreateProperties { * Return true if the field has one of the special mappings. */ private boolean isSpecialScalarType(Field field) { - return (AnnotationBase.findAnnotation(field, DbJson.class) != null) - || (AnnotationBase.findAnnotation(field, DbJsonB.class) != null) - || (AnnotationBase.findAnnotation(field, DbArray.class) != null) - || (AnnotationBase.findAnnotation(field, DbHstore.class) != null) - || (AnnotationBase.findAnnotation(field, UnmappedJson.class) != null); + return (AnnotationUtil.findAnnotation(field, DbJson.class) != null) + || (AnnotationUtil.findAnnotation(field, DbJsonB.class) != null) + || (AnnotationUtil.findAnnotation(field, DbArray.class) != null) + || (AnnotationUtil.findAnnotation(field, DbHstore.class) != null) + || (AnnotationUtil.findAnnotation(field, UnmappedJson.class) != null); } private boolean isTransientField(Field field) { - Transient t = AnnotationBase.findAnnotation(field, Transient.class); + Transient t = AnnotationUtil.findAnnotation(field, Transient.class); return (t != null); } diff --git a/src/main/java/io/ebeaninternal/server/deploy/parse/DeployInherit.java b/src/main/java/io/ebeaninternal/server/deploy/parse/DeployInherit.java index 30fab9aca..f7fe0d56c 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/parse/DeployInherit.java +++ b/src/main/java/io/ebeaninternal/server/deploy/parse/DeployInherit.java @@ -1,5 +1,6 @@ package io.ebeaninternal.server.deploy.parse; +import io.ebean.util.AnnotationUtil; import io.ebeaninternal.server.core.bootup.BootupClasses; import io.ebeaninternal.server.deploy.InheritInfo; import io.ebeaninternal.server.deploy.meta.DeployBeanDescriptor; @@ -109,11 +110,11 @@ public class DeployInherit { info.setParent(parent); } - Inheritance ia = AnnotationBase.findAnnotationRecursive(cls, Inheritance.class); + Inheritance ia = AnnotationUtil.findAnnotationRecursive(cls, Inheritance.class); if (ia != null) { ia.strategy(); } - DiscriminatorColumn da = AnnotationBase.findAnnotationRecursive(cls, DiscriminatorColumn.class); + DiscriminatorColumn da = AnnotationUtil.findAnnotationRecursive(cls, DiscriminatorColumn.class); if (da != null) { // lowercase the discriminator column for RawSql and JSON info.setColumnName(da.name().toLowerCase()); @@ -122,7 +123,7 @@ public class DeployInherit { info.setColumnDefn(da.columnDefinition()); } - DiscriminatorValue dv = AnnotationBase.findAnnotationRecursive(cls, DiscriminatorValue.class); + DiscriminatorValue dv = AnnotationUtil.findAnnotation(cls, DiscriminatorValue.class); // do not search recursive if (dv != null) { info.setDiscriminatorValue(dv.value()); } @@ -144,7 +145,7 @@ public class DeployInherit { if (cls.equals(Object.class)) { return false; } - Annotation a = AnnotationBase.findAnnotationRecursive(cls, Inheritance.class); + Annotation a = AnnotationUtil.findAnnotationRecursive(cls, Inheritance.class); if (a != null) { return true; } diff --git a/src/main/java/io/ebeaninternal/server/type/DefaultTypeManager.java b/src/main/java/io/ebeaninternal/server/type/DefaultTypeManager.java index e99b9eefd..bbb81b501 100644 --- a/src/main/java/io/ebeaninternal/server/type/DefaultTypeManager.java +++ b/src/main/java/io/ebeaninternal/server/type/DefaultTypeManager.java @@ -10,10 +10,10 @@ import io.ebean.config.ScalarTypeConverter; import io.ebean.config.ServerConfig; import io.ebean.config.dbplatform.DatabasePlatform; import io.ebean.config.dbplatform.DbPlatformType; +import io.ebean.util.AnnotationUtil; import io.ebeaninternal.dbmigration.DbOffline; import io.ebeaninternal.api.ExtraTypeFactory; import io.ebeaninternal.server.core.bootup.BootupClasses; -import io.ebeaninternal.server.deploy.parse.AnnotationBase; import io.ebeanservice.docstore.api.mapping.DocPropertyType; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -544,7 +544,7 @@ public final class DefaultTypeManager implements TypeManager { Field[] fields = enumType.getDeclaredFields(); for (Field field : fields) { - EnumValue enumValue = AnnotationBase.findAnnotation(field, EnumValue.class); + EnumValue enumValue = AnnotationUtil.findAnnotation(field, EnumValue.class); if (enumValue != null) { nameValueMap.put(field.getName(), enumValue.value()); if (integerType && !isIntegerType(enumValue.value())) { @@ -575,7 +575,7 @@ public final class DefaultTypeManager implements TypeManager { Method[] methods = enumType.getMethods(); for (Method method : methods) { - DbEnumValue dbValue = AnnotationBase.findAnnotation(method, DbEnumValue.class); + DbEnumValue dbValue = AnnotationUtil.findAnnotation(method, DbEnumValue.class); if (dbValue != null) { boolean integerValues = DbEnumType.INTEGER == dbValue.storage(); return createEnumScalarTypeDbValue(enumType, method, integerValues); diff --git a/src/test/java/org/tests/basic/TestAnnotationBase.java b/src/test/java/org/tests/basic/TestAnnotationBase.java index 985a70715..e40c36cd9 100644 --- a/src/test/java/org/tests/basic/TestAnnotationBase.java +++ b/src/test/java/org/tests/basic/TestAnnotationBase.java @@ -2,10 +2,10 @@ package org.tests.basic; import io.ebean.BaseTestCase; import io.ebean.annotation.Where; +import io.ebean.util.AnnotationUtil; import io.ebean.annotation.Platform; import io.ebeaninternal.server.deploy.BeanDescriptor; import io.ebeaninternal.server.deploy.BeanProperty; -import io.ebeaninternal.server.deploy.parse.AnnotationBase; import org.tests.model.basic.ValidationGroupSomething; import org.junit.Test; @@ -150,41 +150,41 @@ public class TestAnnotationBase extends BaseTestCase { Field fld = TestAnnotationBaseEntity.class.getDeclaredField("direct"); String s; - s = AnnotationBase.findAnnotation(fld, Where.class, Platform.MYSQL).clause(); + s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.MYSQL).clause(); assertEquals("SELECT 'mysql' from 1", s); - s = AnnotationBase.findAnnotation(fld, Where.class, Platform.H2).clause(); + s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.H2).clause(); assertEquals("SELECT 'h2' from 1", s); - s = AnnotationBase.findAnnotation(fld, Where.class, Platform.POSTGRES).clause(); + s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.POSTGRES).clause(); assertEquals("SELECT 'other' from 1", s); // meta fld = TestAnnotationBaseEntity.class.getDeclaredField("meta"); - s = AnnotationBase.findAnnotation(fld, Where.class, Platform.MYSQL).clause(); + s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.MYSQL).clause(); assertEquals("SELECT 'mysql' from 1", s); - s = AnnotationBase.findAnnotation(fld, Where.class, Platform.H2).clause(); + s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.H2).clause(); assertEquals("SELECT 'h2' from 1", s); - s = AnnotationBase.findAnnotation(fld, Where.class, Platform.POSTGRES).clause(); + s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.POSTGRES).clause(); assertEquals("SELECT 'other' from 1", s); // mixed fld = TestAnnotationBaseEntity.class.getDeclaredField("mixed"); - s = AnnotationBase.findAnnotation(fld, Where.class, Platform.MYSQL).clause(); + s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.MYSQL).clause(); assertEquals("SELECT 'mysql' from 1", s); - s = AnnotationBase.findAnnotation(fld, Where.class, Platform.H2).clause(); + s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.H2).clause(); assertEquals("SELECT 'h2' from 1", s); - s = AnnotationBase.findAnnotation(fld, Where.class, Platform.POSTGRES).clause(); + s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.POSTGRES).clause(); assertEquals("SELECT 'other' from 1", s); - s = AnnotationBase.findAnnotation(fld, Where.class, Platform.ORACLE).clause(); + s = AnnotationUtil.findAnnotation(fld, Where.class, Platform.ORACLE).clause(); assertEquals("SELECT 'oracle' from 1", s); }