Gaurav Agrawal
Committed by Gerrit Code Review

[ONOS-4070] Translator of YANG union.

Change-Id: I5216687b6ea7cb6baeb3ef8e905719468370a1f4
Showing 25 changed files with 1375 additions and 449 deletions
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.yangutils.datamodel;
import java.util.List;
/**
* Represents the holder with type(s).
*/
public interface HasType {
/**
* Returns type list.
*
* @return type list
*/
List<YangType<?>> getTypeList();
}
......@@ -15,6 +15,8 @@
*/
package org.onosproject.yangutils.datamodel;
import java.util.LinkedList;
import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.utils.YangConstructType;
......@@ -48,10 +50,11 @@ import org.onosproject.yangutils.utils.YangConstructType;
* | units | 7.3.3 | 0..1 |-string |
* +--------------+---------+-------------+------------------+
*/
/**
* Represents data model node to maintain information defined in YANG typedef.
*/
public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable, HasType {
/**
* Default value in string, needs to be converted to the target object,
......@@ -90,10 +93,17 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
private String units;
/**
* List of YANG type, for typedef it will have single type.
* This is done to unify the code with union.
*/
private List<YangType<?>> typeList;
/**
* Creates a typedef node.
*/
public YangTypeDef() {
super(YangNodeType.TYPEDEF_NODE);
typeList = new LinkedList<>();
}
/**
......@@ -180,7 +190,10 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
* @return the data type
*/
public YangType<?> getTypeDefBaseType() {
return dataType;
if (!(getTypeList().isEmpty())) {
return getTypeList().get(0);
}
return null;
}
/**
......@@ -189,7 +202,7 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
* @param dataType the data type
*/
public void setDataType(YangType<?> dataType) {
this.dataType = dataType;
getTypeList().add(0, dataType);
}
/**
......@@ -259,4 +272,9 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
public void setName(String name) {
this.name = name;
}
@Override
public List<YangType<?>> getTypeList() {
return typeList;
}
}
......
......@@ -18,7 +18,6 @@ package org.onosproject.yangutils.datamodel;
import java.util.LinkedList;
import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.utils.YangConstructType;
......@@ -48,7 +47,7 @@ import org.onosproject.yangutils.utils.YangConstructType;
/**
* Represents data model node to maintain information defined in YANG union.
*/
public class YangUnion extends YangNode implements Parsable {
public class YangUnion extends YangNode implements Parsable, HasType {
// List of YANG type.
private List<YangType<?>> typeList;
......@@ -68,11 +67,7 @@ public class YangUnion extends YangNode implements Parsable {
childUnionNumber = 1;
}
/**
* Returns list of YANG type.
*
* @return the list of YANG type
*/
@Override
public List<YangType<?>> getTypeList() {
return typeList;
}
......@@ -105,11 +100,11 @@ public class YangUnion extends YangNode implements Parsable {
}
/**
* Add YANG type to type list.
* Adds YANG type to type list.
*
* @param yangType YANG type to be added to list
* @throws DataModelException union member type must not be one of the
* built-in types "empty" or "leafref"
* built-in types "empty" or "leafref"
*/
public void addType(YangType<?> yangType) throws DataModelException {
if (yangType.getDataType() == YangDataTypes.EMPTY || yangType.getDataType() == YangDataTypes.LEAFREF) {
......@@ -144,11 +139,21 @@ public class YangUnion extends YangNode implements Parsable {
return YangConstructType.UNION_DATA;
}
/**
* Validates the data on entering the corresponding parse tree node.
*
* @throws DataModelException a violation of data model rules
*/
@Override
public void validateDataOnEntry() throws DataModelException {
//TODO: implement the method.
}
/**
* Validates the data on exiting the corresponding parse tree node.
*
* @throws DataModelException a violation of data model rules
*/
@Override
public void validateDataOnExit() throws DataModelException {
//TODO: implement the method.
......
......@@ -25,6 +25,7 @@ import org.onosproject.yangutils.datamodel.YangList;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangSubModule;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangUnion;
import org.onosproject.yangutils.datamodel.YangUses;
import org.onosproject.yangutils.datamodel.YangNotification;
import org.onosproject.yangutils.datamodel.YangRpc;
......@@ -40,6 +41,7 @@ import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaList;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeDef;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaUnion;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaUses;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaNotification;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaRpc;
......@@ -53,7 +55,7 @@ import org.onosproject.yangutils.translator.exception.TranslatorException;
public final class YangDataModelFactory {
/**
* Utility class, hence private to prevent creating objects.
* Creates a YANG data model factory object.
*/
private YangDataModelFactory() {
}
......@@ -224,7 +226,25 @@ public final class YangDataModelFactory {
* Returns based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated
* generated
* @return the corresponding inherited node based on the target language
*/
public static YangUnion getYangUnionNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
case JAVA_GENERATION: {
return new YangJavaUnion();
}
default: {
throw new TranslatorException("Only YANG to Java is supported.");
}
}
}
/**
* Returns based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated
* @return the corresponding inherited node based on the target language
*/
public static YangUses getYangUsesNode(GeneratedLanguage targetLanguage) {
......
......@@ -146,7 +146,7 @@ public final class TypeListener {
* in resolution list.
*/
if (yangDataTypes == YangDataTypes.DERIVED) {
// Parent YANG node of leaf to be added in resolution information.
// Parent YANG node of leaf list to be added in resolution information.
Parsable leafListData = listener.getParsedDataStack().pop();
Parsable parentNodeOfLeafList = listener.getParsedDataStack().peek();
listener.getParsedDataStack().push(leafListData);
......@@ -181,6 +181,26 @@ public final class TypeListener {
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
/*
* If data type is derived, resolution information to be added
* in resolution list.
*/
if (yangDataTypes == YangDataTypes.DERIVED) {
// Get the prefix information
String prefix = ((YangType<?>) type).getPrefix();
// Create empty derived info and attach it to type extended info.
YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
// Add resolution information to the list
YangResolutionInfo resolutionInfo =
new YangResolutionInfo<YangType>(type, (YangNode) unionNode, errorLine, errorPosition);
addToResolutionList(resolutionInfo, ctx);
}
break;
case TYPEDEF_DATA:
/* Prepare the base type info and set in derived type */
......@@ -244,7 +264,7 @@ public final class TypeListener {
* @param ctx context object of the grammar rule
*/
private static void addToResolutionList(YangResolutionInfo<YangType> resolutionInfo,
GeneratedYangParser.TypeStatementContext ctx) {
GeneratedYangParser.TypeStatementContext ctx) {
try {
addResolutionInfo(resolutionInfo);
} catch (DataModelException e) {
......
......@@ -53,6 +53,8 @@ import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import org.onosproject.yangutils.utils.YangConstructType;
import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangUnionNode;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
......@@ -96,7 +98,7 @@ public final class UnionListener {
checkStackIsNotEmpty(listener, MISSING_HOLDER, UNION_DATA, "", ENTRY);
if (listener.getParsedDataStack().peek() instanceof YangType) {
YangUnion unionNode = new YangUnion();
YangUnion unionNode = getYangUnionNode(JAVA_GENERATION);
Parsable typeData = listener.getParsedDataStack().pop();
// Check for stack to be non empty.
......
......@@ -22,12 +22,6 @@ package org.onosproject.yangutils.translator.tojava;
public final class GeneratedJavaFileType {
/**
* Creates an instance of generate java file type.
*/
private GeneratedJavaFileType() {
}
/**
* Interface file.
*/
public static final int INTERFACE_MASK = 1;
......@@ -56,4 +50,15 @@ public final class GeneratedJavaFileType {
* Java class corresponding to typedef.
*/
public static final int GENERATE_TYPEDEF_CLASS = 16;
/**
* Java class corresponding to union.
*/
public static final int GENERATE_UNION_CLASS = 32;
/**
* Creates an instance of generate java file type.
*/
private GeneratedJavaFileType() {
}
}
......
......@@ -22,53 +22,68 @@ package org.onosproject.yangutils.translator.tojava;
public final class GeneratedTempFileType {
/**
* Creates an instance of generated temp file type.
*/
private GeneratedTempFileType() {
}
/**
* attributes definition temporary file.
* Attributes definition temporary file.
*/
public static final int ATTRIBUTES_MASK = 1;
/**
* getter methods for interface.
* Getter methods for interface.
*/
public static final int GETTER_FOR_INTERFACE_MASK = 2;
/**
* getter methods for class.
* Getter methods for class.
*/
public static final int GETTER_FOR_CLASS_MASK = 4;
/**
* setter methods for interface.
* Setter methods for interface.
*/
public static final int SETTER_FOR_INTERFACE_MASK = 8;
/**
* setter methods for class.
* Setter methods for class.
*/
public static final int SETTER_FOR_CLASS_MASK = 16;
/**
* constructor method of class.
* Constructor method of class.
*/
public static final int CONSTRUCTOR_IMPL_MASK = 32;
/**
* hash code implementation of class.
* Hash code implementation of class.
*/
public static final int HASH_CODE_IMPL_MASK = 64;
/**
* equals implementation of class.
* Equals implementation of class.
*/
public static final int EQUALS_IMPL_MASK = 128;
/**
* to string implementation of class.
* To string implementation of class.
*/
public static final int TO_STRING_IMPL_MASK = 256;
/**
* Of string implementation of class.
*/
public static final int OF_STRING_IMPL_MASK = 512;
/**
* Constructor for type class like typedef, union.
*/
public static final int CONSTRUCTOR_FOR_TYPE_MASK = 1024;
/**
* From string implementation of class.
*/
public static final int UNION_FROM_STRING_IMPL_MASK = 2048;
/**
* Creates an instance of generated temp file type.
*/
private GeneratedTempFileType() {
}
}
......
......@@ -21,8 +21,9 @@ import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getIsQualifiedAccessOrAddToImportList;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfAttribute;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfLeafAttribute;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
/**
......@@ -65,9 +66,9 @@ public final class JavaAttributeInfo {
/**
* Creates object of java attribute info.
*
* @param attrType YANG type
* @param name attribute name
* @param isListAttr is list attribute
* @param attrType YANG type
* @param name attribute name
* @param isListAttr is list attribute
* @param isQualifiedName is qualified name
*/
public JavaAttributeInfo(YangType<?> attrType, String name, boolean isListAttr, boolean isQualifiedName) {
......@@ -78,6 +79,51 @@ public final class JavaAttributeInfo {
}
/**
* Creates an attribute info object corresponding to the passed type's attribute
* information and return it.
*
* @param curNode current data model node for which the java file is being generated
* @param referredTypesAttrInfo attribute of referred type
* @return JavaAttributeInfo attribute details required to add in temporary files
*/
public static JavaAttributeInfo getFromStringAttributeInfo(YangNode curNode,
JavaAttributeInfo referredTypesAttrInfo) {
JavaQualifiedTypeInfo qualifiedInfoOfFromString = getQualifiedInfoOfFromString(referredTypesAttrInfo);
/*
* Create a new java attribute info with qualified information of
* wrapper classes.
*/
return getAttributeInfoForTheData(qualifiedInfoOfFromString, referredTypesAttrInfo.getAttributeName(),
referredTypesAttrInfo.getAttributeType(), curNode, false);
}
/**
* Creates an attribute info object corresponding to the passed type attribute
* information and return it.
*
* @param curNode current data model node for which the java file is being
* generated
* @param attributeType leaf data type
* @param attributeName leaf name
* @param isListAttribute is the current added attribute needs to be a list
* @return AttributeInfo attribute details required to add in temporary
* files
*/
public static JavaAttributeInfo getAttributeInfoOfType(YangNode curNode,
YangType<?> attributeType, String attributeName,
boolean isListAttribute) {
/*
* Get the import info corresponding to the attribute for import in
* generated java files or qualified access
*/
JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfAttribute(curNode,
attributeType, attributeName, isListAttribute);
return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute);
}
/**
* Returns the data type info of attribute.
*
* @return the data type info of attribute
......@@ -144,7 +190,7 @@ public final class JavaAttributeInfo {
* manner.
*
* @return the if the added attribute has to be accessed in a fully
* qualified manner.
* qualified manner.
*/
public boolean isQualifiedName() {
return isQualifiedName;
......@@ -155,7 +201,7 @@ public final class JavaAttributeInfo {
* manner.
*
* @param isQualified if the added attribute has to be accessed in a fully
* qualified manner
* qualified manner
*/
public void setIsQualifiedAccess(boolean isQualified) {
isQualifiedName = isQualified;
......@@ -184,23 +230,23 @@ public final class JavaAttributeInfo {
* Creates an attribute info object corresponding to the passed leaf
* information and return it.
*
* @param curNode current data model node for which the java file is being
* generated
* @param attributeType leaf data type
* @param attributeName leaf name
* @param curNode current data model node for which the java file is being
* generated
* @param attributeType leaf data type
* @param attributeName leaf name
* @param isListAttribute is the current added attribute needs to be a list
* @return AttributeInfo attribute details required to add in temporary
* files
* files
*/
public static JavaAttributeInfo getAttributeInfoOfLeaf(YangNode curNode,
YangType<?> attributeType, String attributeName,
boolean isListAttribute) {
YangType<?> attributeType, String attributeName,
boolean isListAttribute) {
/*
* Get the import info corresponding to the attribute for import in
* generated java files or qualified access
*/
JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfLeafAttribute(curNode,
JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfAttribute(curNode,
attributeType, attributeName, isListAttribute);
return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute);
......@@ -210,12 +256,12 @@ public final class JavaAttributeInfo {
* Creates an attribute info object corresponding to a data model node and
* return it.
*
* @param curNode current data model node for which the java code generation
* is being handled
* @param curNode current data model node for which the java code generation
* is being handled
* @param parentNode parent node in which the current node is an attribute
* @param isListNode is the current added attribute needs to be a list
* @return AttributeInfo attribute details required to add in temporary
* files
* files
*/
public static JavaAttributeInfo getCurNodeAsAttributeInParent(
YangNode curNode, YangNode parentNode, boolean isListNode) {
......@@ -233,43 +279,18 @@ public final class JavaAttributeInfo {
}
/**
* Creates an attribute info object corresponding to the passed type def attribute
* information and return it.
*
* @param curNode current data model node for which the java file is being
* generated
* @param attributeType leaf data type
* @param attributeName leaf name
* @param isListAttribute is the current added attribute needs to be a list
* @return AttributeInfo attribute details required to add in temporary
* files
*/
public static JavaAttributeInfo getAttributeInfoOfTypeDef(YangNode curNode,
YangType<?> attributeType, String attributeName,
boolean isListAttribute) {
/*
* Get the import info corresponding to the attribute for import in
* generated java files or qualified access
*/
JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfLeafAttribute(curNode,
attributeType, attributeName, isListAttribute);
return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute);
}
/**
* Returns java attribute info.
*
* @param importInfo java qualified type info
* @param attributeName attribute name
* @param attributeType attribute type
* @param curNode current YANG node
* @param importInfo java qualified type info
* @param attributeName attribute name
* @param attributeType attribute type
* @param curNode current YANG node
* @param isListAttribute is list attribute
* @return java attribute info.
*/
private static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfo importInfo, String attributeName,
YangType<?> attributeType, YangNode curNode, boolean isListAttribute) {
YangType<?> attributeType, YangNode curNode,
boolean isListAttribute) {
JavaAttributeInfo newAttr = new JavaAttributeInfo();
newAttr.setImportInfo(importInfo);
......
......@@ -17,14 +17,15 @@
package org.onosproject.yangutils.translator.tojava;
import java.util.Objects;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
import com.google.common.base.MoreObjects;
import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportClass;
import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportPackage;
/**
* Represents the information about individual imports in the generated file.
*/
......@@ -86,18 +87,18 @@ public class JavaQualifiedTypeInfo implements Comparable<JavaQualifiedTypeInfo>
* Returns the import info for an attribute, which needs to be used for code
* generation for import or for qualified access.
*
* @param curNode current data model node for which the java file is being
* generated
* @param attrType type of attribute being added, it will be null, when the
* child class is added as an attribute
* @param curNode current data model node for which the java file is being
* generated
* @param attrType type of attribute being added, it will be null, when the
* child class is added as an attribute
* @param attributeName name of the attribute being added, it will used in
* import info for child class
* @param isListAttr is the added attribute going to be used as a list
* import info for child class
* @param isListAttr is the added attribute going to be used as a list
* @return return the import info for this attribute
*/
public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfLeafAttribute(YangNode curNode,
YangType<?> attrType, String attributeName,
boolean isListAttr) {
public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfAttribute(YangNode curNode,
YangType<?> attrType, String attributeName,
boolean isListAttr) {
JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
......@@ -139,15 +140,15 @@ public class JavaQualifiedTypeInfo implements Comparable<JavaQualifiedTypeInfo>
* Returns the import info for an attribute, which needs to be used for code
* generation for import or for qualified access.
*
* @param curNode current data model node for which the java file is being
* generated
* @param curNode current data model node for which the java file is being
* generated
* @param attributeName name of the attribute being added, it will used in
* import info for child class
* @param isListAttr is the added attribute going to be used as a list
* import info for child class
* @param isListAttr is the added attribute going to be used as a list
* @return return the import info for this attribute
*/
public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfCurNode(YangNode curNode,
String attributeName, boolean isListAttr) {
String attributeName, boolean isListAttr) {
JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
......@@ -168,16 +169,35 @@ public class JavaQualifiedTypeInfo implements Comparable<JavaQualifiedTypeInfo>
}
/**
* Get the java qualified type information for the wrapper classes.
*
* @param referredTypesAttrInfo attribute of referred type
* @return return the import info for this attribute
*/
public static JavaQualifiedTypeInfo getQualifiedInfoOfFromString(JavaAttributeInfo referredTypesAttrInfo) {
/*
* Get the java qualified type information for the wrapper classes and
* set it in new java attribute information.
*/
JavaQualifiedTypeInfo qualifiedInfoOfFromString = new JavaQualifiedTypeInfo();
qualifiedInfoOfFromString.setClassInfo(
getJavaImportClass(referredTypesAttrInfo.getAttributeType(), true));
qualifiedInfoOfFromString.setPkgInfo(
getJavaImportPackage(referredTypesAttrInfo.getAttributeType(), true, null));
return qualifiedInfoOfFromString;
}
/**
* Returns if the attribute needs to be accessed in a qualified manner or not,
* if it needs to be imported, then the same needs to be done.
*
* @param curNode current cache of the data model node for which java file
* is bing generated
* @param curNode current cache of the data model node for which java file
* is bing generated
* @param importInfo import info for the current attribute being added
* @return status of the qualified access to the attribute
*/
public static boolean getIsQualifiedAccessOrAddToImportList(YangNode curNode,
JavaQualifiedTypeInfo importInfo) {
JavaQualifiedTypeInfo importInfo) {
boolean isImportPkgEqualCurNodePkg;
if (!(curNode instanceof HasJavaFileInfo)) {
......@@ -229,10 +249,10 @@ public class JavaQualifiedTypeInfo implements Comparable<JavaQualifiedTypeInfo>
* Checks if the import info is same as the package of the current generated
* java file.
*
* @param curNode Java identifier of the current data model node
* @param curNode Java identifier of the current data model node
* @param importInfo import info for an attribute
* @return true if the import info is same as the current nodes package
* false otherwise
* false otherwise
*/
public static boolean isImportPkgEqualCurNodePkg(
YangNode curNode, JavaQualifiedTypeInfo importInfo) {
......
......@@ -20,31 +20,36 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.onosproject.yangutils.datamodel.HasType;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangLeavesHolder;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.UNION_FROM_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfLeaf;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfTypeDef;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfType;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getCurNodeAsAttributeInParent;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getFromStringAttributeInfo;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
......@@ -52,6 +57,7 @@ import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerato
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
......@@ -62,16 +68,17 @@ import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForTypeDefClass;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeDefConstructor;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport;
import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
......@@ -91,11 +98,9 @@ import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_CONSTRUCTOR;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_SETTER_METHOD;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
......@@ -193,6 +198,21 @@ public class TempJavaCodeFragmentFiles {
private static final String EQUALS_METHOD_FILE_NAME = "Equals";
/**
* File name for of string method.
*/
private static final String OF_STRING_METHOD_FILE_NAME = "OfString";
/**
* File name for construction for special type like union, typedef.
*/
private static final String CONSTRUCTOR_FOR_TYPE_FILE_NAME = "ConstructorForType";
/**
* File name for from string method.
*/
private static final String UNION_FROM_STRING_METHOD_FILE_NAME = "UnionFromString";
/**
* File name for interface java file name suffix.
*/
private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
......@@ -218,6 +238,11 @@ public class TempJavaCodeFragmentFiles {
private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
/**
* File name for generated class file for special type like union, typedef suffix.
*/
private static final String UNION_TYPE_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
/**
* Java file handle for interface file.
*/
private File interfaceJavaFileHandle;
......@@ -243,6 +268,11 @@ public class TempJavaCodeFragmentFiles {
private File typedefClassJavaFileHandle;
/**
* Java file handle for type class like union, typedef file.
*/
private File typeClassJavaFileHandle;
/**
* Temporary file handle for attribute.
*/
private File attributesTempFileHandle;
......@@ -288,6 +318,21 @@ public class TempJavaCodeFragmentFiles {
private File toStringImplTempFileHandle;
/**
* Temporary file handle for of string method of class.
*/
private File ofStringImplTempFileHandle;
/**
* Temporary file handle for constructor for type class.
*/
private File constructorForTypeTempFileHandle;
/**
* Temporary file handle for union's from string method of class.
*/
private File unionFromStringImplTempFileHandle;
/**
* Java attribute info.
*/
private JavaAttributeInfo newAttrInfo;
......@@ -306,8 +351,8 @@ public class TempJavaCodeFragmentFiles {
* Creates an instance of temporary java code fragment.
*
* @param genFileType file generation type
* @param genDir file generation directory
* @param className class name
* @param genDir file generation directory
* @param className class name
* @throws IOException when fails to create new file handle
*/
public TempJavaCodeFragmentFiles(int genFileType, String genDir, String className)
......@@ -368,6 +413,24 @@ public class TempJavaCodeFragmentFiles {
generatedTempFiles |= HASH_CODE_IMPL_MASK;
generatedTempFiles |= EQUALS_IMPL_MASK;
generatedTempFiles |= TO_STRING_IMPL_MASK;
generatedTempFiles |= OF_STRING_IMPL_MASK;
generatedTempFiles |= CONSTRUCTOR_FOR_TYPE_MASK;
}
/**
* Initialize getterImpl, attributes, hash code, equals, of string,
* constructor, union's to string, union's from string when generation
* file type matches to union class mask.
*/
if ((genFileType & GENERATE_UNION_CLASS) != 0) {
generatedTempFiles |= ATTRIBUTES_MASK;
generatedTempFiles |= GETTER_FOR_CLASS_MASK;
generatedTempFiles |= HASH_CODE_IMPL_MASK;
generatedTempFiles |= EQUALS_IMPL_MASK;
generatedTempFiles |= OF_STRING_IMPL_MASK;
generatedTempFiles |= CONSTRUCTOR_FOR_TYPE_MASK;
generatedTempFiles |= TO_STRING_IMPL_MASK;
generatedTempFiles |= UNION_FROM_STRING_IMPL_MASK;
}
if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
......@@ -404,6 +467,18 @@ public class TempJavaCodeFragmentFiles {
if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
}
if ((generatedTempFiles & OF_STRING_IMPL_MASK) != 0) {
setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
}
if ((generatedTempFiles & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME));
}
if ((generatedTempFiles & UNION_FROM_STRING_IMPL_MASK) != 0) {
setUnionFromStringImplTempFileHandle(getTemporaryFileHandle(UNION_FROM_STRING_METHOD_FILE_NAME));
}
}
/**
......@@ -497,6 +572,24 @@ public class TempJavaCodeFragmentFiles {
}
/**
* Returns java file handle for type class file.
*
* @return java file handle for type class file
*/
private File getTypeClassJavaFileHandle() {
return typeClassJavaFileHandle;
}
/**
* Sets the java file handle for type class file.
*
* @param typeClassJavaFileHandle type file handle
*/
private void setTypeClassJavaFileHandle(File typeClassJavaFileHandle) {
this.typeClassJavaFileHandle = typeClassJavaFileHandle;
}
/**
* Returns attribute's temporary file handle.
*
* @return temporary file handle
......@@ -659,6 +752,60 @@ public class TempJavaCodeFragmentFiles {
}
/**
* Returns of string method's temporary file handle.
*
* @return of string method's temporary file handle
*/
public File getOfStringImplTempFileHandle() {
return ofStringImplTempFileHandle;
}
/**
* Set of string method's temporary file handle.
*
* @param ofStringImplTempFileHandle of string method's temporary file handle
*/
private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) {
this.ofStringImplTempFileHandle = ofStringImplTempFileHandle;
}
/**
* Returns type class constructor method's temporary file handle.
*
* @return type class constructor method's temporary file handle
*/
public File getConstructorForTypeTempFileHandle() {
return constructorForTypeTempFileHandle;
}
/**
* Sets type class constructor method's temporary file handle.
*
* @param constructorForTypeTempFileHandle type class constructor method's temporary file handle
*/
private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) {
this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle;
}
/**
* Returns union's from string method's temporary file handle.
*
* @return union's from string method's temporary file handle
*/
public File getUnionFromStringImplTempFileHandle() {
return unionFromStringImplTempFileHandle;
}
/**
* Sets union's from string method's temporary file handle.
*
* @param unionFromStringImplTempFileHandle union's from string method's temporary file handle
*/
private void setUnionFromStringImplTempFileHandle(File unionFromStringImplTempFileHandle) {
this.unionFromStringImplTempFileHandle = unionFromStringImplTempFileHandle;
}
/**
* Returns java attribute info.
*
* @return java attribute info
......@@ -727,6 +874,28 @@ public class TempJavaCodeFragmentFiles {
}
/**
* Adds of string for type.
*
* @param attr attribute info
* @throws IOException when fails to append to temporary file
*/
private void addOfStringMethod(JavaAttributeInfo attr) throws IOException {
appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr, generatedJavaClassName)
+ NEW_LINE);
}
/**
* Adds type constructor.
*
* @param attr attribute info
* @throws IOException when fails to append to temporary file
*/
private void addTypeConstructor(JavaAttributeInfo attr) throws IOException {
appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr,
generatedJavaClassName) + NEW_LINE);
}
/**
* Adds attribute for class.
*
* @param attr attribute info
......@@ -749,7 +918,7 @@ public class TempJavaCodeFragmentFiles {
/**
* Adds getter method's impl for class.
*
* @param attr attribute info
* @param attr attribute info
* @param genFiletype generated file type
* @throws IOException when fails to append to temporary file
*/
......@@ -828,28 +997,6 @@ public class TempJavaCodeFragmentFiles {
}
/**
* Adds typedef constructor for class.
*
* @return typedef constructor for class
* @throws IOException when fails to append to file
*/
public String addTypeDefConstructor() throws IOException {
return NEW_LINE + getJavaDoc(TYPE_DEF_CONSTRUCTOR, generatedJavaClassName, false)
+ getTypeDefConstructor(newAttrInfo, generatedJavaClassName) + NEW_LINE;
}
/**
* Adds default constructor for class.
*
* @return default constructor for class
* @throws IOException when fails to append to file
*/
public String addTypeDefsSetter() throws IOException {
return getJavaDoc(TYPE_DEF_SETTER_METHOD, generatedJavaClassName, false) + getSetterForTypeDefClass(newAttrInfo)
+ NEW_LINE;
}
/**
* Adds default constructor for class.
*
* @return default constructor for class
......@@ -891,6 +1038,19 @@ public class TempJavaCodeFragmentFiles {
}
/**
* Add from string method for union class.
*
* @param javaAttributeInfo type attribute info
* @param fromStringAttributeInfo from string attribute info
* @throws IOException when fails to append to temporary file
*/
private void addUnionFromStringMethod(JavaAttributeInfo javaAttributeInfo,
JavaAttributeInfo fromStringAttributeInfo) throws IOException {
appendToFile(getUnionFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
fromStringAttributeInfo) + NEW_LINE);
}
/**
* Returns a temporary file handle for the specific file type.
*
* @param fileName file name
......@@ -993,12 +1153,12 @@ public class TempJavaCodeFragmentFiles {
* Adds current node info as and attribute to the parent generated file.
*
* @param curNode current node which needs to be added as an attribute in
* the parent generated code
* @param isList is list construct
* the parent generated code
* @param isList is list construct
* @throws IOException IO operation exception
*/
public void addCurNodeInfoInParentTempFile(YangNode curNode,
boolean isList) throws IOException {
boolean isList) throws IOException {
YangNode parent = getParentNodeInGenCode(curNode);
if (!(parent instanceof JavaCodeGenerator)) {
......@@ -1019,11 +1179,11 @@ public class TempJavaCodeFragmentFiles {
* Adds leaf attributes in generated files.
*
* @param listOfLeaves list of YANG leaf
* @param curNode current data model node
* @param curNode current data model node
* @throws IOException IO operation fail
*/
private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
YangNode curNode) throws IOException {
YangNode curNode) throws IOException {
if (listOfLeaves != null) {
for (YangLeaf leaf : listOfLeaves) {
......@@ -1039,11 +1199,11 @@ public class TempJavaCodeFragmentFiles {
* Adds leaf list's attributes in generated files.
*
* @param listOfLeafList list of YANG leaves
* @param curNode cached file handle
* @param curNode cached file handle
* @throws IOException IO operation fail
*/
private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList,
YangNode curNode) throws IOException {
YangNode curNode) throws IOException {
if (listOfLeafList != null) {
......@@ -1087,16 +1247,45 @@ public class TempJavaCodeFragmentFiles {
}
/**
* Adds leaf attributes in generated files.
* Add all the type in the current data model node as part of the
* generated temporary file.
*
* @param curNode current data model node
* @param hasType YANG java data model node which has type info, eg union / typedef
* @throws IOException IO operation fail
*/
public void addTypeDefAttributeToTempFiles(YangNode curNode) throws IOException {
public void addTypeInfoToTempFiles(HasType hasType) throws IOException {
List<YangType<?>> typeList = hasType.getTypeList();
if (typeList != null) {
for (YangType<?> yangType : typeList) {
JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfType((YangNode) hasType,
yangType, getTypeName(yangType.getDataTypeName()), false);
addJavaSnippetInfoToApplicableTempFiles((YangNode) hasType, javaAttributeInfo);
}
}
}
private String getTypeName(String dataTypeName) {
return dataTypeName;
//TODO: implement the method.
}
/**
* Adds the new attribute info to the target generated temporary files for union class.
*
* @param hasType the node for which the type is being added as an attribute
* @param javaAttributeInfo the attribute info that needs to be added to temporary
* files
* @throws IOException IO operation fail
*/
private void addJavaSnippetInfoToApplicableTempFiles(YangNode hasType, JavaAttributeInfo javaAttributeInfo)
throws IOException {
JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfTypeDef(curNode,
((YangTypeDef) curNode).getTypeDefBaseType(),
((YangTypeDef) curNode).getName(), false);
JavaAttributeInfo fromStringAttributeInfo = getFromStringAttributeInfo(hasType, javaAttributeInfo);
if ((generatedTempFiles & UNION_FROM_STRING_IMPL_MASK) != 0) {
addUnionFromStringMethod(javaAttributeInfo, fromStringAttributeInfo);
}
addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
}
......@@ -1104,7 +1293,7 @@ public class TempJavaCodeFragmentFiles {
* Adds the new attribute info to the target generated temporary files.
*
* @param newAttrInfo the attribute info that needs to be added to temporary
* files
* files
* @throws IOException IO operation fail
*/
void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
......@@ -1147,6 +1336,15 @@ public class TempJavaCodeFragmentFiles {
if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
addToStringMethod(newAttrInfo);
}
if ((generatedTempFiles & OF_STRING_IMPL_MASK) != 0) {
addOfStringMethod(newAttrInfo);
}
if ((generatedTempFiles & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
addTypeConstructor(newAttrInfo);
}
}
}
......@@ -1182,7 +1380,7 @@ public class TempJavaCodeFragmentFiles {
* Constructs java code exit.
*
* @param fileType generated file type
* @param curNode current YANG node
* @param curNode current YANG node
* @throws IOException when fails to generate java files
*/
public void generateJavaFile(int fileType, YangNode curNode) throws IOException {
......@@ -1285,6 +1483,15 @@ public class TempJavaCodeFragmentFiles {
}
/**
* Creates type class file.
*/
if ((fileType & GENERATE_UNION_CLASS) != 0) {
addImportsToStringAndHasCodeMethods(curNode, imports);
setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX)));
setTypeClassJavaFileHandle(generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports));
}
/**
* Close all the file handles.
*/
close(false);
......@@ -1294,8 +1501,7 @@ public class TempJavaCodeFragmentFiles {
* Removes all temporary file handles.
*
* @param isErrorOccurred when translator fails to generate java files we need to close
* all open file handles include temporary files and java files.
*
* all open file handles include temporary files and java files.
* @throws IOException when failed to delete the temporary files
*/
public void close(boolean isErrorOccurred) throws IOException {
......@@ -1319,6 +1525,9 @@ public class TempJavaCodeFragmentFiles {
if ((generatedJavaFiles & GENERATE_TYPEDEF_CLASS) != 0) {
closeFile(getTypedefClassJavaFileHandle(), isError);
}
if ((generatedJavaFiles & GENERATE_UNION_CLASS) != 0) {
closeFile(getTypeClassJavaFileHandle(), isError);
}
/**
* Close all temporary file handles and delete the files.
......@@ -1350,7 +1559,15 @@ public class TempJavaCodeFragmentFiles {
if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
closeFile(getEqualsImplTempFileHandle(), true);
}
if ((generatedTempFiles & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
closeFile(getConstructorForTypeTempFileHandle(), true);
}
if ((generatedTempFiles & OF_STRING_IMPL_MASK) != 0) {
closeFile(getOfStringImplTempFileHandle(), true);
}
if ((generatedTempFiles & UNION_FROM_STRING_IMPL_MASK) != 0) {
closeFile(getUnionFromStringImplTempFileHandle(), true);
}
clean(getTempDirPath());
generatedTempFiles = 0;
}
......
......@@ -16,12 +16,8 @@
package org.onosproject.yangutils.translator.tojava.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles;
import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaImportData;
......@@ -29,17 +25,12 @@ import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfType;
/**
* Represents type define information extended to support java code generation.
*/
public class YangJavaTypeDef extends YangTypeDef
implements JavaCodeGenerator, HasJavaFileInfo, HasJavaImportData, HasTempJavaCodeFragmentFiles {
public class YangJavaTypeDef extends YangTypeDef implements JavaCodeGeneratorInfo, JavaCodeGenerator {
/**
* Contains the information of the java file being generated.
......@@ -106,7 +97,7 @@ public class YangJavaTypeDef extends YangTypeDef
* Sets the data of java imports to be included in generated file.
*
* @param javaImportData data of java imports to be included in generated
* file
* file
*/
@Override
public void setJavaImportData(JavaImportData javaImportData) {
......@@ -142,23 +133,7 @@ public class YangJavaTypeDef extends YangTypeDef
*/
@Override
public void generateCodeEntry(YangPluginConfig yangPlugin) throws IOException {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName(), yangPlugin.getConflictResolver())));
getJavaFileInfo().setPackage(getCurNodePackage(this));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
getTempJavaCodeFragmentFiles().addTypeDefAttributeToTempFiles(this);
generateCodeOfType(this, yangPlugin, false);
}
/**
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.yangutils.translator.tojava.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangUnion;
import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfType;
/**
* Represents union information extended to support java code generation.
*/
public class YangJavaUnion extends YangUnion implements JavaCodeGeneratorInfo, JavaCodeGenerator {
/**
* Contains the information of the java file being generated.
*/
private JavaFileInfo javaFileInfo;
/**
* Contains information of the imports to be inserted in the java file
* generated.
*/
private JavaImportData javaImportData;
/**
* File handle to maintain temporary java code fragments as per the code
* snippet types.
*/
private TempJavaCodeFragmentFiles tempFileHandle;
/**
* Creates an instance of YANG java union.
*/
public YangJavaUnion() {
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_UNION_CLASS);
}
/**
* Returns the generated java file information.
*
* @return generated java file information
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
return javaFileInfo;
}
/**
* Sets the java file info object.
*
* @param javaInfo java file info object
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
/**
* Returns the data of java imports to be included in generated file.
*
* @return data of java imports to be included in generated file
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
/**
* Sets the data of java imports to be included in generated file.
*
* @param javaImportData data of java imports to be included in generated
* file
*/
@Override
public void setJavaImportData(JavaImportData javaImportData) {
this.javaImportData = javaImportData;
}
/**
* Returns the temporary file handle.
*
* @return temporary file handle
*/
@Override
public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
if (tempFileHandle == null) {
throw new RuntimeException("Missing temp file hand for current node "
+ getJavaFileInfo().getJavaName());
}
return tempFileHandle;
}
/**
* Sets temporary file handle.
*
* @param fileHandle temporary file handle
*/
@Override
public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
tempFileHandle = fileHandle;
}
/**
* Prepare the information for java code generation corresponding to YANG
* union info.
*
* @param yangPlugin YANG plugin config
* @throws IOException IO operations fails
*/
@Override
public void generateCodeEntry(YangPluginConfig yangPlugin) throws IOException {
generateCodeOfType(this, yangPlugin, false);
}
/**
* Creates a java file using the YANG union info.
*
* @throws IOException IO operations fails
*/
@Override
public void generateCodeExit() throws IOException {
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_UNION_CLASS, this);
}
}
......@@ -21,10 +21,12 @@ import org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangUnion;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeDef;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaUnion;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
......@@ -33,15 +35,23 @@ import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_DATA_TYPE;
import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.BYTE;
import static org.onosproject.yangutils.utils.UtilConstants.BYTE_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.INT;
import static org.onosproject.yangutils.utils.UtilConstants.INTEGER_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_MATH;
import static org.onosproject.yangutils.utils.UtilConstants.LONG;
import static org.onosproject.yangutils.utils.UtilConstants.LONG_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.NEW;
import static org.onosproject.yangutils.utils.UtilConstants.OF;
import static org.onosproject.yangutils.utils.UtilConstants.PARSE_BYTE;
import static org.onosproject.yangutils.utils.UtilConstants.PARSE_INT;
import static org.onosproject.yangutils.utils.UtilConstants.PARSE_LONG;
import static org.onosproject.yangutils.utils.UtilConstants.PARSE_SHORT;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.SHORT;
import static org.onosproject.yangutils.utils.UtilConstants.SHORT_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
/**
......@@ -94,9 +104,56 @@ public final class AttributesJavaDataType {
}
/**
* Returns from string method parsed string.
*
* @param targetDataType target data type
* @param yangType YANG type
* @return parsed string
*/
public static String getParseFromStringMethod(String targetDataType, YangType<?> yangType) {
YangDataTypes type = yangType.getDataType();
switch (type) {
case INT8:
return BYTE_WRAPPER + PERIOD + PARSE_BYTE;
case INT16:
return SHORT_WRAPPER + PERIOD + PARSE_SHORT;
case INT32:
return INTEGER_WRAPPER + PERIOD + PARSE_INT;
case INT64:
return LONG_WRAPPER + PERIOD + PARSE_LONG;
case UINT8:
return SHORT_WRAPPER + PERIOD + PARSE_SHORT;
case UINT16:
return INTEGER_WRAPPER + PERIOD + PARSE_INT;
case UINT32:
return LONG_WRAPPER + PERIOD + PARSE_LONG;
case UINT64:
return NEW + SPACE + BIG_INTEGER;
case DECIMAL64:
//TODO: DECIMAL64.
case STRING:
return EMPTY_STRING;
case BOOLEAN:
return BOOLEAN_DATA_TYPE;
case ENUMERATION:
//TODO:ENUMERATION.
case BITS:
//TODO:BITS
case BINARY:
//TODO:BINARY
case DERIVED:
return targetDataType + PERIOD + OF;
default:
throw new TranslatorException("given data type is not supported.");
}
}
/**
* Returns java import class.
*
* @param yangType YANG type
* @param yangType YANG type
* @param isListAttr if the attribute need to be a list
* @return java import class
*/
......@@ -141,13 +198,14 @@ public final class AttributesJavaDataType {
case EMPTY:
return BOOLEAN_WRAPPER;
case UNION:
//TODO:UNION
return getCaptialCase(getCamelCase(((YangJavaUnion) yangType.getDataTypeExtendedInfo()).getName(),
null));
case INSTANCE_IDENTIFIER:
//TODO:INSTANCE_IDENTIFIER
case DERIVED:
return getCaptialCase(getCamelCase(yangType.getDataTypeName(), null));
default:
throw new TranslatorException("given data type is not supported.");
return null;
}
} else {
switch (type) {
......@@ -170,7 +228,8 @@ public final class AttributesJavaDataType {
case EMPTY:
//TODO:EMPTY
case UNION:
//TODO:UNION
return getCaptialCase(getCamelCase(((YangJavaUnion) yangType.getDataTypeExtendedInfo()).getName(),
null));
case INSTANCE_IDENTIFIER:
//TODO:INSTANCE_IDENTIFIER
case DERIVED:
......@@ -184,9 +243,9 @@ public final class AttributesJavaDataType {
/**
* Returns java import package.
*
* @param yangType YANG type
* @param yangType YANG type
* @param isListAttr if the attribute is of list type
* @param classInfo java import class info
* @param classInfo java import class info
* @return java import package
*/
public static String getJavaImportPackage(YangType<?> yangType, boolean isListAttr, String classInfo) {
......@@ -222,13 +281,13 @@ public final class AttributesJavaDataType {
case EMPTY:
//TODO:EMPTY
case UNION:
//TODO:UNION
return getUnionPackage(yangType);
case INSTANCE_IDENTIFIER:
//TODO:INSTANCE_IDENTIFIER
case DERIVED:
return getTypDefsPackage(yangType);
default:
throw new TranslatorException("given data type is not supported.");
return null;
}
} else {
switch (type) {
......@@ -251,7 +310,7 @@ public final class AttributesJavaDataType {
case EMPTY:
//TODO:EMPTY
case UNION:
//TODO:UNION
return getUnionPackage(yangType);
case INSTANCE_IDENTIFIER:
//TODO:INSTANCE_IDENTIFIER
case DERIVED:
......@@ -286,6 +345,25 @@ public final class AttributesJavaDataType {
}
/**
* Returns java package for union node.
*
* @param type YANG type
* @return java package for union node
*/
private static String getUnionPackage(YangType<?> type) {
if (!(type.getDataTypeExtendedInfo() instanceof YangUnion)) {
throw new TranslatorException("type should have been union.");
}
YangJavaUnion union = (YangJavaUnion) type.getDataTypeExtendedInfo();
if (union.getJavaFileInfo().getPackage() == null) {
return getPackageFromParent(union.getParent());
}
return union.getJavaFileInfo().getPackage();
}
/**
* Returns package from parent node.
*
* @param parent parent YANG node
......@@ -293,7 +371,7 @@ public final class AttributesJavaDataType {
*/
private static String getPackageFromParent(YangNode parent) {
if (!(parent instanceof HasJavaFileInfo)) {
throw new TranslatorException("Invalid child node is being processed.");
throw new TranslatorException("invalid child node is being processed.");
}
JavaFileInfo parentInfo = ((HasJavaFileInfo) parent).getJavaFileInfo();
return parentInfo.getPackage() + PERIOD + parentInfo.getJavaName().toLowerCase();
......
......@@ -19,6 +19,7 @@ package org.onosproject.yangutils.translator.tojava.utils;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.getExtendsList;
......@@ -54,7 +55,7 @@ public final class ClassDefinitionGenerator {
* / interface definition start.
*
* @param genFileTypes generated file type
* @param yangName class name
* @param yangName class name
* @return class definition
*/
public static String generateClassDefinition(int genFileTypes, String yangName) {
......@@ -64,20 +65,17 @@ public final class ClassDefinitionGenerator {
* class / interface definition start.
*/
if ((genFileTypes & INTERFACE_MASK) != 0) {
return getInterfaceDefinition(yangName);
} else if ((genFileTypes & BUILDER_CLASS_MASK) != 0) {
return getBuilderClassDefinition(yangName);
} else if ((genFileTypes & IMPL_CLASS_MASK) != 0) {
return getImplClassDefinition(yangName);
} else if ((genFileTypes & BUILDER_INTERFACE_MASK) != 0) {
return getBuilderInterfaceDefinition(yangName);
} else if ((genFileTypes & GENERATE_TYPEDEF_CLASS) != 0) {
return getTypeDefClassDefinition(yangName);
return getTypeClassDefinition(yangName);
} else if ((genFileTypes & GENERATE_UNION_CLASS) != 0) {
return getTypeClassDefinition(yangName);
}
return null;
}
......@@ -105,7 +103,7 @@ public final class ClassDefinitionGenerator {
* Returns builder interface file class definition.
*
* @param yangName java class name, corresponding to which the builder class
* is being generated
* is being generated
* @return definition
*/
private static String getBuilderInterfaceDefinition(String yangName) {
......@@ -135,12 +133,12 @@ public final class ClassDefinitionGenerator {
}
/**
* Returns typeDef file class definition.
* Returns type file class definition.
*
* @param yangName file name
* @return definition
*/
private static String getTypeDefClassDefinition(String yangName) {
private static String getTypeClassDefinition(String yangName) {
return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
}
......
......@@ -20,7 +20,6 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles;
......@@ -29,17 +28,21 @@ import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.UNION_FROM_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getAugmentedInfoAttribute;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getDataFromTempFileHandle;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.initiateJavaFileGeneration;
......@@ -49,8 +52,11 @@ import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructorStart;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethodClose;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethodOpen;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethodClose;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethodSignature;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodClose;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodOpen;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOmitNullValueString;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRemoveAugmentationImpl;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethodClose;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethodOpen;
......@@ -82,8 +88,8 @@ public final class JavaFileGenerator {
private static List<String> extendsList = new ArrayList<>();
/**
* Creates an instance of java file generator.
*/
* Creates an instance of java file generator.
*/
private JavaFileGenerator() {
}
......@@ -126,10 +132,10 @@ public final class JavaFileGenerator {
/**
* Returns generated interface file for current node.
*
* @param file file
* @param imports imports for the file
* @param curNode current YANG node
* @param isAttrPresent if any attribute is present or not
* @param file file
* @param imports imports for the file
* @param curNode current YANG node
* @param isAttrPresent if any attribute is present or not
* @return interface file
* @throws IOException when fails to write in file
*/
......@@ -163,9 +169,9 @@ public final class JavaFileGenerator {
/**
* Return generated builder interface file for current node.
*
* @param file file
* @param curNode current YANG node
* @param isAttrPresent if any attribute is present or not
* @param file file
* @param curNode current YANG node
* @param isAttrPresent if any attribute is present or not
* @return builder interface file
* @throws IOException when fails to write in file
*/
......@@ -215,15 +221,15 @@ public final class JavaFileGenerator {
/**
* Returns generated builder class file for current node.
*
* @param file file
* @param imports imports for the file
* @param curNode current YANG node
* @param isAttrPresent if any attribute is present or not
* @param file file
* @param imports imports for the file
* @param curNode current YANG node
* @param isAttrPresent if any attribute is present or not
* @return builder class file
* @throws IOException when fails to write in file
*/
public static File generateBuilderClassFile(File file, List<String> imports, YangNode curNode,
boolean isAttrPresent) throws IOException {
boolean isAttrPresent) throws IOException {
JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
......@@ -281,8 +287,8 @@ public final class JavaFileGenerator {
/**
* Returns generated impl class file for current node.
*
* @param file file
* @param curNode current YANG node
* @param file file
* @param curNode current YANG node
* @param isAttrPresent if any attribute is present or not
* @return impl class file
* @throws IOException when fails to write in file
......@@ -378,14 +384,14 @@ public final class JavaFileGenerator {
}
/**
* Generates class file for type def.
* Generate class file for type def.
*
* @param file generated file
* @param file generated file
* @param curNode current YANG node
* @param imports imports for file
* @return type def class file
* @throws IOException when fails to generate class file
*/
*/
public static File generateTypeDefClassFile(File file, YangNode curNode, List<String> imports) throws IOException {
JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
......@@ -414,19 +420,104 @@ public final class JavaFileGenerator {
methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles()
.addDefaultConstructor(PRIVATE, EMPTY_STRING));
try {
/**
* Type constructor.
*/
methods.add(getDataFromTempFileHandle(CONSTRUCTOR_FOR_TYPE_MASK, curNode));
/**
* Of method.
*/
methods.add(getDataFromTempFileHandle(OF_STRING_IMPL_MASK, curNode));
/**
* Getter method.
*/
methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode));
/**
* Hash code method.
*/
methods.add(getHashCodeMethodClose(getHashCodeMethodOpen() + partString(
getDataFromTempFileHandle(HASH_CODE_IMPL_MASK, curNode).replace(NEW_LINE, EMPTY_STRING))));
/**
* Equals method.
*/
methods.add(getEqualsMethodClose(getEqualsMethodOpen(className + EMPTY_STRING)
+ getDataFromTempFileHandle(EQUALS_IMPL_MASK, curNode)));
/**
* To string method.
*/
methods.add(getToStringMethodOpen() + getDataFromTempFileHandle(TO_STRING_IMPL_MASK, curNode)
+ getToStringMethodClose());
} catch (IOException e) {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while type def class file generation");
}
for (String method : methods) {
insertDataIntoJavaFile(file, method);
}
insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE);
return file;
}
/**
* Generate class file for union type.
*
* @param file generated file
* @param curNode current YANG node
* @param imports imports for file
* @return type def class file
* @throws IOException when fails to generate class file
*/
public static File generateUnionClassFile(File file, YangNode curNode, List<String> imports) throws IOException {
JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
String className = getCaptialCase(javaFileInfo.getJavaName());
String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
initiateJavaFileGeneration(file, className, GENERATE_UNION_CLASS, imports, path);
List<String> methods = new ArrayList<>();
/**
* Constructor.
* Add attribute strings.
*/
methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles()
.addTypeDefConstructor());
try {
insertDataIntoJavaFile(file,
NEW_LINE + FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(ATTRIBUTES_MASK, curNode));
} catch (IOException e) {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while union class file generation");
}
/**
* Of method.
* Default constructor.
*/
methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles().addOfMethod());
methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles()
.addDefaultConstructor(PRIVATE, EMPTY_STRING));
try {
/**
* Type constructor.
*/
methods.add(getDataFromTempFileHandle(CONSTRUCTOR_FOR_TYPE_MASK, curNode));
/**
* Of string method.
*/
methods.add(getDataFromTempFileHandle(OF_STRING_IMPL_MASK, curNode));
/**
* Getter method.
*/
methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode));
......@@ -446,12 +537,19 @@ public final class JavaFileGenerator {
/**
* To string method.
*/
methods.add(getToStringMethodOpen() + getDataFromTempFileHandle(TO_STRING_IMPL_MASK, curNode)
+ getToStringMethodClose());
methods.add(getToStringMethodOpen() + getOmitNullValueString() +
getDataFromTempFileHandle(TO_STRING_IMPL_MASK, curNode) + getToStringMethodClose());
/**
* From string method.
*/
methods.add(getFromStringMethodSignature(className)
+ getDataFromTempFileHandle(UNION_FROM_STRING_IMPL_MASK, curNode)
+ getFromStringMethodClose());
} catch (IOException e) {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while type def class file generation");
+ " while union class file generation");
}
for (String method : methods) {
......
......@@ -19,7 +19,6 @@ package org.onosproject.yangutils.translator.tojava.utils;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
......@@ -30,17 +29,21 @@ import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.UNION_FROM_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefStart;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getJavaPackageFromPackagePath;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
......@@ -49,11 +52,11 @@ import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_CLASS;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_INTERFACE;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.IMPL_CLASS;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.INTERFACE;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
/**
......@@ -70,10 +73,10 @@ public final class JavaFileGeneratorUtils {
/**
* Returns a file object for generated file.
*
* @param fileName file name
* @param filePath file package path
* @param fileName file name
* @param filePath file package path
* @param extension file extension
* @param handle cached file handle
* @param handle cached file handle
* @return file object
*/
public static File getFileObject(String filePath, String fileName, String extension, JavaFileInfo handle) {
......@@ -85,7 +88,7 @@ public final class JavaFileGeneratorUtils {
* Returns data stored in temporary files.
*
* @param generatedTempFiles temporary file types
* @param curNode current YANG node
* @param curNode current YANG node
* @return data stored in temporary files
* @throws IOException when failed to get the data from temporary file handle
*/
......@@ -121,6 +124,16 @@ public final class JavaFileGeneratorUtils {
} else if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getToStringImplTempFileHandle());
} else if ((generatedTempFiles & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles
.getConstructorForTypeTempFileHandle());
} else if ((generatedTempFiles & OF_STRING_IMPL_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getOfStringImplTempFileHandle());
} else if ((generatedTempFiles & UNION_FROM_STRING_IMPL_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getUnionFromStringImplTempFileHandle());
}
return null;
}
......@@ -128,15 +141,15 @@ public final class JavaFileGeneratorUtils {
/**
* Initiates generation of file based on generated file type.
*
* @param file generated file
* @param file generated file
* @param className generated file class name
* @param type generated file type
* @param imports imports for the file
* @param pkg generated file package
* @param type generated file type
* @param imports imports for the file
* @param pkg generated file package
* @throws IOException when fails to generate a file
*/
public static void initiateJavaFileGeneration(File file, String className, int type, List<String> imports,
String pkg) throws IOException {
String pkg) throws IOException {
try {
file.createNewFile();
......@@ -149,42 +162,41 @@ public final class JavaFileGeneratorUtils {
/**
* Appends all the contents into a generated java file.
*
* @param file generated file
* @param fileName generated file name
* @param type generated file type
* @param pkg generated file package
* @param file generated file
* @param fileName generated file name
* @param type generated file type
* @param pkg generated file package
* @param importsList list of java imports.
* @throws IOException when fails to append contents
*/
private static void appendContents(File file, String fileName, int type, List<String> importsList,
String pkg) throws IOException {
String pkg) throws IOException {
String pkgString = parsePackageString(pkg, importsList);
if ((type & IMPL_CLASS_MASK) != 0) {
write(file, fileName, type, IMPL_CLASS);
} else if ((type & BUILDER_INTERFACE_MASK) != 0) {
write(file, fileName, type, BUILDER_INTERFACE);
} else if ((type & GENERATE_TYPEDEF_CLASS) != 0) {
appendHeaderContents(file, pkgString, importsList);
write(file, fileName, type, IMPL_CLASS);
} else if ((type & INTERFACE_MASK) != 0) {
appendHeaderContents(file, pkgString, importsList);
write(file, fileName, type, INTERFACE);
} else if ((type & BUILDER_CLASS_MASK) != 0) {
appendHeaderContents(file, pkgString, importsList);
write(file, fileName, type, BUILDER_CLASS);
} else if ((type & GENERATE_UNION_CLASS) != 0) {
appendHeaderContents(file, pkgString, importsList);
write(file, fileName, type, IMPL_CLASS);
}
}
/**
* Removes base directory path from package and generates package string for file.
*
* @param javaPkg generated java package
* @param javaPkg generated java package
* @param importsList list of imports
* @return package string
*/
......@@ -208,10 +220,11 @@ public final class JavaFileGeneratorUtils {
/**
* Appends other contents to interface, builder and typedef classes.
* for example : ONOS copyright, imports and package.
* @param file generated file
* @param pkg generated package
*
* @param file generated file
* @param pkg generated package
* @param importsList list of imports
* @throws IOException when fails to append contents.
* @throws IOException when fails to append contents
*/
private static void appendHeaderContents(File file, String pkg, List<String> importsList) throws IOException {
......@@ -234,15 +247,14 @@ public final class JavaFileGeneratorUtils {
/**
* Writes data to the specific generated file.
*
* @param file generated file
* @param fileName file name
* @param genType generated file type
* @param file generated file
* @param fileName file name
* @param genType generated file type
* @param javaDocType java doc type
* @throws IOException when fails to write into a file
*/
private static void write(File file, String fileName, int genType, JavaDocType javaDocType)
throws IOException {
insertDataIntoJavaFile(file, getJavaDoc(javaDocType, fileName, false));
insertDataIntoJavaFile(file, getJavaClassDefStart(genType, fileName));
}
......
......@@ -18,6 +18,7 @@ package org.onosproject.yangutils.translator.tojava.utils;
import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getParseFromStringMethod;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
......@@ -28,7 +29,9 @@ import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_DATA_TYPE;
import static org.onosproject.yangutils.utils.UtilConstants.BUILD;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
import static org.onosproject.yangutils.utils.UtilConstants.CATCH;
import static org.onosproject.yangutils.utils.UtilConstants.CHECK_NOT_NULL_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.CLEAR;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS;
import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
......@@ -38,8 +41,12 @@ import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTAT
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.EQUAL;
import static org.onosproject.yangutils.utils.UtilConstants.EQUALS_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.EXCEPTION;
import static org.onosproject.yangutils.utils.UtilConstants.EXCEPTION_VAR;
import static org.onosproject.yangutils.utils.UtilConstants.FALSE;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.FROM_STRING_METHOD_NAME;
import static org.onosproject.yangutils.utils.UtilConstants.FROM_STRING_PARAM_NAME;
import static org.onosproject.yangutils.utils.UtilConstants.GET_METHOD_PREFIX;
import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_METHOD_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.HASH;
......@@ -51,10 +58,12 @@ import static org.onosproject.yangutils.utils.UtilConstants.INT;
import static org.onosproject.yangutils.utils.UtilConstants.LIST;
import static org.onosproject.yangutils.utils.UtilConstants.NEW;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.NULL;
import static org.onosproject.yangutils.utils.UtilConstants.OBJ;
import static org.onosproject.yangutils.utils.UtilConstants.OBJECT;
import static org.onosproject.yangutils.utils.UtilConstants.OBJECT_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.OF;
import static org.onosproject.yangutils.utils.UtilConstants.OMIT_NULL_VALUE_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS;
import static org.onosproject.yangutils.utils.UtilConstants.OTHER;
......@@ -71,17 +80,22 @@ import static org.onosproject.yangutils.utils.UtilConstants.STATIC;
import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
import static org.onosproject.yangutils.utils.UtilConstants.SUFFIX_S;
import static org.onosproject.yangutils.utils.UtilConstants.THIS;
import static org.onosproject.yangutils.utils.UtilConstants.TMP_VAL;
import static org.onosproject.yangutils.utils.UtilConstants.TO;
import static org.onosproject.yangutils.utils.UtilConstants.TRUE;
import static org.onosproject.yangutils.utils.UtilConstants.TRY;
import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.VALUE;
import static org.onosproject.yangutils.utils.UtilConstants.VOID;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILD_METHOD;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.CONSTRUCTOR;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.SETTER_METHOD;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_CONSTRUCTOR;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.UNION_FROM_METHOD;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
/**
......@@ -123,7 +137,7 @@ public final class MethodsGenerator {
/**
* Returns setter string.
*
* @param attr attribute info
* @param attr attribute info
* @param className java class name
* @return setter string
*/
......@@ -149,46 +163,13 @@ public final class MethodsGenerator {
/**
* Returns default constructor method string.
*
* @param name class name
* @param name class name
* @param modifierType modifier type
* @return default constructor string
*/
public static String getDefaultConstructorString(String name, String modifierType) {
return getJavaDoc(DEFAULT_CONSTRUCTOR, name, false) + getDefaultConstructor(name, modifierType);
}
/**
* Returns default constructor method string.
*
* @param attr attribute info
* @param className class name
* @return default constructor string
*/
public static String getTypeDefConstructor(JavaAttributeInfo attr, String className) {
String attrQuaifiedType = getReturnType(attr);
String attributeName = getSmallCase(attr.getAttributeName());
if (!attr.isListAttr()) {
return getTypeDefConstructorString(attrQuaifiedType, attributeName, className);
}
String listAttr = getListString() + attrQuaifiedType + DIAMOND_CLOSE_BRACKET;
return getTypeDefConstructorString(listAttr, attributeName, className);
}
/**
* Returns type def's constructor for attribute.
*
* @param type data type
* @param name attribute name
* @param className class name
* @return setter for type def's attribute
*/
private static String getTypeDefConstructorString(String type, String name, String className) {
return FOUR_SPACE_INDENTATION + PUBLIC + SPACE + className + OPEN_PARENTHESIS + type + SPACE + VALUE
+ CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION + THIS + PERIOD
+ name + SPACE + EQUAL + SPACE + VALUE + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION
+ CLOSE_CURLY_BRACKET;
return getJavaDoc(DEFAULT_CONSTRUCTOR, name, false) + getDefaultConstructor(name, modifierType)
+ NEW_LINE;
}
/**
......@@ -246,7 +227,7 @@ public final class MethodsGenerator {
/**
* Returns the setter method strings for class file.
*
* @param attr attribute info
* @param attr attribute info
* @param className name of the class
* @return setter method for class
*/
......@@ -265,8 +246,8 @@ public final class MethodsGenerator {
* Returns setter for attribute.
*
* @param className class name
* @param name attribute name
* @param type return type
* @param name attribute name
* @param type return type
* @return setter for attribute
*/
private static String getSetter(String className, String name, String type) {
......@@ -316,9 +297,9 @@ public final class MethodsGenerator {
/**
* Returns the getter method strings for interface file.
*
* @param yangName name of the attribute
* @param yangName name of the attribute
* @param returnType return type of attribute
* @param isList is list attribute
* @param isList is list attribute
* @return getter method for interface
*/
public static String getGetterForInterface(String yangName, String returnType, boolean isList) {
......@@ -334,7 +315,7 @@ public final class MethodsGenerator {
* Returns getter for attribute in interface.
*
* @param returnType return type
* @param yangName attribute name
* @param yangName attribute name
* @return getter for interface
*/
private static String getGetterInterfaceString(String returnType, String yangName) {
......@@ -345,10 +326,10 @@ public final class MethodsGenerator {
/**
* Returns the setter method strings for interface file.
*
* @param attrName name of the attribute
* @param attrType return type of attribute
* @param attrName name of the attribute
* @param attrType return type of attribute
* @param className name of the java class being generated
* @param isList is list attribute
* @param isList is list attribute
* @return setter method for interface
*/
public static String getSetterForInterface(String attrName, String attrType, String className, boolean isList) {
......@@ -364,8 +345,8 @@ public final class MethodsGenerator {
* Returns setter string for interface.
*
* @param className class name
* @param attrName attribute name
* @param attrType attribute type
* @param attrName attribute name
* @param attrType attribute type
* @return setter string
*/
private static String getSetterInterfaceString(String className, String attrName, String attrType) {
......@@ -428,7 +409,7 @@ public final class MethodsGenerator {
* Returns the constructor strings for class file.
*
* @param yangName name of the class
* @param attr attribute info
* @param attr attribute info
* @return constructor for class
*/
public static String getConstructor(String yangName, JavaAttributeInfo attr) {
......@@ -459,7 +440,7 @@ public final class MethodsGenerator {
/**
* Returns the Default constructor strings for class file.
*
* @param name name of the class
* @param name name of the class
* @param modifierType modifier type for default constructor
* @return Default constructor for class
*/
......@@ -469,9 +450,9 @@ public final class MethodsGenerator {
}
/**
* Returns to string method open strings.
* Returns to string method's open strings.
*
* @return to string method open string
* @return string method's open string
*/
public static String getToStringMethodOpen() {
return getOverRideString() + FOUR_SPACE_INDENTATION + PUBLIC + SPACE + STRING_DATA_TYPE + SPACE + TO
......@@ -480,7 +461,16 @@ public final class MethodsGenerator {
}
/**
* Returns to string methods close string.
* Returns omit null value string.
*
* @return omit null value string
*/
public static String getOmitNullValueString() {
return TWELVE_SPACE_INDENTATION + PERIOD + OMIT_NULL_VALUE_STRING + NEW_LINE;
}
/**
* Returns to string method's close string.
*
* @return to string method close string
*/
......@@ -503,6 +493,90 @@ public final class MethodsGenerator {
}
/**
* Returns from string method's open string.
*
* @param className name of the class
* @return from string method's open string
*/
public static String getFromStringMethodSignature(String className) {
return getJavaDoc(UNION_FROM_METHOD, className, false) + FOUR_SPACE_INDENTATION + PUBLIC + SPACE
+ className + SPACE + FROM_STRING_METHOD_NAME + OPEN_PARENTHESIS + STRING_DATA_TYPE + SPACE
+ FROM_STRING_PARAM_NAME + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
/**
* Return from string method's close string.
*
* @return from string method's close string
*/
public static String getFromStringMethodClose() {
return EIGHT_SPACE_INDENTATION + RETURN + SPACE + NULL + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION +
CLOSE_CURLY_BRACKET + NEW_LINE;
}
/**
* Return from string method's body string.
*
* @param attr attribute info
* @param fromStringAttributeInfo attribute info for the from string
* wrapper type
* @return from string method's body string
*/
public static String getFromStringMethod(JavaAttributeInfo attr,
JavaAttributeInfo fromStringAttributeInfo) {
return EIGHT_SPACE_INDENTATION + getTrySubString() + NEW_LINE + TWELVE_SPACE_INDENTATION
+ getParsedSubString(attr, fromStringAttributeInfo) + SEMI_COLAN + NEW_LINE + TWELVE_SPACE_INDENTATION
+ getReturnOfSubString() + NEW_LINE + EIGHT_SPACE_INDENTATION + getCatchSubString()
+ NEW_LINE + EIGHT_SPACE_INDENTATION + CLOSE_CURLY_BRACKET;
}
/**
* Returns sub string with try statement for union's from string method.
*
* @return sub string with try statement for union's from string method
*/
public static String getTrySubString() {
return TRY + SPACE + OPEN_CURLY_BRACKET;
}
/**
* Returns sub string with return statement for union's from string method.
*
* @return sub string with return statement for union's from string method
*/
public static String getReturnOfSubString() {
return RETURN + SPACE + OF + OPEN_PARENTHESIS + TMP_VAL + CLOSE_PARENTHESIS + SEMI_COLAN;
}
/**
* Returns sub string with catch statement for union's from string method.
*
* @return sub string with catch statement for union's from string method
*/
public static String getCatchSubString() {
return CLOSE_CURLY_BRACKET + SPACE + CATCH + SPACE + OPEN_PARENTHESIS + EXCEPTION + SPACE + EXCEPTION_VAR
+ CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET;
}
/**
* Returns sub string with parsed statement for union's from string method.
*
* @param attr attribute info
* @return sub string with parsed statement for union's from string method
*/
private static String getParsedSubString(JavaAttributeInfo attr,
JavaAttributeInfo fromStringAttributeInfo) {
String targetDataType = getReturnType(attr);
String parseFromStringMethod = getParseFromStringMethod(targetDataType,
fromStringAttributeInfo.getAttributeType());
return targetDataType + SPACE + TMP_VAL + SPACE + EQUAL + SPACE + parseFromStringMethod
+ OPEN_PARENTHESIS + FROM_STRING_PARAM_NAME + CLOSE_PARENTHESIS;
}
/**
* Returns hash code method open strings.
*
* @return hash code method open string
......@@ -611,9 +685,10 @@ public final class MethodsGenerator {
*/
public static String getOfMethod(String name, JavaAttributeInfo attr) {
String attrQuaifiedType = getReturnType(attr);
String attrQualifiedType = getReturnType(attr);
return FOUR_SPACE_INDENTATION + PUBLIC + SPACE + STATIC + SPACE + name + SPACE + OF + OPEN_PARENTHESIS
+ attrQuaifiedType + SPACE + VALUE + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE
+ attrQualifiedType + SPACE + VALUE + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE
+ EIGHT_SPACE_INDENTATION + RETURN + SPACE + NEW + SPACE + name + OPEN_PARENTHESIS + VALUE
+ CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET + NEW_LINE;
}
......@@ -635,6 +710,69 @@ public final class MethodsGenerator {
}
/**
* Returns of method's string and java doc for special type.
*
* @param attr attribute info
* @param generatedJavaClassName class name
* @return of method's string and java doc for special type
*/
public static String getOfMethodStringAndJavaDoc(JavaAttributeInfo attr, String generatedJavaClassName) {
String attrType = getReturnType(attr);
String attrName = getSmallCase(attr.getAttributeName());
return getJavaDoc(OF_METHOD, generatedJavaClassName + " for type " + attrName, false)
+ getOfMethodString(attrType, generatedJavaClassName);
}
/**
* Returns of method's string.
*
* @param type data type
* @param className class name
* @return of method's string
*/
private static String getOfMethodString(String type, String className) {
return FOUR_SPACE_INDENTATION + PUBLIC + SPACE + STATIC + SPACE + className + SPACE + OF + OPEN_PARENTHESIS
+ type + SPACE + VALUE + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE
+ EIGHT_SPACE_INDENTATION + RETURN + SPACE + NEW + SPACE + className + OPEN_PARENTHESIS + VALUE
+ CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET;
}
/**
* Returns string and java doc for constructor of type class.
*
* @param attr attribute info
* @param generatedJavaClassName class name
* @return string and java doc for constructor of type class
*/
public static String getTypeConstructorStringAndJavaDoc(JavaAttributeInfo attr, String generatedJavaClassName) {
String attrType = getReturnType(attr);
String attrName = getSmallCase(attr.getAttributeName());
return getJavaDoc(TYPE_CONSTRUCTOR, generatedJavaClassName + " for type " + attrName, false)
+ getTypeConstructorString(attrType, attrName, generatedJavaClassName);
}
/**
* Returns type constructor string.
*
* @param type data type
* @param name attribute name
* @param className class name
* @return type constructor string
*/
private static String getTypeConstructorString(String type, String name, String className) {
return FOUR_SPACE_INDENTATION + PUBLIC + SPACE + className + OPEN_PARENTHESIS + type + SPACE + VALUE
+ CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION + THIS + PERIOD
+ name + SPACE + EQUAL + SPACE + VALUE + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION
+ CLOSE_CURLY_BRACKET;
}
/**
* Returns implementation of get augment info list method of HasAugmentation class.
*
* @return implementation of get augment info list method of HasAugmentation class
......@@ -660,7 +798,7 @@ public final class MethodsGenerator {
method = method + getOverRideString() + FOUR_SPACE_INDENTATION + PUBLIC + SPACE + VOID + SPACE + "remove"
+ AUGMENTATION + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE
+ EIGHT_SPACE_INDENTATION + GET_METHOD_PREFIX + AUGMENTED_INFO + LIST + OPEN_PARENTHESIS
+ CLOSE_PARENTHESIS + PERIOD + "clear" + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE
+ CLOSE_PARENTHESIS + PERIOD + CLEAR + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE
+ FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET;
return method;
}
......
......@@ -17,7 +17,7 @@
package org.onosproject.yangutils.translator.tojava.utils;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.HasType;
import org.onosproject.yangutils.datamodel.YangAugment;
import org.onosproject.yangutils.datamodel.YangCase;
import org.onosproject.yangutils.datamodel.YangChoice;
......@@ -54,7 +54,7 @@ public final class YangJavaModelUtils {
* Updates YANG java file package information.
*
* @param javaCodeGeneratorInfo YANG java file info node
* @param yangPlugin YANG plugin config
* @param yangPlugin YANG plugin config
* @throws IOException IO operations fails
*/
private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin)
......@@ -72,11 +72,11 @@ public final class YangJavaModelUtils {
* Updates YANG java file package information for specified package.
*
* @param javaCodeGeneratorInfo YANG java file info node
* @param yangPlugin YANG plugin config
* @param yangPlugin YANG plugin config
* @throws IOException IO operations fails
*/
private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
String pkg)
String pkg)
throws IOException {
javaCodeGeneratorInfo.getJavaFileInfo()
.setJavaName(getCaptialCase(
......@@ -113,9 +113,11 @@ public final class YangJavaModelUtils {
if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
.addCurNodeLeavesInfoToTempFiles((YangNode) javaCodeGeneratorInfo);
} else if (javaCodeGeneratorInfo instanceof HasType) {
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
.addTypeInfoToTempFiles((HasType) javaCodeGeneratorInfo);
} else {
// TODO: either write a util for ENUM and UNION or, call the
// corresponding implementation in ENUM and UNION
//TODO throw exception
}
}
......@@ -123,7 +125,7 @@ public final class YangJavaModelUtils {
* Process generate code entry of YANG node.
*
* @param javaCodeGeneratorInfo YANG java file info node
* @param codeGenDir code generation directory
* @param codeGenDir code generation directory
* @throws IOException IO operations fails
*/
private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo, String codeGenDir)
......@@ -140,12 +142,12 @@ public final class YangJavaModelUtils {
* Process generate code entry of YANG node.
*
* @param javaCodeGeneratorInfo YANG java file info node
* @param yangPlugin YANG plugin config
* @param isMultiInstance flag to indicate whether it's a list
* @param yangPlugin YANG plugin config
* @param isMultiInstance flag to indicate whether it's a list
* @throws IOException IO operations fails
*/
public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
boolean isMultiInstance) throws IOException {
boolean isMultiInstance) throws IOException {
if (!(javaCodeGeneratorInfo instanceof YangNode)) {
// TODO:throw exception
}
......@@ -172,15 +174,32 @@ public final class YangJavaModelUtils {
}
/**
* Process generate code entry of YANG type.
*
* @param javaCodeGeneratorInfo YANG java file info node
* @param yangPlugin YANG plugin config
* @param isMultiInstance flag to indicate whether it's a list
* @throws IOException IO operations fails
*/
public static void generateCodeOfType(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
boolean isMultiInstance) throws IOException {
if (!(javaCodeGeneratorInfo instanceof YangNode)) {
// TODO:throw exception
}
updatePackageInfo(javaCodeGeneratorInfo, yangPlugin);
generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
}
/**
* Process generate code entry of root node.
*
* @param javaCodeGeneratorInfo YANG java file info node
* @param yangPlugin YANG plugin config
* @param rootPkg package of the root node
* @param yangPlugin YANG plugin config
* @param rootPkg package of the root node
* @throws IOException IO operations fails
*/
public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
String rootPkg) throws IOException {
String rootPkg) throws IOException {
if (!(javaCodeGeneratorInfo instanceof YangNode)) {
// TODO:throw exception
}
......
......@@ -34,18 +34,18 @@ public final class UtilConstants {
/**
* JavaDocs for impl class.
*/
public static final String IMPL_CLASS_JAVA_DOC = " * Reperesents the implementation of ";
public static final String IMPL_CLASS_JAVA_DOC = " * Represents the implementation of ";
/**
* JavaDocs for builder class.
*/
public static final String BUILDER_CLASS_JAVA_DOC = " * Reperesents the builder implementation of ";
public static final String BUILDER_CLASS_JAVA_DOC = " * Represents the builder implementation of ";
/**
* JavaDocs for interface class.
*/
public static final String INTERFACE_JAVA_DOC = " * Abstraction of an entity which Reperesents the"
+ " functionalities of ";
public static final String INTERFACE_JAVA_DOC = " * Abstraction of an entity which represents the"
+ " functionality of ";
/**
* JavaDocs for builder interface class.
......@@ -148,6 +148,31 @@ public final class UtilConstants {
public static final String PERIOD = ".";
/**
* Static attribute for parse byte.
*/
public static final String PARSE_BYTE = "parseByte";
/**
* Static attribute for parse short.
*/
public static final String PARSE_SHORT = "parseShort";
/**
* Static attribute for parse int.
*/
public static final String PARSE_INT = "parseInt";
/**
* Static attribute for parse long.
*/
public static final String PARSE_LONG = "parseLong";
/**
* Static attribute for omit null value.
*/
public static final String OMIT_NULL_VALUE_STRING = "omitNullValues()";
/**
* Static attribute for colan.
*/
public static final String COLAN = ":";
......@@ -173,9 +198,9 @@ public final class UtilConstants {
public static final String SPACE = " ";
/**
* Static attribute for tab.
* Static attribute for input string.
*/
public static final String TAB = "\t";
public static final String INPUT = "input";
/**
* Static attribute for new line.
......@@ -223,6 +248,11 @@ public final class UtilConstants {
public static final String ADD_STRING = "add";
/**
* Static attribute for from syntax.
*/
public static final String FROM_STRING_METHOD_NAME = "fromString";
/**
* Static attribute for check not null syntax.
*/
public static final String CHECK_NOT_NULL_STRING = "checkNotNull";
......@@ -333,14 +363,14 @@ public final class UtilConstants {
public static final String DIAMOND_CLOSE_BRACKET = ">";
/**
* Static attribute for square open bracket syntax.
* Static attribute for exception syntax.
*/
public static final String SQUARE_OPEN_BRACKET = "[";
public static final String EXCEPTION = "Exception";
/**
* Static attribute for square close bracket syntax.
* Static attribute for exception variable syntax.
*/
public static final String SQUARE_CLOSE_BRACKET = "]";
public static final String EXCEPTION_VAR = "e";
/**
* Static attribute for open parenthesis syntax.
......@@ -348,6 +378,21 @@ public final class UtilConstants {
public static final String OPEN_PARENTHESIS = "(";
/**
* Static attribute for clear syntax.
*/
public static final String CLEAR = "clear";
/**
* Static attribute for temp val syntax.
*/
public static final String TMP_VAL = "tmpVal";
/**
* From string parameter name.
*/
public static final String FROM_STRING_PARAM_NAME = "valInString";
/**
* Static attribute for close parenthesis syntax.
*/
public static final String CLOSE_PARENTHESIS = ")";
......@@ -378,6 +423,21 @@ public final class UtilConstants {
public static final String FOUR_SPACE_INDENTATION = " ";
/**
* Static attribute for not syntax.
*/
public static final String NOT = "!";
/**
* Static attribute for try syntax.
*/
public static final String TRY = "try";
/**
* Static attribute for catch syntax.
*/
public static final String CATCH = "catch";
/**
* Static attribute for eight space indentation.
*/
public static final String EIGHT_SPACE_INDENTATION = FOUR_SPACE_INDENTATION + FOUR_SPACE_INDENTATION;
......@@ -518,14 +578,9 @@ public final class UtilConstants {
public static final String LONG_WRAPPER = "Long";
/**
* Float java built in wrapper type.
*/
public static final String FLOAT_WRAPPER = "Float";
/**
* Double java built in wrapper type.
* YangUint64 java built in wrapper type.
*/
public static final String DOUBLE_WRAPPER = "Double";
public static final String YANG_UINT64 = "YangUint64";
/**
* List of keywords in java, this is used for checking if the input does not contain these keywords.
......@@ -733,92 +788,12 @@ public final class UtilConstants {
public static final String AUGMENTED_INFO = "AugmentedInfo";
/**
* Static attribute for abstract collection.
*/
public static final String ABSTRACT_COLLECTION = "AbstractCollection";
/**
* Static attribute for list.
*/
public static final String LIST = "List";
/**
* Static attribute for linked list.
*/
public static final String LINKED_LIST = "LinkedList";
/**
* Static attribute for array list.
*/
public static final String ARRAY_LIST = "ArrayList";
/**
* Static attribute for abstract list.
*/
public static final String ABSTRACT_LIST = "AbstractList";
/**
* Static attribute for abstract sequential list.
*/
public static final String ABSTRACT_SEQUENTAIL_LIST = "AbstractSequentialList";
/**
* Static attribute for set.
*/
public static final String SET = "Set";
/**
* Static attribute for hash set.
*/
public static final String HASH_SET = "HashSet";
/**
* Static attribute for abstract set.
*/
public static final String ABSTRACT_SET = "AbstractSet";
/**
* Static attribute for linked has set.
*/
public static final String LINKED_HASH_SET = "LinkedHashSet";
/**
* Static attribute for tree set.
*/
public static final String TREE_SET = "TreeSet";
/**
* Static attribute for map.
*/
public static final String MAP = "Map";
/**
* Static attribute for abstract map.
*/
public static final String ABSTRACT_MAP = "AbstractMap";
/**
* Static attribute for hash map.
*/
public static final String HASH_MAP = "HashMap";
/**
* Static attribute for tree map.
*/
public static final String TREE_MAP = "TreeMap";
/**
* Static attribute for concurrent map.
*/
public static final String CONCURRENT_MAP = "ConcurrentMap";
/**
* Static attribute for eventually consistent map.
*/
public static final String EVENTUALLY_CONSISTENT_MAP = "EventuallyConsitentMap";
/**
* Static attribute for stack syntax.
*/
public static final String STACK = "stack";
}
......
......@@ -23,8 +23,11 @@ import static org.onosproject.yangutils.utils.UtilConstants.BUILDER_CLASS_JAVA_D
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER_INTERFACE_JAVA_DOC;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER_OBJECT;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.FROM_STRING_METHOD_NAME;
import static org.onosproject.yangutils.utils.UtilConstants.FROM_STRING_PARAM_NAME;
import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
import static org.onosproject.yangutils.utils.UtilConstants.IMPL_CLASS_JAVA_DOC;
import static org.onosproject.yangutils.utils.UtilConstants.INPUT;
import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE_JAVA_DOC;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_BUILD;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_BUILD_RETURN;
......@@ -45,6 +48,7 @@ import static org.onosproject.yangutils.utils.UtilConstants.OF;
import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE_INFO_JAVADOC;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
import static org.onosproject.yangutils.utils.UtilConstants.VALUE;
/**
......@@ -104,11 +108,6 @@ public final class JavaDocGen {
TYPE_DEF_SETTER_METHOD,
/**
* For type def's constructor.
*/
TYPE_DEF_CONSTRUCTOR,
/**
* For of method.
*/
OF_METHOD,
......@@ -124,6 +123,16 @@ public final class JavaDocGen {
CONSTRUCTOR,
/**
* For union's from method.
*/
UNION_FROM_METHOD,
/**
* For type constructor.
*/
TYPE_CONSTRUCTOR,
/**
* For build.
*/
BUILD_METHOD
......@@ -132,8 +141,8 @@ public final class JavaDocGen {
/**
* Returns java docs.
*
* @param type java doc type
* @param name name of the YangNode
* @param type java doc type
* @param name name of the YangNode
* @param isList is list attribute
* @return javadocs.
*/
......@@ -155,8 +164,6 @@ public final class JavaDocGen {
javaDoc = generateForGetters(name, isList);
} else if (type.equals(JavaDocType.TYPE_DEF_SETTER_METHOD)) {
javaDoc = generateForTypeDefSetter(name);
} else if (type.equals(JavaDocType.TYPE_DEF_CONSTRUCTOR)) {
javaDoc = generateForTypeDefConstructor(name);
} else if (type.equals(JavaDocType.SETTER_METHOD)) {
javaDoc = generateForSetters(name, isList);
} else if (type.equals(JavaDocType.OF_METHOD)) {
......@@ -165,6 +172,10 @@ public final class JavaDocGen {
javaDoc = generateForDefaultConstructors(name);
} else if (type.equals(JavaDocType.BUILD_METHOD)) {
javaDoc = generateForBuild(name);
} else if (type.equals(JavaDocType.TYPE_CONSTRUCTOR)) {
javaDoc = generateForTypeConstructor(name);
} else if (type.equals(JavaDocType.UNION_FROM_METHOD)) {
javaDoc = generateForUnionFrom(name);
} else {
javaDoc = generateForConstructors(name);
}
......@@ -175,7 +186,7 @@ public final class JavaDocGen {
* Generate javaDocs for getter method.
*
* @param attribute attribute
* @param isList is list attribute
* @param isList is list attribute
* @return javaDocs
*/
private static String generateForGetters(String attribute, boolean isList) {
......@@ -198,7 +209,7 @@ public final class JavaDocGen {
* Generates javaDocs for setter method.
*
* @param attribute attribute
* @param isList is list attribute
* @param isList is list attribute
* @return javaDocs
*/
private static String generateForSetters(String attribute, boolean isList) {
......@@ -232,6 +243,22 @@ public final class JavaDocGen {
}
/**
* Generates javaDocs for from method.
*
* @param attribute attribute
* @return javaDocs
*/
private static String generateForUnionFrom(String attribute) {
return NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_OF
+ attribute + SPACE + FROM_STRING_METHOD_NAME + SPACE + INPUT + SPACE + STRING_DATA_TYPE + PERIOD
+ NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ASTERISK + FOUR_SPACE_INDENTATION + JAVA_DOC_PARAM
+ FROM_STRING_PARAM_NAME + SPACE + INPUT + SPACE + STRING_DATA_TYPE + PERIOD + NEW_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_RETURN + OBJECT + SPACE + OF + SPACE + attribute + NEW_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
}
/**
* Generates javaDocs for typedef setter method.
*
* @param attribute attribute
......@@ -347,4 +374,18 @@ public final class JavaDocGen {
+ JAVA_DOC_RETURN + JAVA_DOC_BUILD_RETURN + buildName + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION
+ JAVA_DOC_END_LINE;
}
/**
* Generates javaDocs for type constructor.
*
* @param attribute attribute string
* @return javaDocs for type constructor
*/
private static String generateForTypeConstructor(String attribute) {
return (NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_CONSTRUCTOR
+ attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ASTERISK + FOUR_SPACE_INDENTATION
+ JAVA_DOC_PARAM + VALUE + SPACE + VALUE + SPACE + OF + SPACE + attribute + NEW_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE);
}
}
......
......@@ -43,7 +43,7 @@ import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForInterface;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForTypeDefClass;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeDefConstructor;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
import static org.onosproject.yangutils.utils.UtilConstants.ADD_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.BUILD;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
......@@ -113,12 +113,12 @@ public final class MethodsGeneratorTest {
}
/**
* Unit test case for checking the parse builder and typedef constructor.
* Unit test case for checking the parse builder and type constructor.
*/
@Test
public void getTypeDefConstructorTest() {
public void getTypeConstructorTest() {
JavaAttributeInfo testAttr = getTestAttribute();
String test = getTypeDefConstructor(testAttr, CLASS_NAME);
String test = getTypeConstructorStringAndJavaDoc(testAttr, CLASS_NAME);
assertThat(true, is(test.contains(PUBLIC + SPACE + CLASS_NAME + OPEN_PARENTHESIS)));
}
......@@ -178,7 +178,7 @@ public final class MethodsGeneratorTest {
}
/**
* Test case for quals method.
* Test case for equals method.
*/
@Test
public void getEqualsMethodTest() {
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.yangutils.translator.tojava.utils;
import java.io.IOException;
import org.junit.Test;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
/**
* Unit tests for union translator.
*/
public final class UnionTranslatorTest {
private final YangUtilsParserManager manager = new YangUtilsParserManager();
/**
* Checks union translation should not result in any exception.
*/
@Test
public void processUnionTranslator() throws IOException, ParserException {
clean("src/test/org/onosproject/yang");
YangNode node = manager.getDataModel("src/test/resources/UnionTranslator.yang");
YangPluginConfig yangPluginConfig = new YangPluginConfig();
yangPluginConfig.setCodeGenDir("target/UnionTestGenFile");
generateJavaCode(node, yangPluginConfig);
clean("target/UnionTestGenFile");
}
// TODO enhance the test cases, after having a framework of translator test.
}
......@@ -56,7 +56,7 @@ public final class JavaDocGenTest {
@Test
public void builderClassGenerationTest() {
String builderClassJavaDoc = getJavaDoc(BUILDER_CLASS, TEST_NAME, false);
assertThat(true, is(builderClassJavaDoc.contains("Reperesents the builder implementation of")
assertThat(true, is(builderClassJavaDoc.contains("Represents the builder implementation of")
&& builderClassJavaDoc.contains(END_STRING)));
}
......@@ -138,7 +138,7 @@ public final class JavaDocGenTest {
public void implClassGenerationTest() {
String implClassJavaDoc = getJavaDoc(IMPL_CLASS, TEST_NAME, false);
assertThat(true,
is(implClassJavaDoc.contains("Reperesents the implementation of")
is(implClassJavaDoc.contains("Represents the implementation of")
&& implClassJavaDoc.contains(END_STRING)));
}
......@@ -149,7 +149,7 @@ public final class JavaDocGenTest {
public void interfaceGenerationTest() {
String interfaceJavaDoc = getJavaDoc(INTERFACE, TEST_NAME, false);
assertThat(true,
is(interfaceJavaDoc.contains("Abstraction of an entity which Reperesents the functionalities of")
is(interfaceJavaDoc.contains("Abstraction of an entity which represents the functionality of")
&& interfaceJavaDoc.contains(END_STRING)));
}
......
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
container valid {
leaf invalid-interval {
type union {
type int32;
type int8;
}
}
}
}
\ No newline at end of file