我需要找到犀牛脚本中使用的所有进口。例如,导入如下:- importPackage(Packages.org.json) importPackage(Packages.java.lang)
var jsonResponse = (Packages.java.lang.String) (xyz代码)。
如何使用犀牛解析器/ using有效地实现这一目标?
发布于 2018-03-13 16:57:01
我以以下方式实现了所需的功能(如果其他人遇到了这个问题):我使用了mozilla的javascript解析器,这给了我一个AstNode。有了这个AstNode之后,我创建了另一个实现mozilla NodeVisitor的类,在该类中,我根据需要重写了访问方法。
以下是代码片段:-
public class ASTtry {
static class Visitor implements NodeVisitor {
List<String> ls;
Visitor(List<String> loc) {
ls = loc;
}
@Override
public boolean visit(AstNode node) {
if (node instanceof Name) {
ls.add(node.getString());
}
if (node instanceof Loop) {
int type = node.getType();
ls.add(Token.typeToName(type));
}
return true;
}
}
public static void main(String[] args) throws IOException {
ASTtry ast = new ASTtry();
CompilerEnvirons env = new CompilerEnvirons();
setEnvironmentVariables(env);
File scriptFile = new File("whatever path my file was");
FileReader fr = new FileReader(scriptFile);
AstNode root = new Parser(env).parse(fr, null, 1);
List<String> allTokens = new ArrayList<>();
root.visit(new Visitor(allTokens));
Properties properties = new Properties();
InputStream propFile;
propFile = new FileInputStream("I had imports seperated by commas in this file");
properties.load(propFile);
ast.check(properties, allTokens);
}
private static void setEnvironmentVariables(CompilerEnvirons env) {
env.setRecordingLocalJsDocComments(true);
env.setAllowSharpComments(true);
env.setRecordingComments(true);
env.setIdeMode(true);
env.setErrorReporter(new ErrReporter());
env.setRecoverFromErrors(true);
env.setGenerateObserverCount(true);
}
public boolean check(Properties properties, List<String> loc) {
once I had all the lines of code, imports, i can easily loop around them for all the validations I need.
}https://stackoverflow.com/questions/48663191
复制相似问题