/
CpdReport.java
321 lines (288 loc) · 10.1 KB
/
CpdReport.java
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
package org.apache.maven.plugins.pmd;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.pmd.exec.CpdExecutor;
import org.apache.maven.plugins.pmd.exec.CpdRequest;
import org.apache.maven.plugins.pmd.exec.CpdResult;
import org.apache.maven.reporting.MavenReportException;
import org.apache.maven.shared.utils.logging.MessageUtils;
import org.apache.maven.toolchain.Toolchain;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.WriterFactory;
import net.sourceforge.pmd.cpd.JavaTokenizer;
import net.sourceforge.pmd.cpd.renderer.CPDRenderer;
/**
* Creates a report for PMD's Copy/Paste Detector (CPD) tool.
* It can also generate a cpd results file in any of these formats: xml, csv or txt.
*
* <p>See <a href="https://pmd.github.io/latest/pmd_userdocs_cpd.html">Finding duplicated code</a>
* for more details.
*
* @author Mike Perham
* @version $Id$
* @since 2.0
*/
@Mojo( name = "cpd", threadSafe = true )
public class CpdReport
extends AbstractPmdReport
{
/**
* The programming language to be analyzed by CPD. Valid values are currently <code>java</code>,
* <code>javascript</code> or <code>jsp</code>.
*
* @since 3.5
*/
@Parameter( defaultValue = "java" )
private String language;
/**
* The minimum number of tokens that need to be duplicated before it causes a violation.
*/
@Parameter( property = "minimumTokens", defaultValue = "100" )
private int minimumTokens;
/**
* Skip the CPD report generation. Most useful on the command line via "-Dcpd.skip=true".
*
* @since 2.1
*/
@Parameter( property = "cpd.skip", defaultValue = "false" )
private boolean skip;
/**
* If true, CPD ignores literal value differences when evaluating a duplicate block. This means that
* <code>foo=42;</code> and <code>foo=43;</code> will be seen as equivalent. You may want to run PMD with this
* option off to start with and then switch it on to see what it turns up.
*
* @since 2.5
*/
@Parameter( property = "cpd.ignoreLiterals", defaultValue = "false" )
private boolean ignoreLiterals;
/**
* Similar to <code>ignoreLiterals</code> but for identifiers; i.e., variable names, methods names, and so forth.
*
* @since 2.5
*/
@Parameter( property = "cpd.ignoreIdentifiers", defaultValue = "false" )
private boolean ignoreIdentifiers;
/**
* If true, CPD ignores annotations.
*
* @since 3.11.0
*/
@Parameter( property = "cpd.ignoreAnnotations", defaultValue = "false" )
private boolean ignoreAnnotations;
/**
* Contains the result of the last CPD execution.
* It might be <code>null</code> which means, that CPD
* has not been executed yet.
*/
private CpdResult cpdResult;
/**
* {@inheritDoc}
*/
public String getName( Locale locale )
{
return getBundle( locale ).getString( "report.cpd.name" );
}
/**
* {@inheritDoc}
*/
public String getDescription( Locale locale )
{
return getBundle( locale ).getString( "report.cpd.description" );
}
/**
* {@inheritDoc}
*/
@Override
public void executeReport( Locale locale )
throws MavenReportException
{
try
{
execute( locale );
}
finally
{
if ( getSink() != null )
{
getSink().close();
}
}
}
private void execute( Locale locale )
throws MavenReportException
{
if ( !skip && canGenerateReport() )
{
ClassLoader origLoader = Thread.currentThread().getContextClassLoader();
try
{
Thread.currentThread().setContextClassLoader( this.getClass().getClassLoader() );
generateMavenSiteReport( locale );
}
finally
{
Thread.currentThread().setContextClassLoader( origLoader );
}
}
}
@Override
public boolean canGenerateReport()
{
if ( skip )
{
getLog().info( "Skipping CPD execution" );
return false;
}
boolean result = super.canGenerateReport();
if ( result )
{
try
{
executeCpd();
if ( skipEmptyReport )
{
result = cpdResult.hasDuplications();
if ( !result )
{
getLog().debug( "Skipping report since skipEmptyReport is true and there are no CPD issues." );
}
}
}
catch ( MavenReportException e )
{
throw new RuntimeException( e );
}
}
return result;
}
private void executeCpd()
throws MavenReportException
{
if ( cpdResult != null )
{
// CPD has already been run
getLog().debug( "CPD has already been run - skipping redundant execution." );
return;
}
Properties languageProperties = new Properties();
if ( ignoreLiterals )
{
languageProperties.setProperty( JavaTokenizer.IGNORE_LITERALS, "true" );
}
if ( ignoreIdentifiers )
{
languageProperties.setProperty( JavaTokenizer.IGNORE_IDENTIFIERS, "true" );
}
if ( ignoreAnnotations )
{
languageProperties.setProperty( JavaTokenizer.IGNORE_ANNOTATIONS, "true" );
}
try
{
filesToProcess = getFilesToProcess();
CpdRequest request = new CpdRequest();
request.setMinimumTokens( minimumTokens );
request.setLanguage( language );
request.setLanguageProperties( languageProperties );
request.setSourceEncoding( determineEncoding( !filesToProcess.isEmpty() ) );
request.addFiles( filesToProcess.keySet() );
request.setShowPmdLog( showPmdLog );
request.setColorizedLog( MessageUtils.isColorEnabled() );
request.setLogLevel( determineCurrentRootLogLevel() );
request.setExcludeFromFailureFile( excludeFromFailureFile );
request.setTargetDirectory( targetDirectory.getAbsolutePath() );
request.setOutputEncoding( getOutputEncoding() );
request.setFormat( format );
request.setIncludeXmlInSite( includeXmlInSite );
request.setReportOutputDirectory( getReportOutputDirectory().getAbsolutePath() );
Toolchain tc = getToolchain();
if ( tc != null )
{
getLog().info( "Toolchain in maven-pmd-plugin: " + tc );
String javaExecutable = tc.findTool( "java" ); //NOI18N
request.setJavaExecutable( javaExecutable );
}
getLog().info( "PMD version: " + AbstractPmdReport.getPmdVersion() );
cpdResult = CpdExecutor.execute( request );
}
catch ( UnsupportedEncodingException e )
{
throw new MavenReportException( "Encoding '" + getSourceEncoding() + "' is not supported.", e );
}
catch ( IOException e )
{
throw new MavenReportException( e.getMessage(), e );
}
}
private void generateMavenSiteReport( Locale locale )
{
CpdReportGenerator gen = new CpdReportGenerator( getSink(), filesToProcess, getBundle( locale ),
isAggregator() );
gen.generate( cpdResult.getDuplications() );
}
private String determineEncoding( boolean showWarn )
throws UnsupportedEncodingException
{
String encoding = WriterFactory.FILE_ENCODING;
if ( StringUtils.isNotEmpty( getSourceEncoding() ) )
{
encoding = getSourceEncoding();
// test encoding as CPD will convert exception into a RuntimeException
WriterFactory.newWriter( new ByteArrayOutputStream(), encoding );
}
else if ( showWarn )
{
getLog().warn( "File encoding has not been set, using platform encoding " + WriterFactory.FILE_ENCODING
+ ", i.e. build is platform dependent!" );
encoding = WriterFactory.FILE_ENCODING;
}
return encoding;
}
/**
* {@inheritDoc}
*/
public String getOutputName()
{
return "cpd";
}
private static ResourceBundle getBundle( Locale locale )
{
return ResourceBundle.getBundle( "cpd-report", locale, CpdReport.class.getClassLoader() );
}
/**
* Create and return the correct renderer for the output type.
*
* @return the renderer based on the configured output
* @throws org.apache.maven.reporting.MavenReportException if no renderer found for the output type
* @deprecated Use {@link CpdExecutor#createRenderer(String, String)} instead.
*/
@Deprecated
public CPDRenderer createRenderer() throws MavenReportException
{
return CpdExecutor.createRenderer( format, getOutputEncoding() );
}
}