KickJava   Java API By Example, From Geeks To Geeks.

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


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.Diagnostics;
22 import org.apache.beehive.netui.compiler.RuntimeVersionChecker;
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.env.AnnotationProcessorEnvironment;
29 import org.apache.beehive.netui.compiler.typesystem.type.PrimitiveType;
30 import org.apache.beehive.netui.compiler.typesystem.type.TypeInstance;
31
32
33 public class ValidateTypeGrammar
34         extends BaseValidationRuleGrammar
35 {
36     public ValidateTypeGrammar( AnnotationProcessorEnvironment env, Diagnostics diags,
37                          RuntimeVersionChecker runtimeVersionChecker )
38     {
39         super( env, diags, runtimeVersionChecker );
40         
41         addMemberType( TYPE_ATTR, new PrimitiveTypeType() );
42     }
43
44     private class PrimitiveTypeType
45             extends AnnotationMemberType
46     {
47         public PrimitiveTypeType()
48         {
49             super( ValidateTypeGrammar.this.getRequiredRuntimeVersion(), ValidateTypeGrammar.this );
50         }
51
52         
53         public Object JavaDoc onCheck( AnnotationTypeElementDeclaration valueDecl, AnnotationValue value,
54                                AnnotationInstance[] parentAnnotations, MemberDeclaration classMember,
55                                int annotationArrayIndex )
56         {
57             Object JavaDoc val = value.getValue();
58             
59             //
60
// Make sure the type is in the list of valid types.
61
//
62
if ( ! ( val instanceof PrimitiveType ) )
63             {
64                 addError( value, "error.must-be-primitive-type" );
65             }
66             else
67             {
68                 PrimitiveType.Kind kind = ( ( PrimitiveType ) val ).getKind();
69                 
70                 if ( kind.equals( PrimitiveType.Kind.BOOLEAN ) )
71                 {
72                     addError( value, "error.invalid-type" );
73                 }
74                 else if ( classMember instanceof MethodDeclaration )
75                 {
76                     //
77
// Add a warning if this annotation is on a property getter of the same type, in which case the
78
// validation rule will never fail.
79
//
80
TypeInstance returnType = ( ( MethodDeclaration ) classMember ).getReturnType();
81                     
82                     if ( returnType instanceof PrimitiveType )
83                     {
84                         if ( ( ( PrimitiveType ) returnType ).getKind().equals( kind ) )
85                         {
86                             addWarning( value, "warning.validate-type-on-same-type",
87                                         ANNOTATION_INTERFACE_PREFIX + VALIDATE_TYPE_TAG_NAME,
88                                         kind.toString().toLowerCase() );
89                         }
90                     }
91                 }
92             }
93             
94             return null;
95         }
96     }
97 }
98
Popular Tags