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