本文整理汇总了Java中java.lang.reflect.Method.getGenericReturnType方法的典型用法代码示例。如果您正苦于以下问题:Java Method.getGenericReturnType方法的具体用法?Java Method.getGenericReturnType怎么用?Java Method.getGenericReturnType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.reflect.Method
的用法示例。
在下文中一共展示了Method.getGenericReturnType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getReturnTypes
import java.lang.reflect.Method; //导入方法依赖的package包/类
public static Type[] getReturnTypes(Invocation invocation) {
try {
if (invocation != null && invocation.getInvoker() != null
&& invocation.getInvoker().getUrl() != null
&& ! invocation.getMethodName().startsWith("$")) {
String service = invocation.getInvoker().getUrl().getServiceInterface();
if (service != null && service.length() > 0) {
Class<?> cls = ReflectUtils.forName(service);
Method method = cls.getMethod(invocation.getMethodName(), invocation.getParameterTypes());
if (method.getReturnType() == void.class) {
return null;
}
return new Type[]{method.getReturnType(), method.getGenericReturnType()};
}
}
} catch (Throwable t) {
logger.warn(t.getMessage(), t);
}
return null;
}
示例2: getClassesForRecursion
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* Looks at a method and gets any return or parameter types that match the basePackage.
*/
private static List<Class> getClassesForRecursion(String basePackage, Method method){
List<Class> classesForRecursion = new ArrayList<>();
// Check regular return types
if (method.getReturnType().toString().contains(basePackage)) {
classesForRecursion.add(method.getReturnType());
} else {
// Check for generic return types
if (method.getGenericReturnType() instanceof ParameterizedType) {
ParameterizedType type = (ParameterizedType) method.getGenericReturnType();
Arrays.stream(type.getActualTypeArguments())
.filter(actualType -> actualType.getTypeName().contains(basePackage))
.forEach(actualType -> classesForRecursion.add((Class) actualType));
}
}
// Check for parameter types
Arrays.stream(method.getParameterTypes())
.filter(paramType -> paramType.toString().contains(basePackage))
.forEach(classesForRecursion::add);
return classesForRecursion;
}
示例3: getAsyncReturnType
import java.lang.reflect.Method; //导入方法依赖的package包/类
private Class getAsyncReturnType(Method method, Class returnType) {
if(Response.class.isAssignableFrom(returnType)){
Type ret = method.getGenericReturnType();
return erasure(((ParameterizedType)ret).getActualTypeArguments()[0]);
}else{
Type[] types = method.getGenericParameterTypes();
Class[] params = method.getParameterTypes();
int i = 0;
for(Class cls : params){
if(AsyncHandler.class.isAssignableFrom(cls)){
return erasure(((ParameterizedType)types[i]).getActualTypeArguments()[0]);
}
i++;
}
}
return returnType;
}
示例4: getAsyncReturnType
import java.lang.reflect.Method; //导入方法依赖的package包/类
private Class getAsyncReturnType(Method method, Class returnType) {
if(Response.class.isAssignableFrom(returnType)){
Type ret = method.getGenericReturnType();
return (Class) Utils.REFLECTION_NAVIGATOR.erasure(((ParameterizedType)ret).getActualTypeArguments()[0]);
}else{
Type[] types = method.getGenericParameterTypes();
Class[] params = method.getParameterTypes();
int i = 0;
for(Class cls : params){
if(AsyncHandler.class.isAssignableFrom(cls)){
return (Class) Utils.REFLECTION_NAVIGATOR.erasure(((ParameterizedType)types[i]).getActualTypeArguments()[0]);
}
i++;
}
}
return returnType;
}
示例5: getClazz
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* get field type
*/
private Type getClazz(Field field, Class<?> clazz) {
Method method = getGetterMethod(field, clazz);
if (method == null) {
return null;
}
return method.getGenericReturnType();
}
示例6: getType
import java.lang.reflect.Method; //导入方法依赖的package包/类
public Type getType(String methodName){
Method method;
try {
method = getClass().getDeclaredMethod(methodName, new Class<?>[]{} );
} catch (Exception e) {
throw new IllegalStateException(e);
}
Type gtype = method.getGenericReturnType();
return gtype;
}
示例7: getMethodType
import java.lang.reflect.Method; //导入方法依赖的package包/类
public static Type getMethodType(Class<?> clazz, String methodName) {
try {
Method method = clazz.getMethod(methodName);
return method.getGenericReturnType();
} catch (Exception ex) {
return null;
}
}
示例8: get
import java.lang.reflect.Method; //导入方法依赖的package包/类
@Override
public CallAdapter<?, ?> get(ApiComposer apiComposer, Method method) {
Type returnType = method.getGenericReturnType();
Class<?> rawType = Types.getRawType(returnType);
boolean isSingle = rawType == Single.class;
if (rawType != Observable.class && !isSingle) {
return null;
}
Type observableType = getParameterUpperBound(0, returnType);
return new RxCallAdapter(observableType);
}
示例9: addPropertyDescriptor
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* Adds the property descriptor to the list store.
*/
private void addPropertyDescriptor(PropertyDescriptor pd) {
String propName = pd.getName();
List<PropertyDescriptor> list = pdStore.get(propName);
if (list == null) {
list = new ArrayList<>();
pdStore.put(propName, list);
}
if (this.beanClass != pd.getClass0()) {
// replace existing property descriptor
// only if we have types to resolve
// in the context of this.beanClass
Method read = pd.getReadMethod();
Method write = pd.getWriteMethod();
boolean cls = true;
if (read != null) cls = cls && read.getGenericReturnType() instanceof Class;
if (write != null) cls = cls && write.getGenericParameterTypes()[0] instanceof Class;
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
Method readI = ipd.getIndexedReadMethod();
Method writeI = ipd.getIndexedWriteMethod();
if (readI != null) cls = cls && readI.getGenericReturnType() instanceof Class;
if (writeI != null) cls = cls && writeI.getGenericParameterTypes()[1] instanceof Class;
if (!cls) {
pd = new IndexedPropertyDescriptor(ipd);
pd.updateGenericsFor(this.beanClass);
}
}
else if (!cls) {
pd = new PropertyDescriptor(pd);
pd.updateGenericsFor(this.beanClass);
}
}
list.add(pd);
}
示例10: checkReturnType
import java.lang.reflect.Method; //导入方法依赖的package包/类
private static void checkReturnType(Method method1, Method method2) {
Class<?> returnType;
Type returnType1, returnType2;
if (ModuleCall.class.equals(method1.getReturnType())) { // 异步回调的方法
returnType = method2.getReturnType();
if (returnType.equals(Observable.class) || returnType.equals(Single.class) || returnType.equals(Flowable.class) || returnType.equals(Maybe.class)) {
returnType1 = method1.getGenericReturnType();
returnType2 = method2.getGenericReturnType();
if (returnType1 instanceof ParameterizedType && returnType2 instanceof ParameterizedType) { // 都带泛型
// 检查泛型的类型是否一样
if (!((ParameterizedType) returnType1).getActualTypeArguments()[0].equals(((ParameterizedType) returnType2).getActualTypeArguments()[0])) {
throw new IllegalArgumentException(method1.getName() + "方法的返回值类型的泛型的须一样");
}
} else if (!(returnType1 instanceof Class && returnType2 instanceof Class)) {
throw new IllegalArgumentException(method1.getName() + "方法的返回值类型的泛型的须一样");
}
} else {
throw new IllegalArgumentException(String.format("%s::%s的返回值类型必须是Observable,Single,Flowable,Maybe之一", method2.getDeclaringClass().getSimpleName(), method2.getName()));
}
} else {
if (!method1.getGenericReturnType().equals(method2.getGenericReturnType())) { //同步调用的返回值必须一样
throw new IllegalArgumentException(method1.getName() + "方法的返回值类型不一样");
}
}
}
示例11: getBeanHandlerType
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* 获得实体类型(从方法的返回值中获取),用于BeanHandler映射
* @param method
* @return
*/
private Class<?> getBeanHandlerType(Method method) {
if (List.class.isAssignableFrom(method.getReturnType())) {
Type type = method.getGenericReturnType();
String typeName = type.getTypeName().replaceAll(".+<(.+)>", "$1");
return ClassUtil.loadClass(typeName);
}
return method.getReturnType();
}
示例12: testToGenericType_staticMemberClass
import java.lang.reflect.Method; //导入方法依赖的package包/类
public void testToGenericType_staticMemberClass() throws Exception {
Method getStaticAnonymousClassMethod =
TypeTokenTest.class.getDeclaredMethod("getStaticAnonymousClass", Object.class);
ParameterizedType javacReturnType =
(ParameterizedType) getStaticAnonymousClassMethod.getGenericReturnType();
ParameterizedType parameterizedType =
(ParameterizedType) TypeToken.toGenericType(GenericClass.class).getType();
assertThat(parameterizedType.getOwnerType()).isEqualTo(javacReturnType.getOwnerType());
}
示例13: getReturnTypeParameters
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* @param method {@link Method} object
* @return the actual type parameters used in the source code. Return <strong>empty</strong> list if no parametrized type
*/
public static List<String> getReturnTypeParameters(Method method) {
List<String> typeParameters = new ArrayList<>();
Type type = method.getGenericReturnType();
String typeName = type.getTypeName(); // ex: java.util.List<com.timeyang.search.entity.Person>, java.lang.Long
Pattern p = Pattern.compile("<((\\S+\\.?),?\\s*)>");
Matcher m = p.matcher(typeName);
while (m.find()) {
typeParameters.add(m.group(2));
}
return typeParameters;
}
示例14: makeCompositeMapping
import java.lang.reflect.Method; //导入方法依赖的package包/类
private MXBeanMapping makeCompositeMapping(Class<?> c,
MXBeanMappingFactory factory)
throws OpenDataException {
// For historical reasons GcInfo implements CompositeData but we
// shouldn't count its CompositeData.getCompositeType() field as
// an item in the computed CompositeType.
final boolean gcInfoHack =
(c.getName().equals("com.sun.management.GcInfo") &&
c.getClassLoader() == null);
ReflectUtil.checkPackageAccess(c);
final List<Method> methods =
MBeanAnalyzer.eliminateCovariantMethods(Arrays.asList(c.getMethods()));
final SortedMap<String,Method> getterMap = newSortedMap();
/* Select public methods that look like "T getX()" or "boolean
isX()", where T is not void and X is not the empty
string. Exclude "Class getClass()" inherited from Object. */
for (Method method : methods) {
final String propertyName = propertyName(method);
if (propertyName == null)
continue;
if (gcInfoHack && propertyName.equals("CompositeType"))
continue;
Method old =
getterMap.put(decapitalize(propertyName),
method);
if (old != null) {
final String msg =
"Class " + c.getName() + " has method name clash: " +
old.getName() + ", " + method.getName();
throw new OpenDataException(msg);
}
}
final int nitems = getterMap.size();
if (nitems == 0) {
throw new OpenDataException("Can't map " + c.getName() +
" to an open data type");
}
final Method[] getters = new Method[nitems];
final String[] itemNames = new String[nitems];
final OpenType<?>[] openTypes = new OpenType<?>[nitems];
int i = 0;
for (Map.Entry<String,Method> entry : getterMap.entrySet()) {
itemNames[i] = entry.getKey();
final Method getter = entry.getValue();
getters[i] = getter;
final Type retType = getter.getGenericReturnType();
openTypes[i] = factory.mappingForType(retType, factory).getOpenType();
i++;
}
CompositeType compositeType =
new CompositeType(c.getName(),
c.getName(),
itemNames, // field names
itemNames, // field descriptions
openTypes);
return new CompositeMapping(c,
compositeType,
itemNames,
getters,
factory);
}
示例15: getGenericReturnType
import java.lang.reflect.Method; //导入方法依赖的package包/类
@Override
Type getGenericReturnType(Method m) {
return m.getGenericReturnType();
}