YANG Translator optimization
Change-Id: Ie6a6b9d371a4fc5fd973cf56d6f3c7b44a3146ba
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/AttributesJavaDataType.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/AttributesJavaDataType.java
index 138b7d4..97b4758 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/AttributesJavaDataType.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/AttributesJavaDataType.java
@@ -16,8 +16,12 @@
package org.onosproject.yangutils.translator.tojava.utils;
+import java.util.Set;
+import java.util.TreeSet;
+
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangType;
+import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
/**
@@ -25,6 +29,8 @@
*/
public final class AttributesJavaDataType {
+ private static Set<JavaQualifiedTypeInfo> importInfo = new TreeSet<>();
+
/**
* Default constructor.
*/
@@ -32,6 +38,24 @@
}
/**
+ * Returns import info.
+ *
+ * @return import info
+ */
+ public static Set<JavaQualifiedTypeInfo> getImportInfo() {
+ return importInfo;
+ }
+
+ /**
+ * Adds import info to the import info set.
+ *
+ * @param importData import info
+ */
+ public static void addImportInfo(JavaQualifiedTypeInfo importData) {
+ getImportInfo().add(importData);
+ }
+
+ /**
* Returns java type.
*
* @param yangType YANG type
@@ -134,7 +158,8 @@
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
- //TODO:DERIVED
+ return JavaIdentifierSyntax
+ .getCaptialCase(JavaIdentifierSyntax.getCamelCase(yangType.getDataTypeName()));
}
} else {
if (type.equals(YangDataTypes.UINT64)) {
@@ -160,7 +185,8 @@
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
- //TODO:DERIVED
+ return JavaIdentifierSyntax
+ .getCaptialCase(JavaIdentifierSyntax.getCamelCase(yangType.getDataTypeName()));
}
}
return null;
@@ -171,9 +197,10 @@
*
* @param yangType YANG type
* @param isListAttr if the attribute is of list type
+ * @param classInfo java import class info
* @return java import package
*/
- public static String getJavaImportPackage(YangType<?> yangType, boolean isListAttr) {
+ public static String getJavaImportPackage(YangType<?> yangType, boolean isListAttr, String classInfo) {
YangDataTypes type = yangType.getDataType();
if (isListAttr) {
@@ -208,7 +235,11 @@
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
- //TODO:DERIVED
+ for (JavaQualifiedTypeInfo imports : getImportInfo()) {
+ if (imports.getClassInfo().equals(classInfo)) {
+ return imports.getPkgInfo();
+ }
+ }
}
} else {
@@ -235,7 +266,11 @@
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
- //TODO:DERIVED
+ for (JavaQualifiedTypeInfo imports : getImportInfo()) {
+ if (imports.getClassInfo().equals(classInfo)) {
+ return imports.getPkgInfo();
+ }
+ }
}
}
return null;
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
index c0f4a15..6f3b3bb 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
@@ -16,7 +16,7 @@
package org.onosproject.yangutils.translator.tojava.utils;
-import org.onosproject.yangutils.translator.GeneratedFileType;
+import org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType;
import org.onosproject.yangutils.utils.UtilConstants;
/**
@@ -44,19 +44,19 @@
* based on the file type and the YANG name of the file, generate the
* class / interface definition start.
*/
- if ((genFileTypes & GeneratedFileType.INTERFACE_MASK) != 0) {
+ if ((genFileTypes & GeneratedJavaFileType.INTERFACE_MASK) != 0) {
return getInterfaceDefinition(yangName);
- } else if ((genFileTypes & GeneratedFileType.BUILDER_CLASS_MASK) != 0) {
+ } else if ((genFileTypes & GeneratedJavaFileType.BUILDER_CLASS_MASK) != 0) {
return getBuilderClassDefinition(yangName);
- } else if ((genFileTypes & GeneratedFileType.IMPL_CLASS_MASK) != 0) {
+ } else if ((genFileTypes & GeneratedJavaFileType.IMPL_CLASS_MASK) != 0) {
return getImplClassDefinition(yangName);
- } else if ((genFileTypes & GeneratedFileType.BUILDER_INTERFACE_MASK) != 0) {
+ } else if ((genFileTypes & GeneratedJavaFileType.BUILDER_INTERFACE_MASK) != 0) {
return getBuilderInterfaceDefinition(yangName);
- } else if ((genFileTypes & GeneratedFileType.GENERATE_TYPEDEF_CLASS) != 0) {
+ } else if ((genFileTypes & GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS) != 0) {
return getTypeDefClassDefinition(yangName);
}
@@ -112,7 +112,7 @@
return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.FINAL + UtilConstants.SPACE
+ UtilConstants.CLASS + UtilConstants.SPACE + yangName + UtilConstants.IMPL + UtilConstants.SPACE
+ UtilConstants.IMPLEMENTS + UtilConstants.SPACE + yangName + UtilConstants.SPACE
- + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.NEW_LINE;
+ + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
}
/**
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/GenerateJavaCodeExitBuilder.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/GenerateJavaCodeExitBuilder.java
new file mode 100644
index 0000000..4acb1e3
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/GenerateJavaCodeExitBuilder.java
@@ -0,0 +1,232 @@
+/*
+ * 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;
+
+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.HasJavaImportData;
+import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
+import org.onosproject.yangutils.translator.tojava.JavaImportData;
+import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
+import org.onosproject.yangutils.utils.UtilConstants;
+
+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_INTERFACE_WITH_BUILDER;
+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.appendFileContents;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.clean;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.closeFileHandles;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
+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.getFileObject;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.insert;
+
+/**
+ * Provides generate java code exit.
+ */
+public final class GenerateJavaCodeExitBuilder {
+
+ private static final String JAVA_FILE_EXTENSION = ".java";
+
+ /**
+ * Default constructor.
+ */
+ private GenerateJavaCodeExitBuilder() {
+ }
+
+ /**
+ * Sets import for hash and equals method.
+ *
+ * @return import string for implementing hash and equals
+ */
+ private static String setImportForHashAndEquals() {
+
+ return UtilConstants.IMPORT + UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG + UtilConstants.PERIOD
+ + UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
+ }
+
+ /**
+ * Sets import for to string method.
+ *
+ * @return import string for implementing to string
+ */
+ private static String setImportForToString() {
+
+ return UtilConstants.IMPORT + UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG + UtilConstants.PERIOD
+ + UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
+ }
+
+ /**
+ * Sets import for to list.
+ *
+ * @return import string for list collection
+ */
+ private static String setImportForList() {
+
+ return UtilConstants.IMPORT + UtilConstants.COLLECTION_IMPORTS + UtilConstants.PERIOD
+ + UtilConstants.LIST + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
+ }
+
+ /**
+ * Construct java code exit.
+ *
+ * @param fileType generated file type
+ * @param curNode current YANG node
+ * @throws IOException when fails to generate java files
+ */
+ public static void generateJavaFile(int fileType, YangNode curNode) throws IOException {
+
+ JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
+ String className = JavaIdentifierSyntax.getCaptialCase(javaFileInfo.getJavaName());
+ String pkg = javaFileInfo.getPackageFilePath();
+ List<String> imports = getImports(((HasJavaImportData) curNode).getJavaImportData());
+
+ /**
+ * Start generation of files.
+ */
+ if ((fileType & INTERFACE_MASK) != 0 | (fileType & BUILDER_INTERFACE_MASK) != 0
+ | fileType == GENERATE_INTERFACE_WITH_BUILDER) {
+
+ /**
+ * Create interface file.
+ */
+ String interfaceFileName = className;
+ File interfaceFile = getFileObject(pkg, interfaceFileName, JAVA_FILE_EXTENSION, javaFileInfo);
+ interfaceFile = generateInterfaceFile(interfaceFile, imports, curNode);
+ /**
+ * Create temp builder interface file.
+ */
+ String builderInterfaceFileName = className
+ + UtilConstants.BUILDER + UtilConstants.INTERFACE;
+ File builderInterfaceFile = getFileObject(pkg, builderInterfaceFileName, JAVA_FILE_EXTENSION, javaFileInfo);
+ builderInterfaceFile = generateBuilderInterfaceFile(builderInterfaceFile, curNode);
+ /**
+ * Append builder interface file to interface file and close it.
+ */
+ appendFileContents(builderInterfaceFile, interfaceFile);
+ insert(interfaceFile, JavaCodeSnippetGen.getJavaClassDefClose());
+ /**
+ * Close file handle for interface files.
+ */
+ closeFileHandles(builderInterfaceFile);
+ closeFileHandles(interfaceFile);
+
+ /**
+ * Remove temp files.
+ */
+ clean(builderInterfaceFile);
+ }
+
+ imports.add(setImportForHashAndEquals());
+ imports.add(setImportForToString());
+ java.util.Collections.sort(imports);
+
+ if ((fileType & BUILDER_CLASS_MASK) != 0 | (fileType & IMPL_CLASS_MASK) != 0
+ | fileType == GENERATE_INTERFACE_WITH_BUILDER) {
+
+ /**
+ * Create builder class file.
+ */
+ String builderFileName = className
+ + UtilConstants.BUILDER;
+ File builderFile = getFileObject(pkg, builderFileName, JAVA_FILE_EXTENSION, javaFileInfo);
+ builderFile = generateBuilderClassFile(builderFile, imports, curNode);
+ /**
+ * Create temp impl class file.
+ */
+
+ String implFileName = className + UtilConstants.IMPL;
+ File implTempFile = getFileObject(pkg, implFileName, JAVA_FILE_EXTENSION, javaFileInfo);
+ implTempFile = generateImplClassFile(implTempFile, curNode);
+ /**
+ * Append impl class to builder class and close it.
+ */
+ appendFileContents(implTempFile, builderFile);
+ insert(builderFile, JavaCodeSnippetGen.getJavaClassDefClose());
+
+ /**
+ * Close file handle for classes files.
+ */
+ closeFileHandles(implTempFile);
+ closeFileHandles(builderFile);
+
+ /**
+ * Remove temp files.
+ */
+ clean(implTempFile);
+ }
+
+ /**
+ * if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
+ *
+ * /** Create builder class file. //
+ */
+ //String typeDefFileName = className;
+ //File typeDefFile = JavaFileGenerator.getFileObject(path, typeDefFileName, JAVA_FILE_EXTENSION,
+ // ((HasJavaFileInfo) curNode).getJavaFileInfo());
+ //typeDefFile = JavaFileGenerator.generateTypeDefClassFile(typeDefFile, className, imports,
+ // path.replace('/', '.'), attrList, ((HasJavaFileInfo) curNode).getJavaFileInfo());
+ // JavaFileGenerator.insert(typeDefFile, JavaCodeSnippetGen.getJavaClassDefClose());
+
+ // /**
+ // * Close file handle for classes files.
+ // */
+ // JavaFileGenerator.closeFileHandles(typeDefFile);
+ // }
+ //
+ }
+
+ /**
+ * Returns import for class.
+ *
+ * @param javaImportData import data
+ * @return imports for class
+ */
+ private static List<String> getImports(JavaImportData javaImportData) {
+
+ String importString;
+ List<String> imports = new ArrayList<>();
+
+ for (JavaQualifiedTypeInfo importInfo : javaImportData.getImportSet()) {
+ importString = UtilConstants.IMPORT;
+ if (importInfo.getPkgInfo() != "" && importInfo.getClassInfo() != null
+ && importInfo.getPkgInfo() != UtilConstants.JAVA_LANG) {
+ importString = importString + importInfo.getPkgInfo() + ".";
+ importString = importString + importInfo.getClassInfo() + UtilConstants.SEMI_COLAN
+ + UtilConstants.NEW_LINE;
+
+ imports.add(importString);
+ }
+ }
+
+ if (javaImportData.getIfListImported()) {
+ imports.add(setImportForList());
+ }
+
+ java.util.Collections.sort(imports);
+ return imports;
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGen.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGen.java
index 4d8180e..ab8ee5c 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGen.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGen.java
@@ -16,8 +16,7 @@
package org.onosproject.yangutils.translator.tojava.utils;
-import org.onosproject.yangutils.translator.GeneratedFileType;
-import org.onosproject.yangutils.translator.tojava.ImportInfo;
+import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
/**
@@ -51,7 +50,8 @@
* @return the textual java code information corresponding to the import
* list
*/
- public static String getImportText(ImportInfo importInfo) {
+ public static String getImportText(JavaQualifiedTypeInfo importInfo) {
+
return UtilConstants.IMPORT + importInfo.getPkgInfo() + UtilConstants.PERIOD + importInfo.getClassInfo()
+ UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
}
@@ -65,6 +65,7 @@
* @return corresponding textual java code information
*/
public static String getJavaClassDefStart(int genFileTypes, String yangName) {
+
/*
* get the camel case name for java class / interface.
*/
@@ -90,24 +91,24 @@
if (!isList) {
if (javaAttributeTypePkg != null) {
attributeDefination = attributeDefination
- + javaAttributeTypePkg + ".";
+ + javaAttributeTypePkg + UtilConstants.PERIOD;
}
attributeDefination = attributeDefination
+ javaAttributeType
+ UtilConstants.SPACE
+ javaAttributeName
- + UtilConstants.SEMI_COLAN;
+ + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
} else {
attributeDefination = attributeDefination + UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET;
if (javaAttributeTypePkg != null) {
attributeDefination = attributeDefination
- + javaAttributeTypePkg + ".";
+ + javaAttributeTypePkg + UtilConstants.PERIOD;
}
attributeDefination = attributeDefination
+ javaAttributeType + UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE
- + javaAttributeName + UtilConstants.SEMI_COLAN;
+ + javaAttributeName + UtilConstants.SUFIX_S + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
}
return attributeDefination;
}
@@ -119,6 +120,7 @@
* @return list attribute string
*/
public static String getListAttribute(String type) {
+
return UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + type + UtilConstants.DIAMOND_CLOSE_BRACKET;
}
@@ -126,23 +128,11 @@
* Based on the file type and the YANG name of the file, generate the class
* / interface definition close.
*
- * @param genFileTypes type of file being generated
- * @param yangName YANG name
* @return corresponding textual java code information
*/
- public static String getJavaClassDefClose(int genFileTypes, String yangName) {
+ public static String getJavaClassDefClose() {
- if ((genFileTypes & GeneratedFileType.INTERFACE_MASK) != 0) {
-
- return UtilConstants.CLOSE_CURLY_BRACKET;
- } else if ((genFileTypes & GeneratedFileType.BUILDER_CLASS_MASK) != 0) {
-
- return UtilConstants.CLOSE_CURLY_BRACKET;
- } else if ((genFileTypes & GeneratedFileType.GENERATE_TYPEDEF_CLASS) != 0) {
-
- return UtilConstants.CLOSE_CURLY_BRACKET;
- }
- return null;
+ return UtilConstants.CLOSE_CURLY_BRACKET;
}
}
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 221f757..ab537c8 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
@@ -16,36 +16,55 @@
package org.onosproject.yangutils.translator.tojava.utils;
-import static org.onosproject.yangutils.translator.GeneratedFileType.BUILDER_CLASS_MASK;
-import static org.onosproject.yangutils.translator.GeneratedFileType.BUILDER_INTERFACE_MASK;
-import static org.onosproject.yangutils.translator.GeneratedFileType.GENERATE_TYPEDEF_CLASS;
-import static org.onosproject.yangutils.translator.GeneratedFileType.IMPL_CLASS_MASK;
-import static org.onosproject.yangutils.translator.GeneratedFileType.INTERFACE_MASK;
-import static org.slf4j.LoggerFactory.getLogger;
-
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
-import org.onosproject.yangutils.translator.CachedFileHandle;
-import org.onosproject.yangutils.translator.tojava.AttributeInfo;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
+import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles;
+import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
+import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.CopyrightHeader;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType;
-import org.onosproject.yangutils.utils.io.impl.YangIoUtils;
-import org.slf4j.Logger;
+
+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.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_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.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.utils.JavaIdentifierSyntax.getCaptialCase;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructorStart;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
+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.getHashCodeMethodClose;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodOpen;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethodClose;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethodOpen;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.partString;
/**
* Generates java file.
*/
public final class JavaFileGenerator {
- private static final Logger log = getLogger(JavaFileGenerator.class);
-
/**
* Default constructor.
*/
@@ -61,42 +80,37 @@
* @param handle cached file handle
* @return file object
*/
- public static File getFileObject(String filePath, String fileName, String extension, CachedFileHandle handle) {
- return new File(handle.getCodeGenFilePath() + filePath + File.separator + fileName + extension);
+ public static File getFileObject(String filePath, String fileName, String extension, JavaFileInfo handle) {
+
+ return new File(handle.getBaseCodeGenPath() + filePath + File.separator + fileName + extension);
}
/**
* Returns generated interface file for current node.
*
* @param file file
- * @param className class name
* @param imports imports for the file
- * @param attrList attribute info
- * @param pkg generated file package
- * @param handle cached file handle
+ * @param curNode current YANG node
* @return interface file
* @throws IOException when fails to write in file
*/
- public static File generateInterfaceFile(File file, String className, List<String> imports,
- List<AttributeInfo> attrList, String pkg, CachedFileHandle handle) throws IOException {
- String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
- initiateFile(file, className, INTERFACE_MASK, imports, pkg);
+ public static File generateInterfaceFile(File file, List<String> imports, YangNode curNode) throws IOException {
- if (!attrList.isEmpty()) {
- List<String> methods = new ArrayList<>();
- try {
- methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path));
- } catch (ClassNotFoundException | IOException e) {
- log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
- throw new IOException("Fail to read data from temp file.");
- }
+ JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
- /**
- * Add getter methods to interface file.
- */
- for (String method : methods) {
- appendMethod(file, method);
- }
+ String className = getCaptialCase(javaFileInfo.getJavaName());
+ String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
+
+ initiateFile(file, className, INTERFACE_MASK, imports, path);
+
+ /**
+ * Add getter methods to interface file.
+ */
+ try {
+ appendMethod(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;
}
@@ -105,39 +119,41 @@
* Return generated builder interface file for current node.
*
* @param file file
- * @param className class name
- * @param pkg generated file package
- * @param attrList attribute info
- * @param handle cached file handle
+ * @param curNode current YANG node
* @return builder interface file
* @throws IOException when fails to write in file
*/
- public static File generateBuilderInterfaceFile(File file, String className, String pkg,
- List<AttributeInfo> attrList, CachedFileHandle handle) throws IOException {
- String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
- initiateFile(file, className, BUILDER_INTERFACE_MASK, null, pkg);
+ public static File generateBuilderInterfaceFile(File file, YangNode curNode) throws IOException {
+
+ JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
+
+ String className = getCaptialCase(javaFileInfo.getJavaName());
+ String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
+
+ initiateFile(file, className, BUILDER_INTERFACE_MASK, null, path);
List<String> methods = new ArrayList<>();
- if (!attrList.isEmpty()) {
-
- try {
- methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path));
- methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS, className, path));
- } catch (ClassNotFoundException | IOException e) {
- log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
- throw new IOException("Fail to read data from temp file.");
- }
+ try {
+ methods.add(UtilConstants.FOUR_SPACE_INDENTATION
+ + getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK, curNode));
+ methods.add(UtilConstants.NEW_LINE);
+ methods.add(UtilConstants.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.
*/
- methods.add(MethodsGenerator.parseBuilderInterfaceBuildMethodString(className));
+ methods.add(parseBuilderInterfaceBuildMethodString(className));
/**
* Add getters and setters in builder interface.
*/
for (String method : methods) {
- appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method);
+ appendMethod(file, method);
}
insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
@@ -148,61 +164,53 @@
* Returns generated builder class file for current node.
*
* @param file file
- * @param className class name
* @param imports imports for the file
- * @param pkg generated file package
- * @param attrList attribute info
- * @param handle cached file handle
+ * @param curNode current YANG node
* @return builder class file
* @throws IOException when fails to write in file
*/
- public static File generateBuilderClassFile(File file, String className, List<String> imports, String pkg,
- List<AttributeInfo> attrList, CachedFileHandle handle) throws IOException {
- String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
- initiateFile(file, className, BUILDER_CLASS_MASK, imports, pkg);
+ public static File generateBuilderClassFile(File file, List<String> imports, YangNode curNode) throws IOException {
+
+ JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
+
+ String className = getCaptialCase(javaFileInfo.getJavaName());
+ String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
+
+ initiateFile(file, className, BUILDER_CLASS_MASK, imports, path);
List<String> methods = new ArrayList<>();
- if (!attrList.isEmpty()) {
- /**
- * Add attribute strings.
- */
- List<String> attributes = new ArrayList<>();
- try {
- attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path));
- } catch (ClassNotFoundException | IOException e) {
- log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
- throw new IOException("Fail to read data from temp file.");
- }
- /**
- * Add attributes to the file.
- */
- for (String attribute : attributes) {
- insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
- }
- try {
- methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path));
- methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS_IMPL, className, path));
- } catch (ClassNotFoundException | IOException e) {
- log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
- throw new IOException("Fail to read data from temp file.");
- }
-
+ /**
+ * Add attribute strings.
+ */
+ try {
+ insert(file, UtilConstants.NEW_LINE + UtilConstants.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 {
+ methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode));
+ methods.add(getDataFromTempFileHandle(SETTER_FOR_CLASS_MASK, curNode) + UtilConstants.NEW_LINE);
+ } catch (IOException e) {
+ throw new IOException("No data found in temporary java code fragment files for " + className
+ + " while builder class file generation");
+ }
+
/**
* Add default constructor and build method impl.
*/
- methods.add(
- UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
- + MethodsGenerator.getDefaultConstructorString(className + UtilConstants.BUILDER,
- UtilConstants.PUBLIC));
- methods.add(MethodsGenerator.getBuildString(className));
+ methods.add(getBuildString(className) + UtilConstants.NEW_LINE);
+ methods.add(UtilConstants.NEW_LINE
+ + getDefaultConstructorString(className + UtilConstants.BUILDER, UtilConstants.PUBLIC));
/**
* Add methods in builder class.
*/
for (String method : methods) {
- appendMethod(file, method + UtilConstants.NEW_LINE);
+ appendMethod(file, method);
}
return file;
}
@@ -211,67 +219,59 @@
* Returns generated impl class file for current node.
*
* @param file file
- * @param className class name
- * @param pkg generated file package
- * @param attrList attribute's info
- * @param handle cached file handle
+ * @param curNode current YANG node
* @return impl class file
* @throws IOException when fails to write in file
*/
- public static File generateImplClassFile(File file, String className, String pkg, List<AttributeInfo> attrList,
- CachedFileHandle handle)
- throws IOException {
- String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
+ public static File generateImplClassFile(File file, YangNode curNode)
+ throws IOException {
+
+ JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
+
+ String className = getCaptialCase(javaFileInfo.getJavaName());
+ String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
+
initiateFile(file, className, IMPL_CLASS_MASK, null, path);
List<String> methods = new ArrayList<>();
- if (!attrList.isEmpty()) {
- List<String> attributes = new ArrayList<>();
- try {
- attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path));
- } catch (ClassNotFoundException | IOException e) {
- log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
- throw new IOException("Fail to read data from temp file.");
- }
- /**
- * Add attributes to the file.
- */
- for (String attribute : attributes) {
- insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
- }
+ /**
+ * Add attribute strings.
+ */
+ try {
+ insert(file, UtilConstants.NEW_LINE + UtilConstants.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");
+ }
- try {
+ insert(file, UtilConstants.NEW_LINE);
+ try {
- methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path));
+ methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode));
- methods.add(MethodsGenerator.getHashCodeMethodClose(MethodsGenerator.getHashCodeMethodOpen()
- + YangIoUtils
- .partString(handle.getTempData(TempDataStoreTypes.HASH_CODE, className, path).replace(
- UtilConstants.NEW_LINE, ""))));
+ methods.add(getHashCodeMethodClose(getHashCodeMethodOpen() + partString(
+ getDataFromTempFileHandle(HASH_CODE_IMPL_MASK, curNode).replace(UtilConstants.NEW_LINE,
+ UtilConstants.EMPTY_STRING))));
- methods.add(MethodsGenerator
- .getEqualsMethodClose(MethodsGenerator.getEqualsMethodOpen(className + UtilConstants.IMPL)
- + handle.getTempData(TempDataStoreTypes.EQUALS, className, path)));
+ methods.add(getEqualsMethodClose(getEqualsMethodOpen(className + UtilConstants.IMPL)
+ + getDataFromTempFileHandle(EQUALS_IMPL_MASK, curNode)));
- methods.add(MethodsGenerator.getToStringMethodOpen()
- + handle.getTempData(TempDataStoreTypes.TO_STRING, className, path)
- + MethodsGenerator.getToStringMethodClose());
+ methods.add(getToStringMethodOpen() + getDataFromTempFileHandle(TO_STRING_IMPL_MASK, curNode)
+ + getToStringMethodClose());
- } catch (ClassNotFoundException | IOException e) {
- log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
- throw new IOException("Fail to read data from temp file.");
- }
-
+ } catch (IOException e) {
+ throw new IOException("No data found in temporary java code fragment files for " + className
+ + " while impl class file generation");
}
try {
- methods.add(getConstructorString(className)
- + handle.getTempData(TempDataStoreTypes.CONSTRUCTOR, className, path)
+ methods.add(getConstructorStart(className) + getDataFromTempFileHandle(CONSTRUCTOR_IMPL_MASK, curNode)
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET);
- } catch (ClassNotFoundException | IOException e) {
- log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
- throw new IOException("Fail to read data from temp file.");
+ } catch (IOException e) {
+ throw new IOException("No data found in temporary java code fragment files for " + className
+ + " while impl class file generation");
}
/**
* Add methods in impl class.
@@ -285,56 +285,48 @@
}
/**
- * Generate class file for type def.
+ * Return data stored in temporary files.
*
- * @param file generated file
- * @param className file name
- * @param imports imports for file
- * @param pkg package path
- * @param cachedAttributeList attribute list
- * @param handle cached file handle
- * @return type def class file
- * @throws IOException when fails to generate class file
+ * @param curNode current YANG node
+ * @param generatedTempFiles mask for the types of files being generated
+ * @return data stored in temporary files
+ * @throws IOException when failed to get the data from temporary file
+ * handle
*/
- public static File generateTypeDefClassFile(File file, String className, List<String> imports,
- String pkg, List<AttributeInfo> cachedAttributeList, CachedFileHandle handle) throws IOException {
- String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
- initiateFile(file, className, GENERATE_TYPEDEF_CLASS, imports, pkg);
+ private static String getDataFromTempFileHandle(int generatedTempFiles, YangNode curNode) throws IOException {
- List<String> typeDef = new ArrayList<>();
- try {
- typeDef.add(handle.getTempData(TempDataStoreTypes.TYPE_DEF, className, path));
- } catch (ClassNotFoundException | IOException e) {
- log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
- throw new IOException("Fail to read data from temp file.");
+ TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles = ((HasTempJavaCodeFragmentFiles) curNode)
+ .getTempJavaCodeFragmentFiles();
+
+ if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
+ return tempJavaCodeFragmentFiles
+ .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getAttributesTempFileHandle());
+ } else if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
+ return tempJavaCodeFragmentFiles
+ .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getGetterInterfaceTempFileHandle());
+ } else if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
+ return tempJavaCodeFragmentFiles
+ .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getSetterInterfaceTempFileHandle());
+ } else if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
+ return tempJavaCodeFragmentFiles
+ .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getGetterImplTempFileHandle());
+ } else if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
+ return tempJavaCodeFragmentFiles
+ .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getSetterImplTempFileHandle());
+ } else if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) {
+ return tempJavaCodeFragmentFiles
+ .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getConstructorImplTempFileHandle());
+ } else if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) {
+ return tempJavaCodeFragmentFiles
+ .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getHashCodeImplTempFileHandle());
+ } else if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
+ return tempJavaCodeFragmentFiles
+ .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getEqualsImplTempFileHandle());
+ } else if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
+ return tempJavaCodeFragmentFiles
+ .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getToStringImplTempFileHandle());
}
-
- /**
- * Add attributes to the file.
- */
- for (String attribute : typeDef) {
- insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
- }
-
- return file;
- }
-
- /**
- * Returns constructor string for impl class.
- *
- * @param yangName class name
- * @return constructor string
- */
- private static String getConstructorString(String yangName) {
-
- String builderAttribute = yangName.substring(0, 1).toLowerCase() + yangName.substring(1);
- String javadoc = MethodsGenerator.getConstructorString(yangName);
- String constructor = UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
- + yangName + UtilConstants.IMPL + UtilConstants.OPEN_PARENTHESIS + yangName + UtilConstants.BUILDER
- + UtilConstants.SPACE + builderAttribute + UtilConstants.BUILDER + UtilConstants.OBJECT
- + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
- + UtilConstants.NEW_LINE;
- return javadoc + constructor;
+ return null;
}
/**
@@ -349,6 +341,7 @@
*/
private static void initiateFile(File file, String className, int type, List<String> imports,
String pkg) throws IOException {
+
try {
file.createNewFile();
appendContents(file, className, type, imports, pkg);
@@ -365,6 +358,7 @@
* @throws IOException when fails to append contents
*/
public static void appendFileContents(File appendFile, File srcFile) throws IOException {
+
try {
FileSystemUtil.appendFileContents(appendFile, srcFile);
} catch (IOException e) {
@@ -377,184 +371,14 @@
*
* @param file file in which method needs to be appended
* @param method method which needs to be appended
+ * @throws IOException IO operation failure
*/
private static void appendMethod(File file, String method) throws IOException {
+
insert(file, method);
}
/**
- * Closes the current generated file.
- *
- * @param fileType generate file type
- * @param yangName file name
- * @return end of class definition string
- */
- public static String closeFile(int fileType, String yangName) {
- return JavaCodeSnippetGen.getJavaClassDefClose(fileType, yangName);
- }
-
- /**
- * Parses attribute info and fetch specific data and creates serialized
- * files of it.
- *
- * @param attr attribute info.
- * @param genFileType generated file type
- * @param className class name
- * @param path file path
- * @param handle cached file handle
- */
- public static void parseAttributeInfo(AttributeInfo attr, int genFileType, String className, String path,
- CachedFileHandle handle) {
-
- String attrString = "";
-
- String getterString = "";
- String getterImplString = "";
-
- String setterString = "";
- String setterImplString = "";
-
- String constructorString = "";
- String typeDefString = "";
-
- String toString = "";
- String hashCodeString = "";
- String equalsString = "";
-
- className = JavaIdentifierSyntax.getCaptialCase(className);
-
- try {
- /*
- * Get the attribute definition and save attributes to temporary
- * file.
- */
-
- boolean isList = attr.isListAttr();
- String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
- if (attr.isQualifiedName()) {
- attrString = JavaCodeSnippetGen.getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
- attr.getImportInfo().getClassInfo(),
- attributeName, attr.isListAttr());
- } else {
- attrString = JavaCodeSnippetGen.getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(),
- attributeName, attr.isListAttr());
- }
- handle.setTempData(attrString + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION,
- TempDataStoreTypes.ATTRIBUTE, className,
- path);
-
- if ((genFileType & INTERFACE_MASK) != 0) {
- getterString = MethodsGenerator.getGetterString(attr);
- handle.setTempData(getterString + UtilConstants.NEW_LINE,
- TempDataStoreTypes.GETTER_METHODS,
- className,
- path);
-
- }
-
- if ((genFileType & BUILDER_INTERFACE_MASK) != 0) {
- setterString = MethodsGenerator.getSetterString(attr, className);
- handle.setTempData(setterString + UtilConstants.NEW_LINE,
- TempDataStoreTypes.SETTER_METHODS,
- className,
- path);
- }
-
- if ((genFileType & BUILDER_CLASS_MASK) != 0) {
- getterImplString = MethodsGenerator.getGetterForClass(attr);
- handle.setTempData(
- MethodsGenerator.getOverRideString() + getterImplString + UtilConstants.NEW_LINE,
- TempDataStoreTypes.GETTER_METHODS_IMPL, className,
- path);
- setterImplString = MethodsGenerator.getSetterForClass(attr, className);
- handle.setTempData(
- MethodsGenerator.getOverRideString() + setterImplString + UtilConstants.NEW_LINE,
- TempDataStoreTypes.SETTER_METHODS_IMPL, className,
- path);
- }
-
- if ((genFileType & IMPL_CLASS_MASK) != 0) {
- constructorString = MethodsGenerator.getConstructor(className, attr);
- handle.setTempData(constructorString, TempDataStoreTypes.CONSTRUCTOR, className,
- path);
-
- hashCodeString = MethodsGenerator.getHashCodeMethod(attr);
- handle.setTempData(hashCodeString + UtilConstants.NEW_LINE,
- TempDataStoreTypes.HASH_CODE,
- className,
- path);
- equalsString = MethodsGenerator.getEqualsMethod(attr);
- handle.setTempData(equalsString + UtilConstants.NEW_LINE,
- TempDataStoreTypes.EQUALS,
- className,
- path);
-
- toString = MethodsGenerator.getToStringMethod(attr);
- handle.setTempData(toString + UtilConstants.NEW_LINE,
- TempDataStoreTypes.TO_STRING,
- className,
- path);
-
- }
-
- if ((genFileType & GENERATE_TYPEDEF_CLASS) != 0) {
-
- if (attr.isQualifiedName()) {
- typeDefString = JavaCodeSnippetGen.getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
- attr.getImportInfo().getClassInfo(),
- attributeName, attr.isListAttr()) + UtilConstants.NEW_LINE;
- } else {
- typeDefString = JavaCodeSnippetGen.getJavaAttributeDefination(null,
- attr.getImportInfo().getClassInfo(),
- attributeName, attr.isListAttr()) + UtilConstants.NEW_LINE;
- }
-
- typeDefString = typeDefString + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
- + UtilConstants.JAVA_DOC_FIRST_LINE;
-
- typeDefString = typeDefString
- + MethodsGenerator.getDefaultConstructorString(className, UtilConstants.PRIVATE)
- + UtilConstants.NEW_LINE;
-
- typeDefString = typeDefString
- + JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_CONSTRUCTOR, className, isList)
- + MethodsGenerator.getTypeDefConstructor(attr, className)
- + UtilConstants.NEW_LINE;
-
- typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.OF, className, isList)
- + MethodsGenerator.getOfMethod(className, attr) + UtilConstants.NEW_LINE;
-
- typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.GETTER, className, isList)
- + MethodsGenerator.getGetterForClass(attr) + UtilConstants.NEW_LINE;
-
- typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_SETTER, className, isList)
- + MethodsGenerator.getSetterForTypeDefClass(attr)
- + UtilConstants.NEW_LINE;
-
- hashCodeString = MethodsGenerator.getHashCodeMethodOpen()
- + YangIoUtils.partString(
- MethodsGenerator.getHashCodeMethod(attr).replace(UtilConstants.NEW_LINE, ""));
- hashCodeString = MethodsGenerator.getHashCodeMethodClose(hashCodeString) + UtilConstants.NEW_LINE;
-
- equalsString = MethodsGenerator.getEqualsMethodOpen(className) + UtilConstants.NEW_LINE
- + MethodsGenerator.getEqualsMethod(attr);
- equalsString = MethodsGenerator.getEqualsMethodClose(equalsString) + UtilConstants.NEW_LINE;
-
- toString = MethodsGenerator.getToStringMethodOpen()
- + MethodsGenerator.getToStringMethod(attr) + UtilConstants.NEW_LINE
- + MethodsGenerator.getToStringMethodClose()
- + UtilConstants.NEW_LINE;
- typeDefString = typeDefString + hashCodeString + equalsString + toString;
- handle.setTempData(typeDefString, TempDataStoreTypes.TYPE_DEF, className,
- path);
- }
- } catch (IOException e) {
- log.info("Failed to set data for " + attr.getAttributeName() + " in temp data files.");
- }
-
- }
-
- /**
* Appends all the contents into a generated java file.
*
* @param file generated file
@@ -567,10 +391,7 @@
private static void appendContents(File file, String fileName, int type, List<String> importsList,
String pkg) throws IOException {
- if (pkg.contains(UtilConstants.YANG_GEN_DIR)) {
- String[] strArray = pkg.split(UtilConstants.YANG_GEN_DIR);
- pkg = strArray[1].replace(UtilConstants.SLASH, UtilConstants.PERIOD);
- }
+ String pkgString = parsePackageString(pkg, importsList);
if ((type & IMPL_CLASS_MASK) != 0) {
@@ -579,38 +400,68 @@
write(file, fileName, type, JavaDocType.BUILDER_INTERFACE);
} else if ((type & GENERATE_TYPEDEF_CLASS) != 0) {
- insert(file, CopyrightHeader.getCopyrightHeader());
- insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
- if (importsList != null) {
- insert(file, UtilConstants.NEW_LINE);
- for (String imports : importsList) {
- insert(file, imports);
- }
- }
+ appendHeaderContents(file, pkgString, importsList);
write(file, fileName, type, JavaDocType.IMPL_CLASS);
- } else {
+ } else if ((type & INTERFACE_MASK) != 0) {
- if ((type & INTERFACE_MASK) != 0) {
- insert(file, CopyrightHeader.getCopyrightHeader());
- insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
- if (importsList != null) {
- insert(file, UtilConstants.NEW_LINE);
- for (String imports : importsList) {
- insert(file, imports);
- }
- }
- write(file, fileName, type, JavaDocType.INTERFACE);
- } else if ((type & BUILDER_CLASS_MASK) != 0) {
- insert(file, CopyrightHeader.getCopyrightHeader());
- insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
- if (importsList != null) {
- insert(file, UtilConstants.NEW_LINE);
- for (String imports : importsList) {
- insert(file, imports);
- }
- insert(file, UtilConstants.NEW_LINE);
- }
- write(file, fileName, type, JavaDocType.BUILDER_CLASS);
+ appendHeaderContents(file, pkgString, importsList);
+ write(file, fileName, type, JavaDocType.INTERFACE);
+ } else if ((type & BUILDER_CLASS_MASK) != 0) {
+
+ appendHeaderContents(file, pkgString, importsList);
+ write(file, fileName, type, JavaDocType.BUILDER_CLASS);
+ }
+ }
+
+ /**
+ * Removes base directory path from package and generates package string for
+ * file.
+ *
+ * @param pkg generated package
+ * @param importsList list of imports
+ * @return package string
+ */
+ private static String parsePackageString(String pkg, List<String> importsList) {
+
+ if (pkg.contains(UtilConstants.ORG)) {
+ String[] strArray = pkg.split(UtilConstants.ORG);
+ pkg = UtilConstants.ORG + strArray[1].replace(UtilConstants.SLASH, UtilConstants.PERIOD);
+ }
+ if (importsList != null) {
+ if (!importsList.isEmpty()) {
+ return UtilConstants.PACKAGE + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN
+ + UtilConstants.NEW_LINE;
+ } else {
+ return UtilConstants.PACKAGE + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN;
+ }
+ } else {
+ return UtilConstants.PACKAGE + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN;
+ }
+ }
+
+ /**
+ * 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 importsList list of imports
+ * @throws IOException when fails to append contents.
+ */
+ private static void appendHeaderContents(File file, String pkg, List<String> importsList) throws IOException {
+
+ insert(file, CopyrightHeader.getCopyrightHeader());
+ insert(file, pkg);
+
+ /*
+ * TODO: add the file header using
+ * JavaCodeSnippetGen.getFileHeaderComment
+ */
+
+ if (importsList != null) {
+ insert(file, UtilConstants.NEW_LINE);
+ for (String imports : importsList) {
+ insert(file, imports);
}
}
}
@@ -639,6 +490,7 @@
* @throws IOException when fails to insert into file
*/
public static void insert(File file, String data) throws IOException {
+
try {
FileSystemUtil.updateFileHandle(file, data, false);
} catch (IOException e) {
@@ -653,6 +505,7 @@
* @throws IOException when failed to close the file handle
*/
public static void closeFileHandles(File file) throws IOException {
+
try {
FileSystemUtil.updateFileHandle(file, null, true);
} catch (IOException e) {
@@ -666,6 +519,7 @@
* @param file file to be removed
*/
public static void clean(File file) {
+
if (file.exists()) {
file.delete();
}
@@ -678,6 +532,7 @@
* @throws IOException when fails to delete the directory
*/
public static void cleanTempFiles(File tempDir) throws IOException {
+
FileUtils.deleteDirectory(tempDir);
}
}
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 2f79931..2639b8b 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
@@ -16,9 +16,13 @@
package org.onosproject.yangutils.translator.tojava.utils;
+import java.io.File;
import java.util.ArrayList;
+import org.onosproject.yangutils.datamodel.YangNode;
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.utils.UtilConstants;
/**
@@ -61,12 +65,52 @@
}
/**
+ * Get the contained data model parent node.
+ *
+ * @param currentNode current node which parent contained node is required
+ * @return parent node in which the current node is an attribute
+ */
+ public static YangNode getParentNodeInGenCode(YangNode currentNode) {
+
+ /*
+ * TODO: recursive parent lookup to support choice/augment/uses. TODO:
+ * need to check if this needs to be updated for
+ * choice/case/augment/grouping
+ */
+ return currentNode.getParent();
+ }
+
+ /**
+ * Get the node package string.
+ *
+ * @param curNode current java node whose package string needs to be set
+ * @return returns the root package string
+ */
+ public static String getCurNodePackage(YangNode curNode) {
+
+ String pkg;
+ if (!(curNode instanceof HasJavaFileInfo)
+ || curNode.getParent() == null) {
+ throw new RuntimeException("missing parent node to get current node's package");
+ }
+
+ YangNode parentNode = getParentNodeInGenCode(curNode);
+ if (!(parentNode instanceof HasJavaFileInfo)) {
+ throw new RuntimeException("missing parent java node to get current node's package");
+ }
+ JavaFileInfo parentJavaFileHandle = ((HasJavaFileInfo) parentNode).getJavaFileInfo();
+ pkg = parentJavaFileHandle.getPackage() + UtilConstants.PERIOD + parentJavaFileHandle.getJavaName();
+ return pkg.toLowerCase();
+ }
+
+ /**
* Returns version.
*
* @param ver YANG version
* @return version
*/
private static String getYangVersion(byte ver) {
+
return "v" + ver;
}
@@ -77,6 +121,7 @@
* @return java package name as per java rules
*/
public static String getPkgFromNameSpace(String nameSpace) {
+
ArrayList<String> pkgArr = new ArrayList<String>();
nameSpace = nameSpace.replace(UtilConstants.QUOTES, UtilConstants.EMPTY_STRING);
String properNameSpace = nameSpace.replaceAll(UtilConstants.REGEX_WITH_SPECIAL_CHAR, UtilConstants.COLAN);
@@ -96,12 +141,13 @@
* @throws TranslatorException when date is invalid.
*/
public static String getYangRevisionStr(String date) throws TranslatorException {
+
String[] revisionArr = date.split(UtilConstants.HYPHEN);
String rev = "rev";
rev = rev + revisionArr[INDEX_ZERO];
- if ((Integer.parseInt(revisionArr[INDEX_ONE]) <= MAX_MONTHS)
+ if (Integer.parseInt(revisionArr[INDEX_ONE]) <= MAX_MONTHS
&& Integer.parseInt(revisionArr[INDEX_TWO]) <= MAX_DAYS) {
for (int i = INDEX_ONE; i < revisionArr.length; i++) {
@@ -131,7 +177,7 @@
int i = 0;
for (String member : pkgArr) {
boolean presenceOfKeyword = UtilConstants.JAVA_KEY_WORDS.contains(member);
- if (presenceOfKeyword || (member.matches(UtilConstants.REGEX_FOR_FIRST_DIGIT))) {
+ if (presenceOfKeyword || member.matches(UtilConstants.REGEX_FOR_FIRST_DIGIT)) {
member = UtilConstants.UNDER_SCORE + member;
}
pkg = pkg + member;
@@ -144,23 +190,13 @@
}
/**
- * Get the package from parent's package and string.
- *
- * @param parentPkg parent's package
- * @param parentName parent's name
- * @return package string
- */
- public static String getPackageFromParent(String parentPkg, String parentName) {
- return (parentPkg + UtilConstants.PERIOD + getSubPkgFromName(parentName)).toLowerCase();
- }
-
- /**
* Get package sub name from YANG identifier name.
*
* @param name YANG identifier name
* @return java package sub name as per java rules
*/
public static String getSubPkgFromName(String name) {
+
ArrayList<String> pkgArr = new ArrayList<String>();
String[] nameArr = name.split(UtilConstants.COLAN);
@@ -177,6 +213,7 @@
* @return corresponding java identifier
*/
public static String getCamelCase(String yangIdentifier) {
+
String[] strArray = yangIdentifier.split(UtilConstants.HYPHEN);
String camelCase = strArray[0];
for (int i = 1; i < strArray.length; i++) {
@@ -193,16 +230,41 @@
* @return corresponding java identifier
*/
public static String getCaptialCase(String yangIdentifier) {
+
return yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1);
}
/**
- * Translate the YANG identifier name to java identifier with first letter in small.
+ * Translate the YANG identifier name to java identifier with first letter
+ * in small.
*
* @param yangIdentifier identifier in YANG file.
* @return corresponding java identifier
*/
public static String getLowerCase(String yangIdentifier) {
+
return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1);
}
+
+ /**
+ * Get the java Package from package path.
+ *
+ * @param packagePath package path
+ * @return java package
+ */
+ public static String getJavaPackageFromPackagePath(String packagePath) {
+
+ return packagePath.replace(File.separator, UtilConstants.PERIOD);
+ }
+
+ /**
+ * Get the directory path corresponding to java package.
+ *
+ * @param packagePath package path
+ * @return java package
+ */
+ public static String getPackageDirPathFromJavaJPackage(String packagePath) {
+
+ return packagePath.replace(UtilConstants.PERIOD, File.separator);
+ }
}
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 576ebff..716580e 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
@@ -16,7 +16,7 @@
package org.onosproject.yangutils.translator.tojava.utils;
-import org.onosproject.yangutils.translator.tojava.AttributeInfo;
+import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
import org.onosproject.yangutils.utils.io.impl.YangIoUtils;
@@ -35,25 +35,12 @@
/**
* Returns the methods strings for builder interface.
*
- * @param attr attribute info
- * @param className name of the java class being generated
- * @return method string for builder interface
- */
- static String parseBuilderInterfaceMethodString(AttributeInfo attr, String className) {
-
- return getGetterString(attr) + UtilConstants.NEW_LINE + getSetterString(attr, className);
- }
-
- /**
- * Returns the methods strings for builder interface.
- *
* @param name attribute name
* @return method string for builder interface
*/
public static String parseBuilderInterfaceBuildMethodString(String name) {
- return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.BUILD, name, false)
- + getBuildForInterface(name);
+ return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.BUILD, name, false) + getBuildForInterface(name);
}
/**
@@ -62,21 +49,13 @@
* @param attr attribute info
* @return getter string
*/
- public static String getGetterString(AttributeInfo attr) {
+ public static String getGetterString(JavaAttributeInfo attr) {
- String returnType = "";
- boolean isList = attr.isListAttr();
- if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
- returnType = attr.getImportInfo().getPkgInfo() + ".";
- }
-
- returnType = returnType + attr.getImportInfo().getClassInfo();
+ String returnType = getReturnType(attr);
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
- return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.GETTER, attributeName, isList) +
-
- getGetterForInterface(attributeName, returnType, attr.isListAttr())
- + UtilConstants.NEW_LINE;
+ return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.GETTER, attributeName, attr.isListAttr()) +
+ getGetterForInterface(attributeName, returnType, attr.isListAttr());
}
/**
@@ -86,20 +65,13 @@
* @param className java class name
* @return setter string
*/
- public static String getSetterString(AttributeInfo attr, String className) {
+ public static String getSetterString(JavaAttributeInfo attr, String className) {
- String attrType = "";
- boolean isList = attr.isListAttr();
- if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
- attrType = attr.getImportInfo().getPkgInfo() + ".";
- }
-
- attrType = attrType + attr.getImportInfo().getClassInfo();
+ String attrType = getReturnType(attr);
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
- return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.SETTER, attributeName, isList)
- + getSetterForInterface(attributeName, attrType, className, attr.isListAttr())
- + UtilConstants.NEW_LINE;
+ return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.SETTER, attributeName, attr.isListAttr())
+ + getSetterForInterface(attributeName, attrType, className, attr.isListAttr());
}
/**
@@ -133,13 +105,9 @@
* @param className class name
* @return default constructor string
*/
- public static String getTypeDefConstructor(AttributeInfo attr, String className) {
+ public static String getTypeDefConstructor(JavaAttributeInfo attr, String className) {
- String attrQuaifiedType = "";
- if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
- attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
- }
- attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
+ String attrQuaifiedType = getReturnType(attr);
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
if (!attr.isListAttr()) {
@@ -160,13 +128,12 @@
private static String getTypeDefConstructorString(String type, String name, String className) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
- + className + UtilConstants.OPEN_PARENTHESIS
- + type + UtilConstants.SPACE + "value" + UtilConstants.CLOSE_PARENTHESIS
- + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE
- + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS
+ + className + UtilConstants.OPEN_PARENTHESIS + type + UtilConstants.SPACE + UtilConstants.VALUE
+ + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS
+ UtilConstants.PERIOD + name + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE
- + "value" + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
- + UtilConstants.CLOSE_CURLY_BRACKET;
+ + UtilConstants.VALUE + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
}
/**
@@ -176,6 +143,7 @@
* @return check not null string
*/
public static String getCheckNotNull(String name) {
+
return UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.CHECK_NOT_NULL_STRING
+ UtilConstants.OPEN_PARENTHESIS + name + UtilConstants.COMMA + UtilConstants.SPACE + name
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
@@ -199,20 +167,16 @@
* @param attr attribute info
* @return getter method for class
*/
- public static String getGetterForClass(AttributeInfo attr) {
+ public static String getGetterForClass(JavaAttributeInfo attr) {
- String attrQuaifiedType = "";
- if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
- attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
- }
- attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
+ String attrQuaifiedType = getReturnType(attr);
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
if (!attr.isListAttr()) {
return getGetter(attrQuaifiedType, attributeName);
}
String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
- return getGetter(listAttr, attributeName);
+ return getGetter(listAttr, attributeName + UtilConstants.SUFIX_S);
}
/**
@@ -240,19 +204,15 @@
* @param className name of the class
* @return setter method for class
*/
- public static String getSetterForClass(AttributeInfo attr, String className) {
+ public static String getSetterForClass(JavaAttributeInfo attr, String className) {
- String attrQuaifiedType = "";
- if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
- attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
- }
- attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
+ String attrQuaifiedType = getReturnType(attr);
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
if (!attr.isListAttr()) {
return getSetter(className, attributeName, attrQuaifiedType);
}
String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
- return getSetter(className, attributeName, listAttr);
+ return getSetter(className, attributeName + UtilConstants.SUFIX_S, listAttr);
}
/**
@@ -283,20 +243,16 @@
* @param attr attribute info
* @return setter method for class
*/
- public static String getSetterForTypeDefClass(AttributeInfo attr) {
+ public static String getSetterForTypeDefClass(JavaAttributeInfo attr) {
- String attrQuaifiedType = "";
- if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
- attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
- }
- attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
+ String attrQuaifiedType = getReturnType(attr);
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
if (!attr.isListAttr()) {
return getTypeDefSetter(attrQuaifiedType, attributeName);
}
String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
- return getTypeDefSetter(listAttr, attributeName);
+ return getTypeDefSetter(listAttr, attributeName + UtilConstants.SUFIX_S);
}
/**
@@ -311,12 +267,12 @@
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ UtilConstants.VOID + UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX
+ JavaIdentifierSyntax.getCaptialCase(name) + UtilConstants.OPEN_PARENTHESIS
- + type + UtilConstants.SPACE + "value" + UtilConstants.CLOSE_PARENTHESIS
+ + type + UtilConstants.SPACE + UtilConstants.VALUE + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE
+ UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS + UtilConstants.PERIOD
+ name + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE
- + "value" + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
- + UtilConstants.CLOSE_CURLY_BRACKET;
+ + UtilConstants.VALUE + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
}
/**
@@ -325,6 +281,7 @@
* @return override string
*/
public static String getOverRideString() {
+
return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.OVERRIDE + UtilConstants.NEW_LINE;
}
@@ -343,7 +300,7 @@
return getGetterInterfaceString(returnType, yangName);
}
String listAttr = getListString() + returnType + UtilConstants.DIAMOND_CLOSE_BRACKET;
- return getGetterInterfaceString(listAttr, yangName);
+ return getGetterInterfaceString(listAttr, yangName + UtilConstants.SUFIX_S);
}
/**
@@ -378,7 +335,7 @@
return getSetterInterfaceString(className, attrName, attrType);
}
String listAttr = getListString() + attrType + UtilConstants.DIAMOND_CLOSE_BRACKET;
- return getSetterInterfaceString(className, attrName, listAttr);
+ return getSetterInterfaceString(className, attrName + UtilConstants.SUFIX_S, listAttr);
}
/**
@@ -404,10 +361,27 @@
* @return list string
*/
private static String getListString() {
+
return UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET;
}
/**
+ * Returns return type for attribute.
+ *
+ * @param attr attribute info
+ * @return return type
+ */
+ private static String getReturnType(JavaAttributeInfo attr) {
+
+ String returnType = UtilConstants.EMPTY_STRING;
+ if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
+ returnType = attr.getImportInfo().getPkgInfo() + UtilConstants.PERIOD;
+ }
+ returnType = returnType + attr.getImportInfo().getClassInfo();
+ return returnType;
+ }
+
+ /**
* Returns the build method strings for interface file.
*
* @param yangName name of the interface
@@ -421,20 +395,39 @@
}
/**
+ * Returns constructor string for impl class.
+ *
+ * @param yangName class name
+ * @return constructor string
+ */
+ public static String getConstructorStart(String yangName) {
+
+ String javadoc = MethodsGenerator.getConstructorString(yangName);
+ String constructor = UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ + yangName + UtilConstants.IMPL + UtilConstants.OPEN_PARENTHESIS + yangName + UtilConstants.BUILDER
+ + UtilConstants.SPACE + UtilConstants.BUILDER.toLowerCase() + UtilConstants.OBJECT
+ + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ + UtilConstants.NEW_LINE;
+ return javadoc + constructor;
+ }
+
+ /**
* Returns the constructor strings for class file.
*
* @param yangName name of the class
* @param attr attribute info
* @return constructor for class
*/
- public static String getConstructor(String yangName, AttributeInfo attr) {
+ public static String getConstructor(String yangName, JavaAttributeInfo attr) {
- String builderAttribute = JavaIdentifierSyntax.getLowerCase(yangName);
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
+ if (attr.isListAttr()) {
+ attributeName = attributeName + UtilConstants.SUFIX_S;
+ }
String constructor = UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS
+ UtilConstants.PERIOD + JavaIdentifierSyntax.getCamelCase(attributeName)
- + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE + builderAttribute
- + UtilConstants.BUILDER + UtilConstants.OBJECT + UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX
+ + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE + UtilConstants.BUILDER.toLowerCase()
+ + UtilConstants.OBJECT + UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX
+ JavaIdentifierSyntax.getCaptialCase(JavaIdentifierSyntax.getCamelCase(attributeName))
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE;
@@ -472,7 +465,7 @@
return UtilConstants.FOUR_SPACE_INDENTATION + modifierType + UtilConstants.SPACE + name
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
- + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
+ + UtilConstants.CLOSE_CURLY_BRACKET;
}
/**
@@ -483,10 +476,10 @@
public static String getToStringMethodOpen() {
return getOverRideString() + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
- + UtilConstants.STRING + UtilConstants.SPACE + "to" + UtilConstants.STRING
+ + UtilConstants.STRING + UtilConstants.SPACE + UtilConstants.TO + UtilConstants.STRING
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
- + UtilConstants.RETURN + " MoreObjects.toStringHelper(getClass())" + UtilConstants.NEW_LINE;
+ + UtilConstants.RETURN + UtilConstants.GOOGLE_MORE_OBJECT_METHOD_STRING + UtilConstants.NEW_LINE;
}
/**
@@ -495,10 +488,11 @@
* @return to string method close string
*/
public static String getToStringMethodClose() {
- return UtilConstants.TWELVE_SPACE_INDENTATION + ".to" + UtilConstants.STRING
+
+ return UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.PERIOD + UtilConstants.TO + UtilConstants.STRING
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
- + UtilConstants.CLOSE_CURLY_BRACKET;
+ + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
}
/**
@@ -507,8 +501,12 @@
* @param attr attribute info
* @return to string method
*/
- public static String getToStringMethod(AttributeInfo attr) {
+ public static String getToStringMethod(JavaAttributeInfo attr) {
+
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
+ if (attr.isListAttr()) {
+ attributeName = attributeName + UtilConstants.SUFIX_S;
+ }
return UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.PERIOD + UtilConstants.ADD_STRING
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.QUOTES
+ attributeName + UtilConstants.QUOTES + UtilConstants.COMMA + UtilConstants.SPACE + attributeName
@@ -527,7 +525,8 @@
+ UtilConstants.INT + UtilConstants.SPACE + UtilConstants.HASH_CODE_STRING
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
- + UtilConstants.RETURN + " Objects.hash" + UtilConstants.OPEN_PARENTHESIS;
+ + UtilConstants.RETURN + UtilConstants.SPACE + UtilConstants.OBJECT_STRING + UtilConstants.SUFIX_S
+ + UtilConstants.PERIOD + UtilConstants.HASH + UtilConstants.OPEN_PARENTHESIS;
}
/**
@@ -537,10 +536,11 @@
* @return to hash code method close string
*/
public static String getHashCodeMethodClose(String hashcodeString) {
+
hashcodeString = YangIoUtils.trimAtLast(hashcodeString, UtilConstants.COMMA);
hashcodeString = YangIoUtils.trimAtLast(hashcodeString, UtilConstants.SPACE);
return hashcodeString + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
- + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
+ + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
}
/**
@@ -549,8 +549,12 @@
* @param attr attribute info
* @return hash code method
*/
- public static String getHashCodeMethod(AttributeInfo attr) {
+ public static String getHashCodeMethod(JavaAttributeInfo attr) {
+
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
+ if (attr.isListAttr()) {
+ attributeName = attributeName + UtilConstants.SUFIX_S;
+ }
return attributeName
+ UtilConstants.COMMA + UtilConstants.SPACE;
@@ -566,7 +570,7 @@
return getOverRideString() + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ UtilConstants.BOOLEAN + UtilConstants.SPACE + UtilConstants.EQUALS_STRING
- + UtilConstants.OPEN_PARENTHESIS + UtilConstants.OBJECT_STRING + UtilConstants.SPACE + "obj"
+ + UtilConstants.OPEN_PARENTHESIS + UtilConstants.OBJECT_STRING + UtilConstants.SPACE + UtilConstants.OBJ
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.NEW_LINE + getEqualsMethodsCommonIfCondition()
+ getEqualsMethodsSpecificIfCondition(className);
@@ -578,12 +582,13 @@
* @return if condition string
*/
private static String getEqualsMethodsCommonIfCondition() {
+
return UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.IF + UtilConstants.SPACE
- + UtilConstants.OPEN_PARENTHESIS + UtilConstants.THIS
- + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.EQUAL + UtilConstants.SPACE + "obj"
- + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
- + UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.RETURN
- + UtilConstants.SPACE + UtilConstants.TRUE + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ + UtilConstants.OPEN_PARENTHESIS + UtilConstants.THIS + UtilConstants.SPACE + UtilConstants.EQUAL
+ + UtilConstants.EQUAL + UtilConstants.SPACE + UtilConstants.OBJ + UtilConstants.CLOSE_PARENTHESIS
+ + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE
+ + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.RETURN + UtilConstants.SPACE
+ + UtilConstants.TRUE + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
}
@@ -594,14 +599,15 @@
* @return if condition string
*/
private static String getEqualsMethodsSpecificIfCondition(String className) {
+
return UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.IF + UtilConstants.SPACE
- + UtilConstants.OPEN_PARENTHESIS + "obj" + UtilConstants.INSTANCE_OF + className
+ + UtilConstants.OPEN_PARENTHESIS + UtilConstants.OBJ + UtilConstants.INSTANCE_OF + className
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION + className + UtilConstants.SPACE
- + "other " + UtilConstants.EQUAL + UtilConstants.SPACE + UtilConstants.OPEN_PARENTHESIS + className
- + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + "obj" + UtilConstants.SEMI_COLAN
- + UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.RETURN
- + UtilConstants.NEW_LINE;
+ + UtilConstants.OTHER + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE
+ + UtilConstants.OPEN_PARENTHESIS + className + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
+ + UtilConstants.OBJ + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.RETURN + UtilConstants.NEW_LINE;
}
/**
@@ -611,6 +617,7 @@
* @return to equals method close string
*/
public static String getEqualsMethodClose(String equalMethodString) {
+
equalMethodString = YangIoUtils.trimAtLast(equalMethodString, UtilConstants.AND);
equalMethodString = YangIoUtils.trimAtLast(equalMethodString, UtilConstants.AND);
equalMethodString = YangIoUtils.trimAtLast(equalMethodString, UtilConstants.SPACE);
@@ -621,7 +628,7 @@
+ UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN + UtilConstants.SPACE
+ UtilConstants.FALSE + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
- + UtilConstants.CLOSE_CURLY_BRACKET;
+ + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
}
/**
@@ -630,11 +637,16 @@
* @param attr attribute info
* @return equals method
*/
- public static String getEqualsMethod(AttributeInfo attr) {
+ public static String getEqualsMethod(JavaAttributeInfo attr) {
+
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
- return UtilConstants.SIXTEEN_SPACE_INDENTATION + UtilConstants.SPACE + UtilConstants.OBJECT_STRING + "s"
- + UtilConstants.PERIOD + UtilConstants.EQUALS_STRING + UtilConstants.OPEN_PARENTHESIS + attributeName
- + UtilConstants.COMMA + UtilConstants.SPACE + "other." + attributeName + UtilConstants.CLOSE_PARENTHESIS
+ if (attr.isListAttr()) {
+ attributeName = attributeName + UtilConstants.SUFIX_S;
+ }
+ return UtilConstants.SIXTEEN_SPACE_INDENTATION + UtilConstants.SPACE + UtilConstants.OBJECT_STRING
+ + UtilConstants.SUFIX_S + UtilConstants.PERIOD + UtilConstants.EQUALS_STRING
+ + UtilConstants.OPEN_PARENTHESIS + attributeName + UtilConstants.COMMA + UtilConstants.SPACE
+ + UtilConstants.OTHER + UtilConstants.PERIOD + attributeName + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SPACE + UtilConstants.AND + UtilConstants.AND;
}
@@ -646,13 +658,9 @@
* @param attr attribute info
* @return of method string
*/
- public static String getOfMethod(String name, AttributeInfo attr) {
+ public static String getOfMethod(String name, JavaAttributeInfo attr) {
- String attrQuaifiedType = "";
- if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
- attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
- }
- attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
+ String attrQuaifiedType = getReturnType(attr);
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.STATIC
+ UtilConstants.SPACE + name + UtilConstants.SPACE + UtilConstants.OF + UtilConstants.OPEN_PARENTHESIS