Skip to content

Commit e4641f1

Browse files
committed
[Java] Validate the type is correct when using a valueRef for a enum constant. Issue aeron-io#528.
1 parent fcf27d7 commit e4641f1

File tree

1 file changed

+25
-2
lines changed
  • sbe-tool/src/main/java/uk/co/real_logic/sbe/xml

1 file changed

+25
-2
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/xml/Field.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,34 @@ public void validate(final Node node)
9696

9797
checkForValidName(node, name);
9898

99+
if (presence == Presence.CONSTANT && null != valueRef)
100+
{
101+
final String typeName = type == null ? null : type.name();
102+
103+
if (!(type instanceof EnumType))
104+
{
105+
handleError(node, "valueRef only valid for enum constants, type is " + typeName);
106+
}
107+
108+
final int periodIndex = valueRef.indexOf('.');
109+
if (periodIndex < 1 || periodIndex == (valueRef.length() - 1))
110+
{
111+
handleError(
112+
node, "valueRef format not valid for constant (enum-name.valid-value-name): " + valueRef);
113+
}
114+
115+
final String valueRefType = valueRef.substring(0, periodIndex);
116+
if (!valueRefType.equals(typeName))
117+
{
118+
handleError(node, "valueRef type " + valueRefType + " does not match " + typeName);
119+
}
120+
}
121+
99122
if (type instanceof EnumType && presence == Presence.CONSTANT)
100123
{
101124
if (null == valueRef)
102125
{
103-
handleError(node, "valueRef not set for constant Enum");
126+
handleError(node, "valueRef not set for constant enum");
104127
}
105128
else
106129
{
@@ -114,7 +137,7 @@ public void validate(final Node node)
114137
final String valueRefType = valueRef.substring(0, periodIndex);
115138
if (!valueRefType.equals(type.name()))
116139
{
117-
handleError(node, "valueRef for Enum name not found: " + valueRefType);
140+
handleError(node, "valueRef for enum name not found: " + valueRefType);
118141
}
119142

120143
final String validValueName = valueRef.substring(periodIndex + 1);

0 commit comments

Comments
 (0)