KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > enhance > EnhancedClassValidatorImpl


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.enhance;
16
17 import java.lang.reflect.Method JavaDoc;
18 import java.lang.reflect.Modifier JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.apache.hivemind.ErrorLog;
23 import org.apache.hivemind.service.MethodSignature;
24 import org.apache.tapestry.spec.IComponentSpecification;
25
26 /**
27  * Validates that an enhanced class is correct; checks that all inherited abstract methods are, in
28  * fact, implemented in the class.
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class EnhancedClassValidatorImpl implements EnhancedClassValidator
34 {
35     private ErrorLog _errorLog;
36
37     public void validate(Class JavaDoc baseClass, Class JavaDoc enhancedClass, IComponentSpecification specification)
38     {
39         Set JavaDoc implementedMethods = new HashSet JavaDoc();
40
41         Class JavaDoc current = enhancedClass;
42
43         while (true)
44         {
45             Method JavaDoc[] methods = current.getDeclaredMethods();
46
47             for (int i = 0; i < methods.length; i++)
48             {
49                 Method JavaDoc m = methods[i];
50
51                 boolean isAbstract = Modifier.isAbstract(m.getModifiers());
52
53                 MethodSignature s = new MethodSignature(m);
54
55                 if (isAbstract)
56                 {
57                     if (implementedMethods.contains(s))
58                         continue;
59
60                     _errorLog.error(EnhanceMessages.noImplForAbstractMethod(m, current, baseClass
61                             .getName(), enhancedClass), specification.getLocation(), null);
62                 }
63
64                 implementedMethods.add(s);
65             }
66
67             // An earlier version of this code walked the interfaces directly,
68
// but it appears that implementing an interface actually
69
// puts abstract method declarations into the class
70
// (at least, in terms of what getDeclaredMethods() returns).
71

72             // March up to the super class.
73

74             current = current.getSuperclass();
75
76             // Once advanced up to a concrete class, we trust that
77
// the compiler did its checking. Alternately, if
78
// we started on java.lang.Object for some reason, current
79
// will be null and we can stop.S
80

81             if (current == null || !Modifier.isAbstract(current.getModifiers()))
82                 break;
83         }
84
85     }
86
87     public void setErrorLog(ErrorLog errorLog)
88     {
89         _errorLog = errorLog;
90     }
91 }
Popular Tags