use of java.lang.reflect.Executable 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);
}
use of java.lang.reflect.Executable 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");
}
use of java.lang.reflect.Executable in project bazel by bazelbuild.
the class LambdaDesugaring method queueUpBridgeMethodIfNeeded.
/**
* Makes {@link #visitEnd} generate a bridge method for the given method handle if the
* referenced method will be invisible to the generated lambda class.
*
* @return struct containing either {@code invokedMethod} or {@code invokedMethod} and a handle
* representing the bridge method that will be generated for {@code invokedMethod}.
*/
private MethodReferenceBridgeInfo queueUpBridgeMethodIfNeeded(Handle invokedMethod) throws ClassNotFoundException {
if (invokedMethod.getName().startsWith("lambda$")) {
// We adjust lambda bodies to be visible
return MethodReferenceBridgeInfo.noBridge(invokedMethod);
}
// invokedMethod is a method reference if we get here
Executable invoked = findTargetMethod(invokedMethod);
if (isVisibleToLambdaClass(invoked, invokedMethod.getOwner())) {
// Referenced method is visible to the generated class, so nothing to do
return MethodReferenceBridgeInfo.noBridge(invokedMethod);
}
// We need a bridge method if we get here
checkState(!isInterface, "%s is an interface and shouldn't need bridge to %s", internalName, invokedMethod);
checkState(!invokedMethod.isInterface(), "%s's lambda classes can't see interface method: %s", internalName, invokedMethod);
MethodReferenceBridgeInfo result = bridgeMethods.get(invokedMethod);
if (result != null) {
// we're already queued up a bridge method for this method reference
return result;
}
String name = uniqueInPackage(internalName, "bridge$lambda$" + bridgeMethods.size());
Handle bridgeMethod;
switch(invokedMethod.getTag()) {
case Opcodes.H_INVOKESTATIC:
bridgeMethod = new Handle(invokedMethod.getTag(), internalName, name, invokedMethod.getDesc(), /*itf*/
false);
break;
case Opcodes.H_INVOKEVIRTUAL:
case // we end up calling these using invokevirtual
Opcodes.H_INVOKESPECIAL:
bridgeMethod = new Handle(Opcodes.H_INVOKEVIRTUAL, internalName, name, invokedMethod.getDesc(), /*itf*/
false);
break;
case Opcodes.H_NEWINVOKESPECIAL:
{
// Call invisible constructor through generated bridge "factory" method, so we need to
// compute the descriptor for the bridge method from the constructor's descriptor
String desc = Type.getMethodDescriptor(Type.getObjectType(invokedMethod.getOwner()), Type.getArgumentTypes(invokedMethod.getDesc()));
bridgeMethod = new Handle(Opcodes.H_INVOKESTATIC, internalName, name, desc, /*itf*/
false);
break;
}
case Opcodes.H_INVOKEINTERFACE:
// Shouldn't get here
default:
throw new UnsupportedOperationException("Cannot bridge " + invokedMethod);
}
result = MethodReferenceBridgeInfo.bridge(invokedMethod, invoked, bridgeMethod);
MethodReferenceBridgeInfo old = bridgeMethods.put(invokedMethod, result);
checkState(old == null, "Already had bridge %s so we don't also want %s", old, result);
return result;
}
use of java.lang.reflect.Executable in project SpongeCommon by SpongePowered.
the class InjectionPointProvider method findInjectionPoint.
@Nullable
private static SpongeInjectionPoint findInjectionPoint(List<DependencyAndSource> dependencyChain) {
if (dependencyChain.size() < 3) {
throw new AssertionError("Provider is not included in the dependency chain");
}
// @Inject InjectionPoint is the last, so we can skip it
for (int i = dependencyChain.size() - 2; i >= 0; i--) {
final Dependency<?> dependency = dependencyChain.get(i).getDependency();
if (dependency == null) {
return null;
}
final com.google.inject.spi.InjectionPoint spiInjectionPoint = dependency.getInjectionPoint();
if (spiInjectionPoint != null) {
final TypeToken<?> source = TypeToken.of(spiInjectionPoint.getDeclaringType().getType());
final Member member = spiInjectionPoint.getMember();
if (member instanceof Field) {
final Field field = (Field) member;
return new SpongeInjectionPoint(source, TypeToken.of(field.getGenericType()), field.getAnnotations());
} else if (member instanceof Executable) {
final Executable executable = (Executable) member;
final Annotation[][] parameterAnnotations = executable.getParameterAnnotations();
final Type[] parameterTypes = executable.getGenericParameterTypes();
final int index = dependency.getParameterIndex();
return new SpongeInjectionPoint(source, TypeToken.of(parameterTypes[index]), parameterAnnotations[index]);
} else {
throw new IllegalStateException("Unsupported Member type: " + member.getClass().getName());
}
}
}
return null;
}
use of java.lang.reflect.Executable in project junit5 by junit-team.
the class ParameterizedTestParameterResolver method supportsParameter.
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
Executable declaringExecutable = parameterContext.getParameter().getDeclaringExecutable();
Method testMethod = extensionContext.getTestMethod().orElse(null);
return declaringExecutable.equals(testMethod) && parameterContext.getIndex() < arguments.length;
}
Aggregations