Eclipse/プラグイン開発のTIPS集/org.eclipse.jdt.core.ICompilationUnit
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
// 下階層用テンプレート
#topicpath
----
#contents
//ここにコンテンツを記述します。
org.eclipse.jdt.core.ICompilationUnit はパッケージエクス...
**やってみる [#c600ab08]
次のサンプルコードは、Eclipseのアクションクラス(ハンドラ...
IJavaElement[] getChildren() throws JavaModelException;
を使って、コードを解析していきます。
public Object execute(ExecutionEvent event) throws Execu...
ISelection selection = HandlerUtil.getActiveMenuSelect...
if (selection instanceof IStructuredSelection) {
IStructuredSelection sselection = (IStructuredSelect...
Object firstElement = sselection.getFirstElement();
if (firstElement instanceof ICompilationUnit) {
ICompilationUnit unit = (ICompilationUnit) firstEl...
try {
if (!unit.isStructureKnown()) {
return null;
}
// まずは子要素を取得。取得されるのは、パッケー...
// クラスが複数定義されている場合もあるし。
IJavaElement elements[] = unit.getChildren();
for (IJavaElement javaElement : elements) {
// ↓型だったらば、ITypeにキャスト。
if (javaElement.getElementType() == IJavaEleme...
IType type = (IType) javaElement;
// メソッド一覧を取得。
IMethod[] methods = type.getMethods();
// IMethod method = methods[methods.length -...
for (IMethod method : methods) {
System.out.println(method.getElementName());
}
}
}
} catch (JavaModelException e) {
e.printStackTrace();
}
}
}
return null;
}
-Sample.java
package hoge;
public class Sample {
public void setHoge(String hoge){
this.hoge = hoge;
}
private String hoge;
public String getHoge() {
return hoge;
}
}
こんなコードに対して上のハンドラを実行したところ、
setHoge
getHoge
と表示されました。
**TIPS集 [#d2b6421c]
***配下のIJavaElementの変数から、ICompilationUnit を取得...
ITypeなIJavaElementの変数とかからそれを包含しているICompi...
ICompilationUnit unit = (ICompilationUnit) element
.getAncestor(IJavaElement.COMPILATION_UNIT);
***ソースコードのIResourceインスタンスを取得する。 [#b994...
JDT周りをやってるとIResouce系を忘れてしまいそうですが、あ...
// ソースコードに対応するファイルのIResource
IResource resource = cu.getCorrespondingResource();
これでOKですね。
***ある要素(フィールドとか、メソッドとか)のオフセット位置...
EclipseのJDTのソース見てると、ファイルの先頭からの文字数...
それを実装する基本として、ソースの先頭からある要素(ここで...
-Sample.java
package hoge;
public class Sample {
public void setHoge(String hoge){ <-このメソッドの「p...
this.hoge = hoge;
} <-また「p」前の位置からこのカッコ終わりまでの長さは...
private String hoge;
public String getHoge() { <-このメソッドの「p」の位置...
return hoge;
} <-また「p」前の位置からこのカッコ終わりまでの長さは...
}
オフセットを取得するコードは以下の通り。
public Object execute(ExecutionEvent event) throws Execu...
--- 先と同じなので、省略 ---
if (firstElement instanceof ICompilationUnit) {
ICompilationUnit unit = (ICompilationUnit) firstElem...
try {
if (!unit.isStructureKnown()) {
return null;
}
IJavaElement elements[] = unit.getChildren();
for (IJavaElement javaElement : elements) {
if (javaElement.getElementType() == IJavaElement...
IType type = (IType) javaElement;
IMethod[] methods = type.getMethods();
for (IMethod method : methods) {
ISourceRange sourceRange = method
.getSourceRange();
int offset = sourceRange.getOffset();
System.out.println(offset);
}
}
}
} catch (JavaModelException e) {
e.printStackTrace();
// TODO: handle exception
}
}
return null;
}
この通り、IMethod method を取得して、そのインスタンスに対し
ISourceRange sourceRange = method.getSourceRange();
int offset = sourceRange.getOffset();
とすることでオフセット位置を取得することができます。ちな...
38
121
となりました。
またそのメソッドの長さ(オフセット位置から、そのメソッドの...
int length = sourceRange.getLength();
とやって取得できます。実行結果は、
56
43
です。これでメソッド終わり場所のオフセット位置なども取得...
**Eclipse JDTではどうやってるか [#vfeb173b]
なるほどねーと思って上のやり方を見てたのですが、コメント...
コメントの下の、ホントにメソッドの開始はどう取るんだろう...
private int getMemberStartOffset(IMethod lastMethod, IDo...
throws JavaModelException {
ISourceRange sourceRange = lastMethod.getSourceRange();
int memberStartOffset = sourceRange.getOffset();
// 通常は上のオフセットを返すので問題ないけど、コメン...
// ホントにメソッドの直上にする場合は、このようにする...
TokenScanner scanner = new TokenScanner(document, last...
try {
return scanner.getNextStartOffset(memberStartOffset,...
// to the first real non comment token
} catch (CoreException e) {
// ignore
}
return memberStartOffset;
}
TokenScannerってやつにまかせてるんですね:-)。
***ソースコードを開く。 [#s8bdedd8]
unitがICompilationUnitだとして
// open the editor, forces the creation of a working copy
IEditorPart editor = JavaUI.openInEditor(unit);
でエディタでソースコードを開くことができます。ICompilatio...
IEditorPart editor = JavaUI.openInEditor(element.getAnce...
などでIJavaElementからICompilationUnitを取得して開くこと...
**関連リンク [#w3f6351b]
-Eclipse/プラグイン開発のTIPS集/Javaプロジェクトを操作す...
-Eclipse/プラグイン開発のTIPS集/ソースコードを解析するパ...
----
この記事は
#vote(おもしろかった[2],そうでもない[0])
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}
終了行:
// 下階層用テンプレート
#topicpath
----
#contents
//ここにコンテンツを記述します。
org.eclipse.jdt.core.ICompilationUnit はパッケージエクス...
**やってみる [#c600ab08]
次のサンプルコードは、Eclipseのアクションクラス(ハンドラ...
IJavaElement[] getChildren() throws JavaModelException;
を使って、コードを解析していきます。
public Object execute(ExecutionEvent event) throws Execu...
ISelection selection = HandlerUtil.getActiveMenuSelect...
if (selection instanceof IStructuredSelection) {
IStructuredSelection sselection = (IStructuredSelect...
Object firstElement = sselection.getFirstElement();
if (firstElement instanceof ICompilationUnit) {
ICompilationUnit unit = (ICompilationUnit) firstEl...
try {
if (!unit.isStructureKnown()) {
return null;
}
// まずは子要素を取得。取得されるのは、パッケー...
// クラスが複数定義されている場合もあるし。
IJavaElement elements[] = unit.getChildren();
for (IJavaElement javaElement : elements) {
// ↓型だったらば、ITypeにキャスト。
if (javaElement.getElementType() == IJavaEleme...
IType type = (IType) javaElement;
// メソッド一覧を取得。
IMethod[] methods = type.getMethods();
// IMethod method = methods[methods.length -...
for (IMethod method : methods) {
System.out.println(method.getElementName());
}
}
}
} catch (JavaModelException e) {
e.printStackTrace();
}
}
}
return null;
}
-Sample.java
package hoge;
public class Sample {
public void setHoge(String hoge){
this.hoge = hoge;
}
private String hoge;
public String getHoge() {
return hoge;
}
}
こんなコードに対して上のハンドラを実行したところ、
setHoge
getHoge
と表示されました。
**TIPS集 [#d2b6421c]
***配下のIJavaElementの変数から、ICompilationUnit を取得...
ITypeなIJavaElementの変数とかからそれを包含しているICompi...
ICompilationUnit unit = (ICompilationUnit) element
.getAncestor(IJavaElement.COMPILATION_UNIT);
***ソースコードのIResourceインスタンスを取得する。 [#b994...
JDT周りをやってるとIResouce系を忘れてしまいそうですが、あ...
// ソースコードに対応するファイルのIResource
IResource resource = cu.getCorrespondingResource();
これでOKですね。
***ある要素(フィールドとか、メソッドとか)のオフセット位置...
EclipseのJDTのソース見てると、ファイルの先頭からの文字数...
それを実装する基本として、ソースの先頭からある要素(ここで...
-Sample.java
package hoge;
public class Sample {
public void setHoge(String hoge){ <-このメソッドの「p...
this.hoge = hoge;
} <-また「p」前の位置からこのカッコ終わりまでの長さは...
private String hoge;
public String getHoge() { <-このメソッドの「p」の位置...
return hoge;
} <-また「p」前の位置からこのカッコ終わりまでの長さは...
}
オフセットを取得するコードは以下の通り。
public Object execute(ExecutionEvent event) throws Execu...
--- 先と同じなので、省略 ---
if (firstElement instanceof ICompilationUnit) {
ICompilationUnit unit = (ICompilationUnit) firstElem...
try {
if (!unit.isStructureKnown()) {
return null;
}
IJavaElement elements[] = unit.getChildren();
for (IJavaElement javaElement : elements) {
if (javaElement.getElementType() == IJavaElement...
IType type = (IType) javaElement;
IMethod[] methods = type.getMethods();
for (IMethod method : methods) {
ISourceRange sourceRange = method
.getSourceRange();
int offset = sourceRange.getOffset();
System.out.println(offset);
}
}
}
} catch (JavaModelException e) {
e.printStackTrace();
// TODO: handle exception
}
}
return null;
}
この通り、IMethod method を取得して、そのインスタンスに対し
ISourceRange sourceRange = method.getSourceRange();
int offset = sourceRange.getOffset();
とすることでオフセット位置を取得することができます。ちな...
38
121
となりました。
またそのメソッドの長さ(オフセット位置から、そのメソッドの...
int length = sourceRange.getLength();
とやって取得できます。実行結果は、
56
43
です。これでメソッド終わり場所のオフセット位置なども取得...
**Eclipse JDTではどうやってるか [#vfeb173b]
なるほどねーと思って上のやり方を見てたのですが、コメント...
コメントの下の、ホントにメソッドの開始はどう取るんだろう...
private int getMemberStartOffset(IMethod lastMethod, IDo...
throws JavaModelException {
ISourceRange sourceRange = lastMethod.getSourceRange();
int memberStartOffset = sourceRange.getOffset();
// 通常は上のオフセットを返すので問題ないけど、コメン...
// ホントにメソッドの直上にする場合は、このようにする...
TokenScanner scanner = new TokenScanner(document, last...
try {
return scanner.getNextStartOffset(memberStartOffset,...
// to the first real non comment token
} catch (CoreException e) {
// ignore
}
return memberStartOffset;
}
TokenScannerってやつにまかせてるんですね:-)。
***ソースコードを開く。 [#s8bdedd8]
unitがICompilationUnitだとして
// open the editor, forces the creation of a working copy
IEditorPart editor = JavaUI.openInEditor(unit);
でエディタでソースコードを開くことができます。ICompilatio...
IEditorPart editor = JavaUI.openInEditor(element.getAnce...
などでIJavaElementからICompilationUnitを取得して開くこと...
**関連リンク [#w3f6351b]
-Eclipse/プラグイン開発のTIPS集/Javaプロジェクトを操作す...
-Eclipse/プラグイン開発のTIPS集/ソースコードを解析するパ...
----
この記事は
#vote(おもしろかった[2],そうでもない[0])
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}
ページ名: