Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the background colour of elements according to their visibility modifier in Eclipse?

I'd like to set the background colour of fields and methods (on the first level) according to their visibility modifier in Eclipse.

For example private fields and methods should get a red background, while public fields and methods get a green background:

enter image description here

Is there a way to configure this in Eclipse?

like image 805
Edward Avatar asked Nov 09 '22 03:11

Edward


1 Answers

To get this sort of a colored background, you need to use Markers and MarkerAnnotationSpecification. You will find how to use them here: http://cubussapiens.hu/2011/05/custom-markers-and-annotations-the-bright-side-of-eclipse/

As for how to find the private, public fields, you need to use the JDT plugin and the AST parser to parse the Java file and find all the information that you want. I am adding a small code snippet to get you started on this.

        ASTParser parser = ASTParser.newParser(AST_LEVEL);
        parser.setSource(cmpUnit);
        parser.setResolveBindings(true);
        CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
        AST ast = astRoot.getAST();

        TypeDeclaration javaType = null;

        Object type = astRoot.types().get(0);
        if (type instanceof TypeDeclaration) {
            javaType =  ((TypeDeclaration) type);
        }


        List<FieldDeclarationInfo> fieldDeclarations = new ArrayList<FieldDeclarationInfo>();

        // Get the field info
        for (FieldDeclaration fieldDeclaration : javaType.getFields()) {
            // From this object you can recover all the information that you want about the fields.
        }

Here cmpUnit is the ICompilationUnit of the Java File.

like image 137
Henry Avatar answered Nov 15 '22 09:11

Henry