@Override public List<String> tokenize(String source) { try { char[] charArray = source.toCharArray(); scanner.setSource(source.toCharArray()); List<String> tokens = new ArrayList<>(); int token; while ((token = scanner.getNextToken()) != ITerminalSymbols.TokenNameEOF) { int tokenStart = scanner.getCurrentTokenStartPosition(); int tokenEnd = scanner.getCurrentTokenEndPosition(); if (token != ITerminalSymbols.TokenNameWHITESPACE) { tokens.add(new String(charArray, tokenStart, tokenEnd - tokenStart + 1)); } } return tokens; } catch (InvalidInputException e) { throw new RuntimeException(e); } }
/** * Method to check with methods share common attributes, according to * CK definition. * @author Mariana Azevedo * @since 13/07/2014 * @param methods */ private void checkMethodsWithSharedAttributes(IMethod[] methods){ IScanner scanner = null; for (IMethod method : methods) { String methodName = method.getElementName(); try { scanner = ToolFactory.createScanner(false, false, false, false); scanner.setSource(method.getSource().toCharArray()); while(true){ int charactere = scanner.getNextToken(); if (charactere == ITerminalSymbols.TokenNameEOF) break; if (charactere == ITerminalSymbols.TokenNameIdentifier) { addMethods(new String(scanner.getCurrentTokenSource()), methodName); } } } catch (JavaModelException exception1) { logger.error(exception1); } catch (InvalidInputException exception2) { logger.error(exception2); } } }
public static boolean isModifier(int token) { switch (token) { case ITerminalSymbols.TokenNamepublic: case ITerminalSymbols.TokenNameprotected: case ITerminalSymbols.TokenNameprivate: case ITerminalSymbols.TokenNamestatic: case ITerminalSymbols.TokenNamefinal: case ITerminalSymbols.TokenNameabstract: case ITerminalSymbols.TokenNamenative: case ITerminalSymbols.TokenNamevolatile: case ITerminalSymbols.TokenNamestrictfp: case ITerminalSymbols.TokenNametransient: case ITerminalSymbols.TokenNamesynchronized: return true; default: return false; } }
@Override public boolean visit(DoStatement node) { boolean result = super.visit(node); try { int actionStart = getTokenScanner().getTokenEndOffset(ITerminalSymbols.TokenNamedo, node.getStartPosition()); if (getSelection().getOffset() == actionStart) { invalidSelection(RefactoringCoreMessages.ExtractMethodAnalyzer_after_do_keyword, JavaStatusContext.create(fCUnit, getSelection())); return false; } } catch (CoreException e) { // ignore } return result; }
/** * Removes comments and whitespace * * @param reference * the type reference * @return the reference only consisting of dots and java identifier * characters */ public static String normalizeReference(String reference) { IScanner scanner = ToolFactory.createScanner(false, false, false, false); scanner.setSource(reference.toCharArray()); StringBuffer sb = new StringBuffer(); try { int tokenType = scanner.getNextToken(); while (tokenType != ITerminalSymbols.TokenNameEOF) { sb.append(scanner.getRawTokenSource()); tokenType = scanner.getNextToken(); } } catch (InvalidInputException e) { Assert.isTrue(false, reference); } reference = sb.toString(); return reference; }
private static boolean isJustWhitespaceOrComment(int start, int end, IBuffer buffer) { if (start == end) return true; Assert.isTrue(start <= end); String trimmedText = buffer.getText(start, end - start).trim(); if (0 == trimmedText.length()) { return true; } else { IScanner scanner = ToolFactory.createScanner(false, false, false, null); scanner.setSource(trimmedText.toCharArray()); try { return scanner.getNextToken() == ITerminalSymbols.TokenNameEOF; } catch (InvalidInputException e) { return false; } } }
private void doScan() { try { int token = fScanner.getNextToken(); while (token != ITerminalSymbols.TokenNameEOF) { switch (token) { case ITerminalSymbols.TokenNameStringLiteral: case ITerminalSymbols.TokenNameCOMMENT_JAVADOC: case ITerminalSymbols.TokenNameCOMMENT_LINE: case ITerminalSymbols.TokenNameCOMMENT_BLOCK: parseCurrentToken(); } token = fScanner.getNextToken(); } } catch (InvalidInputException e) { // ignore } }
@Override public boolean visit(DoStatement node) { boolean result = super.visit(node); try { int actionStart = getTokenScanner() .getTokenEndOffset(ITerminalSymbols.TokenNamedo, node.getStartPosition()); if (getSelection().getOffset() == actionStart) { invalidSelection( RefactoringCoreMessages.ExtractMethodAnalyzer_after_do_keyword, JavaStatusContext.create(fCUnit, getSelection())); return false; } } catch (CoreException e) { // ignore } return result; }
/** * Removes comments and whitespace * * @param reference the type reference * @return the reference only consisting of dots and java identifier characters */ public static String normalizeReference(String reference) { IScanner scanner = ToolFactory.createScanner(false, false, false, false); scanner.setSource(reference.toCharArray()); StringBuffer sb = new StringBuffer(); try { int tokenType = scanner.getNextToken(); while (tokenType != ITerminalSymbols.TokenNameEOF) { sb.append(scanner.getRawTokenSource()); tokenType = scanner.getNextToken(); } } catch (InvalidInputException e) { Assert.isTrue(false, reference); } reference = sb.toString(); return reference; }
private int getSurroundingComment(IScanner scanner) { try { int start = fLocation.getOffset(); int end = start + fLocation.getLength(); int token = scanner.getNextToken(); while (token != ITerminalSymbols.TokenNameEOF) { if (TokenScanner.isComment(token)) { int currStart = scanner.getCurrentTokenStartPosition(); int currEnd = scanner.getCurrentTokenEndPosition() + 1; if (currStart <= start && end <= currEnd) { return token; } } token = scanner.getNextToken(); } } catch (InvalidInputException e) { // ignore } return ITerminalSymbols.TokenNameEOF; }
@Override public List<FullToken> getTokenListFromCode(final char[] code) { final List<FullToken> tokens = Lists.newArrayList(); tokens.add(new FullToken(SENTENCE_START, SENTENCE_START)); final PublicScanner scanner = prepareScanner(code); do { try { final int token = scanner.getNextToken(); if (token == ITerminalSymbols.TokenNameEOF) { break; } for (final String cToken : getConvertedToken(scanner, token)) { tokens.add(new FullToken(cToken, "")); } } catch (final InvalidInputException e) { LOGGER.warning(ExceptionUtils.getFullStackTrace(e)); } } while (!scanner.atEnd()); tokens.add(new FullToken(SENTENCE_END, SENTENCE_END)); return tokens; }
@Override public List<String> tokenListFromCode(final char[] code) { final List<String> tokens = Lists.newArrayList(); tokens.add(SENTENCE_START); final PublicScanner scanner = prepareScanner(code); do { try { final int token = scanner.getNextToken(); if (token == ITerminalSymbols.TokenNameEOF) { break; } tokens.addAll(getConvertedToken(scanner, token)); } catch (final InvalidInputException e) { LOGGER.warning(ExceptionUtils.getFullStackTrace(e)); } } while (!scanner.atEnd()); tokens.add(SENTENCE_END); return tokens; }
@Override public SortedMap<Integer, String> tokenListWithPos(final char[] code) { final PublicScanner scanner = prepareScanner(); final SortedMap<Integer, String> tokens = Maps.newTreeMap(); tokens.put(-1, SENTENCE_START); tokens.put(Integer.MAX_VALUE, SENTENCE_END); scanner.setSource(code); while (!scanner.atEnd()) { do { try { final int token = scanner.getNextToken(); if (token == ITerminalSymbols.TokenNameEOF) { break; } final String nxtToken = transformToken(token, scanner.getCurrentTokenString()); final int position = scanner.getCurrentTokenStartPosition(); tokens.put(position, stripTokenIfNeeded(nxtToken)); } catch (final InvalidInputException e) { LOGGER.warning(ExceptionUtils.getFullStackTrace(e)); } } while (!scanner.atEnd()); } return tokens; }
private void doScan() { try{ int token = fScanner.getNextToken(); while (token != ITerminalSymbols.TokenNameEOF) { switch (token) { case ITerminalSymbols.TokenNameStringLiteral : case ITerminalSymbols.TokenNameCOMMENT_JAVADOC : case ITerminalSymbols.TokenNameCOMMENT_LINE : case ITerminalSymbols.TokenNameCOMMENT_BLOCK : parseCurrentToken(); } token = fScanner.getNextToken(); } } catch (InvalidInputException e){ //ignore } }
@Override public boolean visit(DoStatement node) { boolean result= super.visit(node); try { int actionStart= getTokenScanner().getTokenEndOffset(ITerminalSymbols.TokenNamedo, node.getStartPosition()); if (getSelection().getOffset() == actionStart) { invalidSelection(RefactoringCoreMessages.ExtractMethodAnalyzer_after_do_keyword, JavaStatusContext.create(fCUnit, getSelection())); return false; } } catch (CoreException e) { // ignore } return result; }
/** * Removes comments and whitespace * @param reference the type reference * @return the reference only consisting of dots and java identifier characters */ public static String normalizeReference(String reference) { IScanner scanner= ToolFactory.createScanner(false, false, false, false); scanner.setSource(reference.toCharArray()); StringBuffer sb= new StringBuffer(); try { int tokenType= scanner.getNextToken(); while (tokenType != ITerminalSymbols.TokenNameEOF) { sb.append(scanner.getRawTokenSource()); tokenType= scanner.getNextToken(); } } catch (InvalidInputException e) { Assert.isTrue(false, reference); } reference= sb.toString(); return reference; }
private static boolean isJustWhitespaceOrComment(int start, int end, IBuffer buffer) { if (start == end) return true; Assert.isTrue(start <= end); String trimmedText= buffer.getText(start, end - start).trim(); if (0 == trimmedText.length()) { return true; } else { IScanner scanner= ToolFactory.createScanner(false, false, false, null); scanner.setSource(trimmedText.toCharArray()); try { return scanner.getNextToken() == ITerminalSymbols.TokenNameEOF; } catch (InvalidInputException e) { return false; } } }
private int getSurroundingComment(IScanner scanner) { try { int start= fLocation.getOffset(); int end= start + fLocation.getLength(); int token= scanner.getNextToken(); while (token != ITerminalSymbols.TokenNameEOF) { if (TokenScanner.isComment(token)) { int currStart= scanner.getCurrentTokenStartPosition(); int currEnd= scanner.getCurrentTokenEndPosition() + 1; if (currStart <= start && end <= currEnd) { return token; } } token= scanner.getNextToken(); } } catch (InvalidInputException e) { // ignore } return ITerminalSymbols.TokenNameEOF; }
/** * @param source * must be not null * @param range * can be null * @return may return null, otherwise an initialized scanner which may * answer which source offset index belongs to which source line * @throws JavaModelException */ private static IScanner initScanner(IType source, ISourceRange range) throws JavaModelException { if (range == null) { return null; } char[] charContent = getContent(source); if (charContent == null) { return null; } IScanner scanner = ToolFactory.createScanner(false, false, false, true); scanner.setSource(charContent); int offset = range.getOffset(); try { while (scanner.getNextToken() != ITerminalSymbols.TokenNameEOF) { // do nothing, just wait for the end of stream if (offset <= scanner.getCurrentTokenEndPosition()) { break; } } } catch (InvalidInputException e) { FindbugsPlugin.getDefault().logException(e, "Could not init scanner for type: " + source); } return scanner; }
/** * Reads the next token. * @param ignoreComments If set, comments will be overread * @return Return the token id. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE) * or a lexical error was detected while scanning (code LEXICAL_ERROR) */ public int readNext(boolean ignoreComments) throws CoreException { int curr= 0; do { try { curr= fScanner.getNextToken(); if (curr == ITerminalSymbols.TokenNameEOF) { throw new CoreException(createError(END_OF_FILE, "End Of File", null)); //$NON-NLS-1$ } } catch (InvalidInputException e) { throw new CoreException(createError(LEXICAL_ERROR, e.getMessage(), e)); } } while (ignoreComments && isComment(curr)); return curr; }
/** * Evaluates the start offset of comments directly ahead of a token specified by its start offset * * @param lastPos An offset to before the node start offset. Can be 0 but better is the end location of the previous node. * @param nodeStart Start offset of the node to find the comments for. * @return Returns the start offset of comments directly ahead of a token. * @exception CoreException Thrown when a lexical error was detected while scanning (code LEXICAL_ERROR) */ public int getTokenCommentStart(int lastPos, int nodeStart) throws CoreException { setOffset(lastPos); int prevEndPos= lastPos; int prevEndLine= prevEndPos > 0 ? getLineOfOffset(prevEndPos - 1) : 0; int nodeLine= getLineOfOffset(nodeStart); int res= -1; int curr= readNextWithEOF(false); int currStartPos= getCurrentStartOffset(); int currStartLine= getLineOfOffset(currStartPos); while (curr != ITerminalSymbols.TokenNameEOF && nodeStart > currStartPos) { if (TokenScanner.isComment(curr)) { int linesDifference= currStartLine - prevEndLine; if ((linesDifference > 1) || (res == -1 && (linesDifference != 0 || nodeLine == currStartLine))) { res= currStartPos; // begin new } } else { res= -1; } if (curr == ITerminalSymbols.TokenNameCOMMENT_LINE) { prevEndLine= currStartLine; } else { prevEndLine= getLineOfOffset(getCurrentEndOffset() - 1); } curr= readNextWithEOF(false); currStartPos= getCurrentStartOffset(); currStartLine= getLineOfOffset(currStartPos); } if (res == -1 || curr == ITerminalSymbols.TokenNameEOF) { return nodeStart; } if (currStartLine - prevEndLine > 1) { return nodeStart; } return res; }
private boolean isValidComment(String template) { IScanner scanner = ToolFactory.createScanner(true, false, false, false); scanner.setSource(template.toCharArray()); try { int next = scanner.getNextToken(); while (TokenScanner.isComment(next)) { next = scanner.getNextToken(); } return next == ITerminalSymbols.TokenNameEOF; } catch (InvalidInputException e) { } return false; }
@Override public boolean visit(LambdaExpression node) { Selection selection = getSelection(); int selectionStart = selection.getOffset(); int selectionExclusiveEnd = selection.getExclusiveEnd(); int lambdaStart = node.getStartPosition(); int lambdaExclusiveEnd = lambdaStart + node.getLength(); ASTNode body = node.getBody(); int bodyStart = body.getStartPosition(); int bodyExclusiveEnd = bodyStart + body.getLength(); boolean isValidSelection = false; if ((body instanceof Block) && (bodyStart < selectionStart && selectionExclusiveEnd <= bodyExclusiveEnd)) { // if selection is inside lambda body's block isValidSelection = true; } else if (body instanceof Expression) { try { TokenScanner scanner = new TokenScanner(fCUnit); int arrowExclusiveEnd = scanner.getTokenEndOffset(ITerminalSymbols.TokenNameARROW, lambdaStart); if (selectionStart >= arrowExclusiveEnd) { isValidSelection = true; } } catch (CoreException e) { // ignore } } if (selectionStart <= lambdaStart && selectionExclusiveEnd >= lambdaExclusiveEnd) { // if selection covers the lambda node isValidSelection = true; } if (!isValidSelection) { return false; } return super.visit(node); }
private void check(RefactoringStatus result, Selection selection, IScanner scanner, int start, int end) { char[] characters = scanner.getSource(); selection = adjustSelection(characters, selection, end); scanner.resetTo(start, end); int token = 0; try { loop: while (token != ITerminalSymbols.TokenNameEOF) { token = scanner.getNextToken(); switch (token) { case ITerminalSymbols.TokenNameCOMMENT_LINE: case ITerminalSymbols.TokenNameCOMMENT_BLOCK: case ITerminalSymbols.TokenNameCOMMENT_JAVADOC: if (checkStart(scanner, selection.getOffset())) { result.addFatalError(RefactoringCoreMessages.CommentAnalyzer_starts_inside_comment); break loop; } if (checkEnd(scanner, selection.getInclusiveEnd())) { result.addFatalError(RefactoringCoreMessages.CommentAnalyzer_ends_inside_comment); break loop; } break; } } } catch (InvalidInputException e) { result.addFatalError(RefactoringCoreMessages.CommentAnalyzer_internal_error); } }
/** * Reads the next token. * * @param ignoreComments If set, comments will be overread * @return Return the token id. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE) or * a lexical error was detected while scanning (code LEXICAL_ERROR) */ public int readNext(boolean ignoreComments) throws CoreException { int curr = 0; do { try { curr = fScanner.getNextToken(); if (curr == ITerminalSymbols.TokenNameEOF) { throw new CoreException(createError(END_OF_FILE, "End Of File", null)); // $NON-NLS-1$ } } catch (InvalidInputException e) { throw new CoreException(createError(LEXICAL_ERROR, e.getMessage(), e)); } } while (ignoreComments && isComment(curr)); return curr; }
private int getLastSimpleNameStart(String reference) { fScanner.setSource(reference.toCharArray()); int lastIdentifierStart = -1; try { int tokenType = fScanner.getNextToken(); while (tokenType != ITerminalSymbols.TokenNameEOF) { if (tokenType == ITerminalSymbols.TokenNameIdentifier) lastIdentifierStart = fScanner.getCurrentTokenStartPosition(); tokenType = fScanner.getNextToken(); } } catch (InvalidInputException e) { JavaPlugin.log(e); } return lastIdentifierStart; }
private void check( RefactoringStatus result, Selection selection, IScanner scanner, int start, int end) { char[] characters = scanner.getSource(); selection = adjustSelection(characters, selection, end); scanner.resetTo(start, end); int token = 0; try { loop: while (token != ITerminalSymbols.TokenNameEOF) { token = scanner.getNextToken(); switch (token) { case ITerminalSymbols.TokenNameCOMMENT_LINE: case ITerminalSymbols.TokenNameCOMMENT_BLOCK: case ITerminalSymbols.TokenNameCOMMENT_JAVADOC: if (checkStart(scanner, selection.getOffset())) { result.addFatalError(RefactoringCoreMessages.CommentAnalyzer_starts_inside_comment); break loop; } if (checkEnd(scanner, selection.getInclusiveEnd())) { result.addFatalError(RefactoringCoreMessages.CommentAnalyzer_ends_inside_comment); break loop; } break; } } } catch (InvalidInputException e) { result.addFatalError(RefactoringCoreMessages.CommentAnalyzer_internal_error); } }