View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // Test case file for checkstyle.
3   // Created: 2001
4   ////////////////////////////////////////////////////////////////////////////////
5   package com.puppycrawl.tools.checkstyle.checks.coding.equalshashcode;
6   
7   import java.awt.*;
8   import java.io.ByteArrayOutputStream;
9   import java.io.File;
10  
11  /**
12   * Test case for detecting simple semantic errors.
13   * @author Lars K├╝hne
14   **/
15  class InputEqualsHashCodeSemantic
16  {
17      /* Boolean instantiation in a static initializer */
18      static {
19          Boolean x = new Boolean(true);
20      }
21  
22      /* Boolean instantiation in a non-static initializer */
23      {
24          Boolean x = new Boolean(true);
25          Boolean[] y = new Boolean[]{Boolean.TRUE, Boolean.FALSE};
26      }
27  
28      /** fully qualified Boolean instantiation in a method. **/
29      Boolean getBoolean()
30      {
31          return new Boolean(true);
32      }
33  
34      void otherInstantiations()
35      {
36          // instantiation of classes in the same package
37          Object o1 = new InputBraces();
38          Object o2 = new InputModifier();
39          // classes in another package with .* import
40          ByteArrayOutputStream s = new ByteArrayOutputStream();
41          File f = new File("/tmp");
42          // classes in another package with explicit import
43          Dimension dim = new Dimension();
44          Color col = new Color(0, 0, 0);
45      }
46  
47      public class EqualsVsHashCode1
48      {
49          public boolean equals(int a) // wrong arg type, don't flag
50          {
51              return a == 1;
52          }
53      }
54  
55      public class EqualsVsHashCode2
56      {
57          public boolean equals(String a) // don't flag
58          {
59              return true;
60          }
61      }
62  
63      public class EqualsVsHashCode3
64      {
65          public boolean equals(Object a) // don't flag
66          {
67              return true;
68          }
69  
70          public int hashCode()
71          {
72              return 0;
73          }
74      }
75  
76      public class EqualsVsHashCode4
77      {
78          // in anon inner class
79          ByteArrayOutputStream bos1 = new ByteArrayOutputStream()
80          {
81              public boolean equals(Object a) // don't flag
82              {
83                  return true;
84              }
85  
86              public int hashCode()
87              {
88                  return 0;
89              }
90          };
91  
92          ByteArrayOutputStream bos2 = new ByteArrayOutputStream()
93          {
94              public boolean equals(Object a) // flag
95              {
96                  return true;
97              }
98          };
99      }
100 
101     public void triggerEmptyBlockWithoutBlock()
102     {
103         // an if statement without a block to increase test coverage
104         if (true)
105             return;
106     }
107     
108     // empty instance initializer
109     {
110     }
111 
112     public class EqualsVsHashCode5
113     {
114         public <A> boolean equals(int a) // wrong arg type, don't flag even with generics
115         {
116             return a == 1;
117         }
118     }
119 
120     public class EqualsVsHashCode6
121     {
122         public <A> boolean equals(Comparable<A> a) // don't flag
123         {
124             return true;
125         }
126     }
127 
128     private class InputBraces {
129         
130     }
131 
132     private class InputModifier {
133         
134     }
135 }