Java Code Examples for java.lang.reflect.Parameter#getDeclaringExecutable()

The following examples show how to use java.lang.reflect.Parameter#getDeclaringExecutable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MethodParameter.java    From java-technology-stack with MIT License 6 votes vote down vote up
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;
		}
	}
	// Potentially try again with object equality checks in order to avoid race
	// 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");
}
 
Example 2
Source File: DefaultInjectorResources.java    From panda with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<InjectorResourceHandler<Annotation, Object, ?>> getHandler(Parameter parameter) {
    Executable executable = parameter.getDeclaringExecutable();
    Annotation[] annotations = fetchAnnotations(parameter);

    Collection<InjectorResourceHandler<Annotation, Object, ?>> matched = new ArrayList<>(executable.getParameterCount());
    add(matched, new HandlerRecord(parameter.getType(), null));

    for (Annotation annotation : annotations) {
        add(matched, new HandlerRecord(parameter.getType(), null));
        add(matched, new HandlerRecord(parameter.getType(), annotation.annotationType()));
        add(matched, new HandlerRecord(null, annotation.annotationType()));
    }

    return matched;
}
 
Example 3
Source File: MethodParameter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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;
		}
	}
	// Potentially try again with object equality checks in order to avoid race
	// 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");
}
 
Example 4
Source File: MethodParameterFactory.java    From spring-test-junit5 with Apache License 2.0 5 votes vote down vote up
/**
 * Create a standard {@link MethodParameter} from the supplied {@link Parameter}.
 * <p>Supports parameters declared in methods and constructors.
 * @param parameter the parameter to create a {@code MethodParameter} for;
 * never {@code null}
 * @return a new {@code MethodParameter}
 * @see #createSynthesizingMethodParameter(Parameter)
 */
public static MethodParameter createMethodParameter(Parameter parameter) {
	Assert.notNull(parameter, "Parameter must not be null");
	Executable executable = parameter.getDeclaringExecutable();
	if (executable instanceof Method) {
		return new MethodParameter((Method) executable, getIndex(parameter));
	}
	// else
	return new MethodParameter((Constructor<?>) executable, getIndex(parameter));
}
 
Example 5
Source File: MethodParameterFactory.java    From tutorials with MIT License 5 votes vote down vote up
private static int getIndex(Parameter parameter) {
    Assert.notNull(parameter, "Parameter must not be null");
    Executable executable = parameter.getDeclaringExecutable();
    Parameter[] parameters = executable.getParameters();
    for (int i = 0; i < parameters.length; i++) {
        if (parameters[i] == parameter) {
            return i;
        }
    }
    throw new IllegalStateException(String.format("Failed to resolve index of parameter [%s] in executable [%s]", parameter, executable.toGenericString()));
}
 
Example 6
Source File: MethodParameterFactory.java    From tutorials with MIT License 5 votes vote down vote up
public static SynthesizingMethodParameter createSynthesizingMethodParameter(Parameter parameter) {
    Assert.notNull(parameter, "Parameter must not be null");
    Executable executable = parameter.getDeclaringExecutable();
    if (executable instanceof Method) {
        return new SynthesizingMethodParameter((Method) executable, getIndex(parameter));
    }
    throw new UnsupportedOperationException("Cannot create a SynthesizingMethodParameter for a constructor parameter: " + parameter);
}
 
Example 7
Source File: MethodParameterFactory.java    From tutorials with MIT License 5 votes vote down vote up
public static MethodParameter createMethodParameter(Parameter parameter) {
    Assert.notNull(parameter, "Parameter must not be null");
    Executable executable = parameter.getDeclaringExecutable();
    if (executable instanceof Method) {
        return new MethodParameter((Method) executable, getIndex(parameter));
    }
    return new MethodParameter((Constructor<?>) executable, getIndex(parameter));
}
 
Example 8
Source File: ParameterAutowireUtils.java    From tutorials with MIT License 5 votes vote down vote up
public static Object resolveDependency(Parameter parameter, Class<?> containingClass, ApplicationContext applicationContext) {

        boolean required = findMergedAnnotation(parameter, Autowired.class).map(Autowired::required)
            .orElse(true);
        MethodParameter methodParameter = (parameter.getDeclaringExecutable() instanceof Method ? MethodParameterFactory.createSynthesizingMethodParameter(parameter) : MethodParameterFactory.createMethodParameter(parameter));
        DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
        descriptor.setContainingClass(containingClass);

        return applicationContext.getAutowireCapableBeanFactory()
            .resolveDependency(descriptor, null);
    }
 
