def protected constructors() '''
«IF genTO.unionType»
«genUnionConstructor»
+ «ELSEIF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
+ «typedefConstructor»
+ «legacyConstructor»
«ELSE»
«allValuesConstructor»
+ «legacyConstructor»
«ENDIF»
+
«IF !allProperties.empty»
«copyConstructor»
«ENDIF»
«ENDIF»
'''
- def protected allValuesConstructor() '''
- «IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
- @«ConstructorParameters.importedName»("value")
- @«ConstructorProperties.importedName»("value")
- «ENDIF»
+ def private allValuesConstructor() '''
public «type.name»(«allProperties.asArgumentsDeclaration») {
«IF false == parentProperties.empty»
super(«parentProperties.asArguments»);
«ENDIF»
«FOR p : allProperties»
- «generateRestrictions(type, p.fieldName.toString, p.returnType)»
+ «generateRestrictions(type, p.fieldName, p.returnType)»
«ENDFOR»
- «/*
- * If we have patterns, we need to apply them to the value field. This is a sad
- * consequence of how this code is structured.
- */
- IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
- «CodeHelpers.importedName».requireValue(_value);
- «genPatternEnforcer("_value")»
+ «FOR p : properties»
+ «val fieldName = p.fieldName»
+ «IF p.returnType.name.endsWith("[]")»
+ this.«fieldName» = «fieldName» == null ? null : «fieldName».clone();
+ «ELSE»
+ this.«fieldName» = «fieldName»;
+ «ENDIF»
+ «ENDFOR»
+ }
+ '''
+
+ def private typedefConstructor() '''
+ @«ConstructorParameters.importedName»("value")
+ @«ConstructorProperties.importedName»("value")
+ public «type.name»(«allProperties.asArgumentsDeclaration») {
+ «IF false == parentProperties.empty»
+ super(«parentProperties.asArguments»);
«ENDIF»
+ «FOR p : allProperties»
+ «generateRestrictions(type, p.fieldName, p.returnType)»
+ «ENDFOR»
+ «/*
+ * If we have patterns, we need to apply them to the value field. This is a sad consequence of how this code is
+ * structured.
+ */»
+ «CodeHelpers.importedName».requireValue(_value);
+ «genPatternEnforcer("_value")»
«FOR p : properties»
- «IF p.returnType.importedName.contains("[]")»
- «IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name
- .equals("value")»
- this.«p.fieldName» = «p.fieldName».clone();
- «ELSE»
- this.«p.fieldName» = «p.fieldName» == null ? null : «p.fieldName».clone();
- «ENDIF»
+ «val fieldName = p.fieldName»
+ «IF p.returnType.name.endsWith("[]")»
+ this.«fieldName» = «fieldName».clone();
«ELSE»
- this.«p.fieldName» = «p.fieldName»;
+ this.«fieldName» = «fieldName»;
«ENDIF»
«ENDFOR»
}
-
'''
+ def private legacyConstructor() {
+ if (!hasUintProperties) {
+ return ""
+ }
+
+ val compatUint = CodeHelpers.importedName + ".compatUint("
+ return '''
+
+ /**
+ * Utility migration constructor.
+ *
+ «FOR prop : allProperties»
+ * @param «prop.fieldName» «prop.name»«IF prop.isUintType» in legacy Java type«ENDIF»
+ «ENDFOR»
+ * @deprecated Use {#link «type.name»(«FOR prop : allProperties SEPARATOR ", "»«prop.returnType.importedJavadocName»«ENDFOR»)} instead.
+ */
+ @Deprecated(forRemoval = true)
+ public «type.getName»(«FOR prop : allProperties SEPARATOR ", "»«prop.legacyType.importedName» «prop.fieldName»«ENDFOR») {
+ this(«FOR prop : allProperties SEPARATOR ", "»«IF prop.isUintType»«compatUint»«prop.fieldName»)«ELSE»«prop.fieldName»«ENDIF»«ENDFOR»);
+ }
+ '''
+ }
+
def protected genUnionConstructor() '''
«FOR p : allProperties»
«val List<GeneratedProperty> other = new ArrayList(properties)»
«genConstructor(p, other)»
«ENDIF»
«ENDFOR»
-
'''
- def protected genConstructor(GeneratedProperty property, GeneratedProperty... other) '''
+ def protected genConstructor(GeneratedProperty property, Iterable<GeneratedProperty> other) '''
public «type.name»(«property.returnType.importedName + " " + property.name») {
«IF false == parentProperties.empty»
super(«parentProperties.asArguments»);
«ENDIF»
- «generateRestrictions(type, property.fieldName.toString, property.returnType)»
+ «val fieldName = property.fieldName»
+ «generateRestrictions(type, fieldName, property.returnType)»
- this.«property.fieldName» = «property.name»;
+ this.«fieldName» = «property.name»;
«FOR p : other»
this.«p.fieldName» = null;
«ENDFOR»
super(source);
«ENDIF»
«FOR p : properties»
- this.«p.fieldName» = source.«p.fieldName»;
+ «val fieldName = p.fieldName»
+ this.«fieldName» = source.«fieldName»;
«ENDFOR»
}
'''
return ""
}
return '''
- @«Override.importedName»
+ @«OVERRIDE.importedName»
public int hashCode() {
«IF size != 1»
«hashCodeResult(genTO.hashCodeIdentifiers)»
*/
def private generateEquals() '''
«IF !genTO.equalsIdentifiers.empty»
- @«Override.importedName»
+ @«OVERRIDE.importedName»
public final boolean equals(java.lang.Object obj) {
if (this == obj) {
return true;
return prop;
}
}
- return null;
+ return null
}
+ def private hasUintProperties() {
+ for (GeneratedProperty prop : allProperties) {
+ if (prop.isUintType) {
+ return true
+ }
+ }
+ return false
+ }
+
+ def private static isUintType(GeneratedProperty prop) {
+ UINT_TYPES.containsKey(prop.returnType)
+ }
+
+ def private static legacyType(GeneratedProperty prop) {
+ val type = prop.returnType
+ val uint = UINT_TYPES.get(type)
+ if (uint !== null) {
+ return uint
+ }
+ return type
+ }
}