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