List of usage examples for org.eclipse.jdt.core.compiler IScanner getCurrentTokenSource
char[] getCurrentTokenSource();
From source file:com.android.ide.eclipse.adt.internal.refactorings.extractstring.ExtractStringRefactoring.java
License:Open Source License
/** * Try to find the selected Java element in the compilation unit. * * If selection matches a string literal, capture it, otherwise add a fatal error * to the status.//from w w w . ja va2s . c o m * * On success, advance the monitor by 3. * Returns status.isOK(). */ private boolean findSelectionInJavaUnit(ICompilationUnit unit, RefactoringStatus status, IProgressMonitor monitor) { try { IBuffer buffer = unit.getBuffer(); IScanner scanner = ToolFactory.createScanner(false, //tokenizeComments false, //tokenizeWhiteSpace false, //assertMode false //recordLineSeparator ); scanner.setSource(buffer.getCharacters()); monitor.worked(1); for (int token = scanner.getNextToken(); token != ITerminalSymbols.TokenNameEOF; token = scanner .getNextToken()) { if (scanner.getCurrentTokenStartPosition() <= mSelectionStart && scanner.getCurrentTokenEndPosition() >= mSelectionEnd) { // found the token, but only keep if the right type if (token == ITerminalSymbols.TokenNameStringLiteral) { mTokenString = new String(scanner.getCurrentTokenSource()); } break; } else if (scanner.getCurrentTokenStartPosition() > mSelectionEnd) { // scanner is past the selection, abort. break; } } } catch (JavaModelException e1) { // Error in unit.getBuffer. Ignore. } catch (InvalidInputException e2) { // Error in scanner.getNextToken. Ignore. } finally { monitor.worked(1); } if (mTokenString != null) { // As a literal string, the token should have surrounding quotes. Remove them. // Note: unquoteAttrValue technically removes either " or ' paired quotes, whereas // the Java token should only have " quotes. Since we know the type to be a string // literal, there should be no confusion here. mTokenString = unquoteAttrValue(mTokenString); // We need a non-empty string literal if (mTokenString.length() == 0) { mTokenString = null; } } if (mTokenString == null) { status.addFatalError("Please select a Java string literal."); } monitor.worked(1); return status.isOK(); }
From source file:com.android.ide.eclipse.adt.refactorings.extractstring.ExtractStringRefactoring.java
License:Open Source License
/** * Try to find the selected Java element in the compilation unit. * //w w w . ja va 2s . c o m * If selection matches a string literal, capture it, otherwise add a fatal error * to the status. * * On success, advance the monitor by 3. */ private boolean findSelectionInJavaUnit(ICompilationUnit unit, RefactoringStatus status, IProgressMonitor monitor) { try { IBuffer buffer = unit.getBuffer(); IScanner scanner = ToolFactory.createScanner(false, //tokenizeComments false, //tokenizeWhiteSpace false, //assertMode false //recordLineSeparator ); scanner.setSource(buffer.getCharacters()); monitor.worked(1); for (int token = scanner.getNextToken(); token != ITerminalSymbols.TokenNameEOF; token = scanner .getNextToken()) { if (scanner.getCurrentTokenStartPosition() <= mSelectionStart && scanner.getCurrentTokenEndPosition() >= mSelectionEnd) { // found the token, but only keep of the right type if (token == ITerminalSymbols.TokenNameStringLiteral) { mTokenString = new String(scanner.getCurrentTokenSource()); } break; } else if (scanner.getCurrentTokenStartPosition() > mSelectionEnd) { // scanner is past the selection, abort. break; } } } catch (JavaModelException e1) { // Error in unit.getBuffer. Ignore. } catch (InvalidInputException e2) { // Error in scanner.getNextToken. Ignore. } finally { monitor.worked(1); } if (mTokenString != null) { // As a literal string, the token should have surrounding quotes. Remove them. int len = mTokenString.length(); if (len > 0 && mTokenString.charAt(0) == '"' && mTokenString.charAt(len - 1) == '"') { mTokenString = mTokenString.substring(1, len - 1); } // We need a non-empty string literal if (mTokenString.length() == 0) { mTokenString = null; } } if (mTokenString == null) { status.addFatalError("Please select a Java string literal."); } monitor.worked(1); return status.isOK(); }
From source file:com.google.googlejavaformat.java.JavaInput.java
License:Apache License
/** * Lex the input and build the list of toks. * * @param text the text to be lexed./*from w w w. j av a 2 s .c o m*/ * @param stopIds a set of Eclipse token names which should cause lexing to stop. If one of these * is found, the returned list will include tokens up to but not including that token. */ ImmutableList<Tok> buildToks(String text, ImmutableSet<Integer> stopIds) throws InvalidInputException, FormatterException { stopIds = ImmutableSet.<Integer>builder().addAll(stopIds).add(ITerminalSymbols.TokenNameEOF).build(); kN = 0; IScanner scanner = ToolFactory.createScanner(true, true, true, "1.8"); scanner.setSource(text.toCharArray()); int textLength = text.length(); List<Tok> toks = new ArrayList<>(); int charI = 0; int columnI = 0; while (scanner.getCurrentTokenEndPosition() < textLength - 1 && !stopIds.contains(scanner.getNextToken())) { int charI0 = scanner.getCurrentTokenStartPosition(); // Get string, possibly with Unicode escapes. String originalTokText = text.substring(charI0, scanner.getCurrentTokenEndPosition() + 1); String tokText = new String(scanner.getCurrentTokenSource()); // Unicode escapes removed. char tokText0 = tokText.charAt(0); // The token's first character. final boolean isToken; // Is this tok a token? final boolean isNumbered; // Is this tok numbered? (tokens and comments) boolean extraNewline = false; // Extra newline at end? List<String> strings = new ArrayList<>(); if (Character.isWhitespace(tokText0)) { isToken = false; isNumbered = false; boolean first = true; for (String spaces : NEWLINE_SPLITTER.split(originalTokText)) { if (!first) { strings.add("\n"); } if (!spaces.isEmpty()) { strings.add(spaces); } first = false; } } else if (tokText.startsWith("'") || tokText.startsWith("\"")) { isToken = true; isNumbered = true; strings.add(originalTokText); } else if (tokText.startsWith("//") || tokText.startsWith("/*")) { // For compatibility with an earlier lexer, the newline after a // comment is its own tok. if (tokText.startsWith("//") && originalTokText.endsWith("\n")) { originalTokText = originalTokText.substring(0, originalTokText.length() - 1); tokText = tokText.substring(0, tokText.length() - 1); extraNewline = true; } isToken = false; isNumbered = true; strings.add(originalTokText); } else if (Character.isJavaIdentifierStart(tokText0) || Character.isDigit(tokText0) || tokText0 == '.' && tokText.length() > 1 && Character.isDigit(tokText.charAt(1))) { // Identifier, keyword, or numeric literal (a dot may begin a number, as in .2D). isToken = true; isNumbered = true; strings.add(tokText); } else { // Other tokens ("+" or "++" or ">>" are broken into one-character toks, because ">>" // cannot be lexed without syntactic knowledge. This implementation fails if the token // contains Unicode escapes. isToken = true; isNumbered = true; for (char c : tokText.toCharArray()) { strings.add(String.valueOf(c)); } } if (strings.size() == 1) { toks.add(new Tok(isNumbered ? kN++ : -1, originalTokText, tokText, charI, columnI, isToken)); for (char c : originalTokText.toCharArray()) { if (c == '\n') { columnI = 0; } else { ++columnI; } ++charI; } } else { if (strings.size() != 1 && !tokText.equals(originalTokText)) { throw new FormatterException( "Unicode escapes not allowed in whitespace or multi-character operators"); } for (String str : strings) { toks.add(new Tok(isNumbered ? kN++ : -1, str, str, charI, columnI, isToken)); for (char c : str.toCharArray()) { if (c == '\n') { columnI = 0; } else { ++columnI; } ++charI; } } } if (extraNewline) { toks.add(new Tok(-1, "\n", "\n", charI, columnI, false)); columnI = 0; ++charI; } } toks.add(new Tok(kN++, "", "", charI, columnI, true)); // EOF tok. --kN; // Don't count EOF tok. computeRanges(toks); return ImmutableList.copyOf(toks); }
From source file:net.sourceforge.metrics.calculators.LackOfCohesion.java
License:Open Source License
private void visitMethods(IMethod[] methods) { boolean countStatics = getPrefs().countStaticMethods(); for (IMethod method2 : methods) { String methodName = method2.getElementName(); try {// w ww. j a v a2 s . c o m if ((countStatics) || ((method2.getFlags() & Flags.AccStatic) == 0)) { IScanner s = ToolFactory.createScanner(false, false, false, false); s.setSource(method2.getSource().toCharArray()); while (true) { int token = s.getNextToken(); if (token == ITerminalSymbols.TokenNameEOF) { break; } if (token == ITerminalSymbols.TokenNameIdentifier) { add(new String(s.getCurrentTokenSource()), methodName); } } } } catch (JavaModelException e) { Log.logError("LCOM:Can't get source for method " + methodName, e); } catch (InvalidInputException e) { Log.logError("LCOM:Invalid scanner input for method" + methodName, e); } } }
From source file:org.eclipseguru.gwt.core.internal.codegen.AsyncServiceCodeGenerator.java
License:Open Source License
@Override protected String getFileComment(final ICompilationUnit parentCU, final String lineDelimiter) throws CoreException { // take remote service comment if possible try {/*from w w w. ja va 2 s .c o m*/ final String source = remoteServiceType.getCompilationUnit().getSource(); final IScanner scanner = ToolFactory.createScanner(true, false, false, false); final StringBuffer buffer = new StringBuffer(); scanner.setSource(source.toCharArray()); int next = scanner.getNextToken(); while (TokenScanner.isComment(next)) { buffer.append(scanner.getCurrentTokenSource()); next = scanner.getNextToken(); } if (buffer.length() > 0) { return buffer.toString(); } } catch (final Exception e) { // ignore } return super.getFileComment(parentCU, lineDelimiter); }
From source file:org.j2eespider.util.AnnotationUtil.java
License:Open Source License
/** * Replace annotations in method./*from w ww .j a v a 2 s .co m*/ */ public static void replaceAnnotationsInMethod(IMethod imethod, List<ValidatorType> annotations, String textNewAnnotations) { try { //monta o scanner do metodo IBuffer methodBuffer = imethod.getOpenable().getBuffer(); ISourceRange sourceRange = imethod.getSourceRange(); ISourceRange javadocRange = null;//imethod.getJavadocRange(); ISourceRange nameRange = imethod.getNameRange(); IScanner scanner = null; // delay initialization if (sourceRange != null && nameRange != null) { if (scanner == null) { scanner = ToolFactory.createScanner(false, false, true, false); scanner.setSource(methodBuffer.getCharacters()); } scanner.resetTo(sourceRange.getOffset(), nameRange.getOffset()); } //apaga todas as annotations que esto em annotationsType e adiciona textNewAnnotations StringBuffer sourceMethod = new StringBuffer(); int tok = scanner.getNextToken(); while (tok != ITerminalSymbols.TokenNameprivate && tok != ITerminalSymbols.TokenNameprotected && tok != ITerminalSymbols.TokenNamepublic) { //loop nas annotations if (tok == ITerminalSymbols.TokenNameAT) { //encontrou o inicio de uma annotation StringBuffer thisAnnotation = new StringBuffer("@"); tok = scanner.getNextToken(); //trabalha todo o contedo da annotation while (tok != ITerminalSymbols.TokenNameAT && tok != ITerminalSymbols.TokenNameprivate && tok != ITerminalSymbols.TokenNameprotected && tok != ITerminalSymbols.TokenNamepublic) { //pega todo o conteudo desta annotation thisAnnotation.append(scanner.getCurrentTokenSource()); //pega o nome dessa annotation tok = scanner.getNextToken(); } //verifica se para apagar essa annotation (s joga no novo sourceMethod se ela no tiver na lista que para apagar) if (!ValidatorUtil.containsValidator(annotations, thisAnnotation.toString())) { sourceMethod.append(thisAnnotation); } } } //grava o resto do metodo int posStartMethod = scanner.getCurrentTokenStartPosition(); int lengthRemoved = posStartMethod - sourceRange.getOffset(); //conta quantos caracteres foram removidos desse metodo (as annotations) String codeRemain = String.valueOf(scanner.getSource()).substring(posStartMethod, posStartMethod + sourceRange.getLength() - lengthRemoved); if (!sourceMethod.toString().equals("") && sourceMethod.toString().lastIndexOf("\n") != sourceMethod.toString().length() - 1) { //se j tem alguma annotation, ve se precisa de quebra de linha sourceMethod.append("\n\t"); } sourceMethod.append(textNewAnnotations); //adiciona as novas annotations antes do resto do mtodo sourceMethod.append(codeRemain); if (javadocRange != null) { //se tiver javadoc, no altera ele... methodBuffer.replace(sourceRange.getOffset() + javadocRange.getLength(), sourceRange.getLength() - javadocRange.getLength(), "\t" + sourceMethod.toString()); //altera o cdigo do mtodo } else { methodBuffer.replace(sourceRange.getOffset(), sourceRange.getLength(), sourceMethod.toString()); //altera o cdigo do mtodo } imethod.getOpenable().save(null, true); } catch (JavaModelException e) { } catch (InvalidInputException e) { } }
From source file:org.j2eespider.util.AnnotationUtil.java
License:Open Source License
private static int readName(IScanner scanner, StringBuffer buf) throws InvalidInputException { int tok = scanner.getNextToken(); while (tok == ITerminalSymbols.TokenNameIdentifier) { buf.append(scanner.getCurrentTokenSource()); tok = scanner.getNextToken();//from ww w . ja v a2 s . c o m if (tok != ITerminalSymbols.TokenNameDOT) { return tok; } buf.append('.'); tok = scanner.getNextToken(); } return tok; }
From source file:seeit3d.java.modeler.generator.metrics.LCOMCalculator.java
License:Open Source License
private boolean methodUseField(IField field, IMethod method) throws JavaModelException { try {//from w ww . j a v a 2s .c om String fieldName = field.getElementName(); String methodSource = method.getSource(); IScanner scanner = ToolFactory.createScanner(false, false, false, false); scanner.setSource(methodSource.toCharArray()); while (true) { int token = scanner.getNextToken(); if (token == ITerminalSymbols.TokenNameEOF) break; if (token == ITerminalSymbols.TokenNameIdentifier) { String identifier = new String(scanner.getCurrentTokenSource()); if (identifier.equals(fieldName)) { return true; } } } } catch (InvalidInputException e) { e.printStackTrace(); } return false; }
From source file:seeit3d.java.modeler.generator.TypeClassModelGenerator.java
License:Open Source License
@Override protected void extraOperationsOnContainer(Container createdContainer, IType element, boolean analizeDependencies) throws JavaModelException { if (analizeDependencies) { try {// w ww . j a v a 2s . c om List<String> relatedTypesNames = new ArrayList<String>(); List<String> resolvedTokens = new ArrayList<String>(); IJavaProject project = element.getJavaProject(); String source = element.getSource(); IScanner scanner = ToolFactory.createScanner(false, false, false, false); scanner.setSource(source.toCharArray()); while (true) { int token = scanner.getNextToken(); if (token == ITerminalSymbols.TokenNameEOF) { break; } if (token == ITerminalSymbols.TokenNameIdentifier) { char[] currentTokenSource = scanner.getCurrentTokenSource(); String currentToken = new String(currentTokenSource); if (!resolvedTokens.contains(currentToken)) { String[][] resolveType = element.resolveType(currentToken); if (resolveType != null) { String elementName = resolveType[0][0] + "." + resolveType[0][1]; relatedTypesNames.add(elementName); } resolvedTokens.add(currentToken); } } } for (String relatedTypeName : relatedTypesNames) { IType type = project.findType(relatedTypeName); if (type != null && !type.isBinary()) { Container relatedContainer = this.analize(type, false); createdContainer.addRelatedContainer(relatedContainer); } } } catch (InvalidInputException e) { e.printStackTrace(); } } }