removed code duplication in translator, and addressed review comments
Change-Id: I27767a81c4bf279c80d2b98192f75f8f507b4457
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
index 3ffc5e2..e2e6211 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
@@ -16,12 +16,7 @@
package org.onosproject.yangutils.translator.tojava.utils;
-import java.util.List;
-
-import org.onosproject.yangutils.datamodel.YangType;
-import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.AttributeInfo;
-import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
@@ -30,45 +25,6 @@
*/
public final class MethodsGenerator {
- private static String builderClassName;
- private static List<AttributeInfo> attrInfo;
-
- /**
- * Sets the builder class name for setter methods of builder class.
- *
- * @param name builder class name
- */
- public static void setBuilderClassName(String name) {
- builderClassName = name;
- }
-
- /**
- * Sets the attribute info for the impl class's constructor method.
- *
- * @param attr list of attribute info
- */
- public static void setAttrInfo(List<AttributeInfo> attr) {
- attrInfo = attr;
- }
-
- /**
- * Returns attribute info for the impl class's constructor method.
- *
- * @return list of attribute info
- */
- public static List<AttributeInfo> getAttrInfo() {
- return attrInfo;
- }
-
- /**
- * Return the class name.
- *
- * @return class name
- */
- public static String getBuilderClassName() {
- return builderClassName;
- }
-
/**
* Default constructor.
*/
@@ -76,181 +32,106 @@
}
/**
- * Return method strings.
- *
- * @param attr attribute info.
- * @param type generated file type
- * @return method string
- */
- public static String getMethodString(AttributeInfo attr, GeneratedFileType type) {
-
- if (type.equals(GeneratedFileType.BUILDER_CLASS)) {
-
- return parseBuilderMethodString(attr);
- } else if (type.equals(GeneratedFileType.INTERFACE)) {
-
- return getGetterString(attr);
- } else if (type.equals(GeneratedFileType.BUILDER_INTERFACE)) {
-
- return parseBuilderInterfaceMethodString(attr);
- } else if (type.equals(GeneratedFileType.IMPL)) {
-
- return parseImplMethodString(attr);
- }
- return null;
- }
-
- /**
- * Returns constructed method impl for specific generated file type.
- *
- * @param genFileTypes generated file type
- * @param yangName class name
- * @param methodTypes method types
- * @param returnType return type of method
- * @return constructed method impl
- */
- public static String constructMethodInfo(GeneratedFileType genFileTypes, String yangName,
- GeneratedMethodTypes methodTypes, YangType<?> returnType) {
-
- if (returnType == null) {
- YangType<?> type = new YangType();
- type.setDataTypeName(yangName);
- returnType = type;
- }
-
- if (genFileTypes.equals(GeneratedFileType.INTERFACE)) {
-
- /**
- * If interface, only getter will be there.
- */
- return getGetterForInterface(yangName, returnType);
- } else if (genFileTypes.equals(GeneratedFileType.BUILDER_INTERFACE)) {
-
- /**
- * If builder interface, getters and setters will be there.
- */
- if (methodTypes.equals(GeneratedMethodTypes.GETTER)) {
-
- return getGetterForInterface(yangName, returnType);
- } else if (methodTypes.equals(GeneratedMethodTypes.SETTER)) {
-
- return getSetterForInterface(yangName, returnType);
- } else if (methodTypes.equals(GeneratedMethodTypes.BUILD)) {
-
- return getBuildForInterface(yangName);
- }
- } else if (genFileTypes.equals(GeneratedFileType.BUILDER_CLASS)) {
-
- /**
- * If Builder class , getters, setters ,build and default constructor impls will be there.
- */
- if (methodTypes.equals(GeneratedMethodTypes.GETTER)) {
-
- return getGetterForClass(yangName, returnType);
- } else if (methodTypes.equals(GeneratedMethodTypes.SETTER)) {
-
- return getSetterForClass(yangName, returnType);
- } else if (methodTypes.equals(GeneratedMethodTypes.BUILD)) {
-
- return getBuild(yangName);
- } else if (methodTypes.equals(GeneratedMethodTypes.DEFAULT_CONSTRUCTOR)) {
-
- return getDefaultConstructor(yangName + UtilConstants.BUILDER);
- }
- } else if (genFileTypes.equals(GeneratedFileType.IMPL)) {
-
- if (methodTypes.equals(GeneratedMethodTypes.GETTER)) {
-
- return getGetterForClass(yangName, returnType);
- } else if (methodTypes.equals(GeneratedMethodTypes.CONSTRUCTOR)) {
-
- return getConstructor(yangName);
- } else if (methodTypes.equals(GeneratedMethodTypes.DEFAULT_CONSTRUCTOR)) {
-
- return getDefaultConstructor(yangName + UtilConstants.IMPL);
- }
- }
- return null;
- }
-
- /**
* Returns the methods strings for builder class.
*
- * @param attr attribute info.
- * @return method string for builder class.
+ * @param attr attribute info
+ * @param className java class name
+ * @return method string for builder class
*/
- private static String parseBuilderMethodString(AttributeInfo attr) {
+ static String parseBuilderMethodString(AttributeInfo attr, String className) {
+ String attrQuaifiedType = "";
+ if (attr.getImportInfo().getPkgInfo() != null) {
+ attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
+ }
+ attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
- String overrideString = UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE
- + UtilConstants.NEW_LINE;
- String getterString = JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
- attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType());
- String setterString = JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
- attr.getAttributeName(), GeneratedMethodTypes.SETTER, attr.getAttributeType());
+ String overrideString = UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ + UtilConstants.OVERRIDE + UtilConstants.NEW_LINE;
+ String getterString = getGetterForClass(attr.getAttributeName(), attrQuaifiedType);
+ String setterString = getSetterForClass(attr.getAttributeName(), attrQuaifiedType, className);
return overrideString + getterString + UtilConstants.NEW_LINE + overrideString + setterString;
}
/**
* Returns the methods strings for builder class.
*
- * @param attr attribute info.
- * @return method string for builder class.
+ * @param attr attribute info
+ * @return method string for builder class
*/
- private static String parseImplMethodString(AttributeInfo attr) {
+ static String parseImplMethodString(AttributeInfo attr) {
+
+ String attrQuaifiedType = "";
+ if (attr.getImportInfo().getPkgInfo() != null) {
+ attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
+ }
+ attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE
- + UtilConstants.NEW_LINE + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
- attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType());
+ + UtilConstants.NEW_LINE + getGetterForClass(attr.getAttributeName(), attrQuaifiedType);
}
/**
* Returns the methods strings for builder interface.
*
- * @param attr attribute info.
- * @return method string for builder interface.
+ * @param attr attribute info
+ * @param className name of the java class being generated
+ * @return method string for builder interface
*/
- private static String parseBuilderInterfaceMethodString(AttributeInfo attr) {
+ static String parseBuilderInterfaceMethodString(AttributeInfo attr, String className) {
- return getGetterString(attr) + UtilConstants.NEW_LINE + getSetterString(attr);
+ 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.
+ * @param name attribute name
+ * @return method string for builder interface
*/
public static String parseBuilderInterfaceBuildMethodString(String name) {
- return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.BUILD, name) + JavaCodeSnippetGen
- .getJavaMethodInfo(GeneratedFileType.BUILDER_INTERFACE, name, GeneratedMethodTypes.BUILD, null);
+ return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.BUILD, name)
+ + getBuildForInterface(name);
}
/**
* Returns getter string.
*
- * @param attr attribute info.
+ * @param attr attribute info
* @return getter string
*/
public static String getGetterString(AttributeInfo attr) {
+ String returnType = "";
+ if (attr.getImportInfo().getPkgInfo() != null) {
+ returnType = attr.getImportInfo().getPkgInfo() + ".";
+ }
+
+ returnType = returnType + attr.getImportInfo().getClassInfo();
+
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.GETTER, attr.getAttributeName())
- + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.INTERFACE, attr.getAttributeName(),
- GeneratedMethodTypes.GETTER, attr.getAttributeType())
+ + getGetterForInterface(attr.getAttributeName(), returnType)
+ UtilConstants.NEW_LINE;
}
/**
* Returns setter string.
*
- * @param attr attribute info.
+ * @param attr attribute info
+ * @param className java class name
* @return setter string
*/
- private static String getSetterString(AttributeInfo attr) {
+ private static String getSetterString(AttributeInfo attr, String className) {
+
+ String attrType = "";
+ if (attr.getImportInfo().getPkgInfo() != null) {
+ attrType = attr.getImportInfo().getPkgInfo() + ".";
+ }
+
+ attrType = attrType + attr.getImportInfo().getClassInfo();
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.SETTER, attr.getAttributeName())
- + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_INTERFACE, attr.getAttributeName(),
- GeneratedMethodTypes.SETTER, attr.getAttributeType());
+ + getSetterForInterface(attr.getAttributeName(), attrType, className);
}
/**
@@ -261,8 +142,8 @@
*/
public static String getConstructorString(String name) {
- return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.CONSTRUCTOR, name) + JavaCodeSnippetGen
- .getJavaMethodInfo(GeneratedFileType.IMPL, name, GeneratedMethodTypes.CONSTRUCTOR, null);
+ return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.CONSTRUCTOR, name)
+ + getConstructor(name);
}
/**
@@ -272,10 +153,10 @@
* @param name class name
* @return default constructor string
*/
- public static String getDefaultConstructorString(GeneratedFileType type, String name) {
+ public static String getDefaultConstructorString(int type, String name) {
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR, name)
- + JavaCodeSnippetGen.getJavaMethodInfo(type, name, GeneratedMethodTypes.DEFAULT_CONSTRUCTOR, null);
+ + getDefaultConstructor(name + UtilConstants.BUILDER);
}
/**
@@ -287,83 +168,85 @@
public static String getBuildString(String name) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE + UtilConstants.NEW_LINE
- + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS, name,
- GeneratedMethodTypes.BUILD, null);
+ + getBuild(name);
}
/**
* Returns the getter method strings for class file.
*
- * @param yangName name of the attribute.
- * @param returnType return type of attribute
- * @return getter method for class.
+ * @param attrName name of the attribute
+ * @param attrType return type of attribute
+ * @return getter method for class
*/
- private static String getGetterForClass(String yangName, YangType<?> returnType) {
+ private static String getGetterForClass(String attrName, String attrType) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
- + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName()) + UtilConstants.SPACE
- + UtilConstants.GET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
- + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
- + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
- + UtilConstants.RETURN + UtilConstants.SPACE + yangName + UtilConstants.SEMI_COLAN
- + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
+ + attrType + UtilConstants.SPACE + UtilConstants.GET_METHOD_PREFIX
+ + JavaIdentifierSyntax.getCaptialCase(attrName) + UtilConstants.OPEN_PARENTHESIS
+ + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN
+ + UtilConstants.SPACE + attrName + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
}
/**
* Returns the setter method strings for class file.
*
- * @param yangName name of the attribute.
- * @param returnType return type of attribute
- * @return setter method for class.
+ * @param attrName name of the attribute
+ * @param attrType return type of attribute
+ * @param className name of the class
+ * @return setter method for class
*/
- private static String getSetterForClass(String yangName, YangType<?> returnType) {
+ private static String getSetterForClass(String attrName, String attrType, String className) {
- return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + getBuilderClassName()
- + UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
- + UtilConstants.OPEN_PARENTHESIS + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName())
- + UtilConstants.SPACE + yangName + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
- + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
- + UtilConstants.THIS + UtilConstants.PERIOD + yangName + UtilConstants.SPACE + UtilConstants.EQUAL
- + UtilConstants.SPACE + yangName + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
- + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN + UtilConstants.SPACE
- + UtilConstants.THIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
- + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
+ return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ + className + UtilConstants.BUILDER + UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX
+ + JavaIdentifierSyntax.getCaptialCase(attrName) + UtilConstants.OPEN_PARENTHESIS
+ + attrType + UtilConstants.SPACE + attrName + UtilConstants.CLOSE_PARENTHESIS
+ + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE
+ + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS + UtilConstants.PERIOD
+ + attrName + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE + attrName
+ + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
+ + UtilConstants.RETURN + UtilConstants.SPACE + UtilConstants.THIS + UtilConstants.SEMI_COLAN
+ + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
}
/**
* Returns the getter method strings for interface file.
*
- * @param yangName name of the attribute.
+ * @param yangName name of the attribute
* @param returnType return type of attribute
- * @return getter method for interface.
+ * @return getter method for interface
*/
- private static String getGetterForInterface(String yangName, YangType<?> returnType) {
- returnType.setDataTypeName(returnType.getDataTypeName().replace("\"", ""));
- return UtilConstants.FOUR_SPACE_INDENTATION + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName())
- + UtilConstants.SPACE + UtilConstants.GET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
- + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN;
+ private static String getGetterForInterface(String yangName, String returnType) {
+ return UtilConstants.FOUR_SPACE_INDENTATION + returnType
+ + UtilConstants.SPACE + UtilConstants.GET_METHOD_PREFIX
+ + JavaIdentifierSyntax.getCaptialCase(yangName)
+ + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS
+ + UtilConstants.SEMI_COLAN;
}
/**
* Returns the setter method strings for interface file.
*
- * @param yangName name of the attribute.
- * @param returnType return type of attribute
- * @return setter method for interface.
+ * @param attrName name of the attribute
+ * @param attrType return type of attribute
+ * @param className name of the java class being generated
+ * @return setter method for interface
*/
- private static String getSetterForInterface(String yangName, YangType<?> returnType) {
- return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.BUILDER + UtilConstants.SPACE
- + UtilConstants.SET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
- + UtilConstants.OPEN_PARENTHESIS + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName())
- + UtilConstants.SPACE + yangName + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN;
+ private static String getSetterForInterface(String attrName, String attrType, String className) {
+ return UtilConstants.FOUR_SPACE_INDENTATION + className + UtilConstants.BUILDER
+ + UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX
+ + JavaIdentifierSyntax.getCaptialCase(attrName) + UtilConstants.OPEN_PARENTHESIS
+ + attrType + UtilConstants.SPACE + attrName + UtilConstants.CLOSE_PARENTHESIS
+ + UtilConstants.SEMI_COLAN;
}
/**
* Returns the build method strings for interface file.
*
- * @param yangName name of the attribute.
- * @param returnType return type of attribute
- * @return build method for interface.
+ * @param yangName name of the interface
+ * @return build method for interface
*/
private static String getBuildForInterface(String yangName) {
@@ -374,30 +257,33 @@
/**
* Returns the constructor strings for class file.
*
- * @param yangName name of the class.
+ * @param yangName name of the class
* @return constructor for class
*/
private static String getConstructor(String yangName) {
- String builderAttribute = (yangName.substring(0, 1).toLowerCase() + yangName.substring(1));
+ String builderAttribute = yangName.substring(0, 1).toLowerCase() + yangName.substring(1);
String constructor = UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ yangName + UtilConstants.IMPL + UtilConstants.OPEN_PARENTHESIS + yangName + UtilConstants.BUILDER
+ UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
- if (getAttrInfo() != null) {
- for (AttributeInfo attribute : getAttrInfo()) {
- attribute.setAttributeName(JavaIdentifierSyntax.getCamelCase(attribute.getAttributeName()));
- constructor = constructor + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.THIS
- + UtilConstants.PERIOD + attribute.getAttributeName() + UtilConstants.SPACE
- + UtilConstants.EQUAL + UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT
- + UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX
- + JavaIdentifierSyntax.getCaptialCase(attribute.getAttributeName())
- + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
- + UtilConstants.NEW_LINE;
- }
- getAttrInfo().clear();
- }
+ // TODO: need to get the partial constructor from constructor temp file.
+ // if (getAttrInfo() != null) {
+ // for (AttributeInfo attribute : getAttrInfo()) {
+ // attribute.setAttributeName(JavaIdentifierSyntax.getCamelCase(attribute.getAttributeName()));
+ // constructor = constructor + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.THIS
+ // + UtilConstants.PERIOD + attribute.getAttributeName() + UtilConstants.SPACE
+ // + UtilConstants.EQUAL + UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT
+ // + UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX
+ // + JavaIdentifierSyntax.getCaptialCase(attribute.getAttributeName())
+ // + UtilConstants.OPEN_PARENTHESIS
+ // + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
+ // + UtilConstants.NEW_LINE;
+ // }
+ // getAttrInfo().clear();
+ // }
+
return constructor + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
}
@@ -405,7 +291,7 @@
* Returns the build method strings for class file.
*
* @param yangName class name
- * @return build method string for class.
+ * @return build method string for class
*/
private static String getBuild(String yangName) {
@@ -422,7 +308,7 @@
/**
* Returns the Default constructor strings for class file.
*
- * @param yangName name of the class.
+ * @param name name of the class
* @return Default constructor for class
*/
private static String getDefaultConstructor(String name) {