[Jiemamy-notify] commit [1913] ChangeListenerの自動生成を実装

Zurück zum Archiv-Index

svnno****@sourc***** svnno****@sourc*****
2008年 9月 12日 (金) 02:43:24 JST


Revision: 1913
          http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=jiemamy&view=rev&rev=1913
Author:   shin1
Date:     2008-09-12 02:43:24 +0900 (Fri, 12 Sep 2008)

Log Message:
-----------
ChangeListenerの自動生成を実装

Modified Paths:
--------------
    sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/main/java/org/jiemamy/core/eventcodegen/JiemamyModelDoclet.java
    sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/main/resources/ChangeListener.vm


-------------- next part --------------
Modified: sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/main/java/org/jiemamy/core/eventcodegen/JiemamyModelDoclet.java
===================================================================
--- sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/main/java/org/jiemamy/core/eventcodegen/JiemamyModelDoclet.java	2008-09-11 08:51:14 UTC (rev 1912)
+++ sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/main/java/org/jiemamy/core/eventcodegen/JiemamyModelDoclet.java	2008-09-11 17:43:24 UTC (rev 1913)
@@ -1,10 +1,20 @@
 package org.jiemamy.core.eventcodegen;
 
+import java.io.File;
+import java.io.FileWriter;
+import java.io.Writer;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
 
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
+import org.apache.velocity.exception.ParseErrorException;
+import org.apache.velocity.exception.ResourceNotFoundException;
 import org.jiemamy.core.model.JiemamyModel;
 
 import com.sun.javadoc.ClassDoc;
