You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2017/02/26 23:05:08 UTC

[4/5] incubator-tamaya git commit: TAMAYA-236: Added Javadoc to ordinal evaluation and added support for a static ORDINAL field.

TAMAYA-236: Added Javadoc to ordinal evaluation and added support for a static ORDINAL field.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/b1e91222
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/b1e91222
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/b1e91222

Branch: refs/heads/master
Commit: b1e912221d08cb37dbd1b73ac66f6ce5a740f9b3
Parents: edaee8e
Author: anatole <an...@apache.org>
Authored: Sun Feb 26 12:36:09 2017 +0100
Committer: anatole <an...@apache.org>
Committed: Mon Feb 27 00:05:00 2017 +0100

----------------------------------------------------------------------
 .../core/internal/PropertySourceComparator.java | 32 ++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b1e91222/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java
----------------------------------------------------------------------
diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java b/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java
index a0006b4..50d3653 100644
--- a/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java
+++ b/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java
@@ -23,8 +23,10 @@ import org.apache.tamaya.spi.PropertyValue;
 
 import javax.annotation.Priority;
 import java.io.Serializable;
+import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.util.Comparator;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -68,6 +70,20 @@ public class PropertySourceComparator implements Comparator<PropertySource>, Ser
         }
     }
 
+    /**
+     * Evaluates an ordinal value from a {@link PropertySource}, Herey the ordinal of type {@code int}
+     * is evaluated as follows:
+     * <ol>
+     *     <li>It evaluates the {@code String} value for {@link PropertySource#TAMAYA_ORDINAL} and tries
+     *     to convert it to an {@code int} value, using {@link Integer#parseInt(String)}.</li>
+     *     <li>It tries to find and evaluate a method {@code int getOrdinal()}</li>.
+     *     <li>It tries to find and evaluate a static field {@code int ORDINAL}.</li>
+     *     <li>It tries to find an d evaluate a class level {@link Priority} annotation.</li>
+     *     <li>It uses the default priority ({@code 0}.</li>
+     * </ol>
+     * @param propertySource the property source, not null.
+     * @return the ordinal value to compare the property source.
+     */
     public static int getOrdinal(PropertySource propertySource) {
         PropertyValue ordinalValue = propertySource.get(PropertySource.TAMAYA_ORDINAL);
         if(ordinalValue!=null){
@@ -84,11 +100,23 @@ public class PropertySourceComparator implements Comparator<PropertySource>, Ser
                 try {
                     return (int)method.invoke(propertySource);
                 } catch (Exception e) {
-                    LOG.log(Level.FINEST, "Error calling int getOrdinal() on " + propertySource.getName(), e);
+                    LOG.log(Level.FINEST, "Error calling 'int getOrdinal()' on " + propertySource.getName(), e);
                 }
             }
         } catch (NoSuchMethodException e) {
-            LOG.finest("No int getOrdinal() method found in " + propertySource.getName());
+            LOG.finest("No 'int getOrdinal()' method found in " + propertySource.getName());
+        }
+        try {
+            Field field = propertySource.getClass().getField("ORDINAL");
+            if(int.class.equals(field.getType()) && Modifier.isStatic(field.getModifiers())){
+                try {
+                    return (int)field.get(propertySource);
+                } catch (Exception e) {
+                    LOG.log(Level.FINEST, "Error evaluating 'int ORDINAL' on " + propertySource.getName(), e);
+                }
+            }
+        } catch (NoSuchFieldException e) {
+            LOG.finest("No 'int ORDINAL' field found in " + propertySource.getName());
         }
         Priority prio = propertySource.getClass().getAnnotation(Priority.class);
         if(prio!=null){