java.lang.reflect.Method.getTypeParameters()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(114)

本文整理了Java中java.lang.reflect.Method.getTypeParameters()方法的一些代码示例,展示了Method.getTypeParameters()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Method.getTypeParameters()方法的具体详情如下:
包路径:java.lang.reflect.Method
类名称:Method
方法名:getTypeParameters

Method.getTypeParameters介绍

[英]Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order. Returns an array of length 0 if the underlying generic declaration declares no type variables.
[中]返回TypeVariable对象数组,这些对象按声明顺序表示由此GenericDeclaration对象表示的泛型声明声明的类型变量。如果基础泛型声明未声明任何类型变量,则返回长度为0的数组。

代码示例

代码示例来源:origin: google/guava

@Override
public final TypeVariable<?>[] getTypeParameters() {
 return method.getTypeParameters();
}

代码示例来源:origin: prestodb/presto

@Override
public final TypeVariable<?>[] getTypeParameters() {
 return method.getTypeParameters();
}

代码示例来源:origin: google/j2objc

@Override
public final TypeVariable<?>[] getTypeParameters() {
 return method.getTypeParameters();
}

代码示例来源:origin: jersey/jersey

/**
 * Get method type parameters.
 *
 * See also {@link Method#getTypeParameters()}.
 *
 * @return method type parameters.
 */
@SuppressWarnings("UnusedDeclaration")
public TypeVariable<Method>[] getTypeParameters() {
  return am.getTypeParameters();
}

代码示例来源:origin: jersey/jersey

/**
 * Get method type parameters.
 *
 * See also {@link Method#getTypeParameters()}.
 *
 * @return method type parameters.
 */
@SuppressWarnings("UnusedDeclaration")
public TypeVariable<Method>[] getTypeParameters() {
  return am.getTypeParameters();
}

代码示例来源:origin: wildfly/wildfly

@Override
public final TypeVariable<?>[] getTypeParameters() {
 return method.getTypeParameters();
}

代码示例来源:origin: pmd/pmd

public static boolean isGeneric(Method method) {
  return method.getTypeParameters().length != 0;
}

代码示例来源:origin: spring-projects/spring-loaded

public static List<TypeVariable<Method>> callGetTypeParameters(Method thiz) {
  return Arrays.asList(thiz.getTypeParameters());
}

代码示例来源:origin: org.mockito/mockito-core

private GenericMetadataSupport resolveGenericType(Type type, Method method) {
  if (type instanceof Class) {
    return new NotGenericReturnTypeSupport(this, type);
  }
  if (type instanceof ParameterizedType) {
    return new ParameterizedReturnType(this, method.getTypeParameters(), (ParameterizedType) type);
  }
  if (type instanceof TypeVariable) {
    return new TypeVariableReturnType(this, method.getTypeParameters(), (TypeVariable<?>) type);
  }
  throw new MockitoException("Ouch, it shouldn't happen, type '" + type.getClass().getCanonicalName() + "' on method : '" + method.toGenericString() + "' is not supported : " + type);
}

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

/**
 * As per [JACKSON-468], we need to also allow declaration of local
 * type bindings; mostly it will allow defining bounds.
 */
@Override
public JavaType getType(TypeBindings bindings) {
  return getType(bindings, _method.getTypeParameters());
}

代码示例来源:origin: google/guava

MethodSignature(Method method) {
 name = method.getName();
 parameterTypes = Arrays.asList(method.getParameterTypes());
 typeSignature = new TypeSignature(method.getTypeParameters());
}

代码示例来源:origin: google/guava

public <T> void testCaptureTypeParameter() throws Exception {
 TypeVariable<?> variable = new TypeParameter<T>() {}.typeVariable;
 TypeVariable<?> expected =
   TypeParameterTest.class.getDeclaredMethod("testCaptureTypeParameter")
     .getTypeParameters()[0];
 assertEquals(expected, variable);
}

代码示例来源:origin: pmd/pmd

private JavaTypeDefinition getGenericType(final String parameterName, Method method,
                     List<JavaTypeDefinition> methodTypeArguments) {
  if (method != null && methodTypeArguments != null) {
    int paramIndex = getGenericTypeIndex(method.getTypeParameters(), parameterName);
    if (paramIndex != -1) {
      return methodTypeArguments.get(paramIndex);
    }
  }
  return getGenericType(parameterName);
}

