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

pmd / pmd / #3722

pending completion
#3722

push

github actions

adangel
Suppress MissingOverride for Chars::isEmpty (#4291)

67270 of 127658 relevant lines covered (52.7%)

0.53 hits per line

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

96.36
/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/JPrimitiveType.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.types;
6

7
import java.util.Locale;
8
import java.util.Objects;
9
import java.util.Set;
10
import java.util.function.Function;
11

12
import org.checkerframework.checker.nullness.qual.NonNull;
13
import org.checkerframework.checker.nullness.qual.Nullable;
14
import org.pcollections.PSet;
15

16
import net.sourceforge.pmd.lang.java.symbols.JClassSymbol;
17
import net.sourceforge.pmd.lang.java.symbols.SymbolicValue.SymAnnot;
18

19
/**
20
 * Mirror a primitive types. Even though {@code void.class.isPrimitive()}
21
 * returns true, we don't treat it as such and represent it with
22
 * {@link TypeSystem#NO_TYPE} instead.
23
 */
24
public final class JPrimitiveType implements JTypeMirror {
25

26
    Set<JTypeMirror> superTypes; // set by the constructor of TypeSystem
27

28
    private final TypeSystem ts;
29

30
    private final PrimitiveTypeKind kind;
31
    /** Primitive class. */
32
    private final JClassSymbol type;
33
    /** Boxed representation. */
34
    private final JClassType box;
35
    private final PSet<SymAnnot> typeAnnots;
36

37
    JPrimitiveType(TypeSystem ts, PrimitiveTypeKind kind, JClassSymbol type, JClassSymbol boxType, PSet<SymAnnot> typeAnnots) {
1✔
38
        this.ts = ts;
1✔
39
        this.kind = kind;
1✔
40
        this.type = type;
1✔
41
        this.typeAnnots = typeAnnots;
1✔
42
        this.box = new BoxedPrimitive(ts, boxType, this, typeAnnots); // not erased
1✔
43
    }
1✔
44

45
    @Override
46
    public PSet<SymAnnot> getTypeAnnotations() {
47
        return typeAnnots;
1✔
48
    }
49

50
    @Override
51
    public JTypeMirror withAnnotations(PSet<SymAnnot> newTypeAnnots) {
52
        if (newTypeAnnots.isEmpty() && this.typeAnnots.isEmpty()) {
1✔
53
            return this;
1✔
54
        }
55
        return new JPrimitiveType(ts, kind, type, box.getSymbol(), newTypeAnnots);
1✔
56
    }
57

58
    @Override
59
    public TypeSystem getTypeSystem() {
60
        return ts;
1✔
61
    }
62

63
    @Override
64
    public JClassType box() {
65
        return box;
1✔
66
    }
67

68
    @Override
69
    public JPrimitiveType unbox() {
70
        return this;
1✔
71
    }
72

73
    @Override
74
    public JTypeMirror getErasure() {
75
        return this;
1✔
76
    }
77

78
    /**
79
     * Returns the type of the primitive class, eg {@link Integer#TYPE}.
80
     * The returned type {@link Class#isPrimitive()} is true.
81
     */
82
    @Override
83
    public @NonNull JClassSymbol getSymbol() {
84
        return type;
1✔
85
    }
86

87
    @Override
88
    public boolean isNumeric() {
89
        return kind != PrimitiveTypeKind.BOOLEAN;
1✔
90
    }
91

92

93
    @Override
94
    public boolean isPrimitive(PrimitiveTypeKind kind) {
95
        return this.kind == Objects.requireNonNull(kind, "null kind");
1✔
96
    }
97

98
    @Override
99
    public boolean isFloatingPoint() {
100
        return kind == PrimitiveTypeKind.DOUBLE || kind == PrimitiveTypeKind.FLOAT;
1✔
101
    }
102

103
    @Override
104
    public boolean isIntegral() {
105
        return kind != PrimitiveTypeKind.BOOLEAN && !isFloatingPoint();
×
106
    }
107

108
    @Override
109
    public boolean isPrimitive() {
110
        return true;
1✔
111
    }
112

113
    @Override
114
    public Set<JTypeMirror> getSuperTypeSet() {
115
        return superTypes;
1✔
116
    }
117

118
    @Override
119
    public String toString() {
120
        return TypePrettyPrint.prettyPrint(this);
1✔
121
    }
122

123
    @Override
124
    public boolean equals(Object obj) {
125
        return obj instanceof JPrimitiveType && ((JPrimitiveType) obj).kind == this.kind;
1✔
126
    }
127

128
    @Override
129
    public int hashCode() {
130
        return kind.hashCode();
1✔
131
    }
132

133
    /**
134
     * Returns the token used to represent the type in source,
135
     * e.g. "int" or "double".
136
     */
137
    public @NonNull String getSimpleName() {
138
        return kind.name;
1✔
139
    }
140

141
    public PrimitiveTypeKind getKind() {
142
        return kind;
1✔
143
    }
144

145
    @Override
146
    public <T, P> T acceptVisitor(JTypeVisitor<T, P> visitor, P p) {
147
        return visitor.visitPrimitive(this, p);
1✔
148
    }
149

150
    @Override
151
    public JTypeMirror subst(Function<? super SubstVar, ? extends @NonNull JTypeMirror> subst) {
152
        return this;
1✔
153
    }
154

155
    public enum PrimitiveTypeKind {
1✔
156
        BOOLEAN(boolean.class),
1✔
157
        CHAR(char.class),
1✔
158
        BYTE(byte.class),
1✔
159
        SHORT(short.class),
1✔
160
        INT(int.class),
1✔
161
        LONG(long.class),
1✔
162
        FLOAT(float.class),
1✔
163
        DOUBLE(double.class);
1✔
164

165
        final String name = name().toLowerCase(Locale.ROOT);
1✔
166
        private final Class<?> jvm;
167

168
        PrimitiveTypeKind(Class<?> jvm) {
1✔
169
            this.jvm = jvm;
1✔
170
        }
1✔
171

172
        public String getSimpleName() {
173
            return name;
1✔
174
        }
175

176
        @Override
177
        public String toString() {
178
            return name;
1✔
179
        }
180

181
        public Class<?> jvmRepr() {
182
            return jvm;
×
183
        }
184

185
        /**
186
         * Gets an enum constant from the token used to represent it in source,
187
         * e.g. "int" or "double". Note that "void" is not a valid primitive name
188
         * in this API, and this would return null in this case.
189
         *
190
         * @param token String token
191
         *
192
         * @return A constant, or null if the string doesn't correspond
193
         *     to a primitive type
194
         */
195
        public static @Nullable PrimitiveTypeKind fromName(String token) {
196
            switch (token) {
1✔
197
            case "boolean": return BOOLEAN;
1✔
198
            case "char": return CHAR;
1✔
199
            case "byte": return BYTE;
1✔
200
            case "short": return SHORT;
1✔
201
            case "int": return INT;
1✔
202
            case "long": return LONG;
1✔
203
            case "float": return FLOAT;
1✔
204
            case "double": return DOUBLE;
1✔
205
            default:
206
                return null;
1✔
207
            }
208
        }
209
    }
210

211
}
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