This commit is contained in:
@@ -20,7 +20,7 @@ import java.util.Optional;
|
||||
*/
|
||||
public class RedisKeyResolver {
|
||||
|
||||
private static final int MAX_DEPTH = 6;
|
||||
private static final int MAX_DEPTH = 8;
|
||||
|
||||
private final SourceIndex index;
|
||||
|
||||
@@ -52,7 +52,7 @@ public class RedisKeyResolver {
|
||||
}
|
||||
if (expr instanceof NameExpr) {
|
||||
String name = ((NameExpr) expr).getNameAsString();
|
||||
String constVal = lookupConstant(enclosingClass, name);
|
||||
String constVal = lookupConstant(enclosingClass, name, context, depth);
|
||||
if (constVal != null) {
|
||||
return constVal;
|
||||
}
|
||||
@@ -62,12 +62,23 @@ public class RedisKeyResolver {
|
||||
if (expr instanceof FieldAccessExpr) {
|
||||
FieldAccessExpr fae = (FieldAccessExpr) expr;
|
||||
String fieldName = fae.getNameAsString();
|
||||
if (fae.getScope() instanceof NameExpr) {
|
||||
String scope = ((NameExpr) fae.getScope()).getNameAsString();
|
||||
String local = lookupConstant(enclosingClass, fieldName, context, depth);
|
||||
if (local != null && scope.equals(enclosingClass == null ? "" : enclosingClass.getNameAsString())) {
|
||||
return local;
|
||||
}
|
||||
String external = lookupExternalConstant(scope, fieldName, context, depth);
|
||||
if (external != null) {
|
||||
return external;
|
||||
}
|
||||
}
|
||||
String scope = fae.getScope().toString();
|
||||
String external = lookupExternalConstant(scope, fieldName, context);
|
||||
String external = lookupExternalConstant(scope, fieldName, context, depth);
|
||||
if (external != null) {
|
||||
return external;
|
||||
}
|
||||
String local = lookupConstant(enclosingClass, fieldName);
|
||||
String local = lookupConstant(enclosingClass, fieldName, context, depth);
|
||||
return local != null ? local : "*";
|
||||
}
|
||||
if (expr instanceof MethodCallExpr) {
|
||||
@@ -77,23 +88,26 @@ public class RedisKeyResolver {
|
||||
String fmt = resolveExpr(call.getArgument(0), enclosingClass, context, depth + 1);
|
||||
return fmt.replaceAll("%[-0-9.]*[sdxDX]", "*");
|
||||
}
|
||||
// buildCacheKey(...) 等本类方法:解析其 return 表达式
|
||||
String methodVal = lookupMethodReturn(enclosingClass, name, context, depth);
|
||||
return methodVal != null ? methodVal : "*";
|
||||
}
|
||||
return "*";
|
||||
}
|
||||
|
||||
private String lookupConstant(ClassOrInterfaceDeclaration clazz, String name) {
|
||||
private String lookupConstant(ClassOrInterfaceDeclaration clazz, String name,
|
||||
SourceIndex.IndexedType context, int depth) {
|
||||
if (clazz == null) {
|
||||
return null;
|
||||
}
|
||||
for (FieldDeclaration field : clazz.getFields()) {
|
||||
if (!field.isStatic()) {
|
||||
continue;
|
||||
}
|
||||
for (VariableDeclarator var : field.getVariables()) {
|
||||
if (var.getNameAsString().equals(name)) {
|
||||
Optional<Expression> init = var.getInitializer();
|
||||
if (init.isPresent() && init.get() instanceof StringLiteralExpr) {
|
||||
return ((StringLiteralExpr) init.get()).asString();
|
||||
if (init.isPresent()) {
|
||||
return resolveExpr(init.get(), clazz, context, depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,7 +115,8 @@ public class RedisKeyResolver {
|
||||
return null;
|
||||
}
|
||||
|
||||
private String lookupExternalConstant(String scopeName, String fieldName, SourceIndex.IndexedType context) {
|
||||
private String lookupExternalConstant(String scopeName, String fieldName,
|
||||
SourceIndex.IndexedType context, int depth) {
|
||||
String fqn = index.resolveFqn(scopeName, context);
|
||||
if (fqn == null) {
|
||||
return null;
|
||||
@@ -110,7 +125,7 @@ public class RedisKeyResolver {
|
||||
if (type == null) {
|
||||
return null;
|
||||
}
|
||||
return lookupConstant(type.getDeclaration(), fieldName);
|
||||
return lookupConstant(type.getDeclaration(), fieldName, type, depth);
|
||||
}
|
||||
|
||||
private String lookupMethodReturn(ClassOrInterfaceDeclaration clazz, String methodName,
|
||||
|
||||
Reference in New Issue
Block a user