Search in sources :

Example 66 with Parameter

use of java.lang.reflect.Parameter in project quarkus by quarkusio.

the class QuarkusRestClientBuilder method verifyInterface.

private <T> void verifyInterface(Class<T> typeDef) {
    Method[] methods = typeDef.getMethods();
    // multiple verbs
    for (Method method : methods) {
        boolean hasHttpMethod = false;
        for (Annotation annotation : method.getAnnotations()) {
            boolean isHttpMethod = (annotation.annotationType().getAnnotation(HttpMethod.class) != null);
            if (!hasHttpMethod && isHttpMethod) {
                hasHttpMethod = true;
            } else if (hasHttpMethod && isHttpMethod) {
                throw new RestClientDefinitionException("Ambiguous @HttpMethod definition on type " + typeDef);
            }
        }
    }
    // invalid parameter
    Path classPathAnno = typeDef.getAnnotation(Path.class);
    ResteasyUriBuilder template = null;
    for (Method method : methods) {
        Path methodPathAnno = method.getAnnotation(Path.class);
        if (methodPathAnno != null) {
            template = classPathAnno == null ? (ResteasyUriBuilder) new ResteasyUriBuilderImpl().uri(methodPathAnno.value()) : (ResteasyUriBuilder) new ResteasyUriBuilderImpl().uri(classPathAnno.value() + "/" + methodPathAnno.value());
        } else if (classPathAnno != null) {
            template = (ResteasyUriBuilder) new ResteasyUriBuilderImpl().uri(classPathAnno.value());
        } else {
            template = null;
        }
        if (template == null) {
            continue;
        }
        // it's not executed, so this can be anything - but a hostname needs to present
        template.host("localhost");
        Set<String> allVariables = new HashSet<>(template.getPathParamNamesInDeclarationOrder());
        Map<String, Object> paramMap = new HashMap<>();
        for (Parameter p : method.getParameters()) {
            PathParam pathParam = p.getAnnotation(PathParam.class);
            if (pathParam != null) {
                paramMap.put(pathParam.value(), "foobar");
            } else if (p.isAnnotationPresent(org.jboss.resteasy.annotations.jaxrs.PathParam.class)) {
                org.jboss.resteasy.annotations.jaxrs.PathParam rePathParam = p.getAnnotation(org.jboss.resteasy.annotations.jaxrs.PathParam.class);
                String name = rePathParam.value() == null || rePathParam.value().length() == 0 ? p.getName() : rePathParam.value();
                paramMap.put(name, "foobar");
            } else if (p.isAnnotationPresent(BeanParam.class)) {
                verifyBeanPathParam(p.getType(), paramMap);
            }
        }
        if (allVariables.size() != paramMap.size()) {
            throw new RestClientDefinitionException("Parameters and variables don't match on " + typeDef + "::" + method.getName());
        }
        try {
            template.resolveTemplates(paramMap, false).build();
        } catch (IllegalArgumentException ex) {
            throw new RestClientDefinitionException("Parameter names don't match variable names on " + typeDef + "::" + method.getName(), ex);
        }
    }
}
Also used : Path(jakarta.ws.rs.Path) ResteasyUriBuilderImpl(org.jboss.resteasy.specimpl.ResteasyUriBuilderImpl) HashMap(java.util.HashMap) HttpMethod(jakarta.ws.rs.HttpMethod) Method(java.lang.reflect.Method) RestClientDefinitionException(org.eclipse.microprofile.rest.client.RestClientDefinitionException) Annotation(java.lang.annotation.Annotation) ResteasyUriBuilder(org.jboss.resteasy.spi.ResteasyUriBuilder) Parameter(java.lang.reflect.Parameter) PathParam(jakarta.ws.rs.PathParam) HashSet(java.util.HashSet)

Example 67 with Parameter

use of java.lang.reflect.Parameter in project today-infrastructure by TAKETODAY.

the class ResolvableTypeTests method forParameter.

