自从用上了SpringBoot,文件里是一堆注解,有的熟悉,有的生疏,如果有天需要整理一遍的话,编个程序把所有用到的注解都统计出来是个好主意。这个任务正适合用python完成,下面就是程序:
#encoding=utf-8 import os,fnmatch,re class Caculator: def __init__(self,javaProjectFolder): self.folder=javaProjectFolder def tranversal(self): print('进入目录:'+self.folder) self.matches=[] for root,subdirs,files in os.walk(self.folder): for filename in fnmatch.filter(files,'*.java'): self.matches.append(os.path.join(root,filename)) def printJavaFiles(self): print(' 共找到'+str(len(self.matches))+'个java文件如下:') for i,javafile in enumerate(self.matches): print(str(i+1)+'.'+javafile) def findAnnotations(self): self.map={} pattern="s*[@]([A-Z]{1}[a-zA-Z]+)s*" for i,javafile in enumerate(self.matches): with open(javafile,'r',encoding='utf-8') as infile: for line in infile: list=re.findall(pattern,line) if len(list)>0: for i,anno in enumerate(list): #print(anno) if anno in self.map: self.map[anno]=self.map[anno]+1 else: self.map[anno]=1 def printAnnoAsAbcOrder(self): print(' >以字典序打印(含出现次数)注解如下:') list=sorted(self.map.items(),key=lambda x:x[0]) for item in list: print(item) def printAnnoAsCountOrder(self): print(' >以出现次数打印注解如下:') list=sorted(self.map.items(),key=lambda x:x[1]) for item in list: print(item) def printAnnotations(self): print(' >以字典序打印注解如下:') list=[] for key in self.map.keys(): list.append(key) list=sorted(list) for item in list: print(item) cclt = Caculator('C:hyprogmyBank') cclt.tranversal() cclt.printJavaFiles() cclt.findAnnotations() cclt.printAnnoAsAbcOrder() cclt.printAnnoAsCountOrder() cclt.printAnnotations()
运行效果:
C:hypy>python annotationsInJava.py 进入目录:C:hyprogmyBank 共找到15个java文件如下: 1.C:hyprogmyBank.mvnwrapperMavenWrapperDownloader.java 2.C:hyprogmyBanksrcmainjavacomhymybankMyBankApplication.java 3.C:hyprogmyBanksrcmainjavacomhymybankRedisConfig.java 4.C:hyprogmyBanksrcmainjavacomhymybankctrlActionController.java 5.C:hyprogmyBanksrcmainjavacomhymybankctrlJsonController.java 6.C:hyprogmyBanksrcmainjavacomhymybankEntityStudent.java 7.C:hyprogmyBanksrcmainjavacomhymybankmapperAccountMapper.java 8.C:hyprogmyBanksrcmainjavacomhymybankmapperAccountSql.java 9.C:hyprogmyBanksrcmainjavacomhymybankmapperStudentMapper.java 10.C:hyprogmyBanksrcmainjavacomhymybankmapperStudentSql.java 11.C:hyprogmyBanksrcmainjavacomhymybankmapperTransferException.java 12.C:hyprogmyBanksrcmainjavacomhymybankmqRabbitMqMsgReceiver.java 13.C:hyprogmyBanksrcmainjavacomhymybankmqRabbitMqMsgSender.java 14.C:hyprogmyBanksrcmainjavacomhymybankserviceAccountService.java 15.C:hyprogmyBanksrc estjavacomhymybankMyBankApplicationTests.java >以字典序打印(含出现次数)注解如下: ('Autowired', 8) ('Bean', 4) ('Component', 3) ('Configuration', 1) ('Controller', 1) ('EnableCaching', 1) ('GetMapping', 3) ('Insert', 1) ('Mapper', 2) ('ModelAttribute', 2) ('Override', 2) ('Param', 4) ('RabbitHandler', 1) ('RabbitListener', 1) ('RequestMapping', 7) ('Resource', 3) ('RestController', 1) ('Select', 4) ('SelectProvider', 2) ('SpringBootApplication', 1) ('SpringBootTest', 1) ('Test', 1) ('Transactional', 1) ('Update', 1) ('Value', 8) >以出现次数打印注解如下: ('SpringBootApplication', 1) ('Configuration', 1) ('EnableCaching', 1) ('Controller', 1) ('RestController', 1) ('Update', 1) ('Insert', 1) ('RabbitListener', 1) ('RabbitHandler', 1) ('Transactional', 1) ('SpringBootTest', 1) ('Test', 1) ('Override', 2) ('ModelAttribute', 2) ('Mapper', 2) ('SelectProvider', 2) ('GetMapping', 3) ('Component', 3) ('Resource', 3) ('Bean', 4) ('Select', 4) ('Param', 4) ('RequestMapping', 7) ('Value', 8) ('Autowired', 8) >以字典序打印注解如下: Autowired Bean Component Configuration Controller EnableCaching GetMapping Insert Mapper ModelAttribute Override Param RabbitHandler RabbitListener RequestMapping Resource RestController Select SelectProvider SpringBootApplication SpringBootTest Test Transactional Update Value
-END-