-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPMDAnalyzer.java
More file actions
389 lines (334 loc) · 11.7 KB
/
PMDAnalyzer.java
File metadata and controls
389 lines (334 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package software.xdev.pmd.analysis;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.OrderEnumerator;
import com.intellij.psi.PsiFile;
import com.intellij.util.PathsList;
import net.sourceforge.pmd.PMDConfiguration;
import net.sourceforge.pmd.PmdAnalysis;
import net.sourceforge.pmd.internal.util.ClasspathClassLoader;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.document.TextFile;
import net.sourceforge.pmd.lang.rule.RuleSet;
import net.sourceforge.pmd.reporting.FileAnalysisListener;
import net.sourceforge.pmd.reporting.GlobalAnalysisListener;
import net.sourceforge.pmd.reporting.Report;
import software.xdev.pmd.config.PluginConfiguration;
import software.xdev.pmd.config.PluginConfigurationManager;
import software.xdev.pmd.external.org.apache.shiro.lang.util.SoftHashMap;
import software.xdev.pmd.langversion.ManagedLanguageVersionResolver;
import software.xdev.pmd.model.config.ConfigurationLocation;
public class PMDAnalyzer implements Disposable
{
private static final Logger LOG = Logger.getInstance(PMDAnalyzer.class);
private static final ExecutorService RULESET_LOADER_SERVICE = Executors.newThreadPerTaskExecutor(Thread.ofVirtual()
.name("RuleSetLoader", 0)
.factory());
private final Project project;
private final Map<Optional<Module>, ReentrantLock> locks = Collections.synchronizedMap(new HashMap<>());
private final Map<Optional<Module>, CacheFile> cacheFiles = Collections.synchronizedMap(new HashMap<>());
// Reuse classloader when path is the same
private final Map<Set<String>, ClassLoader> cachedSdkLibAuxClassLoader =
Collections.synchronizedMap(new SoftHashMap<>());
public PMDAnalyzer(final Project project)
{
this.project = project;
}
private String cacheFile(final Optional<Module> optModule)
{
return this.cacheFiles.computeIfAbsent(
optModule,
ignored -> {
try
{
final Path path = Files.createTempFile("pmd-intellij-cache", ".cache");
return new CacheFile(path, path.toAbsolutePath().toString());
}
catch(final IOException e)
{
throw new UncheckedIOException(e);
}
})
.absolutePath();
}
public PMDAnalysisResult analyze(
final Optional<Module> optModule,
final Set<PsiFile> filesToScan,
final boolean determineIfFilesApplicable,
final Collection<ConfigurationLocation> configurationLocations,
final ProgressIndicator progressIndicator)
{
if(filesToScan.isEmpty())
{
return PMDAnalysisResult.empty(NoAnalysisReason.NO_FILES);
}
if(configurationLocations.isEmpty())
{
return PMDAnalysisResult.empty(NoAnalysisReason.NO_CONFIG_LOCATION_OR_EXCLUDED);
}
final ReentrantLock lock = this.locks.computeIfAbsent(optModule, ignored -> new ReentrantLock());
lock.lock();
try
{
return this.analyzeInternal(
optModule,
filesToScan,
determineIfFilesApplicable,
configurationLocations,
progressIndicator);
}
finally
{
lock.unlock();
}
}
private PMDAnalysisResult analyzeInternal(
final Optional<Module> optModule,
final Set<PsiFile> filesToScan,
final boolean determineIfFilesApplicable,
final Collection<ConfigurationLocation> configurationLocations,
final ProgressIndicator progressIndicator)
{
final long startMs = System.currentTimeMillis();
// Load ruleset - if required - async in background
final CompletableFuture<List<RuleSet>> cfLoadRuleSetsAsync =
CompletableFuture.supplyAsync(
() -> configurationLocations.stream()
.map(ConfigurationLocation::getOrRefreshCachedRuleSet)
.filter(Objects::nonNull)
.toList(),
RULESET_LOADER_SERVICE);
final PluginConfiguration pluginConfiguration =
this.project.getService(PluginConfigurationManager.class).getCurrent();
final Collection<PsiFile> applicableFiles = determineIfFilesApplicable
? this.determineApplicableFiles(optModule, filesToScan, pluginConfiguration, progressIndicator)
: filesToScan;
if(applicableFiles.isEmpty())
{
cfLoadRuleSetsAsync.cancel(false);
return PMDAnalysisResult.empty(NoAnalysisReason.NO_APPLICABLE_FILES);
}
progressIndicator.checkCanceled();
progressIndicator.setText("Calculating languages and version");
progressIndicator.setIndeterminate(true);
final Map<LanguageVersion, Set<PsiFile>> highestLanguageVersionAndFiles =
this.getHighestLanguageVersionAndFiles(this.groupPsiFilesBySupportedLanguageAndVersion(applicableFiles));
progressIndicator.checkCanceled();
progressIndicator.setText("Preparing configuration");
final PMDConfiguration pmdConfig = new PMDConfiguration();
pmdConfig.setDefaultLanguageVersions(highestLanguageVersionAndFiles.keySet().stream().toList());
final List<Module> modules = optModule
.map(List::of)
.orElseGet(() -> List.of(ModuleManager.getInstance(this.project).getModules()));
pmdConfig.setClassLoader(this.classLoaderFor(modules));
if(pluginConfiguration.showSuppressedWarnings())
{
pmdConfig.setShowSuppressedViolations(true);
}
if(pluginConfiguration.useSingleThread())
{
pmdConfig.setThreads(-1);
}
if(pluginConfiguration.useCacheFile())
{
pmdConfig.setAnalysisCacheLocation(this.cacheFile(optModule));
}
progressIndicator.setText("Preparing files for scan");
final List<IDETextFile> ideFiles = highestLanguageVersionAndFiles.entrySet()
.stream()
.flatMap(e -> e.getValue().stream().map(f -> new IDETextFile(e.getKey(), f)))
.toList();
final Report report;
try(final PmdAnalysis pmd = PmdAnalysis.create(pmdConfig))
{
// Prevent ruleset parsing
pmd.addRuleSets(cfLoadRuleSetsAsync.join());
ideFiles.forEach(pmd.files()::addFile);
progressIndicator.checkCanceled();
progressIndicator.setText("Analysing");
progressIndicator.setFraction(0);
pmd.addListener(new ProgressReportingAnalysisListener(progressIndicator, ideFiles.size()));
report = pmd.performAnalysisAndCollectReport();
}
progressIndicator.setText("Finishing analysis");
progressIndicator.setText2("");
progressIndicator.setIndeterminate(true);
final PMDAnalysisResult result = new PMDAnalysisResult(
report,
ideFiles.stream()
.filter(IDETextFile::hasFileId)
.collect(Collectors.toMap(
IDETextFile::getFileIdIfPresent,
IDETextFile::getPsiFile)));
LOG.info("Analysis took " + (System.currentTimeMillis() - startMs) + "ms");
return result;
}
static class ProgressReportingAnalysisListener implements GlobalAnalysisListener
{
private final AtomicInteger counter = new AtomicInteger(0);
private final ProgressIndicator progressIndicator;
private final int totalFiles;
ProgressReportingAnalysisListener(final ProgressIndicator progressIndicator, final int totalFiles)
{
this.progressIndicator = progressIndicator;
this.totalFiles = totalFiles;
}
@Override
public FileAnalysisListener startFileAnalysis(final TextFile file)
{
this.progressIndicator.setFraction((double)this.counter.incrementAndGet() / this.totalFiles);
this.progressIndicator.setText2(((IDETextFile)file).getPsiFile().getName());
return FileAnalysisListener.noop();
}
@Override
public void close()
{
// Nothing
}
}
@NotNull
private List<PsiFile> determineApplicableFiles(
final Optional<Module> optModule,
final Set<PsiFile> filesToScan,
final PluginConfiguration pluginConfiguration,
final ProgressIndicator progressIndicator)
{
progressIndicator.setText("Determining files for scan");
progressIndicator.setIndeterminate(false);
progressIndicator.setFraction(0);
final int totalFiles = filesToScan.size();
final AtomicInteger counter = new AtomicInteger(0);
final List<PsiFile> files = ReadAction.computeBlocking(() -> filesToScan.stream()
.filter(file -> {
progressIndicator.setFraction((double)counter.incrementAndGet() / totalFiles);
progressIndicator.setText2(file != null ? file.getName() : null);
return PsiFileValidator.isScannable(
file,
optModule,
pluginConfiguration);
})
.toList());
progressIndicator.setText2("");
return files;
}
private Map<Language, Map<LanguageVersion, List<PsiFile>>> groupPsiFilesBySupportedLanguageAndVersion(
final Collection<PsiFile> files)
{
final ManagedLanguageVersionResolver resolver = new ManagedLanguageVersionResolver();
return files.stream()
.collect(Collectors.groupingBy(resolver::resolveLanguage))
.entrySet()
.stream()
.filter(e -> e.getKey().isPresent())
.collect(Collectors.groupingBy(
e -> e.getKey().orElseThrow().getLanguage(),
Collectors.toMap(e -> e.getKey().orElseThrow(), Map.Entry::getValue)));
}
private Map<LanguageVersion, Set<PsiFile>> getHighestLanguageVersionAndFiles(
final Map<Language, Map<LanguageVersion, List<PsiFile>>> groupPsiFilesByLanguageAndVersion)
{
return groupPsiFilesByLanguageAndVersion.entrySet()
.stream()
.collect(Collectors.toMap(
e -> e.getValue()
.keySet()
.stream()
.max(LanguageVersion::compareTo)
.orElseThrow(),
e -> e.getValue()
.values()
.stream()
.flatMap(Collection::stream)
.collect(Collectors.toSet())));
}
@NotNull
private ClasspathClassLoader classLoaderFor(final List<Module> modules)
{
final Set<String> fullClassPaths = this.classPathFor(modules, UnaryOperator.identity());
final Set<String> appClassPaths = this.classPathFor(modules, o -> o.withoutSdk().withoutLibraries());
final Set<String> sdkLibClassPaths = fullClassPaths.stream()
.filter(s -> !appClassPaths.contains(s))
.collect(Collectors.toSet());
return this.createClasspathClassLoader(
appClassPaths,
this.cachedSdkLibAuxClassLoader.computeIfAbsent(
sdkLibClassPaths,
paths -> this.createClasspathClassLoader(paths, PMDConfiguration.class.getClassLoader())));
}
private Set<String> classPathFor(
final Collection<Module> modules,
final UnaryOperator<OrderEnumerator> mapOrderEnumerator)
{
return modules.stream()
.map(OrderEnumerator::orderEntries)
.map(OrderEnumerator::recursively)
.map(mapOrderEnumerator)
.map(OrderEnumerator::getPathsList)
.map(PathsList::getPathList)
.flatMap(Collection::stream)
.collect(Collectors.toSet());
}
private ClasspathClassLoader createClasspathClassLoader(
final Set<String> classPaths,
final ClassLoader parentLoader)
{
try
{
return new ClasspathClassLoader(String.join(File.pathSeparator, classPaths), parentLoader);
}
catch(final IOException e)
{
throw new UncheckedIOException(e);
}
}
@Override
public void dispose()
{
this.cacheFiles.values()
.stream()
.map(CacheFile::path)
.forEach(f -> {
try
{
Files.deleteIfExists(f);
}
catch(final IOException ioe)
{
LOG.warn("Failed to delete cache file", ioe);
}
});
this.cacheFiles.clear();
}
record CacheFile(
Path path,
String absolutePath)
{
}
}