Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/pmdPublic

Commite50ac05

Browse files
committed
[core]Fix#6330: Cannot access Chars attribute from XPath (#6342)
2 parentse9579d7 +bbb7438 commite50ac05

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

‎docs/pages/release_notes.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ This is a {{ site.pmd.release_type }} release.
2525
###🚀️ New and noteworthy
2626

2727
###🐛️ Fixed Issues
28+
* core
29+
*[#6330](https://github.com/pmd/pmd/issues/6330):\[core] "Unable to create ValueRepresentation" when using @<!---->LiteralText (XPath)
2830
* java-bestpractices
2931
*[#6028](https://github.com/pmd/pmd/issues/6028):\[java] UnusedPrivateMethod: False positive with raw type for generic method
3032
*[#6257](https://github.com/pmd/pmd/issues/6257):\[java] UnusedLocalVariable: False positive with instanceof pattern guard

‎pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/xpath/Attribute.java‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
importjava.lang.reflect.Method;
99
importjava.lang.reflect.Type;
1010
importjava.util.Objects;
11+
importjava.util.function.Function;
1112

1213
importorg.checkerframework.checker.nullness.qual.NonNull;
1314
importorg.checkerframework.checker.nullness.qual.Nullable;
1415
importorg.slf4j.Logger;
1516
importorg.slf4j.LoggerFactory;
1617

18+
importnet.sourceforge.pmd.annotation.InternalApi;
1719
importnet.sourceforge.pmd.lang.ast.Node;
1820

1921
/**
@@ -47,6 +49,7 @@ public final class Attribute {
4749

4850
/** Must be non-null after {@link #getStringValue()} has been invoked. */
4951
privateStringstringValue;
52+
privatefinalFunction<Method,Type>getType;
5053

5154
/**
5255
* Creates a new attribute belonging to the given node using its accessor.
@@ -62,16 +65,35 @@ public Attribute(@NonNull Node parent, @NonNull String name, @NonNull MethodHand
6265
this.name =Objects.requireNonNull(name);
6366
this.handle =Objects.requireNonNull(handle);
6467
this.method =Objects.requireNonNull(method);
68+
this.getType =Method::getGenericReturnType;
6569
}
6670

6771
/** Creates a new attribute belonging to the given node using its string value. */
6872
publicAttribute(@NonNullNodeparent,@NonNullStringname,@NullableStringvalue) {
73+
this(parent,name,value,m ->String.class);
74+
}
75+
76+
/**
77+
* Creates a new attribute belonging to the given node using an arbitrary object value and a type.
78+
* Note that the type must be supported by {@link net.sourceforge.pmd.lang.rule.xpath.internal.DomainConversion}
79+
* if you want this attribute to be used from XPath.
80+
*
81+
* @apiNote Internal API
82+
* @since 7.20.0
83+
*/
84+
@InternalApi// used for tests
85+
publicAttribute(@NonNullNodeparent,@NonNullStringname,@NullableObjectvalue,Typetype) {
86+
this(parent,name,value,m ->type);
87+
}
88+
89+
privateAttribute(@NonNullNodeparent,@NonNullStringname,@NullableObjectvalue,Function<Method,Type>getType) {
6990
this.parent =Objects.requireNonNull(parent);
7091
this.name =Objects.requireNonNull(name);
92+
this.getType =getType;
7193
this.value =value;
7294
this.handle =null;
7395
this.method =null;
74-
this.stringValue =value ==null ?"" :value;
96+
this.stringValue =value ==null ?"" :value.toString();
7597
this.invoked =true;
7698
}
7799

@@ -80,7 +102,7 @@ public Attribute(@NonNull Node parent, @NonNull String name, @Nullable String va
80102
* Gets the generic type of the value of this attribute.
81103
*/
82104
publicTypegetType() {
83-
returnmethod ==null ?String.class :method.getGenericReturnType();
105+
returngetType.apply(this.method);
84106
}
85107

86108
/** Return the name of the attribute (without leading @ sign). */

‎pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/xpath/internal/DomainConversion.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
importnet.sf.saxon.om.AtomicArray;
1515
importnet.sf.saxon.om.AtomicSequence;
1616
importnet.sf.saxon.om.EmptyAtomicSequence;
17+
importnet.sf.saxon.str.StringTool;
1718
importnet.sf.saxon.type.BuiltInAtomicType;
1819
importnet.sf.saxon.type.SchemaType;
1920
importnet.sf.saxon.value.AtomicValue;
@@ -146,6 +147,8 @@ private static void flattenInto(Collection<?> list, List<AtomicValue> values) {
146147
returnnewFloatValue((Float)value);
147148
}elseif (valueinstanceofPattern ||valueinstanceofEnum) {
148149
returnnewStringValue(String.valueOf(value));
150+
}elseif (valueinstanceofCharSequence) {
151+
returnnewStringValue(StringTool.fromCharSequence((CharSequence)value));
149152
}else {
150153
// We could maybe use UntypedAtomicValue
151154
thrownewRuntimeException("Unable to create ValueRepresentation for value of type: " +value.getClass());

‎pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/DummyNode.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
packagenet.sourceforge.pmd.lang.ast;
66

7+
importjava.lang.reflect.Type;
78
importjava.util.ArrayList;
89
importjava.util.Arrays;
910
importjava.util.Iterator;
@@ -116,6 +117,9 @@ public void setXPathAttribute(String name, String value) {
116117
attributes.add(newAttribute(this,name,value));
117118
}
118119

120+
publicvoidsetXPathAttribute(Stringname,Objectvalue,Typetype) {
121+
attributes.add(newAttribute(this,name,value,type));
122+
}
119123

120124
publicvoidclearXPathAttributes() {
121125
attributes.clear();

‎pmd-core/src/test/java/net/sourceforge/pmd/lang/rule/xpath/internal/SaxonXPathRuleQueryTest.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
importnet.sourceforge.pmd.lang.ast.DummyNodeWithListAndEnum;
2929
importnet.sourceforge.pmd.lang.ast.Node;
3030
importnet.sourceforge.pmd.lang.ast.RootNode;
31+
importnet.sourceforge.pmd.lang.document.Chars;
3132
importnet.sourceforge.pmd.lang.rule.xpath.PmdXPathException;
3233
importnet.sourceforge.pmd.lang.rule.xpath.XPathVersion;
3334
importnet.sourceforge.pmd.lang.rule.xpath.impl.XPathHandler;
@@ -140,6 +141,19 @@ void testListAttributes() {
140141
assertEquals(dummy.getChild(0),result.get(0));
141142
}
142143

144+
@Test
145+
voidtestCharsAttributes() {
146+
DummyRootNodedummy =helper.parse("(a(b))");
147+
Charsvalue =Chars.wrap("__abcd__").slice(2,4);
148+
// ----
149+
dummy.setXPathAttribute("CharsAttr",value,Chars.class);
150+
151+
List<Node>result =assertQuery(1,
152+
"//dummyRootNode[@CharsAttr = 'abcd']",dummy);
153+
154+
assertEquals(dummy,result.get(0));
155+
}
156+
143157
@Test
144158
voidruleChainVisits() {
145159
SaxonXPathRuleQueryquery =createQuery("//dummyNode[@Image='baz']/foo | //bar[@Public = 'true'] | //dummyNode[@Public = false()] | //dummyNode");

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp