Search in sources :

Example 26 with Parameter

use of java.lang.reflect.Parameter in project SilverKing by Morgan-Stanley.

the class CPPWrapperGenerator method getMethodSignature.

private String getMethodSignature(Method m, Mode mode) {
    StringBuffer sb;
    boolean isFirst;
    sb = new StringBuffer();
    isFirst = true;
    for (Parameter p : m.getParameters()) {
        if (!isFirst) {
            sb.append(", ");
        }
        sb.append(p.getType().getName());
        sb.append(' ');
        sb.append(p.getName());
        isFirst = false;
    }
    return sb.toString();
}
Also used : Parameter(java.lang.reflect.Parameter)

Example 27 with Parameter

use of java.lang.reflect.Parameter in project jdbi by jdbi.

the class ConstructorMapper method paramName.

private static String paramName(Parameter[] parameters, int position, ConstructorProperties parameterNames) {
    final Parameter parameter = parameters[position];
    ColumnName dbName = parameter.getAnnotation(ColumnName.class);
    if (dbName != null) {
        return dbName.value();
    }
    if (parameterNames != null) {
        return parameterNames.value()[position];
    }
    return parameter.getName();
}
Also used : Parameter(java.lang.reflect.Parameter)

Example 28 with Parameter

use of java.lang.reflect.Parameter in project wildfly-swarm by wildfly-swarm.

the class BuilderImpl 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 defintion on type " + typeDef);
            }
        }
    }
    // invalid parameter
    Path classPathAnno = typeDef.getAnnotation(Path.class);
    final Set<String> classLevelVariables = new HashSet<>();
    ResteasyUriBuilder classTemplate = null;
    if (classPathAnno != null) {
        classTemplate = (ResteasyUriBuilder) UriBuilder.fromUri(classPathAnno.value());
        classLevelVariables.addAll(classTemplate.getPathParamNamesInDeclarationOrder());
    }
    ResteasyUriBuilder template;
    for (Method method : methods) {
        Path methodPathAnno = method.getAnnotation(Path.class);
        if (methodPathAnno != null) {
            template = classPathAnno == null ? (ResteasyUriBuilder) UriBuilder.fromUri(methodPathAnno.value()) : (ResteasyUriBuilder) UriBuilder.fromUri(classPathAnno.value() + "/" + methodPathAnno.value());
        } else {
            template = classTemplate;
        }
        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");
            }
        }
        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(javax.ws.rs.Path) HashMap(java.util.HashMap) HttpMethod(javax.ws.rs.HttpMethod) Method(java.lang.reflect.Method) RestClientDefinitionException(org.eclipse.microprofile.rest.client.RestClientDefinitionException) Annotation(java.lang.annotation.Annotation) ResteasyUriBuilder(org.jboss.resteasy.specimpl.ResteasyUriBuilder) Parameter(java.lang.reflect.Parameter) PathParam(javax.ws.rs.PathParam) HashSet(java.util.HashSet)

Example 29 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) Test(org.junit.Test)

Example 30 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)

Aggregations

Parameter (java.lang.reflect.Parameter)165 Method (java.lang.reflect.Method)74 ArrayList (java.util.ArrayList)31 Annotation (java.lang.annotation.Annotation)25 Type (java.lang.reflect.Type)21 List (java.util.List)20 Test (org.junit.Test)18 Constructor (java.lang.reflect.Constructor)16 Map (java.util.Map)11 Executable (java.lang.reflect.Executable)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 HashMap (java.util.HashMap)9 PathParam (javax.ws.rs.PathParam)9 FormParameter (io.swagger.models.parameters.FormParameter)7 IOException (java.io.IOException)7 Arrays (java.util.Arrays)7 SqlStatementCustomizerFactory (org.jdbi.v3.sqlobject.customizer.SqlStatementCustomizerFactory)7 SqlStatementParameterCustomizer (org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer)7 RequestPart (org.springframework.web.bind.annotation.RequestPart)7 HashSet (java.util.HashSet)6