KickJava   Java API By Example, From Geeks To Geeks.

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


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.AnnotationMemberType;
21 import org.apache.beehive.netui.compiler.CompilerUtils;
22 import org.apache.beehive.netui.compiler.Diagnostics;
23 import org.apache.beehive.netui.compiler.FlowControllerInfo;
24 import org.apache.beehive.netui.compiler.JpfLanguageConstants;
25 import org.apache.beehive.netui.compiler.RuntimeVersionChecker;
26 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
27 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
28 import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
29 import org.apache.beehive.netui.compiler.typesystem.declaration.MethodDeclaration;
30 import org.apache.beehive.netui.compiler.typesystem.declaration.ParameterDeclaration;
31 import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration;
32 import org.apache.beehive.netui.compiler.typesystem.env.AnnotationProcessorEnvironment;
33 import org.apache.beehive.netui.compiler.typesystem.type.DeclaredType;
34 import org.apache.beehive.netui.compiler.typesystem.type.TypeInstance;
35
36 import java.util.Collection JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40
41 public class CatchGrammar
42         extends BaseFlowControllerGrammar
43         implements JpfLanguageConstants
44 {
45     private static String JavaDoc[][] MUTUALLY_EXCLUSIVE_ATTRS = { { PATH_ATTR, METHOD_ATTR } };
46     private static String JavaDoc[][] REQUIRED_ATTRS = { { TYPE_ATTR }, { PATH_ATTR, METHOD_ATTR } };
47
48     private String JavaDoc _annotationRootName;
49     
50     
51     public CatchGrammar( AnnotationProcessorEnvironment env, Diagnostics diags, String JavaDoc requiredRuntimeVersion,
52                          RuntimeVersionChecker runtimeVersionChecker, String JavaDoc annotationRootName,
53                          FlowControllerInfo fcInfo )
54     {
55         super( env, diags, requiredRuntimeVersion, runtimeVersionChecker, fcInfo );
56         
57         _annotationRootName = annotationRootName; // the parent of the list of @Jpf.Catch annotations.
58
addMemberType( METHOD_ATTR, new CatchTagMethodType() );
59         AnnotationMemberType typeAttrType =
60                 new UniqueValueType( CATCHES_ATTR, false, false, null, this,
61                                      new TypeNameType( THROWABLE_CLASS_NAME, false, null, this ) );
62         addMemberType( TYPE_ATTR, typeAttrType );
63         addMemberType( PATH_ATTR, new ForwardToExternalPathType( new WebappPathOrActionType( false, null, this, fcInfo ), null, this ) );
64         addMemberType( MESSAGE_ATTR, new AnnotationMemberType( null, this ) );
65         addMemberType( MESSAGE_KEY_ATTR, new AnnotationMemberType( null, this ) );
66     }
67     
68     public String JavaDoc[][] getMutuallyExclusiveAttrs()
69     {
70         return MUTUALLY_EXCLUSIVE_ATTRS;
71     }
72
73     public String JavaDoc[][] getRequiredAttrs()
74     {
75         return REQUIRED_ATTRS;
76     }
77
78     /**
79      * @param checkResults map of member-name (String) -> result-from-checking (Object)
80      * @return a result (any Object) that will be passed back to the parent checker. May be null</code>.
81      */

82     protected Object JavaDoc onEndCheck( AnnotationInstance annotation, AnnotationInstance[] parentAnnotations,
83                                  MemberDeclaration classMember, Map JavaDoc checkResults )
84     {
85         MethodDeclaration handlerMethod = ( MethodDeclaration ) checkResults.get( METHOD_ATTR );
86         DeclaredType exceptionType = ( DeclaredType ) checkResults.get( TYPE_ATTR );
87         
88         //
89
// If either of these are null, then there was another already-reported error (e.g., type was unresolved).
90
//
91
if ( handlerMethod == null || exceptionType == null )
92         {
93             return null;
94         }
95         
96         //
97
// Make sure the given handler method can catch the right kind of exception.
98
//
99
ParameterDeclaration[] parameters = handlerMethod.getParameters();
100                 
101         //
102
// If the method's arguments are wrong in any way, don't worry about it -- the exception-handler checker will
103
// report an error.
104
//
105
if ( parameters.length > 0 )
106         {
107             TypeInstance handledExceptionType = parameters[0].getType();
108             
109             if ( ! CompilerUtils.isAssignableFrom( handledExceptionType, CompilerUtils.getDeclaration( exceptionType ) ) )
110             {
111                 addError( annotation, "error.incompatible-exception-handler", handlerMethod.getSimpleName(),
112                           CompilerUtils.getDeclaration( exceptionType ).getQualifiedName() );
113             }
114         }
115
116         return null;
117     }
118     
119     private class CatchTagMethodType
120             extends MemberMethodType
121     {
122         public CatchTagMethodType()
123         {
124             super( EXCEPTION_HANDLER_TAG_NAME, "error.unresolved-exception-handler", null, CatchGrammar.this );
125         }
126
127         /**
128          * Derived classes can plug in here to do additional checks.
129          */

130         protected void checkMethod( MethodDeclaration methodBeingChecked, AnnotationValue value,
131                                     AnnotationInstance[] parentAnnotations, MemberDeclaration classMember )
132         {
133             //
134
// Make sure the current entity (class or action method) doesn't have two @Jpf.Catch annotations
135
// that refer to methods with duplicate @Jpf.Forwards.
136
//
137
Collection JavaDoc catches =
138                     CompilerUtils.getAnnotationArrayValue( classMember, _annotationRootName, CATCHES_ATTR, true );
139             TypeDeclaration outerType = CompilerUtils.getOuterClass( classMember );
140             
141             if ( catches == null )
142             {
143                 return;
144             }
145             
146             for ( Iterator JavaDoc ii = catches.iterator(); ii.hasNext(); )
147             {
148                 AnnotationInstance catchAnnotation = ( AnnotationInstance ) ii.next();
149                 //
150
// Find the method referred to in this annotation. If we can't find it, do nothing -- this
151
// will get caught elsewhere in the checking.
152
//
153
String JavaDoc methodName = CompilerUtils.getString( catchAnnotation, METHOD_ATTR, false );
154             
155                 if ( methodName.length() > 0 && ! methodName.equals( methodBeingChecked.getSimpleName() ) )
156                 {
157                     MethodDeclaration otherMethod = findMethod( methodName, outerType );
158                     
159                     if ( otherMethod != null )
160                     {
161                         //
162
// Look through this other method's forwards. None may have the same name (and different path)
163
// as the current one.
164
//
165
Collection JavaDoc otherForwards =
166                                 CompilerUtils.getAnnotationArrayValue( otherMethod, EXCEPTION_HANDLER_TAG_NAME,
167                                                                           FORWARDS_ATTR, false );
168                         
169                         for ( Iterator JavaDoc i2 = otherForwards.iterator(); i2.hasNext(); )
170                         {
171                             AnnotationInstance otherForward = ( AnnotationInstance ) i2.next();
172                             String JavaDoc otherForwardName = CompilerUtils.getString( otherForward, NAME_ATTR, true );
173                             String JavaDoc otherForwardPath = CompilerUtils.getString( otherForward, PATH_ATTR, true );
174                             String JavaDoc otherFwdNavigateTo =
175                                     CompilerUtils.getEnumFieldName( otherForward, NAVIGATE_TO_ATTR, true );
176                         
177                             Collection JavaDoc forwards =
178                                     CompilerUtils.getAnnotationArrayValue( methodBeingChecked,
179                                                                               EXCEPTION_HANDLER_TAG_NAME,
180                                                                               FORWARDS_ATTR, false );
181                             
182                             for ( Iterator JavaDoc i3 = forwards.iterator(); i3.hasNext(); )
183                             {
184                                 AnnotationInstance forward = ( AnnotationInstance ) i3.next();
185                                 String JavaDoc forwardName = CompilerUtils.getString( forward, NAME_ATTR, true );
186                                 String JavaDoc forwardPath = CompilerUtils.getString( forward, PATH_ATTR, true );
187                                 String JavaDoc fwdNavigateTo = CompilerUtils.getEnumFieldName( forward, NAVIGATE_TO_ATTR, true );
188                             
189                                 if ( forwardName != null && forwardName.equals( otherForwardName ) )
190                                 {
191                                     if ( ( forwardPath == null || ! forwardPath.equals( otherForwardPath ) )
192                                          && ( fwdNavigateTo == null || ! fwdNavigateTo.equals( otherFwdNavigateTo ) ) )
193                                     {
194                                         addError( value, "error.duplicate-exception-handler-forwards",
195                                                   new Object JavaDoc[]{ methodBeingChecked.getSimpleName(), methodName, forwardName } );
196                                     }
197                                 }
198                             }
199                         }
200                     }
201                 }
202             }
203         }
204     }
205 }
206
207
Popular Tags