Skip to content

Commit

Permalink
添加通用工具类
Browse files Browse the repository at this point in the history
  • Loading branch information
qinfuxiang committed Nov 16, 2021
1 parent 73614f5 commit 3b9027f
Show file tree
Hide file tree
Showing 2 changed files with 297 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package fast.cloud.nacos.common.model.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.ConfigurableEnvironment;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PropertiesUtil {

private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);

@SuppressWarnings("unchecked")
public static <T> T propertiesToClass(Properties properties, Class<T> clazz) {
try {
ConfigurationProperties configurationProperties = clazz.getAnnotation(ConfigurationProperties.class);
if (configurationProperties == null) {
return null;
}
boolean isNull = true;
Field[] fields = clazz.getDeclaredFields();
String prefix = configurationProperties.prefix();
Object object = clazz.newInstance();
if (null != prefix) {
for (Field field : fields) {
Type typeClz = field.getGenericType();

String attr = field.getName();
String key = prefix + "." + attr;

String value = properties.getProperty(key);
if (null == value) {
key = prefix + "." + humpToLine(attr);
value = properties.getProperty(key);
}

if (null == value) {
continue;
}
isNull = false;
ReflectHelper.setFieldValue(object, field.getName(), value);
}
}
if(isNull) {
return null;
} else {
return (T) object;
}
} catch (Exception e) {
logger.error("exchange properties error", e);
}
return null;
}

/**
* 从当前换的配置信息解析到对应的类实例
* @param environment 当前环境的配置信息
* @param clazz 配置对应的对象实例Class类型
* @param <T> 配置对应的对象实例Class类型
* @return 配置对应的对象实例
*/
@SuppressWarnings("unchecked")
public static <T> T propertiesToClass(ConfigurableEnvironment environment, Class<T> clazz) {
try {
ConfigurationProperties configurationProperties = clazz.getAnnotation(ConfigurationProperties.class);
if (configurationProperties == null) {
return null;
}
boolean isNull = true;
Field[] fields = clazz.getDeclaredFields();
String prefix = configurationProperties.prefix();
Object object = clazz.newInstance();
if (null != prefix) {
for (Field field : fields) {
Type typeClz = field.getGenericType();

String attr = field.getName();
String key = prefix + "." + attr;

String value = environment.getProperty(key);
if (null == value) {
key = prefix + "." + humpToLine(attr);
value = environment.getProperty(key);
}

if (null == value) {
continue;
}
isNull = false;
ReflectHelper.setFieldValue(object, field.getName(), value);
}
}

if(isNull) {
return null;
} else {
return (T) object;
}
} catch (Exception e) {
logger.error("exchange properties error", e);
}
return null;
}

/**
* 根据指定的前缀信息从当前环境中获取配置解析成为对应类型的对象实例
* @param environment 当前环境的配置信息
* @param clazz 配置对应的对象实例Class类型
* @param prefix 指定配置前缀信息
* @param <T> 配置对应的对象实例Class类型
* @return 配置对应的对象实例
*/
@SuppressWarnings("unchecked")
public static <T> T propertiesToClass(ConfigurableEnvironment environment, Class<T> clazz, String prefix) {
try {
boolean isNull = true;
Field[] fields = clazz.getDeclaredFields();
Object object = clazz.newInstance();
if (null != prefix) {
for (Field field : fields) {
Type typeClz = field.getGenericType();

String attr = field.getName();
String key = prefix + "." + attr;

String value = environment.getProperty(key);
if (null == value) {
key = prefix + "." + humpToLine(attr);
value = environment.getProperty(key);
}

if (null == value) {
continue;
}
isNull = false;
ReflectHelper.setFieldValue(object, field.getName(), value);
}
}

if(isNull) {
return null;
} else {
return (T) object;
}
} catch (Exception e) {
logger.error("exchange properties error", e);
}
return null;
}

/**
* 根据给定的key集合从环境配置中获取value,并返回k-v集合
* @param environment 当前环境的配置信息
* @param keySet 需要获取的配置信息的Key集合
* @return 配置k-v集合
*/
public static Map<String, Object> propertiesToMap(ConfigurableEnvironment environment, Set<String> keySet) {
Map<String, Object> map = new HashMap<>();
try {

for (String key : keySet) {

String value = environment.getProperty(key);
if (null == value) {
key = humpToLine(key);
value = environment.getProperty(key);
}

if (null == value) {
continue;
} else {
map.put(key, value);
}
}
} catch (Exception e) {
logger.error("exchange properties error", e);
}
return map;
}

public static Map<String, Object> propertiesToMap(ConfigurableEnvironment environment, Set<String> keySet, String prefix) {
Map<String, Object> map = new HashMap<>();
try {

for (String key : keySet) {

String value = environment.getProperty(key);
if (null == value) {
key = humpToLine(key);
value = environment.getProperty(key);
}

if (null == value) {
continue;
} else {
map.put(key.replace(prefix, ""), value);
}
}
} catch (Exception e) {
logger.error("exchange properties error", e);
}
return map;
}

public static String humpToLine(String str) {
return str.replaceAll("[A-Z]", "-$0").toLowerCase();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package fast.cloud.nacos.common.model.utils;

import org.apache.commons.lang3.reflect.FieldUtils;

import java.lang.reflect.Field;

public final class ReflectHelper {

private ReflectHelper() {
}

/**
* 反射根据obj filename 获取 值
*
* @param obj
* @param fieldName
* @return
*/
public static Object getFieldValue(Object obj, String fieldName) {

if (obj == null) {
return null;
}

Field targetField = getTargetField(obj.getClass(), fieldName);

try {
return FieldUtils.readField(targetField, obj, true);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}

/**
* 根据类 filename 获取属性
*
* @param targetClass
* @param fieldName
* @return
*/
public static Field getTargetField(Class<?> targetClass, String fieldName) {
Field field = null;

try {
if (targetClass == null) {
return field;
}

if (Object.class.equals(targetClass)) {
return field;
}

field = FieldUtils.getDeclaredField(targetClass, fieldName, true);
if (field == null) {
field = getTargetField(targetClass.getSuperclass(), fieldName);
}
} catch (Exception e) {
}

return field;
}

/**
* 设置属性值
*
* @param obj
* @param fieldName
* @param value
*/
public static void setFieldValue(Object obj, String fieldName, Object value) {
if (null == obj) {
return;
}
Field targetField = getTargetField(obj.getClass(), fieldName);
try {
FieldUtils.writeField(targetField, obj, value);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

}

0 comments on commit 3b9027f

Please sign in to comment.