1 package org.apache.beehive.controls.runtime.generator.apt; 2 3 20 21 import java.io.IOException ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Set ; 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 <AnnotationTypeDeclaration> atds, 40 AnnotationProcessorEnvironment env) 41 { 42 super(atds, env); 43 } 44 45 @Override 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 try 63 { 64 AnnotationConstraintAptValidator.validate(decl); 65 } 66 catch (IllegalArgumentException 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 <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 ioe) 95 { 96 throw new CodeGenerationException("Code generation failure: ", ioe); 97 } 98 } 99 } 100 101 @Override 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 <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 ioe) 133 { 134 throw new CodeGenerationException("Code generation failure: ", ioe); 135 } 136 } 137 } 138 139 143 protected CodeGenerator getGenerator() 144 { 145 if (_generator == null) 146 { 147 AnnotationProcessorEnvironment env = getAnnotationProcessorEnvironment(); 151 152 try 153 { 154 _generator = new VelocityGenerator(env); 155 } 156 catch (Exception e) 157 { 158 throw new CodeGenerationException("Unable to create code generator", e); 159 } 160 } 161 return _generator; 162 } 163 164 HashMap <Declaration, Generator> _typeMap = new HashMap <Declaration,Generator>(); 165 CodeGenerator _generator; 166 } 167 | Popular Tags |