Defect Fixes and optimization for YANG translator.
Change-Id: I974a968f3c41e1abea9f2567aceb3d523645d0ae
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
index 299f5ac..42a55a6 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
@@ -78,10 +78,12 @@
* @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
*/
- public static File generateInterfaceFile(File file, List<String> imports, YangNode curNode) throws IOException {
+ public static File generateInterfaceFile(File file, List<String> imports, YangNode curNode, boolean isAttrPresent)
+ throws IOException {
JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
@@ -89,18 +91,19 @@
String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
initiateJavaFileGeneration(file, className, INTERFACE_MASK, imports, path);
-
- /**
- * Add getter methods to interface file.
- */
- try {
+ if (isAttrPresent) {
/**
- * Getter methods.
+ * Add getter methods to interface file.
*/
- insertDataIntoJavaFile(file, getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK, curNode));
- } catch (IOException e) {
- throw new IOException("No data found in temporary java code fragment files for " + className
- + " while interface file generation");
+ try {
+ /**
+ * Getter methods.
+ */
+ insertDataIntoJavaFile(file, getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK, curNode));
+ } catch (IOException e) {
+ throw new IOException("No data found in temporary java code fragment files for " + className
+ + " while interface file generation");
+ }
}
return file;
}
@@ -110,10 +113,12 @@
*
* @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
*/
- public static File generateBuilderInterfaceFile(File file, YangNode curNode) throws IOException {
+ public static File generateBuilderInterfaceFile(File file, YangNode curNode, boolean isAttrPresent)
+ throws IOException {
JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
@@ -122,22 +127,22 @@
initiateJavaFileGeneration(file, className, BUILDER_INTERFACE_MASK, null, path);
List<String> methods = new ArrayList<>();
-
- try {
- /**
- * Getter methods.
- */
- methods.add(FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK, curNode));
- /**
- * Setter methods.
- */
- methods.add(NEW_LINE);
- methods.add(FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(SETTER_FOR_INTERFACE_MASK, curNode));
- } catch (IOException e) {
- throw new IOException("No data found in temporary java code fragment files for " + className
- + " while builder interface file generation");
+ if (isAttrPresent) {
+ try {
+ /**
+ * Getter methods.
+ */
+ methods.add(FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK, curNode));
+ /**
+ * Setter methods.
+ */
+ methods.add(NEW_LINE);
+ methods.add(FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(SETTER_FOR_INTERFACE_MASK, curNode));
+ } catch (IOException e) {
+ throw new IOException("No data found in temporary java code fragment files for " + className
+ + " while builder interface file generation");
+ }
}
-
/**
* Add build method to builder interface file.
*/
@@ -161,10 +166,12 @@
* @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) throws IOException {
+ public static File generateBuilderClassFile(File file, List<String> imports, YangNode curNode,
+ boolean isAttrPresent) throws IOException {
JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
@@ -175,31 +182,34 @@
List<String> methods = new ArrayList<>();
- /**
- * Add attribute strings.
- */
- 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 builder class file generation");
- }
-
- try {
+ if (isAttrPresent) {
/**
- * Getter methods.
+ * Add attribute strings.
*/
- methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode));
- /**
- * Setter methods.
- */
- methods.add(getDataFromTempFileHandle(SETTER_FOR_CLASS_MASK, curNode) + NEW_LINE);
- } catch (IOException e) {
- throw new IOException("No data found in temporary java code fragment files for " + className
- + " while builder class file generation");
- }
+ 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 builder class file generation");
+ }
+ try {
+ /**
+ * Getter methods.
+ */
+ methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode));
+ /**
+ * Setter methods.
+ */
+ methods.add(getDataFromTempFileHandle(SETTER_FOR_CLASS_MASK, curNode) + NEW_LINE);
+ } catch (IOException e) {
+ throw new IOException("No data found in temporary java code fragment files for " + className
+ + " while builder class file generation");
+ }
+ } else {
+ insertDataIntoJavaFile(file, NEW_LINE);
+ }
/**
* Add default constructor and build method impl.
*/
@@ -221,10 +231,11 @@
*
* @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
*/
- public static File generateImplClassFile(File file, YangNode curNode)
+ public static File generateImplClassFile(File file, YangNode curNode, boolean isAttrPresent)
throws IOException {
JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
@@ -235,45 +246,48 @@
initiateJavaFileGeneration(file, className, IMPL_CLASS_MASK, null, path);
List<String> methods = new ArrayList<>();
+ if (isAttrPresent) {
+ /**
+ * Add attribute strings.
+ */
+ 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 impl class file generation");
+ }
- /**
- * Add attribute strings.
- */
- 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 impl class file generation");
+ insertDataIntoJavaFile(file, NEW_LINE);
+ try {
+ /**
+ * Getter methods.
+ */
+ 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 + IMPL) + 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 impl class file generation");
+ }
+ } else {
+ insertDataIntoJavaFile(file, NEW_LINE);
}
-
- insertDataIntoJavaFile(file, NEW_LINE);
- try {
- /**
- * Getter methods.
- */
- 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 + IMPL) + 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 impl class file generation");
- }
-
try {
/**
* Constructor.
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
index ad4dd0d..56f365d 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
@@ -42,6 +42,7 @@
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.utils.JavaCodeSnippetGen.getJavaClassDefStart;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getJavaPackageFromPackagePath;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.ORG;
import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
@@ -53,7 +54,6 @@
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.YangIoUtils.convertPathToPkg;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
/**
@@ -192,7 +192,7 @@
if (javaPkg.contains(ORG)) {
String[] strArray = javaPkg.split(ORG);
- javaPkg = ORG + convertPathToPkg(strArray[1]);
+ javaPkg = ORG + getJavaPackageFromPackagePath(strArray[1]);
}
if (importsList != null) {
if (!importsList.isEmpty()) {
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
index e89a8a9..5ea5f1e 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
@@ -32,8 +32,10 @@
import static org.onosproject.yangutils.utils.UtilConstants.QUOTES;
import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_FIRST_DIGIT;
import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_SPECIAL_CHAR;
+import static org.onosproject.yangutils.utils.UtilConstants.REVISION_PREFIX;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.UtilConstants.UNDER_SCORE;
+import static org.onosproject.yangutils.utils.UtilConstants.VERSION_PREFIX;
/**
* Utility Class for translating the name from YANG to java convention.
@@ -121,7 +123,7 @@
*/
private static String getYangVersion(byte ver) {
- return "v" + ver;
+ return VERSION_PREFIX + ver;
}
/**
@@ -154,7 +156,7 @@
String[] revisionArr = date.split(HYPHEN);
- String rev = "rev";
+ String rev = REVISION_PREFIX;
rev = rev + revisionArr[INDEX_ZERO];
if (Integer.parseInt(revisionArr[INDEX_ONE]) <= MAX_MONTHS
@@ -251,7 +253,7 @@
* @param yangIdentifier identifier in YANG file.
* @return corresponding java identifier
*/
- public static String getLowerCase(String yangIdentifier) {
+ public static String getSmallCase(String yangIdentifier) {
return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1);
}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
index f1c17db..4eeadf6 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
@@ -20,7 +20,7 @@
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.getLowerCase;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
import static org.onosproject.yangutils.utils.UtilConstants.ADD_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.AND;
import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_DATA_TYPE;
@@ -113,7 +113,7 @@
public static String getGetterString(JavaAttributeInfo attr) {
String returnType = getReturnType(attr);
- String attributeName = getLowerCase(attr.getAttributeName());
+ String attributeName = getSmallCase(attr.getAttributeName());
return getJavaDoc(GETTER_METHOD, attributeName, attr.isListAttr())
+ getGetterForInterface(attributeName, returnType, attr.isListAttr());
@@ -129,7 +129,7 @@
public static String getSetterString(JavaAttributeInfo attr, String className) {
String attrType = getReturnType(attr);
- String attributeName = getLowerCase(attr.getAttributeName());
+ String attributeName = getSmallCase(attr.getAttributeName());
return getJavaDoc(SETTER_METHOD, attributeName, attr.isListAttr())
+ getSetterForInterface(attributeName, attrType, className, attr.isListAttr());
@@ -168,7 +168,7 @@
public static String getTypeDefConstructor(JavaAttributeInfo attr, String className) {
String attrQuaifiedType = getReturnType(attr);
- String attributeName = getLowerCase(attr.getAttributeName());
+ String attributeName = getSmallCase(attr.getAttributeName());
if (!attr.isListAttr()) {
return getTypeDefConstructorString(attrQuaifiedType, attributeName, className);
@@ -225,7 +225,7 @@
public static String getGetterForClass(JavaAttributeInfo attr) {
String attrQuaifiedType = getReturnType(attr);
- String attributeName = getLowerCase(attr.getAttributeName());
+ String attributeName = getSmallCase(attr.getAttributeName());
if (!attr.isListAttr()) {
return getGetter(attrQuaifiedType, attributeName);
@@ -258,7 +258,7 @@
public static String getSetterForClass(JavaAttributeInfo attr, String className) {
String attrQuaifiedType = getReturnType(attr);
- String attributeName = getLowerCase(attr.getAttributeName());
+ String attributeName = getSmallCase(attr.getAttributeName());
if (!attr.isListAttr()) {
return getSetter(className, attributeName, attrQuaifiedType);
}
@@ -292,7 +292,7 @@
public static String getSetterForTypeDefClass(JavaAttributeInfo attr) {
String attrQuaifiedType = getReturnType(attr);
- String attributeName = getLowerCase(attr.getAttributeName());
+ String attributeName = getSmallCase(attr.getAttributeName());
return getTypeDefSetter(attrQuaifiedType, attributeName);
}
@@ -445,7 +445,7 @@
*/
public static String getConstructor(String yangName, JavaAttributeInfo attr) {
- String attributeName = getLowerCase(attr.getAttributeName());
+ String attributeName = getSmallCase(attr.getAttributeName());
String constructor = EIGHT_SPACE_INDENTATION + THIS + PERIOD + getCamelCase(attributeName) + SPACE + EQUAL
+ SPACE + BUILDER.toLowerCase() + OBJECT + PERIOD + GET_METHOD_PREFIX
@@ -513,7 +513,7 @@
*/
public static String getToStringMethod(JavaAttributeInfo attr) {
- String attributeName = getLowerCase(attr.getAttributeName());
+ String attributeName = getSmallCase(attr.getAttributeName());
return TWELVE_SPACE_INDENTATION + PERIOD + ADD_STRING + OPEN_PARENTHESIS + QUOTES + attributeName + QUOTES
+ COMMA + SPACE + attributeName + CLOSE_PARENTHESIS;
@@ -554,7 +554,7 @@
*/
public static String getHashCodeMethod(JavaAttributeInfo attr) {
- return getLowerCase(attr.getAttributeName()) + COMMA + SPACE;
+ return getSmallCase(attr.getAttributeName()) + COMMA + SPACE;
}
/**
@@ -621,7 +621,7 @@
*/
public static String getEqualsMethod(JavaAttributeInfo attr) {
- String attributeName = getLowerCase(attr.getAttributeName());
+ String attributeName = getSmallCase(attr.getAttributeName());
return SIXTEEN_SPACE_INDENTATION + SPACE + OBJECT_STRING + SUFFIX_S + PERIOD + EQUALS_STRING + OPEN_PARENTHESIS
+ attributeName + COMMA + SPACE + OTHER + PERIOD + attributeName + CLOSE_PARENTHESIS + SPACE + AND
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/TempDataStoreTypes.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/TempDataStoreTypes.java
deleted file mode 100644
index 0344672..0000000
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/TempDataStoreTypes.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright 2016 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;
-
-/**
- * Data Store types.
- */
-public enum TempDataStoreTypes {
-
- /**
- * Getter methods for interfaces.
- */
- GETTER_METHODS,
-
- /**
- * Getter methods impl for classes.
- */
- GETTER_METHODS_IMPL,
-
- /**
- * Setter methods for interfaces.
- */
- SETTER_METHODS,
-
- /**
- * Setter methods impl for classes.
- */
- SETTER_METHODS_IMPL,
-
- /**
- * Constructor for impl class.
- */
- CONSTRUCTOR,
-
- /**
- * Attributes.
- */
- ATTRIBUTE,
-
- /**
- * TypeDef.
- */
- TYPE_DEF,
-
- /**
- * ToString method.
- */
- TO_STRING,
-
- /**
- * HashCode method.
- */
- HASH_CODE,
-
- /**
- * Equals method.
- */
- EQUALS
-}