KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > FormBeanChecker


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;
19
20 import org.apache.beehive.netui.compiler.grammar.ValidatablePropertyGrammar;
21 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
22 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeElementDeclaration;
23 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
24 import org.apache.beehive.netui.compiler.typesystem.declaration.ClassDeclaration;
25 import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
26 import org.apache.beehive.netui.compiler.typesystem.declaration.MethodDeclaration;
27 import org.apache.beehive.netui.compiler.typesystem.declaration.Modifier;
28 import org.apache.beehive.netui.compiler.typesystem.env.AnnotationProcessorEnvironment;
29
30 import java.util.Map JavaDoc;
31
32
33 public class FormBeanChecker
34         extends BaseChecker
35         implements JpfLanguageConstants
36 {
37     public FormBeanChecker( AnnotationProcessorEnvironment env, Diagnostics diags )
38     {
39         super( env, null, diags );
40     }
41
42     public Map JavaDoc onCheck( ClassDeclaration jclass )
43             throws FatalCompileTimeException
44     {
45         GetterValidatablePropertyGrammar validatablePropertyGrammar = new GetterValidatablePropertyGrammar();
46         boolean isFormBeanClass = CompilerUtils.getAnnotation( jclass, FORM_BEAN_TAG_NAME ) != null;
47         
48         //
49
// Look for ValidationField annotations on the methods; if there are some present, then we consider this
50
// a form bean class, even if it's not annotated as such.
51
//
52
MethodDeclaration[] methods = CompilerUtils.getClassMethods( jclass, null );
53         
54         for ( int i = 0; i < methods.length; i++ )
55         {
56             MethodDeclaration method = methods[i];
57             isFormBeanClass |=
58                 checkValidationAnnotation( method, VALIDATABLE_PROPERTY_TAG_NAME, validatablePropertyGrammar );
59             // We don't currently support validation rule annotations directly on getter methods.
60
/*
61             hasOne |= checkValidationAnnotation( method, LOCALE_RULES_ATTR, _validationLocaleRulesGrammar );
62             hasOne |= checkValidationAnnotation( method, VALIDATE_REQUIRED_TAG_NAME, _baseValidationRuleGrammar );
63             hasOne |= checkValidationAnnotation( method, VALIDATE_RANGE_TAG_NAME, _validateRangeGrammar );
64             hasOne |= checkValidationAnnotation( method, VALIDATE_MIN_LENGTH_TAG_NAME, _baseValidationRuleGrammar );
65             hasOne |= checkValidationAnnotation( method, VALIDATE_MAX_LENGTH_TAG_NAME, _baseValidationRuleGrammar );
66             hasOne |= checkValidationAnnotation( method, VALIDATE_CREDIT_CARD_TAG_NAME, _baseValidationRuleGrammar );
67             hasOne |= checkValidationAnnotation( method, VALIDATE_EMAIL_TAG_NAME, _baseValidationRuleGrammar );
68             hasOne |= checkValidationAnnotation( method, VALIDATE_MASK_TAG_NAME, _baseValidationRuleGrammar );
69             hasOne |= checkValidationAnnotation( method, VALIDATE_DATE_TAG_NAME, _baseValidationRuleGrammar );
70             hasOne |= checkValidationAnnotation( method, VALIDATE_TYPE_TAG_NAME, _validateTypeGrammar );
71             */

72         }
73         
74         //
75
// Make sure ActionForm subclasses are public static, and that they have default
76
// constructors.
77
//
78
if ( isFormBeanClass || CompilerUtils.isAssignableFrom( STRUTS_FORM_CLASS_NAME, jclass, getEnv() ) )
79         {
80             if ( jclass.getDeclaringType() != null && ! jclass.hasModifier( Modifier.STATIC ) )
81             {
82                 getDiagnostics().addError( jclass, "error.form-not-static" );
83             }
84             
85             if ( ! jclass.hasModifier( Modifier.PUBLIC ) )
86             {
87                 getDiagnostics().addError( jclass, "error.form-not-public" );
88             }
89             
90             if ( ! CompilerUtils.hasDefaultConstructor( jclass ) )
91             {
92                getDiagnostics().addError( jclass, "error.form-no-default-constructor" );
93             }
94         }
95         
96         return null;
97     }
98
99     private boolean checkValidationAnnotation( MethodDeclaration method, String JavaDoc annotationTagName,
100                                                AnnotationGrammar grammar )
101             throws FatalCompileTimeException
102     {
103         AnnotationInstance annotation = CompilerUtils.getAnnotation( method, annotationTagName );
104         
105         if ( annotation != null )
106         {
107             if ( CompilerUtils.getBeanProperty( method ) == null )
108             {
109                 getDiagnostics().addError( annotation, "error.validation-field-on-non-getter" );
110             }
111             
112             grammar.check( annotation, null, method );
113             
114             return true;
115         }
116         
117         return false;
118     }
119         
120     private class GetterValidatablePropertyGrammar
121             extends ValidatablePropertyGrammar
122     {
123         public GetterValidatablePropertyGrammar()
124         {
125             super( FormBeanChecker.this.getEnv(), FormBeanChecker.this.getDiagnostics(),
126                    FormBeanChecker.this.getRuntimeVersionChecker() );
127         }
128         
129         public String JavaDoc[][] getRequiredAttrs()
130         {
131             return null; // This override causes the 'propertyName' attribute *not* to be required
132
}
133         
134         protected void onCheckMember( AnnotationTypeElementDeclaration memberDecl, AnnotationValue member,
135                                       AnnotationInstance annotation, AnnotationInstance[] parentAnnotations,
136                                       MemberDeclaration classMember )
137         {
138             if ( memberDecl.getSimpleName().equals( PROPERTY_NAME_ATTR ) )
139             {
140                 addError( member, "error.validatable-field-property-name-not-allowed", PROPERTY_NAME_ATTR );
141             }
142         }
143     }
144 }
145
Popular Tags