JavaSE Doclet
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
] [
Trackback(0)
]
開始行:
#contents
*Docletとは? [#w0cee1ab]
-JDK に付属する javadoc の plugin
-通常の Javadoc が、ソースコードのコメントを元に HTML を...
-昔々、Struts1 の設定ファイル (struts-config.xml / valida...
-そういう用途では、最近は annotation を使って、動作時に処...
*Docletの作り方 [#l8ae254b]
-http://docs.oracle.com/javase/jp/1.5.0/guide/javadoc/doc...
-Doclet のエントリーポイントは public static boolean star...
$ javadoc -doclet MyDoclet -docletpath . MyClass.java
で、start() が呼ばれる
-通常は com.sun.javadoc.Doclet を継承して作る
*メソッド一覧作成 Doclet [#hcb47feb]
-MethodListGenerator
#code(java){{{
package com.mycompany.docletexam;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.DocErrorReporter;
import com.sun.javadoc.Doclet;
import com.sun.javadoc.MethodDoc;
import com.sun.javadoc.Parameter;
import com.sun.javadoc.RootDoc;
import com.sun.javadoc.SourcePosition;
import com.sun.javadoc.Type;
/**
* メソッド列挙 Doclet. 使い方 javadoc -encoding UTF-8 -s...
* -doclet com.mycompany.docletexam.MethodListGenerator -...
* lib/MyDoclet.jar -d .\doc\summary.csv -ignore-get-set ...
*
* -encodieng : ソースのエンコーディング (javadoc共通) -s...
* -public pkg1 pkg2 ... / -private pkg1 pkg2 ... : 対象...
* : (必須) カスタム Docklet クラス -docletpath : (必須) ...
* -d : (必須) 出力先 -ignore-get-set : (任意) getXXX と ...
* -ignore-gui : (任意) java.awt.* の子孫クラスと、それら...
*
* ant や maven からも使えます。(サンプルは本クラスの pom...
*/
public class MethodListGenerator extends Doclet {
/**
* 処理開始
* @param rootDoc RootDoc
* @return true 出力成功<br/>false 失敗
*/
public static boolean start(final RootDoc rootDoc) {
try {
MethodListGenerator test = new MethodListGene...
test.printOption(rootDoc);
test.printDoclet(rootDoc);
System.out.println("...done");
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* オプションの個数を返す. -d が 2 を返すほかは、com....
*
* @param option
* @return オプションの個数
*/
public static int optionLength(final String option) {
if (option.matches("-d")) {
return 2;
} else if (option.matches("-ignore-get-set")) {
return 1;
} else if (option.matches("-ignore-gui")) {
return 1;
}
return Doclet.optionLength(option);
}
/**
* オプションの検証.
*
* @param options コマンドオプション
* @param reporter エラー
* @return true : 合格<br/>false : 不合格
*/
public static boolean validOptions(final String[][] o...
DocErrorReporter reporter) {
// -d があったら、com.sun.javadoc.Doclet#validOpt...
for (String[] option : options) {
if ("-d".equals(option[0])) {
return Doclet.validOptions(options, repor...
}
}
reporter.printError("-d オプションで、出力先 cvs ...
return false;
}
/**
* 起動オプションの表示
*/
private void printOption(final RootDoc rootDoc) {
System.out.println("Options:");
for (String[] param : rootDoc.options()) {
System.out.format("\t%s\r\n", Arrays.toString...
}
}
/**
* Docletの表示
*/
private void printDoclet(final RootDoc rootDoc) throw...
File report = new File(findOptionValue(rootDoc, "...
boolean ignoreGetSet = findOptionFlag(rootDoc, "-...
boolean ignoreGui = findOptionFlag(rootDoc, "-ign...
// ディレクトリを作成する
File dir = report.getParentFile();
if (dir != null && !dir.exists()) {
dir.mkdirs();
}
// MS-Excel で開けるように MS932 で出力
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStre...
int no = 1;
for (ClassDoc classDoc : rootDoc.classes()) {
bw.write(String.format("#,\"%s\",\"\",\"%s\"\...
classDoc.qualifiedName(), getFirstLine(clas...
if (ignoreGui && isGuiComponet(classDoc)) {
continue;
}
for (MethodDoc methodDoc : classDoc.methods()...
//メソッド名
String methodName = methodDoc.name();
if (ignoreGetSet) {
if (methodName.startsWith("get") && f...
// 対抗する setter がある getter ...
continue;
}
if (methodName.startsWith("set") && f...
// 対抗する getter がある setter ...
continue;
}
}
//ソース位置
SourcePosition mtdPosition = methodDoc.po...
int mtdLine = mtdPosition.line();
//修飾子
String modifiersName = methodDoc.modifier...
//戻り値
Type returnType = methodDoc.returnType();
if (ignoreGui && isGuiComponet(returnType...
continue;
}
String returnName = returnType.typeName();
if (returnType.dimension() != null) {
returnName += returnType.dimension();
}
//パラメータ
String paramName = "";
for (Parameter parameter : methodDoc.para...
if (ignoreGui && isGuiComponet(parame...
continue;
}
paramName += "".equals(paramName) ? p...
}
bw.write(
String.format("%d,\"\",\"%s %s %s(%s)\"...
no++, modifiersName, returnName, meth...
getFirstLine(methodDoc.commentText())...
}
}
bw.close();
System.out.println("REPORT : " + report.getAbsolu...
}
/**
* classDoc が AWT コンポーネントを継承しているかどう...
*
* @param classDoc 検査対象
* @return true : AWT コンポーネント <br/> false : AW...
*/
private boolean isGuiComponet(final ClassDoc classDoc...
if (classDoc == null) {
return false;
}
if (classDoc.qualifiedName().startsWith("java.awt...
return true;
}
return isGuiComponet(classDoc.superclass());
}
/**
* doclet のコマンドオプションにフラグ option がある...
*
* @param rootDoc rootDoc
* @param option 調査対象の option
* @return true : あり <br/> false : なし
*/
private String findOptionValue(final RootDoc rootDoc,...
for (String[] param : rootDoc.options()) {
if (param[0].equals(option)) {
return param[1];
}
}
return null;
}
/**
* doclet のコマンドオプションに項目 option があるか...
*
* @param rootDoc rootDoc
* @param option 調査対象の option
* @return option の値
*/
private boolean findOptionFlag(final RootDoc rootDoc,...
for (String[] param : rootDoc.options()) {
if (param[0].equals(option)) {
return true;
}
}
return false;
}
/**
* Javadoc の 1行目を取得します.
*
* @param doc Javadoc
* @return 1行目
*/
private String getFirstLine(final String doc) {
if (doc == null) {
return "";
}
// 1行目を取り出し、HTMLタグ (<br/>) があれば削除する
// " は "" にする (CSVの制約)
String[] parts = doc.split("[\r\n]");
return parts[0]
.replaceAll("<.*>", "")
.replaceAll("\"", "\"\"");
}
/**
* setter に対応する getter があるかどうかを調べます.
*
* @param getter
* @return
*/
private boolean findGetter(final ClassDoc classDoc, f...
//メソッド名
String methodName = setter.name();
return findMethod(classDoc, methodName.replace("s...
}
/**
* getter に対応する setter があるかどうかを調べます.
*
* @param getter
* @return
*/
private boolean findSetter(final ClassDoc classDoc, f...
//メソッド名
String methodName = getter.name();
return findMethod(classDoc, methodName.replace("g...
}
/**
* class に、引数が arg で返値が ret のメソッド metho...
*
* @param classDoc Class
* @param method メソッド名
* @return true あり<br/>false なし
*/
private boolean findMethod(final ClassDoc classDoc, f...
for (MethodDoc methodDoc : classDoc.methods()) {
//メソッド名
String methodName = methodDoc.name();
if (method.equals(methodName)) {
return true;
}
}
return false;
}
}
}}}
-pom.xml
#code(xml){{{
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:...
xsi:schemaLocation="http://maven.apache.org/POM/...
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.docletexam</groupId>
<artifactId>doclet</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>doclet</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.buil...
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.7</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</sy...
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<configuration>
<locales>ja</locales>
<inputEncoding>UTF-8</inputEncoding>
<outputEncoding>UTF-8</outputEncoding>
<reportPlugins>
<plugin>
<groupId>org.apache.maven.plu...
<artifactId>maven-javadoc-plu...
<version>2.9</version>
<configuration>
<doclet>com.mycompany.doc...
<encoding>UTF-8</encoding>
<access>private</access>
<docletPath>target/doclet...
<additionalparam>-d doc\r...
</configuration>
</plugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
</project>
}}}
*実行結果 [#ica08d3a]
$ javadoc -doclet com.mycompany.docletexam.MethodListGen...
-sourcepath src/main/java/ -public com.mycompany.doclete...
#ref(csv.png)
pom.xml からも実行できる (使用例は MethodListGenerator の...
----
[[Java#JavaSE]]
終了行:
#contents
*Docletとは? [#w0cee1ab]
-JDK に付属する javadoc の plugin
-通常の Javadoc が、ソースコードのコメントを元に HTML を...
-昔々、Struts1 の設定ファイル (struts-config.xml / valida...
-そういう用途では、最近は annotation を使って、動作時に処...
*Docletの作り方 [#l8ae254b]
-http://docs.oracle.com/javase/jp/1.5.0/guide/javadoc/doc...
-Doclet のエントリーポイントは public static boolean star...
$ javadoc -doclet MyDoclet -docletpath . MyClass.java
で、start() が呼ばれる
-通常は com.sun.javadoc.Doclet を継承して作る
*メソッド一覧作成 Doclet [#hcb47feb]
-MethodListGenerator
#code(java){{{
package com.mycompany.docletexam;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.DocErrorReporter;
import com.sun.javadoc.Doclet;
import com.sun.javadoc.MethodDoc;
import com.sun.javadoc.Parameter;
import com.sun.javadoc.RootDoc;
import com.sun.javadoc.SourcePosition;
import com.sun.javadoc.Type;
/**
* メソッド列挙 Doclet. 使い方 javadoc -encoding UTF-8 -s...
* -doclet com.mycompany.docletexam.MethodListGenerator -...
* lib/MyDoclet.jar -d .\doc\summary.csv -ignore-get-set ...
*
* -encodieng : ソースのエンコーディング (javadoc共通) -s...
* -public pkg1 pkg2 ... / -private pkg1 pkg2 ... : 対象...
* : (必須) カスタム Docklet クラス -docletpath : (必須) ...
* -d : (必須) 出力先 -ignore-get-set : (任意) getXXX と ...
* -ignore-gui : (任意) java.awt.* の子孫クラスと、それら...
*
* ant や maven からも使えます。(サンプルは本クラスの pom...
*/
public class MethodListGenerator extends Doclet {
/**
* 処理開始
* @param rootDoc RootDoc
* @return true 出力成功<br/>false 失敗
*/
public static boolean start(final RootDoc rootDoc) {
try {
MethodListGenerator test = new MethodListGene...
test.printOption(rootDoc);
test.printDoclet(rootDoc);
System.out.println("...done");
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* オプションの個数を返す. -d が 2 を返すほかは、com....
*
* @param option
* @return オプションの個数
*/
public static int optionLength(final String option) {
if (option.matches("-d")) {
return 2;
} else if (option.matches("-ignore-get-set")) {
return 1;
} else if (option.matches("-ignore-gui")) {
return 1;
}
return Doclet.optionLength(option);
}
/**
* オプションの検証.
*
* @param options コマンドオプション
* @param reporter エラー
* @return true : 合格<br/>false : 不合格
*/
public static boolean validOptions(final String[][] o...
DocErrorReporter reporter) {
// -d があったら、com.sun.javadoc.Doclet#validOpt...
for (String[] option : options) {
if ("-d".equals(option[0])) {
return Doclet.validOptions(options, repor...
}
}
reporter.printError("-d オプションで、出力先 cvs ...
return false;
}
/**
* 起動オプションの表示
*/
private void printOption(final RootDoc rootDoc) {
System.out.println("Options:");
for (String[] param : rootDoc.options()) {
System.out.format("\t%s\r\n", Arrays.toString...
}
}
/**
* Docletの表示
*/
private void printDoclet(final RootDoc rootDoc) throw...
File report = new File(findOptionValue(rootDoc, "...
boolean ignoreGetSet = findOptionFlag(rootDoc, "-...
boolean ignoreGui = findOptionFlag(rootDoc, "-ign...
// ディレクトリを作成する
File dir = report.getParentFile();
if (dir != null && !dir.exists()) {
dir.mkdirs();
}
// MS-Excel で開けるように MS932 で出力
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStre...
int no = 1;
for (ClassDoc classDoc : rootDoc.classes()) {
bw.write(String.format("#,\"%s\",\"\",\"%s\"\...
classDoc.qualifiedName(), getFirstLine(clas...
if (ignoreGui && isGuiComponet(classDoc)) {
continue;
}
for (MethodDoc methodDoc : classDoc.methods()...
//メソッド名
String methodName = methodDoc.name();
if (ignoreGetSet) {
if (methodName.startsWith("get") && f...
// 対抗する setter がある getter ...
continue;
}
if (methodName.startsWith("set") && f...
// 対抗する getter がある setter ...
continue;
}
}
//ソース位置
SourcePosition mtdPosition = methodDoc.po...
int mtdLine = mtdPosition.line();
//修飾子
String modifiersName = methodDoc.modifier...
//戻り値
Type returnType = methodDoc.returnType();
if (ignoreGui && isGuiComponet(returnType...
continue;
}
String returnName = returnType.typeName();
if (returnType.dimension() != null) {
returnName += returnType.dimension();
}
//パラメータ
String paramName = "";
for (Parameter parameter : methodDoc.para...
if (ignoreGui && isGuiComponet(parame...
continue;
}
paramName += "".equals(paramName) ? p...
}
bw.write(
String.format("%d,\"\",\"%s %s %s(%s)\"...
no++, modifiersName, returnName, meth...
getFirstLine(methodDoc.commentText())...
}
}
bw.close();
System.out.println("REPORT : " + report.getAbsolu...
}
/**
* classDoc が AWT コンポーネントを継承しているかどう...
*
* @param classDoc 検査対象
* @return true : AWT コンポーネント <br/> false : AW...
*/
private boolean isGuiComponet(final ClassDoc classDoc...
if (classDoc == null) {
return false;
}
if (classDoc.qualifiedName().startsWith("java.awt...
return true;
}
return isGuiComponet(classDoc.superclass());
}
/**
* doclet のコマンドオプションにフラグ option がある...
*
* @param rootDoc rootDoc
* @param option 調査対象の option
* @return true : あり <br/> false : なし
*/
private String findOptionValue(final RootDoc rootDoc,...
for (String[] param : rootDoc.options()) {
if (param[0].equals(option)) {
return param[1];
}
}
return null;
}
/**
* doclet のコマンドオプションに項目 option があるか...
*
* @param rootDoc rootDoc
* @param option 調査対象の option
* @return option の値
*/
private boolean findOptionFlag(final RootDoc rootDoc,...
for (String[] param : rootDoc.options()) {
if (param[0].equals(option)) {
return true;
}
}
return false;
}
/**
* Javadoc の 1行目を取得します.
*
* @param doc Javadoc
* @return 1行目
*/
private String getFirstLine(final String doc) {
if (doc == null) {
return "";
}
// 1行目を取り出し、HTMLタグ (<br/>) があれば削除する
// " は "" にする (CSVの制約)
String[] parts = doc.split("[\r\n]");
return parts[0]
.replaceAll("<.*>", "")
.replaceAll("\"", "\"\"");
}
/**
* setter に対応する getter があるかどうかを調べます.
*
* @param getter
* @return
*/
private boolean findGetter(final ClassDoc classDoc, f...
//メソッド名
String methodName = setter.name();
return findMethod(classDoc, methodName.replace("s...
}
/**
* getter に対応する setter があるかどうかを調べます.
*
* @param getter
* @return
*/
private boolean findSetter(final ClassDoc classDoc, f...
//メソッド名
String methodName = getter.name();
return findMethod(classDoc, methodName.replace("g...
}
/**
* class に、引数が arg で返値が ret のメソッド metho...
*
* @param classDoc Class
* @param method メソッド名
* @return true あり<br/>false なし
*/
private boolean findMethod(final ClassDoc classDoc, f...
for (MethodDoc methodDoc : classDoc.methods()) {
//メソッド名
String methodName = methodDoc.name();
if (method.equals(methodName)) {
return true;
}
}
return false;
}
}
}}}
-pom.xml
#code(xml){{{
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:...
xsi:schemaLocation="http://maven.apache.org/POM/...
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.docletexam</groupId>
<artifactId>doclet</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>doclet</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.buil...
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.7</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</sy...
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<configuration>
<locales>ja</locales>
<inputEncoding>UTF-8</inputEncoding>
<outputEncoding>UTF-8</outputEncoding>
<reportPlugins>
<plugin>
<groupId>org.apache.maven.plu...
<artifactId>maven-javadoc-plu...
<version>2.9</version>
<configuration>
<doclet>com.mycompany.doc...
<encoding>UTF-8</encoding>
<access>private</access>
<docletPath>target/doclet...
<additionalparam>-d doc\r...
</configuration>
</plugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
</project>
}}}
*実行結果 [#ica08d3a]
$ javadoc -doclet com.mycompany.docletexam.MethodListGen...
-sourcepath src/main/java/ -public com.mycompany.doclete...
#ref(csv.png)
pom.xml からも実行できる (使用例は MethodListGenerator の...
----
[[Java#JavaSE]]
ページ名:
ISBN10
ISBN13
9784061426061