Search in sources :

Example 36 with Parameter

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

the class DefineListFactory method createForParameter.

@Override
public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) {
    final DefineList d = (DefineList) annotation;
    final String name = ParameterUtil.findParameterName(d.value(), param).orElseThrow(() -> new UnsupportedOperationException("A @DefineList parameter was not given a name, " + "and parameter name data is not present in the class file, for: " + param.getDeclaringExecutable() + "::" + param));
    return (stmt, arg) -> {
        List<?> argsList;
        if (arg instanceof List) {
            argsList = (List<?>) arg;
        } else if (arg instanceof Object[]) {
            argsList = Arrays.asList((Object[]) arg);
        } else if (arg == null) {
            throw new IllegalArgumentException("A null object was passed as a @DefineList parameter. " + "@DefineList is only supported on List and array arguments");
        } else {
            throw new IllegalArgumentException("A " + arg.getClass() + " object was passed as a @DefineList " + "parameter. @DefineList is only supported on List and array arguments");
        }
        if (argsList.isEmpty()) {
            throw new IllegalArgumentException("An empty list was passed as a @DefineList parameter. Can't define " + "an empty attribute.");
        }
        if (argsList.contains(null)) {
            throw new IllegalArgumentException("A @DefineList parameter was passed a list with null values in it.");
        }
        stmt.defineList(name, argsList);
    };
}
Also used : Arrays(java.util.Arrays) List(java.util.List) ParameterUtil(org.jdbi.v3.sqlobject.internal.ParameterUtil) Type(java.lang.reflect.Type) Parameter(java.lang.reflect.Parameter) SqlStatementCustomizerFactory(org.jdbi.v3.sqlobject.customizer.SqlStatementCustomizerFactory) Annotation(java.lang.annotation.Annotation) SqlStatementParameterCustomizer(org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer) DefineList(org.jdbi.v3.sqlobject.customizer.DefineList) Method(java.lang.reflect.Method) DefineList(org.jdbi.v3.sqlobject.customizer.DefineList) List(java.util.List) DefineList(org.jdbi.v3.sqlobject.customizer.DefineList)

Example 37 with Parameter

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

the class BindJpaFactory method createForParameter.

@Override
public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) {
    BindJpa bind = (BindJpa) annotation;
    final String prefix;
    if (bind.value().isEmpty()) {
        prefix = "";
    } else {
        prefix = bind.value() + ".";
    }
    return (stmt, arg) -> {
        JpaClass<?> jpaClass = JpaClass.get(arg.getClass());
        for (JpaMember member : jpaClass.members()) {
            stmt.bindByType(prefix + member.getColumnName(), readMember(arg, member), member.getType());
        }
    };
}
Also used : EntityMemberAccessException(org.jdbi.v3.jpa.EntityMemberAccessException) BindJpa(org.jdbi.v3.jpa.BindJpa) Type(java.lang.reflect.Type) Parameter(java.lang.reflect.Parameter) SqlStatementCustomizerFactory(org.jdbi.v3.sqlobject.customizer.SqlStatementCustomizerFactory) Annotation(java.lang.annotation.Annotation) SqlStatementParameterCustomizer(org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) BindJpa(org.jdbi.v3.jpa.BindJpa)

Example 38 with Parameter

use of java.lang.reflect.Parameter in project commons by terran4j.

the class DsqlRepositoryProxy method getContext.

private Map<String, Object> getContext(Method method, Object[] args) throws BusinessException {
    Map<String, Object> context = new HashMap<>();
    // 没有参数的情况。
    Parameter[] params = method.getParameters();
    if (params == null || params.length == 0) {
        return context;
    }
    // 只有一个参数,并且没有 @Param 注解时,用 query 作默认的 key.
    if (params.length == 1 && params[0].getAnnotation(Param.class) == null) {
        if (args[0] != null) {
            context.put("args", args[0]);
        }
    }
    for (int i = 0; i < params.length; i++) {
        Parameter param = params[i];
        String key = param.getName();
        Param paramAnnotation = param.getAnnotation(Param.class);
        if (paramAnnotation != null) {
            key = paramAnnotation.value();
        }
        Object value = args[i];
        if (value != null) {
            context.put(key, value);
        }
    }
    return context;
}
Also used : HashMap(java.util.HashMap) Param(org.springframework.data.repository.query.Param) Parameter(java.lang.reflect.Parameter)

Example 39 with Parameter

use of java.lang.reflect.Parameter in project OA4MP by ncsa.

the class RequestFactory method convertObjectsToParameters.

public Parameter[] convertObjectsToParameters(Object[] objArray) {
    Parameter[] paramArray = new Parameter[objArray.length];
    int i = 0;
    for (Object obj : objArray) {
        try {
            Constructor<Parameter> cons = Parameter.class.getConstructor(obj.getClass());
            paramArray[i++] = cons.newInstance(obj);
        } catch (Exception e) {
            throw new IllegalArgumentException("This method can't handle objects of type: " + obj.getClass(), e);
        }
    }
    return paramArray;
}
Also used : Parameter(java.lang.reflect.Parameter) JSONObject(net.sf.json.JSONObject) NFWException(edu.uiuc.ncsa.security.core.exceptions.NFWException) GeneralException(edu.uiuc.ncsa.security.core.exceptions.GeneralException)

Example 40 with Parameter

use of java.lang.reflect.Parameter in project tutorials by eugenp.

the class SpringExtension method resolveParameter.

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
    Parameter parameter = parameterContext.getParameter();
    Class<?> testClass = extensionContext.getTestClass().get();
    ApplicationContext applicationContext = getApplicationContext(extensionContext);
    return ParameterAutowireUtils.resolveDependency(parameter, testClass, applicationContext);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) 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