Example usage for org.eclipse.jdt.core.dom Javadoc getStartPosition

List of usage examples for org.eclipse.jdt.core.dom Javadoc getStartPosition

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom Javadoc getStartPosition.

Prototype

public final int getStartPosition() 

Source Link

Document

Returns the character index into the original source file indicating where the source fragment corresponding to this node begins.

Usage

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(Javadoc node) {
    boa.types.Ast.Comment.Builder b = boa.types.Ast.Comment.newBuilder();
    buildPosition(node);// w ww .  j a  v  a2 s  .  c  o  m
    b.setPosition(pos.build());
    b.setKind(boa.types.Ast.Comment.CommentKind.DOC);
    b.setValue(src.substring(node.getStartPosition(), node.getStartPosition() + node.getLength()));
    comments.add(b.build());
    return false;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

public int beginLine(BodyDeclaration node) {
    int beginLine = INVALID_LINE;

    List modifiers = node.modifiers();
    for (Object modifier : modifiers) {
        if (modifier instanceof Modifier) {
            Modifier m = (Modifier) modifier;
            beginLine = cu.getLineNumber(m.getStartPosition());
        }/*from  ww w . ja v  a 2  s. com*/
    }

    if (beginLine == INVALID_LINE) {

        beginLine = cu.getLineNumber(node.getStartPosition());

        Javadoc javadoc = node.getJavadoc();
        int javadocBegin = INVALID_JAVADOC;

        if (javadoc != null) {
            javadocBegin = cu.getLineNumber(javadoc.getStartPosition());
        }

        if (beginLine == javadocBegin) {
            beginLine = cu.getLineNumber(javadoc.getStartPosition() + javadoc.getLength() + 1);
        }
    }
    return beginLine;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

public int beginLine(PackageDeclaration node) {
    int beginLine = cu.getLineNumber(node.getStartPosition());

    Javadoc javadoc = node.getJavadoc();
    int javadocBegin = INVALID_LINE;

    if (javadoc != null) {
        javadocBegin = cu.getLineNumber(javadoc.getStartPosition());
    }//  ww w .j a  va  2s  .c o  m

    if (beginLine == javadocBegin) {
        beginLine = cu.getLineNumber(javadoc.getStartPosition() + javadoc.getLength() + 1);
    }

    return beginLine;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

public int beginColunm(BodyDeclaration node) {

    int begincolumn = INVALID_COLUMN;

    List modifiers = node.modifiers();
    for (Object modifier : modifiers) {
        if (modifier instanceof Modifier) {
            Modifier m = (Modifier) modifier;
            begincolumn = cu.getColumnNumber(m.getStartPosition());
        }//  ww  w. j a va  2 s.  c  om
    }

    if (begincolumn == INVALID_COLUMN) {

        begincolumn = cu.getColumnNumber(node.getStartPosition());

        Javadoc javadoc = node.getJavadoc();
        int javadocBegin = INVALID_JAVADOC;

        if (javadoc != null) {
            javadocBegin = cu.getColumnNumber(javadoc.getStartPosition());
        }

        if (begincolumn == javadocBegin) {
            begincolumn = cu.getColumnNumber(javadoc.getStartPosition() + javadoc.getLength() + 1);
        }
    }

    return begincolumn;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

public int beginColunm(PackageDeclaration node) {
    int beginColumn = cu.getColumnNumber(node.getStartPosition());

    Javadoc javadoc = node.getJavadoc();
    int javadocBegin = INVALID_JAVADOC;

    if (javadoc != null) {
        javadocBegin = cu.getColumnNumber(javadoc.getStartPosition());
    }//www.java 2 s.  c  o m

    if (beginColumn == javadocBegin) {
        beginColumn = cu.getColumnNumber(javadoc.getStartPosition() + javadoc.getLength() + 1);
    }

    return beginColumn;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

@Override
public boolean visit(Javadoc node) {
    int beginLine = cu.getLineNumber(node.getStartPosition());
    int endLine = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int beginColumn = cu.getColumnNumber(node.getStartPosition());
    int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    languageConstructs.add(/*from   w  w  w . j  av  a  2  s.  c o m*/
            new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine, beginColumn, endColumn));

    return true;
}

From source file:com.android.icu4j.srcgen.FixupBidiClassDoc.java

License:Apache License

@Override
public void process(Context context, CompilationUnit cu) {
    TypeDeclaration classNode = (TypeDeclaration) LOCATOR.find(cu);
    Javadoc javadoc = classNode.getJavadoc();
    try {/*from www  .  j av a 2 s .  c  o  m*/
        Document document = context.document();
        String commentText = document.get(javadoc.getStartPosition(), javadoc.getLength());
        String newCommentText = commentText.replace(BAD_TEXT, "");
        document.replace(javadoc.getStartPosition(), javadoc.getLength(), newCommentText);
    } catch (BadLocationException e) {
        throw new AssertionError(e);
    }
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.JavaSyncUtils.java

License:Open Source License

/**
 * Loads the java file and sets the retrieved comments for every method/attribute found inside it
 * @throws JavaModelException //w w  w . ja  v  a2s.  c om
 * @flowerModelElementId _zVs8LZiOEd6aNMdNFvR5WQ
 */
@SuppressWarnings("unchecked")
public static CompilationUnit loadJavaFile(IFile file, ASTParser parser) throws Exception {
    CompilationUnit result = null;
    char[] fileContent;
    try {
        fileContent = getFileContent(file).toCharArray();
    } catch (ResourceException e) {
        file.refreshLocal(IResource.DEPTH_INFINITE, null);
        fileContent = getFileContent(file).toCharArray();
    }

    HashMap defaultOptions = new HashMap(JavaCore.getOptions());
    String compilerSourceLevel = (String) defaultOptions.get(JavaCore.COMPILER_SOURCE);

    // Source level 1.5 is necessary to enable annotations
    if (Double.valueOf(compilerSourceLevel) < 1.5) {
        defaultOptions.put(JavaCore.COMPILER_SOURCE, "1.5");
        parser.setCompilerOptions(defaultOptions);
    }

    parser.setSource(fileContent);
    result = (CompilationUnit) parser.createAST(null);

    for (Object o : result.getCommentList()) {
        if (o instanceof Javadoc) {
            Javadoc doc = (Javadoc) o;
            String docComment = new String(fileContent, doc.getStartPosition(), doc.getLength());

            //use Pattern and Matcher instead of string.replaceAll(...) to compile the pattern only once
            Matcher matcher = COMMENT_LINE_PATTERN.matcher(docComment);
            docComment = matcher.replaceAll("\r\n");
            matcher = COMMENT_MARKERS_PATTERN.matcher(docComment);
            docComment = matcher.replaceAll("");

            doc.setProperty("comment", docComment);
        }
    }
    return result;
}

From source file:com.google.gdt.eclipse.designer.builders.GwtBuilder.java

License:Open Source License

/**
 * Removes annotations of service {@link TypeDeclaration}.
 *///w w w. j  av a 2 s.c  om
private static void removeAnnotations(TypeDeclaration serviceType, String source, MultiTextEdit edits) {
    int typePos = serviceType.getStartPosition();
    {
        Javadoc javadoc = serviceType.getJavadoc();
        if (javadoc != null) {
            typePos = javadoc.getStartPosition() + javadoc.getLength();
            while (Character.isWhitespace(source.charAt(typePos))) {
                typePos++;
            }
        }
    }
    int pureTypePos = StringUtils.indexOfAny(source,
            new String[] { "public ", "protected ", "class ", "interface " });
    if (pureTypePos != -1 && pureTypePos != typePos) {
        edits.addChild(new DeleteEdit(typePos, pureTypePos - typePos));
    }
}

From source file:de.ovgu.featureide.munge.signatures.MungeSignatureBuilder.java

License:Open Source License

private static Collection<AbstractSignature> parse(ProjectSignatures projectSignatures, ASTNode root) {
    final HashMap<AbstractSignature, AbstractSignature> map = new HashMap<>();

    final CompilationUnit cu = (CompilationUnit) root;
    final PackageDeclaration pckgDecl = cu.getPackage();
    final String packageName = (pckgDecl == null) ? null : pckgDecl.getName().getFullyQualifiedName();
    List<?> l = cu.getCommentList();
    List<Javadoc> cl = new LinkedList<>();
    for (Object object : l) {
        if (object instanceof Javadoc) {
            Javadoc comment = (Javadoc) object;
            cl.add(comment);//  w ww .j av  a2 s . c o m
        }
    }

    final ListIterator<Javadoc> it = cl.listIterator();
    final FeatureDataConstructor featureDataConstructor = new FeatureDataConstructor(projectSignatures,
            FeatureDataConstructor.TYPE_PP);

    root.accept(new ASTVisitor() {
        private BodyDeclaration curDeclaration = null;
        private PreprocessorFeatureData curfeatureData = null;
        private String lastComment = null;
        private MethodDeclaration lastCommentedMethod = null;

        @Override
        public boolean visit(Javadoc node) {
            if (curDeclaration != null) {
                final StringBuilder sb = new StringBuilder();
                while (it.hasNext()) {
                    final Javadoc comment = it.next();
                    if (comment.getStartPosition() <= curDeclaration.getStartPosition()) {
                        sb.append(comment);
                        sb.append("\n");
                    } else {
                        it.previous();
                        break;
                    }
                }
                lastComment = sb.toString();

                curfeatureData.setComment(lastComment);
                lastCommentedMethod = (curDeclaration instanceof MethodDeclaration)
                        ? (MethodDeclaration) curDeclaration
                        : null;
            }
            return false;
        }

        private void attachFeatureData(AbstractSignature curSignature, BodyDeclaration curDeclaration) {
            this.curDeclaration = curDeclaration;
            final Javadoc javadoc = curDeclaration.getJavadoc();
            final int startPosition = (javadoc == null) ? curDeclaration.getStartPosition()
                    : curDeclaration.getStartPosition() + javadoc.getLength();
            curfeatureData = (PreprocessorFeatureData) featureDataConstructor.create(null,
                    unit.getLineNumber(startPosition),
                    unit.getLineNumber(curDeclaration.getStartPosition() + curDeclaration.getLength()));
            curSignature.setFeatureData(curfeatureData);
            map.put(curSignature, curSignature);
        }

        @Override
        public boolean visit(CompilationUnit unit) {
            this.unit = unit;
            return true;
        }

        CompilationUnit unit = null;

        @Override
        public boolean visit(MethodDeclaration node) {
            int pos = unit.getLineNumber(node.getBody().getStartPosition());
            int end = unit.getLineNumber(node.getBody().getStartPosition() + node.getBody().getLength());
            final MungeMethodSignature methodSignature = new MungeMethodSignature(getParent(node.getParent()),
                    node.getName().getIdentifier(), node.getModifiers(), node.getReturnType2(),
                    node.parameters(), node.isConstructor(), pos, end);

            attachFeatureData(methodSignature, node);

            if (node.getJavadoc() == null && lastCommentedMethod != null
                    && lastCommentedMethod.getName().equals(node.getName())) {
                curfeatureData.setComment(lastComment);
            } else {
                lastCommentedMethod = null;
            }
            return true;
        }

        private AbstractClassSignature getParent(ASTNode astnode) {
            final AbstractClassSignature sig;
            if (astnode instanceof TypeDeclaration) {
                final TypeDeclaration node = (TypeDeclaration) astnode;
                sig = new MungeClassSignature(null, node.getName().getIdentifier(), node.getModifiers(),
                        node.isInterface() ? "interface" : "class", packageName);
            } else {
                return null;
            }
            AbstractClassSignature uniqueSig = (AbstractClassSignature) map.get(sig);
            if (uniqueSig == null) {
                visit((TypeDeclaration) astnode);
            }
            return uniqueSig;
        }

        @Override
        public boolean visit(FieldDeclaration node) {
            for (Iterator<?> it = node.fragments().iterator(); it.hasNext();) {
                VariableDeclarationFragment fragment = (VariableDeclarationFragment) it.next();

                final MungeFieldSignature fieldSignature = new MungeFieldSignature(getParent(node.getParent()),
                        fragment.getName().getIdentifier(), node.getModifiers(), node.getType());

                attachFeatureData(fieldSignature, node);
            }

            return true;
        }

        @Override
        public boolean visit(TypeDeclaration node) {
            final MungeClassSignature classSignature = new MungeClassSignature(getParent(node.getParent()),
                    node.getName().getIdentifier(), node.getModifiers(),
                    node.isInterface() ? "interface" : "class", packageName);

            attachFeatureData(classSignature, node);

            return super.visit(node);
        }

    });
    return map.keySet();
}