TAMAYA-63: Implemented backport for Java 7.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/ffffdc8e Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/ffffdc8e Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/ffffdc8e Branch: refs/heads/master Commit: ffffdc8eb7df1b0a9d30286cbd312437017efd1f Parents: b9735e0 Author: anatole <[email protected]> Authored: Sat Jan 24 11:17:05 2015 +0100 Committer: anatole <[email protected]> Committed: Sat Jan 24 21:06:15 2015 +0100 ---------------------------------------------------------------------- .../java/org/apache/tamaya/TypeLiteral.java | 137 +++++++++++++++++++ .../services/org.apache.tamaya.Configuration | 19 +++ 2 files changed, 156 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ffffdc8e/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 new file mode 100644 index 0000000..32c32a6 --- /dev/null +++ b/java7/api/src/main/java/org/apache/tamaya/TypeLiteral.java @@ -0,0 +1,137 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one or more +* contributor license agreements. See the NOTICE file distributed with +* this work for additional information regarding copyright ownership. +* The ASF licenses this file to You under the Apache License, Version 2.0 +* (the "License"); you may not use this file except in compliance with +* the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package org.apache.tamaya; + +import java.io.Serializable; +import java.lang.reflect.GenericArrayType; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + + +/** + * <p>Class for instantiation of objects that represent parameterized types + * with current parameters.</p> + * <p> + * <p>An object that represents a parameterized type may be obtained by + * subclassing <tt>TypeLiteral</tt>.</p> + * <p> + * <pre> + * TypeLiteral<List<Integer>> stringListType = new TypeLiteral<List<Integer>>() {}; + * </pre> + * + * @param <T> the type, including all type parameters + */ +public class TypeLiteral<T> implements Serializable { + + private static final long serialVersionUID = 1L; + private Type type; + + protected TypeLiteral(Type type) { + this.type = type; + } + + protected TypeLiteral() { } + + public static <T> TypeLiteral<T> of(Type type){ + return new TypeLiteral<>(type); + } + + private static Class<?> getTypeLiteralSubclass(Class<?> clazz) { + Class<?> superclass = clazz.getSuperclass(); + if (superclass.equals(TypeLiteral.class)) { + return clazz; + } else if (superclass.equals(Object.class)) { + return null; + } else { + return (getTypeLiteralSubclass(superclass)); + } + } + + 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]; + } + } + return null; + } + + /** + * @return the actual type represented by this object + */ + public final Type getType() { + if (type == null) { + Class<?> typeLiteralSubclass = getTypeLiteralSubclass(this.getClass()); + if (typeLiteralSubclass == null) { + throw new RuntimeException(getClass() + " is not a subclass of TypeLiteral"); + } + type = getTypeParameter(typeLiteralSubclass); + if (type == null) { + throw new RuntimeException(getClass() + " does not specify the type parameter T of TypeLiteral<T>"); + } + } + return type; + } + + /** + * @return the raw type represented by this object + */ + @SuppressWarnings("unchecked") + public final Class<T> getRawType() { + Type type = getType(); + if (type instanceof Class) { + return (Class<T>) type; + } else if (type instanceof ParameterizedType) { + return (Class<T>) ((ParameterizedType) type).getRawType(); + } else if (type instanceof GenericArrayType) { + return (Class<T>) Object[].class; + } else { + throw new RuntimeException("Illegal type"); + } + } + + @Override + public boolean equals(Object o) { + if (this == o){ + return true; + } + if (!(o instanceof TypeLiteral)){ + return false; + } + TypeLiteral that = (TypeLiteral) o; + if (type != null ? !type.equals(that.type) : that.type != null){ + return false; + } + return true; + } + + @Override + public int hashCode() { + int result = type != null ? type.hashCode() : 0; + return result; + } + + @Override + public String toString() { + return "TypeLiteral{" + + "type=" + type + + '}'; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ffffdc8e/java7/core/src/main/resources/META-INF/services/org.apache.tamaya.Configuration ---------------------------------------------------------------------- diff --git a/java7/core/src/main/resources/META-INF/services/org.apache.tamaya.Configuration b/java7/core/src/main/resources/META-INF/services/org.apache.tamaya.Configuration new file mode 100644 index 0000000..e51a247 --- /dev/null +++ b/java7/core/src/main/resources/META-INF/services/org.apache.tamaya.Configuration @@ -0,0 +1,19 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy current the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +org.apache.tamaya.core.internal.DefaultConfiguration
