该Util是去除Object对象中String类型的Field指定的字符或字符串
public class TestMain {
public static void main(String[] args) throws IllegalAccessException {
School sc = new School();
sc.setStr1("s \n \r \r\n s");
sc.setStr2("s \n \r \r\n s");
sc.setStr3("s \n \r \r\n s");
StringsUtil.removeCustomizeValue(sc);
System.out.print(sc.getStr1() + "\n" + sc.getStr2() + "\n" + sc.getStr3());
}
}
输出结果
ss
s
s
s
s
@Remove
public class School {
private String str1;
@Ignore
private String str2;
@Remove(values = {" "})
private String str3;
public String getStr1() {
return str1;
}
public void setStr1(String str1) {
this.str1 = str1;
}
public String getStr2() {
return str2;
}
public void setStr2(String str2) {
this.str2 = str2;
}
public String getStr3() {
return str3;
}
public void setStr3(String str3) {
this.str3 = str3;
}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Ignore {
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Remove {
String[] values() default {" ", "\n", "\r", "\n\r"};
}
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
public class StringsUtil {
public static final Logger LOGGER = LoggerFactory.getLogger(StringsUtil.class);
public static void removeCustomizeValue(Object object) {
Class<?> interfaceClass = object.getClass();
Field[] fields = interfaceClass.getDeclaredFields();
if (fields == null || fields.length == 0) {
return;
}
Remove classRemoveAnnotation = interfaceClass.getAnnotation(Remove.class);
/**
*若class上添加了Remove注解
*/
if (classRemoveAnnotation != null) {
String[] values = classRemoveAnnotation.values();
handleFieldsAnnotation(object, fields, true, values);
return;
}
/**
*若未在class未上添加了Remove注解
*/
handleFieldsAnnotation(object, fields, false, null);
}
private static void handleFieldsAnnotation(Object object, Field[] fields,
boolean defaultRemoveTag, String[] annotationDefaultValues) {
for (Field field : fields) {
if (needReplaceField(field, defaultRemoveTag)) {
String[] values = getFieldAnnotationValues(field, annotationDefaultValues);
if (values == null || values.length == 0)
continue;
replaceFieldValue(object, field, values);
}
}
}
private static String[] getFieldAnnotationValues(Field field, String[] values) {
Remove filedRemoveAnnotation = field.getAnnotation(Remove.class);
if (filedRemoveAnnotation != null) {
values = filedRemoveAnnotation.values();
}
return values;
}
private static boolean needReplaceField(Field field, boolean defaultRemoveTag) {
if (!(String.class).equals(field.getType())) {
return false;
}
field.setAccessible(true);
Ignore ignoreAnnotation = field.getAnnotation(Ignore.class);
if (ignoreAnnotation != null) {
return false;
}
Remove filedRemoveAnnotation = field.getAnnotation(Remove.class);
if (filedRemoveAnnotation != null) {
return true;
}
return defaultRemoveTag;
}
private static void replaceFieldValue(Object object, Field field, String[] values) {
try {
String fieldValue = (String) field.get(object);
if (Strings.isNullOrEmpty(fieldValue))
return;
for (String value : values) {
fieldValue = fieldValue.replaceAll(value, "");
}
field.set(object, fieldValue);
} catch (IllegalAccessException e) {
LOGGER.error(String.format("字段:%s,出现字符串替换异常", field.getName()), e);
}
}
}