使用javalang做Java词法分析

  1. 安装javalang
pip install javalang
  1. 输入java的方法体,方法体可由如下代码提取得到:https://github.com/kangyujian/JavaMethodExactor
{    Map<String, Object> params = new HashMap<String, Object>();    params.put("count", count);    params.put("page", currentpage + 1);    String next = paging_template.expand(params);    next = request.getResolvedUri().resolve(next).toString();    feed.addLink(next, "next");    if (currentpage > 0) {        params.put("page", currentpage - 1);        String prev = paging_template.expand(params);        prev = request.getResolvedUri().resolve(prev).toString();        feed.addLink(prev, "previous");    }    params.put("page", 0);    String current = paging_template.expand(params);    current = request.getResolvedUri().resolve(current).toString();    feed.addLink(current, "current");}

3.调用javalang进行词法分析:

import javalang as jl
def tokenzierImp(imp):
    tokens = jl.tokenizer.tokenize(imp)
    lst=[]
    for token in tokens:
        typeName=token.__class__.__name__
        valueName=token.value
        lst.append(typeName)
        lst.append(valueName)
    return ' '.join(lst)

4.效果:
使用javalang做Java词法分析

上一篇:蓝桥杯 PREV-5 错误票据(排序+连续输入未知个数数据)


下一篇:LinkedList源码分析(三)