001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.compiler.grammar;
020:
021: import org.apache.beehive.netui.compiler.AnnotationMemberType;
022: import org.apache.beehive.netui.compiler.CompilerUtils;
023: import org.apache.beehive.netui.compiler.AnnotationGrammar;
024: import org.apache.beehive.netui.compiler.FatalCompileTimeException;
025: import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
026: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
027: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
028: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeElementDeclaration;
029:
030: import java.util.Map;
031: import java.util.HashMap;
032:
033: /**
034: * Supports deprecated values and values that require particular runtime versions.
035: */
036: public class EnumType extends AnnotationMemberType {
037: /** map of enum-val (String) -> required-runtime-version (String, may be null) */
038: private Map _enumRequiredRuntimeVersions = null;
039:
040: /** Map of deprecated-value (String) -> error message key (String) **/
041: private Map _deprecatedValues;
042:
043: public EnumType(String[][] enumValues, String[][] deprecatedValues,
044: String requiredRuntimeVersion,
045: AnnotationGrammar parentGrammar) {
046: super (requiredRuntimeVersion, parentGrammar);
047:
048: if (deprecatedValues != null) {
049: _deprecatedValues = new HashMap();
050:
051: for (int i = 0; i < deprecatedValues.length; ++i) {
052: String[] valueAndDiagnostic = deprecatedValues[i];
053: assert valueAndDiagnostic.length == 2;
054: _deprecatedValues.put(valueAndDiagnostic[0],
055: valueAndDiagnostic[1]);
056: }
057: }
058:
059: if (enumValues != null) {
060: _enumRequiredRuntimeVersions = new HashMap();
061:
062: for (int i = 0; i < enumValues.length; i++) {
063: String[] valueAndRequiredRuntimeVersion = enumValues[i];
064: assert valueAndRequiredRuntimeVersion.length == 2;
065: String enumValue = valueAndRequiredRuntimeVersion[0];
066: String enumValRequiredRuntimeVersion = valueAndRequiredRuntimeVersion[1];
067:
068: if (enumValRequiredRuntimeVersion != null) {
069: _enumRequiredRuntimeVersions.put(enumValue,
070: enumValRequiredRuntimeVersion);
071: }
072: }
073: }
074: }
075:
076: public Object onCheck(AnnotationTypeElementDeclaration valueDecl,
077: AnnotationValue member,
078: AnnotationInstance[] parentAnnotations,
079: MemberDeclaration classMember, int annotationArrayIndex)
080: throws FatalCompileTimeException {
081: //
082: // Check deprecated values.
083: //
084: String val = CompilerUtils.getEnumFieldName(member);
085: String errorKey = _deprecatedValues != null ? (String) _deprecatedValues
086: .get(val)
087: : null;
088:
089: if (errorKey != null) {
090: addWarning(member, errorKey, val);
091: }
092:
093: //
094: // Check required runtime version for enum values.
095: //
096: String ver = _enumRequiredRuntimeVersions != null ? (String) _enumRequiredRuntimeVersions
097: .get(val)
098: : null;
099:
100: if (ver != null) {
101: getParentGrammar().getRuntimeVersionChecker()
102: .checkRuntimeVersion(ver, member,
103: getParentGrammar().getDiagnostics(),
104: "error.required-runtime-version-enumval",
105: new Object[] { val, PAGEFLOW_RUNTIME_JAR });
106: }
107:
108: return super.onCheck(valueDecl, member, parentAnnotations,
109: classMember, annotationArrayIndex);
110: }
111:
112: }
|