如何根据Eclipse中的可见性修饰符设置元素的背景颜色?

Edw*_*ard 5 eclipse eclipse-plugin colors modifier

我想根据Eclipse中的可见性修饰符设置字段和方法的背景颜色(在第一级).

例如,私有字段和方法应该获得红色背景,而公共字段和方法获得绿色背景:

在此输入图像描述

有没有办法在Eclipse中配置它?

Hen*_*nry 1

要获得这种彩色背景,您需要使用MarkersMarkerAnnotationSpecification。您将在这里找到如何使用它们:http://cubussapiens.hu/2011/05/custom-markers-and-annotations-the-bright-side-of-eclipse/

至于如何找到privatepublic字段,则需要使用 JDT 插件和 AST 解析器来解析 Java 文件并找到所有你想要的信息。我添加了一个小代码片段来帮助您开始执行此操作。

        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.
        }
Run Code Online (Sandbox Code Playgroud)

cmpUnitICompilationUnitJava 文件的。