This commit is contained in:
maxf 2019-01-02 14:08:21 +08:00
parent 67d6f27cae
commit 5129cdd162
5 changed files with 291 additions and 27 deletions

View File

@ -1,6 +1,12 @@
yexuejc-base 更新记录
------------------
#### version 1.3.1
**time2019-1-2 14:06:47** <br/>
**branch** master <br/>
**update** <br/>
>1. objUtil 增加类字段(驼峰)转换成下划线
#
#### version 1.3.0
**time2018-12-30 16:47:50** <br/>
**branch** master <br/>

60
pom.xml
View File

@ -6,7 +6,7 @@
<groupId>top.yexuejc</groupId>
<artifactId>yexuejc-base</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
<name>${project.artifactId}</name>
<url>https://github.com/yexuejc/yexuejc-base</url>
@ -39,6 +39,7 @@
<properties>
<repos.yexuejc.url>https://nexus.yexuejc.club/repository/</repos.yexuejc.url>
<repos.mcworle.url>https://nexus.mcworle.com/repository/</repos.mcworle.url>
<repos.aliyun.url>http://maven.aliyun.com/nexus/content/groups/public</repos.aliyun.url>
<repos.jitpack.url>https://jitpack.io</repos.jitpack.url>
<jjwt.version>0.7.0</jjwt.version>
@ -148,19 +149,19 @@
<!--</executions>-->
<!--</plugin>-->
<!-- GPG -->
<plugin> <!-- 进行延签 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<!--<plugin> &lt;!&ndash; 进行延签 &ndash;&gt;-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-gpg-plugin</artifactId>-->
<!--<version>1.6</version>-->
<!--<executions>-->
<!--<execution>-->
<!--<phase>verify</phase>-->
<!--<goals>-->
<!--<goal>sign</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
</plugins>
</build>
<repositories>
@ -183,16 +184,16 @@
<!-- 中间件jar包发布目标 -->
<distributionManagement>
<!--中央仓库发布-->
<snapshotRepository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>sonatype-nexus-staging</id>
<name>Nexus Release Repository</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
<!--<snapshotRepository>-->
<!--<id>sonatype-nexus-snapshots</id>-->
<!--<name>Sonatype Nexus Snapshots</name>-->
<!--<url>https://oss.sonatype.org/content/repositories/snapshots/</url>-->
<!--</snapshotRepository>-->
<!--<repository>-->
<!--<id>sonatype-nexus-staging</id>-->
<!--<name>Nexus Release Repository</name>-->
<!--<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>-->
<!--</repository>-->
<!-- 私服仓库发布
<repository>
@ -206,6 +207,17 @@
<url>${repos.yexuejc.url}maven-snapshots/</url>
</snapshotRepository>
-->
<repository>
<id>releases</id>
<name>nexus-release</name>
<url>${repos.mcworle.url}maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>nexus-snapshots</name>
<url>${repos.mcworle.url}maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<profiles>

View File

