Skip to content

Commit

Permalink
优化部分badsmell:包括空指针风险以及多次使用变量的提取 (elunez#772)
Browse files Browse the repository at this point in the history
* 获取验证码逻辑,无需加锁,不涉及线程安全问题,提高效率

* 自定义生成策略时,Map容量设置不准确,会导致无必要的扩容

* 优化badsmell:包括空指针以及多次使用变量的提取

* 修改优化部分badsmeall
  • Loading branch information
kdjj2006 authored Oct 24, 2022
1 parent 164ce50 commit e0af08e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class GeneratorServiceImpl implements GeneratorService {

private final ColumnInfoRepository columnInfoRepository;

private final String CONFIG_MESSAGE = "请先配置生成器";
@Override
public Object getTables() {
// 使用预编译防止sql注入
Expand Down Expand Up @@ -169,7 +170,7 @@ public void save(List<ColumnInfo> columnInfos) {
@Override
public void generator(GenConfig genConfig, List<ColumnInfo> columns) {
if (genConfig.getId() == null) {
throw new BadRequestException("请先配置生成器");
throw new BadRequestException(CONFIG_MESSAGE);
}
try {
GenUtil.generatorCode(columns, genConfig);
Expand All @@ -182,7 +183,7 @@ public void generator(GenConfig genConfig, List<ColumnInfo> columns) {
@Override
public ResponseEntity<Object> preview(GenConfig genConfig, List<ColumnInfo> columns) {
if (genConfig.getId() == null) {
throw new BadRequestException("请先配置生成器");
throw new BadRequestException(CONFIG_MESSAGE);
}
List<Map<String, Object>> genList = GenUtil.preview(columns, genConfig);
return new ResponseEntity<>(genList, HttpStatus.OK);
Expand All @@ -191,7 +192,7 @@ public ResponseEntity<Object> preview(GenConfig genConfig, List<ColumnInfo> colu
@Override
public void download(GenConfig genConfig, List<ColumnInfo> columns, HttpServletRequest request, HttpServletResponse response) {
if (genConfig.getId() == null) {
throw new BadRequestException("请先配置生成器");
throw new BadRequestException(CONFIG_MESSAGE);
}
try {
File file = new File(GenUtil.download(columns, genConfig));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public class MenuServiceImpl implements MenuService {
private final RoleService roleService;
private final RedisUtils redisUtils;

private static final String HTTP_PRE = "http://";
private static final String HTTPS_PRE = "https://";
private static final String YES_STR = "是";
private static final String NO_STR = "否";
private static final String BAD_REQUEST = "外链必须以http://或者https://开头";

@Override
public List<MenuDto> queryAll(MenuQueryCriteria criteria, Boolean isQuery) throws Exception {
Sort sort = Sort.by(Sort.Direction.ASC, "menuSort");
Expand Down Expand Up @@ -114,13 +120,12 @@ public void create(Menu resources) {
throw new EntityExistException(Menu.class,"componentName",resources.getComponentName());
}
}
if(resources.getPid().equals(0L)){
if (Long.valueOf(0L).equals(resources.getPid())) {
resources.setPid(null);
}
if(resources.getIFrame()){
String http = "http://", https = "https://";
if (!(resources.getPath().toLowerCase().startsWith(http)||resources.getPath().toLowerCase().startsWith(https))) {
throw new BadRequestException("外链必须以http://或者https://开头");
if (!(resources.getPath().toLowerCase().startsWith(HTTP_PRE)||resources.getPath().toLowerCase().startsWith(HTTPS_PRE))) {
throw new BadRequestException(BAD_REQUEST);
}
}
menuRepository.save(resources);
Expand All @@ -140,9 +145,8 @@ public void update(Menu resources) {
ValidationUtil.isNull(menu.getId(),"Permission","id",resources.getId());

if(resources.getIFrame()){
String http = "http://", https = "https://";
if (!(resources.getPath().toLowerCase().startsWith(http)||resources.getPath().toLowerCase().startsWith(https))) {
throw new BadRequestException("外链必须以http://或者https://开头");
if (!(resources.getPath().toLowerCase().startsWith(HTTP_PRE)||resources.getPath().toLowerCase().startsWith(HTTPS_PRE))) {
throw new BadRequestException(BAD_REQUEST);
}
}
Menu menu1 = menuRepository.findByTitle(resources.getTitle());
Expand Down Expand Up @@ -322,9 +326,9 @@ public void download(List<MenuDto> menuDtos, HttpServletResponse response) throw
map.put("菜单标题", menuDTO.getTitle());
map.put("菜单类型", menuDTO.getType() == null ? "目录" : menuDTO.getType() == 1 ? "菜单" : "按钮");
map.put("权限标识", menuDTO.getPermission());
map.put("外链菜单", menuDTO.getIFrame() ? "是" : "否");
map.put("菜单可见", menuDTO.getHidden() ? "否" : "是");
map.put("是否缓存", menuDTO.getCache() ? "是" : "否");
map.put("外链菜单", menuDTO.getIFrame() ? YES_STR : NO_STR);
map.put("菜单可见", menuDTO.getHidden() ? NO_STR : YES_STR);
map.put("是否缓存", menuDTO.getCache() ? YES_STR : NO_STR);
map.put("创建日期", menuDTO.getCreateTime());
list.add(map);
}
Expand Down

0 comments on commit e0af08e

Please sign in to comment.