|
org.eclipse.jdt.core.ICompilationUnit? ¤Ï¥Ñ¥Ã¥±¡¼¥¸¥¨¥¯¥¹¥×¥í¡¼¥é¾å¤Î¥½¡¼¥¹¥³¡¼¥É°ì¤Ä¤ËÂбþ¤¹¤ë¤È¤¤¤¦¤Ù¤¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Ç¤¹¡£¤³¤Î¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò¥Ñ¥Ã¥±¡¼¥¸¥¨¥¯¥¹¥×¥í¡¼¥é¤Ê¤É¤«¤é¥¢¥¯¥·¥ç¥ó·Ðͳ¤Ç¼èÆÀ¤·¤Æ¡¢¥½¡¼¥¹¥³¡¼¥É¤Î²òÀϤʤɤò¹Ô¤¦¤³¤È¤¬¤Ç¤¤Þ¤¹¡£ ¤ä¤Ã¤Æ¤ß¤ë †¼¡¤Î¥µ¥ó¥×¥ë¥³¡¼¥É¤Ï¡¢Eclipse¤Î¥¢¥¯¥·¥ç¥ó¥¯¥é¥¹(¥Ï¥ó¥É¥é¥¯¥é¥¹)·Ðͳ¤Ç¥Ñ¥Ã¥±¡¼¥¸¥¨¥¯¥¹¥×¥í¡¼¥é¤«¤éICompilationUnit?¤ò¼èÆÀ¤·¡¢¤½¤Î¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Î¼ÂÁõ¥á¥½¥Ã¥É IJavaElement[] getChildren() throws JavaModelException; ¤ò»È¤Ã¤Æ¡¢¥³¡¼¥É¤ò²òÀϤ·¤Æ¤¤¤¤Þ¤¹¡£ public Object execute(ExecutionEvent event) throws ExecutionException {
ISelection selection = HandlerUtil.getActiveMenuSelectionChecked(event);
if (selection instanceof IStructuredSelection) {
IStructuredSelection sselection = (IStructuredSelection) selection;
Object firstElement = sselection.getFirstElement();
if (firstElement instanceof ICompilationUnit) {
ICompilationUnit unit = (ICompilationUnit) firstElement;
try {
if (!unit.isStructureKnown()) {
return null;
}
// ¤Þ¤º¤Ï»ÒÍ×ÁǤò¼èÆÀ¡£¼èÆÀ¤µ¤ì¤ë¤Î¤Ï¡¢¥Ñ¥Ã¥±¡¼¥¸Àë¸À¤È¤«¡¢¥¯¥é¥¹·¿¤È¤«
// ¥¯¥é¥¹¤¬Ê£¿ôÄêµÁ¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤â¤¢¤ë¤·¡£
IJavaElement elements[] = unit.getChildren();
for (IJavaElement javaElement : elements) {
// ¢·¿¤À¤Ã¤¿¤é¤Ð¡¢IType¤Ë¥¥ã¥¹¥È¡£
if (javaElement.getElementType() == IJavaElement.TYPE) {
IType type = (IType) javaElement;
// ¥á¥½¥Ã¥É°ìÍ÷¤ò¼èÆÀ¡£
IMethod[] methods = type.getMethods();
// IMethod method = methods[methods.length - 1];
for (IMethod method : methods) {
System.out.println(method.getElementName());
}
}
}
} catch (JavaModelException e) {
e.printStackTrace();
}
}
}
return null;
}
TIPS½¸ †ÇÛ²¼¤ÎIJavaElement?¤ÎÊÑ¿ô¤«¤é¡¢ICompilationUnit? ¤ò¼èÆÀ¤¹¤ë¡£ †IType¤ÊIJavaElement?¤ÎÊÑ¿ô¤È¤«¤«¤é¤½¤ì¤òÊñ´Þ¤·¤Æ¤¤¤ëICompilationUnit? ¤ò¼èÆÀ¤¹¤ëÊýË¡¤Ç¤¹¡£ ICompilationUnit unit = (ICompilationUnit) element
.getAncestor(IJavaElement.COMPILATION_UNIT);
¥½¡¼¥¹¥³¡¼¥É¤ÎIResource¥¤¥ó¥¹¥¿¥ó¥¹¤ò¼èÆÀ¤¹¤ë¡£ †JDT¼þ¤ê¤ò¤ä¤Ã¤Æ¤ë¤ÈIResouce·Ï¤ò˺¤ì¤Æ¤·¤Þ¤¤¤½¤¦¤Ç¤¹¤¬¡¢¤¢¤ê¤Þ¤·¤¿¡£ // ¥½¡¼¥¹¥³¡¼¥É¤ËÂбþ¤¹¤ë¥Õ¥¡¥¤¥ë¤ÎIResource IResource resource = cu.getCorrespondingResource(); ¤³¤ì¤ÇOK¤Ç¤¹¤Í¡£ ¤¢¤ëÍ×ÁÇ(¥Õ¥£¡¼¥ë¥É¤È¤«¡¢¥á¥½¥Ã¥É¤È¤«)¤Î¥ª¥Õ¥»¥Ã¥È°ÌÃÖ¤ò¼èÆÀ¤¹¤ë †Eclipse¤ÎJDT¤Î¥½¡¼¥¹¸«¤Æ¤ë¤È¡¢¥Õ¥¡¥¤¥ë¤ÎÀèÆ¬¤«¤é¤Îʸ»ú¿ô¤ò¥«¥¦¥ó¥È¤·¤Æ¡¢¤½¤Î¾ì½ê¤Ë¥³¡¼¥ÉÁÞÆþ¤¹¤ë¤È¤«¡¢³ä¤ÈÃÏÌ£¡¼¤Ê¥³¡¼¥É¤¬¤è¤¯¤¢¤ê¤Þ¤¹¡£ ¤½¤ì¤ò¼ÂÁõ¤¹¤ë´ðËܤȤ·¤Æ¡¢¥½¡¼¥¹¤ÎÀèÆ¬¤«¤é¤¢¤ëÍ×ÁÇ(¤³¤³¤Ç¤Ï¥á¥½¥Ã¥É)¤Þ¤Ç¤Î¥ª¥Õ¥»¥Ã¥È*1°ÌÃÖ¤ò¼èÆÀ¤·¤Æ¤ß¤Þ¤¹¡£²òÀÏÂоݤΥ½¡¼¥¹¤Ï°Ê²¼¤ÎÄ̤ê¤Ç¤¹¡£
¥ª¥Õ¥»¥Ã¥È¤ò¼èÆÀ¤¹¤ë¥³¡¼¥É¤Ï°Ê²¼¤ÎÄ̤ꡣ public Object execute(ExecutionEvent event) throws ExecutionException {
--- Àè¤ÈƱ¤¸¤Ê¤Î¤Ç¡¢¾Êά ---
if (firstElement instanceof ICompilationUnit) {
ICompilationUnit unit = (ICompilationUnit) firstElement;
try {
if (!unit.isStructureKnown()) {
return null;
}
IJavaElement elements[] = unit.getChildren();
for (IJavaElement javaElement : elements) {
if (javaElement.getElementType() == IJavaElement.TYPE) {
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¤Ç¤Ï¤É¤¦¤ä¤Ã¤Æ¤ë¤« †¤Ê¤ë¤Û¤É¤Í¡¼¤È»×¤Ã¤Æ¾å¤Î¤ä¤êÊý¤ò¸«¤Æ¤¿¤Î¤Ç¤¹¤¬¡¢¥³¥á¥ó¥Èʸ¤È¤«¤¬¤¢¤ë¾ì¹ç¤Ï¡¢¥á¥½¥Ã¥É¤ÎÀèÆ¬¤Î¥ª¥Õ¥»¥Ã¥È°ÌÃÖ¤ò¼è¤í¤¦¤È¥á¥½¥Ã¥É¤ò¼Â¹Ô¤¹¤ë¤È¡¢¥³¥á¥ó¥È¤Î¾åÉôʬ¤Î°ÌÃÖ¤òÊÖ¤·¤Æ¤¤Þ¤¹¡£ ¥³¥á¥ó¥È¤Î²¼¤Î¡¢¥Û¥ó¥È¤Ë¥á¥½¥Ã¥É¤Î³«»Ï¤Ï¤É¤¦¼è¤ë¤ó¤À¤í¤¦¤ÈEclipse JDT¤Î¥½¡¼¥¹¤ò¸«¤Æ¤ß¤ë¤È¡¢TokenScanner? ¤È¤¤¤¦Internal¤Ê¥¯¥é¥¹¤ò»È¤Ã¤Æ¼ÂÁõ¤¬¤µ¤ì¤Æ¤¤¤Þ¤·¤¿¡£ private int getMemberStartOffset(IMethod lastMethod, IDocument document)
throws JavaModelException {
ISourceRange sourceRange = lastMethod.getSourceRange();
int memberStartOffset = sourceRange.getOffset();
// Ä̾ï¤Ï¾å¤Î¥ª¥Õ¥»¥Ã¥È¤òÊÖ¤¹¤Î¤ÇÌäÂê¤Ê¤¤¤±¤É¡¢¥³¥á¥ó¥È¤È¤«¤¬¤¢¤ë¤È¡¢¥³¥á¥ó¥È¤Î¾å¤Î°ÌÃÖ¤òÊÖ¤·¤Æ¤¯¤ë¡£
// ¥Û¥ó¥È¤Ë¥á¥½¥Ã¥É¤Îľ¾å¤Ë¤¹¤ë¾ì¹ç¤Ï¡¢¤³¤Î¤è¤¦¤Ë¤¹¤ëɬÍפ¬¤¢¤ë¡£
TokenScanner scanner = new TokenScanner(document, lastMethod.getJavaProject());
try {
return scanner.getNextStartOffset(memberStartOffset, true); // read
// to the first real non comment token
} catch (CoreException e) {
// ignore
}
return memberStartOffset;
}
TokenScanner?¤Ã¤Æ¤ä¤Ä¤Ë¤Þ¤«¤»¤Æ¤ë¤ó¤Ç¤¹¤Í:-)¡£ ¥½¡¼¥¹¥³¡¼¥É¤ò³«¤¯¡£ †unit¤¬ICompilationUnit?¤À¤È¤·¤Æ // open the editor, forces the creation of a working copy IEditorPart editor = JavaUI.openInEditor(unit); ¤Ç¥¨¥Ç¥£¥¿¤Ç¥½¡¼¥¹¥³¡¼¥É¤ò³«¤¯¤³¤È¤¬¤Ç¤¤Þ¤¹¡£ICompilationUnit?¤Ø¤Î¥Ý¥¤¥ó¥¿¤¬¤Ê¤«¤Ã¤¿¤È¤·¤Æ¤â¡¢ IEditorPart editor = JavaUI.openInEditor(element.getAncestor(IJavaElement.COMPILATION_UNIT)); ¤Ê¤É¤ÇIJavaElement?¤«¤éICompilationUnit?¤ò¼èÆÀ¤·¤Æ³«¤¯¤³¤È¤¬¤Ç¤¤Þ¤¹¤Í¡£ ´ØÏ¢¥ê¥ó¥¯ †
¤³¤Îµ»ö¤Ï ¸½ºß¤Î¥¢¥¯¥»¥¹:4442 |