Java Code Examples for cn.hutool.core.util.ReflectUtil#newInstance()

The following examples show how to use cn.hutool.core.util.ReflectUtil#newInstance() . 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: IocHelper.java    From openAGV with Apache License 2.0 6 votes vote down vote up
private static Object getDbInjectDao(Field field, Class<?> paramTypeClass) {
        String key = paramTypeClass.getName();
        Object dbDaoObj = DB_DAO_MAP.get(key);
        if (ToolsKit.isEmpty(dbDaoObj)) {
            String dbClientId = "";
            if (null != field) {
                DbClient dbClient = field.getAnnotation(DbClient.class);
                dbClientId = ToolsKit.isNotEmpty(dbClient) ? dbClient.id() : "";
                if (ToolsKit.isEmpty(dbClientId)) {
                    Import importAnn = field.getAnnotation(Import.class);
                    dbClientId = importAnn.client();
                }
            }
//            List<?> proxyList = null;
//            dbDaoObj =  MongoUtils.getMongoDao(dbClientId, paramTypeClass, proxyList);
            dbDaoObj = ReflectUtil.newInstance(MongoDao.class, dbClientId, paramTypeClass);
            DB_DAO_MAP.put(key, dbDaoObj);
        }
        return DB_DAO_MAP.get(key);
    }
 
Example 2
Source File: RouteHelper.java    From openAGV with Apache License 2.0 6 votes vote down vote up
private void routeAction() {
    if (isRestart()) {
        return;
    }
    if (ACTION_ROUTE_MAP.isEmpty()) {
        List<Class<?>> actionClassList = ClassHelper.duang().getActionClassList();
        if (ToolsKit.isEmpty(actionClassList)) {
            LOG.info("工站逻辑处理类为空,退出routeAction方法");
            return;
        }
        for (Class<?> actionClass : actionClassList) {
            Action actionAonn = actionClass.getAnnotation(Action.class);
            if (ToolsKit.isEmpty(actionAonn)) {
                continue;
            }
            IAction action = (IAction) ReflectUtil.newInstance(actionClass);
            if (ToolsKit.isNotEmpty(action)) {
                String key = action.actionKey();
                Route route = new Route(key, action);
                ACTION_ROUTE_MAP.put(key, route);
                BeanHelper.duang().setBean(route.getServiceObj());
            }
        }
        printActionKey();
    }
}
 
Example 3
Source File: RouteHelper.java    From openAGV with Apache License 2.0 6 votes vote down vote up
private void routeListener() {
    if (LISTENER_ROUTE_MAP.isEmpty()) {
        List<Class<?>> listenerClassList = ClassHelper.duang().getListenerClassList();
        if (ToolsKit.isEmpty(listenerClassList)) {
            LOG.info("监听器类为空,退出routeListener方法");
            return;
        }
        for (Class<?> listenerClass : listenerClassList) {
            Listener listenerAnnot = listenerClass.getAnnotation(Listener.class);
            if (ToolsKit.isEmpty(listenerAnnot)) {
                continue;
            }
            EventListener eventListener = (EventListener) ReflectUtil.newInstance(listenerClass);
            if (ToolsKit.isNotEmpty(eventListener)) {
                String key = listenerAnnot.key();
                if (ToolsKit.isEmpty(key)) {
                    key = listenerClass.getName();
                }
                Route route = new Route(key, eventListener);
                LISTENER_ROUTE_MAP.put(key, route);
                BeanHelper.duang().setBean(route.getServiceObj());
            }
        }
        printListenetKey();
    }
}
 
Example 4
Source File: HtmlMakerFactory.java    From bitchat with Apache License 2.0 6 votes vote down vote up
/**
 * 创建HtmlMaker实例
 */
public HtmlMaker build(HtmlMakerEnum type, Class<? extends HtmlMaker> clazz) {
    if (type == null) {
        return null;
    } else {
        HtmlMaker htmlMaker = htmlMakerMap.get(type);
        if (htmlMaker == null) {
            lock.lock();
            try {
                if (!htmlMakerMap.containsKey(type)) {
                    htmlMaker = ReflectUtil.newInstance(clazz);
                    htmlMakerMap.putIfAbsent(type, htmlMaker);
                } else {
                    htmlMaker = htmlMakerMap.get(type);
                }
            } finally {
                lock.unlock();
            }
        }
        return htmlMaker;
    }
}
 
Example 5
Source File: HtmlMakerFactory.java    From redant with Apache License 2.0 6 votes vote down vote up
/**
 * 创建HtmlMaker实例
 */
public HtmlMaker build(HtmlMakerEnum type,Class<? extends HtmlMaker> clazz){
    if(type==null){
        return null;
    }else{
        HtmlMaker htmlMaker = htmlMakerMap.get(type);
        if(htmlMaker==null){
            lock.lock();
            try {
                if(!htmlMakerMap.containsKey(type)) {
                    htmlMaker = ReflectUtil.newInstance(clazz);
                    htmlMakerMap.putIfAbsent(type,htmlMaker);
                }else{
                    htmlMaker = htmlMakerMap.get(type);
                }
            }finally {
                lock.unlock();
            }
        }
        return htmlMaker;
    }
}
 
