Search in sources :

Example 31 with Executable

use of java.lang.reflect.Executable in project tutorials by eugenp.

the class SpringExtension method supportsParameter.

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

use of java.lang.reflect.Executable in project LanternServer by LanternPowered.

the class InjectionPointProvider method findInjectionPoint.

@Nullable
private static InjectionPoint findInjectionPoint(List<DependencyAndSource> dependencyChain) {
    if (dependencyChain.size() < 3) {
        new AssertionError("Provider is not included in the dependency chain").printStackTrace();
    }
    // @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();
            final InjectionPoint injectionPoint;
            if (member instanceof Field) {
                final Field field = (Field) member;
                injectionPoint = new InjectionPoint(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();
                injectionPoint = new InjectionPoint(source, TypeToken.of(parameterTypes[index]), parameterAnnotations[index]);
            } else {
                throw new IllegalStateException("Unsupported Member type: " + member.getClass().getName());
            }
            return injectionPoint;
        }
    }
    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 33 with Executable

use of java.lang.reflect.Executable in project guice by google.

the class Enhancer method generateGlue.

@Override
protected byte[] generateGlue(Collection<Executable> members) {
    ClassWriter cw = new ClassWriter(COMPUTE_MAXS);
    // target Java8 because that's all we need for the generated trampoline code
    cw.visit(V1_8, PUBLIC | ACC_SUPER, proxyName, null, hostName, null);
    cw.visitSource(GENERATED_SOURCE, null);
    // this shared field either contains the trampoline or glue to make it into an invoker table
    cw.visitField(PUBLIC | STATIC | FINAL, INVOKERS_NAME, INVOKERS_DESCRIPTOR, null, null).visitEnd();
    setupInvokerTable(cw);
    generateTrampoline(cw, members);
    // this field will hold the handlers configured for this particular enhanced instance
    cw.visitField(PRIVATE | FINAL, HANDLERS_NAME, HANDLERS_DESCRIPTOR, null, null).visitEnd();
    Set<Method> remainingBridgeMethods = new HashSet<>(bridgeDelegates.keySet());
    int methodIndex = 0;
    for (Executable member : members) {
        if (member instanceof Constructor<?>) {
            enhanceConstructor(cw, (Constructor<?>) member);
        } else {
            enhanceMethod(cw, (Method) member, methodIndex++);
            remainingBridgeMethods.remove(member);
        }
    }
    // replace any remaining bridge methods with virtual dispatch to their non-bridge targets
    for (Method method : remainingBridgeMethods) {
        Method target = bridgeDelegates.get(method);
        if (target != null) {
            generateVirtualBridge(cw, method, target);
        }
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) Executable(java.lang.reflect.Executable) ClassWriter(org.objectweb.asm.ClassWriter) HashSet(java.util.HashSet)

Example 34 with Executable

use of java.lang.reflect.Executable in project guice by google.

the class EnhancerBuilderImpl method doBuildEnhancer.

private Function<String, BiFunction<Object, Object[], Object>> doBuildEnhancer(BitSet methodIndices) {
    NavigableMap<String, Executable> glueMap = new TreeMap<>();
    visitMembers(hostClass.getDeclaredConstructors(), hasPackageAccess(), ctor -> glueMap.put(signature(ctor), ctor));
    for (int methodIndex = methodIndices.nextSetBit(0); methodIndex >= 0; methodIndex = methodIndices.nextSetBit(methodIndex + 1)) {
        Method method = enhanceableMethods[methodIndex];
        glueMap.put(signature(method), method);
    }
    return new Enhancer(hostClass, bridgeDelegates).glue(glueMap);
}
Also used : Method(java.lang.reflect.Method) Executable(java.lang.reflect.Executable) TreeMap(java.util.TreeMap)

Example 35 with Executable

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

the class LocalVariableTableParameterNameDiscoverer method inspectClass.

/**
 * Inspects the target class.
 * <p>Exceptions will be logged, and a marker map returned to indicate the
 * lack of debug information.
 */
private Map<Executable, String[]> inspectClass(Class<?> clazz) {
    InputStream is = clazz.getResourceAsStream(ClassUtils.getClassFileName(clazz));
    if (is == null) {
        // simply means this method of discovering parameter names won't work.
        if (logger.isDebugEnabled()) {
            logger.debug("Cannot find '.class' file for class [" + clazz + "] - unable to determine constructor/method parameter names");
        }
        return NO_DEBUG_INFO_MAP;
    }
    // custom handling of the close() method in a finally-block.
    try {
        ClassReader classReader = new ClassReader(is);
        Map<Executable, String[]> map = new ConcurrentHashMap<>(32);
        classReader.accept(new ParameterNameDiscoveringVisitor(clazz, map), 0);
        return map;
    } catch (IOException ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Exception thrown while reading '.class' file for class [" + clazz + "] - unable to determine constructor/method parameter names", ex);
        }
    } catch (IllegalArgumentException ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("ASM ClassReader failed to parse class file [" + clazz + "], probably due to a new Java class file version that isn't supported yet " + "- unable to determine constructor/method parameter names", ex);
        }
    } finally {
        try {
            is.close();
        } catch (IOException ex) {
        // ignore
        }
    }
    return NO_DEBUG_INFO_MAP;
}
Also used : InputStream(java.io.InputStream) ClassReader(org.springframework.asm.ClassReader) IOException(java.io.IOException) Executable(java.lang.reflect.Executable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

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