Example usage for org.aspectj.ajdt.internal.compiler.lookup EclipseSourceLocation getEndPos

List of usage examples for org.aspectj.ajdt.internal.compiler.lookup EclipseSourceLocation getEndPos

Introduction

In this page you can find the example usage for org.aspectj.ajdt.internal.compiler.lookup EclipseSourceLocation getEndPos.

Prototype

public int getEndPos() 

Source Link

Usage

From source file:org.eclipse.ajdt.core.javaelements.AJCodeElement.java

License:Open Source License

public void initializeLocations() {
    // try the easy way:
    IProgramElement ipe = AJProjectModelFactory.getInstance().getModelForJavaElement(this)
            .javaElementToProgramElement(this);
    ISourceLocation sloc = ipe.getSourceLocation();
    if (sloc != null) {
        startLine = sloc.getLine();/*from  w  w  w  . ja va2s. c  om*/

        nameStart = sloc.getOffset();
        if (sloc instanceof EclipseSourceLocation) {
            EclipseSourceLocation esloc = (EclipseSourceLocation) sloc;
            nameEnd = esloc.getEndPos();
        }
    }

    // sometimes the start and end values are not set...so do it the hard way
    // so calculate it from the line
    if (nameStart <= 0 || nameEnd <= 0) {
        try {
            IOpenable openable = this.parent.getOpenableParent();
            IBuffer buffer;
            if (openable instanceof AJCompilationUnit) {
                AJCompilationUnit ajCompUnit = (AJCompilationUnit) openable;
                ajCompUnit.requestOriginalContentMode();
                buffer = openable.getBuffer();
                ajCompUnit.discardOriginalContentMode();
            } else {
                buffer = openable.getBuffer();
            }
            String source = buffer.getContents();

            int lines = 0;
            for (int i = 0; i < source.length(); i++) {
                if (source.charAt(i) == '\n') {
                    lines++;
                    if (lines == startLine - 1) {
                        // starting remove white space
                        i++;
                        while (i < source.length()
                                && (Character.isWhitespace(source.charAt(i)) && source.charAt(i) != '\n')) {
                            i++;
                        }
                        nameStart = i;
                        break;
                    }
                }
            }

            for (int i = nameStart + 1; i < source.length(); i++) {
                if (source.charAt(i) == '\n' || source.charAt(i) == ';') {
                    nameEnd = i - 1;
                    break;
                }
            }

            nameStart = Math.min(nameStart, nameEnd);
        } catch (JavaModelException e) {
        }
    }
}