01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: * $Header:$
18: */
19: package org.apache.beehive.netui.compiler.grammar;
20:
21: import org.apache.beehive.netui.compiler.AnnotationMemberType;
22: import org.apache.beehive.netui.compiler.Diagnostics;
23: import org.apache.beehive.netui.compiler.RuntimeVersionChecker;
24: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
25: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeElementDeclaration;
26: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
27: import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
28: import org.apache.beehive.netui.compiler.typesystem.declaration.MethodDeclaration;
29: import org.apache.beehive.netui.compiler.typesystem.env.CoreAnnotationProcessorEnv;
30: import org.apache.beehive.netui.compiler.typesystem.type.PrimitiveType;
31: import org.apache.beehive.netui.compiler.typesystem.type.TypeInstance;
32:
33: public class ValidateTypeGrammar extends BaseValidationRuleGrammar {
34: private static final String[][] REQUIRED_ATTRS = { { TYPE_ATTR } };
35:
36: public ValidateTypeGrammar(CoreAnnotationProcessorEnv env,
37: Diagnostics diags,
38: RuntimeVersionChecker runtimeVersionChecker) {
39: super (env, diags, runtimeVersionChecker);
40:
41: addMemberType(TYPE_ATTR, new PrimitiveTypeType());
42: }
43:
44: public String[][] getRequiredAttrs() {
45: return REQUIRED_ATTRS;
46: }
47:
48: private class PrimitiveTypeType extends AnnotationMemberType {
49: public PrimitiveTypeType() {
50: super (ValidateTypeGrammar.this .getRequiredRuntimeVersion(),
51: ValidateTypeGrammar.this );
52: }
53:
54: public Object onCheck(
55: AnnotationTypeElementDeclaration valueDecl,
56: AnnotationValue value,
57: AnnotationInstance[] parentAnnotations,
58: MemberDeclaration classMember, int annotationArrayIndex) {
59: Object val = value.getValue();
60:
61: //
62: // Make sure the type is in the list of valid types.
63: //
64: if (!(val instanceof PrimitiveType)) {
65: addError(value, "error.must-be-primitive-type");
66: } else {
67: PrimitiveType.Kind kind = ((PrimitiveType) val)
68: .getKind();
69:
70: if (kind.equals(PrimitiveType.Kind.BOOLEAN)) {
71: addError(value, "error.invalid-type");
72: } else if (classMember instanceof MethodDeclaration) {
73: //
74: // Add a warning if this annotation is on a property getter of the same type, in which case the
75: // validation rule will never fail.
76: //
77: TypeInstance returnType = ((MethodDeclaration) classMember)
78: .getReturnType();
79:
80: if (returnType instanceof PrimitiveType) {
81: if (((PrimitiveType) returnType).getKind()
82: .equals(kind)) {
83: addWarning(
84: value,
85: "warning.validate-type-on-same-type",
86: ANNOTATION_INTERFACE_PREFIX
87: + VALIDATE_TYPE_TAG_NAME,
88: kind.toString().toLowerCase());
89: }
90: }
91: }
92: }
93:
94: return null;
95: }
96: }
97: }
|