comparison mxtool/mx.py @ 8607:c7672a325faf

search for classes containing annotations returns source file as well as class name
author Doug Simon <doug.simon@oracle.com>
date Wed, 03 Apr 2013 10:08:42 +0200
parents aaf3988bd1b4
children c4bca84d86d7
comparison
equal deleted inserted replaced
8606:f00f02691677 8607:c7672a325faf
309 cp.append(self.output_dir()) 309 cp.append(self.output_dir())
310 310
311 def find_classes_with_matching_source_line(self, pkgRoot, function, includeInnerClasses=False): 311 def find_classes_with_matching_source_line(self, pkgRoot, function, includeInnerClasses=False):
312 """ 312 """
313 Scan the sources of this project for Java source files containing a line for which 313 Scan the sources of this project for Java source files containing a line for which
314 'function' returns true. The fully qualified class name of each existing class 314 'function' returns true. A map from class name to source file path for each existing class
315 corresponding to a matched source file is returned in a list. 315 corresponding to a matched source file is returned.
316 """ 316 """
317 classes = [] 317 result = dict()
318 pkgDecl = re.compile(r"^package\s+([a-zA-Z_][\w\.]*)\s*;$") 318 pkgDecl = re.compile(r"^package\s+([a-zA-Z_][\w\.]*)\s*;$")
319 for srcDir in self.source_dirs(): 319 for srcDir in self.source_dirs():
320 outputDir = self.output_dir() 320 outputDir = self.output_dir()
321 for root, _, files in os.walk(srcDir): 321 for root, _, files in os.walk(srcDir):
322 for name in files: 322 for name in files:
323 if name.endswith('.java') and name != 'package-info.java': 323 if name.endswith('.java') and name != 'package-info.java':
324 matchFound = False 324 matchFound = False
325 with open(join(root, name)) as f: 325 source = join(root, name)
326 with open(source) as f:
326 pkg = None 327 pkg = None
327 for line in f: 328 for line in f:
328 if line.startswith("package "): 329 if line.startswith("package "):
329 match = pkgDecl.match(line) 330 match = pkgDecl.match(line)
330 if match: 331 if match:
340 if pkgRoot is None or pkg.startswith(pkgRoot): 341 if pkgRoot is None or pkg.startswith(pkgRoot):
341 pkgOutputDir = join(outputDir, pkg.replace('.', os.path.sep)) 342 pkgOutputDir = join(outputDir, pkg.replace('.', os.path.sep))
342 for e in os.listdir(pkgOutputDir): 343 for e in os.listdir(pkgOutputDir):
343 if includeInnerClasses: 344 if includeInnerClasses:
344 if e.endswith('.class') and (e.startswith(basename) or e.startswith(basename + '$')): 345 if e.endswith('.class') and (e.startswith(basename) or e.startswith(basename + '$')):
345 classes.append(pkg + '.' + e[:-len('.class')]) 346 className = pkg + '.' + e[:-len('.class')]
347 result[className] = source
346 elif e == basename + '.class': 348 elif e == basename + '.class':
347 classes.append(pkg + '.' + basename) 349 className = pkg + '.' + basename
348 return classes 350 result[className] = source
351 return result
349 352
350 def _init_packages_and_imports(self): 353 def _init_packages_and_imports(self):
351 if not hasattr(self, '_defined_java_packages'): 354 if not hasattr(self, '_defined_java_packages'):
352 packages = set() 355 packages = set()
353 extendedPackages = set() 356 extendedPackages = set()