Example 6
Source File: AuthRequestFactory.java    From justauth-spring-boot-starter with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 获取自定义的 request
 *
 * @param clazz  枚举类 {@link AuthSource}
 * @param source {@link AuthSource}
 * @return {@link AuthRequest}
 */
@SuppressWarnings("unchecked")
private AuthRequest getExtendRequest(Class clazz, String source) {
    try {
        EnumUtil.fromString(clazz, source.toUpperCase());
    } catch (IllegalArgumentException e) {
        // 无自定义匹配
        return null;
    }

    Map<String, ExtendProperties.ExtendRequestConfig> extendConfig = properties.getExtend().getConfig();

    // key 转大写
    Map<String, ExtendProperties.ExtendRequestConfig> upperConfig = new HashMap<>(6);
    extendConfig.forEach((k, v) -> upperConfig.put(k.toUpperCase(), v));

    ExtendProperties.ExtendRequestConfig extendRequestConfig = upperConfig.get(source.toUpperCase());
    if (extendRequestConfig != null) {
        Class<? extends AuthRequest> requestClass = extendRequestConfig.getRequestClass();

        if (requestClass != null) {
            // 反射获取 Request 对象,所以必须实现 2 个参数的构造方法
            return ReflectUtil.newInstance(requestClass, (AuthConfig) extendRequestConfig, authStateCache);
        }
    }

    return null;
}
 
Example 7
Source File: HutoolController.java    From mall-learning with Apache License 2.0 5 votes vote down vote up
@ApiOperation("ReflectUtil使用:Java反射工具类")
@GetMapping("/reflectUtil")
public CommonResult reflectUtil() {
    //获取某个类的所有方法
    Method[] methods = ReflectUtil.getMethods(PmsBrand.class);
    //获取某个类的指定方法
    Method method = ReflectUtil.getMethod(PmsBrand.class, "getId");
    //使用反射来创建对象
    PmsBrand pmsBrand = ReflectUtil.newInstance(PmsBrand.class);
    //反射执行对象的方法
    ReflectUtil.invoke(pmsBrand, "setId", 1);
    return CommonResult.success(null, "操作成功");
}
 
Example 8
Source File: PageUtil.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 校验分页参数,为NULL,设置分页参数默认值
 *
 * @param condition 查询参数
 * @param clazz     类
 * @param <T>       {@link PageCondition}
 */
public static <T extends PageCondition> void checkPageCondition(T condition, Class<T> clazz) {
    if (ObjectUtil.isNull(condition)) {
        condition = ReflectUtil.newInstance(clazz);
    }
    // 校验分页参数
    if (ObjectUtil.isNull(condition.getCurrentPage())) {
        condition.setCurrentPage(Consts.DEFAULT_CURRENT_PAGE);
    }
    if (ObjectUtil.isNull(condition.getPageSize())) {
        condition.setPageSize(Consts.DEFAULT_PAGE_SIZE);
    }
}
 
Example 9
Source File: PageUtil.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 校验分页参数,为NULL,设置分页参数默认值
 *
 * @param condition 查询参数
 * @param clazz     类
 * @param <T>       {@link PageCondition}
 */
public static <T extends PageCondition> void checkPageCondition(T condition, Class<T> clazz) {
    if (ObjectUtil.isNull(condition)) {
        condition = ReflectUtil.newInstance(clazz);
    }
    // 校验分页参数
    if (ObjectUtil.isNull(condition.getCurrentPage())) {
        condition.setCurrentPage(Consts.DEFAULT_CURRENT_PAGE);
    }
    if (ObjectUtil.isNull(condition.getPageSize())) {
        condition.setPageSize(Consts.DEFAULT_PAGE_SIZE);
    }
}
 
Example 10
Source File: PageUtil.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 校验分页参数,为NULL,设置分页参数默认值
 *
 * @param condition 查询参数
 * @param clazz     类
 * @param <T>       {@link PageCondition}
 */
public static <T extends PageCondition> void checkPageCondition(T condition, Class<T> clazz) {
    if (ObjectUtil.isNull(condition)) {
        condition = ReflectUtil.newInstance(clazz);
    }
    // 校验分页参数
    if (ObjectUtil.isNull(condition.getCurrentPage())) {
        condition.setCurrentPage(Consts.DEFAULT_CURRENT_PAGE);
    }
    if (ObjectUtil.isNull(condition.getPageSize())) {
        condition.setPageSize(Consts.DEFAULT_PAGE_SIZE);
    }
}
 
Example 11
Source File: Route.java    From openAGV with Apache License 2.0 4 votes vote down vote up
public Route(Class<?> clazz, Map<String, Method> methodMap) {
    this.key = clazz.getName();
    this.serviceObj = ReflectUtil.newInstance(clazz);
    this.serviceClass = clazz;
    this.methodMap.putAll(methodMap);
}