KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > javadoc > JavaDocAccess


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.javadoc;
12
13  
14 import java.io.IOException JavaDoc;
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     /**
41      * Gets a reader for an IMember's JavaDoc comment
42      * Returns null if the member does not contain a JavaDoc comment or
43      * if no source is available.
44      * @param allowInherited For methods with no comment, the comment of the overriden class
45      * is returned if <code>allowInherited</code> is <code>true</code>.
46      */

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             // no source attachment found
51
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                 // try if there is inherited Javadoc
77
}
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     /**
90      * Gets a reader for an IMember's JavaDoc comment
91      * Returns null if the member does not contain a JavaDoc comment or
92      * if no source is available.
93      */

94     public static JavaDocCommentReader getJavaDoc(IMember member) throws JavaModelException {
95         return getJavaDoc(member, false);
96     }
97
98     private static JavaDocCommentReader findDocInHierarchy(IType type, String JavaDoc name, String JavaDoc[] 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     /**
115      * Gets a text content for an IMember's JavaDoc comment
116      * Returns null if the member does not contain a JavaDoc comment or
117      * if no source is available.
118      */

119     public static String JavaDoc 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 JavaDoc e) {
126             throw new JavaModelException(e, IStatus.ERROR);
127         }
128         
129         return null;
130     }
131     
132
133 }
134
Popular Tags