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

pmd / pmd / 4502

13 Mar 2025 12:14PM UTC coverage: 83.089% (+3.4%) from 79.659%
4502

push

github

adangel
[java] Fix crash when parsing class for anonymous class (#5588)

Merge pull request #5588 from oowekyala:fix-anon-class-loading

1912 of 2411 branches covered (79.3%)

Branch coverage included in aggregate %.

29 of 33 new or added lines in 2 files covered. (87.88%)

30 existing lines in 6 files now uncovered.

4559 of 5377 relevant lines covered (84.79%)

14.17 hits per line

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

87.01
/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaNode.java
1
/*
2
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3
 */
4

5
package net.sourceforge.pmd.lang.java.ast;
6

7
import org.checkerframework.checker.nullness.qual.NonNull;
8

9
import net.sourceforge.pmd.lang.ast.AstVisitor;
10
import net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode;
11
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken;
12
import net.sourceforge.pmd.lang.java.symbols.table.JSymbolTable;
13
import net.sourceforge.pmd.lang.java.types.TypeSystem;
14

15
abstract class AbstractJavaNode extends AbstractJjtreeNode<AbstractJavaNode, JavaNode> implements JavaNode {
2✔
16

17
    protected JSymbolTable symbolTable;
18
    private ASTCompilationUnit root;
19

20
    AbstractJavaNode(int id) {
21
        super(id);
2✔
22
    }
2✔
23

24

25
    @Override
26
    public void jjtClose() {
27
        super.jjtClose();
2✔
28
        if (this instanceof LeftRecursiveNode && getNumChildren() > 0) {
2✔
29
            fitTokensToChildren(0);
2✔
30
        }
31
    }
2✔
32
    // override those to make them accessible in this package
33

34
    @Override
35
    @SuppressWarnings("unchecked")
36
    public final <P, R> R acceptVisitor(AstVisitor<? super P, ? extends R> visitor, P data) {
37
        if (visitor instanceof JavaVisitor) {
2!
38
            return this.acceptVisitor((JavaVisitor<? super P, ? extends R>) visitor, data);
2✔
39
        }
40
        return visitor.cannotVisit(this, data);
×
41
    }
42

43
    protected abstract <P, R> R acceptVisitor(JavaVisitor<? super P, ? extends R> visitor, P data);
44

45
    // override those to make them accessible in this package
46

47
    @Override
48
    protected void addChild(AbstractJavaNode child, int index) {
49
        super.addChild(child, index);
2✔
50
    }
2✔
51

52
    @Override // override to make it accessible to tests that build nodes (which have been removed on java-grammar)
53
    protected void insertChild(AbstractJavaNode child, int index) {
54
        super.insertChild(child, index);
2✔
55
    }
2✔
56

57
    @Override
58
    protected void removeChildAtIndex(int childIndex) {
59
        super.removeChildAtIndex(childIndex);
2✔
60
    }
2✔
61

62
    @Override
63
    protected void setImage(String image) {
64
        super.setImage(image);
2✔
65
    }
2✔
66

67

68
    @Override
69
    protected void setFirstToken(JavaccToken token) {
70
        super.setFirstToken(token);
2✔
71
    }
2✔
72

73
    @Override
74
    protected void setLastToken(JavaccToken token) {
75
        super.setLastToken(token);
2✔
76
    }
2✔
77

78
    @Override
79
    protected void enlargeLeft(JavaccToken child) {
80
        super.enlargeLeft(child);
2✔
81
    }
2✔
82

83
    @Override
84
    protected void setChild(AbstractJavaNode child, int index) {
85
        super.setChild(child, index);
2✔
86
    }
2✔
87

88
    void setSymbolTable(JSymbolTable table) {
89
        this.symbolTable = table;
2✔
90
    }
2✔
91

92
    @Override
93
    public @NonNull JSymbolTable getSymbolTable() {
94
        AbstractJavaNode parent = (AbstractJavaNode) getParent();
2✔
95
        JSymbolTable table = symbolTable;
2✔
96
        while (parent != null && table == null) {
2✔
97
            table = parent.getSymbolTable();
2✔
98
            parent = (AbstractJavaNode) parent.getParent();
2✔
99
        }
100
        assert table != null : "Symbol table was not set";
2!
101
        return table;
2✔
102
    }
103

104
    @Override
105
    public TypeSystem getTypeSystem() {
106
        return getRoot().getTypeSystem();
2✔
107
    }
108

109

110

111
    @Override
112
    public final @NonNull ASTCompilationUnit getRoot() {
113
        // storing a reference on each node ensures that each path is roamed
114
        // at most once.
115
        if (root == null) {
2✔
116
            setRoot(getParent().getRoot());
2✔
117
        }
118
        return root;
2✔
119
    }
120

121

122
    /**
123
     * Shift the start and end tokens by the given offsets.
124
     * @throws IllegalStateException if the right shift identifies
125
     * a token that is left of this node
126
     */
127
    void shiftTokens(int leftShift, int rightShift) {
128
        if (leftShift != 0) {
2!
UNCOV
129
            setFirstToken(findTokenSiblingInThisNode(getFirstToken(), leftShift));
×
130
        }
131
        if (rightShift != 0) {
2!
132
            setLastToken(findTokenSiblingInThisNode(getLastToken(), rightShift));
2✔
133
        }
134
    }
2✔
135

136
    private JavaccToken findTokenSiblingInThisNode(JavaccToken token, int shift) {
137
        if (shift == 0) {
2!
UNCOV
138
            return token;
×
139
        } else if (shift < 0) {
2!
140
            // expects a positive shift
141
            return TokenUtils.nthPrevious(getFirstToken(), token, -shift);
2✔
142
        } else {
UNCOV
143
            return TokenUtils.nthFollower(token, shift);
×
144
        }
145
    }
146

147

148
    void copyTextCoordinates(AbstractJavaNode copy) {
149
        setFirstToken(copy.getFirstToken());
2✔
150
        setLastToken(copy.getLastToken());
2✔
151
    }
2✔
152

153
    @Override
154
    public final String getXPathNodeName() {
155
        return JavaParserImplTreeConstants.jjtNodeName[id];
2✔
156
    }
157

158
    void setRoot(ASTCompilationUnit root) {
159
        this.root = root;
2✔
160
    }
2✔
161
}
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