Search in sources :

Example 1 with Javadoc

use of org.eclipse.jdt.internal.compiler.ast.Javadoc in project spoon by INRIA.

the class PositionBuilder method buildPositionCtElement.

SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {
    CoreFactory cf = this.jdtTreeBuilder.getFactory().Core();
    CompilationUnit cu = this.jdtTreeBuilder.getFactory().CompilationUnit().getOrCreate(new String(this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.getFileName()));
    CompilationResult cr = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.compilationResult;
    int[] lineSeparatorPositions = cr.lineSeparatorPositions;
    char[] contents = cr.compilationUnit.getContents();
    int sourceStart = node.sourceStart;
    int sourceEnd = node.sourceEnd;
    if ((node instanceof Annotation)) {
        Annotation ann = (Annotation) node;
        int declEnd = ann.declarationSourceEnd;
        if (declEnd > 0) {
            sourceEnd = declEnd;
        }
    } else if ((node instanceof Expression)) {
        Expression expression = (Expression) node;
        int statementEnd = expression.statementEnd;
        if (statementEnd > 0) {
            sourceEnd = statementEnd;
        }
    }
    if (node instanceof AbstractVariableDeclaration) {
        AbstractVariableDeclaration variableDeclaration = (AbstractVariableDeclaration) node;
        int modifiersSourceStart = variableDeclaration.modifiersSourceStart;
        int declarationSourceStart = variableDeclaration.declarationSourceStart;
        int declarationSourceEnd = variableDeclaration.declarationSourceEnd;
        int declarationEnd = variableDeclaration.declarationEnd;
        Annotation[] annotations = variableDeclaration.annotations;
        if (annotations != null && annotations.length > 0) {
            if (annotations[0].sourceStart() == sourceStart) {
                modifiersSourceStart = annotations[annotations.length - 1].sourceEnd() + 2;
            }
        }
        if (modifiersSourceStart == 0) {
            modifiersSourceStart = declarationSourceStart;
        }
        int modifiersSourceEnd;
        if (variableDeclaration.type != null) {
            modifiersSourceEnd = variableDeclaration.type.sourceStart() - 2;
        } else {
            // variable that has no type such as TypeParameter
            modifiersSourceEnd = declarationSourceStart - 1;
        }
        // when no modifier
        if (modifiersSourceStart > modifiersSourceEnd) {
            modifiersSourceEnd = modifiersSourceStart - 1;
        }
        return cf.createDeclarationSourcePosition(cu, sourceStart, sourceEnd, modifiersSourceStart, modifiersSourceEnd, declarationSourceStart, declarationSourceEnd, lineSeparatorPositions);
    } else if (node instanceof TypeDeclaration) {
        TypeDeclaration typeDeclaration = (TypeDeclaration) node;
        int declarationSourceStart = typeDeclaration.declarationSourceStart;
        int declarationSourceEnd = typeDeclaration.declarationSourceEnd;
        int modifiersSourceStart = typeDeclaration.modifiersSourceStart;
        int bodyStart = typeDeclaration.bodyStart;
        int bodyEnd = typeDeclaration.bodyEnd;
        Annotation[] annotations = typeDeclaration.annotations;
        if (annotations != null && annotations.length > 0) {
            if (annotations[0].sourceStart() == declarationSourceStart) {
                modifiersSourceStart = findNextNonWhitespace(contents, annotations[annotations.length - 1].declarationSourceEnd + 1);
            }
        }
        if (modifiersSourceStart == 0) {
            modifiersSourceStart = declarationSourceStart;
        }
        // look for start of first keyword before the type keyword e.g. "class". `sourceStart` points at first char of type name
        int modifiersSourceEnd = findPrevNonWhitespace(contents, findPrevWhitespace(contents, findPrevNonWhitespace(contents, sourceStart - 1)));
        if (modifiersSourceEnd < modifiersSourceStart) {
            // there is no modifier
            modifiersSourceEnd = modifiersSourceStart - 1;
        }
        return cf.createBodyHolderSourcePosition(cu, sourceStart, sourceEnd, modifiersSourceStart, modifiersSourceEnd, declarationSourceStart, declarationSourceEnd, bodyStart - 1, bodyEnd, lineSeparatorPositions);
    } else if (node instanceof AbstractMethodDeclaration) {
        AbstractMethodDeclaration methodDeclaration = (AbstractMethodDeclaration) node;
        int bodyStart = methodDeclaration.bodyStart;
        int bodyEnd = methodDeclaration.bodyEnd;
        int declarationSourceStart = methodDeclaration.declarationSourceStart;
        int declarationSourceEnd = methodDeclaration.declarationSourceEnd;
        int modifiersSourceStart = methodDeclaration.modifiersSourceStart;
        if (modifiersSourceStart == 0) {
            modifiersSourceStart = declarationSourceStart;
        }
        if (node instanceof AnnotationMethodDeclaration && bodyStart == bodyEnd) {
            // The ";" at the end of annotation method declaration is not part of body
            // let it behave same like in abstract MethodDeclaration
            bodyEnd--;
        }
        Javadoc javadoc = methodDeclaration.javadoc;
        if (javadoc != null && javadoc.sourceEnd() > declarationSourceStart) {
            modifiersSourceStart = javadoc.sourceEnd() + 1;
        }
        Annotation[] annotations = methodDeclaration.annotations;
        if (annotations != null && annotations.length > 0) {
            if (annotations[0].sourceStart() == declarationSourceStart) {
                modifiersSourceStart = annotations[annotations.length - 1].sourceEnd() + 2;
            }
        }
        int modifiersSourceEnd = sourceStart - 1;
        if (methodDeclaration instanceof MethodDeclaration && ((MethodDeclaration) methodDeclaration).returnType != null) {
            modifiersSourceEnd = ((MethodDeclaration) methodDeclaration).returnType.sourceStart() - 2;
        }
        TypeParameter[] typeParameters = methodDeclaration.typeParameters();
        if (typeParameters != null && typeParameters.length > 0) {
            modifiersSourceEnd = typeParameters[0].declarationSourceStart - 3;
        }
        if (getModifiers(methodDeclaration.modifiers, false, true).isEmpty()) {
            modifiersSourceStart = modifiersSourceEnd + 1;
        }
        sourceEnd = sourceStart + methodDeclaration.selector.length - 1;
        if (e instanceof CtStatementList) {
            return cf.createSourcePosition(cu, bodyStart - 1, bodyEnd + 1, lineSeparatorPositions);
        } else {
            if (bodyStart == 0) {
                return SourcePosition.NOPOSITION;
            } else {
                if (bodyStart < bodyEnd) {
                    // include brackets if they are there
                    if (contents[bodyStart - 1] == '{') {
                        bodyStart--;
                        if (contents[bodyEnd + 1] == '}') {
                            bodyEnd++;
                        } else {
                            throw new SpoonException("Missing body end in\n" + new String(contents, sourceStart, sourceEnd - sourceStart));
                        }
                    }
                }
                return cf.createBodyHolderSourcePosition(cu, sourceStart, sourceEnd, modifiersSourceStart, modifiersSourceEnd, declarationSourceStart, declarationSourceEnd, bodyStart, bodyEnd, lineSeparatorPositions);
            }
        }
    }
    return cf.createSourcePosition(cu, sourceStart, sourceEnd, lineSeparatorPositions);
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) TypeParameter(org.eclipse.jdt.internal.compiler.ast.TypeParameter) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) SpoonException(spoon.SpoonException) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) Javadoc(org.eclipse.jdt.internal.compiler.ast.Javadoc) CoreFactory(spoon.reflect.factory.CoreFactory) AbstractVariableDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) CtStatementList(spoon.reflect.code.CtStatementList) CompilationResult(org.eclipse.jdt.internal.compiler.CompilationResult) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)

Aggregations

CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 AbstractVariableDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration)1 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)1 AnnotationMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration)1 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)1 Javadoc (org.eclipse.jdt.internal.compiler.ast.Javadoc)1 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)1 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)1 TypeParameter (org.eclipse.jdt.internal.compiler.ast.TypeParameter)1 SpoonException (spoon.SpoonException)1 CtStatementList (spoon.reflect.code.CtStatementList)1 CompilationUnit (spoon.reflect.cu.CompilationUnit)1 CoreFactory (spoon.reflect.factory.CoreFactory)1