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