[Jiemamy-notify] commit [1910] Generics情報をDocletで拾える事を確認してみた。

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2008年 9月 11日 (木) 02:08:38 JST


Revision: 1910
          http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=jiemamy&view=rev&rev=1910
Author:   shin1
Date:     2008-09-11 02:08:38 +0900 (Thu, 11 Sep 2008)

Log Message:
-----------
Generics情報をDocletで拾える事を確認してみた。

Modified Paths:
--------------
    sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/test/java/org/jiemamy/core/eventcodegen/ListenerTemplateTest.java

Added Paths:
-----------
    sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/JiemamyDocletMain.launch
    sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/main/java/org/jiemamy/core/eventcodegen/JiemamyModelDoclet.java


-------------- next part --------------
Added: sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/JiemamyDocletMain.launch
===================================================================
--- sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/JiemamyDocletMain.launch	                        (rev 0)
+++ sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/JiemamyDocletMain.launch	2008-09-10 17:08:38 UTC (rev 1910)
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
+<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
+<listEntry value="/org.jiemamy.core.eventcodegen"/>
+</listAttribute>
+<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
+<listEntry value="4"/>
+</listAttribute>
+<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
+<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.sun.tools.javadoc.Main"/>
+<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-doclet org.jiemamy.core.eventcodegen.JiemamyModelDoclet -sourcepath ../org.jiemamy.core/src/main/java -subpackages org.jiemamy.core.model -private"/>
+<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.jiemamy.core.eventcodegen"/>
+</launchConfiguration>


Property changes on: sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/JiemamyDocletMain.launch
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: 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	                        (rev 0)
+++ sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/main/java/org/jiemamy/core/eventcodegen/JiemamyModelDoclet.java	2008-09-10 17:08:38 UTC (rev 1910)
@@ -0,0 +1,115 @@
+package org.jiemamy.core.eventcodegen;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.jiemamy.core.model.JiemamyModel;
+
+import com.sun.javadoc.ClassDoc;
+import com.sun.javadoc.Doclet;
+import com.sun.javadoc.FieldDoc;
+import com.sun.javadoc.LanguageVersion;
+import com.sun.javadoc.RootDoc;
+
+/**
+ * Event用のソースを自動生成する。 j-coreプロジェクト配下のソースを読み込み、{@link JiemamyModel}を発見した場合にその{@link JiemamyModel}用のChangeListenerとChangeSupportを自動生成する。
+ * 
+ * @author shin1ogawa
+ */
+public class JiemamyModelDoclet extends Doclet {
+	public List<JiemamyModel> model;
+	public Map<Class<?>, CollectionProperty> collectionProperties = new HashMap<Class<?>, CollectionProperty>();
+
+	public static LanguageVersion languageVersion() {
+		return LanguageVersion.JAVA_1_5;
+	}
+
+	/**
+	 * Docletが開始された時に呼ばれる。
+	 * 
+	 * @param root
+	 * @return
+	 */
+	public static boolean start(RootDoc root) {
+		ClassDoc[] classes = root.classes();
+		for (int i = 0; i < classes.length; ++i) {
+			processClassDoc(classes[i]);
+		}
+		return true;
+	}
+
+	/**
+	 * 読み込んだ{@link ClassDoc}を処理する。
+	 * 
+	 * @param classDoc
+	 */
+	public static void processClassDoc(ClassDoc classDoc) {
+		try {
+			try {
+				Class<?> clazz = Class.forName(classDoc.qualifiedName());
+				// Instance化出来ないものはInstantialtionExceptionを発生させて弾く。
+				clazz.newInstance();
+				// JiemamyModelのサブクラスでないものはClassCastExceptionを発生させて弾く。
+				Class<? extends JiemamyModel> modelClass = clazz
+						.asSubclass(JiemamyModel.class);
+				processModel(classDoc);
+			} catch (Exception ex) {
+				// 無視する。
+			}
+		} catch (Exception ex) {
+			//
+		}
+	}
+
+	/**
+	 * {@link JiemamyModel}を解析し、情報を抜き出す。
+	 * <p>
+	 * {@link JiemamyModel}のField情報を取得し、{@link CollectionProperty}を作成し、
+	 * {@link #collectionProperties}に追加する。
+	 * </p>
+	 * 
+	 * @param classDoc
+	 * @throws ClassNotFoundException
+	 */
+	private static void processModel(ClassDoc classDoc)
+			throws ClassNotFoundException {
+		System.out.println(classDoc.qualifiedName());
+		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));
+				properties.add(new CollectionProperty(fieldDoc.name(),
+						Class.forName(fieldDoc.type().qualifiedTypeName()
+								.toString()), Class
+								.forName(getParameterClassName(fieldDoc))));
+			}
+		}
+	}
+
+	/**
+	 * {@link List}フィールドのGeneric情報を返す。
+	 * 
+	 * @param fieldDoc
+	 * @return
+	 */
+	private static String getParameterClassName(FieldDoc fieldDoc) {
+		String parameterizedType = fieldDoc.type().asParameterizedType()
+				.toString();
+		int index1 = parameterizedType.indexOf('<');
+		int index2 = parameterizedType.lastIndexOf('>');
+		return parameterizedType.substring(index1 + 1, index2 + 1).replaceAll(
+				"<?>", "");
+	}
+}


