refactor: 提取工具类。

This commit is contained in:
Suomm 2023-06-23 15:59:14 +08:00
parent dc36280e8d
commit 3f786baf6c
2 changed files with 178 additions and 0 deletions

View File

@ -0,0 +1,64 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.processor.util;
import java.io.File;
/**
* 文件工具类
*
* @author 王帅
* @since 2023-06-22
*/
public class FileUtil {
private FileUtil() {
}
public static boolean isFromTestSource(String path) {
return path.contains("test-sources") || path.contains("test-annotations");
}
public static boolean isAbsolutePath(String path) {
return path != null && (path.startsWith("/") || path.contains(":"));
}
/**
* 获取项目的根目录也就是根节点 pom.xml 所在的目录
*/
public static String getProjectRootPath(String genFilePath) {
File file = new File(genFilePath);
int count = 20;
return getProjectRootPath(file, count);
}
public static String getProjectRootPath(File file, int count) {
if (count <= 0) {
return null;
}
if (file.isFile()) {
return getProjectRootPath(file.getParentFile(), --count);
} else {
if (new File(file, "pom.xml").exists() && !new File(file.getParentFile(), "pom.xml").exists()) {
return file.getAbsolutePath();
} else {
return getProjectRootPath(file.getParentFile(), --count);
}
}
}
}

View File

@ -0,0 +1,114 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.processor.util;
/**
* 字符串工具类
*
* @author 王帅
* @since 2023-06-22
*/
@SuppressWarnings("all")
public class StrUtil {
private StrUtil() {
}
/**
* com.mybatisflex.test.entity.Account -> Account
*/
public static String getClassName(String str) {
return str.substring(str.lastIndexOf(".") + 1);
}
public static boolean isBlank(String str) {
return str == null || str.trim().length() == 0;
}
public static String camelToUnderline(String str) {
if (isBlank(str)) {
return "";
}
int len = str.length();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = str.charAt(i);
if (Character.isUpperCase(c) && i > 0) {
sb.append('_');
}
sb.append(Character.toLowerCase(c));
}
return sb.toString();
}
public static String firstCharToLowerCase(String str) {
char firstChar = str.charAt(0);
if (firstChar >= 'A' && firstChar <= 'Z') {
char[] arr = str.toCharArray();
arr[0] += ('a' - 'A');
return new String(arr);
}
return str;
}
public static String firstCharToUpperCase(String str) {
char firstChar = str.charAt(0);
if (firstChar >= 'a' && firstChar <= 'z') {
char[] arr = str.toCharArray();
arr[0] -= ('a' - 'A');
return new String(arr);
}
return str;
}
public static String buildFieldName(String name, String style) {
if ("upperCase".equalsIgnoreCase(style)) {
return camelToUnderline(name).toUpperCase();
} else if ("lowerCase".equalsIgnoreCase(style)) {
return camelToUnderline(name).toLowerCase();
} else if ("upperCamelCase".equalsIgnoreCase(style)) {
return firstCharToUpperCase(name);
} else {
//lowerCamelCase
return firstCharToLowerCase(name);
}
}
public static String buildTableDefPackage(String entityClass) {
StringBuilder guessPackage = new StringBuilder();
if (!entityClass.contains(".")) {
guessPackage.append("table");
} else {
guessPackage.append(entityClass, 0, entityClass.lastIndexOf(".")).append(".table");
}
return guessPackage.toString();
}
public static String buildMapperPackage(String entityClass) {
if (!entityClass.contains(".")) {
return "mapper";
} else {
String entityPackage = entityClass.substring(0, entityClass.lastIndexOf("."));
if (entityPackage.contains(".")) {
return entityPackage.substring(0, entityPackage.lastIndexOf(".")) + ".mapper";
} else {
return "mapper";
}
}
}
}