View Javadoc
1   package com.google.checkstyle.test.chapter6programpractice.rule64finalizers;
2   
3   class InputNoFinalizeExtend {
4   
5     public static void doStuff() {
6       // This method do some stuff
7     }
8   
9     protected void finalize() throws Throwable { // violation 'Avoid using finalizer method.'
10      try {
11        doStuff();
12      } finally {
13        super.finalize();
14      }
15    }
16  }
17  
18  // negates effect of superclass finalizer
19  // violation below 'Top-level class EmptyFinalizer has to reside in its own source file.'
20  class EmptyFinalizer {
21  
22    protected void finalize() throws Throwable { // violation 'Avoid using finalizer method.'
23      // empty finalize ()
24    }
25  }
26  
27  // fails to call superclass finalize method
28  // violation below 'Top-level class WithoutTryCatchFinalizer has to reside in its own source file.'
29  class WithoutTryCatchFinalizer {
30  
31    public static void doStuff() {
32      // This method do some stuff
33    }
34  
35    protected void finalize() throws Throwable { // violation 'Avoid using finalizer method.'
36      doStuff();
37    }
38  }
39  
40  // public finalizer
41  // violation below 'Top-level class InputPublicFinalizer has to reside in its own source file.'
42  class InputPublicFinalizer {
43  
44    public static void doStuff() {
45      // This method do some stuff
46    }
47  
48    public void finalize() throws Throwable { // violation 'Avoid using finalizer method.'
49      try {
50        doStuff();
51      } finally {
52        super.finalize();
53      }
54    }
55  }
56  
57  // unless (or worse) finalizer
58  // violation below 'Top-level class InputSuperFinalizer has to reside in its own source file.'
59  class InputSuperFinalizer {
60  
61    protected void finalize() throws Throwable { // violation 'Avoid using finalizer method.'
62      super.finalize();
63    }
64  }
65  
66  // public finalizer
67  // violation below 'Top-level class InputStaticFinalizer has to reside in its own source file.'
68  class InputStaticFinalizer {
69  
70    public static void doStuff() {
71      // This method do some stuff
72    }
73  
74    protected void finalize() { // violation 'Avoid using finalizer method.'
75      try {
76        doStuff();
77      } finally {
78        // do nothing
79      }
80    }
81  
82    class InnerFinalizer {
83  
84      protected void finalize() { // violation 'Avoid using finalizer method.'
85        try {
86          doStuff();
87        } finally {
88          // do nothing
89        }
90      }
91    }
92  }
93  
94  // violation below 'Top-level class WithoutFinalize has to reside in its own source file.'
95  class WithoutFinalize {
96    public void doStuff() {
97      // This method do some stuff
98    }
99  
100   public void finalizeMe() {
101     // This method do some stuff
102   }
103 
104   public void doFinalize() {
105     // This method do some stuff
106   }
107 }
108 
109 // violation below 'Top-level class WithoutMethods has to reside in its own source file.'
110 class WithoutMethods {}
111 
112 // violation below 'Top-level class WithAnonymousClass has to reside in its own source file.'
113 class WithAnonymousClass {
114 
115   public static void doStuff() {
116     // This method do some stuff
117   }
118 
119   public void foo() {
120 
121     Ball b =
122         new Ball() {
123 
124           public void hit() {
125             System.identityHashCode("You hit it!");
126           }
127 
128           protected void finalize() { // violation 'Avoid using finalizer method.'
129             try {
130               doStuff();
131             } finally {
132               // do nothing
133             }
134           }
135         };
136     b.hit();
137   }
138 
139   interface Ball {
140     void hit();
141   }
142 }
143 
144 // violation below 'Top-level class WithFinalizer has to reside in its own source file.'
145 interface WithFinalizer {
146   void finalize(); // violation 'Avoid using finalizer method.'
147 }