@Test
void forParameter() throws Exception {
    Method method = Methods.class.getMethod("charSequenceParameter", List.class);
    final Parameter parameter = method.getParameters()[0];
    ResolvableType type = ResolvableType.fromParameter(parameter);
    assertThat(type.getType()).isEqualTo(method.getGenericParameterTypes()[0]);
}
Also used : Parameter(java.lang.reflect.Parameter) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 68 with Parameter

use of java.lang.reflect.Parameter in project today-infrastructure by TAKETODAY.

the class ResolvableTypeTests method resolveTypeVariableFromMethodParameterTypeWithImplementsClass.

@Test
@SuppressWarnings("deprecation")
void resolveTypeVariableFromMethodParameterTypeWithImplementsClass() throws Exception {
    Method method = Methods.class.getMethod("typedParameter", Object.class);
    final Parameter[] parameters = method.getParameters();
    ResolvableType type = ResolvableType.fromParameter(parameters[0], TypedMethods.class);
    assertThat(type.resolve()).isEqualTo(String.class);
    assertThat(type.getType().toString()).isEqualTo("T");
}
Also used : Parameter(java.lang.reflect.Parameter) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 69 with Parameter

use of java.lang.reflect.Parameter in project today-infrastructure by TAKETODAY.

the class TypeDescriptorTests method getSource.

@Test
public void getSource() throws Exception {
    Field field = getClass().getField("fieldScalar");
    final Method testParameterPrimitive = getClass().getMethod("testParameterPrimitive", int.class);
    final Parameter[] parameters = testParameterPrimitive.getParameters();
    final Parameter parameter = parameters[0];
    final TypeDescriptor descriptor = TypeDescriptor.forParameter(testParameterPrimitive, 0);
    assertThat(new TypeDescriptor(field).getSource()).isEqualTo(field);
    assertThat(descriptor.getSource()).isEqualTo(parameter);
    assertThat(TypeDescriptor.valueOf(Integer.class).getSource()).isEqualTo(Integer.class);
}
Also used : Field(java.lang.reflect.Field) Parameter(java.lang.reflect.Parameter) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 70 with Parameter

use of java.lang.reflect.Parameter in project today-infrastructure by TAKETODAY.

the class ParameterResolutionDelegateTests method assertAutowirableParameters.

private void assertAutowirableParameters(Executable executable) {
    int startIndex = (executable instanceof Constructor) && ClassUtils.isInnerClass(executable.getDeclaringClass()) ? 1 : 0;
    Parameter[] parameters = executable.getParameters();
    for (int parameterIndex = startIndex; parameterIndex < parameters.length; parameterIndex++) {
        Parameter parameter = parameters[parameterIndex];
        assertThat(ParameterResolutionDelegate.isAutowirable(parameter, parameterIndex)).as("Parameter " + parameter + " must be autowirable").isTrue();
    }
}
Also used : Constructor(java.lang.reflect.Constructor) Parameter(java.lang.reflect.Parameter)

Aggregations

Parameter (java.lang.reflect.Parameter)771 Method (java.lang.reflect.Method)276 ArrayList (java.util.ArrayList)142 Annotation (java.lang.annotation.Annotation)95 List (java.util.List)90 Type (java.lang.reflect.Type)77 HashMap (java.util.HashMap)75 Map (java.util.Map)72 Constructor (java.lang.reflect.Constructor)60 Test (org.junit.jupiter.api.Test)57 Arrays (java.util.Arrays)53 InvocationTargetException (java.lang.reflect.InvocationTargetException)52 Executable (java.lang.reflect.Executable)50 Field (java.lang.reflect.Field)45 Collectors (java.util.stream.Collectors)45 ParameterizedType (java.lang.reflect.ParameterizedType)42 Optional (java.util.Optional)42 Test (org.junit.Test)42 Set (java.util.Set)37 Modifier (java.lang.reflect.Modifier)29