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