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.processor;
020:
021: import org.apache.beehive.netui.compiler.*;
022: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
023: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeDeclaration;
024: import org.apache.beehive.netui.compiler.typesystem.declaration.ClassDeclaration;
025: import org.apache.beehive.netui.compiler.typesystem.declaration.Declaration;
026: import org.apache.beehive.netui.compiler.typesystem.declaration.MethodDeclaration;
027: import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration;
028: import org.apache.beehive.netui.compiler.typesystem.env.CoreAnnotationProcessorEnv;
029:
030: import java.io.File;
031: import java.util.ArrayList;
032: import java.util.Collection;
033: import java.util.HashSet;
034: import java.util.Iterator;
035:
036: public class PageFlowCoreAnnotationProcessor extends
037: BaseCoreAnnotationProcessor implements JpfLanguageConstants {
038: public PageFlowCoreAnnotationProcessor(
039: AnnotationTypeDeclaration[] annotationTypeDecls,
040: CoreAnnotationProcessorEnv env) {
041: super (annotationTypeDecls, env);
042: }
043:
044: /*
045: * Run the checkers on classes including form beans with declarative
046: * validation annotations. This override will find class declarations
047: * for classes that may only have method level declarative validation
048: * annotations.
049: */
050: protected void check(Collection decls)
051: throws FatalCompileTimeException {
052: ArrayList classDecls = new ArrayList();
053: HashSet classNames = new HashSet();
054: for (Iterator i = decls.iterator(); i.hasNext();) {
055: Declaration decl = (Declaration) i.next();
056: if (decl instanceof ClassDeclaration) {
057: ClassDeclaration jclass = (ClassDeclaration) decl;
058: if (!classNames.contains(jclass.getQualifiedName())) {
059: classNames.add(jclass.getQualifiedName());
060: classDecls.add(jclass);
061: }
062: } else if (decl instanceof MethodDeclaration) {
063: //
064: // Declarative validation annotations on a method. Include as a
065: // possible form bean that does not have the @FormBean annotation.
066: //
067: MethodDeclaration methodDecl = (MethodDeclaration) decl;
068: TypeDeclaration type = methodDecl.getDeclaringType();
069: if (type instanceof ClassDeclaration) {
070: ClassDeclaration jclass = (ClassDeclaration) type;
071: if (!classNames.contains(jclass.getQualifiedName())) {
072: classNames.add(jclass.getQualifiedName());
073: classDecls.add(jclass);
074: }
075: }
076: }
077: }
078:
079: super .check(classDecls);
080: }
081:
082: public BaseChecker getChecker(ClassDeclaration classDecl,
083: Diagnostics diagnostics) {
084: CoreAnnotationProcessorEnv env = getAnnotationProcessorEnvironment();
085:
086: if (CompilerUtils.isAssignableFrom(JPF_BASE_CLASS, classDecl,
087: env)) {
088: if (expectAnnotation(classDecl, CONTROLLER_TAG_NAME,
089: JPF_FILE_EXTENSION_DOT, JPF_BASE_CLASS, diagnostics)) {
090: FlowControllerInfo fcInfo = new FlowControllerInfo(
091: classDecl);
092: setSourceFileInfo(classDecl, fcInfo);
093:
094: return new PageFlowChecker(env, diagnostics, fcInfo);
095: }
096: } else if (CompilerUtils.isAssignableFrom(
097: SHARED_FLOW_BASE_CLASS, classDecl, env)) {
098: if (expectAnnotation(classDecl, CONTROLLER_TAG_NAME,
099: SHARED_FLOW_FILE_EXTENSION_DOT,
100: SHARED_FLOW_BASE_CLASS, diagnostics)) {
101: FlowControllerInfo fcInfo = new FlowControllerInfo(
102: classDecl);
103: setSourceFileInfo(classDecl, fcInfo);
104:
105: return new SharedFlowChecker(env, fcInfo, diagnostics);
106: }
107: } else if (CompilerUtils.isAssignableFrom(GLOBALAPP_BASE_CLASS,
108: classDecl, env)) {
109: if (expectAnnotation(classDecl, CONTROLLER_TAG_NAME,
110: GLOBALAPP_FILE_EXTENSION_DOT, GLOBALAPP_BASE_CLASS,
111: diagnostics)) {
112: FlowControllerInfo fcInfo = new FlowControllerInfo(
113: classDecl);
114: setSourceFileInfo(classDecl, fcInfo);
115:
116: return new SharedFlowChecker(env, fcInfo, diagnostics);
117: }
118: } else if (CompilerUtils.isAssignableFrom(
119: FACES_BACKING_BEAN_CLASS, classDecl, env)) {
120: if (expectAnnotation(classDecl, FACES_BACKING_TAG_NAME,
121: JPF_FILE_EXTENSION_DOT, JPF_BASE_CLASS, diagnostics)) {
122: File originalFile = CompilerUtils.getSourceFile(
123: classDecl, true);
124: FacesBackingInfo fbInfo = new FacesBackingInfo(
125: originalFile, classDecl.getQualifiedName());
126: setSourceFileInfo(classDecl, fbInfo);
127: return new FacesBackingChecker(env, fbInfo, diagnostics);
128: }
129: } else {
130: AnnotationInstance ann = CompilerUtils.getAnnotation(
131: classDecl, CONTROLLER_TAG_NAME);
132:
133: if (ann != null) {
134: diagnostics.addError(ann,
135: "error.annotation-invalid-base-class2",
136: CONTROLLER_TAG_NAME, JPF_BASE_CLASS,
137: SHARED_FLOW_BASE_CLASS);
138: }
139:
140: ann = CompilerUtils.getAnnotation(classDecl,
141: FACES_BACKING_TAG_NAME);
142:
143: if (ann != null) {
144: diagnostics.addError(ann,
145: "error.annotation-invalid-base-class",
146: FACES_BACKING_TAG_NAME,
147: FACES_BACKING_BEAN_CLASS);
148: }
149: }
150:
151: // check the class as a form bean for declarative validation annotations
152: return new FormBeanChecker(getAnnotationProcessorEnvironment(),
153: diagnostics);
154: }
155:
156: public BaseGenerator getGenerator(ClassDeclaration classDecl,
157: Diagnostics diags) {
158: CoreAnnotationProcessorEnv env = getAnnotationProcessorEnvironment();
159: SourceFileInfo sourceFileInfo = getSourceFileInfo(classDecl);
160:
161: if (CompilerUtils.isAssignableFrom(JPF_BASE_CLASS, classDecl,
162: env)) {
163: assert sourceFileInfo != null : classDecl
164: .getQualifiedName();
165: assert sourceFileInfo instanceof FlowControllerInfo : sourceFileInfo
166: .getClass().getName();
167: return new PageFlowGenerator(env,
168: (FlowControllerInfo) sourceFileInfo, diags);
169: } else if (CompilerUtils.isAssignableFrom(
170: SHARED_FLOW_BASE_CLASS, classDecl, env)) {
171: assert sourceFileInfo != null : classDecl
172: .getQualifiedName();
173: assert sourceFileInfo instanceof FlowControllerInfo : sourceFileInfo
174: .getClass().getName();
175: return new SharedFlowGenerator(env,
176: (FlowControllerInfo) sourceFileInfo, diags);
177: } else if (CompilerUtils.isAssignableFrom(
178: FACES_BACKING_BEAN_CLASS, classDecl, env)) {
179: assert sourceFileInfo != null : classDecl
180: .getQualifiedName();
181: assert sourceFileInfo instanceof FacesBackingInfo : sourceFileInfo
182: .getClass().getName();
183: return new FacesBackingGenerator(env, sourceFileInfo, diags);
184: }
185:
186: return null;
187: }
188: }
|