Eclipse/プラグイン開発のTIPS集/ソースコードを解析するパーサASTParser/TypeDeclaration
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。
#contents
VisitorのうちTypeDeclaration を引数に取る
public boolean visit(TypeDeclaration node);
はクラスのいろいろな情報がとれそうです。
TypeDeclaration はあるソースコード内の一つのクラスに対応...
**対象のクラス [#n229a531]
-nu.mine.kino.IBL インタフェース
package nu.mine.kino;
public interface IBL {
void exe();
String exe(String hoge);
}
-nu.mine.kino.BL クラス
package nu.mine.kino;
import java.io.Serializable;
public class BL implements IBL, Serializable {
private final String field;
public BL() {
System.out.println("Constructor");
field = "";
}
public BL(String field) {
System.out.println("Constructor");
this.field = field;
}
public void exe() {
System.out.println("hoge");
}
public String exe(String hoge) {
System.out.println(hoge);
return hoge;
}
}
**いろいろ試してみる [#gba5f11f]
***実装しているインタフェースを取得する [#n3933319]
上のBLに対して
List interfaces = node.superInterfaceTypes();
を実行するとorg.eclipse.jdt.core.dom.SimpleTypeのListが取...
SimpleType#getName();
とすることで実装しているインタフェース名が取得できます。
***ソースコード内に登場するクラスのパッケージ名を取得する...
苦労しました。以下のような感じ??elementは ICompilationU...
List superInterfaceTypes = node.superInterfaceTypes();
logger.debug("SuperInterfaceTypes: " + superInterfaceTyp...
try {
// ソースコードの文字列を取得
String source = element.getSource();
// 取得したインタフェース名でループ
for (Object interfaze : superInterfaceTypes) {
Type typee = (Type) interfaze;
// System.out.println(typee.toString());
int start = source.indexOf(typee.toString());
int length = typee.toString().length();
// 上のstart,lengthで挟まれる部分から、IJavaElement...
IJavaElement[] elements = element.codeSelect(start, ...
for (IJavaElement element : elements) {
logger.debug("Path: " + element.getPath().toOSStri...
logger.debug("Element Name: "
+ element.getElementName());
// elementがITypeだったら、
if (element.getElementType() == IJavaElement.TYPE) {
logger.debug(element.getElementName() + "はITYPE...
IType type = (IType) element;
logger.debug("パッケージ名: "
+ type.getPackageFragment()
.getElementName());
}
}
}
} catch (JavaModelException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
ようするにソース上の文字列上の場所を探してきて、
IJavaElement[] elements = unit.codeSelect(start, length);
としてIJavaElementを生成してるわけですね。。これだと、び...
***あるソースコードに書かれているクラス名を全て取得する [...
elementは ICompilationUnit です。
try {
IType[] types = element.getTypes();
for (IType type : types) {
logger.debug("Types[i]: " + type.getElementName());
}
} catch (JavaModelException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
これでそのソースコードに書かれているクラスのクラス名をす...
----
この記事は
#vote(おもしろかった[5],そうでもない[0])
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}
終了行:
// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。
#contents
VisitorのうちTypeDeclaration を引数に取る
public boolean visit(TypeDeclaration node);
はクラスのいろいろな情報がとれそうです。
TypeDeclaration はあるソースコード内の一つのクラスに対応...
**対象のクラス [#n229a531]
-nu.mine.kino.IBL インタフェース
package nu.mine.kino;
public interface IBL {
void exe();
String exe(String hoge);
}
-nu.mine.kino.BL クラス
package nu.mine.kino;
import java.io.Serializable;
public class BL implements IBL, Serializable {
private final String field;
public BL() {
System.out.println("Constructor");
field = "";
}
public BL(String field) {
System.out.println("Constructor");
this.field = field;
}
public void exe() {
System.out.println("hoge");
}
public String exe(String hoge) {
System.out.println(hoge);
return hoge;
}
}
**いろいろ試してみる [#gba5f11f]
***実装しているインタフェースを取得する [#n3933319]
上のBLに対して
List interfaces = node.superInterfaceTypes();
を実行するとorg.eclipse.jdt.core.dom.SimpleTypeのListが取...
SimpleType#getName();
とすることで実装しているインタフェース名が取得できます。
***ソースコード内に登場するクラスのパッケージ名を取得する...
苦労しました。以下のような感じ??elementは ICompilationU...
List superInterfaceTypes = node.superInterfaceTypes();
logger.debug("SuperInterfaceTypes: " + superInterfaceTyp...
try {
// ソースコードの文字列を取得
String source = element.getSource();
// 取得したインタフェース名でループ
for (Object interfaze : superInterfaceTypes) {
Type typee = (Type) interfaze;
// System.out.println(typee.toString());
int start = source.indexOf(typee.toString());
int length = typee.toString().length();
// 上のstart,lengthで挟まれる部分から、IJavaElement...
IJavaElement[] elements = element.codeSelect(start, ...
for (IJavaElement element : elements) {
logger.debug("Path: " + element.getPath().toOSStri...
logger.debug("Element Name: "
+ element.getElementName());
// elementがITypeだったら、
if (element.getElementType() == IJavaElement.TYPE) {
logger.debug(element.getElementName() + "はITYPE...
IType type = (IType) element;
logger.debug("パッケージ名: "
+ type.getPackageFragment()
.getElementName());
}
}
}
} catch (JavaModelException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
ようするにソース上の文字列上の場所を探してきて、
IJavaElement[] elements = unit.codeSelect(start, length);
としてIJavaElementを生成してるわけですね。。これだと、び...
***あるソースコードに書かれているクラス名を全て取得する [...
elementは ICompilationUnit です。
try {
IType[] types = element.getTypes();
for (IType type : types) {
logger.debug("Types[i]: " + type.getElementName());
}
} catch (JavaModelException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
これでそのソースコードに書かれているクラスのクラス名をす...
----
この記事は
#vote(おもしろかった[5],そうでもない[0])
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}
ページ名: