當前位置: 首頁>>代碼示例>>Java>>正文


Java Method.getGenericParameterTypes方法代碼示例

本文整理匯總了Java中java.lang.reflect.Method.getGenericParameterTypes方法的典型用法代碼示例。如果您正苦於以下問題:Java Method.getGenericParameterTypes方法的具體用法?Java Method.getGenericParameterTypes怎麽用?Java Method.getGenericParameterTypes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.lang.reflect.Method的用法示例。


在下文中一共展示了Method.getGenericParameterTypes方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: invokeFunction

import java.lang.reflect.Method; //導入方法依賴的package包/類
private static <T> Function<Data<T>, Object> invokeFunction(Method method, Object target, Class<T> dataType) {
    Type[] parameters = method.getGenericParameterTypes();
    if (parameters.length != 1) {
        throw new IllegalArgumentException(
                String.format("Upcaster method '%s' has unexpected number of parameters. Expected 1 or 0.", method));
    }
    if (parameters[0] instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) parameters[0];
        if (parameterizedType.getRawType().equals(Data.class) && dataType
                .isAssignableFrom((Class<?>) parameterizedType.getActualTypeArguments()[0])) {
            return data -> invokeMethod(method, data, target);
        }
        if (dataType.isAssignableFrom((Class<?>) parameterizedType.getRawType())) {
            return data -> invokeMethod(method, data.getValue(), target);
        }
    } else if (dataType.isAssignableFrom((Class<?>) parameters[0])) {
        return data -> invokeMethod(method, data.getValue(), target);
    }
    throw new IllegalArgumentException(String.format(
            "First parameter in upcaster method '%s' is of unexpected type. Expected Data<%s> or %s.",
            method, dataType.getName(), dataType.getName()));
}
 
開發者ID:flux-capacitor-io,項目名稱:flux-capacitor-client,代碼行數:23,代碼來源:UpcastInspector.java

示例2: registerMethodParameterTypes

import java.lang.reflect.Method; //導入方法依賴的package包/類
public boolean registerMethodParameterTypes(String service, Class<?> clazz) {

		Method[] methods = clazz.getMethods();

		for (Method method : methods) {
			Class<?> declaredClazz = method.getDeclaringClass();
			if (declaredClazz == Object.class) {
				continue;
			}

			Type[] parameterTypes = method.getGenericParameterTypes();
			String key = service + "/" + method.getName() + "/" + parameterTypes.length;
			this.serviceMethodParameterTypesCache.put(key, new ServiceMethodDesc(key, parameterTypes));

			String defaultKey = service + "/" + method.getName() + "/";
			this.serviceMethodParameterTypesCache.put(defaultKey, new ServiceMethodDesc(key, parameterTypes));
		}

		return false;

	}
 
開發者ID:sylinklee,項目名稱:netto_rpc,代碼行數:22,代碼來源:ArgsDeserializer.java

示例3: getActualTypeArguments

import java.lang.reflect.Method; //導入方法依賴的package包/類
/**
 * 查找泛型類型
 *
 * @param m
 * @return
 */
protected Type getActualTypeArguments(Method m) {
    Type[] genericParameterTypes = m.getGenericParameterTypes();
    for (Type type : genericParameterTypes) {
        Type[] actualTypeArguments = ReflectionUtils.getActualTypeArguments(type);
        if (null == actualTypeArguments || actualTypeArguments.length == 0)
            return null;
        //Map 取value的泛型
        if (actualTypeArguments.length > 1) {
            return actualTypeArguments[1];
            //Collection 取泛型
        } else {
            return actualTypeArguments[0];
        }
    }
    return null;
}
 
開發者ID:lovejj1994,項目名稱:Simplify-Core,代碼行數:23,代碼來源:AbstractJson.java

示例4: getParamProviders

import java.lang.reflect.Method; //導入方法依賴的package包/類
/**
 * Returns the param providers for a resource method.
 *
 * This is very similar to the logic used in building param providers for a normal
 * <code>@Inject</code> constructor, with one major difference.
 *
 * A resource method is allowed one special "entity" parameter representing the content body.
 * This entity parameter is handled by a <code>EntityProvider</code>.
 *
 * @param method The resource method.
 * @return The array of resource method param providers.
 */