代码示例来源:origin: com.google.inject/guice

private static boolean isValidMethod(InjectableMethod injectableMethod, Errors errors) {
 boolean result = true;
 if (injectableMethod.jsr330) {
  Method method = injectableMethod.method;
  if (Modifier.isAbstract(method.getModifiers())) {
   errors.cannotInjectAbstractMethod(method);
   result = false;
  }
  if (method.getTypeParameters().length > 0) {
   errors.cannotInjectMethodWithTypeParameters(method);
   result = false;
  }
 }
 return result;
}

代码示例来源:origin: pmd/pmd

/**
 * https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.1
 * Potential applicability.
 */
public static boolean isMethodApplicable(Method method, String methodName, int argArity,
                     Class<?> accessingClass, List<JavaTypeDefinition> typeArguments) {
  return method.getName().equals(methodName) // name matches
      // is visible
      && isMemberVisibleFromClass(method.getDeclaringClass(), method.getModifiers(), accessingClass)
      // if method is vararg with arity n, then the invocation's arity >= n - 1
      && (!method.isVarArgs() || argArity >= getArity(method) - 1)
      // if the method isn't vararg, then arity matches
      && (method.isVarArgs() || argArity == getArity(method))
      // isn't generic or arity of type arguments matches that of parameters
      && (!isGeneric(method) || typeArguments.isEmpty()
      || method.getTypeParameters().length == typeArguments.size());
}

代码示例来源:origin: spring-projects/spring-framework

Assert.notNull(args, "Argument array must not be null");
TypeVariable<Method>[] declaredTypeVariables = method.getTypeParameters();
Type genericReturnType = method.getGenericReturnType();
Type[] methodParameterTypes = method.getGenericParameterTypes();

代码示例来源:origin: pmd/pmd

public static List<Constraint> produceInitialConstraints(Method method, ASTArgumentList argList,
                             List<Variable> variables) {
  List<Constraint> result = new ArrayList<>();
  Type[] methodParameters = method.getGenericParameterTypes();
  TypeVariable<Method>[] methodTypeParameters = method.getTypeParameters();
  // TODO: add support for variable arity methods
  for (int i = 0; i < methodParameters.length; i++) {
    int typeParamIndex = -1;
    if (methodParameters[i] instanceof TypeVariable) {
      typeParamIndex = JavaTypeDefinition
          .getGenericTypeIndex(methodTypeParameters, ((TypeVariable<?>) methodParameters[i]).getName());
    }
    if (typeParamIndex != -1) {
      // TODO: we are cheating here, it should be a contraint of the form 'var -> expression' not 'var->type'
      result.add(new Constraint(((TypeNode) argList.jjtGetChild(i)).getTypeDefinition(),
                   variables.get(typeParamIndex), LOOSE_INVOCATION));
    }
  }
  return result;
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testTypesSatisfyVariables() throws SecurityException, NoSuchFieldException,
    NoSuchMethodException {
  final Map<TypeVariable<?>, Type> typeVarAssigns = new HashMap<>();
  final Integer max = TypeUtilsTest.<Integer> stub();
  typeVarAssigns.put(getClass().getMethod("stub").getTypeParameters()[0], Integer.class);
  assertTrue(TypeUtils.typesSatisfyVariables(typeVarAssigns));
  typeVarAssigns.clear();
  typeVarAssigns.put(getClass().getMethod("stub2").getTypeParameters()[0], Integer.class);
  assertTrue(TypeUtils.typesSatisfyVariables(typeVarAssigns));
  typeVarAssigns.clear();
  typeVarAssigns.put(getClass().getMethod("stub3").getTypeParameters()[0], Integer.class);
  assertTrue(TypeUtils.typesSatisfyVariables(typeVarAssigns));
}

代码示例来源:origin: org.hamcrest/hamcrest-all

classToString(javaMethod.getReturnType()));
for (TypeVariable<Method> typeVariable : javaMethod.getTypeParameters()) {
  boolean hasBound = false;
  StringBuilder s = new StringBuilder(typeVariable.getName());

代码示例来源:origin: spring-projects/spring-framework

candidate.getParameterCount() >= minNrOfArgs) {
if (candidate.getTypeParameters().length > 0) {
  try {

相关文章

微信公众号

最新文章

更多