KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > generator > apt > ControlAnnotationProcessor


1 package org.apache.beehive.controls.runtime.generator.apt;
2
3 /*
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * $Header:$
19  */

20
21 import java.io.IOException JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import com.sun.mirror.apt.AnnotationProcessorEnvironment;
27 import com.sun.mirror.declaration.AnnotationTypeDeclaration;
28 import com.sun.mirror.declaration.Declaration;
29
30 import org.apache.beehive.controls.api.bean.ControlExtension;
31 import org.apache.beehive.controls.api.bean.ControlImplementation;
32 import org.apache.beehive.controls.api.bean.ControlInterface;
33 import org.apache.beehive.controls.api.properties.PropertySet;
34
35 import org.apache.beehive.controls.runtime.generator.*;
36
37 public class ControlAnnotationProcessor extends TwoPhaseAnnotationProcessor
38 {
39     public ControlAnnotationProcessor(Set JavaDoc<AnnotationTypeDeclaration> atds,
40                                       AnnotationProcessorEnvironment env)
41     {
42         super(atds, env);
43     }
44
45     @Override JavaDoc
46     public void check(Declaration decl)
47     {
48         AnnotationProcessorEnvironment env = getAnnotationProcessorEnvironment();
49         Generator genClass = null;
50         if (decl.getAnnotation(ControlInterface.class) != null)
51         {
52             genClass = new AptControlInterface(decl, this);
53         }
54         else if (decl.getAnnotation(ControlExtension.class) != null)
55         {
56             genClass = new AptControlInterface(decl, this);
57
58             // When a control extension is declared, values may be assigned to
59
// the properties of the parent controls. The property constraint
60
// validator is called here to ensure all values assigned satisfy any
61
// constraints declared in the properties.
62
try
63             {
64                 AnnotationConstraintAptValidator.validate(decl);
65             }
66             catch (IllegalArgumentException JavaDoc iae)
67             {
68                 printError(decl, "propertyset.illegal.argument.error", iae.getMessage());
69             }
70             
71         }
72         else if (decl.getAnnotation(ControlImplementation.class) != null)
73         {
74             genClass = new AptControlImplementation(decl, this);
75         }
76         else if (decl.getAnnotation(PropertySet.class) != null)
77         {
78             new AptPropertySet(null, decl, this);
79         }
80
81         if ( genClass != null && !hasErrors() )
82         {
83             try
84             {
85                 List JavaDoc<GeneratorOutput> genList = genClass.getCheckOutput(env.getFiler());
86                 if (genList == null || genList.size() == 0)
87                     return;
88
89                 for (GeneratorOutput genOut : genList)
90                 {
91                     getGenerator().generate(genOut);
92                 }
93             }
94             catch (IOException JavaDoc ioe)
95             {
96                 throw new CodeGenerationException("Code generation failure: ", ioe);
97             }
98         }
99     }
100
101     @Override JavaDoc
102     public void generate(Declaration decl)
103     {
104         AnnotationProcessorEnvironment env = getAnnotationProcessorEnvironment();
105         Generator genClass = null;
106         if (decl.getAnnotation(ControlInterface.class) != null)
107         {
108             genClass = new AptControlInterface(decl, this);
109         }
110         if (decl.getAnnotation(ControlExtension.class) != null)
111         {
112             genClass = new AptControlInterface(decl, this);
113         }
114         else if (decl.getAnnotation(ControlImplementation.class) != null)
115         {
116             genClass = new AptControlImplementation(decl, this);
117         }
118         
119         if ( genClass != null )
120         {
121             try
122             {
123                 List JavaDoc<GeneratorOutput> genList = genClass.getGenerateOutput(env.getFiler());
124                 if (genList == null || genList.size() == 0)
125                     return;
126
127                 for (GeneratorOutput genOut : genList)
128                 {
129                     getGenerator().generate(genOut);
130                 }
131             }
132             catch (IOException JavaDoc ioe)
133             {
134                 throw new CodeGenerationException("Code generation failure: ", ioe);
135             }
136         }
137     }
138
139     /**
140      * Returns the CodeGenerator instance supporting this processor, instantiating a new
141      * generator instance if necessary.
142      */

143     protected CodeGenerator getGenerator()
144     {
145         if (_generator == null)
146         {
147             //
148
// Locate the class that wraps the Velocity code generation process
149
//
150
AnnotationProcessorEnvironment env = getAnnotationProcessorEnvironment();
151
152             try
153             {
154                 _generator = new VelocityGenerator(env);
155             }
156             catch (Exception JavaDoc e)
157             {
158                 throw new CodeGenerationException("Unable to create code generator", e);
159             }
160         }
161         return _generator;
162     }
163
164     HashMap JavaDoc<Declaration, Generator> _typeMap = new HashMap JavaDoc<Declaration,Generator>();
165     CodeGenerator _generator;
166 }
167
Popular Tags