private Provider<?>[] getParamProviders(final Method method) {
    final Class<?>[] paramClasses = method.getParameterTypes();
    final Type[] paramTypes = method.getGenericParameterTypes();
    final Annotation[][] annotations = method.getParameterAnnotations();
    final Provider<?>[] result = new Provider<?>[paramTypes.length];

    final Consumes consumes = method.getAnnotation(Consumes.class);
    final List<MediaType> consumesTypes = MediaTypeUtils.parseMediaTypes(consumes);
    boolean consumed = false;

    for (int i = 0; i < paramTypes.length; i++) {
        if (annotations[i].length == 0 && !consumed) {
            result[i] = new EntityProvider<>(paramClasses[i], paramTypes[i], annotations[i], consumesTypes);
            consumed = true;
        } else {
            result[i] = getInjector().getProvider(paramClasses[i], annotations[i]);
        }
    }
    return result;
}
 
開發者ID:minijax,項目名稱:minijax,代碼行數:33,代碼來源:MinijaxApplication.java

示例5: testInterface

import java.lang.reflect.Method; //導入方法依賴的package包/類
@Test
public void testInterface() throws Exception {
  ClassConfig classConfig = new ClassConfig();
  classConfig.setIntf(true);
  String intfName = "cse.ut.TestInterface";
  classConfig.setClassName(intfName);

  String source = "java.util.List method(java.util.Map map, java.util.Set set);";
  String genericSignature =
      "(Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;Ljava/util/Set<Ljava/lang/String;>;)Ljava/util/List<Ljava/lang/String;>;";
  classConfig.addMethod(source, genericSignature);

  Class<?> intf = JavassistUtils.createClass(classConfig);

  Assert.assertEquals(intfName, intf.getName());
  Method method = ReflectUtils.findMethod(intf, "method");
  Assert.assertEquals("method", method.getName());
  Assert.assertEquals("java.util.List<java.lang.String>", method.getGenericReturnType().getTypeName());

  Type[] types = method.getGenericParameterTypes();
  Assert.assertEquals("java.util.Map<java.lang.String, java.lang.String>", types[0].getTypeName());
  Assert.assertEquals("java.util.Set<java.lang.String>", types[1].getTypeName());
}
 
開發者ID:apache,項目名稱:incubator-servicecomb-java-chassis,代碼行數:24,代碼來源:TestJavassistUtils.java

示例6: getMethodGenericParameterTypes

import java.lang.reflect.Method; //導入方法依賴的package包/類
public static List<Class<?>> getMethodGenericParameterTypes(Method method) {
	List<Class<?>> results = new ArrayList<Class<?>>();
	Type[] genericParameterTypes = method.getGenericParameterTypes();
	for (Type genericParameterType : genericParameterTypes) {
		if (genericParameterType instanceof ParameterizedType) {
			ParameterizedType aType = (ParameterizedType) genericParameterType;
			Type[] parameterArgTypes = aType.getActualTypeArguments();
			for (Type parameterArgType : parameterArgTypes) {
				Class<?> parameterArgClass = (Class<?>) parameterArgType;
				results.add(parameterArgClass);
			}
			return results;
		}
	}
	return results;
}
 
開發者ID:funtl,項目名稱:framework,代碼行數:17,代碼來源:CommonUtils.java

示例7: isEventHandler

