KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > util > CommentAnalyzer


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

11 package org.eclipse.jdt.internal.corext.refactoring.util;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
16
17 import org.eclipse.jdt.core.ToolFactory;
18 import org.eclipse.jdt.core.compiler.IScanner;
19 import org.eclipse.jdt.core.compiler.ITerminalSymbols;
20 import org.eclipse.jdt.core.compiler.InvalidInputException;
21
22 import org.eclipse.jdt.internal.corext.dom.Selection;
23 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
24
25 public class CommentAnalyzer {
26     
27     private CommentAnalyzer() {
28     }
29     
30     public static RefactoringStatus perform(Selection selection, IScanner scanner, int start, int length) {
31         RefactoringStatus result= new RefactoringStatus();
32         if (length <= 0)
33             return result;
34         new CommentAnalyzer().check(result, selection, scanner, start, start + length - 1);
35         return result;
36     }
37     
38     private void check(RefactoringStatus result, Selection selection, IScanner scanner, int start, int end) {
39         char[] characters= scanner.getSource();
40         selection= adjustSelection(characters, selection, end);
41         scanner.resetTo(start, end);
42         
43         int token= 0;
44         try {
45             loop: while (token != ITerminalSymbols.TokenNameEOF) {
46                 token= scanner.getNextToken();
47                 switch(token) {
48                     case ITerminalSymbols.TokenNameCOMMENT_LINE:
49                     case ITerminalSymbols.TokenNameCOMMENT_BLOCK:
50                     case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
51                         if (checkStart(scanner, selection.getOffset())) {
52                             result.addFatalError(RefactoringCoreMessages.CommentAnalyzer_starts_inside_comment);
53                             break loop;
54                         }
55                         if (checkEnd(scanner, selection.getInclusiveEnd())) {
56                             result.addFatalError(RefactoringCoreMessages.CommentAnalyzer_ends_inside_comment);
57                             break loop;
58                         }
59                         break;
60                 }
61             }
62         } catch (InvalidInputException e) {
63             result.addFatalError(RefactoringCoreMessages.CommentAnalyzer_internal_error);
64         }
65     }
66     
67     private boolean checkStart(IScanner scanner, int position) {
68         return scanner.getCurrentTokenStartPosition() < position && position <= scanner.getCurrentTokenEndPosition();
69     }
70
71     private boolean checkEnd(IScanner scanner, int position) {
72         return scanner.getCurrentTokenStartPosition() <= position && position < scanner.getCurrentTokenEndPosition();
73     }
74     
75     private Selection adjustSelection(char[] characters, Selection selection, int end) {
76         int newEnd= selection.getInclusiveEnd();
77         for(int i= selection.getExclusiveEnd(); i <= end; i++) {
78             char ch= characters[i];
79             if (ch != '\n' && ch != '\r')
80                 break;
81             newEnd++;
82         }
83         return Selection.createFromStartEnd(selection.getOffset(), newEnd);
84     }
85     
86     /**
87      * Removes comments and whitespace
88      * @param reference the type reference
89      * @return the reference only consisting of dots and java identifier characters
90      */

91     public static String JavaDoc normalizeReference(String JavaDoc reference) {
92         IScanner scanner= ToolFactory.createScanner(false, false, false, false);
93         scanner.setSource(reference.toCharArray());
94         StringBuffer JavaDoc sb= new StringBuffer JavaDoc();
95         try {
96             int tokenType= scanner.getNextToken();
97             while (tokenType != ITerminalSymbols.TokenNameEOF) {
98                 sb.append(scanner.getRawTokenSource());
99                 tokenType= scanner.getNextToken();
100             }
101         } catch (InvalidInputException e) {
102             Assert.isTrue(false, reference);
103         }
104         reference= sb.toString();
105         return reference;
106     }
107 }
108
Popular Tags