Search in sources :

Example 56 with Parameter

use of java.lang.reflect.Parameter in project Flash by Minato262.

the class Annotation method annotation.

@SuppressWarnings("unchecked")
@Test
public void annotation() throws InvocationTargetException, IllegalAccessException {
    Annotation annotationTest = new Annotation();
    Class<Annotation> clazz = (Class<Annotation>) annotationTest.getClass();
    RequestMapping annotation = clazz.getAnnotation(RequestMapping.class);
    Assert.assertNotEquals(annotation.value(), null);
    Assert.assertEquals(annotation.value(), "annotation");
    Assert.assertNotEquals(annotation.method(), null);
    Method[] methods = clazz.getDeclaredMethods();
    for (Method method : methods) {
        if (method.isAnnotationPresent(RequestMapping.class)) {
            RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
            Assert.assertNotEquals(requestMapping, null);
            Assert.assertNotEquals(requestMapping.value(), null);
            Assert.assertNotEquals(requestMapping.method(), null);
            Assert.assertNotEquals(requestMapping.params(), null);
            Assert.assertNotEquals(requestMapping.headers(), null);
            Assert.assertNotEquals(requestMapping.consumes(), null);
            Assert.assertNotEquals(requestMapping.produces(), null);
        }
        if (method.isAnnotationPresent(ResponseBody.class)) {
            ResponseBody responseBody = method.getAnnotation(ResponseBody.class);
            Assert.assertNotEquals(responseBody, null);
        }
        Parameter[] params = method.getParameters();
        if (params.length != 0) {
            method.invoke(annotationTest, 1L);
        }
        for (Parameter param : params) {
            if (param.isAnnotationPresent(RequestParam.class)) {
                RequestParam requestParam = param.getAnnotation(RequestParam.class);
                Assert.assertNotEquals(requestParam, null);
            }
        }
    }
    System.out.println(annotationTest.getGetId());
    System.out.println(annotationTest.getPostId());
    System.out.println(annotationTest.getPutId());
    System.out.println(annotationTest.getDeleteId());
    System.out.println(annotationTest.getHandler());
}
Also used : Parameter(java.lang.reflect.Parameter) RequestMethod(org.flashframework.http.RequestMethod) Method(java.lang.reflect.Method) ResponseBody(org.flashframework.http.annotation.ResponseBody) Test(org.junit.Test)

Example 57 with Parameter

use of java.lang.reflect.Parameter in project jeesuite-libs by vakinge.

the class RequestLoggingInterceptor method getParameterConfigs.

private List<ParameterLogConfig> getParameterConfigs(String className, Method method) {
    String fullName = className + method.getName();
    List<ParameterLogConfig> configs = parameterLogConfigs.get(fullName);
    // 
    if (configs == null) {
        synchronized (parameterLogConfigs) {
            Parameter[] parameters = method.getParameters() == null ? new Parameter[0] : method.getParameters();
            configs = new ArrayList<>(parameters.length);
            ParameterLogConfig config;
            for (Parameter parameter : parameters) {
                config = new ParameterLogConfig();
                if (ServletRequest.class.isAssignableFrom(parameter.getType()) || ServletResponse.class.isAssignableFrom(parameter.getType())) {
                    config.ignore = true;
                    configs.add(config);
                    continue;
                }
                String paramName = null;
                if (parameter.isAnnotationPresent(RequestParam.class)) {
                    paramName = parameter.getAnnotation(RequestParam.class).value();
                    if (StringUtils.isBlank(paramName)) {
                        paramName = parameter.getName();
                    }
                } else if (parameters.length > 1) {
                    paramName = parameter.getName();
                }
                config.paramName = paramName;
                config.isBody = parameter.getType() != MultipartFile.class && !BeanUtils.isSimpleDataType(parameter.getType());
                configs.add(config);
            }
            parameterLogConfigs.put(fullName, configs);
        }
    }
    return configs;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) Parameter(java.lang.reflect.Parameter)

Example 58 with Parameter

