Search in sources :

Example 1 with Parameter

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

the class MethodSignatureCompiler method signatureFor.

public List<FieldSignature> signatureFor(Method method) throws ProcedureException {
    Parameter[] params = method.getParameters();
    Type[] types = method.getGenericParameterTypes();
    List<FieldSignature> signature = new ArrayList<>(params.length);
    boolean seenDefault = false;
    for (int i = 0; i < params.length; i++) {
        Parameter param = params[i];
        Type type = types[i];
        if (!param.isAnnotationPresent(Name.class)) {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Argument at position %d in method `%s` is missing an `@%s` annotation.%n" + "Please add the annotation, recompile the class and try again.", i, method.getName(), Name.class.getSimpleName());
        }
        Name parameter = param.getAnnotation(Name.class);
        String name = parameter.value();
        if (name.trim().length() == 0) {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Argument at position %d in method `%s` is annotated with a name,%n" + "but the name is empty, please provide a non-empty name for the argument.", i, method.getName());
        }
        try {
            NeoValueConverter valueConverter = typeMappers.converterFor(type);
            Optional<Neo4jValue> defaultValue = valueConverter.defaultValue(parameter);
            //it is not allowed to have holes in default values
            if (seenDefault && !defaultValue.isPresent()) {
                throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Non-default argument at position %d with name %s in method %s follows default argument. " + "Add a default value or rearrange arguments so that the non-default values comes first.", i, parameter.value(), method.getName());
            }
            seenDefault = defaultValue.isPresent();
            signature.add(new FieldSignature(name, valueConverter.type(), defaultValue));
        } catch (ProcedureException e) {
            throw new ProcedureException(e.status(), "Argument `%s` at position %d in `%s` with%n" + "type `%s` cannot be converted to a Neo4j type: %s", name, i, method.getName(), param.getType().getSimpleName(), e.getMessage());
        }
    }
    return signature;
}
Also used : NeoValueConverter(org.neo4j.kernel.impl.proc.TypeMappers.NeoValueConverter) ArrayList(java.util.ArrayList) FieldSignature(org.neo4j.kernel.api.proc.FieldSignature) Name(org.neo4j.procedure.Name) Type(java.lang.reflect.Type) Parameter(java.lang.reflect.Parameter) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException)

Example 2 with Parameter

use of java.lang.reflect.Parameter in project Synthese_2BIN by TheYoungSensei.

the class Controleur method chercherConstruteurs.

private List<Propriete> chercherConstruteurs(Class cls) {
    List<Propriete> constructeurs = new ArrayList<Propriete>();
    for (Constructor constructeur : cls.getDeclaredConstructors()) {
        Visibilite visibilite = chercherVisibilite(constructeur.getModifiers());
        boolean estStatique = false;
        if (Modifier.isStatic(constructeur.getModifiers()))
            estStatique = true;
        String nom = constructeur.getName();
        String ex = chercherExceptions(constructeur.getExceptionTypes());
        Parameter[] parameters = constructeur.getParameters();
        nom += " (";
        nom += chercherParametres(parameters);
        nom += ") " + ex;
        Propriete prop = new Propriete(nom, estStatique, Modifier.isAbstract(constructeur.getModifiers()), visibilite);
        constructeurs.add(prop);
    }
    return constructeurs;
}
Also used : Constructor(java.lang.reflect.Constructor) Visibilite(gimme.domaine.Visibilite) ArrayList(java.util.ArrayList) Parameter(java.lang.reflect.Parameter) Propriete(gimme.domaine.Propriete)

Example 3 with Parameter

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

the class SpringExtension method supports.

/**
	 * Determine if the value for the {@link Parameter} in the supplied {@link ParameterContext}
	 * should be autowired from the test's {@link ApplicationContext}.
	 * <p>Returns {@code true} if the parameter is declared in a {@link Constructor}
	 * that is annotated with {@link Autowired @Autowired} and otherwise delegates to
	 * {@link ParameterAutowireUtils#isAutowirable}.
	 * <p><strong>WARNING</strong>: If the parameter is declared in a {@code Constructor}
	 * that is annotated with {@code @Autowired}, Spring will assume the responsibility
	 * for resolving all parameters in the constructor. Consequently, no other registered
	 * {@link ParameterResolver} will be able to resolve parameters.
	 * @see #resolve
	 * @see ParameterAutowireUtils#isAutowirable
	 */
@Override
public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) {
    Parameter parameter = parameterContext.getParameter();
    Executable executable = parameter.getDeclaringExecutable();
    return (executable instanceof Constructor && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) || ParameterAutowireUtils.isAutowirable(parameter);
}
Also used : Autowired(org.springframework.beans.factory.annotation.Autowired) Constructor(java.lang.reflect.Constructor) Parameter(java.lang.reflect.Parameter) Executable(java.lang.reflect.Executable)

Example 4 with Parameter

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

the class MethodParameter method findParameterIndex.

protected static int findParameterIndex(Parameter parameter) {
    Executable executable = parameter.getDeclaringExecutable();
    Parameter[] allParams = executable.getParameters();
    for (int i = 0; i < allParams.length; i++) {
        if (parameter == allParams[i]) {
            return i;
        }
    }
    throw new IllegalArgumentException("Given parameter [" + parameter + "] does not match any parameter in the declaring executable");
}
Also used : KParameter(kotlin.reflect.KParameter) Parameter(java.lang.reflect.Parameter) Executable(java.lang.reflect.Executable)

Example 5 with Parameter

use of java.lang.reflect.Parameter in project felix by apache.

the class Helpers method getLambdaParameterName.

/**
 * Extracts the first parameter of a lambda.
 */
public static String getLambdaParameterName(SerializableLambda lambda, int index) {
    SerializedLambda serialized = getSerializedLambda(lambda);
    Method m = getLambdaMethod(serialized, lambda.getClass().getClassLoader());
    Parameter p = m.getParameters()[index];
    if (Objects.equals("arg0", p.getName())) {
        throw new IllegalStateException("Can'f find lambda method name (Please check you are using javac -parameters option).");
    }
    return p.getName();
}
Also used : Parameter(java.lang.reflect.Parameter) Method(java.lang.reflect.Method) SerializedLambda(java.lang.invoke.SerializedLambda)

Aggregations

Parameter (java.lang.reflect.Parameter)174 Method (java.lang.reflect.Method)77 ArrayList (java.util.ArrayList)33 Annotation (java.lang.annotation.Annotation)26 Type (java.lang.reflect.Type)21 List (java.util.List)21 Test (org.junit.Test)18 Constructor (java.lang.reflect.Constructor)17 Map (java.util.Map)11 Executable (java.lang.reflect.Executable)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)10 PathParam (javax.ws.rs.PathParam)9 IOException (java.io.IOException)8 Arrays (java.util.Arrays)8 HashMap (java.util.HashMap)8 FormParameter (io.swagger.models.parameters.FormParameter)7 Optional (java.util.Optional)7 Collectors (java.util.stream.Collectors)7 SqlStatementCustomizerFactory (org.jdbi.v3.sqlobject.customizer.SqlStatementCustomizerFactory)7 SqlStatementParameterCustomizer (org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer)7