1 29 30 package com.caucho.amber.gen; 31 32 import com.caucho.amber.entity.Embeddable; 33 import com.caucho.amber.entity.Entity; 34 import com.caucho.amber.entity.Listener; 35 import com.caucho.amber.manager.AmberPersistenceUnit; 36 import com.caucho.amber.type.AbstractEnhancedType; 37 import com.caucho.amber.type.EmbeddableType; 38 import com.caucho.amber.type.EntityType; 39 import com.caucho.amber.type.ListenerType; 40 import com.caucho.java.JavaCompiler; 41 import com.caucho.java.gen.DependencyComponent; 42 import com.caucho.java.gen.GenClass; 43 import com.caucho.java.gen.JavaClassGenerator; 44 import com.caucho.log.Log; 45 import com.caucho.util.L10N; 46 47 import java.util.ArrayList ; 48 import java.util.logging.Logger ; 49 50 53 public class AmberGeneratorImpl implements AmberGenerator { 54 private static final L10N L = new L10N(AmberGeneratorImpl.class); 55 private static final Logger log = Log.open(AmberGeneratorImpl.class); 56 57 private AmberPersistenceUnit _amberPersistenceUnit; 58 59 private ArrayList <String > _pendingClassNames = 60 new ArrayList <String >(); 61 62 public AmberGeneratorImpl(AmberPersistenceUnit manager) 63 { 64 _amberPersistenceUnit = manager; 65 } 66 67 70 public void configure(AbstractEnhancedType type) 71 throws Exception 72 { 73 } 74 75 78 public void generate(AbstractEnhancedType type) 79 throws Exception 80 { 81 generateJava(null, type); 82 } 83 84 87 public void generateJava(JavaClassGenerator javaGen, 88 AbstractEnhancedType type) 89 throws Exception 90 { 91 if (isPreload(javaGen, type) || type.isGenerated()) 92 return; 93 94 type.setGenerated(true); 95 97 GenClass genClass = new GenClass(type.getInstanceClassName()); 98 99 genClass.setSuperClassName(type.getBeanClass().getName()); 100 101 if (type instanceof EntityType) { 102 genClass.addInterfaceName("com.caucho.amber.entity.Entity"); 103 104 EntityComponent entity = new EntityComponent(); 105 106 entity.setEntityType((EntityType) type); 107 entity.setBaseClassName(type.getBeanClass().getName()); 108 entity.setExtClassName(type.getInstanceClassName()); 109 110 genClass.addComponent(entity); 111 112 DependencyComponent depend = genClass.addDependencyComponent(); 113 depend.addDependencyList(entity.getDependencies()); 114 } 115 else if (type instanceof EmbeddableType) { 116 genClass.addInterfaceName("com.caucho.amber.entity.Embeddable"); 117 118 EmbeddableComponent embeddable = new EmbeddableComponent(); 119 120 embeddable.setEmbeddableType((EmbeddableType) type); 121 embeddable.setBaseClassName(type.getBeanClass().getName()); 122 embeddable.setExtClassName(type.getInstanceClassName()); 123 124 genClass.addComponent(embeddable); 125 } 126 else { 127 genClass.addInterfaceName("com.caucho.amber.entity.Listener"); 128 129 ListenerComponent listener = new ListenerComponent(); 130 131 listener.setListenerType((ListenerType) type); 132 listener.setBaseClassName(type.getBeanClass().getName()); 133 listener.setExtClassName(type.getInstanceClassName()); 134 135 genClass.addComponent(listener); 136 } 137 138 javaGen.generate(genClass); 139 } 140 141 144 public boolean isPreload(JavaClassGenerator javaGen, 145 AbstractEnhancedType type) 146 throws Exception 147 { 148 Class cl; 149 150 if (type.isEnhanced()) 151 cl = javaGen.loadClass(type.getBeanClass().getName()); 152 else 153 cl = javaGen.preload(type.getInstanceClassName()); 154 155 Class expectedClass = Listener.class; 156 157 if (type instanceof EntityType) 158 expectedClass = Entity.class; 159 else if (type instanceof EmbeddableType) 160 expectedClass = Embeddable.class; 161 162 return cl != null && expectedClass.isAssignableFrom(cl); 163 } 164 165 168 public void compile() 169 throws Exception 170 { 171 if (_pendingClassNames.size() == 0) 172 return; 173 174 String []javaFiles = new String [_pendingClassNames.size()]; 175 176 for (int i = 0; i < _pendingClassNames.size(); i++) { 177 String name = _pendingClassNames.get(i); 178 name = name.replace('.', '/') + ".java"; 179 180 javaFiles[i] = name; 181 } 182 _pendingClassNames.clear(); 183 184 EntityGenerator gen = new EntityGenerator(); 185 JavaCompiler compiler = gen.getCompiler(); 186 187 compiler.compileBatch(javaFiles); 188 } 189 } 190 | Popular Tags |