How to suppress Java warnings for specific directories or files such as generated code

后端 未结 13 1552
太阳男子
太阳男子 2020-12-02 07:13

I\'m using a parser generator that creates somewhat ugly code. As a result my Eclipse project has several dozen warnings emanating from generated source files. I know I can

相关标签:
13条回答
  • 2020-12-02 07:50

    Starting with version 3.8 M6, Eclipse (to be exact: the JDT) has built-in functionality for this. It is configurable through a project's build path: Project properties > Java Build Path > Compiler > Source

    enter image description here

    Announced here: Eclipse 3.8 and 4.2 M6 - New and Noteworthy, called Selectively ignore errors/warnings from source folders. That's also where the screenshot is from. This is the new feature developed on the previously linked Bug 220928.

    0 讨论(0)
  • 2020-12-02 07:50

    I don't think Eclipse inherently provides a way to do this at the directory level (but I'm not sure).

    You could have the generated files go into a separate Java project, and control warnings for that specific project.

    I generally prefer to place automatically-generated code in a separate project anyway.

    0 讨论(0)
  • 2020-12-02 07:50

    In the case of ANTLR 2, it is possible to suppress warnings in generated code by appenidng @SuppressWarnings before the class declaration in the grammar file, e.g.

    {@SuppressWarnings("all")} class MyBaseParser extends Parser;
    
    0 讨论(0)
  • 2020-12-02 07:52

    There is a ticket for this, Bug 220928, that has since been completed for Eclipse 3.8. Please see this answer for details.

    If you're stuck with Eclipse 3.7 or lower: The user "Marc" commenting on that ticket created (or at least links to) a plugin called 'warningcleaner' in comment 35. I'm using that with a lot of success while waiting for this feature to be integrated into Eclipse.

    It's really quite simple:

    1. Install plugin.
    2. Right-click project and select "Add/remove generated code nature".
    3. Open the project settings (right-click and select "properties").
    4. Open the tab 'Warning Cleaner'.
    5. Select the source folders you want to ignore the warnings from.

    Warning Cleaner screenshot

    0 讨论(0)
  • 2020-12-02 07:52

    You can only suppress warnings at the project level. However, you can configure your problems tab to suppress warnings from files or packages. Go into the Configure Contents menu and work with the "On working set:" scope.

    0 讨论(0)
  • 2020-12-02 07:56

    This small python script "patches" the M2E-generated .classpath files and adds the required XML tag to all source folders starting with target/generated-sources. You can just run it from you project's root folder. Obviously you need to re-run it when the Eclipse project information is re-generated from M2E. And all at your own risk, obviously ;-)

    #!/usr/bin/env python
    from xml.dom.minidom import parse
    import glob
    import os
    
    print('Reading .classpath files...')
    for root, dirs, files in os.walk('.'):
        for name in files:
            if (name == '.classpath'):
                classpathFile = os.path.join(root, name)
                print('Patching file:' + classpathFile)
                classpathDOM = parse(classpathFile)
                classPathEntries = classpathDOM.getElementsByTagName('classpathentry')
                for classPathEntry in classPathEntries:
                    if classPathEntry.attributes["path"].value.startswith('target/generated-sources'):
                        # ensure that the <attributes> tag exists
                        attributesNode = None;
                        for attributes in classPathEntry.childNodes:
                                if (attributes.nodeName == 'attributes'):
                                    attributesNode = attributes
    
                        if (attributesNode == None):
                            attributesNode = classpathDOM.createElement('attributes')
                            classPathEntry.appendChild(attributesNode)
    
                        # search if the 'ignore_optional_problems' entry exists
                        hasBeenSet = 0
                        for node in attributesNode.childNodes:
                            if (node.nodeName == 'attribute' and node.getAttribute('name') == 'ignore_optional_problems'):
                                # it exists, make sure its value is true
                                node.setAttribute('value','true')
                                #print(node.getAttribute('name'))
                                hasBeenSet = 1
    
                        if (not(hasBeenSet)):
                            # it does not exist, add it
                            x = classpathDOM.createElement("attribute")
                            x.setAttribute('name','ignore_optional_problems')
                            x.setAttribute('value','true')
                            attributesNode.appendChild(x)
    
                try:
                    f = open(classpathFile, "w") 
                    classpathDOM.writexml(f)
                    print('Writing file:' + classpathFile)
                finally:
                    f.close()
    print('Done.')
    
    0 讨论(0)
提交回复
热议问题