org.glassfish.jersey.internal.util.ReflectionHelper Java Examples

The following examples show how to use org.glassfish.jersey.internal.util.ReflectionHelper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClassInfo.java    From ameba with MIT License 6 votes vote down vote up
/**
 * <p>getClassForName.</p>
 *
 * @param className a {@link java.lang.String} object.
 * @return a {@link java.lang.Class} object.
 */
public Class getClassForName(final String className) {
    try {
        final OsgiRegistry osgiRegistry = ReflectionHelper.getOsgiRegistryInstance();

        if (osgiRegistry != null) {
            return osgiRegistry.classForNameWithException(className);
        } else {
            return AccessController.doPrivileged(ReflectionHelper.classForNameWithExceptionPEA(className));
        }
    } catch (final ClassNotFoundException ex) {
        throw new RuntimeException(LocalizationMessages.ERROR_SCANNING_CLASS_NOT_FOUND(className), ex);
    } catch (final PrivilegedActionException pae) {
        final Throwable cause = pae.getCause();
        if (cause instanceof ClassNotFoundException) {
            throw new RuntimeException(LocalizationMessages.ERROR_SCANNING_CLASS_NOT_FOUND(className), cause);
        } else if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        } else {
            throw new RuntimeException(cause);
        }
    }
}
 
Example #2
Source File: Pac4JValueFactoryProvider.java    From jax-rs-pac4j with Apache License 2.0 5 votes vote down vote up
@Override
protected Factory<?> createValueFactory(Parameter parameter) {
    if (parameter.isAnnotationPresent(Pac4JProfileManager.class)) {
        if (ProfileManager.class.isAssignableFrom(parameter.getRawType())) {
            return manager.get();
        }

        throw new IllegalStateException("Cannot inject a Pac4J profile manager into a parameter of type "
                + parameter.getRawType().getName());
    }

    if (parameter.isAnnotationPresent(Pac4JProfile.class)) {
        if (CommonProfile.class.isAssignableFrom(parameter.getRawType())) {
            return profile.get();
        }

        if (Optional.class.isAssignableFrom(parameter.getRawType())) {
            List<ClassTypePair> ctps = ReflectionHelper.getTypeArgumentAndClass(parameter.getRawType());
            ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null;
            if (ctp == null || CommonProfile.class.isAssignableFrom(ctp.rawClass())) {
                return optProfile.get();
            }
        }

        throw new IllegalStateException(
                "Cannot inject a Pac4J profile into a parameter of type " + parameter.getRawType().getName());
    }

    return null;
}
 
Example #3
Source File: Pac4JValueFactoryProvider.java    From jax-rs-pac4j with Apache License 2.0 5 votes vote down vote up
@Override
protected Function<ContainerRequest, ?> createValueProvider(Parameter parameter) {
    if (parameter.isAnnotationPresent(Pac4JProfileManager.class)) {
        if (ProfileManager.class.isAssignableFrom(parameter.getRawType())) {
            return manager.get();
        }

        throw new IllegalStateException("Cannot inject a Pac4J profile manager into a parameter of type "
            + parameter.getRawType().getName());
    }

    if (parameter.isAnnotationPresent(Pac4JProfile.class)) {
        if (CommonProfile.class.isAssignableFrom(parameter.getRawType())) {
            return profile.get();
        }

        if (Optional.class.isAssignableFrom(parameter.getRawType())) {
            List<ClassTypePair> ctps = ReflectionHelper.getTypeArgumentAndClass(parameter.getRawType());
            ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null;
            if (ctp == null || CommonProfile.class.isAssignableFrom(ctp.rawClass())) {
                return optProfile.get();
            }
        }

        throw new IllegalStateException(
            "Cannot inject a Pac4J profile into a parameter of type " + parameter.getRawType().getName());
    }

    return null;
}
 
Example #4
Source File: Pac4JValueFactoryProvider.java    From jax-rs-pac4j with Apache License 2.0 5 votes vote down vote up
@Override
protected Function<ContainerRequest, ?> createValueProvider(Parameter parameter) {
    if (parameter.isAnnotationPresent(Pac4JProfileManager.class)) {
        if (ProfileManager.class.isAssignableFrom(parameter.getRawType())) {
            return manager.get();
        }

        throw new IllegalStateException("Cannot inject a Pac4J profile manager into a parameter of type "
            + parameter.getRawType().getName());
    }

    if (parameter.isAnnotationPresent(Pac4JProfile.class)) {
        if (CommonProfile.class.isAssignableFrom(parameter.getRawType())) {
            return profile.get();
        }

        if (Optional.class.isAssignableFrom(parameter.getRawType())) {
            List<ClassTypePair> ctps = ReflectionHelper.getTypeArgumentAndClass(parameter.getRawType());
            ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null;
            if (ctp == null || CommonProfile.class.isAssignableFrom(ctp.rawClass())) {
                return optProfile.get();
            }
        }

        throw new IllegalStateException(
            "Cannot inject a Pac4J profile into a parameter of type " + parameter.getRawType().getName());
    }

    return null;
}
 
Example #5
Source File: JacksonUtils.java    From ameba with MIT License 5 votes vote down vote up
/**
 * <p>configFilterIntrospector.</p>
 *
 * @param mapper a {@link com.fasterxml.jackson.databind.ObjectMapper} object.
 * @return a {@link com.fasterxml.jackson.databind.ObjectMapper} object.
 */
