| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.phoenix.report.samples; |
|---|
| 34 | |
|---|
| 35 | import java.math.BigDecimal; |
|---|
| 36 | import java.util.ArrayList; |
|---|
| 37 | import java.util.Collection; |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * This class is used to trigger PMD finders. |
|---|
| 41 | * @author Andreas Mandel |
|---|
| 42 | */ |
|---|
| 43 | public final class SimplePmdFindings |
|---|
| 44 | { |
|---|
| 45 | /** Used to test the finder. */ |
|---|
| 46 | public static int sNoneFinalStatic; |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * Trigger PMD basic/AvoidDecimalLiteralsInBigDecimalConstructor. |
|---|
| 50 | */ |
|---|
| 51 | public static final BigDecimal TEST = new BigDecimal(1.123); |
|---|
| 52 | |
|---|
| 53 | private static Object sSingleton = null; |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Trigger PMD design/UncommentedEmptyConstructor |
|---|
| 57 | */ |
|---|
| 58 | private SimplePmdFindings () |
|---|
| 59 | { |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Trigger method for findings. |
|---|
| 65 | */ |
|---|
| 66 | public void sampleMethod () |
|---|
| 67 | { |
|---|
| 68 | // Trigger PMD basic/ClassCastExceptionWithToArray. |
|---|
| 69 | final Collection c = new ArrayList(); |
|---|
| 70 | final Integer[] a = (Integer []) c.toArray(); |
|---|
| 71 | |
|---|
| 72 | // Trigger PMD basic/MisplacedNullCheck |
|---|
| 73 | if (a.toString().equals("hi") && a != null) |
|---|
| 74 | { |
|---|
| 75 | a.getClass(); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | // Trigger PMD basic/UnusedNullCheckInEquals |
|---|
| 79 | if (a != null && c.equals(a)) |
|---|
| 80 | { |
|---|
| 81 | a.getClass(); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | final BigDecimal bd = new BigDecimal("1"); |
|---|
| 85 | |
|---|
| 86 | // Trigger PMD basic/UselessOperationOnImmutable |
|---|
| 87 | bd.add(new BigDecimal("10")); |
|---|
| 88 | |
|---|
| 89 | final String s = "TEST"; |
|---|
| 90 | // Trigger PMD design/PositionLiteralsFirstInComparisons |
|---|
| 91 | if (s.equals("2")) |
|---|
| 92 | { |
|---|
| 93 | s.getClass(); |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /** Trigger PMD design/UncommentedEmptyMethod. */ |
|---|
| 98 | public void doSomething () |
|---|
| 99 | { |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** Trigger method for findings. */ |
|---|
| 103 | public void bool () |
|---|
| 104 | { |
|---|
| 105 | boolean b = true; |
|---|
| 106 | |
|---|
| 107 | // Trigger PMD controversial/BooleanInversion |
|---|
| 108 | b = !b; // slow |
|---|
| 109 | b ^= true; // fast |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /** Trigger method for findings. */ |
|---|
| 113 | public void design () |
|---|
| 114 | { |
|---|
| 115 | // Trigger PMD design/AssignmentToNonFinalStatic |
|---|
| 116 | sNoneFinalStatic = 1; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * Trigger method for findings. |
|---|
| 121 | * @return a singleton object. |
|---|
| 122 | */ |
|---|
| 123 | public Object getSingleton () |
|---|
| 124 | { |
|---|
| 125 | // Trigger PMD design/NonThreadSafeSingleton |
|---|
| 126 | if (sSingleton == null) |
|---|
| 127 | { |
|---|
| 128 | sSingleton = new Object(); |
|---|
| 129 | } |
|---|
| 130 | return sSingleton; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | /** |
|---|
| 134 | * Trigger PMD basic/UselessOverridingMethod. |
|---|
| 135 | * @return a String. |
|---|
| 136 | */ |
|---|
| 137 | public String toString () |
|---|
| 138 | { |
|---|
| 139 | return super.toString(); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | * Trigger PMD design.xml/UnnecessaryLocalBeforeReturn. |
|---|
| 144 | * @return 97 |
|---|
| 145 | */ |
|---|
| 146 | public int foo () |
|---|
| 147 | { |
|---|
| 148 | final int x = 97; |
|---|
| 149 | return x; |
|---|
| 150 | } |
|---|
| 151 | } |
|---|