Property changes on: sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/main/java/org/jiemamy/core/eventcodegen/JiemamyModelDoclet.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/test/java/org/jiemamy/core/eventcodegen/ListenerTemplateTest.java
===================================================================
--- sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/test/java/org/jiemamy/core/eventcodegen/ListenerTemplateTest.java	2008-09-10 16:31:01 UTC (rev 1909)
+++ sandbox/org.jiemamy.core.eventcodegen/trunk/org.jiemamy.core.eventcodegen/src/test/java/org/jiemamy/core/eventcodegen/ListenerTemplateTest.java	2008-09-10 17:08:38 UTC (rev 1910)
@@ -26,8 +26,9 @@
 	public static void setUpBeforeClass() throws Exception {
 		Properties properties = new Properties();
 		properties.setProperty("resource.loader", "class");
-		properties.setProperty("class.resource.loader.class",
-				"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
+		properties
+				.setProperty("class.resource.loader.class",
+						"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
 		properties.setProperty("input.encoding", "UTF-8");
 		properties.setProperty("runtime.log.invalid.references", "false");
 		Velocity.init(properties);
@@ -49,21 +50,26 @@
 	}
 
 	@Test
-	public void test01() throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, IOException {
+	public void test01() throws ResourceNotFoundException, ParseErrorException,
+			MethodInvocationException, IOException {
 		List<String> importClasses = new ArrayList<String>();
 		importClasses.add("org.jiemamy.core.model.node.TableModel");
 		importClasses.add("org.jiemamy.core.model.node.ColumnModel");
 		importClasses.add("hoge");
 
 		List<CollectionProperty> properties = new ArrayList<CollectionProperty>();
-		properties.add(new CollectionProperty("columns", List.class, ColumnModel.class));
-		properties.add(new CollectionProperty("indexes", List.class, IndexModel.class));
-		properties.add(new CollectionProperty("checks", List.class, CheckConstraintModel.class));
+		properties.add(new CollectionProperty("columns", List.class,
+				ColumnModel.class));
+		properties.add(new CollectionProperty("indexes", List.class,
+				IndexModel.class));
+		properties.add(new CollectionProperty("checks", List.class,
+				CheckConstraintModel.class));
 
 		velocityContext.put("modelClassName", "TableModel");
 		velocityContext.put("importClasses", importClasses);
 		velocityContext.put("properties", properties);
-		FileWriter writer = new FileWriter("target/ListenerTemplateTest#test01.java");
+		FileWriter writer = new FileWriter(
+				"target/ListenerTemplateTest#test01.java");
 		listenerTemplate.merge(velocityContext, writer);
 		writer.flush();
 		writer.close();


Jiemamy-notify メーリングリストの案内
Back to archive index