@@ -70,7 +70,7 @@ public class PmdExample {
70
70
configuration.setReportFormat("xml");
71
71
configuration.setReportFile("/home/workspace/pmd-report.xml");
72
72
73
-
PMD.runPMD(configuration);
73
+
PMD.runPmd(configuration);
74
74
}
75
75
}
76
76
```
@@ -80,66 +80,75 @@ public class PmdExample {
80
80
This gives you more control over which files are processed, but is also more complicated.
81
81
You can also provide your own custom renderers.
82
82
83
-
1. First we create a `PMDConfiguration`. This is currently the only way to specify a ruleset:
83
+
1. First we create a `PMDConfiguration` and configure it, first the rules:
84
84
85
85
```java
86
86
PMDConfiguration configuration = new PMDConfiguration();
87
87
configuration.setMinimumPriority(RulePriority.MEDIUM);
88
88
configuration.setRuleSets("rulesets/java/quickstart.xml");
89
89
```
90
90
91
-
2. In order to support type resolution, PMD needs to have access to the compiled classes and dependencies
92
-
as well. This is called "auxclasspath" and is also configured here.
93
-
Note: you can specify multiple class paths separated by `:` on Unix-systems or `;` under Windows.
91
+
2. Then we configure, which paths to analyze:
94
92
95
93
```java
96
-
configuration.prependClasspath("/home/workspace/target/classes:/home/.m2/repository/my/dependency.jar");
94
+
configuration.setInputPaths("/home/workspace/src/main/java/code");
97
95
```
98
96
99
-
3. Then we need to load the rulesets. This is done by using the configuration, taking the minimum priority into
100
-
account:
97
+
3. The we configure the default language version for Java. And in order to support type resolution,
98
+
PMD needs to have access to the compiled classes and dependencies as well. This is called
99
+
"auxclasspath" and is also configured here.
100
+
101
+
Note: you can specify multiple class paths separated by `:` on Unix-systems or `;` under Windows.
101
102
102
103
```java
103
-
RuleSetLoader ruleSetLoader = RuleSetLoader.fromPmdConfig(configuration);
104
-
List<RuleSet> ruleSets = ruleSetLoader.loadFromResources(Arrays.asList(configuration.getRuleSets().split(",")));
104
+
configuration.setDefaultLanguageVersion(LanguageRegistry.findLanguageByTerseName("java").getVersion("11"));
105
+
configuration.prependAuxClasspath("/home/workspace/target/classes:/home/.m2/repository/my/dependency.jar");
105
106
```
106
107
107
-
4. PMD operates on a list of `DataSource`. You can assemble a own list of `FileDataSource`, e.g.
108
+
4. Then we configure the reporting. Configuring the report file is optional. If not specified, the report
109
+
will be written to `stdout`.
108
110
109
111
```java
110
-
List<DataSource> files = Arrays.asList(new FileDataSource(new File("/path/to/src/MyClass.java")));
112
+
configuration.setReportFormat("xml");
113
+
configuration.setReportFile("/home/workspace/pmd-report.xml");
111
114
```
112
115
113
-
5. For reporting, you can use `GlobalAnalysisListener`, which receives events like violations and errors.
114
-
Useful implementations are provided by `Renderer` instances. To use a renderer, eg the built-in `XMLRenderer`,
115
-
create it and configure it with a suitable `Writer`.
116
-
116
+
5. Now an optional step: If you want to use additional renderers as in the example, set them up before
117
+
calling PMD. You can use a built-in renderer, e.g. `XMLRenderer` or a custom renderer implementing
118
+
`Renderer`. Note, that you must manually initialize the renderer by setting a suitable `Writer`:
119
+
117
120
```java
118
-
StringWriter rendererOutput = new StringWriter();
119
-
Renderer xmlRenderer = new XMLRenderer("UTF-8");
120
-
xmlRenderer.setWriter(rendererOutput);
121
-
// The listener is created from the renderer in the next listing
121
+
Writer rendererOutput = new StringWriter();
122
+
Renderer renderer = createRenderer(rendererOutput);
123
+
124
+
// ...
125
+
private static Renderer createRenderer(Writer writer) {
126
+
XMLRenderer xml = new XMLRenderer("UTF-8");
127
+
xml.setWriter(writer);
128
+
return xml;
129
+
}
122
130
```
123
131
124
-
6. Now, all the preparations are done, and PMD can be executed. This is done by calling
125
-
`PMD.processFiles(...)`. This method call takes the configuration, the rulesets, the files
126
-
to process, and the list of renderers. Provide an empty list, if you don't want to use
127
-
any renderer. Note: The auxclasspath needs to be closed explicitly. Otherwise the class or jar files may
128
-
remain open and file resources are leaked.
132
+
6. Finally we can start the PMD analysis. There is the possibility to fine-tune the configuration
133
+
by adding additional files to analyze or adding additional rulesets or renderers:
129
134
130
135
```java
131
-
try (GlobalAnalysisListener listener = xmlRenderer.newListener()) {
132
-
PMD.processFiles(configuration, ruleSets, files, listener);
133
-
} finally {
134
-
ClassLoader auxiliaryClassLoader = configuration.getClassLoader();
135
-
if (auxiliaryClassLoader instanceof ClasspathClassLoader) {
136
-
((ClasspathClassLoader) auxiliaryClassLoader).close();
137
-
}
136
+
try (PmdAnalysis pmd = PmdAnalysis.create(configuration)) {
137
+
// optional: add more rulesets
138
+
pmd.addRuleSet(RuleSetLoader.fromPmdConfig(configuration).loadFromResource("custom-ruleset.xml"));
139
+
// optional: add more files
140
+
pmd.files().addFile(Paths.get("src", "main", "more-java", "ExtraSource.java"));
141
+
// optional: add more renderers
142
+
pmd.addRenderer(renderer);
143
+
144
+
// or just call PMD
145
+
pmd.performAnalysis();
138
146
}
139
147
```
140
148
141
-
7. After the call, the renderer will have been flushed by PMD (through its `GlobalAnalysisListener`).
142
-
Then you can check the rendered output.
149
+
The renderer will be automatically flushed and closed at the end of the analysis.
150
+
151
+
7. Then you can check the rendered output.
143
152
144
153
``` java
145
154
System.out.println("Rendered Report:");
@@ -152,51 +161,44 @@ Here is a complete example:
152
161
import java.io.IOException;
153
162
import java.io.StringWriter;
154
163
import java.io.Writer;
155
-
import java.nio.file.FileSystems;
156
-
import java.nio.file.FileVisitResult;
157
-
import java.nio.file.Files;
158
-
import java.nio.file.Path;
159
-
import java.nio.file.PathMatcher;
160
-
import java.nio.file.SimpleFileVisitor;
161
-
import java.nio.file.attribute.BasicFileAttributes;
162
-
import java.util.ArrayList;
163
-
import java.util.Arrays;
164
-
import java.util.Collections;
165
-
import java.util.List;
164
+
import java.nio.file.Paths;
166
165
167
-
import net.sourceforge.pmd.PMD;
168
166
import net.sourceforge.pmd.PMDConfiguration;
167
+
import net.sourceforge.pmd.PmdAnalysis;
169
168
import net.sourceforge.pmd.RulePriority;
170
-
import net.sourceforge.pmd.RuleSet;
171
169
import net.sourceforge.pmd.RuleSetLoader;
170
+
import net.sourceforge.pmd.lang.LanguageRegistry;
172
171
import net.sourceforge.pmd.renderers.Renderer;
173
172
import net.sourceforge.pmd.renderers.XMLRenderer;
174
-
import net.sourceforge.pmd.util.ClasspathClassLoader;
175
-
import net.sourceforge.pmd.util.datasource.DataSource;
176
-
import net.sourceforge.pmd.util.datasource.FileDataSource;
177
173
178
174
public class PmdExample2 {
179
175
180
176
public static void main(String[] args) throws IOException {
181
177
PMDConfiguration configuration = new PMDConfiguration();
182
178
configuration.setMinimumPriority(RulePriority.MEDIUM);
183
179
configuration.setRuleSets("rulesets/java/quickstart.xml");
184
-
configuration.prependClasspath("/home/workspace/target/classes");
185
-
RuleSetLoader ruleSetLoader = RuleSetLoader.fromPmdConfig(configuration);
186
-
List<RuleSet> ruleSets = ruleSetLoader.loadFromResources(Arrays.asList(configuration.getRuleSets().split(",")));
187
180
188
-
List<DataSource> files = determineFiles("/home/workspace/src/main/java/code");
181
+
configuration.setInputPaths("/home/workspace/src/main/java/code");
182
+
183
+
configuration.setDefaultLanguageVersion(LanguageRegistry.findLanguageByTerseName("java").getVersion("11"));
184
+
configuration.prependAuxClasspath("/home/workspace/target/classes");
185
+
186
+
configuration.setReportFormat("xml");
187
+
configuration.setReportFile("/home/workspace/pmd-report.xml");
189
188
190
189
Writer rendererOutput = new StringWriter();
191
190
Renderer renderer = createRenderer(rendererOutput);
192
191
193
-
try (GlobalAnalysisListener listener = renderer.newListener()) {
194
-
PMD.processFiles(configuration, ruleSets, files, listener);
195
-
} finally {
196
-
ClassLoader auxiliaryClassLoader = configuration.getClassLoader();
197
-
if (auxiliaryClassLoader instanceof ClasspathClassLoader) {
198
-
((ClasspathClassLoader) auxiliaryClassLoader).close();
199
-
}
192
+
try (PmdAnalysis pmd = PmdAnalysis.create(configuration)) {
193
+
// optional: add more rulesets
194
+
pmd.addRuleSet(RuleSetLoader.fromPmdConfig(configuration).loadFromResource("custom-ruleset.xml"));
195
+
// optional: add more files
196
+
pmd.files().addFile(Paths.get("src", "main", "more-java", "ExtraSource.java"));
197
+
// optional: add more renderers
198
+
pmd.addRenderer(renderer);
199
+
200
+
// or just call PMD
201
+
pmd.performAnalysis();
200
202
}
201
203
202
204
System.out.println("Rendered Report:");
@@ -208,28 +210,6 @@ public class PmdExample2 {
208
210
xml.setWriter(writer);
209
211
return xml;
210
212
}
211
-
212
-
private static List<DataSource> determineFiles(String basePath) throws IOException {
213
-
Path dirPath = FileSystems.getDefault().getPath(basePath);
214
-
final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:*.java");
215
-
216
-
final List<DataSource> files = new ArrayList<>();
217
-
218
-
Files.walkFileTree(dirPath, new SimpleFileVisitor<Path>() {
219
-
@Override
220
-
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
221
-
if (matcher.matches(path.getFileName())) {
222
-
System.out.printf("Using %s%n", path);
223
-
files.add(new FileDataSource(path.toFile()));
224
-
} else {
225
-
System.out.printf("Ignoring %s%n", path);
226
-
}
227
-
return super.visitFile(path, attrs);
228
-
}
229
-
});
230
-
System.out.printf("Analyzing %d files in %s%n", files.size(), basePath);
231
-
return files;
232
-
}
233
213
}
234
214
```
235
215
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4