KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > processor > BaseAnnotationProcessor


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.processor;
19
20 import org.apache.beehive.netui.compiler.BaseChecker;
21 import org.apache.beehive.netui.compiler.BaseGenerator;
22 import org.apache.beehive.netui.compiler.CompilerUtils;
23 import org.apache.beehive.netui.compiler.Diagnostics;
24 import org.apache.beehive.netui.compiler.JpfLanguageConstants;
25 import org.apache.beehive.netui.compiler.SourceFileInfo;
26 import org.apache.beehive.netui.compiler.FatalCompileTimeException;
27 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeDeclaration;
28 import org.apache.beehive.netui.compiler.typesystem.declaration.ClassDeclaration;
29 import org.apache.beehive.netui.compiler.typesystem.declaration.Declaration;
30 import org.apache.beehive.netui.compiler.typesystem.declaration.Modifier;
31 import org.apache.beehive.netui.compiler.typesystem.env.AnnotationProcessorEnvironment;
32 import org.apache.beehive.netui.compiler.typesystem.type.ClassType;
33
34 import java.text.MessageFormat JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.ResourceBundle JavaDoc;
37
38 public abstract class BaseAnnotationProcessor
39         extends TwoPhaseAnnotationProcessor
40         implements JpfLanguageConstants
41 {
42     private HashMap JavaDoc _sourceFileInfo;
43     private SourceFileInfo _singleSourceFileInfo = null;
44     private ResourceBundle JavaDoc _messages;
45     
46     
47     protected BaseAnnotationProcessor( AnnotationTypeDeclaration[] annotationTypeDecls,
48                                        AnnotationProcessorEnvironment env )
49     {
50         super( annotationTypeDecls, env );
51         _messages = ResourceBundle.getBundle( "org.apache.beehive.netui.compiler.diagnostics" );
52     }
53
54     public void check( Declaration decl )
55             throws FatalCompileTimeException
56     {
57         assert _sourceFileInfo != null; // process() should guarantee this.
58

59         if ( decl instanceof ClassDeclaration )
60         {
61             ClassDeclaration classDecl = ( ClassDeclaration ) decl;
62             BaseChecker checker = getChecker( classDecl, this );
63             
64             if ( checker != null )
65             {
66                 checker.check( classDecl );
67                 
68                 //
69
// Also do a silent check on all base classes. We don't want to generate if there were errors
70
// in the base class.
71
//
72
SilentDiagnostics silentDiagnostics = new SilentDiagnostics();
73                 
74                 for ( ClassType i = classDecl.getSuperclass(); i != null; i = i.getSuperclass() )
75                 {
76                     ClassDeclaration baseDecl = i.getClassTypeDeclaration();
77                     
78                     if ( CompilerUtils.getSourceFile( baseDecl, false ) != null )
79                     {
80                         BaseChecker silentChecker = getChecker( baseDecl, silentDiagnostics );
81                         if ( silentChecker != null ) silentChecker.check( baseDecl );
82                     }
83                 }
84                 
85                 if ( silentDiagnostics.hasErrors() ) setHasErrors( true );
86             }
87         }
88     }
89
90     public void generate( Declaration decl )
91             throws FatalCompileTimeException
92     {
93         assert _sourceFileInfo != null; // process() should guarantee this.
94

95         if ( decl instanceof ClassDeclaration )
96         {
97             ClassDeclaration classDecl = ( ClassDeclaration ) decl;
98             BaseGenerator generator = getGenerator( classDecl, this );
99             if ( generator != null ) generator.generate( classDecl );
100         }
101     }
102
103     public void process()
104     {
105         _sourceFileInfo = new HashMap JavaDoc();
106         super.process();
107         _sourceFileInfo = null;
108     }
109
110     protected abstract BaseChecker getChecker( ClassDeclaration decl, Diagnostics diagnostics );
111     
112     protected abstract BaseGenerator getGenerator( ClassDeclaration decl, Diagnostics diagnostics );
113     
114     protected SourceFileInfo getSourceFileInfo( ClassDeclaration decl )
115     {
116         assert _sourceFileInfo != null || _singleSourceFileInfo != null;
117         assert _sourceFileInfo == null || _singleSourceFileInfo == null;
118         return _singleSourceFileInfo != null ? _singleSourceFileInfo : ( SourceFileInfo ) _sourceFileInfo.get( decl.getQualifiedName() );
119     }
120
121     protected void setSourceFileInfo( ClassDeclaration decl, SourceFileInfo sourceFileInfo )
122     {
123         assert _sourceFileInfo != null || _singleSourceFileInfo == null;
124         
125         if ( _sourceFileInfo != null )
126         {
127             _sourceFileInfo.put( decl.getQualifiedName(), sourceFileInfo );
128         }
129         else
130         {
131             _singleSourceFileInfo = sourceFileInfo;
132         }
133     }
134     
135     protected static boolean expectAnnotation( ClassDeclaration classDecl, String JavaDoc annotationBaseName,
136                                                String JavaDoc fileExtensionRequiresAnnotation, String JavaDoc baseClass,
137                                                Diagnostics diagnostics )
138     {
139         if ( CompilerUtils.getAnnotation( classDecl, annotationBaseName ) != null ) return true;
140         
141         String JavaDoc fileName = classDecl.getPosition().file().getName();
142         
143         if ( fileExtensionRequiresAnnotation != null && fileName.endsWith( fileExtensionRequiresAnnotation ) )
144         {
145             diagnostics.addError( classDecl, "error.annotation-required",
146                                   fileExtensionRequiresAnnotation, ANNOTATIONS_CLASSNAME + '.' + annotationBaseName );
147         }
148         else if ( ! classDecl.hasModifier( Modifier.ABSTRACT ) )
149         {
150             diagnostics.addWarning( classDecl, "warning.missing-annotation", baseClass,
151                                     ANNOTATIONS_CLASSNAME + '.' + annotationBaseName );
152         }
153         
154         return false;
155     }
156     
157     protected String JavaDoc getResourceString( String JavaDoc key, Object JavaDoc[] args )
158     {
159         String JavaDoc message = _messages.getString( key );
160         return MessageFormat.format( message, args );
161     }
162 }
163
Popular Tags