Search in sources :

Example 1 with TypeReference

use of cn.hutool.core.lang.TypeReference in project hutool by dromara.

the class SpringUtil method getBean.

/**
 * 通过类型参考返回带泛型参数的Bean
 *
 * @param reference 类型参考,用于持有转换后的泛型类型
 * @param <T>       Bean类型
 * @return 带泛型参数的Bean
 * @since 5.4.0
 */
@SuppressWarnings("unchecked")
public static <T> T getBean(TypeReference<T> reference) {
    final ParameterizedType parameterizedType = (ParameterizedType) reference.getType();
    final Class<T> rawType = (Class<T>) parameterizedType.getRawType();
    final Class<?>[] genericTypes = Arrays.stream(parameterizedType.getActualTypeArguments()).map(type -> (Class<?>) type).toArray(Class[]::new);
    final String[] beanNames = getBeanFactory().getBeanNamesForType(ResolvableType.forClassWithGenerics(rawType, genericTypes));
    return getBean(beanNames[0], rawType);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Arrays(java.util.Arrays) DefaultSingletonBeanRegistry(org.springframework.beans.factory.support.DefaultSingletonBeanRegistry) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) BeansException(org.springframework.beans.BeansException) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ApplicationEvent(org.springframework.context.ApplicationEvent) TypeReference(cn.hutool.core.lang.TypeReference) Component(org.springframework.stereotype.Component) UtilException(cn.hutool.core.exceptions.UtilException) ParameterizedType(java.lang.reflect.ParameterizedType) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Map(java.util.Map) ArrayUtil(cn.hutool.core.util.ArrayUtil) ResolvableType(org.springframework.core.ResolvableType) ApplicationContextAware(org.springframework.context.ApplicationContextAware) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory)

Example 2 with TypeReference

use of cn.hutool.core.lang.TypeReference in project hutool by looly.

the class SpringUtil method getBean.

/**
 * 通过类型参考返回带泛型参数的Bean
 *
 * @param reference 类型参考,用于持有转换后的泛型类型
 * @param <T>       Bean类型
 * @return 带泛型参数的Bean
 * @since 5.4.0
 */
@SuppressWarnings("unchecked")
public static <T> T getBean(TypeReference<T> reference) {
    final ParameterizedType parameterizedType = (ParameterizedType) reference.getType();
    final Class<T> rawType = (Class<T>) parameterizedType.getRawType();
    final Class<?>[] genericTypes = Arrays.stream(parameterizedType.getActualTypeArguments()).map(type -> (Class<?>) type).toArray(Class[]::new);
    final String[] beanNames = getBeanFactory().getBeanNamesForType(ResolvableType.forClassWithGenerics(rawType, genericTypes));
    return getBean(beanNames[0], rawType);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Arrays(java.util.Arrays) DefaultSingletonBeanRegistry(org.springframework.beans.factory.support.DefaultSingletonBeanRegistry) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) BeansException(org.springframework.beans.BeansException) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ApplicationEvent(org.springframework.context.ApplicationEvent) TypeReference(cn.hutool.core.lang.TypeReference) Component(org.springframework.stereotype.Component) UtilException(cn.hutool.core.exceptions.UtilException) ParameterizedType(java.lang.reflect.ParameterizedType) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Map(java.util.Map) ArrayUtil(cn.hutool.core.util.ArrayUtil) ResolvableType(org.springframework.core.ResolvableType) ApplicationContextAware(org.springframework.context.ApplicationContextAware) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory)

Example 3 with TypeReference

use of cn.hutool.core.lang.TypeReference in project epic4j by huisunan.

the class BaseRunner method doStart.

