获取 Java 类中的字段
简介
有时候需要将 java bean 中的字段提取出来写 sql 语句,就需要获取这个类中全部的字段,但实际上代码中的定义会含有其他无关的东西,比如 private
final
等关键字,Integer
Object
等数据类型,Boolean a = true;
这种默认赋值,@JsonIgnore
字段注解,一些常规注释与文档注释。还有可能变量是同时定义的,比如 String a, b, c = "default";
。这些无疑都为「提取字段名」这个目标平添不少障碍,本人实际使用场景中会碰到此类问题,因此这个小节着重于实现提取 java 类中的字段。
代码
此 json 内容可用于导入到插件中使用。
{"procedureList":[{"id":"getJavaBeanFields","name":"获取 java 字段","match":{},"exclude":{},"functionList":[{"id":"removeComment","declaration":"string","doc":"移除注释","definition":"return value.replaceAll(/\\/\\*[\\d\\D]*?\\*\\//g, '')\r\n .replaceAll(/\\/\\/[^\\n]*/g, '')"},{"id":"removeCharString","declaration":"string","doc":"移除字符串","definition":"return value.replaceAll(/([\"'])(?:\\\\.|(?!\\1)[^\\\\])*\\1/g, '')"},{"id":"removeRoundBracket","declaration":"string","doc":"移除圆括号","definition":"const removed = value.replaceAll(/\\([^()]*\\)/g, '')\r\nif (removed.length === value.length) return removed\r\nreturn this.$self.removeRoundBracket(removed)"},{"id":"removeCurlyBrace","declaration":"string","doc":"移除花括号","definition":"const removed = value.replaceAll(/{[^{}]*}/g, '')\r\nif (removed.length === value.length) return removed\r\nreturn this.$self.removeCurlyBrace(removed)"},{"id":"removeAngleBracket","declaration":"string","doc":"移除尖括号","definition":"const removed = value.replaceAll(/<[^<>]*>/g, '')\r\nif (removed.length === value.length) return removed\r\nreturn this.$self.removeAngleBracket(removed)"},{"id":"removeSquareBracket","declaration":"string","doc":"移除方括号","definition":"const removed = value.replaceAll(/\\[[^\\[\\]]*\\]/g, '')\r\nif (removed.length === value.length) return removed\r\nreturn this.$self.removeSquareBracket(removed)"},{"id":"removeAnnotation","declaration":"string","doc":"移除注解","definition":"return value.replaceAll(/@[$\\w]+/g, '')"},{"id":"splitBySemicolon","declaration":"string","doc":"按分号分割","definition":"return value.split(';')"},{"id":"splitByComma","declaration":"string","doc":"按逗号分割","definition":"return value.split(',')"},{"id":"removeValue","declaration":"string","doc":"移除赋值","definition":"return value.split('=')[0]"},{"id":"lastWord","declaration":"string","doc":"取最后一个词语","definition":"const list = value.trim().split(/\\s+/)\r\nreturn list[list.length - 1]"},{"id":"filterEmpty","declaration":"string","doc":"过滤空字符串","definition":"return value ? value : []"}]}],"globalFunctionList":[]}
示例输入1
private Object obj;
private Boolean boo;
示例输出1
obj,boo
示例输入2
private static final DateTimeFormatter EN_DATE_FORMATTER = DateTimeFormatter.ofPattern("MMM d, yyyy", Locale.ENGLISH);
String str = "a,b,c\"d";
char c = '\'';
int a = 1, b = 2;
Object o = new Runnable() {
@Override
public void run() {
System.out.println("hello");
}
};
Function<String, HashMap<String, HashMap<String, String>>> func = (s) -> {
return new HashMap<>();
};
/**
* test comment
*/
// another comment
String comment = null;
@Annotation({
@SubAnnation(value = Object.class, name = "any"),
@SubAnnation(value = Map.class, name = "record")
})
Object annotation = null;
示例输出2
EN_DATE_FORMATTER,str,c,a,b,o,func,comment,annotation