Search in sources :

Example 21 with Executable

use of java.lang.reflect.Executable in project graal by oracle.

the class ReflectionDataBuilder method enclosingMethodOrConstructor.

private Executable enclosingMethodOrConstructor(Class<?> clazz) {
    Method enclosingMethod;
    Constructor<?> enclosingConstructor;
    try {
        enclosingMethod = clazz.getEnclosingMethod();
        enclosingConstructor = clazz.getEnclosingConstructor();
    } catch (InternalError ex) {
        // Checkstyle: stop
        System.err.println("GR-7731: Could not find the enclosing method of class " + clazz.getTypeName() + ". This is a known transient error and most likely does not cause any problems, unless your code relies on the enclosing method of exactly this class. If you can reliably reproduce this problem, please send us a test case.");
        // Checkstyle: resume
        return null;
    }
    if (enclosingMethod == null && enclosingConstructor == null) {
        return null;
    } else if (enclosingMethod != null && enclosingConstructor != null) {
        throw VMError.shouldNotReachHere("Classs has both an enclosingMethod and an enclosingConstructor: " + clazz + ", " + enclosingMethod + ", " + enclosingConstructor);
    }
    Executable enclosingMethodOrConstructor = enclosingMethod != null ? enclosingMethod : enclosingConstructor;
    if (reflectionMethods.contains(enclosingMethodOrConstructor)) {
        return enclosingMethodOrConstructor;
    } else {
        return null;
    }
}
Also used : Method(java.lang.reflect.Method) Executable(java.lang.reflect.Executable)

Example 22 with Executable

use of java.lang.reflect.Executable in project rxlib by RockyLOMO.

the class ValidateUtil method onProcess.

/**
 * Annotation expression只对method有效
 *
 * @param joinPoint
 * @param msg
 * @return
 * @throws Throwable
 */
@Override
protected Object onProcess(ProceedingJoinPoint joinPoint, StringBuilder msg) throws Throwable {
    Class targetType = joinPoint.getTarget().getClass();
    Signature signature = joinPoint.getSignature();
    Executable member;
    if (signature instanceof ConstructorSignature) {
        member = ((ConstructorSignature) signature).getConstructor();
    } else {
        member = ((MethodSignature) signature).getMethod();
    }
    msg.setPrefix(String.format("[Valid] %s.%s ", targetType.getSimpleName(), signature.getName()));
    EnableValid attr = member.getAnnotation(EnableValid.class);
    if (attr == null) {
        attr = (EnableValid) targetType.getAnnotation(EnableValid.class);
        if (attr == null) {
            msg.appendLine("skip validate..");
            return joinPoint.proceed();
        }
    }
    int flags = attr.value();
    boolean validateValues = hasFlags(flags, EnableValid.ParameterValues);
    if (hasFlags(flags, EnableValid.Method)) {
        if (signature instanceof ConstructorSignature) {
            ConstructorSignature cs = (ConstructorSignature) signature;
            validateConstructor(cs.getConstructor(), joinPoint.getArgs(), validateValues);
            return super.onProcess(joinPoint, msg);
        }
        MethodSignature ms = (MethodSignature) signature;
        return validateMethod(ms.getMethod(), joinPoint.getTarget(), joinPoint.getArgs(), validateValues, p -> super.onProcess(joinPoint, msg));
    }
    if (validateValues) {
        for (Object parameterValue : joinPoint.getArgs()) {
            validateBean(parameterValue);
        }
    }
    msg.appendLine("validate ok..").setPrefix(null);
    return super.onProcess(joinPoint, msg);
}
Also used : ConstructorSignature(org.aspectj.lang.reflect.ConstructorSignature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Signature(org.aspectj.lang.Signature) ConstructorSignature(org.aspectj.lang.reflect.ConstructorSignature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Executable(java.lang.reflect.Executable) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint)

Example 23 with Executable

use of java.lang.reflect.Executable in project core by weld.

the class ResolvableBuilder method addQualifier.

private ResolvableBuilder addQualifier(Annotation qualifier, InjectionPoint injectionPoint) {
    QualifierInstance qualifierInstance = QualifierInstance.of(qualifier, store);
    final Class<? extends Annotation> annotationType = qualifierInstance.getAnnotationClass();
    // Handle the @New qualifier special case
    if (annotationType.equals(New.class)) {
        New newQualifier = New.class.cast(qualifier);
        if (newQualifier.value().equals(New.class) && rawType == null) {
            throw new IllegalStateException("Cannot transform @New when there is no known raw type");
        } else if (newQualifier.value().equals(New.class)) {
            qualifier = new NewLiteral(rawType);
            qualifierInstance = QualifierInstance.of(qualifier, store);
        }
    } else if (injectionPoint != null && annotationType.equals(Named.class)) {
        Named named = (Named) qualifier;
        if (named.value().equals("")) {
            // WELD-1739
            // This is an injection point with an @Named qualifier, with no value specified, we need to assume the name of the field or parameter is the
            // value
            Member member = injectionPoint.getMember();
            if (member instanceof Executable) {
                // Method or constructor injection
                Executable executable = (Executable) member;
                AnnotatedParameter<?> annotatedParameter = (AnnotatedParameter<?>) injectionPoint.getAnnotated();
                Parameter parameter = executable.getParameters()[annotatedParameter.getPosition()];
                named = new NamedLiteral(parameter.getName());
            } else {
                named = new NamedLiteral(injectionPoint.getMember().getName());
            }
            qualifier = named;
            qualifierInstance = QualifierInstance.of(named, store);
        }
    }
    checkQualifier(qualifier, qualifierInstance, annotationType);
    this.qualifierInstances.add(qualifierInstance);
    return this;
}
Also used : New(javax.enterprise.inject.New) Named(javax.inject.Named) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) Parameter(java.lang.reflect.Parameter) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) NewLiteral(org.jboss.weld.literal.NewLiteral) Executable(java.lang.reflect.Executable) Member(java.lang.reflect.Member) NamedLiteral(org.jboss.weld.literal.NamedLiteral)

