Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
How can i achieve this? public class GenericClass<T> { public Type getMyType() { //How do I return the type of T? } } Everything I have tried so far always returns type Object rather than the specific type used. Thanks a lot.
I want to use enum values in a selectManyCheckbox. The checkboxes get populated correctly, however, when selecting some values and submitting them, their runtime type is String, and not enum. My code: <h:selectManyCheckbox value="#{userController.roles}" layout="pageDirection"> <f:selectItems value="#{userController.rolesSelectMany}" /> </h:selectManyCheckbox> UserCont...
I wish to a check if a method exists in an interface based on its signatures. The signature that the method should have is: Collection<Foo> methodName(Spam arg0, Eggs arg1, ...) I can find the methods via Class.getMethods() then find the name, parameters and return type respectively with method.getName(), method.getParameterTypes() and method.getReturnType(). But what do I compare ...
I have been trying to determine the type of a field in a class. I've seen all the introspection methods but haven't quite figured out how to do it. This is going to be used to generate xml/json from a java class. I've looked at a number of the questions here but haven't found exactly what I need. Example: class Person { public final String name; public final List<Person> childre...
Typically, I've seen people use the class literal like this: Class<Foo> cls = Foo.class; But what if the type is generic, e.g. List? This works fine, but has a warning since List should be parameterized: Class<List> cls = List.class So why not add a <?>? Well, this causes a type mismatch error: Class<List<?>> cls = List.class I figured something like this ...
Type erasure is supposed to erase all generic information... If this is the case how does a library like GSON use generics to determine what type to deserialize to? e.g. private Map<String,Date> tenordates; This will deserialize to <String,Date> where as private Map<Date,Date> tenordates; will deserialize to <Date,Date> so somehow its using the generic info at ru...
How can I print the type of a generic java type? Reflection? Any tricks? public class Foo<K> { private K element; @Override public String toString() { return "Type: " + K; } }
In Java, Class has an isAssignableFrom method defined as follows: public boolean isAssignableFrom(Class<?> cls) Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class obj...
Given two classes like this: class Example1<A,B> { public Map<A,B> someMap = new HashMap<A,B>(); } class Example2 extends Example1<URL, URL> { } Is there any way using reflection that I can determine the component types of the Map for Example2? I know I can do: ParameterizedType t = (ParameterizedType) Example2.class.getFields()[0].getGenericType(); But f...
I wrote a class that has a map of <String, Object>. I need it to hold arbitrary objects, but at the same time sometimes I need to cast some of those objects, so I'll do something like HashMap<String, Object> map = new HashMap<String, Object>(); Object foo = map.get("bar"); ...
This is my code: The ExecutorImp extends AbstractExecutor which extract the same execute logics of its implementers(ExecutorImp is one case),when calling the execute() method of ExecutorImp, it will call the method in its supertype,but the supertype (the AbstractExcutor) should know another class binding to the implementer(in the example, it is the User class): import java.lang.reflect.Invocat...
I have an interface public interface FooBar<T> { } I have a class that implements it public class BarFoo implements FooBar<Person> { } With reflection, I want to take an instance of BarFoo and get that the version of FooBar it implements is Person. I use .getInterfaces from BarFoo to get back to FooBar, but that doesn't help me find out what T is.
There are other related questions e.g. 6624113, 3403909, 4516891 but my question is simpler and more specific. I want to know at runtime what type my class was parameterized with - I want a Class object of the type of the type parameter. Because of type erasure, the expression T.class doesn't work, and there is no function like typeof(T) in C# to get it. However, there is some "uber-reflecti...
I am using reflection to get all the get all the methods in a class like this: Method[] allMethods = c.getDeclaredMethods(); After that I am iterating through the methods for (Method m: allMethods){ //I want to find out if the return is is a parameterized type or not m.getReturnType(); } For example: if I have a method like this one: public Set<Cat> getCats(); How can I u...
I'm running into a problem with my program where given an object and an attribute name I want to return the method's return type. public static Class<?> getAttributeType(Object object, String attributeName) { try { Method method = object.getClass().getMethod( "get" + StringUtils.capitalize(attributeName)); return method.getReturnType(); } catch (...
 /*
  * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Sun designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package java.lang.reflect;


ParameterizedType represents a parameterized type such as Collection<String>.

A parameterized type is created the first time it is needed by a reflective method, as specified in this package. When a parameterized type p is created, the generic type declaration that p instantiates is resolved, and all type arguments of p are created recursively. See TypeVariable for details on the creation process for type variables. Repeated creation of a parameterized type has no effect.

Instances of classes that implement this interface must implement an equals() method that equates any two instances that share the same generic type declaration and have equal type parameters.

Since:
1.5
public interface ParameterizedType extends Type {
    
Returns an array of Type objects representing the actual type arguments to this type.

Note that in some cases, the returned array be empty. This can occur if this type represents a non-parameterized type nested within a parameterized type.

Returns:
an array of Type objects representing the actual type arguments to this type
Throws:
java.lang.TypeNotPresentException if any of the actual type arguments refers to a non-existent type declaration
MalformedParameterizedTypeException if any of the actual type parameters refer to a parameterized type that cannot be instantiated for any reason
Since:
1.5
    Type[] getActualTypeArguments();

    
Returns the Type object representing the class or interface that declared this type.

Returns:
the Type object representing the class or interface that declared this type
Since:
1.5
    Type getRawType();

    
Returns a Type object representing the type that this type is a member of. For example, if this type is O<T>.I<S>, return a representation of O<T>.

If this type is a top-level type, null is returned.

Returns:
a Type object representing the type that this type is a member of. If this type is a top-level type, null is returned
Throws:
java.lang.TypeNotPresentException if the owner type refers to a non-existent type declaration
MalformedParameterizedTypeException if the owner type refers to a parameterized type that cannot be instantiated for any reason
Since:
1.5
    Type getOwnerType();
New to GrepCode? Check out our FAQ X