• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmd / pmd / 130

06 Sep 2025 12:33PM UTC coverage: 78.533% (+0.04%) from 78.498%
130

push

github

oowekyala
Fix #4770: [java] UnusedFormalParameter should ignore public constructor as same as method (#5994)

Merge branch 'issue-4770-UnusedFormalParameter-should-ignore-public-constructor-as-same-as-method'

17953 of 23697 branches covered (75.76%)

Branch coverage included in aggregate %.

3 of 3 new or added lines in 2 files covered. (100.0%)

27 existing lines in 6 files now uncovered.

39280 of 49181 relevant lines covered (79.87%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.16
/pmd-test-schema/src/main/java/net/sourceforge/pmd/test/schema/TestSchemaParser.java
1
/*
2
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3
 */
4

5
package net.sourceforge.pmd.test.schema;
6

7
import java.io.IOException;
8
import java.text.MessageFormat;
9
import java.util.function.Consumer;
10
import javax.xml.parsers.DocumentBuilder;
11
import javax.xml.parsers.DocumentBuilderFactory;
12
import javax.xml.parsers.ParserConfigurationException;
13

14
import org.xml.sax.InputSource;
15

16
import net.sourceforge.pmd.lang.rule.Rule;
17

18
import com.github.oowekyala.ooxml.messages.NiceXmlMessageSpec;
19
import com.github.oowekyala.ooxml.messages.OoxmlFacade;
20
import com.github.oowekyala.ooxml.messages.PositionedXmlDoc;
21
import com.github.oowekyala.ooxml.messages.PrintStreamMessageHandler;
22
import com.github.oowekyala.ooxml.messages.XmlException;
23
import com.github.oowekyala.ooxml.messages.XmlMessageReporter;
24
import com.github.oowekyala.ooxml.messages.XmlMessageReporterBase;
25
import com.github.oowekyala.ooxml.messages.XmlPosition;
26
import com.github.oowekyala.ooxml.messages.XmlPositioner;
27
import com.github.oowekyala.ooxml.messages.XmlSeverity;
28

29

30
/**
31
 * Entry point to parse a test file.
32
 *
33
 * @author Clément Fournier
34
 */
35
public class TestSchemaParser {
36

37
    private final TestSchemaVersion version;
38

39
    TestSchemaParser(TestSchemaVersion version) {
1✔
40
        this.version = version;
1✔
41
    }
1✔
42

43
    public TestSchemaParser() {
44
        this(TestSchemaVersion.V1);
1✔
45
    }
1✔
46

47
    /**
48
     * Entry point to parse a test file.
49
     *
50
     * @param rule        Rule which owns the tests
51
     * @param inputSource Where to access the test file to parse
52
     *
53
     * @return A test collection, possibly incomplete
54
     *
55
     * @throws IOException  If parsing throws this
56
     * @throws XmlException If parsing throws this
57
     */
58
    public RuleTestCollection parse(Rule rule, InputSource inputSource) throws IOException, XmlException {
59
        class ErrorHandler extends PrintStreamMessageHandler {
60
            private boolean hasError = false;
1✔
61

62
            ErrorHandler() {
1✔
63
                // note: need to explicitly specify the writer here, so that in unit tests
64
                // System.err can be swapped out and in with SystemLambda.tapSystemErr
65
                super(System.err);
1✔
66
            }
1✔
67

68
            @Override
69
            public void accept(XmlException entry) {
70
                super.accept(entry);
1✔
71
                hasError |= entry.getSeverity() == XmlSeverity.ERROR;
1✔
72
            }
1✔
73

74
            public boolean hasError() {
75
                return hasError;
1✔
76
            }
77
        }
78

79
        ErrorHandler validationHandler = new ErrorHandler();
1✔
80
        OoxmlFacade ooxml = new OoxmlFacade().withPrinter(validationHandler);
1✔
81
        PositionedXmlDoc doc = ooxml.parse(newDocumentBuilder(), inputSource);
1✔
82

83
        try (PmdXmlReporterImpl err = new PmdXmlReporterImpl(ooxml, doc.getPositioner())) {
1✔
84
            RuleTestCollection collection = version.getParserImpl().parseDocument(rule, doc, err);
1✔
85
            if (validationHandler.hasError() || err.hasError()) {
1!
86
                // todo maybe add a way not to throw here
87
                throw new IllegalStateException("Errors were encountered while parsing XML tests");
1✔
88
            }
89
            return collection;
1✔
90
        }
91
    }
92

93
    interface PmdXmlReporter extends XmlMessageReporter<Reporter> {
94

95
        boolean hasError();
96

97
        PmdXmlReporter newScope();
98
    }
99

100
    private static class PmdXmlReporterImpl
101
        extends XmlMessageReporterBase<Reporter>
102
        implements PmdXmlReporter {
103

104
        private boolean hasError;
105

106
        protected PmdXmlReporterImpl(OoxmlFacade ooxml,
107
                                     XmlPositioner positioner) {
108
            super(ooxml, positioner);
1✔
109
        }
1✔
110

111
        @Override
112
        protected Reporter create2ndStage(XmlPosition position, XmlPositioner positioner) {
113
            return new Reporter(position, positioner, ooxml, this::handleEx);
1✔
114
        }
115

116
        @Override
117
        protected void handleEx(XmlException e) {
118
            super.handleEx(e);
1✔
119
            hasError |= e.getSeverity() == XmlSeverity.ERROR;
1✔
120
        }
1✔
121

122
        @Override
123
        public PmdXmlReporter newScope() {
124
            return new PmdXmlReporterImpl(ooxml, positioner) {
1✔
125
                @Override
126
                protected void handleEx(XmlException e) {
127
                    super.handleEx(e);
1✔
128
                    PmdXmlReporterImpl.this.hasError |= this.hasError();
1✔
129
                }
1✔
130
            };
131
        }
132

133
        @Override
134
        public boolean hasError() {
135
            return hasError;
1✔
136
        }
137

138
    }
139

140
    private DocumentBuilder newDocumentBuilder() {
141
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
1✔
142
        try {
143
            dbf.setSchema(version.getSchema());
1✔
144
            dbf.setNamespaceAware(true);
1✔
145
            return dbf.newDocumentBuilder();
1✔
UNCOV
146
        } catch (ParserConfigurationException e) {
×
UNCOV
147
            throw new RuntimeException(e);
×
148
        }
149
    }
150

151

152
    static final class Reporter {
153

154
        private final XmlPosition position;
155
        private final XmlPositioner positioner;
156
        private final OoxmlFacade ooxml;
157

158
        private final Consumer<XmlException> handler;
159

160
        private Reporter(XmlPosition position, XmlPositioner positioner, OoxmlFacade ooxml, Consumer<XmlException> handler) {
1✔
161
            this.position = position;
1✔
162
            this.positioner = positioner;
1✔
163
            this.ooxml = ooxml;
1✔
164
            this.handler = handler;
1✔
165
        }
1✔
166

167
        public void warn(String messageFormat, Object... args) {
168
            reportImpl(XmlSeverity.WARNING, MessageFormat.format(messageFormat, args));
1✔
169

170
        }
1✔
171

172
        public void error(String messageFormat, Object... args) {
173
            reportImpl(XmlSeverity.ERROR, MessageFormat.format(messageFormat, args));
1✔
174
        }
1✔
175

176
        private void reportImpl(XmlSeverity severity, String formattedMessage) {
177
            NiceXmlMessageSpec spec =
1✔
178
                new NiceXmlMessageSpec(position, formattedMessage)
179
                    .withSeverity(severity);
1✔
180
            String fullMessage = ooxml.getFormatter().formatSpec(ooxml, spec, positioner);
1✔
181
            XmlException ex = new XmlException(spec, fullMessage);
1✔
182
            handler.accept(ex);
1✔
183
        }
1✔
184
    }
185

186
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc