1 11 package org.eclipse.jdt.internal.corext.javadoc; 12 13 14 import java.io.IOException ; 15 16 import org.eclipse.core.runtime.IStatus; 17 18 import org.eclipse.jdt.core.IBuffer; 19 import org.eclipse.jdt.core.IJavaElement; 20 import org.eclipse.jdt.core.IMember; 21 import org.eclipse.jdt.core.IMethod; 22 import org.eclipse.jdt.core.ISourceRange; 23 import org.eclipse.jdt.core.IType; 24 import org.eclipse.jdt.core.ITypeHierarchy; 25 import org.eclipse.jdt.core.JavaModelException; 26 import org.eclipse.jdt.core.ToolFactory; 27 import org.eclipse.jdt.core.compiler.IScanner; 28 import org.eclipse.jdt.core.compiler.ITerminalSymbols; 29 import org.eclipse.jdt.core.compiler.InvalidInputException; 30 31 import org.eclipse.jdt.internal.corext.dom.TokenScanner; 32 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 33 34 35 public class JavaDocAccess { 36 37 38 39 40 47 public static JavaDocCommentReader getJavaDoc(IMember member, boolean allowInherited) throws JavaModelException { 48 IBuffer buf= member.isBinary() ? member.getClassFile().getBuffer() : member.getCompilationUnit().getBuffer(); 49 if (buf == null) { 50 return null; 52 } 53 ISourceRange range= member.getSourceRange(); 54 int start= range.getOffset(); 55 int length= range.getLength(); 56 if (length > 0 && buf.getChar(start) == '/') { 57 IScanner scanner= ToolFactory.createScanner(true, false, false, false); 58 scanner.setSource(buf.getCharacters()); 59 scanner.resetTo(start, start + length - 1); 60 try { 61 int docOffset= -1; 62 int docEnd= -1; 63 64 int terminal= scanner.getNextToken(); 65 while (TokenScanner.isComment(terminal)) { 66 if (terminal == ITerminalSymbols.TokenNameCOMMENT_JAVADOC) { 67 docOffset= scanner.getCurrentTokenStartPosition(); 68 docEnd= scanner.getCurrentTokenEndPosition() + 1; 69 } 70 terminal= scanner.getNextToken(); 71 } 72 if (docOffset != -1) { 73 return new JavaDocCommentReader(buf, docOffset, docEnd); 74 } 75 } catch (InvalidInputException ex) { 76 } 78 79 } 80 if (allowInherited && (member.getElementType() == IJavaElement.METHOD)) { 81 IMethod method= (IMethod) member; 82 return findDocInHierarchy(method.getDeclaringType(), method.getElementName(), method.getParameterTypes(), method.isConstructor()); 83 } 84 return null; 85 } 86 87 88 89 94 public static JavaDocCommentReader getJavaDoc(IMember member) throws JavaModelException { 95 return getJavaDoc(member, false); 96 } 97 98 private static JavaDocCommentReader findDocInHierarchy(IType type, String name, String [] paramTypes, boolean isConstructor) throws JavaModelException { 99 ITypeHierarchy hierarchy= type.newSupertypeHierarchy(null); 100 IType[] superTypes= hierarchy.getAllSupertypes(type); 101 for (int i= 0; i < superTypes.length; i++) { 102 IMethod method= JavaModelUtil.findMethod(name, paramTypes, isConstructor, superTypes[i]); 103 if (method != null) { 104 JavaDocCommentReader reader= getJavaDoc(method, false); 105 if (reader != null) { 106 return reader; 107 } 108 } 109 } 110 return null; 111 } 112 113 114 119 public static String getJavaDocTextString(IMember member, boolean allowInherited) throws JavaModelException { 120 try { 121 SingleCharReader rd= getJavaDoc(member, allowInherited); 122 if (rd != null) 123 return rd.getString(); 124 125 } catch (IOException e) { 126 throw new JavaModelException(e, IStatus.ERROR); 127 } 128 129 return null; 130 } 131 132 133 } 134 | Popular Tags |