import java.lang.reflect.Method; //導入方法依賴的package包/類
private boolean isEventHandler(Method m) {
    // We assume that a method is an event handler if it has a single
    // argument, whose type inherit from java.util.Event.
    Type argTypes[] = m.getGenericParameterTypes();
    if (argTypes.length != 1) {
        return false;
    }
    return isSubclass(TypeResolver.erase(TypeResolver.resolveInClass(beanClass, argTypes[0])), EventObject.class);
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:10,代碼來源:Introspector.java

示例8: ConvertingMethod

import java.lang.reflect.Method; //導入方法依賴的package包/類
private ConvertingMethod(Method m) throws OpenDataException {
    this.method = m;
    MXBeanMappingFactory mappingFactory = MXBeanMappingFactory.DEFAULT;
    returnMapping =
            mappingFactory.mappingForType(m.getGenericReturnType(), mappingFactory);
    Type[] params = m.getGenericParameterTypes();
    paramMappings = new MXBeanMapping[params.length];
    boolean identity = true;
    for (int i = 0; i < params.length; i++) {
        paramMappings[i] = mappingFactory.mappingForType(params[i], mappingFactory);
        identity &= DefaultMXBeanMappingFactory.isIdentity(paramMappings[i]);
    }
    paramConversionIsIdentity = identity;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:15,代碼來源:ConvertingMethod.java

示例9: create

import java.lang.reflect.Method; //導入方法依賴的package包/類
public static <R, T> RuleSourceBackedRuleAction<R, T> create(ModelType<T> subjectType, R ruleSourceInstance) {
    ModelType<R> ruleSourceType = ModelType.typeOf(ruleSourceInstance);
    List<Method> mutateMethods = JavaReflectionUtil.findAllMethods(ruleSourceType.getConcreteClass(), new Spec<Method>() {
        public boolean isSatisfiedBy(Method element) {
            return element.isAnnotationPresent(Mutate.class);
        }
    });
    FormattingValidationProblemCollector problemsFormatter = new FormattingValidationProblemCollector("rule source", ruleSourceType);
    RuleSourceValidationProblemCollector problems = new DefaultRuleSourceValidationProblemCollector(problemsFormatter);

    if (mutateMethods.size() == 0) {
        problems.add("Must have at exactly one method annotated with @" + Mutate.class.getName());
    } else {
        if (mutateMethods.size() > 1) {
            problems.add("More than one method is annotated with @" + Mutate.class.getName());
        }

        for (Method ruleMethod : mutateMethods) {
            if (ruleMethod.getReturnType() != Void.TYPE) {
                problems.add(ruleMethod, "A rule method must return void");
            }
            Type[] parameterTypes = ruleMethod.getGenericParameterTypes();
            if (parameterTypes.length == 0 || !subjectType.isAssignableFrom(ModelType.of(parameterTypes[0]))) {
                problems.add(ruleMethod, String.format("First parameter of a rule method must be of type %s", subjectType));
            }
        }
    }

    if (problemsFormatter.hasProblems()) {
        throw new RuleActionValidationException(problemsFormatter.format());
    }

    return new RuleSourceBackedRuleAction<R, T>(ruleSourceInstance, new JavaMethod<R, T>(subjectType.getConcreteClass(), mutateMethods.get(0)));
}
 
開發者ID:lxxlxx888,項目名稱:Reer,代碼行數:35,代碼來源:RuleSourceBackedRuleAction.java

示例10: Builder

import java.lang.reflect.Method; //導入方法依賴的package包/類
Builder(Retrofit retrofit, Method method) {
  this.retrofit = retrofit;
  this.method = method;
  this.methodAnnotations = method.getAnnotations();
  this.parameterTypes = method.getGenericParameterTypes();
  this.parameterAnnotationsArray = method.getParameterAnnotations();
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:8,代碼來源:ServiceMethod.java

示例11: generateByNewInstance

import java.lang.reflect.Method; //導入方法依賴的package包/類
/**
 * this method generate record instance by calling newIntance
 * using reflection, add register the generated value to typeValueCache
 */
@SuppressWarnings("rawtypes")
private static Object generateByNewInstance(Class clazz) throws Exception {
  Object ret = typeValueCache.get(clazz);
  if (ret != null) {
    return ret;
  }
  Method newInstance = null;
  Type [] paramTypes = new Type[0];
  // get newInstance method with most parameters
  for (Method m : clazz.getMethods()) {
    int mod = m.getModifiers();
    if (m.getDeclaringClass().equals(clazz) &&
        Modifier.isPublic(mod) &&
        Modifier.isStatic(mod) &&
        m.getName().equals("newInstance")) {
      Type [] pts = m.getGenericParameterTypes();
      if (newInstance == null
          || (pts.length > paramTypes.length)) {
        newInstance = m;
        paramTypes = pts;
      }
    }
  }
  if (newInstance == null) {
    throw new IllegalArgumentException("type " + clazz.getName() +
        " does not have newInstance method");
  }
  Object [] args = new Object[paramTypes.length];
  for (int i=0;i<args.length;i++) {
    args[i] = genTypeValue(paramTypes[i]);
  }
  ret = newInstance.invoke(null, args);
  typeValueCache.put(clazz, ret);
  return ret;
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:40,代碼來源:TestPBImplRecords.java

示例12: getReturnClassImports

import java.lang.reflect.Method; //導入方法依賴的package包/類
private String getReturnClassImports(
                                      Class<?> actionClass ) {

    Method[] actionClassMethods = actionClass.getDeclaredMethods();
    //using TreeSet to prevent duplication of imports and have sorted list
    Set<String> imports = new TreeSet<String>();
    for (Method m : actionClassMethods) {
        if (ActionClassGenerator.isAnActionClass(m)) {

            // import method return type if needed
            Class<?> returnType = m.getReturnType();
            //check if the return type is an array
            if (returnType.getComponentType() != null) {
                returnType = returnType.getComponentType();
            }
            if (needsToImportMethodReturnType(returnType)) {
                addImport(imports, returnType, m.getGenericReturnType());
            }

            // import method parameter types if needed
            Class<?> methodParameterTypes[] = m.getParameterTypes();
            Type methodGenericParameterTypes[] = m.getGenericParameterTypes();
            for (int i = 0; i < methodParameterTypes.length; i++) {
                Class<?> methodParameterType = methodParameterTypes[i];
                if (needsToImportMethodParameterType(methodParameterType)) {
                    addImport(imports, methodParameterType, methodGenericParameterTypes[i]);
                }
            }
        }
    }
    if (imports.size() > 0) {
        StringBuilder sb = new StringBuilder();
        for (String s : imports) {
            sb.append(s);
        }
        return sb.toString();
    } else {
        return "";
    }
}
 
開發者ID:Axway,項目名稱:ats-framework,代碼行數:41,代碼來源:ClassTemplateProcessor.java

示例13: getGenerics

import java.lang.reflect.Method; //導入方法依賴的package包/類
/**
 * 獲取方法某個入參參數上的某個位置的範型Class原型
 * 
 * @param i_Method
 * @param i_ParamIndex      方法的入參參數位置
 * @param i_GenericsIndex   入參參數範型的位置
 * @return
 */
public static Class<?> getGenerics(Method i_Method ,int i_ParamIndex ,int i_GenericsIndex)
{
    try
    {
        ParameterizedType v_PType = (ParameterizedType) i_Method.getGenericParameterTypes()[i_ParamIndex];
        Type              v_Type  = v_PType.getActualTypeArguments()[i_GenericsIndex];
        return (Class<?>)v_Type;
    }
    catch (Exception exce)
    {
        return null;
    }
}
 
開發者ID:HY-ZhengWei,項目名稱:hy.common.base,代碼行數:22,代碼來源:MethodReflect.java

示例14: of

import java.lang.reflect.Method; //導入方法依賴的package包/類
@NonNull
static ArgumentParser of(@NonNull final Binder binder, @NonNull final Method method) {
  final Annotation[][] annotations = method.getParameterAnnotations();
  final Type[] types = method.getGenericParameterTypes();

  final Argument[] arguments = new Argument[types.length];
  for(int i = 0; i < types.length; i++) {
    arguments[i] = createArgument(binder, types[i], Arrays.asList(annotations[i]));
  }

  return new ArgumentParser(arguments);
}
 
開發者ID:KyoriPowered,項目名稱:ghost,代碼行數:13,代碼來源:ArgumentParser.java

示例15: 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);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:38,代碼來源:Introspector.java


注:本文中的java.lang.reflect.Method.getGenericParameterTypes方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。