X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=code-generator%2Fbinding-java-api-generator%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fsal%2Fjava%2Fapi%2Fgenerator%2FClassTemplate.xtend;h=28c2f0dcea03243fe32d9e1694c50d30fb9cbc70;hb=157ebf5fe763a9572121e1a2d9043d1ce3bbb4c7;hp=afebd5846b156465d9ec7de1049124190696a7a1;hpb=82761833e9331bc6fbddbf85ea52506a9b905f0b;p=mdsal.git diff --git a/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend b/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend index afebd5846b..28c2f0dcea 100644 --- a/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend +++ b/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend @@ -26,6 +26,8 @@ import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType import org.opendaylight.yangtools.sal.binding.model.api.Restrictions +import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition +import com.google.common.base.Preconditions /** * Template for generating JAVA class. @@ -52,12 +54,12 @@ class ClassTemplate extends BaseTemplate { * List of generated types which are enclosed inside genType */ protected val List enclosedGeneratedTypes; - + protected val GeneratedTransferObject genTO; /** * Creates instance of this class with concrete genType. - * + * * @param genType generated transfer object which will be transformed to JAVA class source code */ new(GeneratedTransferObject genType) { @@ -83,7 +85,7 @@ class ClassTemplate extends BaseTemplate { /** * Generates JAVA class source code (class body only). - * + * * @return string with JAVA class body source code */ def CharSequence generateAsInnerClass() { @@ -96,7 +98,7 @@ class ClassTemplate extends BaseTemplate { /** * Template method which generates class body. - * + * * @param isInnerClass boolean value which specify if generated class is|isn't inner * @return string with class source code in JAVA format */ @@ -108,11 +110,11 @@ class ClassTemplate extends BaseTemplate { «enumDeclarations» «constantsDeclarations» «generateFields» - - «IF restrictions != null && (!restrictions.rangeConstraints.nullOrEmpty || + + «IF restrictions != null && (!restrictions.rangeConstraints.nullOrEmpty || !restrictions.lengthConstraints.nullOrEmpty)» «generateConstraints» - + «ENDIF» «constructors» @@ -125,6 +127,10 @@ class ClassTemplate extends BaseTemplate { «ENDIF» «ENDFOR» + «IF (genTO.isTypedef() && genTO.getBaseType instanceof BitsTypeDefinition)» + «generateGetValueForBitsTypeDef» + «ENDIF» + «generateHashCode» «generateEquals» @@ -136,6 +142,24 @@ class ClassTemplate extends BaseTemplate { «generateRangeMethod("range", "_range")» } + + ''' + + /** + * Template method which generates the method getValue() for typedef, + * which base type is BitsDefinition. + * + * @return string with the getValue() method definition in JAVA format + */ + def protected generateGetValueForBitsTypeDef() ''' + + public boolean[] getValue() { + return new boolean[]{ + «FOR property: genTO.properties SEPARATOR ','» + «property.fieldName» + «ENDFOR» + }; + } ''' def private generateLengthMethod(String methodName, String varName) ''' @@ -158,14 +182,14 @@ class ClassTemplate extends BaseTemplate { /** * Template method which generates inner classes inside this interface. - * + * * @return string with the source code for inner classes in JAVA format */ def protected innerClassesDeclarations() ''' «IF !enclosedGeneratedTypes.empty» «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"» «IF (innerClass instanceof GeneratedTransferObject)» - «val classTemplate = new ClassTemplate(innerClass as GeneratedTransferObject)» + «val classTemplate = new ClassTemplate(innerClass)» «classTemplate.generateAsInnerClass» «ENDIF» @@ -244,14 +268,42 @@ class ClassTemplate extends BaseTemplate { «IF false == parentProperties.empty» super(«parentProperties.asArguments»); «ENDIF» - «FOR p : allProperties» + «FOR p : allProperties» «generateRestrictions(type, p.fieldName.toString, p.returnType)» «ENDFOR» - «FOR p : properties» + + «/* + * 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.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")» + + «Preconditions.importedName».checkNotNull(_value, "Supplied value may not be null"); + + «FOR c : consts» + «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME && c.value instanceof List» + boolean valid = false; + for (Pattern p : patterns) { + if (p.matcher(_value).matches()) { + valid = true; + break; + } + } + + «Preconditions.importedName».checkArgument(valid, "Supplied value \"%s\" does not match any of the permitted patterns %s", _value, «TypeConstants.PATTERN_CONSTANT_NAME»); + «ENDIF» + «ENDFOR» + «ENDIF» + + «FOR p : properties» + «IF p.returnType.importedName.contains("[]")» + this.«p.fieldName» = «p.fieldName» == null ? null : «p.fieldName».clone(); + «ELSE» this.«p.fieldName» = «p.fieldName»; + «ENDIF» «ENDFOR» } - + ''' def protected genUnionConstructor() ''' @@ -269,11 +321,13 @@ class ClassTemplate extends BaseTemplate { «IF false == parentProperties.empty» super(«parentProperties.asArguments»); «ENDIF» - «generateRestrictions(type, property.fieldName.toString, property.returnType)» - this.«property.fieldName» = «property.name»; - «FOR p : other» + + «generateRestrictions(type, property.fieldName.toString, property.returnType)» + + this.«property.fieldName» = «property.name»; + «FOR p : other» this.«p.fieldName» = null; - «ENDFOR» + «ENDFOR» } ''' @@ -287,7 +341,7 @@ class ClassTemplate extends BaseTemplate { «IF false == parentProperties.empty» super(source); «ENDIF» - «FOR p : properties» + «FOR p : properties» this.«p.fieldName» = source.«p.fieldName»; «ENDFOR» } @@ -310,12 +364,22 @@ class ClassTemplate extends BaseTemplate { «IF !("org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(prop.returnType.fullyQualifiedName))» public static «genTO.name» getDefaultInstance(String defaultValue) { «IF "byte[]".equals(prop.returnType.name)» - «BaseEncoding.importedName» baseEncoding = «BaseEncoding.importedName».base64(); + «BaseEncoding.importedName» baseEncoding = «BaseEncoding.importedName».base64(); return new «genTO.name»(baseEncoding.decode(defaultValue)); «ELSEIF "java.lang.String".equals(prop.returnType.fullyQualifiedName)» return new «genTO.name»(defaultValue); «ELSEIF allProperties.size > 1» «bitsArgs» + «ELSEIF "java.lang.Boolean".equals(prop.returnType.fullyQualifiedName)» + return new «genTO.name»(Boolean.valueOf(defaultValue)); + «ELSEIF "java.lang.Byte".equals(prop.returnType.fullyQualifiedName)» + return new «genTO.name»(Byte.valueOf(defaultValue)); + «ELSEIF "java.lang.Short".equals(prop.returnType.fullyQualifiedName)» + return new «genTO.name»(Short.valueOf(defaultValue)); + «ELSEIF "java.lang.Integer".equals(prop.returnType.fullyQualifiedName)» + return new «genTO.name»(Integer.valueOf(defaultValue)); + «ELSEIF "java.lang.Long".equals(prop.returnType.fullyQualifiedName)» + return new «genTO.name»(Long.valueOf(defaultValue)); «ELSE» return new «genTO.name»(new «prop.returnType.importedName»(defaultValue)); «ENDIF» @@ -332,7 +396,7 @@ class ClassTemplate extends BaseTemplate { int i = 0; return new «genTO.name»( «FOR prop : allProperties SEPARATOR ","» - properties.get(i++).equals(defaultValue) ? new «Boolean.importedName»("true") : null + properties.get(i++).equals(defaultValue) ? «Boolean.importedName».TRUE : null «ENDFOR» ); ''' @@ -345,7 +409,7 @@ class ClassTemplate extends BaseTemplate { /** * Template method which generates JAVA class declaration. - * + * * @param isInnerClass boolean value which specify if generated class is|isn't inner * @return string with class declaration in JAVA format */ @@ -371,7 +435,7 @@ class ClassTemplate extends BaseTemplate { /** * Template method which generates JAVA enum type. - * + * * @return string with inner enum source code in JAVA format */ def protected enumDeclarations() ''' @@ -385,14 +449,14 @@ class ClassTemplate extends BaseTemplate { def protected suidDeclaration() ''' «IF genTO.SUID != null» - private static final long serialVersionUID = «genTO.SUID.value»L; + private static final long serialVersionUID = «genTO.SUID.value»L; «ENDIF» ''' /** - * Template method wich generates JAVA constants. - * - * @return string with constants in JAVA format + * Template method which generates JAVA constants. + * + * @return string with constants in JAVA format */ def protected constantsDeclarations() ''' «IF !consts.empty» @@ -400,12 +464,11 @@ class ClassTemplate extends BaseTemplate { «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME» «val cValue = c.value» «IF cValue instanceof List» - «val cValues = cValue as List» - private static final «List.importedName»<«Pattern.importedName»> «Constants.MEMBER_PATTERN_LIST» = new «ArrayList.importedName»<«Pattern.importedName»>(); - public static final «List.importedName» «TypeConstants.PATTERN_CONSTANT_NAME» = «Arrays.importedName».asList(« - FOR v : cValues SEPARATOR ", "»« + private static final «List.importedName»<«Pattern.importedName»> «Constants.MEMBER_PATTERN_LIST»; + public static final «List.importedName» «TypeConstants.PATTERN_CONSTANT_NAME» = «ImmutableList.importedName».of(« + FOR v : cValue SEPARATOR ", "»« IF v instanceof String»"« - v as String»"« + v»"« ENDIF»« ENDFOR»); @@ -425,9 +488,12 @@ class ClassTemplate extends BaseTemplate { */ def protected generateStaticInicializationBlock() ''' static { + final «List.importedName»<«Pattern.importedName»> l = new «ArrayList.importedName»<«Pattern.importedName»>(); for (String regEx : «TypeConstants.PATTERN_CONSTANT_NAME») { - «Constants.MEMBER_PATTERN_LIST».add(Pattern.compile(regEx)); + l.add(Pattern.compile(regEx)); } + + «Constants.MEMBER_PATTERN_LIST» = «ImmutableList.importedName».copyOf(l); } ''' @@ -450,7 +516,7 @@ class ClassTemplate extends BaseTemplate { «ENDIF» «IF !properties.empty» «FOR f : properties» - «IF f.readOnly»final«ENDIF» private «f.returnType.importedName» «f.fieldName»; + private«IF f.readOnly» final«ENDIF» «f.returnType.importedName» «f.fieldName»; «ENDFOR» «ENDIF» '''