Example 9
Source File: AnnotatedServiceFactory.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the description of the specified {@link AnnotatedElement}.
 */
@Nullable
static String findDescription(AnnotatedElement annotatedElement) {
    requireNonNull(annotatedElement, "annotatedElement");
    final Description description = AnnotationUtil.findFirst(annotatedElement, Description.class);
    if (description != null) {
        final String value = description.value();
        if (DefaultValues.isSpecified(value)) {
            checkArgument(!value.isEmpty(), "value is empty.");
            return value;
        }
    } else if (annotatedElement instanceof Parameter) {
        // JavaDoc/KDoc descriptions only exist for method parameters
        final Parameter parameter = (Parameter) annotatedElement;
        final Executable executable = parameter.getDeclaringExecutable();
        final Class<?> clazz = executable.getDeclaringClass();
        final String fileName = getFileName(clazz.getCanonicalName());
        final String propertyName = executable.getName() + '.' + parameter.getName();
        final Properties cachedProperties = DOCUMENTATION_PROPERTIES_CACHE.getIfPresent(fileName);
        if (cachedProperties != null) {
            return cachedProperties.getProperty(propertyName);
        }
        try (InputStream stream = AnnotatedServiceFactory.class.getClassLoader()
                                                               .getResourceAsStream(fileName)) {
            if (stream == null) {
                return null;
            }
            final Properties properties = new Properties();
            properties.load(stream);
            DOCUMENTATION_PROPERTIES_CACHE.put(fileName, properties);
            return properties.getProperty(propertyName);
        } catch (IOException exception) {
            logger.warn("Failed to load an API description file: {}", fileName, exception);
        }
    }
    return null;
}
 
Example 10
Source File: MethodParameterFactory.java    From spring-test-junit5 with Apache License 2.0 5 votes vote down vote up
private static int getIndex(Parameter parameter) {
	Assert.notNull(parameter, "Parameter must not be null");
	Executable executable = parameter.getDeclaringExecutable();
	Parameter[] parameters = executable.getParameters();
	for (int i = 0; i < parameters.length; i++) {
		if (parameters[i] == parameter) {
			return i;
		}
	}
	throw new IllegalStateException(String.format("Failed to resolve index of parameter [%s] in executable [%s]",
		parameter, executable.toGenericString()));
}
 
Example 11
Source File: MethodParameterFactory.java    From spring-test-junit5 with Apache License 2.0 5 votes vote down vote up
/**
 * Create a {@link SynthesizingMethodParameter} from the supplied {@link Parameter}.
 * <p>Supports parameters declared in methods.
 * @param parameter the parameter to create a {@code SynthesizingMethodParameter}
 * for; never {@code null}
 * @return a new {@code SynthesizingMethodParameter}
 * @throws UnsupportedOperationException if the supplied parameter is declared
 * in a constructor
 * @see #createMethodParameter(Parameter)
 */
public static SynthesizingMethodParameter createSynthesizingMethodParameter(Parameter parameter) {
	Assert.notNull(parameter, "Parameter must not be null");
	Executable executable = parameter.getDeclaringExecutable();
	if (executable instanceof Method) {
		return new SynthesizingMethodParameter((Method) executable, getIndex(parameter));
	}
	// else
	throw new UnsupportedOperationException(
		"Cannot create a SynthesizingMethodParameter for a constructor parameter: " + parameter);
}
 
Example 12
Source File: FuzzStatement.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private ParameterTypeContext createParameterTypeContext(Parameter parameter) {
    Executable exec = parameter.getDeclaringExecutable();
    String declarerName = exec.getDeclaringClass().getName() + '.' + exec.getName();
    return new ParameterTypeContext(
                    parameter.getName(),
                    parameter.getAnnotatedType(),
                    declarerName,
                    typeVariables)
                    .allowMixedTypes(true).annotate(parameter);
}
 
Example 13
Source File: NamedWrap.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
private AccessibleObject getFirstAccessibleObject() {
    AnnotatedElement member = this.getMembers()[0];
    if (member instanceof Parameter) {
        Parameter parameter = (Parameter) member;
        return parameter.getDeclaringExecutable();
    } else if (member instanceof Field) {
        Field field = (Field) member;
        return field;
    } else {
        return (Method) member;
    }
}
 
