Bharat saraswal

Defect Fixes and optimization for YANG translator.

Change-Id: I974a968f3c41e1abea9f2567aceb3d523645d0ae
Showing 22 changed files with 724 additions and 349 deletions
......@@ -36,12 +36,12 @@ import org.sonatype.plexus.build.incremental.BuildContext;
import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES;
import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE;
import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.convertPkgToPath;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.copyYangFilesToTarget;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory;
......@@ -89,7 +89,7 @@ public class YangUtilManager extends AbstractMojo {
@Component
private BuildContext context;
private static final String DEFAULT_PKG = SLASH + convertPkgToPath(DEFAULT_BASE_PKG);
private static final String DEFAULT_PKG = SLASH + getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG);
private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
private String searchDir;
......
/*
* 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;
/**
* Type of method generated.
*/
public enum GeneratedMethodTypes {
/**
* getter method.
*/
GETTER,
/**
* setter method.
*/
SETTER,
/**
* Constructor.
*/
CONSTRUCTOR,
/**
* Build.
*/
BUILD,
/**
* Default Constructor.
*/
DEFAULT_CONSTRUCTOR
}
......@@ -284,6 +284,11 @@ public class TempJavaCodeFragmentFiles {
private YangNode curYangNode;
/**
* Is attribute added.
*/
private boolean isAttributePresent = false;
/**
* Construct an object of temporary java code fragment.
*
* @param genFileType file generation type
......@@ -684,6 +689,9 @@ public class TempJavaCodeFragmentFiles {
*/
public void setNewAttrInfo(JavaAttributeInfo newAttrInfo) {
if (newAttrInfo != null) {
isAttributePresent = true;
}
this.newAttrInfo = newAttrInfo;
}
......@@ -903,9 +911,6 @@ public class TempJavaCodeFragmentFiles {
File file = new File(path + fileName + TEMP_FILE_EXTENSION);
if (!file.exists()) {
file.createNewFile();
} else {
file.delete();
file.createNewFile();
}
return file;
}
......@@ -965,7 +970,7 @@ public class TempJavaCodeFragmentFiles {
* TODO: check if this utility needs to be called or move to the caller
*/
String attributeName = JavaIdentifierSyntax
.getCamelCase(JavaIdentifierSyntax.getLowerCase(attr.getAttributeName()));
.getCamelCase(JavaIdentifierSyntax.getSmallCase(attr.getAttributeName()));
if (attr.isQualifiedName()) {
return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), attr.getImportInfo().getClassInfo(),
attributeName, attr.isListAttr());
......@@ -1113,6 +1118,7 @@ public class TempJavaCodeFragmentFiles {
throws IOException {
setNewAttrInfo(newAttrInfo);
if (isAttributePresent) {
if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
addAttribute(newAttrInfo);
}
......@@ -1148,6 +1154,7 @@ public class TempJavaCodeFragmentFiles {
if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
addToStringMethod(newAttrInfo);
}
}
return;
}
......@@ -1193,7 +1200,7 @@ public class TempJavaCodeFragmentFiles {
setCurYangNode(curNode);
List<String> imports = new ArrayList<>();
if (curNode instanceof HasJavaImportData) {
if (curNode instanceof HasJavaImportData && isAttributePresent) {
imports = ((HasJavaImportData) curNode).getJavaImportData().getImports(getNewAttrInfo());
}
/**
......@@ -1206,13 +1213,14 @@ public class TempJavaCodeFragmentFiles {
* Create interface file.
*/
setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
setInterfaceJavaFileHandle(generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode));
setInterfaceJavaFileHandle(
generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent));
/**
* Create builder interface file.
*/
setBuilderInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
setBuilderInterfaceJavaFileHandle(
generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode));
generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode, isAttributePresent));
/**
* Append builder interface file to interface file and close it.
*/
......@@ -1221,7 +1229,7 @@ public class TempJavaCodeFragmentFiles {
}
if (curNode instanceof HasJavaImportData) {
if (curNode instanceof HasJavaImportData && isAttributePresent) {
imports.add(((HasJavaImportData) curNode).getJavaImportData().getImportForHashAndEquals());
imports.add(((HasJavaImportData) curNode).getJavaImportData().getImportForToString());
java.util.Collections.sort(imports);
......@@ -1234,12 +1242,14 @@ public class TempJavaCodeFragmentFiles {
* Create builder class file.
*/
setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
setBuilderClassJavaFileHandle(generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode));
setBuilderClassJavaFileHandle(
generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode, isAttributePresent));
/**
* Create impl class file.
*/
setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)));
setImplClassJavaFileHandle(generateImplClassFile(getImplClassJavaFileHandle(), curNode));
setImplClassJavaFileHandle(
generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent));
/**
* Append impl class to builder class and close it.
*/
......@@ -1269,46 +1279,38 @@ public class TempJavaCodeFragmentFiles {
*/
private void close() throws IOException {
closeFile(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX));
closeFile(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX));
getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)).delete();
closeFile(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX));
closeFile(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX));
getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)).delete();
closeFile(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX));
closeFile(GETTER_METHOD_FILE_NAME);
getTemporaryFileHandle(GETTER_METHOD_FILE_NAME).delete();
closeFile(GETTER_METHOD_IMPL_FILE_NAME);
getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME).delete();
closeFile(SETTER_METHOD_FILE_NAME);
getTemporaryFileHandle(SETTER_METHOD_FILE_NAME).delete();
closeFile(SETTER_METHOD_IMPL_FILE_NAME);
getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME).delete();
closeFile(CONSTRUCTOR_FILE_NAME);
getTemporaryFileHandle(CONSTRUCTOR_FILE_NAME).delete();
if ((generatedJavaFiles & INTERFACE_MASK) != 0) {
closeFile(getInterfaceJavaFileHandle(), false);
}
closeFile(ATTRIBUTE_FILE_NAME);
getTemporaryFileHandle(ATTRIBUTE_FILE_NAME).delete();
if ((generatedJavaFiles & BUILDER_CLASS_MASK) != 0) {
closeFile(getBuilderClassJavaFileHandle(), false);
}
closeFile(HASH_CODE_METHOD_FILE_NAME);
getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME).delete();
if ((generatedJavaFiles & BUILDER_INTERFACE_MASK) != 0) {
closeFile(getBuilderInterfaceJavaFileHandle(), true);
}
closeFile(TO_STRING_METHOD_FILE_NAME);
getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME).delete();
if ((generatedJavaFiles & IMPL_CLASS_MASK) != 0) {
closeFile(getImplClassJavaFileHandle(), true);
}
closeFile(EQUALS_METHOD_FILE_NAME);
getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME).delete();
if ((generatedJavaFiles & GENERATE_TYPEDEF_CLASS) != 0) {
closeFile(getTypedefClassJavaFileHandle(), false);
}
clean(getTempDirPath());
/**
* Close all temporary file handles and delete the files.
*/
closeFile(getGetterInterfaceTempFileHandle(), true);
closeFile(getGetterImplTempFileHandle(), true);
closeFile(getSetterInterfaceTempFileHandle(), true);
closeFile(getSetterImplTempFileHandle(), true);
closeFile(getConstructorImplTempFileHandle(), true);
closeFile(getAttributesTempFileHandle(), true);
closeFile(getHashCodeImplTempFileHandle(), true);
closeFile(getToStringImplTempFileHandle(), true);
closeFile(getEqualsImplTempFileHandle(), true);
}
/**
......@@ -1317,8 +1319,14 @@ public class TempJavaCodeFragmentFiles {
* @param fileName temporary file's name
* @throws IOException when failed to close the file handle
*/
private void closeFile(String fileName) throws IOException {
private void closeFile(File file, boolean toBeDeleted) throws IOException {
FileSystemUtil.updateFileHandle(new File(fileName), null, true);
if (file.exists()) {
FileSystemUtil.updateFileHandle(file, null, true);
if (toBeDeleted) {
file.delete();
}
clean(getTempDirPath());
}
}
}
......
......@@ -178,7 +178,6 @@ public class YangJavaContainer extends YangContainer
@Override
public void generateCodeExit() throws IOException {
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
}
......
......@@ -180,7 +180,6 @@ public class YangJavaInput extends YangInput
@Override
public void generateCodeExit() throws IOException {
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
}
}
......
......@@ -181,7 +181,6 @@ public class YangJavaList extends YangList
@Override
public void generateCodeExit() throws IOException {
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
}
}
......
......@@ -37,8 +37,8 @@ import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePac
* Module information extended to support java code generation.
*/
public class YangJavaModule extends YangModule
implements JavaCodeGenerator, HasJavaFileInfo,
HasJavaImportData, HasTempJavaCodeFragmentFiles {
implements JavaCodeGenerator, HasJavaFileInfo,
HasJavaImportData, HasTempJavaCodeFragmentFiles {
/**
* Contains the information of the java file being generated.
......@@ -76,7 +76,7 @@ HasJavaImportData, HasTempJavaCodeFragmentFiles {
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node.");
throw new RuntimeException("Missing java info in java datamodel node");
}
return javaFileInfo;
}
......@@ -171,7 +171,6 @@ HasJavaImportData, HasTempJavaCodeFragmentFiles {
@Override
public void generateCodeExit() throws IOException {
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
return;
}
......
......@@ -180,7 +180,6 @@ public class YangJavaOutput extends YangOutput
@Override
public void generateCodeExit() throws IOException {
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
}
}
......
......@@ -175,7 +175,6 @@ public class YangJavaTypeDef extends YangTypeDef
@Override
public void generateCodeExit() throws IOException {
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_TYPEDEF_CLASS, this);
}
......
......@@ -78,10 +78,12 @@ public final class JavaFileGenerator {
* @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,7 +91,7 @@ public final class JavaFileGenerator {
String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
initiateJavaFileGeneration(file, className, INTERFACE_MASK, imports, path);
if (isAttrPresent) {
/**
* Add getter methods to interface file.
*/
......@@ -102,6 +104,7 @@ public final class JavaFileGenerator {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while interface file generation");
}
}
return file;
}
......@@ -110,10 +113,12 @@ public final class JavaFileGenerator {
*
* @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,7 +127,7 @@ public final class JavaFileGenerator {
initiateJavaFileGeneration(file, className, BUILDER_INTERFACE_MASK, null, path);
List<String> methods = new ArrayList<>();
if (isAttrPresent) {
try {
/**
* Getter methods.
......@@ -137,7 +142,7 @@ public final class JavaFileGenerator {
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 @@ public final class JavaFileGenerator {
* @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,6 +182,7 @@ public final class JavaFileGenerator {
List<String> methods = new ArrayList<>();
if (isAttrPresent) {
/**
* Add attribute strings.
*/
......@@ -199,7 +207,9 @@ public final class JavaFileGenerator {
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 @@ public final class JavaFileGenerator {
*
* @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,7 +246,7 @@ public final class JavaFileGenerator {
initiateJavaFileGeneration(file, className, IMPL_CLASS_MASK, null, path);
List<String> methods = new ArrayList<>();
if (isAttrPresent) {
/**
* Add attribute strings.
*/
......@@ -253,6 +264,7 @@ public final class JavaFileGenerator {
* Getter methods.
*/
methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode));
/**
* Hash code method.
*/
......@@ -273,7 +285,9 @@ public final class JavaFileGenerator {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while impl class file generation");
}
} else {
insertDataIntoJavaFile(file, NEW_LINE);
}
try {
/**
* Constructor.
......
......@@ -42,6 +42,7 @@ import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.
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.BUI
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 @@ public final class JavaFileGeneratorUtils {
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()) {
......
......@@ -32,8 +32,10 @@ import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
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 @@ public final class JavaIdentifierSyntax {
*/
private static String getYangVersion(byte ver) {
return "v" + ver;
return VERSION_PREFIX + ver;
}
/**
......@@ -154,7 +156,7 @@ public final class JavaIdentifierSyntax {
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 @@ public final class JavaIdentifierSyntax {
* @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);
}
......
......@@ -20,7 +20,7 @@ import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
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 final class MethodsGenerator {
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 final class MethodsGenerator {
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 final class MethodsGenerator {
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 final class MethodsGenerator {
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 final class MethodsGenerator {
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 final class MethodsGenerator {
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 final class MethodsGenerator {
*/
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 final class MethodsGenerator {
*/
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 final class MethodsGenerator {
*/
public static String getHashCodeMethod(JavaAttributeInfo attr) {
return getLowerCase(attr.getAttributeName()) + COMMA + SPACE;
return getSmallCase(attr.getAttributeName()) + COMMA + SPACE;
}
/**
......@@ -621,7 +621,7 @@ public final class MethodsGenerator {
*/
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
......
/*
* 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
}
......@@ -32,118 +32,405 @@ public final class UtilConstants {
}
/**
* For java-docs.
* JavaDocs for impl class.
*/
public static final String IMPL_CLASS_JAVA_DOC = " * Provides the implementation of ";
/**
* JavaDocs for builder class.
*/
public static final String BUILDER_CLASS_JAVA_DOC = " * Provides the builder implementation of ";
/**
* JavaDocs for interface class.
*/
public static final String INTERFACE_JAVA_DOC = " * Abstraction of an entity which provides functionalities of ";
/**
* JavaDocs for builder interface class.
*/
public static final String BUILDER_INTERFACE_JAVA_DOC = " * Builder for ";
/**
* JavaDocs for package info class.
*/
public static final String PACKAGE_INFO_JAVADOC = " * Implementation of YANG file ";
/**
* JavaDocs's first line.
*/
public static final String JAVA_DOC_FIRST_LINE = "/**\n";
/**
* JavaDocs's last line.
*/
public static final String JAVA_DOC_END_LINE = " */\n";
/**
* JavaDocs's param annotation.
*/
public static final String JAVA_DOC_PARAM = " * @param ";
/**
* JavaDocs's return annotation.
*/
public static final String JAVA_DOC_RETURN = " * @return ";
/**
* JavaDocs's throw annotation.
*/
public static final String JAVA_DOC_THROWS = " * @throws ";
/**
* JavaDocs's description for setter method.
*/
public static final String JAVA_DOC_SETTERS = " * Returns the builder object of ";
/**
* JavaDocs's description for OF method.
*/
public static final String JAVA_DOC_OF = " * Returns the object of ";
/**
* JavaDocs's description for typedef' setter method.
*/
public static final String JAVA_DOC_SETTERS_COMMON = " * Sets the value of ";
/**
* JavaDocs's description for getter method.
*/
public static final String JAVA_DOC_GETTERS = " * Returns the attribute ";
/**
* JavaDocs's description for default constructor.
*/
public static final String JAVA_DOC_DEFAULT_CONSTRUCTOR = " * Default Constructor.\n";
/**
* JavaDocs's description for constructor.
*/
public static final String JAVA_DOC_CONSTRUCTOR = " * Construct the object of ";
/**
* JavaDocs's description for build method.
*/
public static final String JAVA_DOC_BUILD = " * Builds object of ";
/**
* JavaDocs's return statement for build method.
*/
public static final String JAVA_DOC_BUILD_RETURN = "object of ";
/**
* Basic requirements.
* JavaDocs's statement for builder object.
*/
public static final String BUILDER_OBJECT = "builder object of ";
/**
* Static attribute for new line.
*/
public static final String NEW_LINE = "\n";
/**
* Static attribute for multiple new line.
*/
public static final String MULTIPLE_NEW_LINE = "\n\n";
/**
* Static attribute for empty line.
*/
public static final String EMPTY_STRING = "";
public static final String NEW_LINE_ESTRIC = " *\n";
/**
* Static attribute for new line with asterisk.
*/
public static final String NEW_LINE_ASTERISK = " *\n";
/**
* Static attribute for period.
*/
public static final String PERIOD = ".";
/**
* Static attribute for colan.
*/
public static final String COLAN = ":";
/**
* Static attribute for underscore.
*/
public static final String UNDER_SCORE = "_";
/**
* Static attribute for semi-colan.
*/
public static final String SEMI_COLAN = ";";
/**
* Static attribute for hyphen.
*/
public static final String HYPHEN = "-";
/**
* Static attribute for space.
*/
public static final String SPACE = " ";
/**
* Static attribute for tab.
*/
public static final String TAB = "\t";
/**
* Static attribute for new line.
*/
public static final String EQUAL = "=";
/**
* Static attribute for slash syntax.
*/
public static final String SLASH = File.separator;
/**
* Static attribute for add syntax.
*/
public static final String ADD = "+";
/**
* Static attribute for asterisk.
*/
public static final String ASTERISK = "*";
/**
* Static attribute for at.
*/
public static final String AT = "@";
/**
* Static attribute for quotes.
*/
public static final String QUOTES = "\"";
/**
* Static attribute for ampersand.
*/
public static final String AND = "&";
/**
* Static attribute for comma.
*/
public static final String COMMA = ",";
/**
* Static attribute for add syntax.
*/
public static final String ADD_STRING = "add";
/**
* Static attribute for check not null syntax.
*/
public static final String CHECK_NOT_NULL_STRING = "checkNotNull";
/**
* Static attribute for hash code syntax.
*/
public static final String HASH_CODE_STRING = "hashCode";
/**
* Static attribute for equals syntax.
*/
public static final String EQUALS_STRING = "equals";
/**
* Static attribute for object.
*/
public static final String OBJECT_STRING = "Object";
/**
* Static attribute for instance of syntax.
*/
public static final String INSTANCE_OF = " instanceof ";
/**
* Static attribute for value syntax.
*/
public static final String VALUE = "value";
/**
* Static attribute for suffix s.
*/
public static final String SUFFIX_S = "s";
/**
* Static attribute for if.
*/
public static final String IF = "if";
/**
* Static attribute for for.
*/
public static final String FOR = "for";
/**
* Static attribute for while.
*/
public static final String WHILE = "while";
/**
* Static attribute for of.
*/
public static final String OF = "of";
/**
* Static attribute for other.
*/
public static final String OTHER = "other";
/**
* Static attribute for obj syntax.
*/
public static final String OBJ = "obj";
/**
* Static attribute for hash syntax.
*/
public static final String HASH = "hash";
/**
* Static attribute for to syntax.
*/
public static final String TO = "to";
/**
* Static attribute for true syntax.
*/
public static final String TRUE = "true";
/**
* Static attribute for false syntax.
*/
public static final String FALSE = "false";
/**
* Static attribute for org.
*/
public static final String ORG = "org";
/**
* Static attribute for temp.
*/
public static final String TEMP = "temp";
/**
* Static attribute for YANG file directory.
*/
public static final String YANG_RESOURCES = "yang/resources";
/**
* For brackets.
* Static attribute for diamond close bracket syntax.
*/
public static final String DIAMOND_OPEN_BRACKET = "<";
/**
* Static attribute for diamond close bracket syntax.
*/
public static final String DIAMOND_CLOSE_BRACKET = ">";
/**
* Static attribute for square open bracket syntax.
*/
public static final String SQUARE_OPEN_BRACKET = "[";
/**
* Static attribute for square close bracket syntax.
*/
public static final String SQUARE_CLOSE_BRACKET = "]";
/**
* Static attribute for open parenthesis syntax.
*/
public static final String OPEN_PARENTHESIS = "(";
/**
* Static attribute for close parenthesis syntax.
*/
public static final String CLOSE_PARENTHESIS = ")";
/**
* Static attribute for open curly bracket syntax.
*/
public static final String OPEN_CURLY_BRACKET = "{";
/**
* Static attribute for close curly bracket syntax.
*/
public static final String CLOSE_CURLY_BRACKET = "}";
/**
* For methods.
* Static attribute for getter method prefix.
*/
public static final String GET_METHOD_PREFIX = "get";
/**
* Static attribute for setter method prefix.
*/
public static final String SET_METHOD_PREFIX = "set";
/**
* For indentation.
* Static attribute for four space indentation.
*/
public static final String FOUR_SPACE_INDENTATION = " ";
/**
* Static attribute for eight space indentation.
*/
public static final String EIGHT_SPACE_INDENTATION = FOUR_SPACE_INDENTATION + FOUR_SPACE_INDENTATION;
/**
* Static attribute for twelve space indentation.
*/
public static final String TWELVE_SPACE_INDENTATION = FOUR_SPACE_INDENTATION + EIGHT_SPACE_INDENTATION;
/**
* Static attribute for sixteen space indentation.
*/
public static final String SIXTEEN_SPACE_INDENTATION = EIGHT_SPACE_INDENTATION + EIGHT_SPACE_INDENTATION;
/**
* For directories.
* Static attribute for generated code path.
*/
public static final String YANG_GEN_DIR = "src/main/java/";
/**
* Static attribute for base package.
*/
public static final String DEFAULT_BASE_PKG = "org.onosproject.yang.gen";
/**
* Static attribute for YANG date prefix.
*/
public static final String REVISION_PREFIX = "rev";
/**
* Static attribute for YANG version perifx.
*/
public static final String VERSION_PREFIX = "v";
/**
* For class modifiers.
* Static attribute for private modifier.
*/
public static final String PRIVATE = "private";
/**
* Static attribute for public modifier.
*/
public static final String PUBLIC = "public";
public static final String PROTECTED = "protected";
/**
* For data types.
* Static attribute for protected modifier.
*/
public static final String PROTECTED = "protected";
/**
* Void java type.
*/
......@@ -231,71 +518,230 @@ public final class UtilConstants {
/**
* List of keywords in java, this is used for checking if the input does not contain these keywords.
*/
public static final List JAVA_KEY_WORDS = Arrays.asList("abstract", "assert", "boolean", "break", "byte", "case",
"catch", "char", "class", "const", "continue", "default", "do", "double", "else", "extends", "false",
"final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface",
"long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static",
"strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try",
"void", "volatile", "while");
public static final List<String> JAVA_KEY_WORDS = Arrays.asList(
"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue",
"default", "do", "double", "else", "extends", "false", "final", "finally", "float", "for", "goto", "if",
"implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package",
"private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch",
"synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while");
/**
* Defining regular expression.
* Static attribute for regex for special char.
*/
public static final String REGEX_WITH_SPECIAL_CHAR = "[ : / - @ $ # ' * + , ; = ]+";
/**
* Static attribute for regex for digits.
*/
public static final String REGEX_FOR_FIRST_DIGIT = "\\d.*";
/**
* For identifiers.
* Static attribute for class syntax.
*/
public static final String CLASS = "class";
/**
* Static attribute for builder syntax.
*/
public static final String BUILDER = "Builder";
public static final String BUILDER_OBJECT = "builder object of ";
/**
* Static attribute for interface syntax.
*/
public static final String INTERFACE = "interface";
/**
* Static attribute for enum syntax.
*/
public static final String ENUM = "enum";
/**
* Static attribute for static syntax.
*/
public static final String STATIC = "static";
/**
* Static attribute for final syntax.
*/
public static final String FINAL = "final";
/**
* Static attribute for package syntax.
*/
public static final String PACKAGE = "package";
/**
* Static attribute for import syntax.
*/
public static final String IMPORT = "import ";
/**
* Static attribute for null syntax.
*/
public static final String NULL = "null";
/**
* Static attribute for return syntax.
*/
public static final String RETURN = "return";
/**
* Static attribute for java new syntax.
*/
public static final String NEW = "new";
/**
* Static attribute for this syntax.
*/
public static final String THIS = "this";
/**
* Static attribute for implements syntax.
*/
public static final String IMPLEMENTS = "implements";
/**
* Static attribute for extends syntax.
*/
public static final String EXTEND = "extends";
/**
* Static attribute for impl syntax.
*/
public static final String IMPL = "Impl";
/**
* Static attribute for build method syntax.
*/
public static final String BUILD = "build";
/**
* Static attribute for object.
*/
public static final String OBJECT = "Object";
/**
* Static attribute for override annotation.
*/
public static final String OVERRIDE = "@Override";
public static final String CHILDREN = "'s children";
/**
* For collections.
* Static attribute for new line.
*/
public static final String COLLECTION_IMPORTS = "java.util";
/**
* Static attribute for more object import package.
*/
public static final String GOOGLE_MORE_OBJECT_IMPORT_PKG = "com.google.common.base";
/**
* Static attribute for more object import class.
*/
public static final String GOOGLE_MORE_OBJECT_IMPORT_CLASS = "MoreObjects;\n";
/**
* Static attribute for to string method.
*/
public static final String GOOGLE_MORE_OBJECT_METHOD_STRING = " MoreObjects.toStringHelper(getClass())";
/**
* Static attribute for java utilities import package.
*/
public static final String JAVA_UTIL_OBJECTS_IMPORT_PKG = "java.util";
/**
* Static attribute for java utilities objects import class.
*/
public static final String JAVA_UTIL_OBJECTS_IMPORT_CLASS = "Objects;\n";
/**
* Static attribute for abstract collection.
*/
public static final String ABSTRACT_COLLECTION = "AbstractCollection";
/**
* Static attribute for list.
*/
public static final String LIST = "List";
/**
* Static attribute for linked list.
*/
public static final String LINKED_LIST = "LinkedList";
/**
* Static attribute for array list.
*/
public static final String ARRAY_LIST = "ArrayList";
/**
* Static attribute for abstract list.
*/
public static final String ABSTRACT_LIST = "AbstractList";
/**
* Static attribute for abstract sequential list.
*/
public static final String ABSTRACT_SEQUENTAIL_LIST = "AbstractSequentialList";
/**
* Static attribute for set.
*/
public static final String SET = "Set";
/**
* Static attribute for hash set.
*/
public static final String HASH_SET = "HashSet";
/**
* Static attribute for abstract set.
*/
public static final String ABSTRACT_SET = "AbstractSet";
/**
* Static attribute for linked has set.
*/
public static final String LINKED_HASH_SET = "LinkedHashSet";
/**
* Static attribute for tree set.
*/
public static final String TREE_SET = "TreeSet";
/**
* Static attribute for map.
*/
public static final String MAP = "Map";
/**
* Static attribute for abstract map.
*/
public static final String ABSTRACT_MAP = "AbstractMap";
/**
* Static attribute for hash map.
*/
public static final String HASH_MAP = "HashMap";
/**
* Static attribute for tree map.
*/
public static final String TREE_MAP = "TreeMap";
/**
* Static attribute for concurrent map.
*/
public static final String CONCURRENT_MAP = "ConcurrentMap";
/**
* Static attribute for eventually consistent map.
*/
public static final String EVENTUALLY_CONSISTENT_MAP = "EventuallyConsitentMap";
/**
* Static attribute for stack syntax.
*/
public static final String STACK = "stack";
}
......
......@@ -23,6 +23,8 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getJavaPackageFromPackagePath;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
......@@ -31,8 +33,6 @@ import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.convertPathToPkg;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.convertPkgToPath;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
/**
......@@ -54,7 +54,7 @@ public final class FileSystemUtil {
*/
public static boolean doesPackageExist(String pkg) {
File pkgDir = new File(convertPkgToPath(pkg));
File pkgDir = new File(getPackageDirPathFromJavaJPackage(pkg));
File pkgWithFile = new File(pkgDir + SLASH + "package-info.java");
if (pkgDir.exists() && pkgWithFile.isFile()) {
return true;
......@@ -74,7 +74,7 @@ public final class FileSystemUtil {
if (!doesPackageExist(pkg)) {
try {
File pack = createDirectories(pkg);
addPackageInfo(pack, pkgInfo, convertPathToPkg(pkg));
addPackageInfo(pack, pkgInfo, getJavaPackageFromPackagePath(pkg));
} catch (IOException e) {
throw new IOException("failed to create package-info file");
}
......
......@@ -41,7 +41,7 @@ import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_SETTERS;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_SETTERS_COMMON;
import static org.onosproject.yangutils.utils.UtilConstants.LIST;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE_ESTRIC;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE_ASTERISK;
import static org.onosproject.yangutils.utils.UtilConstants.OBJECT;
import static org.onosproject.yangutils.utils.UtilConstants.OF;
import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE_INFO_JAVADOC;
......@@ -141,7 +141,7 @@ public final class JavaDocGen {
*/
public static String getJavaDoc(JavaDocType type, String name, boolean isList) {
name = JavaIdentifierSyntax.getLowerCase(JavaIdentifierSyntax.getCamelCase(name));
name = JavaIdentifierSyntax.getSmallCase(JavaIdentifierSyntax.getCamelCase(name));
String javaDoc = UtilConstants.EMPTY_STRING;
if (type.equals(JavaDocType.IMPL_CLASS)) {
javaDoc = generateForImplClass(name);
......@@ -183,7 +183,7 @@ public final class JavaDocGen {
private static String generateForGetters(String attribute, boolean isList) {
String getter = NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION
+ JAVA_DOC_GETTERS + attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ESTRIC
+ JAVA_DOC_GETTERS + attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ASTERISK
+ FOUR_SPACE_INDENTATION + JAVA_DOC_RETURN;
if (isList) {
String listAttribute = LIST.toLowerCase() + SPACE + OF + SPACE;
......@@ -206,7 +206,7 @@ public final class JavaDocGen {
private static String generateForSetters(String attribute, boolean isList) {
String setter = NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION
+ JAVA_DOC_SETTERS + attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ESTRIC
+ JAVA_DOC_SETTERS + attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ASTERISK
+ FOUR_SPACE_INDENTATION + JAVA_DOC_PARAM + attribute + SPACE;
if (isList) {
String listAttribute = LIST.toLowerCase() + SPACE + OF + SPACE;
......@@ -228,7 +228,7 @@ public final class JavaDocGen {
private static String generateForOf(String attribute) {
return NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_OF
+ attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ESTRIC + FOUR_SPACE_INDENTATION
+ attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ASTERISK + FOUR_SPACE_INDENTATION
+ JAVA_DOC_PARAM + VALUE + SPACE + VALUE + SPACE + OF + SPACE + attribute + NEW_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_RETURN + OBJECT + SPACE + OF + SPACE + attribute + NEW_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
......@@ -243,7 +243,7 @@ public final class JavaDocGen {
private static String generateForTypeDefSetter(String attribute) {
return (NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION
+ JAVA_DOC_SETTERS_COMMON + attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ESTRIC
+ JAVA_DOC_SETTERS_COMMON + attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ASTERISK
+ FOUR_SPACE_INDENTATION + JAVA_DOC_PARAM + VALUE + SPACE + VALUE + SPACE + OF + SPACE + attribute
+ NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE);
}
......@@ -257,7 +257,7 @@ public final class JavaDocGen {
private static String generateForTypeDefConstructor(String attribute) {
return (NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_CONSTRUCTOR
+ attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ESTRIC + FOUR_SPACE_INDENTATION
+ attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ASTERISK + FOUR_SPACE_INDENTATION
+ JAVA_DOC_PARAM + VALUE + SPACE + VALUE + SPACE + OF + SPACE + attribute + NEW_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE);
}
......@@ -341,8 +341,8 @@ public final class JavaDocGen {
return NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_CONSTRUCTOR + className + IMPL + PERIOD + NEW_LINE
+ FOUR_SPACE_INDENTATION + NEW_LINE_ESTRIC + FOUR_SPACE_INDENTATION + JAVA_DOC_PARAM
+ BUILDER.toLowerCase() + OBJECT + SPACE + BUILDER_OBJECT + SPACE + className + NEW_LINE
+ FOUR_SPACE_INDENTATION + NEW_LINE_ASTERISK + FOUR_SPACE_INDENTATION + JAVA_DOC_PARAM
+ BUILDER.toLowerCase() + OBJECT + SPACE + BUILDER_OBJECT + className + NEW_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
}
......@@ -355,7 +355,7 @@ public final class JavaDocGen {
private static String generateForBuild(String buildName) {
return NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_BUILD
+ buildName + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ESTRIC + FOUR_SPACE_INDENTATION
+ buildName + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ASTERISK + FOUR_SPACE_INDENTATION
+ JAVA_DOC_RETURN + JAVA_DOC_BUILD_RETURN + buildName + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION
+ JAVA_DOC_END_LINE;
}
......
......@@ -45,7 +45,7 @@ public final class YangFileScanner {
* @throws IOException when files get deleted while performing the
* operations
*/
public static List<String> getJavaFiles(String root) throws NullPointerException, IOException {
public static List<String> getJavaFiles(String root) throws IOException {
return getFiles(root, JAVA_FILE_EXTENTION);
}
......@@ -59,7 +59,7 @@ public final class YangFileScanner {
* @throws IOException when files get deleted while performing the
* operations
*/
public static List<String> getYangFiles(String root) throws NullPointerException, IOException {
public static List<String> getYangFiles(String root) throws IOException {
return getFiles(root, YANG_FILE_EXTENTION);
}
......@@ -73,7 +73,7 @@ public final class YangFileScanner {
* @throws NullPointerException when no file is there
* @throws IOException when files get deleted while performing the operations
*/
public static List<String> getFiles(String root, String extension) throws NullPointerException, IOException {
public static List<String> getFiles(String root, String extension) throws IOException {
List<String> store = new LinkedList<>();
Stack<String> stack = new Stack<>();
......@@ -100,8 +100,8 @@ public final class YangFileScanner {
}
}
return store;
} catch (NullPointerException e) {
throw new IOException("NullPointerException occured");
} catch (IOException e) {
throw new IOException("No File found of " + extension + " extension in " + root + " directory.");
}
}
}
......
......@@ -36,7 +36,6 @@ import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
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;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
......@@ -294,26 +293,4 @@ public final class YangIoUtils {
throw new IOException("Failed to insert in " + file + "file");
}
}
/**
* Convert directory path in java package format.
*
* @param path directory path
* @return java package
*/
public static String convertPathToPkg(String path) {
return path.replace(SLASH, PERIOD);
}
/**
* Convert java package in directory path format.
*
* @param pkg java package
* @return directory path
*/
public static String convertPkgToPath(String pkg) {
return pkg.replace(PERIOD, SLASH);
}
}
......
......@@ -16,24 +16,39 @@
package org.onosproject.yangutils.translator.tojava.utils;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType;
import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
import org.onosproject.yangutils.translator.tojava.TraversalType;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
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.utils.ClassDefinitionGenerator.generateClassDefinition;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.FINAL;
import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS;
import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
/**
* Unit tests for class definition generator for generated files.
*/
public final class ClassDefinitionGeneratorTest {
private static final String CLASS_NAME = "testclass";
/**
* Unit test for private constructor.
*
......@@ -62,10 +77,10 @@ public final class ClassDefinitionGeneratorTest {
@Test
public void generateBuilderClassDefinitionTest() {
String builderClassDefinition = ClassDefinitionGenerator
.generateClassDefinition(GeneratedJavaFileType.BUILDER_CLASS_MASK, "BuilderClass");
assertThat(true, is(builderClassDefinition.contains(UtilConstants.BUILDER)));
assertThat(true, is(builderClassDefinition.contains(UtilConstants.CLASS)));
String builderClassDefinition = generateClassDefinition(BUILDER_CLASS_MASK, CLASS_NAME);
assertThat(true, is(builderClassDefinition.equals(
PUBLIC + SPACE + CLASS + SPACE + CLASS_NAME + BUILDER + SPACE + IMPLEMENTS + SPACE + CLASS_NAME + PERIOD
+ CLASS_NAME + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
}
/**
......@@ -74,9 +89,9 @@ public final class ClassDefinitionGeneratorTest {
@Test
public void generateBuilderInterfaceDefinitionTest() {
String builderInterfaceDefinition = ClassDefinitionGenerator
.generateClassDefinition(GeneratedJavaFileType.BUILDER_INTERFACE_MASK, "BuilderInterfaceClass");
assertThat(true, is(builderInterfaceDefinition.contains(UtilConstants.BUILDER)));
String builderInterfaceDefinition = generateClassDefinition(BUILDER_INTERFACE_MASK, CLASS_NAME);
assertThat(true, is(builderInterfaceDefinition
.equals(INTERFACE + SPACE + CLASS_NAME + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE)));
}
/**
......@@ -85,9 +100,10 @@ public final class ClassDefinitionGeneratorTest {
@Test
public void generateImplDefinitionTest() {
String implDefinition = ClassDefinitionGenerator.generateClassDefinition(GeneratedJavaFileType.IMPL_CLASS_MASK,
"ImplClass");
assertThat(true, is(implDefinition.contains(UtilConstants.IMPL)));
String implDefinition = generateClassDefinition(IMPL_CLASS_MASK, CLASS_NAME);
assertThat(true, is(implDefinition.equals(
PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + CLASS_NAME + IMPL + SPACE + IMPLEMENTS + SPACE
+ CLASS_NAME + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
}
/**
......@@ -96,10 +112,9 @@ public final class ClassDefinitionGeneratorTest {
@Test
public void generateinterfaceDefinitionTest() {
String interfaceDefinition = ClassDefinitionGenerator.generateClassDefinition(
GeneratedJavaFileType.INTERFACE_MASK,
"InterfaceClass");
assertThat(true, is(interfaceDefinition.contains(UtilConstants.INTERFACE)));
String interfaceDefinition = generateClassDefinition(INTERFACE_MASK, CLASS_NAME);
assertThat(true, is(interfaceDefinition
.equals(PUBLIC + SPACE + INTERFACE + SPACE + CLASS_NAME + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
}
/**
......@@ -108,19 +123,8 @@ public final class ClassDefinitionGeneratorTest {
@Test
public void generateTypeDefTest() {
String typeDef = ClassDefinitionGenerator.generateClassDefinition(GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS,
"invalid");
assertThat(true, is(typeDef.contains(UtilConstants.CLASS)));
}
/**
* Unit test for enum data types.
*/
@Test
public void enumDataTypesTest() {
TraversalType.valueOf(TraversalType.CHILD.toString());
GeneratedMethodTypes.valueOf(GeneratedMethodTypes.CONSTRUCTOR.toString());
TempDataStoreTypes.valueOf(TempDataStoreTypes.CONSTRUCTOR.toString());
String typeDef = generateClassDefinition(GENERATE_TYPEDEF_CLASS, CLASS_NAME);
assertThat(true, is(typeDef.equals(
PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + CLASS_NAME + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
}
}
......
......@@ -16,38 +16,47 @@
package org.onosproject.yangutils.translator.tojava.utils;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
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.getJavaPackageFromPackagePath;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getYangRevisionStr;
import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
/**
* Unit tests for java identifier syntax.
*/
public final class JavaIdentifierSyntaxTest {
public static final String PARENT_PACKAGE = "test5.test6.test7";
public static final String CHILD_PACKAGE = "test1:test2:test3";
public static final String DATE1 = "2000-1-5";
public static final String DATE2 = "1992-01-25";
public static final String PARENT_WITH_PERIOD = "test5.test6.test7";
public static final String CHILD_WITH_PERIOD = "test1.test2.test3";
public static final String DATE_WITH_REV1 = "rev20000105";
public static final String DATE_WITH_REV2 = "rev19920125";
public static final String VERSION_NUMBER = "v1";
public static final String INVALID_NAME_SPACE1 = "byte:#test2:9test3";
public static final String INVALID_NAME_SPACE2 = "const:#test2://9test3";
public static final String VALID_NAME_SPACE1 = "_byte.test2._9test3";
public static final String VALID_NAME_SPACE2 = "_const.test2._9test3";
public static final String WITHOUT_CAMEL_CASE = "test-camel-case-identifier";
public static final String WITH_CAMEL_CASE = "testCamelCaseIdentifier";
public static final String WITHOUT_CAPITAL = "test_this";
public static final String WITH_CAPITAL = "Test_this";
private static final String PARENT_PACKAGE = "test5/test6/test7";
private static final String CHILD_PACKAGE = "test1:test2:test3";
private static final String DATE1 = "2000-1-5";
private static final String DATE2 = "1992-01-25";
private static final String PARENT_WITH_PERIOD = "test5.test6.test7";
private static final String CHILD_WITH_PERIOD = "test1.test2.test3";
private static final String DATE_WITH_REV1 = "rev20000105";
private static final String DATE_WITH_REV2 = "rev19920125";
private static final String VERSION_NUMBER = "v1";
private static final String INVALID_NAME_SPACE1 = "byte:#test2:9test3";
private static final String INVALID_NAME_SPACE2 = "const:#test2://9test3";
private static final String VALID_NAME_SPACE1 = "_byte.test2._9test3";
private static final String VALID_NAME_SPACE2 = "_const.test2._9test3";
private static final String WITHOUT_CAMEL_CASE = "test-camel-case-identifier";
private static final String WITH_CAMEL_CASE = "testCamelCaseIdentifier";
private static final String WITHOUT_CAPITAL = "test_this";
private static final String WITH_CAPITAL = "Test_this";
private static final String WITH_SMALL = "test_this";
/**
* Unit test for private constructor.
......@@ -65,6 +74,7 @@ public final class JavaIdentifierSyntaxTest {
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] classesToConstruct = {JavaIdentifierSyntax.class };
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
......@@ -79,9 +89,9 @@ public final class JavaIdentifierSyntaxTest {
@Test
public void getRootPackageTest() {
String rootPackage = JavaIdentifierSyntax.getRootPackage((byte) 1, CHILD_PACKAGE, DATE1);
assertThat(rootPackage.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER
+ UtilConstants.PERIOD + CHILD_WITH_PERIOD + UtilConstants.PERIOD + DATE_WITH_REV1), is(true));
String rootPackage = getRootPackage((byte) 1, CHILD_PACKAGE, DATE1);
assertThat(rootPackage.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
+ PERIOD + CHILD_WITH_PERIOD + PERIOD + DATE_WITH_REV1), is(true));
}
/**
......@@ -90,12 +100,12 @@ public final class JavaIdentifierSyntaxTest {
@Test
public void getRootPackageWithSpecialCharactersTest() {
String rootPackage = JavaIdentifierSyntax.getRootPackage((byte) 1, INVALID_NAME_SPACE1, DATE1);
assertThat(rootPackage.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER
+ UtilConstants.PERIOD + VALID_NAME_SPACE1 + UtilConstants.PERIOD + DATE_WITH_REV1), is(true));
String rootPackage1 = JavaIdentifierSyntax.getRootPackage((byte) 1, INVALID_NAME_SPACE2, DATE1);
assertThat(rootPackage1.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER
+ UtilConstants.PERIOD + VALID_NAME_SPACE2 + UtilConstants.PERIOD + DATE_WITH_REV1), is(true));
String rootPackage = getRootPackage((byte) 1, INVALID_NAME_SPACE1, DATE1);
assertThat(rootPackage.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
+ PERIOD + VALID_NAME_SPACE1 + PERIOD + DATE_WITH_REV1), is(true));
String rootPackage1 = getRootPackage((byte) 1, INVALID_NAME_SPACE2, DATE1);
assertThat(rootPackage1.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
+ PERIOD + VALID_NAME_SPACE2 + PERIOD + DATE_WITH_REV1), is(true));
}
/**
......@@ -104,9 +114,9 @@ public final class JavaIdentifierSyntaxTest {
@Test
public void getRootPackageWithRevTest() {
String rootPkgWithRev = JavaIdentifierSyntax.getRootPackage((byte) 1, CHILD_PACKAGE, DATE2);
assertThat(rootPkgWithRev.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD
+ VERSION_NUMBER + UtilConstants.PERIOD + CHILD_WITH_PERIOD + UtilConstants.PERIOD + DATE_WITH_REV2),
String rootPkgWithRev = getRootPackage((byte) 1, CHILD_PACKAGE, DATE2);
assertThat(rootPkgWithRev.equals(
DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER + PERIOD + CHILD_WITH_PERIOD + PERIOD + DATE_WITH_REV2),
is(true));
}
......@@ -116,7 +126,7 @@ public final class JavaIdentifierSyntaxTest {
@Test
public void getCapitalCaseTest() {
String capitalCase = JavaIdentifierSyntax.getCaptialCase(WITHOUT_CAPITAL);
String capitalCase = getCaptialCase(WITHOUT_CAPITAL);
assertThat(capitalCase.equals(WITH_CAPITAL), is(true));
}
......@@ -125,7 +135,48 @@ public final class JavaIdentifierSyntaxTest {
*/
@Test
public void getCamelCaseTest() {
String camelCase = JavaIdentifierSyntax.getCamelCase(WITHOUT_CAMEL_CASE);
String camelCase = getCamelCase(WITHOUT_CAMEL_CASE);
assertThat(camelCase.equals(WITH_CAMEL_CASE), is(true));
}
/**
* Unit test for getting the camel case for the received string.
*/
@Test
public void getSmallCaseTest() {
String smallCase = getSmallCase(WITHOUT_CAPITAL);
assertThat(smallCase.equals(WITH_SMALL), is(true));
}
/**
* Unit test for getting the camel case for the received string.
*/
@Test
public void getPackageFromPathTest() {
String pkg = getJavaPackageFromPackagePath(PARENT_PACKAGE);
assertThat(pkg.equals(PARENT_WITH_PERIOD), is(true));
}
/**
* Unit test for getting the camel case for the received string.
*/
@Test
public void getPathFromPackageTest() {
String path = getPackageDirPathFromJavaJPackage(PARENT_WITH_PERIOD);
assertThat(path.equals(PARENT_PACKAGE), is(true));
}
/**
* Unit test for getting the camel case for the received string.
*/
@Test
public void getYangRevTest() {
String rev = getYangRevisionStr(DATE1);
assertThat(rev.equals(DATE_WITH_REV1), is(true));
}
}
......
......@@ -16,20 +16,20 @@
package org.onosproject.yangutils.utils.io.impl;
import org.junit.Test;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.slf4j.Logger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.slf4j.LoggerFactory.getLogger;
/**
......@@ -151,17 +151,16 @@ public final class YangFileScannerTest {
/**
* This test case checks with the sub directories in the given path for java files.
*/
@Test
public void exceptionHandleTest() throws IOException {
String dir = baseDir + File.separator + "scanner4";
thrown.expect(IOException.class);
thrown.expectMessage("NullPointerException occured");
List<String> invalidContents = YangFileScanner.getJavaFiles(dir);
File path = createDirectory(dir);
createFile(path, "except.java");
List<String> dirWithFileName = YangFileScanner
.getJavaFiles(path + File.separator + "except.java" + File.separator + "scanner5");
}
}*/
}
......