How to display the line number of a method in Java?

Asked

Viewed 38 times

0

Good morning ,

I am using AST , so I was able to access the methods, attributes and get if it is public or private, among other things.

How do I display the line number of a particular attribute or method? Example:

int privado = Modifier.ModifierKeyword.PRIVATE_KEYWORD.toFlagValue();

public Teste (ICompilationUnit unit) throws JavaModelException, MalformedTreeException, BadLocationException {
    Document documento = new Document(unit.getSource());
    ASTParser parser = ASTParser.newParser(AST.JLS8);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(unit);
    CompilationUnit compUnit = (CompilationUnit) parser.createAST(null);
    compUnit.recordModifications();
    AST ast = compUnit.getAST();

    Visitor visitor = new Visitor();
    compUnit.accept(visitor);


    for (FieldDeclaration f : type.getFields()) {
        int tipo = f.getModifiers();                                                
        if ((tipo & publico) == publico) {
            mensagem.add(" \n O ATRIBUTO "+f.fragments().toString()+" é public \n");

        }       

    }
}

I wanted to inform in : The test attribute is public in Line x.

1 answer

0

Try this code:

    System.out.println("Número da linha #" + 
    new Exception().getStackTrace()[0].getLineNumber());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.