/
AbstractFixJavadocMojo.java
3783 lines (3371 loc) · 134 KB
/
AbstractFixJavadocMojo.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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.apache.maven.plugins.javadoc;
/*
* 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 com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.library.ClassLibraryBuilder;
import com.thoughtworks.qdox.library.OrderedClassLibraryBuilder;
import com.thoughtworks.qdox.model.DocletTag;
import com.thoughtworks.qdox.model.JavaAnnotatedElement;
import com.thoughtworks.qdox.model.JavaAnnotation;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaConstructor;
import com.thoughtworks.qdox.model.JavaExecutable;
import com.thoughtworks.qdox.model.JavaField;
import com.thoughtworks.qdox.model.JavaGenericDeclaration;
import com.thoughtworks.qdox.model.JavaMember;
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.JavaParameter;
import com.thoughtworks.qdox.model.JavaType;
import com.thoughtworks.qdox.model.JavaTypeVariable;
import com.thoughtworks.qdox.parser.ParseException;
import com.thoughtworks.qdox.type.TypeResolver;
import org.apache.commons.lang3.ClassUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;
import org.apache.maven.shared.invoker.MavenInvocationException;
import org.codehaus.plexus.components.interactivity.InputHandler;
import org.codehaus.plexus.languages.java.version.JavaVersion;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.StringUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Abstract class to fix Javadoc documentation and tags in source files.
* <br>
* See <a href="https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#wheretags">Where Tags
* Can Be Used</a>.
*
* @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
* @since 2.6
*/
public abstract class AbstractFixJavadocMojo
extends AbstractMojo
{
/**
* The vm line separator
*/
private static final String EOL = System.getProperty( "line.separator" );
/**
* Tag name for @author *
*/
private static final String AUTHOR_TAG = "author";
/**
* Tag name for @version *
*/
private static final String VERSION_TAG = "version";
/**
* Tag name for @since *
*/
private static final String SINCE_TAG = "since";
/**
* Tag name for @param *
*/
private static final String PARAM_TAG = "param";
/**
* Tag name for @return *
*/
private static final String RETURN_TAG = "return";
/**
* Tag name for @throws *
*/
private static final String THROWS_TAG = "throws";
/**
* Tag name for @link *
*/
private static final String LINK_TAG = "link";
/**
* Tag name for {@inheritDoc} *
*/
private static final String INHERITED_TAG = "{@inheritDoc}";
/**
* Start Javadoc String i.e. <code>/**</code> *
*/
private static final String START_JAVADOC = "/**";
/**
* End Javadoc String i.e. <code>*/</code> *
*/
private static final String END_JAVADOC = "*/";
/**
* Javadoc Separator i.e. <code> * </code> *
*/
private static final String SEPARATOR_JAVADOC = " * ";
/**
* Inherited Javadoc i.e. <code>/** {@inheritDoc} */</code> *
*/
private static final String INHERITED_JAVADOC = START_JAVADOC + " " + INHERITED_TAG + " " + END_JAVADOC;
/**
* <code>all</code> parameter used by {@link #fixTags} *
*/
private static final String FIX_TAGS_ALL = "all";
/**
* <code>public</code> parameter used by {@link #level} *
*/
private static final String LEVEL_PUBLIC = "public";
/**
* <code>protected</code> parameter used by {@link #level} *
*/
private static final String LEVEL_PROTECTED = "protected";
/**
* <code>package</code> parameter used by {@link #level} *
*/
private static final String LEVEL_PACKAGE = "package";
/**
* <code>private</code> parameter used by {@link #level} *
*/
private static final String LEVEL_PRIVATE = "private";
/**
* The Clirr Maven plugin groupId <code>org.codehaus.mojo</code> *
*/
private static final String CLIRR_MAVEN_PLUGIN_GROUPID = "org.codehaus.mojo";
/**
* The Clirr Maven plugin artifactId <code>clirr-maven-plugin</code> *
*/
private static final String CLIRR_MAVEN_PLUGIN_ARTIFACTID = "clirr-maven-plugin";
/**
* The latest Clirr Maven plugin version <code>2.2.2</code> *
*/
private static final String CLIRR_MAVEN_PLUGIN_VERSION = "2.2.2";
/**
* The Clirr Maven plugin goal <code>check</code> *
*/
private static final String CLIRR_MAVEN_PLUGIN_GOAL = "check";
/**
* Java Files Pattern.
*/
public static final String JAVA_FILES = "**\\/*.java";
/**
* Default version value.
*/
public static final String DEFAULT_VERSION_VALUE = "\u0024Id: \u0024Id";
// ----------------------------------------------------------------------
// Mojo components
// ----------------------------------------------------------------------
/**
* Input handler, needed for command line handling.
*/
@Component
private InputHandler inputHandler;
// ----------------------------------------------------------------------
// Mojo parameters
// ----------------------------------------------------------------------
/**
* Version to compare the current code against using the
* <a href="http://mojo.codehaus.org/clirr-maven-plugin/">Clirr Maven Plugin</a>.
* <br/>
* See <a href="#defaultSince">defaultSince</a>.
*/
@Parameter ( property = "comparisonVersion", defaultValue = "(,${project.version})" )
private String comparisonVersion;
/**
* Default value for the Javadoc tag <code>@author</code>.
* <br/>
* If not specified, the <code>user.name</code> defined in the System properties will be used.
*/
@Parameter ( property = "defaultAuthor" )
private String defaultAuthor;
/**
* Default value for the Javadoc tag <code>@since</code>.
*/
@Parameter ( property = "defaultSince", defaultValue = "${project.version}" )
private String defaultSince;
/**
* Default value for the Javadoc tag <code>@version</code>.
* <br/>
* By default, it is <code>$Id:$</code>, corresponding to a
* <a href="http://svnbook.red-bean.com/en/1.1/ch07s02.html#svn-ch-7-sect-2.3.4">SVN keyword</a>.
* Refer to your SCM to use an other SCM keyword.
*/
@Parameter ( property = "defaultVersion", defaultValue = DEFAULT_VERSION_VALUE )
private String defaultVersion = "\u0024Id: \u0024"; // can't use default-value="\u0024Id: \u0024"
/**
* The file encoding to use when reading the source files. If the property
* <code>project.build.sourceEncoding</code> is not set, the platform default encoding is used.
*/
@Parameter ( property = "encoding", defaultValue = "${project.build.sourceEncoding}" )
private String encoding;
/**
* Comma separated excludes Java files, i.e. <code>**/*Test.java</code>.
*/
@Parameter ( property = "excludes" )
private String excludes;
/**
* Comma separated tags to fix in classes, interfaces or methods Javadoc comments.
* Possible values are:
* <ul>
* <li>all (fix all Javadoc tags)</li>
* <li>author (fix only @author tag)</li>
* <li>version (fix only @version tag)</li>
* <li>since (fix only @since tag)</li>
* <li>param (fix only @param tag)</li>
* <li>return (fix only @return tag)</li>
* <li>throws (fix only @throws tag)</li>
* <li>link (fix only @link tag)</li>
* </ul>
*/
@Parameter ( property = "fixTags", defaultValue = "all" )
private String fixTags;
/**
* Flag to fix the classes or interfaces Javadoc comments according the <code>level</code>.
*/
@Parameter ( property = "fixClassComment", defaultValue = "true" )
private boolean fixClassComment;
/**
* Flag to fix the fields Javadoc comments according the <code>level</code>.
*/
@Parameter ( property = "fixFieldComment", defaultValue = "true" )
private boolean fixFieldComment;
/**
* Flag to fix the methods Javadoc comments according the <code>level</code>.
*/
@Parameter ( property = "fixMethodComment", defaultValue = "true" )
private boolean fixMethodComment;
/**
* <p>Flag to remove throws tags from unknown classes.</p>
* <p><strong>NOTE:</strong>Since 3.1.0 the default value has been changed to {@code true},
* due to JavaDoc 8 strictness.
*/
@Parameter ( property = "removeUnknownThrows", defaultValue = "true" )
private boolean removeUnknownThrows;
/**
* Forcing the goal execution i.e. skip warranty messages (not recommended).
*/
@Parameter ( property = "force" )
private boolean force;
/**
* Flag to ignore or not Clirr.
*/
@Parameter ( property = "ignoreClirr", defaultValue = "false" )
protected boolean ignoreClirr;
/**
* Comma separated includes Java files, i.e. <code>**/*Test.java</code>.
* <p/>
* <strong>Note:</strong> default value is {@code **\/*.java}.
*/
@Parameter ( property = "includes", defaultValue = JAVA_FILES )
private String includes;
/**
* Specifies the access level for classes and members to show in the Javadocs.
* Possible values are:
* <ul>
* <li><a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#public">public</a>
* (shows only public classes and members)</li>
* <li><a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#protected">protected</a>
* (shows only public and protected classes and members)</li>
* <li><a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#package">package</a>
* (shows all classes and members not marked private)</li>
* <li><a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#private">private</a>
* (shows all classes and members)</li>
* </ul>
*/
@Parameter ( property = "level", defaultValue = "protected" )
private String level;
/**
* The local repository where the artifacts are located, used by the tests.
*/
@Parameter ( property = "localRepository" )
private ArtifactRepository localRepository;
/**
* Output directory where Java classes will be rewritten.
*/
@Parameter ( property = "outputDirectory", defaultValue = "${project.build.sourceDirectory}" )
private File outputDirectory;
/**
* The Maven Project Object.
*/
@Parameter( defaultValue = "${project}", readonly = true, required = true )
private MavenProject project;
@Parameter ( defaultValue = "${session}", readonly = true, required = true )
private MavenSession session;
/**
* The current user system settings for use in Maven.
*/
@Parameter( defaultValue = "${settings}", readonly = true, required = true )
private Settings settings;
// ----------------------------------------------------------------------
// Internal fields
// ----------------------------------------------------------------------
/**
* The current project class loader.
*/
private ClassLoader projectClassLoader;
/**
* Split {@link #fixTags} by comma.
*
* @see #init()
*/
private String[] fixTagsSplitted;
/**
* New classes found by Clirr.
*/
private List<String> clirrNewClasses;
/**
* New Methods in a Class (the key) found by Clirr.
*/
private Map<String, List<String>> clirrNewMethods;
/**
* List of classes where <code>*since</code> is added. Will be used to add or not this tag in the methods.
*/
private List<String> sinceClasses;
/**
* {@inheritDoc}
*/
@Override
public void execute()
throws MojoExecutionException, MojoFailureException
{
if ( !fixClassComment && !fixFieldComment && !fixMethodComment )
{
getLog().info( "Specified to NOT fix classes, fields and methods. Nothing to do." );
return;
}
// verify goal params
init();
if ( fixTagsSplitted.length == 0 )
{
getLog().info( "No fix tag specified. Nothing to do." );
return;
}
// add warranty msg
if ( !preCheck() )
{
return;
}
// run clirr
try
{
executeClirr();
}
catch ( MavenInvocationException e )
{
if ( getLog().isDebugEnabled() )
{
getLog().error( "MavenInvocationException: " + e.getMessage(), e );
}
else
{
getLog().error( "MavenInvocationException: " + e.getMessage() );
}
getLog().info( "Clirr is ignored." );
}
// run qdox and process
try
{
Collection<JavaClass> javaClasses = getQdoxClasses();
if ( javaClasses != null )
{
for ( JavaClass javaClass : javaClasses )
{
processFix( javaClass );
}
}
}
catch ( IOException e )
{
throw new MojoExecutionException( "IOException: " + e.getMessage(), e );
}
}
// ----------------------------------------------------------------------
// protected methods
// ----------------------------------------------------------------------
protected final MavenProject getProject()
{
return project;
}
/**
* @param p not null maven project.
* @return the artifact type.
*/
protected String getArtifactType( MavenProject p )
{
return p.getArtifact().getType();
}
/**
* @param p not null maven project.
* @return the list of source paths for the given project.
*/
protected List<String> getProjectSourceRoots( MavenProject p )
{
return ( p.getCompileSourceRoots() == null
? Collections.<String>emptyList()
: new LinkedList<>( p.getCompileSourceRoots() ) );
}
/**
* @param p not null
* @return the compile classpath elements
* @throws DependencyResolutionRequiredException
* if any
*/
protected List<String> getCompileClasspathElements( MavenProject p )
throws DependencyResolutionRequiredException
{
return ( p.getCompileClasspathElements() == null
? Collections.<String>emptyList()
: new LinkedList<>( p.getCompileClasspathElements() ) );
}
/**
* @param javaExecutable not null
* @return the fully qualify name of javaMethod with signature
*/
protected static String getJavaMethodAsString( JavaExecutable javaExecutable )
{
return javaExecutable.getDeclaringClass().getFullyQualifiedName() + "#" + javaExecutable.getCallSignature();
}
// ----------------------------------------------------------------------
// private methods
// ----------------------------------------------------------------------
/**
* Init goal parameters.
*/
private void init()
{
// defaultAuthor
if ( StringUtils.isEmpty( defaultAuthor ) )
{
defaultAuthor = System.getProperty( "user.name" );
}
// defaultSince
int i = defaultSince.indexOf( "-" + Artifact.SNAPSHOT_VERSION );
if ( i != -1 )
{
defaultSince = defaultSince.substring( 0, i );
}
// fixTags
if ( !FIX_TAGS_ALL.equalsIgnoreCase( fixTags.trim() ) )
{
String[] split = StringUtils.split( fixTags, "," );
List<String> filtered = new LinkedList<>();
for ( String aSplit : split )
{
String s = aSplit.trim();
if ( JavadocUtil.equalsIgnoreCase( s, FIX_TAGS_ALL, AUTHOR_TAG, VERSION_TAG, SINCE_TAG, PARAM_TAG,
THROWS_TAG, LINK_TAG, RETURN_TAG ) )
{
filtered.add( s );
}
else
{
if ( getLog().isWarnEnabled() )
{
getLog().warn( "Unrecognized '" + s + "' for fixTags parameter. Ignored it!" );
}
}
}
fixTags = StringUtils.join( filtered.iterator(), "," );
}
fixTagsSplitted = StringUtils.split( fixTags, "," );
// encoding
if ( StringUtils.isEmpty( encoding ) )
{
if ( getLog().isWarnEnabled() )
{
getLog().warn( "File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING
+ ", i.e. build is platform dependent!" );
}
encoding = ReaderFactory.FILE_ENCODING;
}
// level
level = level.trim();
if ( !JavadocUtil.equalsIgnoreCase( level, LEVEL_PUBLIC, LEVEL_PROTECTED, LEVEL_PACKAGE, LEVEL_PRIVATE ) )
{
if ( getLog().isWarnEnabled() )
{
getLog().warn( "Unrecognized '" + level + "' for level parameter, using 'protected' level." );
}
level = "protected";
}
}
/**
* @return <code>true</code> if the user wants to proceed, <code>false</code> otherwise.
* @throws MojoExecutionException if any
*/
private boolean preCheck()
throws MojoExecutionException
{
if ( force )
{
return true;
}
if ( outputDirectory != null && !outputDirectory.getAbsolutePath().equals(
getProjectSourceDirectory().getAbsolutePath() ) )
{
return true;
}
if ( !settings.isInteractiveMode() )
{
getLog().error( "Maven is not attempt to interact with the user for input. "
+ "Verify the <interactiveMode/> configuration in your settings." );
return false;
}
getLog().warn( "" );
getLog().warn( " WARRANTY DISCLAIMER" );
getLog().warn( "" );
getLog().warn( "All warranties with regard to this Maven goal are disclaimed!" );
getLog().warn( "The changes will be done directly in the source code." );
getLog().warn( "The Maven Team strongly recommends the use of a SCM software BEFORE executing this goal." );
getLog().warn( "" );
while ( true )
{
getLog().info( "Are you sure to proceed? [Y]es [N]o" );
try
{
String userExpression = inputHandler.readLine();
if ( userExpression == null || JavadocUtil.equalsIgnoreCase( userExpression, "Y", "Yes" ) )
{
getLog().info( "OK, let's proceed..." );
break;
}
if ( JavadocUtil.equalsIgnoreCase( userExpression, "N", "No" ) )
{
getLog().info( "No changes in your sources occur." );
return false;
}
}
catch ( IOException e )
{
throw new MojoExecutionException( "Unable to read from standard input.", e );
}
}
return true;
}
/**
* @return the source dir as File for the given project
*/
private File getProjectSourceDirectory()
{
return new File( project.getBuild().getSourceDirectory() );
}
/**
* Invoke Maven to run clirr-maven-plugin to find API differences.
*
* @throws MavenInvocationException if any
*/
private void executeClirr()
throws MavenInvocationException
{
if ( ignoreClirr )
{
getLog().info( "Clirr is ignored." );
return;
}
String clirrGoal = getFullClirrGoal();
// http://mojo.codehaus.org/clirr-maven-plugin/check-mojo.html
File clirrTextOutputFile =
FileUtils.createTempFile( "clirr", ".txt", new File( project.getBuild().getDirectory() ) );
Properties properties = new Properties();
properties.put( "textOutputFile", clirrTextOutputFile.getAbsolutePath() );
properties.put( "comparisonVersion", comparisonVersion );
properties.put( "failOnError", "false" );
if ( JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore( "8" ) )
{
// ensure that Java7 picks up TLSv1.2 when connecting with Central
properties.put( "https.protocols", "TLSv1.2" );
}
File invokerDir = new File( project.getBuild().getDirectory(), "invoker" );
invokerDir.mkdirs();
File invokerLogFile = FileUtils.createTempFile( "clirr-maven-plugin", ".txt", invokerDir );
new File( project.getBuild().getDirectory(), "invoker-clirr-maven-plugin.txt" );
JavadocUtil.invokeMaven( getLog(), new File( localRepository.getBasedir() ), project.getFile(),
Collections.singletonList( clirrGoal ), properties, invokerLogFile,
session.getRequest().getGlobalSettingsFile() );
try
{
if ( invokerLogFile.exists() )
{
String invokerLogContent =
StringUtils.unifyLineSeparators( FileUtils.fileRead( invokerLogFile, "UTF-8" ) );
// see org.codehaus.mojo.clirr.AbstractClirrMojo#getComparisonArtifact()
final String artifactNotFoundMsg = "Unable to find a previous version of the project in the repository";
if ( invokerLogContent.contains( artifactNotFoundMsg ) )
{
getLog().warn( "No previous artifact has been deployed, Clirr is ignored." );
return;
}
}
}
catch ( IOException e )
{
getLog().debug( "IOException: " + e.getMessage() );
}
try
{
parseClirrTextOutputFile( clirrTextOutputFile );
}
catch ( IOException e )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "IOException: " + e.getMessage(), e );
}
getLog().info( "IOException when parsing Clirr output '" + clirrTextOutputFile.getAbsolutePath()
+ "', Clirr is ignored." );
}
}
/**
* @param clirrTextOutputFile not null
* @throws IOException if any
*/
private void parseClirrTextOutputFile( File clirrTextOutputFile )
throws IOException
{
if ( !clirrTextOutputFile.exists() )
{
if ( getLog().isInfoEnabled() )
{
getLog().info(
"No Clirr output file '" + clirrTextOutputFile.getAbsolutePath() + "' exists, Clirr is ignored." );
}
return;
}
if ( getLog().isInfoEnabled() )
{
getLog().info( "Clirr output file was created: " + clirrTextOutputFile.getAbsolutePath() );
}
clirrNewClasses = new LinkedList<>();
clirrNewMethods = new LinkedHashMap<>();
try ( BufferedReader reader = new BufferedReader( ReaderFactory.newReader( clirrTextOutputFile, "UTF-8" ) ) )
{
for ( String line = reader.readLine(); line != null; line = reader.readLine() )
{
String[] split = StringUtils.split( line, ":" );
if ( split.length != 4 )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Unable to parse the clirr line: " + line );
}
continue;
}
int code;
try
{
code = Integer.parseInt( split[1].trim() );
}
catch ( NumberFormatException e )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Unable to parse the clirr line: " + line );
}
continue;
}
// http://clirr.sourceforge.net/clirr-core/exegesis.html
// 7011 - Method Added
// 7012 - Method Added to Interface
// 8000 - Class Added
List<String> list;
String[] splits2;
// CHECKSTYLE_OFF: MagicNumber
switch ( code )
{
case 7011:
list = clirrNewMethods.get( split[2].trim() );
if ( list == null )
{
list = new ArrayList<>();
}
splits2 = StringUtils.split( split[3].trim(), "'" );
if ( splits2.length != 3 )
{
continue;
}
list.add( splits2[1].trim() );
clirrNewMethods.put( split[2].trim(), list );
break;
case 7012:
list = clirrNewMethods.get( split[2].trim() );
if ( list == null )
{
list = new ArrayList<>();
}
splits2 = StringUtils.split( split[3].trim(), "'" );
if ( splits2.length != 3 )
{
continue;
}
list.add( splits2[1].trim() );
clirrNewMethods.put( split[2].trim(), list );
break;
case 8000:
clirrNewClasses.add( split[2].trim() );
break;
default:
break;
}
// CHECKSTYLE_ON: MagicNumber
}
}
if ( clirrNewClasses.isEmpty() && clirrNewMethods.isEmpty() )
{
getLog().info( "Clirr NOT found API differences." );
}
else
{
getLog().info( "Clirr found API differences, i.e. new classes/interfaces or methods." );
}
}
/**
* @param tag not null
* @return <code>true</code> if <code>tag</code> is defined in {@link #fixTags}.
*/
private boolean fixTag( String tag )
{
if ( fixTagsSplitted.length == 1 && fixTagsSplitted[0].equals( FIX_TAGS_ALL ) )
{
return true;
}
for ( String aFixTagsSplitted : fixTagsSplitted )
{
if ( aFixTagsSplitted.trim().equals( tag ) )
{
return true;
}
}
return false;
}
/**
* Calling Qdox to find {@link JavaClass} objects from the Maven project sources.
* Ignore java class if Qdox has parsing errors.
*
* @return an array of {@link JavaClass} found by QDox
* @throws IOException if any
* @throws MojoExecutionException if any
*/
private Collection<JavaClass> getQdoxClasses()
throws IOException, MojoExecutionException
{
if ( "pom".equalsIgnoreCase( project.getPackaging() ) )
{
getLog().warn( "This project has 'pom' packaging, no Java sources is available." );
return null;
}
List<File> javaFiles = new LinkedList<>();
for ( String sourceRoot : getProjectSourceRoots( project ) )
{
File f = new File( sourceRoot );
if ( f.isDirectory() )
{
javaFiles.addAll( FileUtils.getFiles( f, includes, excludes, true ) );
}
else
{
if ( getLog().isWarnEnabled() )
{
getLog().warn( f + " doesn't exist. Ignored it." );
}
}
}
ClassLibraryBuilder classLibraryBuilder = new OrderedClassLibraryBuilder();
classLibraryBuilder.appendClassLoader( getProjectClassLoader() );
JavaProjectBuilder builder = new JavaProjectBuilder( classLibraryBuilder );
builder.setEncoding( encoding );
for ( File f : javaFiles )
{
if ( !f.getAbsolutePath().toLowerCase( Locale.ENGLISH ).endsWith( ".java" ) && getLog().isWarnEnabled() )
{
getLog().warn( "'" + f + "' is not a Java file. Ignored it." );
continue;
}
try
{
builder.addSource( f );
}
catch ( ParseException e )
{
if ( getLog().isWarnEnabled() )
{
getLog().warn( "QDOX ParseException: " + e.getMessage() + ". Can't fix it." );
}
}
}
return builder.getClasses();
}
/**
* @return the classLoader for the given project using lazy instantiation.
* @throws MojoExecutionException if any
*/
private ClassLoader getProjectClassLoader()
throws MojoExecutionException
{
if ( projectClassLoader == null )
{
List<String> classPath;
try
{
classPath = getCompileClasspathElements( project );
}
catch ( DependencyResolutionRequiredException e )
{
throw new MojoExecutionException( "DependencyResolutionRequiredException: " + e.getMessage(), e );
}
List<URL> urls = new ArrayList<>( classPath.size() );
for ( String filename : classPath )
{
try
{
urls.add( new File( filename ).toURL() );
}
catch ( MalformedURLException e )
{
throw new MojoExecutionException( "MalformedURLException: " + e.getMessage(), e );
}
}
projectClassLoader = new URLClassLoader( urls.toArray( new URL[urls.size()] ), null );
}
return projectClassLoader;
}
/**
* Process the given {@link JavaClass}, ie add missing javadoc tags depending user parameters.
*
* @param javaClass not null
* @throws IOException if any
* @throws MojoExecutionException if any
*/
private void processFix( JavaClass javaClass )
throws IOException, MojoExecutionException
{
// Skipping inner classes
if ( javaClass.isInner() )
{
return;
}
File javaFile;
try
{