Example 14
Source File: SpringExtension.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
    Parameter parameter = parameterContext.getParameter();
    Executable executable = parameter.getDeclaringExecutable();
    return ((executable instanceof Constructor) && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) || ParameterAutowireUtils.isAutowirable(parameter);
}
 
Example 15
Source File: ParameterAutowireUtils.java    From spring-test-junit5 with Apache License 2.0 3 votes vote down vote up
/**
 * Resolve the dependency for the supplied {@link Parameter} from the
 * supplied {@link ApplicationContext}.
 * <p>Provides comprehensive autowiring support for individual method parameters
 * on par with Spring's dependency injection facilities for autowired fields and
 * methods, including support for {@link Autowired @Autowired},
 * {@link Qualifier @Qualifier}, and {@link Value @Value} with support for property
 * placeholders and SpEL expressions in {@code @Value} declarations.
 * <p>The dependency is required unless the parameter is annotated with
 * {@link Autowired @Autowired} with the {@link Autowired#required required}
 * flag set to {@code false}.
 * <p>If an explicit <em>qualifier</em> is not declared, the name of the parameter
 * will be used as the qualifier for resolving ambiguities.
 * @param parameter the parameter whose dependency should be resolved
 * @param containingClass the concrete class that contains the parameter; this may
 * differ from the class that declares the parameter in that it may be a subclass
 * thereof, potentially substituting type variables
 * @param applicationContext the application context from which to resolve the
 * dependency
 * @return the resolved object, or {@code null} if none found
 * @throws BeansException if dependency resolution failed
 * @see #isAutowirable(Parameter)
 * @see Autowired#required
 * @see MethodParameterFactory#createSynthesizingMethodParameter(Parameter)
 * @see AutowireCapableBeanFactory#resolveDependency(DependencyDescriptor, String)
 */
public static Object resolveDependency(Parameter parameter, Class<?> containingClass,
		ApplicationContext applicationContext) {

	boolean required = findMergedAnnotation(parameter, Autowired.class).map(Autowired::required).orElse(true);
	MethodParameter methodParameter = (parameter.getDeclaringExecutable() instanceof Method
			? MethodParameterFactory.createSynthesizingMethodParameter(parameter)
			: MethodParameterFactory.createMethodParameter(parameter));
	DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
	descriptor.setContainingClass(containingClass);

	return applicationContext.getAutowireCapableBeanFactory().resolveDependency(descriptor, null);
}
 
Example 16
Source File: SpringExtension.java    From spring-test-junit5 with Apache License 2.0 3 votes vote down vote up
/**
 * 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 #resolveParameter
 * @see ParameterAutowireUtils#isAutowirable
 */
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
	Parameter parameter = parameterContext.getParameter();
	Executable executable = parameter.getDeclaringExecutable();
	return (executable instanceof Constructor &&
			AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) ||
			ParameterAutowireUtils.isAutowirable(parameter);
}
 
