KickJava   Java API By Example, From Geeks To Geeks.

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


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.AnnotationGrammar;
23 import org.apache.beehive.netui.compiler.FatalCompileTimeException;
24 import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
25 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
26 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
27 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeElementDeclaration;
28
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31
32
33 /**
34  * Supports deprecated values and values that require particular runtime versions.
35  */

36 public class EnumType
37         extends AnnotationMemberType
38 {
39     /** map of enum-val (String) -> required-runtime-version (String, may be null) */
40     private Map JavaDoc _enumRequiredRuntimeVersions = null;
41     
42     /** Map of deprecated-value (String) -> error message key (String) **/
43     private Map JavaDoc _deprecatedValues;
44     
45
46     public EnumType( String JavaDoc[][] enumValues, String JavaDoc[][] deprecatedValues, String JavaDoc requiredRuntimeVersion,
47                      AnnotationGrammar parentGrammar )
48     {
49         super( requiredRuntimeVersion, parentGrammar );
50         
51         if ( deprecatedValues != null )
52         {
53             _deprecatedValues = new HashMap JavaDoc();
54             
55             for ( int i = 0; i < deprecatedValues.length; ++i )
56             {
57                 String JavaDoc[] valueAndDiagnostic = deprecatedValues[i];
58                 assert valueAndDiagnostic.length == 2;
59                 _deprecatedValues.put( valueAndDiagnostic[0], valueAndDiagnostic[1] );
60             }
61         }
62         
63         if ( enumValues != null )
64         {
65             _enumRequiredRuntimeVersions = new HashMap JavaDoc();
66             
67             for ( int i = 0; i < enumValues.length; i++ )
68             {
69                 String JavaDoc[] valueAndRequiredRuntimeVersion = enumValues[i];
70                 assert valueAndRequiredRuntimeVersion.length == 2;
71                 String JavaDoc enumValue = valueAndRequiredRuntimeVersion[0];
72                 String JavaDoc enumValRequiredRuntimeVersion = valueAndRequiredRuntimeVersion[1];
73                 
74                 if ( enumValRequiredRuntimeVersion != null )
75                 {
76                     _enumRequiredRuntimeVersions.put( enumValue, enumValRequiredRuntimeVersion );
77                 }
78             }
79         }
80     }
81     
82     
83     public Object JavaDoc onCheck( AnnotationTypeElementDeclaration valueDecl, AnnotationValue member,
84                            AnnotationInstance[] parentAnnotations, MemberDeclaration classMember,
85                            int annotationArrayIndex )
86             throws FatalCompileTimeException
87     {
88         //
89
// Check deprecated values.
90
//
91
String JavaDoc val = CompilerUtils.getEnumFieldName( member );
92         String JavaDoc errorKey = _deprecatedValues != null ? ( String JavaDoc ) _deprecatedValues.get( val ) : null;
93         
94         if ( errorKey != null )
95         {
96             addWarning( member, errorKey, val );
97         }
98         
99         //
100
// Check required runtime version for enum values.
101
//
102
String JavaDoc ver = _enumRequiredRuntimeVersions != null ? ( String JavaDoc ) _enumRequiredRuntimeVersions.get( val ) : null;
103         
104         if ( ver != null )
105         {
106             getParentGrammar().getRuntimeVersionChecker().checkRuntimeVersion(
107                     ver, member, getParentGrammar().getDiagnostics(), "error.required-runtime-version-enumval",
108                     new Object JavaDoc[]{ val, PAGEFLOW_RUNTIME_JAR } );
109         }
110         
111         return super.onCheck( valueDecl, member, parentAnnotations, classMember, annotationArrayIndex );
112     }
113     
114 }
115
Popular Tags