* generated property with data about field which is generated as the getter method
* @return string with the getter method source code in JAVA format
*/
- protected def getterMethod(GeneratedProperty field) {
- '''
- public «field.returnType.importedName» «field.getterMethodName»() {
- «val fieldName = field.fieldName»
- «IF field.returnType.name.endsWith("[]")»
- return «fieldName» == null ? null : «fieldName».clone();
- «ELSE»
- return «fieldName»;
- «ENDIF»
- }
- '''
- }
+ protected def getterMethod(GeneratedProperty field) '''
+ public «field.returnType.importedName» «field.getterMethodName»() {
+ «val fieldName = field.fieldName»
+ «IF field.returnType.name.endsWith("[]")»
+ return «fieldName» == null ? null : «fieldName».clone();
+ «ELSE»
+ return «fieldName»;
+ «ENDIF»
+ }
+ '''
final protected def getterMethodName(GeneratedProperty field) {
val prefix = if(field.returnType.equals(Types.BOOLEAN)) "is" else "get"
def private scalarTypeObjectValue(GeneratedProperty field) '''
@«OVERRIDE.importedName»
public «field.returnType.importedName» «BindingMapping.SCALAR_TYPE_OBJECT_GET_VALUE_NAME»() {
- return «field.fieldName»«IF field.returnType.name.endsWith("[]")».clone()«ENDIF»;
+ return «field.fieldName»«field.cloneCall»;
}
'''
«FOR p : properties»
«val fieldName = p.fieldName»
- «IF p.returnType.name.endsWith("[]")»
- this.«fieldName» = «fieldName».clone();
- «ELSE»
- this.«fieldName» = «fieldName»;
- «ENDIF»
+ this.«fieldName» = «fieldName»«p.cloneCall»;
«ENDFOR»
}
'''
return null;
}
+ /**
+ * Generate a call to {@link Object#clone()} if target field represents an array. Returns an empty string otherwise.
+ *
+ * @param property Generated property
+ * @return The string used to clone the property, or an empty string
+ */
+ static final String cloneCall(final GeneratedProperty property) {
+ return property.getReturnType().getName().endsWith("[]") ? ".clone()" : "";
+ }
+
/**
* Returns set of method signature instances which contains all the methods of the <code>genType</code>
* and all the methods of the implemented interfaces.
/**
* Template for generating JAVA class.
*/
-class ListKeyTemplate extends ClassTemplate {
-
+final class ListKeyTemplate extends ClassTemplate {
/**
* Creates instance of this class with concrete <code>genType</code>.
*
super(genType)
}
-
- override final allValuesConstructor() '''
+ override allValuesConstructor() '''
public «type.name»(«allProperties.asNonNullArgumentsDeclaration») {
«FOR p : allProperties»
- «CODEHELPERS.importedName».requireValue(«p.fieldName»);
+ «val fieldName = p.fieldName»
+ this.«fieldName» = «CODEHELPERS.importedName».requireKeyProp(«fieldName», "«p.name»")«p.cloneCall»;
«ENDFOR»
«FOR p : properties»
«generateRestrictions(type, p.fieldName, p.returnType)»
«ENDFOR»
-
- «FOR p : allProperties»
- «val fieldName = p.fieldName»
- «IF p.returnType.name.endsWith("[]")»
- this.«fieldName» = «fieldName».clone();
- «ELSE»
- this.«fieldName» = «fieldName»;
- «ENDIF»
- «ENDFOR»
}
'''
- override final getterMethod(GeneratedProperty field) {
- '''
- public «field.returnType.importedNonNull» «field.getterMethodName»() {
- «val fieldName = field.fieldName»
- «IF field.returnType.name.endsWith("[]")»
- return «fieldName».clone();
- «ELSE»
- return «fieldName»;
- «ENDIF»
- }
- '''
- }
+ override getterMethod(GeneratedProperty field) '''
+ public «field.returnType.importedNonNull» «field.getterMethodName»() {
+ return «field.fieldName»«field.cloneCall»;
+ }
+ '''
}
checkArgument(expression, "expected one of: %s \n%but was: %s", options, value);
}
+ /**
+ * A shortcut for {@code Preconditions.checkNotNull(value, "Key component \"%s\" must not be null", name)}.
+ *
+ * @param value Value itself
+ * @param name Name of the value
+ * @return Non-null value
+ * @throws NullPointerException if value is null
+ */
+ public static <T> @NonNull T requireKeyProp(final @Nullable T value, final @NonNull String name) {
+ if (value == null) {
+ throw new NullPointerException("Key component \"" + name + "\" may not be null");
+ }
+ return value;
+ }
+
/**
* A shortcut for {@code Objects.requireNonNull(value, "Supplied value may not be null")}.
*
* @param value Value itself
* @throws NullPointerException if value is null
*/
- public static void requireValue(@Nullable final Object value) {
+ public static void requireValue(final @Nullable Object value) {
requireNonNull(value, "Supplied value may not be null");
}