1 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 ; 37 import java.util.Map ; 38 import java.util.Iterator ; 39 40 41 public class CatchGrammar 42 extends BaseFlowControllerGrammar 43 implements JpfLanguageConstants 44 { 45 private static String [][] MUTUALLY_EXCLUSIVE_ATTRS = { { PATH_ATTR, METHOD_ATTR } }; 46 private static String [][] REQUIRED_ATTRS = { { TYPE_ATTR }, { PATH_ATTR, METHOD_ATTR } }; 47 48 private String _annotationRootName; 49 50 51 public CatchGrammar( AnnotationProcessorEnvironment env, Diagnostics diags, String requiredRuntimeVersion, 52 RuntimeVersionChecker runtimeVersionChecker, String annotationRootName, 53 FlowControllerInfo fcInfo ) 54 { 55 super( env, diags, requiredRuntimeVersion, runtimeVersionChecker, fcInfo ); 56 57 _annotationRootName = annotationRootName; 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 [][] getMutuallyExclusiveAttrs() 69 { 70 return MUTUALLY_EXCLUSIVE_ATTRS; 71 } 72 73 public String [][] getRequiredAttrs() 74 { 75 return REQUIRED_ATTRS; 76 } 77 78 82 protected Object onEndCheck( AnnotationInstance annotation, AnnotationInstance[] parentAnnotations, 83 MemberDeclaration classMember, Map checkResults ) 84 { 85 MethodDeclaration handlerMethod = ( MethodDeclaration ) checkResults.get( METHOD_ATTR ); 86 DeclaredType exceptionType = ( DeclaredType ) checkResults.get( TYPE_ATTR ); 87 88 if ( handlerMethod == null || exceptionType == null ) 92 { 93 return null; 94 } 95 96 ParameterDeclaration[] parameters = handlerMethod.getParameters(); 100 101 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 130 protected void checkMethod( MethodDeclaration methodBeingChecked, AnnotationValue value, 131 AnnotationInstance[] parentAnnotations, MemberDeclaration classMember ) 132 { 133 Collection 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 ii = catches.iterator(); ii.hasNext(); ) 147 { 148 AnnotationInstance catchAnnotation = ( AnnotationInstance ) ii.next(); 149 String 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 Collection otherForwards = 166 CompilerUtils.getAnnotationArrayValue( otherMethod, EXCEPTION_HANDLER_TAG_NAME, 167 FORWARDS_ATTR, false ); 168 169 for ( Iterator i2 = otherForwards.iterator(); i2.hasNext(); ) 170 { 171 AnnotationInstance otherForward = ( AnnotationInstance ) i2.next(); 172 String otherForwardName = CompilerUtils.getString( otherForward, NAME_ATTR, true ); 173 String otherForwardPath = CompilerUtils.getString( otherForward, PATH_ATTR, true ); 174 String otherFwdNavigateTo = 175 CompilerUtils.getEnumFieldName( otherForward, NAVIGATE_TO_ATTR, true ); 176 177 Collection forwards = 178 CompilerUtils.getAnnotationArrayValue( methodBeingChecked, 179 EXCEPTION_HANDLER_TAG_NAME, 180 FORWARDS_ATTR, false ); 181 182 for ( Iterator i3 = forwards.iterator(); i3.hasNext(); ) 183 { 184 AnnotationInstance forward = ( AnnotationInstance ) i3.next(); 185 String forwardName = CompilerUtils.getString( forward, NAME_ATTR, true ); 186 String forwardPath = CompilerUtils.getString( forward, PATH_ATTR, true ); 187 String 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 []{ methodBeingChecked.getSimpleName(), methodName, forwardName } ); 196 } 197 } 198 } 199 } 200 } 201 } 202 } 203 } 204 } 205 } 206 207 | Popular Tags |