@ -1,9 +1,8 @@
package com.yexuejc.base.util;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.sun.org.apache.xml.internal.serializer.OutputPropertyUtils;
import java.io.*;
import java.lang.reflect.Field;
import java.util.*;
/**
* 对象工具对类的操作
@ -18,6 +17,186 @@ public class ObjUtil {
private ObjUtil() {
}
/**
* <p>把对象按照{@link ToUeProperty}注解转换</p>
* <i>字段值为空不输出</i>
*
* @param obj 要转换的对象
* @return 转换后的值以Map返回
*/
public static Map<String, Object> getToUeMap(Object obj) {
return getUnderlineMap(obj, true, false);
}
/**
* <p>把对象按照注解转换</p>
* <i>字段值为空不输出</i>
*
* @param obj 要转换的对象
* @return 转换后的值toJson以String返回
*/
public static String getToUeStr(Object obj) {
return JsonUtil.obj2Json(getUnderlineMap(obj, true, false));
}
/**
* <p>把对象驼峰字段转换成下划线输出支持继承和字段类型为对象</p>
* <i>字段值为空不输出</i>
*
* @param obj 要转换的对象
* @return 转换后的值以Map返回
*/
public static Map<String, Object> getUnderlineMap(Object obj) {
return getUnderlineMap(obj, true, false);
}
/**
* <p>把对象驼峰字段转换成下划线输出支持继承和字段类型为对象</p>
* <i>字段值为空不输出</i>
*
* @param obj 要转换的对象
* @return 转换后的值toJson以String返回
*/
public static String getUnderlineStr(Object obj) {
return JsonUtil.obj2Json(getUnderlineMap(obj, true, false));
}
/**
* 把对象字段按{@link ToUeProperty}注解规则转换输出支持继承和字段类型为对象
*
* @param obj 要转换的对象
* @param isAnnotationAll 是否全部依赖注解转换全部依赖注解转换true 只有字段上有注解的才转换没有注解的默认不转换false 有注解的依照注解转换没有的全传下划线
* @param putNull 是否映射null
* @return 转换后的值toJson以String返回
*/
public static String getUnderlineStr(Object obj, boolean isAnnotationAll, boolean putNull) {
return JsonUtil.obj2Json(getUnderlineMap(obj, isAnnotationAll, putNull));
}
/**
* 把对象字段按{@link ToUeProperty}注解规则转换成输出支持继承和字段类型为对象
* <p>主要功能解决输出时驼峰-下划线的转换</p>
*
* @param obj 要转换的对象
* @param isAnnotationAll 是否全部依赖注解转换全部依赖注解转换true 只有字段上有注解的才转换没有注解的默认不转换false 有注解的依照注解转换没有的全传下划线
* @param putNull 是否映射null
* @return
*/
public static Map<String, Object> getUnderlineMap(Object obj, boolean isAnnotationAll, boolean putNull) {
Class<?> bindClass = obj.getClass();
Map<String, Object> objMap = new HashMap<>(0);
/*
* 得到类中的所有属性集合
*/
try {
List<Field> fieldList = new ArrayList<>();
//当父类为null的时候说明到达了最上层的父类(Object类).
while (bindClass != null) {
fieldList.addAll(Arrays.asList(bindClass.getDeclaredFields()));
//得到父类,然后赋给自己
bindClass = bindClass.getSuperclass();
}
for (Field f : fieldList) {
//排除序列化
if ("serialVersionUID".equals(f.getName())) {
continue;
}
//设置些属性是可以访问的
f.setAccessible(true);
String fName = f.getName();
if (!isAnnotationAll) {
fName = StrUtil.camelToUnderline(f.getName());
}
boolean annotationPresent = f.isAnnotationPresent(ToUeProperty.class);
if (annotationPresent) {
ToUeProperty annotation = f.getAnnotation(ToUeProperty.class);
String value = annotation.value();
if (StrUtil.isNotEmpty(value)) {
fName = value;
}
}
Object o = f.get(obj);
if (null == o || o.getClass().isPrimitive() || o instanceof String) {
if (null == o && !putNull) {
continue;
}
objMap.put(fName, f.get(obj));
} else {
Map<String, Object> underlineMap = getUnderlineMap(o, isAnnotationAll, putNull);
objMap.put(fName, underlineMap);
}
}
} catch (IllegalAccessException e) {
System.out.println(e.getMessage());
}
return objMap;
}
public static void main(String[] args) {
B a = new B();
a.nameFirst = "张三";
a.ageInt = "5165458";
a.testAss = "asdasdsad";
a.setaM1("method1");
a.setbM1("b1Mthod1");
a.protectedStr = "protectedStr";
a.c = new C();
a.c.ageInt = "test";
Map<String, Object> underlineMap = getUnderlineMap(a, false, false);
System.out.println(JsonUtil.obj2Json(underlineMap));
}
static class A implements Serializable {
private static final long serialVersionUID = -8462118058721865488L;
public String nameFirst;
public String ageInt;
private String aM1;
@ToUeProperty("p_str")
protected String protectedStr;
public String getaM1() {
return aM1;
}
public A setaM1(String aM1) {
this.aM1 = aM1;
return this;
}
}
static class B extends A {
private static final long serialVersionUID = -8462118058721865488L;
public String testAss;
private String bM1;
private C c;
public String getbM1() {
return bM1;
}
public B setbM1(String bM1) {
this.bM1 = bM1;
return this;
}
}
static class C extends A {
private static final long serialVersionUID = -8462118058721865488L;
public String testAss;
private String bM1;
public String getbM1() {
return bM1;
}
public C setbM1(String bM1) {
this.bM1 = bM1;
return this;
}
}
/**
* <h2>深度克隆对象</h2>
* <p>

View File

@ -366,4 +366,57 @@ public final class StrUtil {
}
return msg;
}
/**
* 下划线字符
*/
public static final char UNDERLINE = '_';
/**
* 字符串下划线转驼峰格式
*
* @param param 需要转换的字符串
* @return 转换好的字符串
*/
public static String underlineToCamel(String param) {
if (isEmpty(param)) {
return "";
}
String temp = param.toLowerCase();
int len = temp.length();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = temp.charAt(i);
if (c == UNDERLINE) {
if (++i < len) {
sb.append(Character.toUpperCase(temp.charAt(i)));
}
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* 字符串驼峰转下划线格式
*
* @param param 需要转换的字符串
* @return 转换好的字符串
*/
public static String camelToUnderline(String param) {
if (isEmpty(param)) {
return "";
}
int len = param.length();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = param.charAt(i);
if (Character.isUpperCase(c) && i > 0) {
sb.append(UNDERLINE);
}
sb.append(Character.toLowerCase(c));
}
return sb.toString();
}
}

View File

@ -0,0 +1,14 @@
package com.yexuejc.base.util;
import java.lang.annotation.*;
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ToUeProperty {
/**
* 字段名默认该字段转下划线
* @return
*/
String value() default "";
}