Index: maven-commandline-plugin/src/java/org/codelutin/util/generator/AbstractGenerator.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/util/generator/AbstractGenerator.java:1.1 --- /dev/null Fri Dec 14 18:52:05 2007 +++ maven-commandline-plugin/src/java/org/codelutin/util/generator/AbstractGenerator.java Fri Dec 14 18:52:00 2007 @@ -0,0 +1,143 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, +* Benjamin Poussin, Tony Chemit +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package org.codelutin.util.generator; + +import javassist.CannotCompileException; +import javassist.ClassPath; +import javassist.ClassPool; +import javassist.CtClass; +import javassist.CtField; +import javassist.CtMethod; +import javassist.NotFoundException; +import javassist.bytecode.ClassFile; + +import java.io.IOException; + +/** + * Cette classe sert de base pour générer des classes avec Javassist. + *

+ * La classe ne contient que des propriétés de configuration {@link #out},{@link #backupClass}, + * les différents objets nécessaires à la génération ne sont pas stockés mais juste + * transmis dans les appels de méthodes. + * + * @author tony + */ +public class AbstractGenerator { + + /** chemin du classpath où générer les classes compilées */ + protected String out; + /** flag pour sauver des classes existantes avant regeneration */ + protected boolean backupClass; + /** timestamp pour la sauvegarde des classes */ + protected long timestamp; + + public AbstractGenerator(String out, long timestamp) { + this.out = out; + this.backupClass = timestamp > 0; + this.timestamp = timestamp; + } + + public String getOut() { + return out; + } + + public boolean isBackupClass() { + return backupClass; + } + + public long getTimestamp() { + return timestamp; + } + + public void setOut(String out) { + this.out = out; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + this.backupClass=timestamp>0; + } + + protected String buildGetterMethodName(boolean booleanValueType, String methodName) { + methodName = (booleanValueType ? "is" : "get") + methodName; + return methodName; + } + + protected void addMethod(CtClass impl, String prefix, String methodName, String body) throws CannotCompileException { + CtMethod method; + String b = prefix + " " + methodName + "() {" + body + "}"; + method = CtMethod.make(b, impl); + impl.addMethod(method); + } + + protected void addField(CtClass clazz, String src) throws CannotCompileException { + CtField fld = CtField.make(src, clazz); + clazz.addField(fld); + } + + protected void writeClass(CtClass clazz) throws CannotCompileException, IOException { + writeClass(clazz, clazz.getClassFile()); + } + + protected void writeClass(CtClass clazz, ClassFile cf) throws CannotCompileException, IOException { + // mark to generate java5 code (for annotations) + cf.setVersionToJava5(); + // save class in out classpath + clazz.writeFile(out); + } + + protected CtClass reloadClass(ClassPool pool, CtClass clazz) throws NotFoundException { + detachClazz(clazz); + return pool.get(clazz.getName()); + } + + protected void detachClazz(CtClass... impl) { + for (CtClass ctClass : impl) { + if (ctClass != null) { + ctClass.detach(); + } + } + } + + protected void backupPreviousGeneratedClass(ClassPool pool, String... classNames) throws NotFoundException, CannotCompileException, IOException { + if (!backupClass) { + return; + } + ClassPath cp = pool.insertClassPath(out); + String SUFFIX = "_" + timestamp; + for (String name : classNames) { + try { + CtClass clazz = pool.get(name); + if (clazz != null) { + // rename this clazz to be safe + String newClassName = name + SUFFIX; + clazz.replaceClassName(name, newClassName); + //log.info(_("commandline.backupClass", name, newClassName)); + clazz.writeFile(out); + } + } catch (NotFoundException e) { + // we are safe : class does not exists in class path, + // so no collision is possible :) + } + } + cp.close(); + pool.removeClassPath(cp); + } +}