public static ObjectMapper configFilterIntrospector(final ObjectMapper mapper) {
    final AnnotationIntrospector customIntrospector = mapper.getSerializationConfig().getAnnotationIntrospector();
    // Set the custom (user) introspector to be the primary one.
    return mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(customIntrospector,
            new JacksonAnnotationIntrospector() {
                @Override
                public Object findFilterId(final Annotated a) {
                    final Object filterId = super.findFilterId(a);

                    if (filterId != null) {
                        return filterId;
                    }

                    if (a instanceof AnnotatedMethod) {
                        final Method method = ((AnnotatedMethod) a).getAnnotated();

                        // Interested only in getters - trying to obtain "field" name from them.
                        if (ReflectionHelper.isGetter(method)) {
                            return ReflectionHelper.getPropertyName(method);
                        }
                    }
                    if (a instanceof AnnotatedField || a instanceof AnnotatedClass) {
                        return a.getName();
                    }

                    return null;
                }
            }));

}
 
Example #6
Source File: AbstractTemplateProcessor.java    From ameba with MIT License 5 votes vote down vote up
/**
 * <p>getTemplateObjectFactory.</p>
 *
 * @param serviceLocator a {@link org.glassfish.hk2.api.ServiceLocator} object.
 * @param type           a {@link java.lang.Class} object.
 * @param defaultValue   a {@link org.glassfish.jersey.internal.util.collection.Value} object.
 * @param <F>            a F object.
 * @return a F object.
 */
@SuppressWarnings("unchecked")
protected <F> F getTemplateObjectFactory(ServiceLocator serviceLocator, Class<F> type, Value<F> defaultValue) {
    Object objectFactoryProperty = this.config.getProperty(MvcFeature.TEMPLATE_OBJECT_FACTORY + this.suffix);
    if (objectFactoryProperty != null) {
        if (type.isAssignableFrom(objectFactoryProperty.getClass())) {
            return type.cast(objectFactoryProperty);
        }

        Class<F> factoryClass = null;
        if (objectFactoryProperty instanceof String) {
            factoryClass = ReflectionHelper.<F>classForNamePA((String) objectFactoryProperty).run();
        } else if (objectFactoryProperty instanceof Class) {
            factoryClass = (Class<F>) objectFactoryProperty;
        }

        if (factoryClass != null) {
            if (type.isAssignableFrom(factoryClass)) {
                return serviceLocator.create(factoryClass);
            }

            logger.warn(LocalizationMessages.WRONG_TEMPLATE_OBJECT_FACTORY(factoryClass, type));
        }
    }

    return defaultValue.get();
}
 
Example #7
Source File: TemplateModelProcessor.java    From ameba with MIT License 5 votes vote down vote up
/**
 * Returns a list of enhancing methods for a given {@link org.glassfish.jersey.server.model.RuntimeResource runtime resource}.
 *
 * @param runtimeResource runtime resource to create enhancing methods for.
 * @return list of enhancing methods.
 */
private List<ModelProcessorUtil.Method> getEnhancingMethods(final RuntimeResource runtimeResource) {
    final List<ModelProcessorUtil.Method> newMethods = Lists.newArrayList();

    for (final Resource resource : runtimeResource.getResources()) {
        // Handler classes.
        for (final Class<?> handlerClass : resource.getHandlerClasses()) {
            createEnhancingMethods(handlerClass, null, newMethods);
        }

        // Names - if there are no handler classes / instances.
        if (resource.getHandlerClasses().isEmpty() && resource.getHandlerInstances().isEmpty()) {
            for (String resourceName : resource.getNames()) {
                final Class<Object> resourceClass = AccessController
                        .doPrivileged(ReflectionHelper.classForNamePA(resourceName));
                if (resourceClass != null) {
                    createEnhancingMethods(resourceClass, null, newMethods);
                }
            }
        }

        // Handler instances.
        Errors.process((Producer<Void>) () -> {
            for (final Object handlerInstance : resource.getHandlerInstances()) {
                final Class<?> handlerInstanceClass = handlerInstance.getClass();

                if (!resource.getHandlerClasses().contains(handlerInstanceClass)) {
                    createEnhancingMethods(handlerInstanceClass, handlerInstance, newMethods);
                } else {
                    Errors.warning(resource,
                            LocalizationMessages.TEMPLATE_HANDLER_ALREADY_ENHANCED(handlerInstanceClass));
                }
            }

            return null;
        });
    }

    return newMethods;
}
 
Example #8
Source File: WebResourceFactory.java    From rx-jersey with MIT License 3 votes vote down vote up
/**
 * Creates a new client-side representation of a resource described by
 * the interface passed in the first argument.
 *
 * @param <C>                Type of the resource to be created.
 * @param resourceInterface  Interface describing the resource to be created.
 * @param target             WebTarget pointing to the resource or the parent of the resource.
 * @param ignoreResourcePath If set to true, ignores path annotation on the resource interface (this is used when creating
 *                           sub-resources)
 * @param headers            Header params collected from parent resources (used when creating a sub-resource)
 * @param cookies            Cookie params collected from parent resources (used when creating a sub-resource)
 * @param form               Form params collected from parent resources (used when creating a sub-resource)
 * @return Instance of a class implementing the resource interface that can
 * be used for making requests to the server.
 */
@SuppressWarnings("unchecked")
public static <C> C newResource(final Class<C> resourceInterface,
                                final WebTarget target,
                                final boolean ignoreResourcePath,
                                final MultivaluedMap<String, Object> headers,
                                final List<Cookie> cookies,
                                final Form form,
                                final ClientMethodInvoker invoker) {

    return (C) Proxy.newProxyInstance(AccessController.doPrivileged(ReflectionHelper.getClassLoaderPA(resourceInterface)),
            new Class[]{resourceInterface},
            new WebResourceFactory(ignoreResourcePath ? target : addPathFromAnnotation(resourceInterface, target),
                    headers, cookies, form, invoker));
}