use of java.lang.reflect.Parameter in project markdown-doclet by Abnaxos.

the class ReflectedOptions method processorForMethod.

public static OptionProcessor processorForMethod(Object target, Method method) {
    List<ArgumentConverter<?>> converters = new ArrayList<>(method.getParameterCount());
    for (Parameter parameter : method.getParameters()) {
        ArgumentConverter<?> converter;
        OptionConsumer.Converter converterAnnotation = parameter.getAnnotation(OptionConsumer.Converter.class);
        if (converterAnnotation != null) {
            try {
                converter = converterAnnotation.value().newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                throw new ReflectionException("Error instantiating converter for parameter " + parameter + " method " + method);
            }
        } else {
            converter = StandardArgumentConverters.forType(parameter.getParameterizedType());
            if (converter == null) {
                throw new ReflectionException("No argument converter found for parameter " + parameter.getName() + " of " + method);
            }
        }
        converters.add(converter);
    }
    return (name, arguments) -> {
        if (arguments.size() != converters.size()) {
            throw new InvalidOptionArgumentsException("Unexpected argument count: " + arguments.size() + "!=" + converters.size() + "(expeted)");
        }
        Object[] methodArguments = new Object[arguments.size()];
        for (int i = 0; i < arguments.size(); i++) {
            methodArguments[i] = converters.get(i).convert(arguments.get(i));
        }
        try {
            method.invoke(target, methodArguments);
        } catch (IllegalAccessException e) {
            throw new ArgumentsProcessingException(e);
        } catch (InvocationTargetException e) {
            if (e.getTargetException() instanceof InvalidOptionArgumentsException) {
                throw (InvalidOptionArgumentsException) e.getTargetException();
            } else {
                throw new ArgumentsProcessingException(e);
            }
        }
    };
}
Also used : List(java.util.List) ReflectionException(ch.raffael.mddoclet.core.util.ReflectionException) Parameter(java.lang.reflect.Parameter) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) ReflectionException(ch.raffael.mddoclet.core.util.ReflectionException) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) Parameter(java.lang.reflect.Parameter)

Example 59 with Parameter

use of java.lang.reflect.Parameter in project spring-data-commons by spring-projects.

the class TypeDiscoverer method getParameterTypes.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.util.TypeInformation#getParameterTypes(java.lang.reflect.Constructor)
	 */
public List<TypeInformation<?>> getParameterTypes(Constructor<?> constructor) {
    Assert.notNull(constructor, "Constructor must not be null!");
    List<TypeInformation<?>> parameterTypes = new ArrayList<>(constructor.getParameterCount());
    for (Parameter parameter : constructor.getParameters()) {
        parameterTypes.add(createInfo(parameter.getParameterizedType()));
    }
    return parameterTypes;
}
Also used : Parameter(java.lang.reflect.Parameter)

Example 60 with Parameter

use of java.lang.reflect.Parameter in project spring-native by spring-projects-experimental.

the class ParameterWriter method writeParameterValue.

