KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > grammar > MemberMethodType


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

18 package org.apache.beehive.netui.compiler.grammar;
19
20 import org.apache.beehive.netui.compiler.AnnotationGrammar;
21 import org.apache.beehive.netui.compiler.AnnotationMemberType;
22 import org.apache.beehive.netui.compiler.CompilerUtils;
23 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
24 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeElementDeclaration;
25 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
26 import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
27 import org.apache.beehive.netui.compiler.typesystem.declaration.MethodDeclaration;
28 import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration;
29
30 public class MemberMethodType
31         extends AnnotationMemberType
32 {
33     private String JavaDoc _requiredMethodAnnotation;
34     private String JavaDoc _errorCode;
35
36     public MemberMethodType( String JavaDoc requiredMethodAnnotation, String JavaDoc errorCode, String JavaDoc requiredRuntimeVersion,
37                              AnnotationGrammar parentGrammar )
38     {
39         super( requiredRuntimeVersion, parentGrammar );
40         _requiredMethodAnnotation = requiredMethodAnnotation;
41         _errorCode = errorCode;
42     }
43
44     
45     public Object JavaDoc onCheck( AnnotationTypeElementDeclaration valueDecl, AnnotationValue value,
46                            AnnotationInstance[] parentAnnotations, MemberDeclaration classMember,
47                            int annotationArrayIndex )
48     {
49         //
50
// Look through all the methods to see if there is one whose name matches the given value.
51
//
52
TypeDeclaration outerType = CompilerUtils.getOuterClass( classMember );
53         MethodDeclaration[] methods = CompilerUtils.getClassMethods( outerType, null );
54         String JavaDoc methodName = ( String JavaDoc ) value.getValue();
55         
56         for ( int i = 0; i < methods.length; i++ )
57         {
58             MethodDeclaration method = methods[i];
59             
60             if ( method.getSimpleName().equals( methodName ) )
61             {
62                 if ( _requiredMethodAnnotation == null
63                      || CompilerUtils.getAnnotation( method, _requiredMethodAnnotation ) != null )
64                 {
65                     checkMethod( method, value, parentAnnotations, classMember );
66                     return method;
67                 }
68             }
69         }
70         
71         addError( value, _errorCode, methodName );
72         return null;
73     }
74     
75     protected MethodDeclaration findMethod( String JavaDoc methodName, TypeDeclaration outerType )
76     {
77         MethodDeclaration[] methods = CompilerUtils.getClassMethods( outerType, null );
78         
79         for ( int i = 0; i < methods.length; i++ )
80         {
81             MethodDeclaration method = methods[i];
82             
83             if ( method.getSimpleName().equals( methodName ) )
84             {
85                 if ( _requiredMethodAnnotation == null
86                      || CompilerUtils.getAnnotation( method, _requiredMethodAnnotation ) != null )
87                 {
88                     return method;
89                 }
90             }
91         }
92         
93         return null;
94     }
95
96     /**
97      * Derived classes can plug in here to do additional checks.
98      */

99     protected void checkMethod( MethodDeclaration method, AnnotationValue member, AnnotationInstance[] parentAnnotations,
100                                 MemberDeclaration classMember )
101     {
102     }
103 }
104
Popular Tags