Example 24 with Executable

use of java.lang.reflect.Executable in project core by weld.

the class Validator method validateInjectionPointForDefinitionErrors.

/**
 * Checks for definition errors associated with a given {@link InjectionPoint}
 */
public void validateInjectionPointForDefinitionErrors(InjectionPoint ij, Bean<?> bean, BeanManagerImpl beanManager) {
    if (ij.getAnnotated().getAnnotation(New.class) != null && ij.getQualifiers().size() > 1) {
        throw ValidatorLogger.LOG.newWithQualifiers(ij, Formats.formatAsStackTraceElement(ij));
    }
    if (ij.getType() instanceof TypeVariable<?>) {
        throw ValidatorLogger.LOG.injectionPointWithTypeVariable(ij, Formats.formatAsStackTraceElement(ij));
    }
    // WELD-1739
    if (ij.getMember() instanceof Executable && ij.getAnnotated().isAnnotationPresent(Named.class) && ij.getAnnotated().getAnnotation(Named.class).value().equals("")) {
        Executable executable = (Executable) ij.getMember();
        AnnotatedParameter<?> annotatedParameter = (AnnotatedParameter<?>) ij.getAnnotated();
        if (!executable.getParameters()[annotatedParameter.getPosition()].isNamePresent()) {
            // No parameters info available
            throw ValidatorLogger.LOG.nonFieldInjectionPointCannotUseNamed(ij, Formats.formatAsStackTraceElement(ij));
        }
    }
    if (ij.getAnnotated().isAnnotationPresent(Produces.class)) {
        if (bean != null) {
            throw BeanLogger.LOG.injectedFieldCannotBeProducer(ij.getAnnotated(), bean);
        } else {
            throw BeanLogger.LOG.injectedFieldCannotBeProducer(ij.getAnnotated(), Reflections.<AnnotatedField<?>>cast(ij.getAnnotated()).getDeclaringType());
        }
    }
    boolean newBean = (bean instanceof NewBean);
    if (!newBean) {
        checkScopeAnnotations(ij, beanManager.getServices().get(MetaAnnotationStore.class));
    }
    checkFacadeInjectionPoint(ij, Instance.class);
    checkFacadeInjectionPoint(ij, Event.class);
    if (InterceptionFactory.class.equals(Reflections.getRawType(ij.getType())) && !(bean instanceof ProducerMethod<?, ?>)) {
        throw ValidatorLogger.LOG.invalidInterceptionFactoryInjectionPoint(ij, Formats.formatAsStackTraceElement(ij));
    }
    for (PlugableValidator validator : plugableValidators) {
        validator.validateInjectionPointForDefinitionErrors(ij, bean, beanManager);
    }
}
Also used : PlugableValidator(org.jboss.weld.module.PlugableValidator) Named(javax.inject.Named) TypeVariable(java.lang.reflect.TypeVariable) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) MetaAnnotationStore(org.jboss.weld.metadata.cache.MetaAnnotationStore) NewBean(org.jboss.weld.bean.NewBean) InterceptionFactory(javax.enterprise.inject.spi.InterceptionFactory) Executable(java.lang.reflect.Executable) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField)

Example 25 with Executable

use of java.lang.reflect.Executable in project spoon by INRIA.

the class CtTypeParameterTest method checkParameterErasureOfExecutable.

private void checkParameterErasureOfExecutable(CtParameter<?> param) {
    CtExecutable<?> exec = param.getParent();
    CtTypeReference<?> typeErasure = param.getType().getTypeErasure();
    int paramIdx = exec.getParameters().indexOf(param);
    Class declClass = exec.getParent(CtType.class).getActualClass();
    Executable declExec;
    if (exec instanceof CtConstructor) {
        declExec = declClass.getDeclaredConstructors()[0];
    } else {
        declExec = getMethodByName(declClass, exec.getSimpleName());
    }
    Class<?> paramType = declExec.getParameterTypes()[paramIdx];
    assertEquals(0, typeErasure.getActualTypeArguments().size());
    // contract the type erasure of the method parameter given with Java reflection is the same as the one computed by spoon
    assertEquals("TypeErasure of executable " + exec.getSignature() + " parameter " + param.getSimpleName(), paramType.getName(), typeErasure.getQualifiedName());
}
Also used : CtType(spoon.reflect.declaration.CtType) CtClass(spoon.reflect.declaration.CtClass) Executable(java.lang.reflect.Executable) CtExecutable(spoon.reflect.declaration.CtExecutable) CtConstructor(spoon.reflect.declaration.CtConstructor)

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