Example usage for org.eclipse.jdt.core.compiler IScanner getSource

List of usage examples for org.eclipse.jdt.core.compiler IScanner getSource

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.compiler IScanner getSource.

Prototype

char[] getSource();

Source Link

Document

Answers the original source being processed (not a copy of it).

Usage

From source file:org.j2eespider.util.AnnotationUtil.java

License:Open Source License

/**
 * Replace annotations in method./*  w  w  w.ja  v a  2  s . c  o  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) {
    }

}