mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-04 04:37:59 +08:00
add methods
This commit is contained in:
parent
e14a1a5ad8
commit
2801373c9e
@ -8,7 +8,7 @@
|
||||
### 新特性
|
||||
* 【setting】 Setting中增加addSetting和autoLoad重载(pr#104@Gitee)
|
||||
* 【core 】 增加copyProperties,根据Class创建对象并进行属性拷贝(pr#105@Gitee)
|
||||
* 【core 】 增加copyProperties,根据Class创建对象并进行属性拷贝(pr#105@Gitee)
|
||||
* 【core 】 添加获取class当前文件夹名称方法(pr#106@Gitee)
|
||||
|
||||
### Bug修复
|
||||
|
||||
|
@ -5,7 +5,7 @@ import cn.hutool.core.text.ASCIIStrCache;
|
||||
/**
|
||||
* 字符工具类<br>
|
||||
* 部分工具来自于Apache Commons系列
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
* @since 4.0.1
|
||||
*/
|
||||
@ -41,7 +41,7 @@ public class CharUtil {
|
||||
* CharUtil.isAscii('\n') = true
|
||||
* CharUtil.isAscii('©') = false
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param ch 被检查的字符处
|
||||
* @return true表示为ASCII字符,ASCII字符位于0~127之间
|
||||
*/
|
||||
@ -60,7 +60,7 @@ public class CharUtil {
|
||||
* CharUtil.isAsciiPrintable('\n') = false
|
||||
* CharUtil.isAsciiPrintable('©') = false
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param ch 被检查的字符处
|
||||
* @return true表示为ASCII可见字符,可见字符位于32~126之间
|
||||
*/
|
||||
@ -79,7 +79,7 @@ public class CharUtil {
|
||||
* CharUtil.isAsciiControl('\n') = true
|
||||
* CharUtil.isAsciiControl('©') = false
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param ch 被检查的字符
|
||||
* @return true表示为控制符,控制符位于0~31和127
|
||||
*/
|
||||
@ -99,7 +99,7 @@ public class CharUtil {
|
||||
* CharUtil.isLetter('\n') = false
|
||||
* CharUtil.isLetter('©') = false
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param ch 被检查的字符
|
||||
* @return true表示为字母(包括大写字母和小写字母)字母包括A~Z和a~z
|
||||
*/
|
||||
@ -120,7 +120,7 @@ public class CharUtil {
|
||||
* CharUtil.isLetterUpper('\n') = false
|
||||
* CharUtil.isLetterUpper('©') = false
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param ch 被检查的字符
|
||||
* @return true表示为大写字母,大写字母包括A~Z
|
||||
*/
|
||||
@ -141,7 +141,7 @@ public class CharUtil {
|
||||
* CharUtil.isLetterLower('\n') = false
|
||||
* CharUtil.isLetterLower('©') = false
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param ch 被检查的字符
|
||||
* @return true表示为小写字母,小写字母指a~z
|
||||
*/
|
||||
@ -162,14 +162,14 @@ public class CharUtil {
|
||||
* CharUtil.isNumber('\n') = false
|
||||
* CharUtil.isNumber('©') = false
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param ch 被检查的字符
|
||||
* @return true表示为数字字符,数字字符指0~9
|
||||
*/
|
||||
public static boolean isNumber(char ch) {
|
||||
return ch >= '0' && ch <= '9';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 是否为16进制规范的字符,判断是否为如下字符
|
||||
* <pre>
|
||||
@ -177,7 +177,7 @@ public class CharUtil {
|
||||
* 2. a~f
|
||||
* 4. A~F
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param c 字符
|
||||
* @return 是否为16进制规范的字符
|
||||
* @since 4.1.5
|
||||
@ -197,7 +197,7 @@ public class CharUtil {
|
||||
* CharUtil.isLetterOrNumber('\n') = false
|
||||
* CharUtil.isLetterOrNumber('©') = false
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param ch 被检查的字符
|
||||
* @return true表示为字符或数字,包括A~Z、a~z、0~9
|
||||
*/
|
||||
@ -208,7 +208,7 @@ public class CharUtil {
|
||||
/**
|
||||
* 字符转为字符串<br>
|
||||
* 如果为ASCII字符,使用缓存
|
||||
*
|
||||
*
|
||||
* @param c 字符
|
||||
* @return 字符串
|
||||
* @see ASCIIStrCache#toString(char)
|
||||
@ -219,12 +219,12 @@ public class CharUtil {
|
||||
|
||||
/**
|
||||
* 给定类名是否为字符类,字符类包括:
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* Character.class
|
||||
* char.class
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param clazz 被检查的类
|
||||
* @return true表示为字符类
|
||||
*/
|
||||
@ -234,12 +234,12 @@ public class CharUtil {
|
||||
|
||||
/**
|
||||
* 给定对象对应的类是否为字符类,字符类包括:
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* Character.class
|
||||
* char.class
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param value 被检查的对象
|
||||
* @return true表示为字符类
|
||||
*/
|
||||
@ -251,7 +251,7 @@ public class CharUtil {
|
||||
/**
|
||||
* 是否空白符<br>
|
||||
* 空白符包括空格、制表符、全角空格和不间断空格<br>
|
||||
*
|
||||
*
|
||||
* @param c 字符
|
||||
* @return 是否空白符
|
||||
* @see Character#isWhitespace(int)
|
||||
@ -261,15 +261,15 @@ public class CharUtil {
|
||||
public static boolean isBlankChar(char c) {
|
||||
return isBlankChar((int) c);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 是否空白符<br>
|
||||
* 空白符包括空格、制表符、全角空格和不间断空格<br>
|
||||
*
|
||||
* @see Character#isWhitespace(int)
|
||||
* @see Character#isSpaceChar(int)
|
||||
*
|
||||
* @param c 字符
|
||||
* @return 是否空白符
|
||||
* @see Character#isWhitespace(int)
|
||||
* @see Character#isSpaceChar(int)
|
||||
* @since 4.0.10
|
||||
*/
|
||||
public static boolean isBlankChar(int c) {
|
||||
@ -278,14 +278,14 @@ public class CharUtil {
|
||||
|
||||
/**
|
||||
* 判断是否为emoji表情符<br>
|
||||
*
|
||||
*
|
||||
* @param c 字符
|
||||
* @return 是否为emoji
|
||||
* @since 4.0.8
|
||||
*/
|
||||
public static boolean isEmoji(char c) {
|
||||
//noinspection ConstantConditions
|
||||
return false == ((c == 0x0) || //
|
||||
return false == ((c == 0x0) || //
|
||||
(c == 0x9) || //
|
||||
(c == 0xA) || //
|
||||
(c == 0xD) || //
|
||||
@ -293,11 +293,11 @@ public class CharUtil {
|
||||
((c >= 0xE000) && (c <= 0xFFFD)) || //
|
||||
((c >= 0x100000) && (c <= 0x10FFFF)));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 是否为Windows或者Linux(Unix)文件分隔符<br>
|
||||
* Windows平台下分隔符为\,Linux(Unix)为/
|
||||
*
|
||||
*
|
||||
* @param c 字符
|
||||
* @return 是否为Windows或者Linux(Unix)文件分隔符
|
||||
* @since 4.1.11
|
||||
@ -308,9 +308,9 @@ public class CharUtil {
|
||||
|
||||
/**
|
||||
* 比较两个字符是否相同
|
||||
*
|
||||
* @param c1 字符1
|
||||
* @param c2 字符2
|
||||
*
|
||||
* @param c1 字符1
|
||||
* @param c2 字符2
|
||||
* @param ignoreCase 是否忽略大小写
|
||||
* @return 是否相同
|
||||
* @since 4.0.3
|
||||
@ -324,11 +324,12 @@ public class CharUtil {
|
||||
|
||||
/**
|
||||
* 获取字符类型
|
||||
*
|
||||
* @param c 字符
|
||||
* @return 字符类型
|
||||
* @since 5.2.3
|
||||
*/
|
||||
public static int getType(int c){
|
||||
public static int getType(int c) {
|
||||
return Character.getType(c);
|
||||
}
|
||||
}
|
||||
|
@ -1054,4 +1054,35 @@ public class ClassUtil {
|
||||
|| objectPackageName.startsWith("javax.") //
|
||||
|| clazz.getClassLoader() == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取class类路径URL, 不管是否在jar包中都会返回文件夹的路径<br>
|
||||
* class在jar包中返回jar所在文件夹,class不在jar中返回文件夹目录<br>
|
||||
* jdk中的类不能使用此方法
|
||||
*
|
||||
* @return URL
|
||||
* @since 5.2.4
|
||||
*/
|
||||
public static URL getLocation(Class<?> clazz) {
|
||||
if (null == clazz) {
|
||||
return null;
|
||||
}
|
||||
return clazz.getProtectionDomain().getCodeSource().getLocation();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取class类路径, 不管是否在jar包中都会返回文件夹的路径<br>
|
||||
* class在jar包中返回jar所在文件夹,class不在jar中返回文件夹目录<br>
|
||||
* jdk中的类不能使用此方法
|
||||
*
|
||||
* @return class路径
|
||||
* @since 5.2.4
|
||||
*/
|
||||
public static String getLocationPath(Class<?> clazz) {
|
||||
final URL location = getLocation(clazz);
|
||||
if (null == location) {
|
||||
return null;
|
||||
}
|
||||
return location.getPath();
|
||||
}
|
||||
}
|
@ -1,11 +1,12 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* {@link ClassUtil} 单元测试
|
||||
*
|
||||
@ -35,7 +36,7 @@ public class ClassUtilTest {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@SuppressWarnings({"unused", "InnerClassMayBeStatic"})
|
||||
class TestSubClass extends TestClass {
|
||||
private String subField;
|
||||
|
||||
@ -102,4 +103,10 @@ public class ClassUtilTest {
|
||||
String result = ClassUtil.getShortClassName(className);
|
||||
Assert.assertEquals("c.h.c.u.StrUtil", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLocationPathTest(){
|
||||
final String classDir = ClassUtil.getLocationPath(ClassUtilTest.class);
|
||||
Assert.assertTrue(Objects.requireNonNull(classDir).endsWith("/hutool-core/target/test-classes/"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user