-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathperformance.xml
More file actions
941 lines (869 loc) · 36.9 KB
/
performance.xml
File metadata and controls
941 lines (869 loc) · 36.9 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
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
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Performance"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
Rules that flag suboptimal code.
</description>
<rule name="AddEmptyString"
language="java"
since="4.0"
message="Do not add empty strings"
class="net.sourceforge.pmd.lang.java.rule.performance.AddEmptyStringRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#addemptystring">
<description>
The conversion of literals to strings by concatenating them with empty strings is inefficient.
It is much better to use one of the type-specific `toString()` methods instead or `String.valueOf()`.
</description>
<priority>3</priority>
<example>
<![CDATA[
String s = "" + 123; // inefficient
String t = Integer.toString(456); // preferred approach
]]>
</example>
</rule>
<rule name="AppendCharacterWithChar"
language="java"
since="3.5"
message="Avoid appending characters as strings in StringBuffer.append."
class="net.sourceforge.pmd.lang.java.rule.performance.AppendCharacterWithCharRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#appendcharacterwithchar">
<description>
Avoid concatenating characters as strings in StringBuffer/StringBuilder.append methods.
</description>
<priority>3</priority>
<example>
<![CDATA[
StringBuffer sb = new StringBuffer();
sb.append("a"); // avoid this
StringBuffer sb = new StringBuffer();
sb.append('a'); // use this instead
]]>
</example>
</rule>
<rule name="AvoidArrayLoops"
language="java"
since="3.5"
message="Arrays.copyOf or System.arraycopy are more efficient"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#avoidarrayloops">
<description>
Instead of manually copying data between two arrays, use the more efficient `Arrays.copyOf`
or `System.arraycopy` method instead.
To copy only part of the array, use `Arrays.copyOfRange` or `System.arraycopy`.
If you want to copy/move elements inside the _same_ array (e.g. shift the elements), use `System.arraycopy`.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//(ForStatement[ForUpdate//(UnaryExpression[@Operator=('++','--')] | AssignmentExpression[@Operator = ('+=', '-=')][NumericLiteral[@Image = '1']])]
| WhileStatement | DoStatement)
[not(.//ContinueStatement)]
[not(.//BreakStatement[not(parent::SwitchFallthroughBranch)])]
[not(.//ThrowStatement)]
[not(.//ReturnStatement)]
[count(Block//AssignmentExpression[@Operator='=']
(: no nested arrays or method calls as array index :)
[count(ArrayAccess[not(.//ArrayAccess)]
[not(.//MethodCall)])=2]
(: array access indexes must be same (excluding constants) :)
[deep-equal(
sort(distinct-values(ArrayAccess[1]/(VariableAccess[2]|InfixExpression//VariableAccess)
(: don't consider array length accesses :)
[not(parent::FieldAccess[@Name='length'])]
[
(: exclude referenced constants :)
not(@Name = (ancestor::MethodDeclaration|//FieldDeclaration)//VariableDeclarator[NumericLiteral][not(../../../parent::ForInit)]/VariableId/@Name)
or
(: include loop variable :)
@Name = ancestor::ForStatement/ForInit/LocalVariableDeclaration/VariableDeclarator/VariableId/@Name
]
/@Name)),
sort(distinct-values(ArrayAccess[2]/(VariableAccess[2]|InfixExpression//VariableAccess)
(: don't consider array length accesses :)
[not(parent::FieldAccess[@Name='length'])]
[
(: exclude referenced constants :)
not(@Name = (ancestor::MethodDeclaration|//FieldDeclaration)//VariableDeclarator[NumericLiteral][not(../../../parent::ForInit)]/VariableId/@Name)
or
(: include loop variable :)
@Name = ancestor::ForStatement/ForInit/LocalVariableDeclaration/VariableDeclarator/VariableId/@Name
]
/@Name))
)]
)=1]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
class Scratch {
void copy_a_to_b() {
int[] a = new int[10];
int[] b = new int[10];
for (int i = 0; i < a.length; i++) {
b[i] = a[i];
}
// equivalent
b = Arrays.copyOf(a, a.length);
// equivalent
System.arraycopy(a, 0, b, 0, a.length);
int[] c = new int[10];
// this will not trigger the rule
for (int i = 0; i < c.length; i++) {
b[i] = a[c[i]];
}
}
}
]]>
</example>
<example>
<![CDATA[
class Scratch {
void shift_left(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
a[i] = a[i + 1];
}
// equivalent
System.arraycopy(a, 1, a, 0, a.length - 1);
}
void shift_right(int[] a) {
for (int i = a.length - 1; i > 0; i--) {
a[i] = a[i - 1];
}
// equivalent
System.arraycopy(a, 0, a, 1, a.length - 1);
}
}
]]>
</example>
</rule>
<rule name="AvoidCalendarDateCreation"
since="6.25.0"
language="java"
message="A Calendar is used to get the current time, this is expensive."
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
typeResolution="true"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#avoidcalendardatecreation">
<description>
Problem: `java.util.Calendar` is a heavyweight object and expensive to create. It should only be used, if
calendar calculations are needed.
Solution: Use `new Date()`, Java 8+ `java.time.LocalDateTime.now()` or `ZonedDateTime.now()`.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value><![CDATA[
//MethodCall[pmd-java:matchesSig("java.util.Calendar#getTime()") or pmd-java:matchesSig("java.util.Calendar#getTimeInMillis()")]
[*[1][local-name() = ('MethodCall', 'ConstructorCall')]
[pmd-java:matchesSig("java.util.Calendar#getInstance()")
or pmd-java:matchesSig("java.util.GregorianCalendar#getInstance()")
or pmd-java:matchesSig("java.util.GregorianCalendar#new()")]
]
|
//MethodCall[pmd-java:matchesSig("java.util.Calendar#getTime()") or pmd-java:matchesSig("java.util.Calendar#getTimeInMillis()")]
[*[1][local-name() = 'VariableAccess']]
(: ignore if .set* or .add or .clear or .roll is called on the variable :)
[not(VariableAccess/@Name = ancestor::Block//MethodCall[starts-with(@MethodName, "set") or @MethodName = ("add", "clear", "roll")]/VariableAccess/@Name)]
(: variable must be initialized with getInstance :)
[VariableAccess/@Name = ancestor::Block//LocalVariableDeclaration/VariableDeclarator[
(MethodCall | ConstructorCall)
[pmd-java:matchesSig("java.util.Calendar#getInstance()")
or pmd-java:matchesSig("java.util.GregorianCalendar#getInstance()")
or pmd-java:matchesSig("java.util.GregorianCalendar#new()")]
]/VariableId/@Name]
|
//ConstructorCall[pmd-java:typeIs("org.joda.time.DateTime") or pmd-java:typeIs("org.joda.time.LocalDateTime")]
[ArgumentList[(MethodCall | ConstructorCall)
[pmd-java:matchesSig("java.util.Calendar#getInstance()")
or pmd-java:matchesSig("java.util.GregorianCalendar#getInstance()")
or pmd-java:matchesSig("java.util.GregorianCalendar#new()")]]
]
]]></value>
</property>
</properties>
<example>
<![CDATA[
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
public class DateStuff {
private Date bad1() {
return Calendar.getInstance().getTime(); // now
}
private Date good1a() {
return new Date(); // now
}
private LocalDateTime good1b() {
return LocalDateTime.now();
}
private long bad2() {
return Calendar.getInstance().getTimeInMillis();
}
private long good2() {
return System.currentTimeMillis();
}
}
]]>
</example>
</rule>
<rule name="AvoidFileStream"
since="6.0.0"
message="Avoid instantiating FileInputStream, FileOutputStream, FileReader, or FileWriter"
language="java"
minimumLanguageVersion="1.7"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#avoidfilestream">
<description>
The FileInputStream and FileOutputStream classes contains a finalizer method which will cause garbage
collection pauses.
See [JDK-8080225](https://bugs.openjdk.org/browse/JDK-8080225) for details.
The FileReader and FileWriter constructors instantiate FileInputStream and FileOutputStream,
again causing garbage collection issues while finalizer methods are called.
* Use `Files.newInputStream(Paths.get(fileName))` instead of `new FileInputStream(fileName)`.
* Use `Files.newOutputStream(Paths.get(fileName))` instead of `new FileOutputStream(fileName)`.
* Use `Files.newBufferedReader(Paths.get(fileName))` instead of `new FileReader(fileName)`.
* Use `Files.newBufferedWriter(Paths.get(fileName))` instead of `new FileWriter(fileName)`.
Please note, that the `java.nio` API does not throw a `FileNotFoundException` anymore, instead
it throws a `NoSuchFileException`. If your code dealt explicitly with a `FileNotFoundException`,
then this needs to be adjusted. Both exceptions are subclasses of `IOException`, so catching
that one covers both.
</description>
<priority>1</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ConstructorCall/ClassType[
pmd-java:typeIs('java.io.FileInputStream')
or pmd-java:typeIs('java.io.FileOutputStream')
or pmd-java:typeIs('java.io.FileReader')
or pmd-java:typeIs('java.io.FileWriter')
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
// these instantiations cause garbage collection pauses, even if properly closed
FileInputStream fis = new FileInputStream(fileName);
FileOutputStream fos = new FileOutputStream(fileName);
FileReader fr = new FileReader(fileName);
FileWriter fw = new FileWriter(fileName);
// the following instantiations help prevent Garbage Collection pauses, no finalization
try(InputStream is = Files.newInputStream(Paths.get(fileName))) {
}
try(OutputStream os = Files.newOutputStream(Paths.get(fileName))) {
}
try(BufferedReader br = Files.newBufferedReader(Paths.get(fileName), StandardCharsets.UTF_8)) {
}
try(BufferedWriter wr = Files.newBufferedWriter(Paths.get(fileName), StandardCharsets.UTF_8)) {
}
]]>
</example>
</rule>
<rule name="AvoidInstantiatingObjectsInLoops"
language="java"
since="2.2"
message="Avoid instantiating new objects inside loops"
class="net.sourceforge.pmd.lang.java.rule.performance.AvoidInstantiatingObjectsInLoopsRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#avoidinstantiatingobjectsinloops">
<description>
New objects created within loops should be checked to see if they can created outside them and reused.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Something {
public static void main( String as[] ) {
for (int i = 0; i < 10; i++) {
Foo f = new Foo(); // Avoid this whenever you can it's really expensive
}
}
}
]]>
</example>
</rule>
<rule name="BigIntegerInstantiation"
language="java"
since="3.9"
message="Don''t create instances of already existing BigInteger and BigDecimal (ZERO, ONE, TEN)"
class="net.sourceforge.pmd.lang.java.rule.performance.BigIntegerInstantiationRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#bigintegerinstantiation">
<description>
Don't create instances of already existing BigInteger (`BigInteger.ZERO`, `BigInteger.ONE`),
for Java 1.5 onwards, BigInteger.TEN and BigDecimal (`BigDecimal.ZERO`, `BigDecimal.ONE`, `BigDecimal.TEN`) and
for Java 9 onwards `BigInteger.TWO`.
</description>
<priority>3</priority>
<example>
<![CDATA[
BigInteger bi1 = new BigInteger("1"); // reference BigInteger.ONE instead
BigInteger bi2 = new BigInteger("0"); // reference BigInteger.ZERO instead
BigInteger bi3;
bi3 = new BigInteger("0"); // reference BigInteger.ZERO instead
BigDecimal bd1 = new BigDecimal(0); // reference BigDecimal.ZERO instead
BigDecimal bd2 = new BigDecimal("0.") ; // reference BigDecimal.ZERO instead
BigDecimal bd3 = new BigDecimal(10); // reference BigDecimal.TEN instead
]]>
</example>
</rule>
<rule name="ConsecutiveAppendsShouldReuse"
language="java"
since="5.1"
message="StringBuffer (or StringBuilder).append is called consecutively without reusing the target variable."
class="net.sourceforge.pmd.lang.java.rule.performance.ConsecutiveAppendsShouldReuseRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#consecutiveappendsshouldreuse">
<description>
Consecutive calls to StringBuffer/StringBuilder .append should be chained, reusing the target object. This can improve the performance
by producing a smaller bytecode, reducing overhead and improving inlining. A complete analysis can be found [here](https://github.com/pmd/pmd/issues/202#issuecomment-274349067)
</description>
<priority>3</priority>
<example>
<![CDATA[
String foo = " ";
StringBuffer buf = new StringBuffer();
buf.append("Hello"); // poor
buf.append(foo);
buf.append("World");
StringBuffer buf = new StringBuffer();
buf.append("Hello").append(foo).append("World"); // good
]]>
</example>
</rule>
<rule name="ConsecutiveLiteralAppends"
language="java"
since="3.5"
message="StringBuffer (or StringBuilder).append is called {0} consecutive times with literals. Use a single append with a single combined String."
class="net.sourceforge.pmd.lang.java.rule.performance.ConsecutiveLiteralAppendsRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#consecutiveliteralappends">
<description>
Consecutively calling StringBuffer/StringBuilder.append(...) with literals should be avoided.
Since the literals are constants, they can already be combined into a single String literal and this String
can be appended in a single method call.
</description>
<priority>3</priority>
<example>
<![CDATA[
StringBuilder buf = new StringBuilder();
buf.append("Hello").append(" ").append("World"); // poor
buf.append("Hello World"); // good
buf.append('h').append('e').append('l').append('l').append('o'); // poor
buf.append("hello"); // good
buf.append(1).append('m'); // poor
buf.append("1m"); // good
]]>
</example>
</rule>
<rule name="InefficientEmptyStringCheck"
language="java"
since="3.6"
message="String.trim().length() == 0 / String.trim().isEmpty() is an inefficient way to validate a blank String."
class="net.sourceforge.pmd.lang.java.rule.performance.InefficientEmptyStringCheckRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#inefficientemptystringcheck">
<description>
<![CDATA[
Checking the length of `string.trim()` or `string.strip()` is an inefficient way
to decide if a string is really blank, as it creates a new `String` object just to check its size.
As of Java 11 you can use `string.isBlank()` to decide if a string consists only of whitespace,
that check is equivalent to `string.strip().isEmpty()`. To get the semantic of
`string.trim().isEmpty()`, you can loop through the characters and compare them to 0x20:
```java
private boolean checkTrimEmpty(String str) {
return str.chars().allMatch(c -> c <= 0x20);
}
```
You can also consider using library functions that include a null check
(e.g. Apache's `StringUtils#isBlank` in commons-lang,
Spring's `StringUtils#hasText` in the Spring framework) or use a different definition of whitespace
(e.g. Google's `CharMatcher#whitespace` in Guava).
Calling `string.strip().isBlank()` is also redundant and should be simplified to `string.isBlank()`.
]]>
</description>
<priority>3</priority>
<example>
<![CDATA[
public void bar(String string) {
if (string != null && string.trim().length() > 0) {
doSomething();
}
}
]]>
</example>
</rule>
<rule name="InefficientStringBuffering"
language="java"
since="3.4"
message="Avoid concatenating nonliterals in a StringBuffer/StringBuilder constructor or append()."
class="net.sourceforge.pmd.lang.java.rule.performance.InefficientStringBufferingRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#inefficientstringbuffering">
<description>
Avoid concatenating non-literals in a StringBuffer constructor or append() since intermediate buffers will
need to be be created and destroyed by the JVM.
</description>
<priority>3</priority>
<example>
<![CDATA[
// Avoid this, two buffers are actually being created here
StringBuffer sb = new StringBuffer("tmp = "+System.getProperty("java.io.tmpdir"));
// do this instead
StringBuffer sb = new StringBuffer("tmp = ");
sb.append(System.getProperty("java.io.tmpdir"));
]]>
</example>
</rule>
<rule name="InsufficientStringBufferDeclaration"
language="java"
since="3.6"
message="{0} has been initialized with size {1}, but has at least {2} characters appended."
class="net.sourceforge.pmd.lang.java.rule.performance.InsufficientStringBufferDeclarationRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#insufficientstringbufferdeclaration">
<description>
Failing to pre-size a StringBuffer or StringBuilder properly could cause it to re-size many times
during runtime. This rule attempts to determine the total number the characters that are actually
passed into StringBuffer.append(), but represents a best guess "worst case" scenario. An empty
StringBuffer/StringBuilder constructor initializes the object to 16 characters. This default
is assumed if the length of the constructor can not be determined.
</description>
<priority>3</priority>
<example>
<![CDATA[
StringBuilder bad = new StringBuilder();
bad.append("This is a long string that will exceed the default 16 characters");
StringBuilder good = new StringBuilder(41);
good.append("This is a long string, which is pre-sized");
]]>
</example>
</rule>
<rule name="OptimizableToArrayCall"
language="java"
since="1.8"
minimumLanguageVersion="1.6"
message="This call to Collection.toArray() may be optimizable"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#optimizabletoarraycall">
<description>
Calls to a collection's `toArray(E[])` method should specify a target array of zero size. This allows the JVM
to optimize the memory allocation and copying as much as possible.
Previous versions of this rule (pre PMD 6.0.0) suggested the opposite, but current JVM implementations
perform always better, when they have full control over the target array. And allocation an array via
reflection is nowadays as fast as the direct allocation.
See also [Arrays of Wisdom of the Ancients](https://shipilev.net/blog/2016/arrays-wisdom-ancients/)
Note: If you don't need an array of the correct type, then the simple `toArray()` method without an array
is faster, but returns only an array of type `Object[]`.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//MethodCall[pmd-java:matchesSig("java.util.Collection#toArray(_)")]
[ArgumentList/ArrayAllocation/ArrayType/ArrayDimensions/ArrayDimExpr[not(NumericLiteral[@Image="0"])]]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
List<Foo> foos = getFoos();
// much better; this one allows the jvm to allocate an array of the correct size and effectively skip
// the zeroing, since each array element will be overridden anyways
Foo[] fooArray = foos.toArray(new Foo[0]);
// inefficient, the array needs to be zeroed out by the jvm before it is handed over to the toArray method
Foo[] fooArray = foos.toArray(new Foo[foos.size()]);
]]>
</example>
</rule>
<rule name="RedundantFieldInitializer"
language="java"
since="5.0"
message="Avoid using redundant field initializer for ''${variableName}''"
class="net.sourceforge.pmd.lang.java.rule.performance.RedundantFieldInitializerRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#redundantfieldinitializer">
<description>
Java will initialize fields with known default values so any explicit initialization of those same defaults
is redundant and results in a larger class file (approximately three additional bytecode instructions per field).
</description>
<priority>3</priority>
<example>
<![CDATA[
public class C {
boolean b = false; // examples of redundant initializers
byte by = 0;
short s = 0;
char c = 0;
int i = 0;
long l = 0;
float f = .0f; // all possible float literals
double d = 0d; // all possible double literals
Object o = null;
MyClass mca[] = null;
int i1 = 0, ia1[] = null;
class Nested {
boolean b = false;
}
}
]]>
</example>
</rule>
<rule name="StringInstantiation"
language="java"
since="1.0"
message="Avoid instantiating String objects; this is usually unnecessary."
class="net.sourceforge.pmd.lang.java.rule.performance.StringInstantiationRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#stringinstantiation">
<description>
Avoid instantiating String objects; this is usually unnecessary since they are immutable and can be safely shared.
</description>
<priority>2</priority>
<example>
<![CDATA[
private String bar = new String("bar"); // just do a String bar = "bar";
]]>
</example>
</rule>
<rule name="StringToString"
language="java"
since="1.0"
message="Avoid calling toString() on String objects; this is unnecessary."
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#stringtostring">
<description>
Avoid calling toString() on objects already known to be string instances; this is unnecessary.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
//MethodCall[pmd-java:matchesSig("java.lang.String#toString()")]
</value>
</property>
</properties>
<example>
<![CDATA[
private String baz() {
String bar = "howdy";
return bar.toString();
}
]]>
</example>
</rule>
<rule name="TooFewBranchesForASwitchStatement" deprecated="true" ref="TooFewBranchesForSwitch"/>
<rule name="TooFewBranchesForSwitch"
language="java"
since="4.2"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
message="A switch with less than three branches is inefficient, use a 'if statement' instead."
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#toofewbranchesforswitch">
<description>
Switch statements are intended to be used to support complex branching behaviour. Using a switch for only a few
cases is ill-advised, since switches are not as easy to understand as if-else statements. In these cases use the
if-else statement to increase code readability.
Note: This rule was named TooFewBranchesForASwitchStatement before PMD 7.7.0.
</description>
<priority>3</priority>
<properties>
<property name="minimumNumberCaseForASwitch" type="Integer" description="Minimum number of branches for a switch" min="1" max="100" value="3"/>
<property name="xpath">
<value>
<![CDATA[
//(SwitchStatement | SwitchExpression)
[ count(*) > 1 ] (: ignore empty switch blocks :)
[ (count(*/SwitchLabel/*) + count(*/SwitchLabel[@Default = true()])) < $minimumNumberCaseForASwitch ]
(: do not consider exhaustive switches unless there is a default case :)
[@Exhaustive = false() or @DefaultCase = true()]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
// With a minimumNumberCaseForASwitch of 3
public class Foo {
public void bar(int condition) {
switch (condition) {
case 1:
instruction;
break;
default:
break; // not enough for a 'switch' stmt, a simple 'if' stmt would have been more appropriate
}
}
}
]]>
</example>
</rule>
<rule name="UseArrayListInsteadOfVector"
language="java"
since="3.0"
message="Use ArrayList instead of Vector"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#usearraylistinsteadofvector">
<description>
ArrayList is a much better Collection implementation than Vector if thread-safe operation is not required.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ConstructorCall/ClassType[pmd-java:typeIsExactly('java.util.Vector') or pmd-java:typeIsExactly('Vector')]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
import java.util.*;
public class SimpleTest extends TestCase {
public void testX() {
Collection c1 = new Vector();
Collection c2 = new ArrayList(); // achieves the same with much better performance
}
}
]]>
</example>
</rule>
<rule name="UseArraysAsList"
language="java"
since="3.5"
message="Use asList instead of tight loops"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#usearraysaslist">
<description>
<![CDATA[
The `java.util.Arrays` class has a `asList()` method that should be used when you want to create a new List from
an array of objects. It is faster than executing a loop to copy all the elements of the array one by one.
Note that the result of `Arrays.asList()` is backed by the specified array,
changes in the returned list will result in the array to be modified.
For that reason, it is not possible to add new elements to the returned list of `Arrays.asList()`
(UnsupportedOperationException).
You must use `new ArrayList<>(Arrays.asList(...))` if that is inconvenient for you (e.g. because of concurrent access).
]]>
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ForStatement
[ForInit
[LocalVariableDeclaration
[PrimitiveType[@Kind = 'int']]
[VariableDeclarator[NumericLiteral[@IntLiteral][@Image = '0']]]
]
]
[*[2]//FieldAccess[@Name = 'length']/VariableAccess[pmd-java:typeIs("java.lang.Object[]")]]
[ForUpdate
[StatementExpressionList
[UnaryExpression[@Operator = '++']
| AssignmentExpression[@Operator = '+='][NumericLiteral[@IntLiteral][@Image = '1']]]
]
]
/*[last()][not(IfStatement)]/ExpressionStatement/
MethodCall
[pmd-java:matchesSig('java.util.List#add(_)')]
[ArgumentList/ArrayAccess
[VariableAccess[@Name = ancestor::ForStatement/ForInit/LocalVariableDeclaration/VariableDeclarator/VariableId/@Name]]
]
|
//ForeachStatement
[VariableAccess[pmd-java:typeIs("java.lang.Object[]")]]
/*[last()][not(IfStatement)]/ExpressionStatement/MethodCall
[pmd-java:matchesSig('java.util.List#add(_)')]
[ArgumentList
[VariableAccess[@Name = ancestor::ForeachStatement/LocalVariableDeclaration/VariableDeclarator/VariableId/@Name]]
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Test {
public void foo(Integer[] ints) {
// could just use Arrays.asList(ints)
List<Integer> l = new ArrayList<>(100);
for (int i = 0; i < ints.length; i++) {
l.add(ints[i]);
}
List<Integer> anotherList = new ArrayList<>();
for (int i = 0; i < ints.length; i++) {
anotherList.add(ints[i].toString()); // won't trigger the rule
}
}
}
]]>
</example>
</rule>
<rule name="UseIndexOfChar"
language="java"
since="3.5"
message="String.indexOf(char) is faster than String.indexOf(String)."
class="net.sourceforge.pmd.lang.java.rule.performance.UseIndexOfCharRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#useindexofchar">
<description>
Use String.indexOf(char) when checking for the index of a single character; it executes faster.
</description>
<priority>3</priority>
<example>
<![CDATA[
String s = "hello world";
// avoid this
if (s.indexOf("d") {}
// instead do this
if (s.indexOf('d') {}
]]>
</example>
</rule>
<rule name="UseIOStreamsWithApacheCommonsFileItem"
since="6.25.0"
language="java"
message="Avoid memory intensive FileItem.get() or FileItem.getString()"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
typeResolution="true"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#useiostreamswithapachecommonsfileitem">
<description>
Problem: Use of [FileItem.get()](https://javadoc.io/static/commons-fileupload/commons-fileupload/1.5/org/apache/commons/fileupload/FileItem.html#get--)
and [FileItem.getString()](https://javadoc.io/static/commons-fileupload/commons-fileupload/1.5/org/apache/commons/fileupload/FileItem.html#getString--)
could exhaust memory since they load the entire file into memory.
Solution: Use [FileItem.getInputStream()](https://javadoc.io/static/commons-fileupload/commons-fileupload/1.5/org/apache/commons/fileupload/FileItem.html#getInputStream--)
and buffering.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//MethodCall
[@MethodName = 'get' or @MethodName = 'getString']
[*[pmd-java:typeIs('org.apache.commons.fileupload.FileItem')]]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
import org.apache.commons.fileupload.FileItem;
public class FileStuff {
private String bad(FileItem fileItem) {
return fileItem.getString();
}
private InputStream good(FileItem fileItem) {
return fileItem.getInputStream();
}
}
]]>
</example>
</rule>
<rule name="UselessStringValueOf"
language="java"
since="3.8"
message="No need to call String.valueOf to append to a string."
class="net.sourceforge.pmd.lang.java.rule.performance.UselessStringValueOfRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#uselessstringvalueof">
<description>
No need to call String.valueOf to append to a string; just use the valueOf() argument directly.
</description>
<priority>3</priority>
<example>
<![CDATA[
public String convert(int i) {
String s;
s = "a" + String.valueOf(i); // not required
s = "a" + i; // preferred approach
return s;
}
]]>
</example>
</rule>
<rule name="UseStringBufferForStringAppends"
language="java"
since="3.1"
message="Prefer StringBuilder (non-synchronized) or StringBuffer (synchronized) over += for concatenating strings"
class="net.sourceforge.pmd.lang.java.rule.performance.UseStringBufferForStringAppendsRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#usestringbufferforstringappends">
<description>
The use of the '+=' operator for appending strings causes the JVM to create and use an internal StringBuffer.
If a non-trivial number of these concatenations are being used then the explicit use of a StringBuilder or
threadsafe StringBuffer is recommended to avoid this.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
String inefficientConcatenation() {
String result = "";
for (int i = 0; i < 10; i++) {
// warning: this concatenation will create one new StringBuilder per iteration
result += getStringFromSomeWhere(i);
}
return result;
}
String efficientConcatenation() {
// better would be to use one StringBuilder for the entire loop
StringBuilder result = new StringBuilder();
for (int i = 0; i < 10; i++) {
result.append(getStringFromSomeWhere(i));
}
return result.toString();
}
}
]]>
</example>
</rule>
<rule name="UseStringBufferLength"
language="java"
since="3.4"
message="This is an inefficient use of CharSequence.toString; call CharSequence.length instead."
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#usestringbufferlength">
<description>
Use StringBuffer.length() to determine StringBuffer length rather than using StringBuffer.toString().equals("")
or StringBuffer.toString().length() == ...
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value><![CDATA[
//MethodCall[pmd-java:matchesSig('_#length()')
and MethodCall[pmd-java:matchesSig('java.lang.CharSequence#toString()')]]
|
(: finds sb.toString().equals(someVar) where var is a final variable initialized with literal "" :)
//MethodCall[pmd-java:matchesSig('_#equals(_)')
and MethodCall[pmd-java:matchesSig('java.lang.AbstractStringBuilder#toString()')]
and ArgumentList/VariableAccess[@Name = //VariableDeclarator[StringLiteral[@Image='""']]
/VariableId[pmd-java:modifiers() = 'final']/@Name]]
|
(: finds sb.toString().equals("") :)
//MethodCall[pmd-java:matchesSig('_#equals(_)')
and MethodCall[pmd-java:matchesSig('java.lang.AbstractStringBuilder#toString()')]
and ArgumentList/StringLiteral[@Image='""']]
]]></value>
</property>
</properties>
<example>
<![CDATA[
StringBuffer sb = new StringBuffer();
if (sb.toString().equals("")) {} // inefficient
if (sb.length() == 0) {} // preferred
]]>
</example>
</rule>
<!--
other optimization/performance should be like avoiding
"" + int
or "" + (int) i
and String.valueOf(int)
and using Integer.toString(int)
IntegerToStringShouldBeUsed
LongToStringShouldBeUsed
BooleanToStringShouldBeUsed
-->
</ruleset>