You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bval.apache.org by mb...@apache.org on 2010/09/10 16:26:57 UTC
svn commit: r995805 - in
/incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util:
IndexedAccess.java KeyedAccess.java
Author: mbenson
Date: Fri Sep 10 14:26:56 2010
New Revision: 995805
URL: http://svn.apache.org/viewvc?rev=995805&view=rev
Log:
extract javaType calculation to a static method
Modified:
incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java
incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/KeyedAccess.java
Modified: incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java?rev=995805&r1=995804&r2=995805&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java (original)
+++ incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java Fri Sep 10 14:26:56 2010
@@ -25,17 +25,34 @@ import java.util.List;
import org.apache.commons.lang3.reflect.TypeUtils;
/**
- * {@link AccessStrategy} to get an indexed member of an {@link Iterable} or array object.
+ * {@link AccessStrategy} to get an indexed member of an {@link Iterable} or
+ * array object.
*/
public class IndexedAccess extends AccessStrategy {
- private static final TypeVariable<?> ITERABLE_TYPE = Iterable.class
- .getTypeParameters()[0];
+ private static final TypeVariable<?> ITERABLE_TYPE = Iterable.class.getTypeParameters()[0];
+
+ /**
+ * Get the Java element type of a particular container type.
+ *
+ * @param containerType
+ * @return Type
+ */
+ public static Type getJavaElementType(Type containerType) {
+ if (TypeUtils.isArrayType(containerType)) {
+ return TypeUtils.getArrayComponentType(containerType);
+ }
+ if (TypeUtils.isAssignable(containerType, Iterable.class)) {
+ return TypeUtils.getTypeArguments(containerType, Iterable.class).get(ITERABLE_TYPE);
+ }
+ return Object.class;
+ }
private Type containerType;
private int index;
/**
* Create a new IndexedAccessStrategy instance.
+ *
* @param containerType
* @param index
*/
@@ -83,14 +100,7 @@ public class IndexedAccess extends Acces
*/
@Override
public Type getJavaType() {
- if (TypeUtils.isArrayType(containerType)) {
- return TypeUtils.getArrayComponentType(containerType);
- }
- if (TypeUtils.isAssignable(containerType, Iterable.class)) {
- return TypeUtils.getTypeArguments(containerType, Iterable.class)
- .get(ITERABLE_TYPE);
- }
- return Object.class;
+ return getJavaElementType(containerType);
}
/**
Modified: incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/KeyedAccess.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/KeyedAccess.java?rev=995805&r1=995804&r2=995805&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/KeyedAccess.java (original)
+++ incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/KeyedAccess.java Fri Sep 10 14:26:56 2010
@@ -24,21 +24,34 @@ import java.util.Map;
import org.apache.commons.lang3.reflect.TypeUtils;
/**
- * {@link AccessStrategy} to get a keyed value from a {@link Map}.
- * Contains special handling when a string key is used against a container type
- * whose key parameter is not assignable from {@link String}: against a map
- * whose key type is an enum class, it will be interpreted as a named enum constant;
- * other key types will be compared via {@link Object#toString()}.
+ * {@link AccessStrategy} to get a keyed value from a {@link Map}. Contains
+ * special handling when a string key is used against a container type whose key
+ * parameter is not assignable from {@link String}: against a map whose key type
+ * is an enum class, it will be interpreted as a named enum constant; other key
+ * types will be compared via {@link Object#toString()}.
*/
public class KeyedAccess extends AccessStrategy {
- private static final TypeVariable<?>[] MAP_TYPEVARS = Map.class
- .getTypeParameters();
+ private static final TypeVariable<?>[] MAP_TYPEVARS = Map.class.getTypeParameters();
+
+ /**
+ * Get the Java element type of a particular container type.
+ *
+ * @param containerType
+ * @return Type
+ */
+ public static Type getJavaElementType(Type containerType) {
+ if (TypeUtils.isAssignable(containerType, Map.class)) {
+ return TypeUtils.getTypeArguments(containerType, Map.class).get(MAP_TYPEVARS[1]);
+ }
+ return Object.class;
+ }
private Type containerType;
private Object key;
/**
* Create a new KeyedAccess instance.
+ *
* @param containerType
* @param key
*/
@@ -55,16 +68,14 @@ public class KeyedAccess extends AccessS
public Object get(Object instance) {
if (instance instanceof Map<?, ?>) {
Map<?, ?> map = (Map<?, ?>) instance;
- Map<TypeVariable<?>, Type> typeArguments = TypeUtils
- .getTypeArguments(containerType, Map.class);
+ Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Map.class);
Type keyType = typeArguments.get(MAP_TYPEVARS[0]);
if (key == null || TypeUtils.isInstance(key, keyType)) {
return map.get(key);
}
if (key instanceof String) {
String name = (String) key;
- Class<?> rawKeyType = TypeUtils.getRawType(keyType,
- containerType);
+ Class<?> rawKeyType = TypeUtils.getRawType(keyType, containerType);
if (rawKeyType.isEnum()) {
@SuppressWarnings("unchecked")
final Object result = map.get(Enum.valueOf((Class<? extends Enum>) rawKeyType, name));
@@ -93,10 +104,7 @@ public class KeyedAccess extends AccessS
*/
@Override
public Type getJavaType() {
- if (TypeUtils.isAssignable(containerType, Map.class)) {
- return TypeUtils.getTypeArguments(containerType, Map.class).get(MAP_TYPEVARS[1]);
- }
- return Object.class;
+ return getJavaElementType(containerType);
}
/**