Search in sources :

Example 61 with Parameter

use of java.lang.reflect.Parameter in project springframework-source-5.1.x by wb02125055.

the class StandardReflectionParameterNameDiscoverer method getParameterNames.

@Nullable
private String[] getParameterNames(Parameter[] parameters) {
    String[] parameterNames = new String[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
        Parameter param = parameters[i];
        if (!param.isNamePresent()) {
            return null;
        }
        parameterNames[i] = param.getName();
    }
    return parameterNames;
}
Also used : Parameter(java.lang.reflect.Parameter) Nullable(org.springframework.lang.Nullable)

Example 62 with Parameter

use of java.lang.reflect.Parameter in project springframework-source-5.1.x by wb02125055.

the class MethodParameter method findParameterIndex.

protected static int findParameterIndex(Parameter parameter) {
    Executable executable = parameter.getDeclaringExecutable();
    Parameter[] allParams = executable.getParameters();
    // Try first with identity checks for greater performance.
    for (int i = 0; i < allParams.length; i++) {
        if (parameter == allParams[i]) {
            return i;
        }
    }
    // conditions while invoking java.lang.reflect.Executable.getParameters().
    for (int i = 0; i < allParams.length; i++) {
        if (parameter.equals(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 63 with Parameter

use of java.lang.reflect.Parameter in project springframework-source-5.1.x by wb02125055.

the class MethodParameter method getParameter.

/**
 * Return the {@link Parameter} descriptor for method/constructor parameter.
 * @since 5.0
 */
public Parameter getParameter() {
    if (this.parameterIndex < 0) {
        throw new IllegalStateException("Cannot retrieve Parameter descriptor for method return type");
    }
    Parameter parameter = this.parameter;
    if (parameter == null) {
        parameter = getExecutable().getParameters()[this.parameterIndex];
        this.parameter = parameter;
    }
    return parameter;
}
Also used : KParameter(kotlin.reflect.KParameter) Parameter(java.lang.reflect.Parameter)

Example 64 with Parameter

use of java.lang.reflect.Parameter in project VMF by miho.

the class DelegationInfo method newInstance.

public static DelegationInfo newInstance(Model model, Method m, DelegationInfo constructorDelegationInfo) {
    DelegateTo delegation = m.getAnnotation(DelegateTo.class);
    String className = delegation == null ? (constructorDelegationInfo == null ? "" : constructorDelegationInfo.fullTypeName) : delegation.className();
    // if(delegation==null) {
    // return null;
    // }
    List<String> paramTypes = new ArrayList<>(m.getParameters().length);
    List<String> paramNames = new ArrayList<>(m.getParameters().length);
    boolean delegated = !m.getName().startsWith("get") && !m.getName().startsWith("is");
    boolean varargs = m.isVarArgs();
    for (Parameter p : m.getParameters()) {
        paramTypes.add(TypeUtil.getTypeAsString(model, p.getType(), delegated, varargs));
        paramNames.add(p.getName());
    }
    Doc doc = m.getAnnotation(Doc.class);
    String customDocumentation;
    if (doc != null) {
        customDocumentation = doc.value();
    } else {
        customDocumentation = "";
    }
    return newInstance(className, m.getName(), TypeUtil.getReturnTypeAsString(model, m), paramTypes, paramNames, false, customDocumentation);
}
Also used : ArrayList(java.util.ArrayList) Parameter(java.lang.reflect.Parameter)

Example 65 with Parameter

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

the class GenerateAllLogUsages method main.

public static void main(String[] args) {
    println("package io.quarkus.logging;");
    println("");
    println("// generated by GenerateAllLogUsages, don't update manually");
    println("public class GeneratedBean {");
    println("    public void testLogging() {");
    Arrays.stream(Log.class.getDeclaredMethods()).filter(method -> Modifier.isPublic(method.getModifiers())).sorted(Comparator.comparing(Method::getName).thenComparingInt(Method::getParameterCount)).forEach(method -> {
        Parameter[] parameters = method.getParameters();
        String[] arguments = new String[method.getParameterCount()];
        for (int i = 0; i < parameters.length; i++) {
            Parameter parameter = parameters[i];
            String argument;
            if (parameter.getType() == Object.class && "message".equals(parameter.getName())) {
                argument = "\"foobar\"";
            } else if (parameter.getType() == Object.class && (parameter.getName().startsWith("param") || parameter.getName().startsWith("arg"))) {
                argument = "new Object()";
            } else if (parameter.getType() == Object[].class && "params".equals(parameter.getName())) {
                argument = "new Object[] {}";
            } else if (parameter.getType() == String.class && "loggerFqcn".equals(parameter.getName())) {
                argument = "\"io.quarkus.logging.GeneratedBean\"";
            } else if (parameter.getType() == String.class && "format".equals(parameter.getName())) {
                argument = "\"foobar\"";
            } else if (parameter.getType() == int.class) {
                argument = "13";
            } else if (parameter.getType() == long.class) {
                argument = "42L";
            } else if (parameter.getType() == Throwable.class) {
                argument = "new NoStackTraceTestException()";
            } else if (parameter.getType() == Logger.Level.class) {
                argument = "org.jboss.logging.Logger.Level.INFO";
            } else {
                throw new RuntimeException("Couldn't generate argument value for parameter " + i + " of type " + method.getParameterTypes()[i] + ": " + method);
            }
            arguments[i] = argument;
        }
        println("        Log." + method.getName() + "(" + String.join(", ", arguments) + ");");
    });
    println("    }");
    println("}");
}
Also used : Parameter(java.lang.reflect.Parameter) Method(java.lang.reflect.Method) Logger(org.jboss.logging.Logger)

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