in src/main/java/com/ql/util/express/parse/ExpressParse.java [87:284]
public List<ExpressNode> transferWord2ExpressNode(ExpressPackage rootExpressPackage, Word[] wordObjects,
Map<String, String> selfClassDefine, boolean dealJavaClass) throws Exception {
List<ExpressNode> result = new ArrayList<>();
String tempWord;
NodeType tempType;
int point = 0;
ExpressPackage tmpImportPackage = null;
if (dealJavaClass) {
tmpImportPackage = new ExpressPackage(rootExpressPackage);
//先处理import,import必须放在文件的最开始,必须以;结束
boolean isImport = false;
StringBuilder importName = new StringBuilder();
while (point < wordObjects.length) {
if ("/**".equals(wordObjects[point].word)) {
while ((++point) < wordObjects.length && !"**/".equals(wordObjects[point].word)) ;
point++;
continue;
}
if ("import".equals(wordObjects[point].word)) {
isImport = true;
importName.setLength(0);
} else if (";".equals(wordObjects[point].word)) {
isImport = false;
tmpImportPackage.addPackage(importName.toString());
} else if (isImport) {
importName.append(wordObjects[point].word);
} else {
break;
}
point = point + 1;
}
}
String originalValue = null;
Object objectValue = null;
NodeType treeNodeType = null;
Word tmpWordObject;
while (point < wordObjects.length) {
tmpWordObject = wordObjects[point];
tempWord = wordObjects[point].word;
char firstChar = tempWord.charAt(0);
char lastChar = tempWord.substring(tempWord.length() - 1).toLowerCase().charAt(0);
if (firstChar >= '0' && firstChar <= '9') {
if (!result.isEmpty()) {
// 对负号进行特殊处理
if ("-".equals(result.get(result.size() - 1).getValue())) {
if (result.size() == 1
|| result.size() >= 2
&& (result.get(result.size() - 2).isTypeEqualsOrChild("OP_LIST")
|| result.get(result.size() - 2).isTypeEqualsOrChild(",")
|| result.get(result.size() - 2).isTypeEqualsOrChild("return")
|| result.get(result.size() - 2).isTypeEqualsOrChild("?")
|| result.get(result.size() - 2).isTypeEqualsOrChild(":"))
&& !result.get(result.size() - 2).isTypeEqualsOrChild(")")
&& !result.get(result.size() - 2).isTypeEqualsOrChild("]")
) {
result.remove(result.size() - 1);
tempWord = "-" + tempWord;
}
}
}
if (lastChar == 'd') {
tempType = nodeTypeManager.findNodeType("CONST_DOUBLE");
tempWord = tempWord.substring(0, tempWord.length() - 1);
if (this.isPrecise) {
objectValue = new BigDecimal(tempWord);
} else {
objectValue = Double.valueOf(tempWord);
}
} else if (lastChar == 'f') {
tempType = nodeTypeManager.findNodeType("CONST_FLOAT");
tempWord = tempWord.substring(0, tempWord.length() - 1);
if (this.isPrecise) {
objectValue = new BigDecimal(tempWord);
} else {
objectValue = Float.valueOf(tempWord);
}
} else if (tempWord.contains(".")) {
tempType = nodeTypeManager.findNodeType("CONST_DOUBLE");
if (this.isPrecise) {
objectValue = new BigDecimal(tempWord);
} else {
objectValue = Double.valueOf(tempWord);
}
} else if (lastChar == 'l') {
tempType = nodeTypeManager.findNodeType("CONST_LONG");
tempWord = tempWord.substring(0, tempWord.length() - 1);
objectValue = Long.valueOf(tempWord);
} else {
long tempLong = Long.parseLong(tempWord);
if (tempLong <= Integer.MAX_VALUE && tempLong >= Integer.MIN_VALUE) {
tempType = nodeTypeManager.findNodeType("CONST_INTEGER");
objectValue = (int)tempLong;
} else {
tempType = nodeTypeManager.findNodeType("CONST_LONG");
objectValue = tempLong;
}
}
treeNodeType = nodeTypeManager.findNodeType("CONST");
point = point + 1;
} else if (firstChar == '"') {
if (lastChar != '"' || tempWord.length() < 2) {
throw new QLCompileException("没有关闭的字符串:" + tempWord);
}
tempWord = tempWord.substring(1, tempWord.length() - 1);
tempType = nodeTypeManager.findNodeType("CONST_STRING");
objectValue = tempWord;
treeNodeType = nodeTypeManager.findNodeType("CONST");
point = point + 1;
} else if (firstChar == '\'') {
if (lastChar != '\'' || tempWord.length() < 2) {
throw new QLCompileException("没有关闭的字符:" + tempWord);
}
tempWord = tempWord.substring(1, tempWord.length() - 1);
treeNodeType = nodeTypeManager.findNodeType("CONST");
if (tempWord.length() == 1 && !ignoreConstChar) {
//转换为字符串
tempType = nodeTypeManager.findNodeType("CONST_CHAR");
objectValue = tempWord.charAt(0);
} else {
tempType = nodeTypeManager.findNodeType("CONST_STRING");
objectValue = tempWord;
}
point = point + 1;
} else if ("true".equals(tempWord) || "false".equals(tempWord)) {
tempType = nodeTypeManager.findNodeType("CONST_BOOLEAN");
treeNodeType = nodeTypeManager.findNodeType("CONST");
objectValue = Boolean.valueOf(tempWord);
point = point + 1;
} else if ("/**".equals(tempWord)) {
while ((++point) < wordObjects.length && !"**/".equals(wordObjects[point].word));
point++;
continue;
} else {
tempType = nodeTypeManager.isExistNodeTypeDefine(tempWord);
if (tempType != null && tempType.getKind() != NodeTypeKind.KEYWORD) {
//不是关键字
tempType = null;
}
if (tempType == null) {
boolean isClass = false;
String tmpStr = "";
Class<?> tmpClass = null;
if (dealJavaClass) {
int j = point;
while (j < wordObjects.length) {
tmpStr = tmpStr + wordObjects[j].word;
tmpClass = tmpImportPackage.getClass(tmpStr);
if (tmpClass != null) {
point = j + 1;
isClass = true;
// 编译期类型白名单校验
if (!tmpClass.isPrimitive() &&
!QLExpressRunStrategy.checkWhiteClassList(tmpClass)) {
throw new QLSecurityRiskException("脚本中引用了不安全的类: " +
tmpClass.getCanonicalName());
}
break;
}
if (j < wordObjects.length - 1 && ".".equals(wordObjects[j + 1].word)) {
tmpStr = tmpStr + wordObjects[j + 1].word;
j = j + 2;
continue;
} else {
break;
}
}
}
if (isClass) {
tempWord = ExpressUtil.getClassName(tmpClass);
originalValue = tmpStr;
tempType = nodeTypeManager.findNodeType("CONST_CLASS");
objectValue = tmpClass;
} else if (this.nodeTypeManager.isFunction(tempWord)) {
tempType = nodeTypeManager.findNodeType("FUNCTION_NAME");
point = point + 1;
} else if (selfClassDefine != null && selfClassDefine.containsKey(tempWord)) {
tempType = nodeTypeManager.findNodeType("VClass");
point = point + 1;
} else {
tempType = nodeTypeManager.findNodeType("ID");
point = point + 1;
}
} else {
point = point + 1;
}
}
result.add(new ExpressNode(tempType, tempWord, originalValue, objectValue, treeNodeType, tmpWordObject.line,
tmpWordObject.col));
treeNodeType = null;
objectValue = null;
originalValue = null;
}
return result;
}