Example 17
Source File: ParameterAutowireUtils.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Due to a bug in {@code javac} on JDK versions prior to JDK 9, looking up
 * annotations directly on a {@link Parameter} will fail for inner class
 * constructors.
 * <h4>Bug in javac in JDK &lt; 9</h4>
 * <p>The parameter annotations array in the compiled byte code excludes an entry
 * for the implicit <em>enclosing instance</em> parameter for an inner class
 * constructor.
 * <h4>Workaround</h4>
 * <p>This method provides a workaround for this off-by-one error by allowing the
 * caller to access annotations on the preceding {@link Parameter} object (i.e.,
 * {@code index - 1}). If the supplied {@code index} is zero, this method returns
 * an empty {@code AnnotatedElement}.
 * <h4>WARNING</h4>
 * <p>The {@code AnnotatedElement} returned by this method should never be cast and
 * treated as a {@code Parameter} since the metadata (e.g., {@link Parameter#getName()},
 * {@link Parameter#getType()}, etc.) will not match those for the declared parameter
 * at the given index in an inner class constructor.
 * @return the supplied {@code parameter} or the <em>effective</em> {@code Parameter}
 * if the aforementioned bug is in effect
 */
private static AnnotatedElement getEffectiveAnnotatedParameter(Parameter parameter, int index) {
	Executable executable = parameter.getDeclaringExecutable();
	if (executable instanceof Constructor && ClassUtils.isInnerClass(executable.getDeclaringClass()) &&
			executable.getParameterAnnotations().length == executable.getParameterCount() - 1) {
		// Bug in javac in JDK <9: annotation array excludes enclosing instance parameter
		// for inner classes, so access it with the actual parameter index lowered by 1
		return (index == 0 ? EMPTY_ANNOTATED_ELEMENT : executable.getParameters()[index - 1]);
	}
	return parameter;
}
 
Example 18
Source File: SpringExtension.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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 #resolveParameter
 * @see ParameterAutowireUtils#isAutowirable
 */
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
	Parameter parameter = parameterContext.getParameter();
	int index = parameterContext.getIndex();
	Executable executable = parameter.getDeclaringExecutable();
	return (executable instanceof Constructor &&
			AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) ||
			ParameterAutowireUtils.isAutowirable(parameter, index);
}
 
Example 19
Source File: ParameterResolutionDelegate.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Due to a bug in {@code javac} on JDK versions prior to JDK 9, looking up
 * annotations directly on a {@link Parameter} will fail for inner class
 * constructors.
 * <h4>Bug in javac in JDK &lt; 9</h4>
 * <p>The parameter annotations array in the compiled byte code excludes an entry
 * for the implicit <em>enclosing instance</em> parameter for an inner class
 * constructor.
 * <h4>Workaround</h4>
 * <p>This method provides a workaround for this off-by-one error by allowing the
 * caller to access annotations on the preceding {@link Parameter} object (i.e.,
 * {@code index - 1}). If the supplied {@code index} is zero, this method returns
 * an empty {@code AnnotatedElement}.
 * <h4>WARNING</h4>
 * <p>The {@code AnnotatedElement} returned by this method should never be cast and
 * treated as a {@code Parameter} since the metadata (e.g., {@link Parameter#getName()},
 * {@link Parameter#getType()}, etc.) will not match those for the declared parameter
 * at the given index in an inner class constructor.
 * @return the supplied {@code parameter} or the <em>effective</em> {@code Parameter}
 * if the aforementioned bug is in effect
 */
private static AnnotatedElement getEffectiveAnnotatedParameter(Parameter parameter, int index) {
	Executable executable = parameter.getDeclaringExecutable();
	if (executable instanceof Constructor && ClassUtils.isInnerClass(executable.getDeclaringClass()) &&
			executable.getParameterAnnotations().length == executable.getParameterCount() - 1) {
		// Bug in javac in JDK <9: annotation array excludes enclosing instance parameter
		// for inner classes, so access it with the actual parameter index lowered by 1
		return (index == 0 ? EMPTY_ANNOTATED_ELEMENT : executable.getParameters()[index - 1]);
	}
	return parameter;
}
 
Example 20
Source File: SpringExtension.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Determine if the value for the {@link Parameter} in the supplied {@link ParameterContext}
 * should be autowired from the test's {@link ApplicationContext}.
 * <p>A parameter is considered to be autowirable if one of the following
 * conditions is {@code true}.
 * <ol>
 * <li>The {@linkplain ParameterContext#getDeclaringExecutable() declaring
 * executable} is a {@link Constructor} and
 * {@link TestConstructorUtils#isAutowirableConstructor(Constructor, Class)}
 * returns {@code true}.</li>
 * <li>The parameter is of type {@link ApplicationContext} or a sub-type thereof.</li>
 * <li>{@link ParameterResolutionDelegate#isAutowirable} returns {@code true}.</li>
 * </ol>
 * <p><strong>WARNING</strong>: If a test class {@code Constructor} is annotated
 * with {@code @Autowired} or automatically autowirable (see {@link TestConstructor}),
 * 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 #resolveParameter
 * @see TestConstructorUtils#isAutowirableConstructor(Constructor, Class)
 * @see ParameterResolutionDelegate#isAutowirable
 */
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
	Parameter parameter = parameterContext.getParameter();
	Executable executable = parameter.getDeclaringExecutable();
	Class<?> testClass = extensionContext.getRequiredTestClass();
	return (TestConstructorUtils.isAutowirableConstructor(executable, testClass) ||
			ApplicationContext.class.isAssignableFrom(parameter.getType()) ||
			ParameterResolutionDelegate.isAutowirable(parameter, parameterContext.getIndex()));
}