shz.ast.KeyHelp Maven / Gradle / Ivy
package shz.ast;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.ListBuffer;
import shz.core.*;
import shz.core.cache.CacheKeyBuilder;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class KeyHelp {
private KeyHelp() {
throw new IllegalStateException();
}
/**
* 生成key
*
* @param keys 主key
* @param aName 注解名 shz.core.cache.CacheKey,shz.core.cache.HashKey
* @param keysName 注解中keys名 value
*/
public static ListBuffer keys(MethodHelp methodHelp, List params, ListBuffer keys, String aName, String keysName) {
Pattern pattern = Pattern.compile("(?:" + keysName + "\\s*=\\s*)?\\{?(\\s*\"?\\s*[\\w._]+(\\s*\"?\\s*,\\s*\"?\\s*[\\w._]+)*\\s*\"?\\s*)}?");
for (JCTree.JCVariableDecl param : params) {
if (param.mods == null || param.mods.annotations == null) continue;
for (JCTree.JCAnnotation a : param.mods.annotations) {
if (a.annotationType == null
|| a.annotationType.type == null
|| !aName.equals(a.annotationType.type.toString())) continue;
if (AccessibleHelp.isCommon(methodHelp.cls(param.vartype.type))) {
keys.append(methodHelp.apply("String.valueOf", param.name.toString()));
continue;
}
ListBuffer appendKeyList = new ListBuffer<>();
appendKeyList.append(methodHelp.ident(param.name.toString()));
if (a.args != null && a.args.length() > 0) {
Matcher matcher = pattern.matcher(a.args.get(0).toString());
if (matcher.find()) appendKeyList.appendList(methodHelp.analysisValue(matcher.group(1), true));
}
keys.append(methodHelp.apply("shz.ast.KeyHelp.generateKey", appendKeyList.toList()));
}
}
return keys;
}
/**
* 生成key
*
* @param arg 请求参数
* @param keys 请求key集合
*/
public static String generateKey(Object arg, String... keys) {
if (NullHelp.isEmpty(arg)) return "null";
Map deep = ToMap.getDeep(arg);
Map map = ToMap.get(deep.size()).build();
deep.forEach((k, v) -> map.put(k, Help.toString(v)));
if (map.isEmpty()) {
if (arg instanceof Collection) return CacheKeyBuilder.generateKey((Collection>) arg);
if (arg instanceof Object[]) return CacheKeyBuilder.generateKey((Object[]) arg);
return "null";
}
StringBuilder sb = new StringBuilder(50);
CacheKeyBuilder.generateKey(sb, map, keys);
return sb.toString();
}
public static JCTree.JCExpression joinKey(MethodHelp methodHelp, ListBuffer keys) {
if (keys.size() == 1) return keys.first();
if (keys.size() == 2) return methodHelp.treeMaker.Binary(
JCTree.Tag.PLUS,
methodHelp.treeMaker.Binary(JCTree.Tag.PLUS, keys.first(), methodHelp.treeMaker.Literal(":")),
keys.last()
);
ListBuffer list = new ListBuffer<>();
list.append(methodHelp.treeMaker.Literal(":"));
list.appendList(keys);
return methodHelp.apply("String.join", list.toList());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy