KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > structure > JavaElementCommentFinder


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.refactoring.structure;
12
13 import org.eclipse.jdt.core.IMember;
14 import org.eclipse.jdt.core.IOpenable;
15 import org.eclipse.jdt.core.ISourceRange;
16 import org.eclipse.jdt.core.JavaModelException;
17 import org.eclipse.jdt.core.ToolFactory;
18 import org.eclipse.jdt.core.compiler.IScanner;
19 import org.eclipse.jdt.core.compiler.InvalidInputException;
20
21 import org.eclipse.jdt.internal.corext.SourceRange;
22 import org.eclipse.jdt.internal.corext.dom.TokenScanner;
23
24
25 public class JavaElementCommentFinder {
26     private JavaElementCommentFinder(){}
27     
28     /**
29      * returns <code>null</code> if there's no comment or member is not declared inside of a
30      * <code>ICompilationUnit</code> or <code>IClassFile</code>
31      */

32     public static String JavaDoc getCommentContent(IMember member) throws JavaModelException{
33         if (member.getCompilationUnit() != null)
34             return getBufferContents(member.getCompilationUnit(), getCommentRange(member));
35         else if (member.getClassFile() != null)
36             return getBufferContents(member.getClassFile(), getCommentRange(member));
37         else
38             return null;
39     }
40
41     private static String JavaDoc getBufferContents(IOpenable element, ISourceRange range) throws JavaModelException{
42         if (range == null)
43             return null;
44         return element.getBuffer().getText(range.getOffset(), range.getLength());
45     }
46     
47     /**
48      * returns <code>null</code> if there's no comment or member is not declared inside of a
49      * <code>ICompilationUnit</code> or <code>IClassFile</code>
50      */

51     public static ISourceRange getCommentRange(IMember member) throws JavaModelException{
52         IScanner scanner= ToolFactory.createScanner(true, false, false, false);
53         scanner.setSource(member.getSource().toCharArray());
54         try {
55             int firstToken= scanner.getNextToken();
56             if (! TokenScanner.isComment(firstToken))
57                 return null;
58             return new SourceRange(member.getSourceRange().getOffset() + scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition() - scanner.getCurrentTokenStartPosition() + 1);
59         } catch (InvalidInputException e) {
60             return null;
61         }
62     }
63 }
64
Popular Tags