1 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 ; 35 import java.util.HashMap ; 36 import java.util.ResourceBundle ; 37 38 public abstract class BaseAnnotationProcessor 39 extends TwoPhaseAnnotationProcessor 40 implements JpfLanguageConstants 41 { 42 private HashMap _sourceFileInfo; 43 private SourceFileInfo _singleSourceFileInfo = null; 44 private ResourceBundle _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; 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 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; 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 (); 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 annotationBaseName, 136 String fileExtensionRequiresAnnotation, String baseClass, 137 Diagnostics diagnostics ) 138 { 139 if ( CompilerUtils.getAnnotation( classDecl, annotationBaseName ) != null ) return true; 140 141 String 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 getResourceString( String key, Object [] args ) 158 { 159 String message = _messages.getString( key ); 160 return MessageFormat.format( message, args ); 161 } 162 } 163 | Popular Tags |