| 171 | |
| 172 | |
| 173 | class ClassToFilename(object): |
| 174 | def __init__(self, scan_dir): |
| 175 | """ |
| 176 | |
| 177 | :param scan_dir: |
| 178 | """ |
| 179 | self._scan_dir = scan_dir |
| 180 | self._source_files = self._get_java_source_files() |
| 181 | |
| 182 | def _get_specified_ext_files(self, ext): |
| 183 | ret = [] |
| 184 | for dirpath, _, filenames in os.walk(self._scan_dir): |
| 185 | for f in filenames: |
| 186 | if f.endswith(ext): |
| 187 | ret.append(os.path.relpath(os.path.join(dirpath, f), self._scan_dir).replace("\\", "/")) |
| 188 | return ret |
| 189 | |
| 190 | def _get_java_source_files(self): |
| 191 | return self._get_specified_ext_files(".java") |
| 192 | |
| 193 | def get_file_path_from_classname(self, classname): |
| 194 | """ |
| 195 | 当文件为None,或者class文件对应的java文件不在文件列表中,则返回None |
| 196 | :param classname: |
| 197 | :return: |
| 198 | """ |
| 199 | if not classname: |
| 200 | return None |
| 201 | if classname.find("$") > -1: |
| 202 | # 取class主类名 |
| 203 | classname = classname.split("$")[0] |
| 204 | else: |
| 205 | classname = classname.replace(".class", "") |
| 206 | sourcepath = classname.replace(".", "/") + ".java" |
| 207 | return self.get_file_path_from_sourcepath(sourcepath) |
| 208 | |
| 209 | def get_file_path_from_sourcepath(self, sourcepath): |
| 210 | """ |
| 211 | 当文件为None,或者java文件不在文件列表中,则返回None |
| 212 | :param sourcepath: |
| 213 | :return: |
| 214 | """ |
| 215 | if not sourcepath: |
| 216 | return None |
| 217 | sourcepath = sourcepath.replace("\\", "/") |
| 218 | for source_file in self._source_files: |
| 219 | if source_file.endswith(sourcepath): |
| 220 | return source_file |
| 221 | return None |
| 222 | |
| 223 | |
| 224 | tool = Findbugs |