|
19 | 19 |
|
20 | 20 | import java.io.File; |
21 | 21 | import java.io.IOException; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.nio.file.Files; |
22 | 24 | import java.util.Collections; |
23 | 25 | import java.util.HashMap; |
24 | 26 | import java.util.Iterator; |
25 | 27 | import java.util.LinkedHashSet; |
26 | 28 | import java.util.List; |
27 | 29 | import java.util.Map; |
28 | 30 | import java.util.Set; |
| 31 | +import java.util.concurrent.ConcurrentHashMap; |
| 32 | +import java.util.regex.Pattern; |
29 | 33 |
|
30 | 34 | import javax.xml.stream.XMLStreamException; |
31 | 35 |
|
@@ -147,6 +151,29 @@ private void launchAnalyze(String dir) throws IOException, XMLStreamException { |
147 | 151 | } |
148 | 152 | } |
149 | 153 |
|
| 154 | + private void analyzeViewFiles(File dir, Map<String, Pattern> patternsByMethod) |
| 155 | + throws IOException { |
| 156 | + for (final File file : DcdHelper.listFiles(dir)) { |
| 157 | + if (isInterrupted()) { |
| 158 | + break; |
| 159 | + } |
| 160 | + if (file.isDirectory()) { |
| 161 | + analyzeViewFiles(file, patternsByMethod); |
| 162 | + } else if (DcdHelper.isViewFile(file.getName())) { |
| 163 | + final byte[] bytes = Files.readAllBytes(file.toPath()); |
| 164 | + final String string = new String(bytes, StandardCharsets.UTF_8); |
| 165 | + for (final Map.Entry<String, Pattern> entry : patternsByMethod.entrySet()) { |
| 166 | + final Pattern pattern = entry.getValue(); |
| 167 | + if (pattern.matcher(string).matches()) { |
| 168 | + final String method = entry.getKey(); |
| 169 | + result.methodCalled(method); |
| 170 | + patternsByMethod.remove(method); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
150 | 177 | private void analyzeDirectory(String dir, String packageName) |
151 | 178 | throws IOException, XMLStreamException { |
152 | 179 | final Set<String> classNameList = listClassesAndAnalyzeSubDirectories(dir, packageName); |
@@ -226,11 +253,11 @@ private Set<String> listClassesAndAnalyzeSubDirectories(String dir, String packa |
226 | 253 | if (isInterrupted()) { |
227 | 254 | break; |
228 | 255 | } |
229 | | - final String name = packageName != null ? packageName + '.' + file.getName() |
230 | | - : file.getName(); |
231 | | - if (file.isDirectory() && file.getName().indexOf('.') == -1) { |
| 256 | + final String fileName = file.getName(); |
| 257 | + final String name = packageName != null ? packageName + '.' + fileName : fileName; |
| 258 | + if (file.isDirectory() && fileName.indexOf('.') == -1) { |
232 | 259 | analyzeDirectory(dir, name); |
233 | | - } else if (file.getName().endsWith(".class")) { |
| 260 | + } else if (fileName.endsWith(".class")) { |
234 | 261 | classNameList.add(name.substring(0, name.length() - ".class".length())); |
235 | 262 | } |
236 | 263 | } |
@@ -542,6 +569,18 @@ private void launchAllAnalyses() throws IOException, XMLStreamException { |
542 | 569 | // analyse des classes du répertoire WEB-INF/classes du war |
543 | 570 | launchAnalyze(tmpDirectory.getPath() + webInfClasses); |
544 | 571 | } |
| 572 | + if (parameters.includesViewFiles() && currentStep == Step.PUBLIC_ANALYSIS) { |
| 573 | + final Set<String> allMethods = result.getAllMethods(); |
| 574 | + final Map<String, Pattern> patternsByMethod = new ConcurrentHashMap<>(); |
| 575 | + for (final String method : allMethods) { |
| 576 | + // regex to search with word boundaries, for less false negatives in view files |
| 577 | + final String regex = ".*\\b" + DcdHelper.getMethodName(method) |
| 578 | + + "\\b.*"; |
| 579 | + final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL); |
| 580 | + patternsByMethod.put(method, pattern); |
| 581 | + } |
| 582 | + analyzeViewFiles(tmpDirectory, patternsByMethod); |
| 583 | + } |
545 | 584 | } else { |
546 | 585 | // analyse des classes du jar |
547 | 586 | launchAnalyze(tmpDirectory.getPath()); |
|
0 commit comments