private void writeParameterValue(Builder code, Object value, Supplier<ResolvableType> parameterTypeSupplier) {
    if (value == null) {
        code.add("null");
        return;
    }
    ResolvableType parameterType = parameterTypeSupplier.get();
    if (parameterType.isArray()) {
        code.add("new $T { ", parameterType.toClass());
        code.add(writeAll(Arrays.asList(ObjectUtils.toObjectArray(value)), (item) -> parameterType.getComponentType()));
        code.add(" }");
    } else if (value instanceof List) {
        List<?> list = (List<?>) value;
        if (list.isEmpty()) {
            code.add("$T.emptyList()", Collections.class);
        } else {
            Class<?> listType = (value instanceof ManagedList ? ManagedList.class : List.class);
            code.add("$T.of(", listType);
            ResolvableType collectionType = parameterType.as(List.class).getGenerics()[0];
            code.add(writeAll(list, (item) -> collectionType));
            code.add(")");
        }
    } else if (value instanceof Set) {
        Set<?> set = (Set<?>) value;
        if (set.isEmpty()) {
            code.add("$T.emptySet()", Collections.class);
        } else {
            Class<?> setType = (value instanceof ManagedSet ? ManagedSet.class : Set.class);
            code.add("$T.of(", setType);
            ResolvableType collectionType = parameterType.as(Set.class).getGenerics()[0];
            code.add(writeAll(set, (item) -> collectionType));
            code.add(")");
        }
    } else if (value instanceof Map) {
        Map<?, ?> map = (Map<?, ?>) value;
        if (map.size() <= 10) {
            code.add("$T.of(", Map.class);
            List<Object> parameters = new ArrayList<>();
            map.forEach((mapKey, mapValue) -> {
                parameters.add(mapKey);
                parameters.add(mapValue);
            });
            code.add(writeAll(parameters, ResolvableType::forInstance));
            code.add(")");
        }
    } else if (value instanceof Character) {
        String result = '\'' + characterLiteralWithoutSingleQuotes((Character) value) + '\'';
        code.add(result);
    } else if (isPrimitiveOrWrapper(value)) {
        code.add("$L", value);
    } else if (value instanceof String) {
        code.add("$S", value);
    } else if (value instanceof Enum) {
        Enum<?> enumValue = (Enum<?>) value;
        code.add("$T.$N", enumValue.getClass(), enumValue.name());
    } else if (value instanceof Class) {
        code.add("$T.class", value);
    } else if (value instanceof ResolvableType) {
        code.add(this.typeWriter.generateTypeFor((ResolvableType) value));
    } else if (value instanceof BeanDefinition) {
        this.innerBeanDefinitionWriter.accept((BeanDefinition) value, code);
    } else if (value instanceof BeanReference) {
        code.add("new $T($S)", RuntimeBeanReference.class, ((BeanReference) value).getBeanName());
    } else {
        throw new IllegalArgumentException("Parameter of type " + parameterType + " is not supported");
    }
}
Also used : Arrays(java.util.Arrays) BeanReference(org.springframework.beans.factory.config.BeanReference) Builder(com.squareup.javapoet.CodeBlock.Builder) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ObjectUtils(org.springframework.util.ObjectUtils) Set(java.util.Set) ManagedSet(org.springframework.beans.factory.support.ManagedSet) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ManagedList(org.springframework.beans.factory.support.ManagedList) List(java.util.List) Parameter(java.lang.reflect.Parameter) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Executable(java.lang.reflect.Executable) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ResolvableType(org.springframework.core.ResolvableType) Collections(java.util.Collections) CodeBlock(com.squareup.javapoet.CodeBlock) ManagedSet(org.springframework.beans.factory.support.ManagedSet) Set(java.util.Set) ManagedSet(org.springframework.beans.factory.support.ManagedSet) ManagedList(org.springframework.beans.factory.support.ManagedList) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ArrayList(java.util.ArrayList) ManagedList(org.springframework.beans.factory.support.ManagedList) List(java.util.List) BeanReference(org.springframework.beans.factory.config.BeanReference) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ResolvableType(org.springframework.core.ResolvableType) Collections(java.util.Collections) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Map(java.util.Map)

Aggregations

Parameter (java.lang.reflect.Parameter)766 Method (java.lang.reflect.Method)277 ArrayList (java.util.ArrayList)142 Annotation (java.lang.annotation.Annotation)95 List (java.util.List)91 Type (java.lang.reflect.Type)77 HashMap (java.util.HashMap)76 Map (java.util.Map)73 Constructor (java.lang.reflect.Constructor)61 Test (org.junit.jupiter.api.Test)57 Arrays (java.util.Arrays)54 Executable (java.lang.reflect.Executable)51 InvocationTargetException (java.lang.reflect.InvocationTargetException)50 Field (java.lang.reflect.Field)46 Collectors (java.util.stream.Collectors)46 ParameterizedType (java.lang.reflect.ParameterizedType)43 Optional (java.util.Optional)43 Test (org.junit.Test)42 Set (java.util.Set)38 Modifier (java.lang.reflect.Modifier)30