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