Search in sources :

Example 1 with Executable

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);
}
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 2 with Executable

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");
}
Also used : KParameter(kotlin.reflect.KParameter) Parameter(java.lang.reflect.Parameter) Executable(java.lang.reflect.Executable)

Example 3 with 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;
}
Also used : Executable(java.lang.reflect.Executable) MethodHandle(java.lang.invoke.MethodHandle) Handle(org.objectweb.asm.Handle)

Example 4 with Executable

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;
}
Also used : Annotation(java.lang.annotation.Annotation) Field(java.lang.reflect.Field) Executable(java.lang.reflect.Executable) Member(java.lang.reflect.Member) Nullable(javax.annotation.Nullable)

Example 5 with Executable

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;
}
Also used : Method(java.lang.reflect.Method) Executable(java.lang.reflect.Executable)

Aggregations

Executable (java.lang.reflect.Executable)39 Method (java.lang.reflect.Method)14 Constructor (java.lang.reflect.Constructor)9 Parameter (java.lang.reflect.Parameter)8 Field (java.lang.reflect.Field)6 Member (java.lang.reflect.Member)5 Annotation (java.lang.annotation.Annotation)4 List (java.util.List)4 AnalysisField (com.oracle.graal.pointsto.meta.AnalysisField)2 CEntryPointOptions (com.oracle.svm.core.c.function.CEntryPointOptions)2 JNIAccessibleMethodDescriptor (com.oracle.svm.jni.access.JNIAccessibleMethodDescriptor)2 AnnotatedType (java.lang.reflect.AnnotatedType)2 TypeVariable (java.lang.reflect.TypeVariable)2 Arrays (java.util.Arrays)2 HashSet (java.util.HashSet)2 Nullable (javax.annotation.Nullable)2 AnnotatedParameter (javax.enterprise.inject.spi.AnnotatedParameter)2 Named (javax.inject.Named)2 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2