1 11 package org.eclipse.jdt.ui; 12 13 import java.io.IOException ; 14 import java.io.Reader ; 15 import java.io.StringReader ; 16 17 import org.eclipse.jdt.core.IBuffer; 18 import org.eclipse.jdt.core.IJavaElement; 19 import org.eclipse.jdt.core.IMember; 20 import org.eclipse.jdt.core.IMethod; 21 import org.eclipse.jdt.core.ISourceRange; 22 import org.eclipse.jdt.core.IType; 23 import org.eclipse.jdt.core.ITypeHierarchy; 24 import org.eclipse.jdt.core.JavaModelException; 25 26 import org.eclipse.jdt.internal.corext.javadoc.JavaDocCommentReader; 27 import org.eclipse.jdt.internal.corext.util.MethodOverrideTester; 28 29 import org.eclipse.jdt.internal.ui.text.javadoc.JavaDoc2HTMLTextReader; 30 31 40 public class JavadocContentAccess { 41 42 private JavadocContentAccess() { 43 } 45 46 57 public static Reader getContentReader(IMember member, boolean allowInherited) throws JavaModelException { 58 IBuffer buf= member.getOpenable().getBuffer(); 59 if (buf == null) { 60 return null; } 62 63 ISourceRange javadocRange= member.getJavadocRange(); 64 if (javadocRange != null) { 65 JavaDocCommentReader reader= new JavaDocCommentReader(buf, javadocRange.getOffset(), javadocRange.getOffset() + javadocRange.getLength() - 1); 66 if (!containsOnlyInheritDoc(reader, javadocRange.getLength())) { 67 reader.reset(); 68 return reader; 69 } 70 } 71 72 if (allowInherited && (member.getElementType() == IJavaElement.METHOD)) { 73 return findDocInHierarchy((IMethod) member); 74 } 75 76 return null; 77 } 78 79 88 private static boolean containsOnlyInheritDoc(Reader reader, int length) { 89 char[] content= new char[length]; 90 try { 91 reader.read(content, 0, length); 92 } catch (IOException e) { 93 return false; 94 } 95 return new String (content).trim().equals("{@inheritDoc}"); 97 } 98 99 114 public static Reader getHTMLContentReader(IMember member, boolean allowInherited, boolean useAttachedJavadoc) throws JavaModelException { 115 Reader contentReader= getContentReader(member, allowInherited); 116 if (contentReader != null) 117 return new JavaDoc2HTMLTextReader(contentReader); 118 119 if (useAttachedJavadoc && member.getOpenable().getBuffer() == null) { String s= member.getAttachedJavadoc(null); 121 if (s != null) 122 return new StringReader (s); 123 } 124 return null; 125 } 126 127 128 129 142 public static Reader getHTMLContentReader(IMember member, boolean allowInherited) throws JavaModelException { 143 return getHTMLContentReader(member, allowInherited, false); 144 } 145 146 private static Reader findDocInHierarchy(IMethod method) throws JavaModelException { 147 IType type= method.getDeclaringType(); 148 ITypeHierarchy hierarchy= type.newSupertypeHierarchy(null); 149 150 MethodOverrideTester tester= new MethodOverrideTester(type, hierarchy); 151 152 IType[] superTypes= hierarchy.getAllSupertypes(type); 153 for (int i= 0; i < superTypes.length; i++) { 154 IType curr= superTypes[i]; 155 IMethod overridden= tester.findOverriddenMethodInType(curr, method); 156 if (overridden != null) { 157 Reader reader= getContentReader(overridden, false); 158 if (reader != null) { 159 return reader; 160 } 161 } 162 } 163 return null; 164 } 165 166 } 167 | Popular Tags |