Here you can find the source of getGenericInterfaceParamType(Class> cls, Class> rawType)
Parameter | Description |
---|---|
cls | a parameter |
rawType | a parameter |
public static Type getGenericInterfaceParamType(Class<?> cls, Class<?> rawType)
//package com.java2s; /******************************************************************************* * 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 * /*from w ww. j a v a2 s.c om*/ * 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. * *******************************************************************************/ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; public class Main { /** * Returns the Type of parameter of the generic interface of the class. * <p> * E.g. Let A be class that implements <tt>List<String></tt>. Calling * <tt>getGenericInterfaceType(A.class, List.class)</tt> will return * <tt>String.class</tt>. * <p> * In case the interface has more than one parameter, only the type of the * first parameter is returned by this method. * * @param cls * @param rawType * @return java.lang.reflect.Type */ public static Type getGenericInterfaceParamType(Class<?> cls, Class<?> rawType) { while (cls != null) { Type[] interfaces = cls.getGenericInterfaces(); for (Type type : interfaces) { if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; if (pType.getRawType() == rawType) { return pType.getActualTypeArguments()[0]; } else { continue; } } // look through the base interfaces of the current interface Type interfaceType = getGenericInterfaceParamType((Class<?>) type, rawType); if (interfaceType != null) { return interfaceType; } } cls = cls.getSuperclass(); } // if we're done with the recursive calls, perhaps developer // did not parameterize their interface return null; } }