@@ -21,6 +31,7 @@
 public class JiemamyModelDoclet extends Doclet {
 	public List<JiemamyModel> model;
 	public Map<Class<?>, CollectionProperty> collectionProperties = new HashMap<Class<?>, CollectionProperty>();
+	public static final String OUTPUTDIR = "target/";
 
 	public static LanguageVersion languageVersion() {
 		return LanguageVersion.JAVA_1_5;
@@ -33,14 +44,34 @@
 	 * @return
 	 */
 	public static boolean start(RootDoc root) {
-		ClassDoc[] classes = root.classes();
-		for (int i = 0; i < classes.length; ++i) {
-			processClassDoc(classes[i]);
+		try {
+			setupVelocity();
+			ClassDoc[] classes = root.classes();
+			for (int i = 0; i < classes.length; ++i) {
+				processClassDoc(classes[i]);
+			}
+		} catch (Exception ex) {
+			ex.printStackTrace();
 		}
 		return true;
 	}
 
 	/**
+	 * Velocityの初期化を行う。
+	 * 
+	 * @throws Exception
+	 */
+	public static void setupVelocity() throws Exception {
+		Properties p = new Properties();
+		p.setProperty("resource.loader", "class");
+		p
+				.setProperty("class.resource.loader.class",
+						"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
+		p.setProperty("input.encoding", "UTF-8");
+		Velocity.init(p);
+	}
+
+	/**
 	 * 読み込んだ{@link ClassDoc}を処理する。
 	 * 
 	 * @param classDoc
@@ -52,8 +83,7 @@
 				// Instance化出来ないものはInstantialtionExceptionを発生させて弾く。
 				clazz.newInstance();
 				// JiemamyModelのサブクラスでないものはClassCastExceptionを発生させて弾く。
-				Class<? extends JiemamyModel> modelClass = clazz
-						.asSubclass(JiemamyModel.class);
+				clazz.asSubclass(JiemamyModel.class);
 				processModel(classDoc);
 			} catch (Exception ex) {
 				// 無視する。
@@ -71,34 +101,81 @@
 	 * </p>
 	 * 
 	 * @param classDoc
-	 * @throws ClassNotFoundException
+	 * @throws Exception
+	 * @throws ParseErrorException
+	 * @throws ResourceNotFoundException
 	 */
 	private static void processModel(ClassDoc classDoc)
-			throws ClassNotFoundException {
-		System.out.println(classDoc.qualifiedName());
+			throws ResourceNotFoundException, ParseErrorException, Exception {
+		System.out.println(classDoc.qualifiedTypeName() + ":"
+				+ getPackageName(classDoc));
+
+		List<CollectionProperty> properties = new ArrayList<CollectionProperty>();
 		FieldDoc[] fields = classDoc.fields();
 		for (FieldDoc fieldDoc : fields) {
-			List<CollectionProperty> properties = new ArrayList<CollectionProperty>();
-			// System.out.println(" #" + fieldDoc.name() + ":"
-			// + fieldDoc.type().qualifiedTypeName());
-			if (fieldDoc.type().qualifiedTypeName().equals("java.util.List")
-					|| fieldDoc
-							.type()
-							.qualifiedTypeName()
-							.equals(
-									"org.jiemamy.core.utils.collectionimpl.ObservableList")) {
-				System.out.println("  #" + fieldDoc.name() + ":"
-						+ fieldDoc.type().qualifiedTypeName() + ":: "
-						+ getParameterClassName(fieldDoc));
+			String fieldTypeName = fieldDoc.type().qualifiedTypeName();
+			Class<?> fieldType = Class.forName(fieldTypeName);
+			try {
+				// ListのサブクラスでないものはClassCastExceptionを発生させて弾く。
+				fieldType.asSubclass(List.class);
 				properties.add(new CollectionProperty(fieldDoc.name(),
 						Class.forName(fieldDoc.type().qualifiedTypeName()
 								.toString()), Class
 								.forName(getParameterClassName(fieldDoc))));
+				continue;
+			} catch (Exception ex) {
+				// 
 			}
+			try {
+				// SetのサブクラスでないものはClassCastExceptionを発生させて弾く。
+				fieldType.asSubclass(Set.class);
+				properties.add(new CollectionProperty(fieldDoc.name(),
+						Class.forName(fieldDoc.type().qualifiedTypeName()
+								.toString()), Class
+								.forName(getParameterClassName(fieldDoc))));
+				continue;
+			} catch (Exception ex) {
+				// 
+			}
 		}
+		// Templateからjavaファイルを生成する。
+		try {
+			String packageName = getPackageName(classDoc).replaceAll(
+					"org.jiemamy.core", "org.jiemamy.core.event");
+			File dir = new File(OUTPUTDIR + packageName.replaceAll("\\.", "/"));
+			if (!dir.exists()) {
+				dir.mkdirs();
+			}
+			File file = new File(dir.getAbsolutePath() + "/"
+					+ classDoc.name().toString() + "ChangeListener.java");
+			System.out.println(file.getAbsolutePath());
+			VelocityContext velocityContext = new VelocityContext();
+			Template listenerTemplate = Velocity
+					.getTemplate("ChangeListener.vm");
+			Writer writer = new FileWriter(file);
+			velocityContext.put("package", packageName);
+			velocityContext.put("modelClassName", classDoc.name().toString());
+			velocityContext.put("properties", properties);
+			listenerTemplate.merge(velocityContext, writer);
+			writer.flush();
+			writer.close();
+		} catch (Exception ex) {
+			ex.printStackTrace();
+		}
 	}
 
 	/**
+	 * パッケージ名を取得する。
+	 * 
+	 * @param classDoc
+	 * @return
+	 */
+	private static String getPackageName(ClassDoc classDoc) {
+		return classDoc.qualifiedTypeName().replace(
+				"." + classDoc.simpleTypeName(), "");
+	}
+
+	/**
 	 * {@link List}フィールドのGeneric情報を返す。
 	 * 
 	 * @param fieldDoc
@@ -109,7 +186,6 @@
 				.toString();
 		int index1 = parameterizedType.indexOf('<');
 		int index2 = parameterizedType.lastIndexOf('>');
-		return parameterizedType.substring(index1 + 1, index2 + 1).replaceAll(
-				"<?>", "");
+		return parameterizedType.substring(index1 + 1, index2);
 	}
 }

Modified: sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/main/resources/ChangeListener.vm
===================================================================
--- sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/main/resources/ChangeListener.vm	2008-09-11 08:51:14 UTC (rev 1912)
+++ sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/main/resources/ChangeListener.vm	2008-09-11 17:43:24 UTC (rev 1913)
@@ -28,6 +28,7 @@
 
 /**
  * ${modelClassName}用のListener
+ * 
  * @author shin1ogawa
  */
 public interface ${modelClassName}ChangeListener extends ModelChangeListener {


Jiemamy-notify メーリングリストの案内
Zurück zum Archiv-Index