Example usage for org.aspectj.bridge ISourceLocation getOffset

List of usage examples for org.aspectj.bridge ISourceLocation getOffset

Introduction

In this page you can find the example usage for org.aspectj.bridge ISourceLocation getOffset.

Prototype

int getOffset();

Source Link

Usage

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

License:Open Source License

protected Object createElementInfo() {
    try {/*www .  j av  a2 s. c  o m*/
        IProgramElement ipe = AJProjectModelFactory.getInstance().getModelForJavaElement(this)
                .javaElementToProgramElement(this);

        AdviceElementInfo info = new AdviceElementInfo();
        info.setAJExtraInfo(ipe.getExtraInfo());
        info.setName(name.toCharArray());
        info.setAJKind(IProgramElement.Kind.ADVICE);
        info.setAJModifiers(ipe.getModifiers());
        ISourceLocation sourceLocation = ipe.getSourceLocation();
        info.setSourceRangeStart(sourceLocation.getOffset());
        info.setNameSourceStart(sourceLocation.getOffset());
        info.setNameSourceEnd(sourceLocation.getOffset() + ipe.getName().length());

        return info;
    } catch (Exception e) {
        // can fail for any of a number of reasons.
        // return null so that we can try again later.
        return null;
    }
}

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  www .  j  av  a 2 s .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) {
        }
    }
}

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

License:Open Source License

protected Object createElementInfo() {

    AspectElementInfo info = new AspectElementInfo();
    info.setAJKind(IProgramElement.Kind.ASPECT);
    info.setHandle(this);
    info.setSourceRangeStart(0);/*from  w w w . j av a2s. c o m*/

    IProgramElement ipe = AJProjectModelFactory.getInstance().getModelForJavaElement(this)
            .javaElementToProgramElement(this);
    if (ipe != null && ipe != IHierarchy.NO_STRUCTURE) {
        info.setAJExtraInfo(ipe.getExtraInfo());
        info.setAJModifiers(ipe.getModifiers());
        info.setFlags(getProgramElementModifiers(ipe));
        info.setAJAccessibility(ipe.getAccessibility());
        ISourceLocation sourceLocation = ipe.getSourceLocation();
        info.setSourceRangeStart(sourceLocation.getOffset());
        info.setNameSourceStart(sourceLocation.getOffset());
        info.setNameSourceEnd(sourceLocation.getOffset() + ipe.getName().length());
        // info.setPrivileged(???); not setting this yet
    }
    return info;
}

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

License:Open Source License

protected Object createElementInfo() {
    IntertypeElementInfo info = new IntertypeElementInfo();

    IProject project = this.getJavaProject().getProject();
    IProgramElement ipe = AJProjectModelFactory.getInstance().getModelForProject(project)
            .javaElementToProgramElement(this);
    if (ipe != IHierarchy.NO_STRUCTURE) {
        // this way of creating the element info does not contain proper source locations for the name and target type
        info.setAJExtraInfo(ipe.getExtraInfo());
        info.setName(name.toCharArray());
        info.setAJKind(ipe.getKind());//from   w w w .  ja  v a  2 s.  c o  m
        info.setAJModifiers(ipe.getModifiers());
        info.setFlags(ipe.getRawModifiers());
        info.setDeclaredModifiers(info.getModifiers());
        info.setAJAccessibility(ipe.getAccessibility());
        ISourceLocation sourceLocation = ipe.getSourceLocation();
        info.setSourceRangeStart(sourceLocation.getOffset());
        info.setNameSourceStart(sourceLocation.getOffset()); // This is wrong
        info.setNameSourceEnd(sourceLocation.getOffset() + ipe.getName().length()); // also wrong

        info.setConstructor(info.getAJKind() == IProgramElement.Kind.INTER_TYPE_CONSTRUCTOR);
        char[][] argumentNames = CoreUtils.listStringsToCharArrays(ipe.getParameterNames());
        char[][] argumentTypeNames = CoreUtils.listCharsToCharArrays(ipe.getParameterTypes());
        if (argumentNames.length == 0 && argumentTypeNames.length > 0) {
            // argument names not found.  likely coming from binary class file w/p source attachment
            // generate argument names
            argumentNames = new char[argumentTypeNames.length][];
            for (int i = 0; i < argumentNames.length; i++) {
                argumentNames[i] = ("arg" + i).toCharArray();
            }
        }

        info.setArgumentNames(argumentNames);
        info.setArgumentTypeNames(argumentTypeNames);
        info.setReturnType(ipe.getCorrespondingType(false).toCharArray());
        info.setQualifiedReturnType(ipe.getCorrespondingType(true).toCharArray());

        info.setTypeParameters(createTypeParameters(project));

        if (argumentNames != null && argumentNames.length > 0) {
            ILocalVariable[] arguments = new ILocalVariable[argumentNames.length];
            for (int i = 0; i < argumentNames.length; i++) {
                arguments[i] = new LocalVariable(this, String.valueOf(argumentNames[i]),
                        // sloc is not correct, but it is close enough
                        sourceLocation.getOffset(), sourceLocation.getOffset() + 1, sourceLocation.getOffset(),
                        sourceLocation.getOffset() + 1, String.valueOf(argumentTypeNames[i]), new Annotation[0],
                        Flags.AccDefault, true);
            }
            info.setArguments(arguments);
        }

    } else {
        // no successful build yet, we don't know the contents
        info.setName(name.toCharArray());
        info.setAJKind(IProgramElement.Kind.ERROR);
        info.setAJModifiers(Collections.<Modifiers>emptyList());
    }
    return info;
}

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

