All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.lang3.reflect.TypeLiteral Maven / Gradle / Ivy

Go to download

Apache Commons Lang, a package of Java utility classes for the classes that are in java.lang's hierarchy, or are considered to be so standard as to justify existence in java.lang.

There is a newer version: 3.14.0
Show newest version
/*
 * 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.commons.lang3.reflect;

import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

import org.apache.commons.lang3.Validate;

/**
 * 

Type literal comparable to {@code javax.enterprise.util.TypeLiteral}, * made generally available outside the JEE context. Allows the passing around of * a "token" that represents a type in a typesafe manner, as opposed to * passing the (non-parameterized) {@link Type} object itself. Consider:

*

* You might see such a typesafe API as: *

 * class Typesafe {
 *   <T> T obtain(Class<T> type, ...);
 * }
 * 
* Consumed in the manner of: *
 * Foo foo = typesafe.obtain(Foo.class, ...);
 * 
* Yet, you run into problems when you want to do this with a parameterized type: *
 * List<String> listOfString = typesafe.obtain(List.class, ...); // could only give us a raw List
 * 
* {@code java.lang.reflect.Type} might provide some value: *
 * Type listOfStringType = ...; // firstly, how to obtain this? Doable, but not straightforward.
 * List<String> listOfString = (List<String>) typesafe.obtain(listOfStringType, ...); // nongeneric Type would necessitate a cast
 * 
* The "type literal" concept was introduced to provide an alternative, i.e.: *
 * class Typesafe {
 *   <T> T obtain(TypeLiteral<T> type, ...);
 * }
 * 
* Consuming code looks like: *
 * List<String> listOfString = typesafe.obtain(new TypeLiteral<List<String>>() {}, ...);
 * 
*

* This has the effect of "jumping up" a level to tie a {@code java.lang.reflect.Type} * to a type variable while simultaneously making it short work to obtain a * {@code Type} instance for any given type, inline. *

*

Additionally {@link TypeLiteral} implements the {@link Typed} interface which * is a generalization of this concept, and which may be implemented in custom classes. * It is suggested that APIs be defined in terms of the interface, in the following manner: *

*
 *   <T> T obtain(Typed<T> typed, ...);
 * 
* * @param the type * @since 3.2 */ public abstract class TypeLiteral implements Typed { @SuppressWarnings("rawtypes") private static final TypeVariable> T = TypeLiteral.class.getTypeParameters()[0]; /** * Represented type. */ public final Type value; private final String toString; /** * The default constructor. */ protected TypeLiteral() { this.value = Validate.notNull(TypeUtils.getTypeArguments(getClass(), TypeLiteral.class).get(T), "%s does not assign type parameter %s", getClass(), TypeUtils.toLongString(T)); this.toString = String.format("%s<%s>", TypeLiteral.class.getSimpleName(), TypeUtils.toString(value)); } @Override public final boolean equals(final Object obj) { if (obj == this) { return true; } if (!(obj instanceof TypeLiteral)) { return false; } final TypeLiteral other = (TypeLiteral) obj; return TypeUtils.equals(value, other.value); } @Override public int hashCode() { return 37 << 4 | value.hashCode(); } @Override public String toString() { return toString; } @Override public Type getType() { return value; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy