2017-10-25 17 views
0

nodeJs를 사용하여 주어진 Java 소스 코드의 모든 메소드를 리턴하는 모듈을 빌드하려고합니다. 지금까지 AST 트리를 "java-parser"모듈을 사용하여 빌드 할 수 있지만 메소드를 필터링하기 위해 트래버스해야합니다.JavaScript : Java 소스 코드 파싱, 메서드 추출

나는 바벨 트래버스가 Js를위한 것이지만 나는 트래버스하는 방법을 원했기 때문에 나는 그 방법들을 걸러 낼 수있다. 나는 방법이 유형에서 "MethodDeclaration"에 의해 식별됩니다

{ node: 'CompilationUnit', 
    types: 
    [ { node: 'TypeDeclaration', 
     name: [Object], 
     superInterfaceTypes: [], 
     superclassType: null, 
     bodyDeclarations: [Array], 
     typeParameters: [], 
     interface: false, 
     modifiers: [Array] } ], 
    package: null, 
    imports: [] } 

같은 기록 외모 경우이 오류를

throw new Error(messages.get("traverseNeedsParent", parent.type)); 
^

Error: You must pass a scope and parentPath unless traversing a 
Program/File. Instead of that you tried to traverse a undefined node 
without passing scope and parentPath. 

대서양 표준시를 얻고있다. 어떤 도움을 주셔서 감사합니다.

답변

2

AST는 또 다른 JSON 개체입니다. 시도해보십시오 jsonpath.

npm install jsonpath 

는 조건 node=="MethodDeclaration" 필터링, 모든 방법을 추출하려면

var jp = require('jsonpath'); 
var methods = jp.query(ast, '$.types..[?(@.node=="MethodDeclaration")]'); 
console.log(methods); 

더 JSON 경로 구문 here를 참조하십시오.