License:Open Source License

protected Object createElementInfo() {
    if (elementInfo != null) {
        return elementInfo;
    }/*from  w  ww .  ja v a 2  s .com*/

    try {
        IProgramElement ipe = AJProjectModelFactory.getInstance().getModelForJavaElement(this)
                .javaElementToProgramElement(this);

        elementInfo = new MethodElementInfo();
        ISourceLocation sourceLocation = ipe.getSourceLocation();
        elementInfo.setSourceRangeStart(sourceLocation.getOffset());
        elementInfo.setNameSourceStart(sourceLocation.getOffset());
        elementInfo.setNameSourceEnd(sourceLocation.getOffset() + ipe.getName().length());
        elementInfo.setAJExtraInfo(ipe.getExtraInfo());
        elementInfo.setName(name.toCharArray());
        elementInfo.setAJKind(IProgramElement.Kind.METHOD);
        elementInfo.setAJModifiers(ipe.getModifiers());
        elementInfo.setAJAccessibility(ipe.getAccessibility());

        return elementInfo;
    } catch (Exception e) {
        // can fail for any of a number of reasons.
        // return null so that we can try again later.
        return null;
    }
}

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

License:Open Source License

protected Object createElementInfo() {
    IProgramElement ipe = AJProjectModelFactory.getInstance().getModelForJavaElement(this)
            .javaElementToProgramElement(this);

    PointcutElementInfo info = new PointcutElementInfo();
    info.setAJKind(IProgramElement.Kind.POINTCUT);
    info.setName(this.getElementName().toCharArray());
    info.setAJAccessibility(ipe.getAccessibility());
    ISourceLocation sourceLocation = ipe.getSourceLocation();
    if (sourceLocation != null) {
        info.setSourceRangeStart(sourceLocation.getOffset());
        info.setNameSourceStart(sourceLocation.getOffset());
        info.setNameSourceEnd(sourceLocation.getOffset() + ipe.getName().length());
    }/*  ww w. j av  a2 s.  c  o m*/
    return info;
}

From source file:org.eclipse.ajdt.core.model.AJProjectModelFacade.java

License:Open Source License

/**
 * Open up the buffer to to convert from line number to offset
 * this is slow// w  ww. ja v  a  2 s. c o  m
 * We are always working in an AJCU with an aspect element
 * 
 * cache the results since it is likely that we will be 
 * calling this often for the same sloc
 */
private int offsetFromLine(ITypeRoot unit, ISourceLocation sloc) throws JavaModelException {
    if (sloc.getOffset() > 0) {
        return sloc.getOffset();
    }

    if (slocCache != null && slocCache.containsKey(sloc)) {
        return slocCache.get(sloc).intValue();
    }

    if (unit instanceof AJCompilationUnit) {
        AJCompilationUnit ajUnit = (AJCompilationUnit) unit;
        ajUnit.requestOriginalContentMode();
    }
    IBuffer buf = unit.getBuffer();
    if (unit instanceof AJCompilationUnit) {
        AJCompilationUnit ajUnit = (AJCompilationUnit) unit;
        ajUnit.discardOriginalContentMode();
    }
    if (buf != null) {
        int requestedLine = sloc.getLine();
        int currentLine = 1;
        int offset = 0;
        while (offset < buf.getLength() && currentLine < requestedLine) {
            if (buf.getChar(offset++) == '\n') {
                currentLine++;
            }
        }
        while (offset < buf.getLength() && Character.isWhitespace(buf.getChar(offset))) {
            offset++;
        }

        // cache
        if (slocCache == null) {
            slocCache = new HashMap<ISourceLocation, Integer>();
        }
        slocCache.put(sloc, new Integer(offset));
        return offset;
    }
    // no source code
    return 0;
}