吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1701|回复: 2
收起左侧

[Python 转载] ply之解析java文件,找出包名、类名、依赖类

[复制链接]
c03xp 发表于 2020-10-12 14:46
暂不支持多重继承

plyjava.py:
[Python] 纯文本查看 复制代码
tokens = (
    'PACKAGE',
    'PACKAGENAME',
    'COMMA',
    'ID',
    'PUBLIC',
    'CLASS',
    'LBRACE',
    'INTERFACE',
    'EXTENDS',
    'IMPLEMENTS',
    'PACKAGEPATH',
    'IMPORT'
    )

states = (
    ('multicomment','exclusive'),
    ('package','exclusive'),
 )


reserved = {
    'public' : 'PUBLIC',
    'class' : 'CLASS',
    'extends' : 'EXTENDS',
    'implements' : 'IMPLEMENTS',
    'interface' : 'INTERFACE',
    'import'    : 'IMPORT'
 }



def t_LBRACE(t):
    r'\{'
    return t


def t_COMMENT(t):
    r'\/\/.*'


def t_PACKAGE(t):
     r'\bpackage\b'
     t.lexer.begin('package') 
     return t
     
def t_package_PACKAGENAME(t):
    r'([a-zA-Z_$][a-zA-Z_$0-9]*)(\.[a-zA-Z_$][a-zA-Z_$0-9]*)*'
    return t

t_package_ignore = " \t"
    
def t_package_COMMA(t):
    r';'
    t.lexer.begin('INITIAL')
    return t
    
def t_package_error(t):
    print("Illegal character '%s'" % t.value[0])
    t.lexer.skip(ply_java_filelen) 
    

    
def t_LCOMMENT(t):
     r'\/\*'
     t.lexer.begin('multicomment') 

def t_multicomment_newline(t):
     r'[\r\n]+'    
    
def t_multicomment_RCOMMENT(t):
    r'\*\/'
    t.lexer.begin('INITIAL')
    
def t_multicomment_MCOMMENT(t):
    r'((?!\*\/).)+'
    
def t_multicomment_error(t):
    print("Illegal character '%s'" % t.value[0])
    t.lexer.skip(ply_java_filelen)    
    
t_multicomment_ignore = ""       


def t_PACKAGEPATH(t):
    r'([a-zA-Z_$][a-zA-Z_$0-9]*)(\.[a-zA-Z_$][a-zA-Z_$0-9]*)+'
    return t  



def t_ID(t):
     r'[a-zA-Z_$][a-zA-Z_$0-9]*'
     t.type = reserved.get(t.value,'ID')    # Check for reserved words
     return t    
     
def t_COMMA(t):
    r';'
    return t  

    
    
def t_newline(t):
     r'[\r\n]+'       
    
def t_error(t):
    #print("Illegal character '%s'" % t.value[0])
    t.lexer.skip(ply_java_filelen)
    
# Ignored characters
t_ignore = " \t"


def p_error(t):
    #print("Syntax error at '%s'" % t.value)
    pass


     
def p_prog_java(t):
    '''prog : pkgline importlines decors PUBLIC decors CLASS ID extend PACKAGEPATH LBRACE
            | pkgline importlines decors PUBLIC decors INTERFACE ID extend PACKAGEPATH LBRACE
            | pkgline importlines decors PUBLIC decors CLASS ID extend ID LBRACE
            | pkgline importlines decors PUBLIC decors INTERFACE ID extend ID LBRACE            
            | pkgline importlines decors PUBLIC decors CLASS ID LBRACE
            | pkgline importlines decors PUBLIC decors INTERFACE ID LBRACE'''        
    global ply_java_classname
    ply_java_classname = t[7]
    
    

def p_importlines_java(t):
    '''importlines : importline
                    | '''
    
def p_importline_java(t):  
    '''importline : importline import
              | import'''

       

def p_import_java(t):
    'import : IMPORT PACKAGEPATH COMMA'
    global ply_java_depends
    ply_java_depends.append(t[2])
    
def p_pkgline_java(t):
    "pkgline : PACKAGE pkgname COMMA"
    global ply_java_pkgname
    ply_java_pkgname = t[2]

def p_extend_java(t):
    '''extend : EXTENDS
              | IMPLEMENTS'''

    

    
def p_pkgname_java(t):
    'pkgname : PACKAGENAME'
    t[0] = t[1]
    
def p_decors_java(t):
    '''decors : decor
            | '''

def p_decor_java(t):   
    '''decor : decor ID
            | ID'''
    




    
    
ply_java_filelen = 0     
ply_java_pkgname = ''
ply_java_classname = ''
ply_java_depends = []    
    
    
import ply.lex as lex
lexer = lex.lex()    
    
    
import ply.yacc as yacc
parser = yacc.yacc()



def analyzeJava(fname):
    global ply_java_pkgname,ply_java_classname,ply_java_depends,ply_java_filelen
    ply_java_pkgname = ""
    ply_java_classname = ""
    ply_java_depends.clear()
    ply_java_filelen = 0 
    try:
        f = open(fname,"r")
        f.seek(0,2)
        ply_java_filelen = f.tell()
        f.seek(0,0)
        buffer = f.read(ply_java_filelen)
        f.close()        
    except Exception as e:
        print(str(e))
        
      
    parser.parse(buffer)
    return {'pkgname':ply_java_pkgname,'classname':ply_java_classname,'depends':ply_java_depends}



client.py:
[Asm] 纯文本查看 复制代码
import plyjava



print(plyjava.analyzeJava("Socket.java"))
image.png

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

puz_zle 发表于 2020-10-12 15:37
可是 正常情况 大家都会整成 jar包啊

不过不得不说 这正则写的很有灵性
 楼主| c03xp 发表于 2020-10-12 15:50
puz_zle 发表于 2020-10-12 15:37
可是 正常情况 大家都会整成 jar包啊

不过不得不说 这正则写的很有灵性

这是第一步,下一步准备画出当前目录下所有java类之间的依赖图
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 23:41

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表