|
31 | 31 | import com.sun.codemodel.JMod;
|
32 | 32 | import com.sun.codemodel.JPackage;
|
33 | 33 | import com.sun.codemodel.JType;
|
| 34 | +import com.sun.codemodel.JVar; |
34 | 35 | import java.io.UnsupportedEncodingException;
|
35 | 36 | import java.net.URI;
|
36 | 37 | import java.net.URLDecoder;
|
@@ -102,13 +103,27 @@ private boolean isCandidateForCreation(Collection<JType> unionTypes) {
|
102 | 103 |
|
103 | 104 | private JDefinedClass populateClass(
|
104 | 105 | JDefinedClass definedClass, Optional<JType> refType, Collection<JType> unionTypes) {
|
105 |
| - unionTypes.forEach(unionType -> wrapIt(definedClass, unionType)); |
| 106 | + JType clazzClass = definedClass.owner()._ref(Object.class); |
| 107 | + |
| 108 | + JFieldVar valueField = |
| 109 | + definedClass.field( |
| 110 | + JMod.PRIVATE, |
| 111 | + clazzClass, |
| 112 | + ruleFactory.getNameHelper().getPropertyName("value", null), |
| 113 | + null); |
| 114 | + |
| 115 | + definedClass._implements( |
| 116 | + definedClass.owner().ref(GeneratorUtils.ONE_OF_VALUE_PROVIDER_INTERFACE_NAME)); |
| 117 | + |
| 118 | + GeneratorUtils.implementInterface(definedClass, valueField); |
| 119 | + |
| 120 | + unionTypes.forEach(unionType -> wrapIt(definedClass, valueField, unionType)); |
106 | 121 | refType.ifPresent(
|
107 | 122 | type -> {
|
108 | 123 | if (type instanceof JClass) {
|
109 | 124 | definedClass._extends((JClass) type);
|
110 | 125 | } else {
|
111 |
| - wrapIt(definedClass, type); |
| 126 | + wrapIt(definedClass, valueField, type); |
112 | 127 | }
|
113 | 128 | });
|
114 | 129 | if (definedClass.constructors().hasNext()
|
@@ -166,23 +181,25 @@ private JDefinedClass createUnionClass(
|
166 | 181 | definedClass
|
167 | 182 | .annotate(JsonDeserialize.class)
|
168 | 183 | .param("using", generateDeserializer(definedClass, unionTypes));
|
| 184 | + |
169 | 185 | return populateClass(definedClass, refType, unionTypes);
|
170 | 186 | } catch (JClassAlreadyExistsException e) {
|
171 | 187 | throw new IllegalArgumentException(e);
|
172 | 188 | }
|
173 | 189 | }
|
174 | 190 |
|
175 |
| - private void wrapIt(JDefinedClass definedClass, JType unionType) { |
| 191 | + private void wrapIt(JDefinedClass definedClass, JFieldVar valueField, JType unionType) { |
176 | 192 | final String name = unionType.name();
|
177 | 193 | JFieldVar instanceField =
|
178 | 194 | definedClass.field(
|
179 | 195 | JMod.PRIVATE, unionType, ruleFactory.getNameHelper().getPropertyName(name, null));
|
180 | 196 | GeneratorUtils.buildMethod(definedClass, instanceField, ruleFactory.getNameHelper(), name);
|
181 | 197 | JMethod constructor = definedClass.constructor(JMod.PUBLIC);
|
| 198 | + JVar instanceParam = constructor.param(unionType, instanceField.name()); |
182 | 199 | constructor
|
183 | 200 | .body()
|
184 |
| - .assign( |
185 |
| - JExpr._this().ref(instanceField), constructor.param(unionType, instanceField.name())); |
| 201 | + .assign(JExpr._this().ref(valueField), instanceParam) |
| 202 | + .assign(JExpr._this().ref(instanceField), instanceParam); |
186 | 203 | }
|
187 | 204 |
|
188 | 205 | private void unionType(
|
|
0 commit comments