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

pmd / pmd / 4553

25 Apr 2025 06:55AM UTC coverage: 77.84% (+0.008%) from 77.832%
4553

push

github

adangel
[core] Support language dialects (#5438)

Merge pull request #5438 from Monits:lang-dialects

17661 of 23654 branches covered (74.66%)

Branch coverage included in aggregate %.

113 of 137 new or added lines in 12 files covered. (82.48%)

20 existing lines in 5 files now uncovered.

38710 of 48765 relevant lines covered (79.38%)

0.8 hits per line

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

84.38
/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/xpath/impl/XPathFunctionDefinition.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.impl;
6

7
import java.util.Objects;
8
import javax.xml.namespace.QName;
9

10
import org.checkerframework.checker.nullness.qual.Nullable;
11

12
import net.sourceforge.pmd.lang.Language;
13
import net.sourceforge.pmd.lang.ast.Node;
14

15
/**
16
 * Base impl for an XPath function definition.
17
 *
18
 * @since 7.0.0
19
 */
20
public abstract class XPathFunctionDefinition {
21

22
    private static final String PMD_URI_PREFIX = "http://pmd.sourceforge.net/";
23
    private final QName qname;
24

25
    private XPathFunctionDefinition(String localName, String namespacePrefix, String uri) {
1✔
26
        this.qname = new QName(uri, localName, namespacePrefix);
1✔
27
    }
1✔
28

29
    protected XPathFunctionDefinition(String localName) {
30
        this(localName, "pmd", PMD_URI_PREFIX + "pmd-core");
1✔
31
    }
1✔
32

33
    protected XPathFunctionDefinition(String localName, Language language) {
34
        this(localName, "pmd-" + language.getId(), PMD_URI_PREFIX + "pmd-" + language.getId());
1✔
35
    }
1✔
36

37
    public final QName getQName() {
38
        return qname;
1✔
39
    }
40

41
    /**
42
     * Defines the types of the function arguments. By default, an empty array is returned, indicating
43
     * that the function takes no arguments.
44
     */
45
    public Type[] getArgumentTypes() {
46
        return new Type[0];
1✔
47
    }
48

49
    /**
50
     * Defines the return type of the function.
51
     */
52
    public abstract Type getResultType();
53

54
    /**
55
     * If the function depends on the context item, then
56
     * this method should return {@code true}.
57
     *
58
     * <p>Note: Only if this is true, the contextNode parameter will be present in the
59
     * {@link FunctionCall#call(Node, Object[])} method.
60
     */
61
    public boolean dependsOnContext() {
62
        return false;
1✔
63
    }
64

65
    /**
66
     * Create a call on this function. This method is called, when a function call
67
     * is found in the XPath expression.
68
     */
69
    public abstract FunctionCall makeCallExpression();
70

71
    @Override
72
    public int hashCode() {
73
        return Objects.hashCode(qname);
1✔
74
    }
75

76
    @Override
77
    public boolean equals(Object o) {
78
        if (this == o) {
1!
NEW
79
            return true;
×
80
        }
81
        if (o == null || getClass() != o.getClass()) {
1!
NEW
82
            return false;
×
83
        }
84
        XPathFunctionDefinition that = (XPathFunctionDefinition) o;
1✔
85
        return Objects.equals(qname, that.qname);
1✔
86
    }
87

88
    /**
89
     * Supported types of a custom XPath function. These can be used as {@link #getResultType() result types}
90
     * or {@link #getArgumentTypes() argument types}.
91
     */
92
    public enum Type {
1✔
93
        /** Represents {@link String}. */
94
        SINGLE_STRING,
1✔
95
        /** Represents {@link Boolean}. */
96
        SINGLE_BOOLEAN,
1✔
97
        /** Represents {@link Integer}. */
98
        SINGLE_INTEGER,
1✔
99
        /** Represents any node. Usually used as an argument type. */
100
        SINGLE_ELEMENT,
1✔
101
        /** Represents a {@link java.util.List} of {@link String}, potentially empty. */
102
        STRING_SEQUENCE,
1✔
103
        /** Represents a {@link java.util.Optional} {@link String}. */
104
        OPTIONAL_STRING,
1✔
105
        /** Represents a {@link java.util.Optional} {@link Double}. */
106
        OPTIONAL_DECIMAL,
1✔
107
    }
108

109
    /**
110
     * Provides the actual implementation of a custom XPath function.
111
     */
112
    public interface FunctionCall {
113
        /**
114
         * This method is called at runtime to evaluate the XPath function expression.
115
         *
116
         * @param contextNode the context node or {@code null}, if this function doesn't depend on the context.
117
         *                    See {@link XPathFunctionDefinition#dependsOnContext()}.
118
         * @param arguments The arguments converted as the corresponding java types.
119
         *                  See {@link XPathFunctionDefinition#getArgumentTypes()}.
120
         * @return The result of the function. This should be the corresponding java type of
121
         * {@link XPathFunctionDefinition#getResultType()}.
122
         * @throws XPathFunctionException when any problem during evaluation occurs, like invalid arguments.
123
         */
124
        Object call(@Nullable Node contextNode, Object[] arguments) throws XPathFunctionException;
125

126
        /**
127
         * This is called once before the function is evaluated. It can be used to optimize the
128
         * implementation by doing expensive operations only once and cache the result.
129
         * This is useful, if the argument of the function is of type {@link String} and is provided
130
         * as a String literal in the XPath expression.
131
         *
132
         * <p>This is an optional step. The default implementation does nothing.
133
         *
134
         * @param arguments The arguments converted as the corresponding java types.
135
         *                  See {@link XPathFunctionDefinition#getArgumentTypes()}.
136
         *                  Note: This array might contain {@code null} elements, if the values are
137
         *                  not known yet because they are dynamic. Only literal values are available.
138
         * @throws XPathFunctionException when any problem during initialization occurs, like invalid arguments.
139
         */
140
        default void staticInit(Object[] arguments) throws XPathFunctionException {
141
            // default implementation does nothing
142
        }
1✔
143
    }
144
}
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