/** * Invokes the method on a given instance passing in given parameters. If the invocation yields * {@link InvocationTargetException}, the exception is unwrapped. * * It is a responsibility of the caller to make sure that the method is accessible to the caller. */ public static <T> T invokeAndUnwrap(final Object instance, final Method method, final Object... parameters) throws Throwable { try { return cast(method.invoke(instance, parameters)); } catch (IllegalArgumentException e) { throw ReflectionLogger.LOG.illegalArgumentExceptionOnReflectionInvocation(instance.getClass(), instance, method, Arrays.toString(parameters), e); } catch (IllegalAccessException e) { throw new WeldException(e); } catch (InvocationTargetException e) { throw e.getCause(); } }
private void checkMetaAnnotations(EnhancedAnnotation<T> annotatedAnnotation) { ElementType[] elementTypes = getTargetElementTypes(annotatedAnnotation.getAnnotation(Target.class)); for (Annotation inheritedBinding : getInheritedInterceptionBindingTypes()) { ElementType[] metaAnnotationElementTypes = getTargetElementTypes(inheritedBinding.annotationType().getAnnotation(Target.class)); if (!Arrays2.containsAll(metaAnnotationElementTypes, elementTypes)) { ReflectionLogger.LOG.invalidInterceptorBindingTargetDeclaration(inheritedBinding.annotationType().getName(), Arrays.toString(metaAnnotationElementTypes), annotatedAnnotation.getJavaClass().getName(), Arrays.toString(elementTypes)); } } }
private static boolean isAssignableFrom(ParameterizedType type1, ParameterizedType type2) { // first, raw types have to be equal if (!Reflections.getRawType(type1).equals(Reflections.getRawType(type2))) { return false; } final Type[] types1 = type1.getActualTypeArguments(); final Type[] types2 = type2.getActualTypeArguments(); if (types1.length != types2.length) { throw ReflectionLogger.LOG.invalidTypeArgumentCombination(type1, type2); } for (int i = 0; i < types1.length; i++) { if (!isAssignableFrom(types1[i], types2[i])) { return false; } } return true; }
protected void check(EnhancedAnnotation<T> annotatedAnnotation) { super.check(annotatedAnnotation); if (isValid()) { if (!annotatedAnnotation.isAnnotationPresent(Target.class)) { ReflectionLogger.LOG.missingTarget(annotatedAnnotation); } if (!isValidTargetType(annotatedAnnotation)) { ReflectionLogger.LOG.missingTargetTypeMethodOrTargetType(annotatedAnnotation); } } }
@Override protected void check(EnhancedAnnotation<T> annotatedAnnotation) { super.check(annotatedAnnotation); if (isValid()) { if (!annotatedAnnotation.isAnnotationPresent(Target.class)) { ReflectionLogger.LOG.missingTarget(annotatedAnnotation); } else if (!Arrays2.unorderedEquals(annotatedAnnotation.getAnnotation(Target.class).value(), METHOD, FIELD, TYPE)) { ReflectionLogger.LOG.missingTargetMethodFieldType(annotatedAnnotation); } } }
/** * Validates the members */ protected void check(EnhancedAnnotation<T> annotatedAnnotation) { super.check(annotatedAnnotation); if (isValid()) { if (!annotatedAnnotation.isAnnotationPresent(Target.class)) { ReflectionLogger.LOG.missingTarget(annotatedAnnotation); } else if (!Arrays2.unorderedEquals(annotatedAnnotation.getAnnotation(Target.class).value(), METHOD, FIELD, PARAMETER, TYPE)) { ReflectionLogger.LOG.missingTargetMethodFieldParameterType(annotatedAnnotation); } } }
this.delegate = annotated; if (annotated instanceof AnnotatedType<?>) { this.rawType = Reflections.<AnnotatedType<T>>cast(annotated).getJavaClass(); } else { this.rawType = Reflections.getRawType(annotated.getBaseType()); throw ReflectionLogger.LOG.annotationMapNull(); throw ReflectionLogger.LOG.declaredAnnotationMapNull();
@Override protected void check(EnhancedAnnotation<T> annotatedAnnotation) { super.check(annotatedAnnotation); if (isValid()) { if (!annotatedAnnotation.isAnnotationPresent(Target.class)) { ReflectionLogger.LOG.missingTarget(annotatedAnnotation); } else { ElementType[] elementTypes = annotatedAnnotation.getAnnotation(Target.class).value(); if (!(Arrays2.unorderedEquals(elementTypes, METHOD, FIELD, TYPE) || Arrays2.unorderedEquals(elementTypes, TYPE) || Arrays2.unorderedEquals(elementTypes, METHOD) || Arrays2.unorderedEquals(elementTypes, FIELD) || Arrays2.unorderedEquals(elementTypes, METHOD, TYPE) )) { ReflectionLogger.LOG.missingTargetMethodFieldTypeParameterOrTargetMethodTypeOrTargetMethodOrTargetTypeOrTargetField(annotatedAnnotation); } } } }
protected void check(EnhancedAnnotation<T> annotatedAnnotation) { if (valid && (!annotatedAnnotation.isAnnotationPresent(Retention.class) || annotatedAnnotation.isAnnotationPresent(Retention.class) && !annotatedAnnotation.getAnnotation(Retention.class).value().equals(RetentionPolicy.RUNTIME))) { this.valid = false; ReflectionLogger.LOG.missingRetention(annotatedAnnotation); } }
private void checkInterceptorBindings() { if (interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.POST_CONSTRUCT) || interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.PRE_DESTROY) || interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.POST_ACTIVATE) || interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.PRE_PASSIVATE)) { for (Annotation interceptorBindingType : interceptorBindingTypes) { Target target = interceptorBindingType.annotationType().getAnnotation(Target.class); if (target == null || hasInvalidTargetType(target.value())) { ReflectionLogger.LOG.lifecycleCallbackInterceptorWithInvalidBindingTarget(this, interceptorBindingType.annotationType().getName(), target != null ? Arrays.toString(target.value()) : "Target meta-annotation is not present"); } } } }
private boolean matches(ParameterizedType observedType, ParameterizedType eventType) { if (!observedType.getRawType().equals(eventType.getRawType())) { return false; } if (observedType.getActualTypeArguments().length != eventType.getActualTypeArguments().length) { throw ReflectionLogger.LOG.invalidTypeArgumentCombination(observedType, eventType); } for (int i = 0; i < observedType.getActualTypeArguments().length; i++) { if (!parametersMatch(observedType.getActualTypeArguments()[i], eventType.getActualTypeArguments()[i])) { return false; } } return true; }
protected static void validateParameterCount(AnnotatedCallable<?> callable) { if (callable instanceof BackedAnnotatedMember) { return; // do not validate backed implementation } Class<?>[] parameterTypes = null; if (callable instanceof AnnotatedConstructor<?>) { parameterTypes = AnnotatedConstructor.class.cast(callable).getJavaMember().getParameterTypes(); } else { parameterTypes = AnnotatedMethod.class.cast(callable).getJavaMember().getParameterTypes(); } if (callable.getParameters().size() != parameterTypes.length) { // For enums, BackedAnnotatedConstructor sets parameters to an empty list, so we shouldn't throw the DefinitionException Class<?> declaringClass = callable.getDeclaringType().getJavaClass(); if (!declaringClass.isEnum() && !declaringClass.isMemberClass()) { throw ReflectionLogger.LOG.incorrectNumberOfAnnotatedParametersMethod(callable.getParameters().size(), callable, callable.getParameters(), Arrays.asList(parameterTypes)); } } } }
this.delegate = annotated; if (annotated instanceof AnnotatedType<?>) { this.rawType = Reflections.<AnnotatedType<T>>cast(annotated).getJavaClass(); } else { this.rawType = Reflections.getRawType(annotated.getBaseType()); throw ReflectionLogger.LOG.annotationMapNull(); throw ReflectionLogger.LOG.declaredAnnotationMapNull();
@Override protected void check(EnhancedAnnotation<T> annotatedAnnotation) { super.check(annotatedAnnotation); if (isValid()) { if (!annotatedAnnotation.isAnnotationPresent(Target.class)) { ReflectionLogger.LOG.missingTarget(annotatedAnnotation); } else if (!Arrays2.unorderedEquals(annotatedAnnotation.getAnnotation(Target.class).value(), METHOD, FIELD, TYPE)) { ReflectionLogger.LOG.missingTargetMethodFieldType(annotatedAnnotation); } } }
protected void check(EnhancedAnnotation<T> annotatedAnnotation) { super.check(annotatedAnnotation); if (isValid()) { if (!annotatedAnnotation.isAnnotationPresent(Target.class)) { ReflectionLogger.LOG.missingTarget(annotatedAnnotation); } if (!isValidTargetType(annotatedAnnotation)) { ReflectionLogger.LOG.missingTargetTypeMethodOrTargetType(annotatedAnnotation); } } }
/** * Validates the members */ protected void check(EnhancedAnnotation<T> annotatedAnnotation) { super.check(annotatedAnnotation); if (isValid()) { if (!annotatedAnnotation.isAnnotationPresent(Target.class)) { ReflectionLogger.LOG.missingTarget(annotatedAnnotation); } else if (!Arrays2.unorderedEquals(annotatedAnnotation.getAnnotation(Target.class).value(), METHOD, FIELD, PARAMETER, TYPE)) { ReflectionLogger.LOG.missingTargetMethodFieldParameterType(annotatedAnnotation); } } }
@Override protected void check(EnhancedAnnotation<T> annotatedAnnotation) { super.check(annotatedAnnotation); if (isValid()) { if (!annotatedAnnotation.isAnnotationPresent(Target.class)) { ReflectionLogger.LOG.missingTarget(annotatedAnnotation); } else { ElementType[] elementTypes = annotatedAnnotation.getAnnotation(Target.class).value(); if (!(Arrays2.unorderedEquals(elementTypes, METHOD, FIELD, TYPE) || Arrays2.unorderedEquals(elementTypes, TYPE) || Arrays2.unorderedEquals(elementTypes, METHOD) || Arrays2.unorderedEquals(elementTypes, FIELD) || Arrays2.unorderedEquals(elementTypes, METHOD, TYPE) )) { ReflectionLogger.LOG.missingTargetMethodFieldTypeParameterOrTargetMethodTypeOrTargetMethodOrTargetTypeOrTargetField(annotatedAnnotation); } } } }
protected void check(EnhancedAnnotation<T> annotatedAnnotation) { if (valid && (!annotatedAnnotation.isAnnotationPresent(Retention.class) || annotatedAnnotation.isAnnotationPresent(Retention.class) && !annotatedAnnotation.getAnnotation(Retention.class).value().equals(RetentionPolicy.RUNTIME))) { this.valid = false; ReflectionLogger.LOG.missingRetention(annotatedAnnotation); } }
private void checkInterceptorBindings() { if (interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.POST_CONSTRUCT) || interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.PRE_DESTROY) || interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.POST_ACTIVATE) || interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.PRE_PASSIVATE)) { for (Annotation interceptorBindingType : interceptorBindingTypes) { Target target = interceptorBindingType.annotationType().getAnnotation(Target.class); if (target == null || hasInvalidTargetType(target.value())) { ReflectionLogger.LOG.lifecycleCallbackInterceptorWithInvalidBindingTarget(this, interceptorBindingType.annotationType().getName(), target != null ? Arrays.toString(target.value()) : "Target meta-annotation is not present"); } } } }
private boolean matches(ParameterizedType observedType, ParameterizedType eventType) { if (!observedType.getRawType().equals(eventType.getRawType())) { return false; } if (observedType.getActualTypeArguments().length != eventType.getActualTypeArguments().length) { throw ReflectionLogger.LOG.invalidTypeArgumentCombination(observedType, eventType); } for (int i = 0; i < observedType.getActualTypeArguments().length; i++) { if (!parametersMatch(observedType.getActualTypeArguments()[i], eventType.getActualTypeArguments()[i])) { return false; } } return true; }