View Javadoc
1   /*
2   HideUtilityClassConstructor
3   
4   
5   */
6   
7   package com.puppycrawl.tools.checkstyle.checks.design.hideutilityclassconstructor;
8   
9   /**
10   * Test case for the "design for inheritance" check.
11   * @author Lars Kühne
12   **/
13  public abstract class InputHideUtilityClassConstructorDesignForExtension
14  {
15      // some methods that are OK
16  
17      public interface InterfaceOK
18      {
19          void implicitlyAbstract();
20      }
21  
22      final class ClassOK
23      {
24          protected void finalThroughClassDef()
25          {
26              System.identityHashCode("no way to override");
27          }
28      }
29  
30      protected void nonFinalButEmpty()
31      {
32      }
33  
34      public void nonFinalButEmpty2()
35      {
36          // comments don't count as content...
37      }
38  
39      private void aPrivateMethod()
40      {
41          System.identityHashCode("no way to override");
42      }
43  
44      protected abstract void nonFinalButAbstract();
45  
46      // this one is bad: neither abstract, final, nor empty
47  
48      protected void doh()
49      {
50          System.identityHashCode("nonempty and overriding possible");
51      }
52  
53      public final void aFinalMethod()
54      {
55      System.identityHashCode("no way to override");
56      }
57  
58      public static void aStaticMethod()
59      {
60      System.identityHashCode("no way to override");
61      }
62  
63      // tries to trigger bug #884035
64      // MyComparator is a private class, so there cannot be subclasses
65      // and it should not be necessary to declare compare() as final
66      private class MyComparator implements java.util.Comparator
67      {
68          public int compare(Object o1, Object o2)
69          {
70              // some complex stuff that would normally trigger a violation report
71              if (o1.hashCode() > o2.hashCode()) {
72                  return -1;
73              }
74              else {
75                  return 1;
76              }
77          }
78      }
79  
80      public final class aFinalClass
81      {
82          public void someMethod()
83          {
84          System.identityHashCode("nonempty and overriding is possible");
85          }
86      }
87  
88      public class nonFinalClass
89      {
90      //private ctor
91      private nonFinalClass(){}
92          public void someMethod()
93          {
94          System.identityHashCode("nonempty and overriding is possible");
95          }
96      }
97  
98      public class anotherNonFinalClass
99      {
100     //nonPrivate ctor
101     public anotherNonFinalClass(){}
102         public void someMethod()
103         {
104         System.identityHashCode("nonempty and overriding is possible");
105         }
106     }
107 
108     // enums should be skipped
109     public enum TEnum
110     {
111         FIRST,
112         SECOND;
113 
114         public int value()
115         {
116             return 3;
117         }
118     }
119 }