public void doStart(String email, String password) {
    Browser browser = null;
    try {
        log.info("start {} work", email);
        // 用户文件路径
        String userDataPath = new FileUrlResource(epicConfig.getDataPath() + File.separator + email).getFile().getAbsolutePath();
        // 获取浏览器对象
        browser = iStart.getBrowser(userDataPath);
        // 获取默认page
        Page page = iStart.getDefaultPage(browser);
        // 加载cookie
        if (StrUtil.isNotBlank(epicConfig.getCookiePath()) && FileUtil.exist(epicConfig.getCookiePath())) {
            log.debug("load cookie");
            List<CookieParam> cookies = JSONUtil.toBean(IoUtil.read(new FileReader(epicConfig.getCookiePath())), new TypeReference<List<CookieParam>>() {
            }, false);
            page.setCookie(cookies);
        }
        // 反爬虫设置
        PageUtil.crawSet(page);
        // 打开epic主页
        page.goTo(epicConfig.getEpicUrl());
        boolean needLogin = iStart.needLogin(browser);
        log.debug("needLogin:{}", needLogin);
        if (needLogin) {
            iLogin.login(page, email, password);
        }
        List<Item> weekFreeItems = iStart.getWeekFreeItems(page);
        // 领取游戏
        List<Item> receive = iStart.receive(page, weekFreeItems);
        for (INotify notify : notifies) {
            if (notify.notifyReceive(receive)) {
                break;
            }
        }
    } catch (Exception e) {
        if (epicConfig.getErrorScreenShoot()) {
            Optional.ofNullable(browser).map(Browser::pages).filter(CollUtil::isNotEmpty).map(pages -> pages.get(0)).ifPresent(page -> {
                try {
                    FileUrlResource errorDir = new FileUrlResource("data/error");
                    log.debug("create error dir {}", errorDir.getFile().mkdirs());
                    ScreenshotOptions options = new ScreenshotOptions();
                    options.setQuality(100);
                    options.setPath(errorDir.getFile().getAbsolutePath() + File.separator + System.currentTimeMillis() + ".jpg");
                    options.setType("jpeg");
                    page.screenshot(options);
                } catch (IOException ioException) {
                    log.error("截图失败");
                }
            });
        }
        log.error("程序异常", e);
    } finally {
        if (browser != null) {
            browser.close();
        }
    }
}
Also used : DateUtil(cn.hutool.core.date.DateUtil) Item(com.hsn.epic4j.bean.Item) SneakyThrows(lombok.SneakyThrows) Autowired(org.springframework.beans.factory.annotation.Autowired) JSONUtil(cn.hutool.json.JSONUtil) PreDestroy(javax.annotation.PreDestroy) DateTime(cn.hutool.core.date.DateTime) FileUrlResource(org.springframework.core.io.FileUrlResource) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Browser(com.ruiyun.jvppeteer.core.browser.Browser) UserInfo(com.hsn.epic4j.bean.UserInfo) INotify(com.hsn.epic4j.notify.INotify) IoUtil(cn.hutool.core.io.IoUtil) EpicConfig(com.hsn.epic4j.config.EpicConfig) ScreenshotOptions(com.ruiyun.jvppeteer.options.ScreenshotOptions) IOException(java.io.IOException) File(java.io.File) CronTrigger(org.springframework.scheduling.support.CronTrigger) Page(com.ruiyun.jvppeteer.core.page.Page) CollUtil(cn.hutool.core.collection.CollUtil) TypeReference(cn.hutool.core.lang.TypeReference) StrUtil(cn.hutool.core.util.StrUtil) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) CookieParam(com.ruiyun.jvppeteer.protocol.network.CookieParam) DateField(cn.hutool.core.date.DateField) Optional(java.util.Optional) FileUtil(cn.hutool.core.io.FileUtil) PageUtil(com.hsn.epic4j.util.PageUtil) FileReader(java.io.FileReader) INotify(com.hsn.epic4j.notify.INotify) ScreenshotOptions(com.ruiyun.jvppeteer.options.ScreenshotOptions) Page(com.ruiyun.jvppeteer.core.page.Page) IOException(java.io.IOException) IOException(java.io.IOException) CookieParam(com.ruiyun.jvppeteer.protocol.network.CookieParam) Item(com.hsn.epic4j.bean.Item) FileReader(java.io.FileReader) List(java.util.List) FileUrlResource(org.springframework.core.io.FileUrlResource) Browser(com.ruiyun.jvppeteer.core.browser.Browser)

Aggregations

TypeReference (cn.hutool.core.lang.TypeReference)3 Component (org.springframework.stereotype.Component)3 UtilException (cn.hutool.core.exceptions.UtilException)2 ArrayUtil (cn.hutool.core.util.ArrayUtil)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Arrays (java.util.Arrays)2 Map (java.util.Map)2 BeansException (org.springframework.beans.BeansException)2 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)2 BeanFactoryPostProcessor (org.springframework.beans.factory.config.BeanFactoryPostProcessor)2 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)2 DefaultSingletonBeanRegistry (org.springframework.beans.factory.support.DefaultSingletonBeanRegistry)2 ApplicationContext (org.springframework.context.ApplicationContext)2 ApplicationContextAware (org.springframework.context.ApplicationContextAware)2 CollUtil (cn.hutool.core.collection.CollUtil)1 DateField (cn.hutool.core.date.DateField)1 DateTime (cn.hutool.core.date.DateTime)1 DateUtil (cn.hutool.core.date.DateUtil)1 FileUtil (cn.hutool.core.io.FileUtil)1 IoUtil (cn.hutool.core.io.IoUtil)1