From f724982776d94a1529e05bf6a9e07a062454b8a1 Mon Sep 17 00:00:00 2001
From: Rob Bygrave
Date: Wed, 23 Nov 2016 20:33:35 +1300
Subject: [PATCH] #889 - Refactor: Change @Where @Formula to use Platform
(enum) rather than DatabasePlatform (class)
---
.../com/avaje/ebean/annotation/Formula.java | 4 +-
.../com/avaje/ebean/annotation/Where.java | 6 +--
.../server/deploy/parse/AnnotationBase.java | 27 ++++++-------
.../avaje/tests/basic/TestAnnotationBase.java | 39 +++++++++----------
4 files changed, 38 insertions(+), 38 deletions(-)
diff --git a/src/main/java/com/avaje/ebean/annotation/Formula.java b/src/main/java/com/avaje/ebean/annotation/Formula.java
index c51c1f86e..cbd3d4180 100644
--- a/src/main/java/com/avaje/ebean/annotation/Formula.java
+++ b/src/main/java/com/avaje/ebean/annotation/Formula.java
@@ -1,7 +1,7 @@
package com.avaje.ebean.annotation;
import com.avaje.ebean.Query;
-import com.avaje.ebean.config.dbplatform.DatabasePlatform;
+import com.avaje.ebean.config.Platform;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
@@ -104,7 +104,7 @@ public @interface Formula {
*/
String join() default "";
- Class extends DatabasePlatform>[] platforms() default {};
+ Platform[] platforms() default {};
/**
* Repeatable support for {@link Formula}.
diff --git a/src/main/java/com/avaje/ebean/annotation/Where.java b/src/main/java/com/avaje/ebean/annotation/Where.java
index 081d61d5a..aa4bfa874 100644
--- a/src/main/java/com/avaje/ebean/annotation/Where.java
+++ b/src/main/java/com/avaje/ebean/annotation/Where.java
@@ -1,13 +1,13 @@
package com.avaje.ebean.annotation;
+import com.avaje.ebean.config.Platform;
+
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
-import com.avaje.ebean.config.dbplatform.DatabasePlatform;
-
/**
* Add an Literal to add to the where clause when a many property (List, Set or
* Map) is loaded or refreshed.
@@ -54,7 +54,7 @@ public @interface Where {
/**
* The platform where this annotation is active. Default: any platform
*/
- Class extends DatabasePlatform>[] platforms() default {};
+ Platform[] platforms() default {};
/**
* Repeatable support for {@link Where}.
diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationBase.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationBase.java
index 61b23ed74..eef771d30 100644
--- a/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationBase.java
+++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationBase.java
@@ -3,9 +3,12 @@ package com.avaje.ebeaninternal.server.deploy.parse;
import com.avaje.ebean.annotation.Formula;
import com.avaje.ebean.annotation.Where;
import com.avaje.ebean.config.NamingConvention;
+import com.avaje.ebean.config.Platform;
import com.avaje.ebean.config.dbplatform.DatabasePlatform;
import com.avaje.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;
@@ -16,21 +19,20 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
-import javax.persistence.JoinColumn;
-import javax.persistence.JoinColumns;
-
/**
* Provides some base methods for processing deployment annotations.
*/
public abstract class AnnotationBase {
protected final DatabasePlatform databasePlatform;
+ protected final Platform platform;
protected final NamingConvention namingConvention;
protected final DeployUtil util;
protected AnnotationBase(DeployUtil util) {
this.util = util;
this.databasePlatform = util.getDbPlatform();
+ this.platform = Platform.valueOf(databasePlatform.getName().toUpperCase());
this.namingConvention = util.getNamingConvention();
}
@@ -53,7 +55,7 @@ public abstract class AnnotationBase {
*
*
* If a repeatable annotation class is specified and the annotation is platform
- * specific(see {@link #getPlatformMatchingAnnotation(Set, Class)}), then the platform specific
+ * specific(see {@link #getPlatformMatchingAnnotation(Set, Platform)}), then the platform specific
* annotation is returned. Otherwise the first annotation is retured. Note that you must no longer
* handle "java 1.6 repeatable containers" like {@link JoinColumn} / {@link JoinColumns} yourself.
*
@@ -104,7 +106,7 @@ public abstract class AnnotationBase {
protected T find(DeployBeanProperty prop, Class annClass) {
T a = get(prop, annClass);
if (a == null) {
- a = findAnnotation(prop.getOwningType(), annClass, databasePlatform.getClass());
+ a = findAnnotation(prop.getOwningType(), annClass, platform);
}
return a;
}
@@ -172,13 +174,12 @@ public abstract class AnnotationBase {
* 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,
- Class extends DatabasePlatform> databasePlatform) {
+ public static A findAnnotation(AnnotatedElement annotatedElement, Class annotationType, Platform platform) {
if (annotationType == null) {
return null;
}
Set anns = findAnnotations(annotatedElement, annotationType);
- return getPlatformMatchingAnnotation(anns, databasePlatform);
+ return getPlatformMatchingAnnotation(anns, platform);
}
/**
@@ -311,7 +312,7 @@ public abstract class AnnotationBase {
*
* (This mechanism is currently used by {@link Where} and {@link Formula})
*/
- public static T getPlatformMatchingAnnotation(Set anns, Class extends DatabasePlatform> databasePlatform) {
+ public static T getPlatformMatchingAnnotation(Set anns, Platform matchPlatform) {
if (anns.isEmpty()) {
return null;
}
@@ -322,15 +323,15 @@ public abstract class AnnotationBase {
if (getPlatformsMethod == null) {
getPlatformsMethod = ann.getClass().getMethod("platforms");
}
- if (!Class[].class.isAssignableFrom(getPlatformsMethod.getReturnType())) {
+ if (!Platform[].class.isAssignableFrom(getPlatformsMethod.getReturnType())) {
return ann;
}
- Class>[] platforms = (Class[]) getPlatformsMethod.invoke(ann);
+ Platform[] platforms = (Platform[]) getPlatformsMethod.invoke(ann);
if (platforms.length == 0) {
fallback = ann;
} else {
- for (Class> platform : platforms) {
- if (databasePlatform.isAssignableFrom(platform)) {
+ for (Platform platform : platforms) {
+ if (matchPlatform == platform) {
return ann;
}
}
diff --git a/src/test/java/com/avaje/tests/basic/TestAnnotationBase.java b/src/test/java/com/avaje/tests/basic/TestAnnotationBase.java
index 911fa5351..94b46ab79 100644
--- a/src/test/java/com/avaje/tests/basic/TestAnnotationBase.java
+++ b/src/test/java/com/avaje/tests/basic/TestAnnotationBase.java
@@ -3,10 +3,7 @@ package com.avaje.tests.basic;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.annotation.EbeanDDL;
import com.avaje.ebean.annotation.Where;
-import com.avaje.ebean.config.dbplatform.H2Platform;
-import com.avaje.ebean.config.dbplatform.MySqlPlatform;
-import com.avaje.ebean.config.dbplatform.OraclePlatform;
-import com.avaje.ebean.config.dbplatform.PostgresPlatform;
+import com.avaje.ebean.config.Platform;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.parse.AnnotationBase;
@@ -22,15 +19,17 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
public class TestAnnotationBase extends BaseTestCase {
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
- @Where(clause = "SELECT 'mysql' from 1", platforms = MySqlPlatform.class)
- @Where(clause = "SELECT 'h2' from 1", platforms = H2Platform.class)
+ @Where(clause = "SELECT 'mysql' from 1", platforms = Platform.MYSQL)
+ @Where(clause = "SELECT 'h2' from 1", platforms = Platform.H2)
@Where(clause = "SELECT 'other' from 1")
public @interface MetaTest {
@@ -39,8 +38,8 @@ public class TestAnnotationBase extends BaseTestCase {
@Entity
public static class TestAnnotationBaseEntity {
- @Where(clause = "SELECT 'mysql' from 1", platforms = MySqlPlatform.class)
- @Where(clause = "SELECT 'h2' from 1", platforms = H2Platform.class)
+ @Where(clause = "SELECT 'mysql' from 1", platforms = Platform.MYSQL)
+ @Where(clause = "SELECT 'h2' from 1", platforms = Platform.H2)
@Where(clause = "SELECT 'other' from 1")
private String direct;
@@ -49,7 +48,7 @@ public class TestAnnotationBase extends BaseTestCase {
@MetaTest
- @Where(clause = "SELECT 'oracle' from 1", platforms = OraclePlatform.class)
+ @Where(clause = "SELECT 'oracle' from 1", platforms = Platform.ORACLE)
private String mixed;
@Size.List({
@@ -154,41 +153,41 @@ public class TestAnnotationBase extends BaseTestCase {
Field fld = TestAnnotationBaseEntity.class.getDeclaredField("direct");
String s;
- s = AnnotationBase.findAnnotation(fld, Where.class, MySqlPlatform.class).clause();
+ s = AnnotationBase.findAnnotation(fld, Where.class, Platform.MYSQL).clause();
assertEquals("SELECT 'mysql' from 1", s);
- s = AnnotationBase.findAnnotation(fld, Where.class, H2Platform.class).clause();
+ s = AnnotationBase.findAnnotation(fld, Where.class, Platform.H2).clause();
assertEquals("SELECT 'h2' from 1", s);
- s = AnnotationBase.findAnnotation(fld, Where.class, PostgresPlatform.class).clause();
+ s = AnnotationBase.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, MySqlPlatform.class).clause();
+ s = AnnotationBase.findAnnotation(fld, Where.class, Platform.MYSQL).clause();
assertEquals("SELECT 'mysql' from 1", s);
- s = AnnotationBase.findAnnotation(fld, Where.class, H2Platform.class).clause();
+ s = AnnotationBase.findAnnotation(fld, Where.class, Platform.H2).clause();
assertEquals("SELECT 'h2' from 1", s);
- s = AnnotationBase.findAnnotation(fld, Where.class, PostgresPlatform.class).clause();
+ s = AnnotationBase.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, MySqlPlatform.class).clause();
+ s = AnnotationBase.findAnnotation(fld, Where.class, Platform.MYSQL).clause();
assertEquals("SELECT 'mysql' from 1", s);
- s = AnnotationBase.findAnnotation(fld, Where.class, H2Platform.class).clause();
+ s = AnnotationBase.findAnnotation(fld, Where.class, Platform.H2).clause();
assertEquals("SELECT 'h2' from 1", s);
- s = AnnotationBase.findAnnotation(fld, Where.class, PostgresPlatform.class).clause();
+ s = AnnotationBase.findAnnotation(fld, Where.class, Platform.POSTGRES).clause();
assertEquals("SELECT 'other' from 1", s);
- s = AnnotationBase.findAnnotation(fld, Where.class, OraclePlatform.class).clause();
+ s = AnnotationBase.findAnnotation(fld, Where.class, Platform.ORACLE).clause();
assertEquals("SELECT 'oracle' from 1", s);
}