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

pmd / pmd / 277

27 Nov 2025 01:37PM UTC coverage: 78.778% (+0.03%) from 78.749%
277

push

github

adangel
[java] UseArraysAsList: skip when if-statements (#6228)

18419 of 24233 branches covered (76.01%)

Branch coverage included in aggregate %.

40090 of 50038 relevant lines covered (80.12%)

0.81 hits per line

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

41.32
/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/xpath/internal/DomainConversion.java
1
/*
2
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3
 */
4

5
package net.sourceforge.pmd.lang.rule.xpath.internal;
6

7
import java.util.ArrayList;
8
import java.util.Collection;
9
import java.util.List;
10
import java.util.regex.Pattern;
11

12
import org.checkerframework.checker.nullness.qual.NonNull;
13

14
import net.sf.saxon.om.AtomicArray;
15
import net.sf.saxon.om.AtomicSequence;
16
import net.sf.saxon.om.EmptyAtomicSequence;
17
import net.sf.saxon.type.BuiltInAtomicType;
18
import net.sf.saxon.type.SchemaType;
19
import net.sf.saxon.value.AtomicValue;
20
import net.sf.saxon.value.BigIntegerValue;
21
import net.sf.saxon.value.BooleanValue;
22
import net.sf.saxon.value.DoubleValue;
23
import net.sf.saxon.value.FloatValue;
24
import net.sf.saxon.value.Int64Value;
25
import net.sf.saxon.value.SequenceType;
26
import net.sf.saxon.value.StringValue;
27

28

29
/**
30
 * Converts Java values into XPath values.
31
 */
32
public final class DomainConversion {
33

34
    private DomainConversion() {
35

36
    }
37

38

39
    public static SchemaType buildType(java.lang.reflect.Type type) {
40
        switch (type.getTypeName()) {
1!
41
        case "java.lang.Integer":
42
        case "java.lang.Long":
43
            return BuiltInAtomicType.INTEGER;
×
44
        case "java.lang.Double":
45
        case "java.lang.Float":
46
            return BuiltInAtomicType.DOUBLE;
×
47
        case "java.lang.String":
48
        case "java.lang.Character":
49
        case "java.lang.Class":
50
        case "java.util.regex.Pattern":
51
            return BuiltInAtomicType.STRING;
1✔
52
        default:
53
            return BuiltInAtomicType.UNTYPED_ATOMIC;
1✔
54
        }
55
    }
56

57
    public static @NonNull AtomicSequence convert(Object obj) {
58
        if (obj instanceof Collection) {
1✔
59
            return getSequenceRepresentation((Collection<?>) obj);
1✔
60
        }
61
        return getAtomicRepresentation(obj);
1✔
62
    }
63

64
    public static SequenceType typeOf(Object obj) {
65
        if (obj instanceof Collection) {
×
66
            if (((Collection<?>) obj).isEmpty()) {
×
67
                return SequenceType.EMPTY_SEQUENCE;
×
68
            }
69
            return SequenceType.NON_EMPTY_SEQUENCE;
×
70
        } else if (obj instanceof String) {
×
71
            return SequenceType.SINGLE_STRING;
×
72
        } else if (obj instanceof Boolean) {
×
73
            return SequenceType.SINGLE_BOOLEAN;
×
74
        } else if (obj instanceof Integer) {
×
75
            return SequenceType.SINGLE_INTEGER;
×
76
        } else if (obj instanceof Float) {
×
77
            return SequenceType.SINGLE_FLOAT;
×
78
        } else if (obj instanceof Long) {
×
79
            return SequenceType.SINGLE_INTEGER;
×
80
        } else if (obj instanceof Double) {
×
81
            return SequenceType.SINGLE_DOUBLE;
×
82
        } else if (obj instanceof Number) {
×
83
            return SequenceType.SINGLE_NUMERIC;
×
84
        } else if (obj instanceof Enum<?>) {
×
85
            return SequenceType.SINGLE_STRING;
×
86
        } else if (obj instanceof Character) {
×
87
            return SequenceType.SINGLE_STRING;
×
88
        } else if (obj instanceof Pattern) {
×
89
            return SequenceType.SINGLE_STRING;
×
90
        }
91
        return SequenceType.SINGLE_ITEM;
×
92
    }
93

94
    public static AtomicSequence getSequenceRepresentation(Collection<?> list) {
95
        if (list == null || list.isEmpty()) {
1!
96
            return EmptyAtomicSequence.getInstance();
×
97
        }
98
        List<AtomicValue> vs = new ArrayList<>(list.size());
1✔
99
        flattenInto(list, vs);
1✔
100
        return new AtomicArray(vs);
1✔
101
    }
102

103
    // sequences cannot be nested, this takes care of list of lists,
104
    // just in case
105
    private static void flattenInto(Collection<?> list, List<AtomicValue> values) {
106
        for (Object o : list) {
1✔
107
            if (o instanceof Collection) {
1!
108
                flattenInto((Collection<?>) o, values);
×
109
            } else {
110
                values.add(getAtomicRepresentation(o));
1✔
111
            }
112
        }
1✔
113
    }
1✔
114

115

116
    /**
117
     * Gets the Saxon representation of the parameter, if its type corresponds
118
     * to an XPath 2.0 atomic datatype.
119
     *
120
     * @param value The value to convert
121
     *
122
     * @return The converted AtomicValue
123
     */
124
    public static @NonNull AtomicValue getAtomicRepresentation(final Object value) {
125

126
        /*
127
        FUTURE When supported, we should consider refactor this implementation to use Pattern Matching
128
        (see http://openjdk.java.net/jeps/305) so that it looks clearer.
129
        */
130
        if (value == null) {
1!
131
            return StringValue.ZERO_LENGTH_UNTYPED;
×
132

133
        } else if (value instanceof String) {
1✔
134
            return new StringValue((String) value);
1✔
135
        } else if (value instanceof Boolean) {
1✔
136
            return BooleanValue.get((Boolean) value);
1✔
137
        } else if (value instanceof Integer) {
1✔
138
            return Int64Value.makeIntegerValue((Integer) value);
1✔
139
        } else if (value instanceof Long) {
1!
140
            return new BigIntegerValue((Long) value);
×
141
        } else if (value instanceof Double) {
1!
142
            return new DoubleValue((Double) value);
×
143
        } else if (value instanceof Character) {
1!
144
            return new StringValue(value.toString());
×
145
        } else if (value instanceof Float) {
1!
146
            return new FloatValue((Float) value);
×
147
        } else if (value instanceof Pattern || value instanceof Enum) {
1!
148
            return new StringValue(String.valueOf(value));
1✔
149
        } else {
150
            // We could maybe use UntypedAtomicValue
151
            throw new RuntimeException("Unable to create ValueRepresentation for value of type: " + value.getClass());
×
152
        }
153
    }
154
}
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