package org.opendaylight.mdsal.binding.java.api.generator
import static java.util.Objects.requireNonNull
+import static org.opendaylight.mdsal.binding.generator.util.BaseYangTypes.EMPTY_TYPE;
import static org.opendaylight.mdsal.binding.model.util.Types.BOOLEAN;
import static org.opendaylight.mdsal.binding.model.util.Types.BYTE_ARRAY;
import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
import com.google.common.base.Preconditions
import com.google.common.collect.ImmutableList
import com.google.common.collect.Lists
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import java.beans.ConstructorProperties
import java.util.ArrayList
import java.util.Base64;
import java.util.Comparator
import java.util.List
import java.util.Map
-import java.util.regex.Pattern
import javax.management.ConstructorParameters
import org.gaul.modernizer_maven_annotations.SuppressModernizer
import org.opendaylight.mdsal.binding.model.api.ConcreteType
import org.opendaylight.mdsal.binding.model.api.Restrictions
import org.opendaylight.mdsal.binding.model.api.Type
import org.opendaylight.mdsal.binding.model.util.TypeConstants
-import org.opendaylight.yangtools.yang.binding.CodeHelpers
import org.opendaylight.yangtools.yang.common.Empty
import org.opendaylight.yangtools.yang.common.Uint16
import org.opendaylight.yangtools.yang.common.Uint32
import org.opendaylight.yangtools.yang.common.Uint64
import org.opendaylight.yangtools.yang.common.Uint8
import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
+import org.opendaylight.mdsal.binding.model.util.BindingTypes
+import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
/**
* Template for generating JAVA class.
this.consts = genType.constantDefinitions
if (restrictions !== null && restrictions.rangeConstraint.present) {
- rangeGenerator = requireNonNull(AbstractRangeGenerator.forType(findProperty(genType, "value").returnType))
+ rangeGenerator = requireNonNull(AbstractRangeGenerator.forType(TypeUtils.encapsulatedValueType(genType)))
} else {
rangeGenerator = null
}
«IF restrictions !== null»
«IF restrictions.lengthConstraint.present»
- «LengthGenerator.generateLengthChecker("_value", findProperty(genTO, "value").returnType, restrictions.lengthConstraint.get, this)»
+ «LengthGenerator.generateLengthChecker("_value", TypeUtils.encapsulatedValueType(genTO),
+ restrictions.lengthConstraint.get, this)»
«ENDIF»
«IF restrictions.rangeConstraint.present»
«rangeGenerator.generateRangeChecker("_value", restrictions.rangeConstraint.get, this)»
«defaultInstance»
- «FOR field : properties SEPARATOR "\n"»
- «field.getterMethod»
- «IF !field.readOnly»
- «field.setterMethod»
- «ENDIF»
- «ENDFOR»
+ «propertyMethods»
«IF (genTO.isTypedef() && genTO.getBaseType instanceof BitsTypeDefinition)»
«generateGetValueForBitsTypeDef»
'''
+ def private propertyMethods() {
+ if (properties.empty) {
+ return ""
+ }
+ isScalarTypeObject ? scalarTypeObjectValue(properties.get(0)) : defaultProperties
+ }
+
+ def private isScalarTypeObject() {
+ for (impl : genTO.implements) {
+ if (BindingTypes.SCALAR_TYPE_OBJECT.identifier.equals(impl.identifier)) {
+ return true
+ }
+ }
+ return false
+ }
+
+ def private defaultProperties() '''
+ «FOR field : properties SEPARATOR "\n"»
+ «field.getterMethod»
+ «IF !field.readOnly»
+ «field.setterMethod»
+ «ENDIF»
+ «ENDFOR»
+ '''
+
+ 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»;
+ }
+ '''
+
/**
* Template method which generates the method <code>getValue()</code> for typedef,
* which base type is BitsDefinition.
def protected constructors() '''
«IF genTO.unionType»
«genUnionConstructor»
- «ELSEIF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
+ «ELSEIF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals(TypeConstants.VALUE_PROP)»
«typedefConstructor»
«ELSE»
«allValuesConstructor»
def private allValuesConstructor() '''
public «type.name»(«allProperties.asArgumentsDeclaration») {
- «IF false == parentProperties.empty»
+ «IF !parentProperties.empty»
super(«parentProperties.asArguments»);
«ENDIF»
«FOR p : allProperties»
- «generateRestrictions(type, p.fieldName.toString, p.returnType)»
+ «generateRestrictions(type, p.fieldName, p.returnType)»
«ENDFOR»
«FOR p : properties»
'''
def private typedefConstructor() '''
- @«ConstructorParameters.importedName»("value")
- @«ConstructorProperties.importedName»("value")
+ @«ConstructorParameters.importedName»("«TypeConstants.VALUE_PROP»")
+ @«ConstructorProperties.importedName»("«TypeConstants.VALUE_PROP»")
public «type.name»(«allProperties.asArgumentsDeclaration») {
- «IF false == parentProperties.empty»
+ «IF !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.
*/»
- «CodeHelpers.importedName».requireValue(_value);
+ «CODEHELPERS.importedName».requireValue(_value);
«genPatternEnforcer("_value")»
«FOR p : properties»
«ENDIF»
«ENDFOR»
}
- «val propType = allProperties.get(0).returnType»
- «val uintType = UINT_TYPES.get(propType)»
- «IF uintType !== null»
-
- /**
- * Utility migration constructor.
- *
- * @param value Wrapped value in legacy type
- * @deprecated Use {#link «type.name»(«propType.importedName»)} instead.
- */
- @Deprecated(forRemoval = true)
- public «type.getName»(final «uintType.importedName» value) {
- this(«CodeHelpers.importedName».compatUint(value));
- }
- «ENDIF»
'''
def protected genUnionConstructor() '''
def protected genConstructor(GeneratedProperty property, Iterable<GeneratedProperty> other) '''
public «type.name»(«property.returnType.importedName + " " + property.name») {
- «IF false == parentProperties.empty»
+ «IF !parentProperties.empty»
super(«parentProperties.asArguments»);
«ENDIF»
«val fieldName = property.fieldName»
- «generateRestrictions(type, fieldName.toString, property.returnType)»
+ «generateRestrictions(type, fieldName, property.returnType)»
this.«fieldName» = «property.name»;
«FOR p : other»
def private genPatternEnforcer(String ref) '''
«FOR c : consts»
«IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
- «CodeHelpers.importedName».checkPattern(«ref», «Constants.MEMBER_PATTERN_LIST», «Constants.MEMBER_REGEX_LIST»);
+ «CODEHELPERS.importedName».checkPattern(«ref», «Constants.MEMBER_PATTERN_LIST», «Constants.MEMBER_REGEX_LIST»);
«ENDIF»
«ENDFOR»
'''
* @param source Source object
*/
public «type.name»(«type.name» source) {
- «IF false == parentProperties.empty»
+ «IF !parentProperties.empty»
super(source);
«ENDIF»
«FOR p : properties»
return new «genTO.name»(«Base64.importedName».getDecoder().decode(defaultValue));
«ELSEIF STRING.equals(prop.returnType)»
return new «genTO.name»(defaultValue);
- «ELSEIF Constants.EMPTY.equals(prop.returnType)»
+ «ELSEIF EMPTY_TYPE.equals(prop.returnType)»
«Preconditions.importedName».checkArgument(defaultValue.isEmpty(), "Invalid value %s", defaultValue);
return new «genTO.name»(«Empty.importedName».getInstance());
«ELSEIF allProperties.size > 1»
«ENDIF»
'''
+ @SuppressFBWarnings(value = "DLS_DEAD_LOCAL_STORE", justification = "FOR with SEPARATOR, not needing for value")
def protected bitsArgs() '''
- «List.importedName»<«String.importedName»> properties = «Lists.importedName».newArrayList(«allProperties.propsAsArgs»);
+ «JU_LIST.importedName»<«STRING.importedName»> properties = «Lists.importedName».newArrayList(«allProperties.propsAsArgs»);
if (!properties.contains(defaultValue)) {
throw new «IllegalArgumentException.importedName»("invalid default parameter");
}
«FOR c : consts»
«IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
«val cValue = c.value as Map<String, String>»
- public static final «List.importedName»<String> «TypeConstants.PATTERN_CONSTANT_NAME» = «ImmutableList.importedName».of(«
+ «val jurPatternRef = JUR_PATTERN.importedName»
+ public static final «JU_LIST.importedName»<String> «TypeConstants.PATTERN_CONSTANT_NAME» = «ImmutableList.importedName».of(«
FOR v : cValue.keySet SEPARATOR ", "»"«v.escapeJava»"«ENDFOR»);
«IF cValue.size == 1»
- private static final «Pattern.importedName» «Constants.MEMBER_PATTERN_LIST» = «Pattern.importedName».compile(«TypeConstants.PATTERN_CONSTANT_NAME».get(0));
+ private static final «jurPatternRef» «Constants.MEMBER_PATTERN_LIST» = «jurPatternRef».compile(«TypeConstants.PATTERN_CONSTANT_NAME».get(0));
private static final String «Constants.MEMBER_REGEX_LIST» = "«cValue.values.iterator.next.escapeJava»";
«ELSE»
- private static final «Pattern.importedName»[] «Constants.MEMBER_PATTERN_LIST» = «CodeHelpers.importedName».compilePatterns(«TypeConstants.PATTERN_CONSTANT_NAME»);
+ private static final «jurPatternRef»[] «Constants.MEMBER_PATTERN_LIST» = «CODEHELPERS.importedName».compilePatterns(«TypeConstants.PATTERN_CONSTANT_NAME»);
private static final String[] «Constants.MEMBER_REGEX_LIST» = { «
FOR v : cValue.values SEPARATOR ", "»"«v.escapeJava»"«ENDFOR» };
«ENDIF»
return ""
}
return '''
- @«Override.importedName»
+ @«OVERRIDE.importedName»
public int hashCode() {
«IF size != 1»
«hashCodeResult(genTO.hashCodeIdentifiers)»
return result;
«ELSE»
- return «CodeHelpers.importedName».wrapperHashCode(«genTO.hashCodeIdentifiers.get(0).fieldName»);
+ return «CODEHELPERS.importedName».wrapperHashCode(«genTO.hashCodeIdentifiers.get(0).fieldName»);
«ENDIF»
}
'''
*/
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
}
-
}