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

pmd / pmd / 43

20 Jun 2025 06:39PM UTC coverage: 78.375% (-0.002%) from 78.377%
43

push

github

adangel
Fix #1639 #5832: Use filtered comment text for UnnecessaryImport (#5833)

Merged pull request #5833 from adangel:java/issue-5832-unnecessaryimport

17714 of 23438 branches covered (75.58%)

Branch coverage included in aggregate %.

3 of 3 new or added lines in 1 file covered. (100.0%)

109 existing lines in 17 files now uncovered.

38908 of 48807 relevant lines covered (79.72%)

0.81 hits per line

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

76.6
/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JavaEscapeTranslator.java
1
/*
2
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3
 */
4

5
package net.sourceforge.pmd.lang.ast.impl.javacc;
6

7
import net.sourceforge.pmd.lang.document.Chars;
8
import net.sourceforge.pmd.lang.document.TextDocument;
9

10
/**
11
 * An implementation of {@link EscapeTranslator} that translates Java
12
 * unicode escapes.
13
 */
14
public final class JavaEscapeTranslator extends BackslashEscapeTranslator {
15

16
    public JavaEscapeTranslator(TextDocument input) {
17
        super(input);
1✔
18
    }
1✔
19

20
    @Override
21
    protected int handleBackslash(final int maxOff, final int firstBackslashOff) throws MalformedSourceException {
22
        int off = firstBackslashOff;
1✔
23
        while (off < input.length() && input.charAt(off) == '\\') {
1!
24
            off++;
1✔
25
        }
26

27
        int bslashCount = off - firstBackslashOff;
1✔
28
        // is there an escape at offset firstBslashOff?
29
        if ((bslashCount & 1) == 1 // odd number of backslashes
1!
30
            && off < input.length() && input.charAt(off) == 'u') { // at least one 'u'
1!
31
            // this is enough to expect an escape or throw an exception
32
            while (off < input.length() && input.charAt(off) == 'u') {
1!
33
                // consume all the 'u's
34
                off++;
1✔
35
            }
36
            Chars value = escapeValue(firstBackslashOff, off - 1);
1✔
37
            int endOffset = off + 4; // + 4 hex digits
1✔
38
            return recordEscape(firstBackslashOff, endOffset, value);
1✔
39
        } else {
40
            return abortEscape(off, maxOff);
1✔
41
        }
42
    }
43

44
    private Chars escapeValue(int posOfFirstBackSlash, final int offOfTheU) throws MalformedSourceException {
45
        int off = offOfTheU;
1✔
46
        try {
47
            char c = (char)
1✔
48
                ( hexVal(input.charAt(++off)) << 12 // SUPPRESS CHECKSTYLE paren pad
1✔
49
                | hexVal(input.charAt(++off)) << 8
1✔
50
                | hexVal(input.charAt(++off)) << 4
1✔
51
                | hexVal(input.charAt(++off))
1✔
52
                );
53

54
            return Chars.wrap(Character.toString(c));
1✔
UNCOV
55
        } catch (NumberFormatException | IndexOutOfBoundsException e) {
×
56
            // cut off u and 4 digits
UNCOV
57
            String escape = input.substring(offOfTheU, Math.min(input.length(), offOfTheU + 5));
×
58
            throw new MalformedSourceException("Invalid unicode escape \\" + escape, e, locationAt(posOfFirstBackSlash));
×
59
        }
60
    }
61

62
    private static int hexVal(char c) {
63
        switch (c) {
1!
64
        case '0':
65
        case '1':
66
        case '2':
67
        case '3':
68
        case '4':
69
        case '5':
70
        case '6':
71
        case '7':
72
        case '8':
73
        case '9':
74
            return c - '0';
1✔
75
        case 'A':
76
        case 'B':
77
        case 'C':
78
        case 'D':
79
        case 'E':
80
        case 'F':
UNCOV
81
            return c - ('A' - 10);
×
82
        case 'a':
83
        case 'b':
84
        case 'c':
85
        case 'd':
86
        case 'e':
87
        case 'f':
88
            return c - ('a' - 10);
1✔
89
        default:
UNCOV
90
            throw new NumberFormatException("Character '" + c + "' is not a valid hexadecimal digit");
×
91
        }
92
    }
93
}
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