1 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 32 public static String 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 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 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 |