Skip to content

Commit 5a7abd6

Browse files
committed
Issue #11.
1 parent 5b22d92 commit 5a7abd6

File tree

5 files changed

+39
-32
lines changed

5 files changed

+39
-32
lines changed

basic/src/main/java/org/jvnet/jaxb2_commons/plugin/simpleequals/SimpleEqualsPlugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ protected void generate(ClassOutline classOutline, JDefinedClass theClass) {
9898

9999
final JType type = leftFieldAccessor.getType();
100100
final JVar leftValue = block.decl(type, "left" + name);
101-
leftFieldAccessor.getValue(block, leftValue, false);
101+
leftFieldAccessor.toRawValue(block, leftValue);
102102

103103
final JVar rightValue = block.decl(
104104
rightFieldAccessor.getType(), "right" + name);
105-
rightFieldAccessor.getValue(block, rightValue, false);
105+
rightFieldAccessor.toRawValue(block, rightValue);
106106

107107
final JType exposedType = leftFieldAccessor.getType();
108108

basic/src/main/java/org/jvnet/jaxb2_commons/plugin/simplehashcode/SimpleHashCodePlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected void generate(ClassOutline classOutline, JDefinedClass theClass) {
9999
final JVar value = block.decl(fieldAccessor.getType(),
100100
"the" + propertyName);
101101

102-
fieldAccessor.getValue(block, value, false);
102+
fieldAccessor.toRawValue(block, value);
103103
final JType exposedType = fieldAccessor.getType();
104104

105105
final Collection<JType> possibleTypes = FieldUtils

tests/simple-hashCode-equals-01/src/main/resources/cases.xsd

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
<xs:annotation>
77
<xs:appinfo>
8-
<!-- jaxb:globalBindings optionalProperty="primitive"
8+
<jaxb:globalBindings optionalProperty="primitive"
99
generateIsSetMethod="true">
10-
</jaxb:globalBindings-->
10+
</jaxb:globalBindings>
1111
<jaxb:schemaBindings>
1212
<jaxb:package
1313
name="org.jvnet.jaxb2_commons.tests.simple_hashcode_equals_01.cases" />
@@ -56,15 +56,22 @@
5656
</xs:appinfo>
5757
</xs:annotation>
5858
</xs:element>
59-
<xs:element name="char" type="xs:string">
59+
<!-- See JAXB-1061 -->
60+
<!-- xs:element name="char">
6061
<xs:annotation>
6162
<xs:appinfo>
6263
<jaxb:property>
6364
<jaxb:baseType name="char" />
6465
</jaxb:property>
6566
</xs:appinfo>
6667
</xs:annotation>
67-
</xs:element>
68+
<xs:simpleType>
69+
<xs:restriction base="xs:string">
70+
<xs:minLength value="1"/>
71+
<xs:maxLength value="1"/>
72+
</xs:restriction>
73+
</xs:simpleType>
74+
</xs:element-->
6875
<xs:element name="double" type="xs:double">
6976
<xs:annotation>
7077
<xs:appinfo>
@@ -113,6 +120,19 @@
113120
</xs:sequence>
114121
</xs:complexType>
115122

123+
<xs:element name="unboxedPrimitives">
124+
<xs:complexType>
125+
<xs:attribute name="unboxedBoolean" type="xs:boolean" use="optional" />
126+
<xs:attribute name="unboxedByte" type="xs:byte" use="optional" />
127+
<!--xs:attribute name="unboxedChar" type="xs:char" use="optional" /-->
128+
<xs:attribute name="unboxedDouble" type="xs:double" use="optional" />
129+
<xs:attribute name="unboxedFloat" type="xs:float" use="optional" />
130+
<xs:attribute name="unboxedLong" type="xs:long" use="optional" />
131+
<xs:attribute name="unboxedInt" type="xs:int" use="optional" />
132+
<xs:attribute name="unboxedShort" type="xs:short" use="optional" />
133+
</xs:complexType>
134+
</xs:element>
135+
116136
<xs:complexType name="arbitraryObject">
117137
<xs:sequence>
118138
<xs:element name="object" />

tools/src/main/java/org/jvnet/jaxb2_commons/util/PropertyFieldAccessorFactory.java

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ private static class PropertyFieldAccessor implements FieldAccessorEx {
4040
private final JMethod getter;
4141
private final JMethod setter;
4242
private final JFieldVar constantField;
43+
private final JFieldVar field;
4344
private FieldAccessor fieldAccessor;
4445
private final JType type;
46+
@SuppressWarnings("unused")
47+
private final JType fieldType;
4548

4649
public PropertyFieldAccessor(final FieldOutline fieldOutline,
4750
JExpression targetObject) {
@@ -51,16 +54,25 @@ public PropertyFieldAccessor(final FieldOutline fieldOutline,
5154
this.fieldAccessor = fieldOutline.create(targetObject);
5255
final String publicName = fieldOutline.getPropertyInfo().getName(
5356
true);
57+
final String privateName = fieldOutline.getPropertyInfo().getName(
58+
false);
5459
this.theClass = fieldOutline.parent().implClass;
5560
final String setterName = "set" + publicName;
5661
final JMethod getGetter = theClass.getMethod("get" + publicName,
5762
ABSENT);
5863
final JMethod isGetter = theClass.getMethod("is" + publicName,
5964
ABSENT);
65+
final JFieldVar field = theClass.fields().get(privateName);
66+
this.field = field != null
67+
&& ((field.mods().getValue() & JMod.PROTECTED) != 0)
68+
&& ((field.mods().getValue() & JMod.STATIC) == 0)
69+
&& ((field.mods().getValue() & JMod.FINAL) == 0) ? field
70+
: null;
6071
this.getter = getGetter != null ? getGetter
6172
: (isGetter != null ? isGetter : null);
6273
this.type = this.getter != null ? this.getter.type() : fieldOutline
6374
.getRawType();
75+
this.fieldType = this.field != null ? this.field.type() : this.type;
6476

6577
final JFieldVar constantField = theClass.fields().get(publicName);
6678
this.constantField = constantField != null
@@ -103,9 +115,6 @@ public CPropertyInfo getPropertyInfo() {
103115
public boolean isAlwaysSet() {
104116
if (constantField != null) {
105117
return true;
106-
} else if (type.isPrimitive()) {
107-
// TODO this is due to a bug in JAXB - char does not get unboxed
108-
return true;
109118
} else {
110119
return JExpr.TRUE == fieldAccessor.hasSetValue();
111120
}
@@ -116,9 +125,6 @@ public JExpression hasSetValue() {
116125
return JExpr.TRUE;
117126
} else if (isSetter != null) {
118127
return targetObject.invoke(isSetter);
119-
} else if (type.isPrimitive()) {
120-
// TODO this is due to a bug in JAXB - char does not get unboxed
121-
return JExpr.TRUE;
122128
} else {
123129
return fieldAccessor.hasSetValue();
124130
}
@@ -219,22 +225,5 @@ public void toRawValue(JBlock block, JVar $var) {
219225
}
220226
}
221227
}
222-
223-
@Override
224-
public void getValue(JBlock block, JVar $var, boolean checkHasSetValue) {
225-
if (checkHasSetValue) {
226-
toRawValue(block, $var);
227-
} else {
228-
if (constantField != null) {
229-
block.assign($var, theClass.staticRef(this.constantField));
230-
} else {
231-
if (getter != null) {
232-
block.assign($var, targetObject.invoke(getter));
233-
} else {
234-
fieldAccessor.toRawValue(block, $var);
235-
}
236-
}
237-
}
238-
}
239228
}
240229
}

tools/src/main/java/org/jvnet/jaxb2_commons/xjc/outline/FieldAccessorEx.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,4 @@ public interface FieldAccessorEx extends FieldAccessor {
1414
public boolean isVirtual();
1515

1616
public boolean isAlwaysSet();
17-
18-
public void getValue(JBlock block, JVar $var, boolean checkHasSetValue);
1917
}

0 commit comments

Comments
 (0)