tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject [1/3] incubator-tamaya git commit: Removed unused import. Unified TypeLiteral.
Date Sat, 28 Feb 2015 09:18:25 GMT
Repository: incubator-tamaya
Updated Branches:
  refs/heads/master 54828fa94 -> 0d6859daa


Removed unused import.
Unified TypeLiteral.


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

Branch: refs/heads/master
Commit: a43b91efb29b13bb7437a098ff11b170477012e2
Parents: 54828fa
Author: anatole <anatole@apache.org>
Authored: Fri Feb 27 23:59:11 2015 +0100
Committer: anatole <anatole@apache.org>
Committed: Fri Feb 27 23:59:11 2015 +0100

----------------------------------------------------------------------
 .../java/org/apache/tamaya/Configuration.java   |  1 -
 .../java/org/apache/tamaya/TypeLiteral.java     | 65 ++++++++++++++++----
 2 files changed, 53 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/a43b91ef/java7/api/src/main/java/org/apache/tamaya/Configuration.java
----------------------------------------------------------------------
diff --git a/java7/api/src/main/java/org/apache/tamaya/Configuration.java b/java7/api/src/main/java/org/apache/tamaya/Configuration.java
index c9a012e..3d06f6e 100644
--- a/java7/api/src/main/java/org/apache/tamaya/Configuration.java
+++ b/java7/api/src/main/java/org/apache/tamaya/Configuration.java
@@ -19,7 +19,6 @@
 package org.apache.tamaya;
 
 import java.util.Map;
-import java.util.Objects;
 
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/a43b91ef/java7/api/src/main/java/org/apache/tamaya/TypeLiteral.java
----------------------------------------------------------------------
diff --git a/java7/api/src/main/java/org/apache/tamaya/TypeLiteral.java b/java7/api/src/main/java/org/apache/tamaya/TypeLiteral.java
index 2e1ab31..174bf36 100644
--- a/java7/api/src/main/java/org/apache/tamaya/TypeLiteral.java
+++ b/java7/api/src/main/java/org/apache/tamaya/TypeLiteral.java
@@ -44,16 +44,26 @@ public class TypeLiteral<T> implements Serializable {
         this.type = type;
     }
 
+    /**
+     * Constructor only for directly implemeting a TypeLiteral hereby dynamically implementing
a generic interface.
+     */
     protected TypeLiteral() { }
 
-    public static <L> TypeLiteral<L> of(Type type){
-        return new TypeLiteral<>(type);
-    }
-
-    public static <L> TypeLiteral<L> of(Class<L> type){
+    /**
+     * Creates a new TypeLiteral based on a given type.
+     * @param type the type , not null.
+     * @param <R> the literal generic type.
+     * @return the corresponding TypeLiteral, never null.
+     */
+    public static <R> TypeLiteral<R> of(Type type){
         return new TypeLiteral<>(type);
     }
 
+    /**
+     * Evaluates the subclass of a TypeLiteral instance.
+     * @param clazz the typeliteral class (could be an anonymous class).
+     * @return the subclass implemented by the TypeLiteral.
+     */
     private static Class<?> getTypeLiteralSubclass(Class<?> clazz) {
         Class<?> superclass = clazz.getSuperclass();
         if (superclass.equals(TypeLiteral.class)) {
@@ -65,18 +75,48 @@ public class TypeLiteral<T> implements Serializable {
         }
     }
 
-    private static Type getTypeParameter(Class<?> superclass) {
-        Type type = superclass.getGenericSuperclass();
-        if (type instanceof ParameterizedType) {
-            ParameterizedType parameterizedType = (ParameterizedType) type;
-            if (parameterizedType.getActualTypeArguments().length == 1) {
-                return parameterizedType.getActualTypeArguments()[0];
+    /**
+     * Checks the current implemented generic interfaces and evaluates the given single type
parameter.
+     * @param clazz the class to check, not null.
+     * @param interfaceType the interface type to be checked, not null.
+     * @return the generic type parameter, or null, if it cannot be evaluated.
+     */
+    public static Type getGenericInterfaceTypeParameter(Class<?> clazz, Class<?>
interfaceType) {
+        for(Type type: clazz.getGenericInterfaces()){
+            if(interfaceType!=null && !interfaceType.equals(type)){
+                continue;
+            }
+            if (type instanceof ParameterizedType) {
+                ParameterizedType parameterizedType = (ParameterizedType) type;
+                if (parameterizedType.getActualTypeArguments().length == 1) {
+                    return parameterizedType.getActualTypeArguments()[0];
+                }
             }
         }
         return null;
     }
 
     /**
+     * Method that checks the class's type for a generic interface implementation type.
+     * @param clazz the type class, not null.
+     * @param interfaceType the generic interface to check (there could be multiple ones
implemented by a class).
+     * @return the generic type parameter of the given single type generic interfaceType,
or null.
+     */
+    public static Type getTypeParameter(Class<?> clazz, Class<?> interfaceType)
{
+        Type type = clazz.getGenericSuperclass();
+        if (type instanceof ParameterizedType) {
+            if(interfaceType == null ||type.equals(interfaceType)){
+                ParameterizedType parameterizedType = (ParameterizedType) type;
+                if (parameterizedType.getActualTypeArguments().length == 1) {
+                    return parameterizedType.getActualTypeArguments()[0];
+                }
+            }
+        }
+        return getTypeParameter(clazz, interfaceType);
+    }
+
+    /**
+     * Returns basic Java type.
      * @return the actual type represented by this object
      */
     public final Type getType() {
@@ -85,7 +125,7 @@ public class TypeLiteral<T> implements Serializable {
             if (typeLiteralSubclass == null) {
                 throw new RuntimeException(getClass() + " is not a subclass of TypeLiteral");
             }
-            type = getTypeParameter(typeLiteralSubclass);
+            type = getTypeParameter(typeLiteralSubclass, null);
             if (type == null) {
                 throw new RuntimeException(getClass() + " does not specify the type parameter
T of TypeLiteral<T>");
             }
@@ -94,6 +134,7 @@ public class TypeLiteral<T> implements Serializable {
     }
 
     /**
+     * Get the raw type of the current type.
      * @return the raw type represented by this object
      */
     @SuppressWarnings("unchecked")


Mime
View raw message