Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.yangutils.translator.tojava; |
| 17 | |
| 18 | import java.io.File; |
| 19 | import java.io.IOException; |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.List; |
| 22 | import java.util.Set; |
| 23 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.RpcNotificationContainer; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangCase; |
| 26 | import org.onosproject.yangutils.datamodel.YangEnum; |
| 27 | import org.onosproject.yangutils.datamodel.YangEnumeration; |
| 28 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 29 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 30 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 31 | import org.onosproject.yangutils.datamodel.YangNode; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
| 33 | import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 35 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK; |
| 37 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK; |
| 38 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER; |
| 40 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK; |
| 42 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK; |
| 43 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK; |
| 45 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK; |
| 47 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK; |
| 48 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK; |
| 50 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK; |
| 51 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK; |
| 52 | import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 53 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode; |
| 54 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.generateEnumAttributeString; |
| 55 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination; |
| 56 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose; |
| 57 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile; |
| 58 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile; |
| 59 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEnumClassFile; |
| 60 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile; |
| 61 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 62 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject; |
| 63 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 64 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase; |
| 65 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
| 66 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode; |
| 67 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase; |
| 68 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 69 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString; |
| 70 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 71 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass; |
| 72 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString; |
| 73 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod; |
| 74 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 75 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 76 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass; |
| 77 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString; |
| 78 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 79 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString; |
| 80 | import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport; |
| 81 | import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport; |
| 82 | import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addHasAugmentationImport; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 83 | import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile; |
| 84 | import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentedInfoExtended; |
| 85 | import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils |
| 86 | .isHasAugmentationExtended; |
| 87 | import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils |
| 88 | .prepareJavaFileGeneratorForExtendsList; |
| 89 | import static org.onosproject.yangutils.utils.UtilConstants.BUILDER; |
| 90 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
| 91 | import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION; |
| 92 | import static org.onosproject.yangutils.utils.UtilConstants.IMPL; |
| 93 | import static org.onosproject.yangutils.utils.UtilConstants.IMPORT; |
| 94 | import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE; |
| 95 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 96 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 97 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
| 98 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
| 99 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage; |
| 100 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile; |
| 101 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD; |
| 102 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 103 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 104 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath; |
| 105 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile; |
| 106 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles; |
| 107 | |
| 108 | /** |
| 109 | * Represents implementation of java code fragments temporary implementations. |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 110 | * Manages the common temp file required for Java file(s) generated. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 111 | */ |
| 112 | public class TempJavaFragmentFiles { |
| 113 | /** |
| 114 | * Information about the java files being generated. |
| 115 | */ |
| 116 | private JavaFileInfo javaFileInfo; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 117 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 118 | /** |
| 119 | * Imported class info. |
| 120 | */ |
| 121 | private JavaImportData javaImportData; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 122 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 123 | /** |
| 124 | * The variable which guides the types of temporary files generated using |
| 125 | * the temporary generated file types mask. |
| 126 | */ |
| 127 | private int generatedTempFiles; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 128 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 129 | /** |
| 130 | * Absolute path where the target java file needs to be generated. |
| 131 | */ |
| 132 | private String absoluteDirPath; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 133 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 134 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 135 | * Contains all the interface(s)/class name which will be extended by generated files. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 136 | */ |
| 137 | private List<String> extendsList = new ArrayList<>(); |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 138 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 139 | /** |
| 140 | * File type extension for java classes. |
| 141 | */ |
| 142 | private static final String JAVA_FILE_EXTENSION = ".java"; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 143 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 144 | /** |
| 145 | * File type extension for temporary classes. |
| 146 | */ |
| 147 | private static final String TEMP_FILE_EXTENSION = ".tmp"; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 148 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 149 | /** |
| 150 | * Folder suffix for temporary files folder. |
| 151 | */ |
| 152 | private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp"; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 153 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 154 | /** |
| 155 | * File name for getter method. |
| 156 | */ |
| 157 | private static final String GETTER_METHOD_FILE_NAME = "GetterMethod"; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 158 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 159 | /** |
| 160 | * File name for setter method. |
| 161 | */ |
| 162 | private static final String SETTER_METHOD_FILE_NAME = "SetterMethod"; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * File name for getter method implementation. |
| 166 | */ |
| 167 | private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl"; |
| 168 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 169 | /** |
| 170 | * File name for setter method implementation. |
| 171 | */ |
| 172 | private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl"; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 173 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 174 | /** |
| 175 | * File name for attributes. |
| 176 | */ |
| 177 | private static final String ATTRIBUTE_FILE_NAME = "Attributes"; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 178 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 179 | /** |
| 180 | * File name for to string method. |
| 181 | */ |
| 182 | private static final String TO_STRING_METHOD_FILE_NAME = "ToString"; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 183 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 184 | /** |
| 185 | * File name for hash code method. |
| 186 | */ |
| 187 | private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode"; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 188 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 189 | /** |
| 190 | * File name for equals method. |
| 191 | */ |
| 192 | private static final String EQUALS_METHOD_FILE_NAME = "Equals"; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 193 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 194 | /** |
| 195 | * File name for temporary enum class. |
| 196 | */ |
| 197 | private static final String ENUM_CLASS_TEMP_FILE_NAME = "EnumClass"; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 198 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 199 | /** |
| 200 | * File name for interface java file name suffix. |
| 201 | */ |
| 202 | private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 203 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 204 | /** |
| 205 | * File name for builder interface file name suffix. |
| 206 | */ |
| 207 | private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 208 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 209 | /** |
| 210 | * File name for builder class file name suffix. |
| 211 | */ |
| 212 | private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 213 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 214 | /** |
| 215 | * File name for impl class file name suffix. |
| 216 | */ |
| 217 | private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 218 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 219 | /** |
| 220 | * File name for enum class file name suffix. |
| 221 | */ |
| 222 | private static final String ENUM_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 223 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 224 | /** |
| 225 | * Java file handle for interface file. |
| 226 | */ |
| 227 | private File interfaceJavaFileHandle; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 228 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 229 | /** |
| 230 | * Java file handle for builder interface file. |
| 231 | */ |
| 232 | private File builderInterfaceJavaFileHandle; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 233 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 234 | /** |
| 235 | * Java file handle for builder class file. |
| 236 | */ |
| 237 | private File builderClassJavaFileHandle; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 238 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 239 | /** |
| 240 | * Java file handle for impl class file. |
| 241 | */ |
| 242 | private File implClassJavaFileHandle; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 243 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 244 | /** |
| 245 | * Temporary file handle for attribute. |
| 246 | */ |
| 247 | private File attributesTempFileHandle; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 248 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 249 | /** |
| 250 | * Temporary file handle for getter of interface. |
| 251 | */ |
| 252 | private File getterInterfaceTempFileHandle; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 253 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 254 | /** |
| 255 | * Temporary file handle for setter of interface. |
| 256 | */ |
| 257 | private File setterInterfaceTempFileHandle; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * Temporary file handle for getter of class. |
| 261 | */ |
| 262 | private File getterImplTempFileHandle; |
| 263 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 264 | /** |
| 265 | * Temporary file handle for setter of class. |
| 266 | */ |
| 267 | private File setterImplTempFileHandle; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 268 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 269 | /** |
| 270 | * Temporary file handle for hash code method of class. |
| 271 | */ |
| 272 | private File hashCodeImplTempFileHandle; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 273 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 274 | /** |
| 275 | * Temporary file handle for equals method of class. |
| 276 | */ |
| 277 | private File equalsImplTempFileHandle; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 278 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 279 | /** |
| 280 | * Temporary file handle for to string method of class. |
| 281 | */ |
| 282 | private File toStringImplTempFileHandle; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 283 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 284 | /** |
| 285 | * Temporary file handle for enum class file. |
| 286 | */ |
| 287 | private File enumClassTempFileHandle; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 288 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 289 | /** |
| 290 | * Import info for case. |
| 291 | */ |
| 292 | private JavaQualifiedTypeInfo caseImportInfo; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 293 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 294 | /** |
| 295 | * Is attribute added. |
| 296 | */ |
| 297 | private boolean isAttributePresent = false; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 298 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 299 | /** |
| 300 | * Current enum's value. |
| 301 | */ |
| 302 | private int enumValue; |
VinodKumarS-Huawei | bb2c27e | 2016-05-11 09:34:21 +0530 | [diff] [blame^] | 303 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 304 | /* |
| 305 | * Java file handle for enum class. |
| 306 | */ |
| 307 | private File enumClassJavaFileHandle; |
| 308 | |
| 309 | /** |
| 310 | * Returns enum class java file handle. |
| 311 | * |
| 312 | * @return enum class java file handle |
| 313 | */ |
| 314 | private File getEnumClassJavaFileHandle() { |
| 315 | return enumClassJavaFileHandle; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Sets enum class java file handle. |
| 320 | * |
| 321 | * @param enumClassJavaFileHandle enum class java file handle |
| 322 | */ |
| 323 | private void setEnumClassJavaFileHandle(File enumClassJavaFileHandle) { |
| 324 | this.enumClassJavaFileHandle = enumClassJavaFileHandle; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Returns enum's value. |
| 329 | * |
| 330 | * @return enum's value |
| 331 | */ |
| 332 | private int getEnumValue() { |
| 333 | return enumValue; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Sets enum's value. |
| 338 | * |
| 339 | * @param enumValue enum's value |
| 340 | */ |
| 341 | private void setEnumValue(int enumValue) { |
| 342 | this.enumValue = enumValue; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Retrieves the absolute path where the file needs to be generated. |
| 347 | * |
| 348 | * @return absolute path where the file needs to be generated |
| 349 | */ |
| 350 | private String getAbsoluteDirPath() { |
| 351 | return absoluteDirPath; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Sets absolute path where the file needs to be generated. |
| 356 | * |
| 357 | * @param absoluteDirPath absolute path where the file needs to be |
| 358 | * generated. |
| 359 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 360 | void setAbsoluteDirPath(String absoluteDirPath) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 361 | this.absoluteDirPath = absoluteDirPath; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Sets the generated java file information. |
| 366 | * |
| 367 | * @param javaFileInfo generated java file information |
| 368 | */ |
| 369 | public void setJavaFileInfo(JavaFileInfo javaFileInfo) { |
| 370 | this.javaFileInfo = javaFileInfo; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Retrieves the generated java file information. |
| 375 | * |
| 376 | * @return generated java file information |
| 377 | */ |
| 378 | public JavaFileInfo getJavaFileInfo() { |
| 379 | return javaFileInfo; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Retrieves the generated temp files. |
| 384 | * |
| 385 | * @return generated temp files |
| 386 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 387 | int getGeneratedTempFiles() { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 388 | return generatedTempFiles; |
| 389 | } |
| 390 | |
| 391 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 392 | * Clears the generated file mask. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 393 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 394 | void clearGeneratedTempFileMask() { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 395 | generatedTempFiles = 0; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Sets generated file files. |
| 400 | * |
| 401 | * @param generatedTempFile generated file |
| 402 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 403 | void addGeneratedTempFile(int generatedTempFile) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 404 | generatedTempFiles |= generatedTempFile; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Retrieves the generated Java files. |
| 409 | * |
| 410 | * @return generated Java files |
| 411 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 412 | int getGeneratedJavaFiles() { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 413 | return getJavaFileInfo().getGeneratedFileTypes(); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Retrieves the mapped Java class name. |
| 418 | * |
| 419 | * @return mapped Java class name |
| 420 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 421 | String getGeneratedJavaClassName() { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 422 | return getJavaFileInfo().getJavaName(); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Retrieves the import data for the generated Java file. |
| 427 | * |
| 428 | * @return import data for the generated Java file |
| 429 | */ |
| 430 | public JavaImportData getJavaImportData() { |
| 431 | return javaImportData; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Sets import data for the generated Java file. |
| 436 | * |
| 437 | * @param javaImportData import data for the generated Java file |
| 438 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 439 | void setJavaImportData(JavaImportData javaImportData) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 440 | this.javaImportData = javaImportData; |
| 441 | } |
| 442 | |
| 443 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 444 | * Retrieves the status of any attributes added. |
| 445 | * |
| 446 | * @return status of any attributes added |
| 447 | */ |
| 448 | public boolean isAttributePresent() { |
| 449 | return isAttributePresent; |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Sets status of any attributes added. |
| 454 | * |
| 455 | * @param attributePresent status of any attributes added |
| 456 | */ |
| 457 | public void setAttributePresent(boolean attributePresent) { |
| 458 | isAttributePresent = attributePresent; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Returns getter methods's temporary file handle. |
| 463 | * |
| 464 | * @return temporary file handle |
| 465 | */ |
| 466 | public File getGetterInterfaceTempFileHandle() { |
| 467 | return getterInterfaceTempFileHandle; |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Sets to getter method's temporary file handle. |
| 472 | * |
| 473 | * @param getterForInterface file handle for to getter method |
| 474 | */ |
| 475 | private void setGetterInterfaceTempFileHandle(File getterForInterface) { |
| 476 | getterInterfaceTempFileHandle = getterForInterface; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Returns setter method's temporary file handle. |
| 481 | * |
| 482 | * @return temporary file handle |
| 483 | */ |
| 484 | public File getSetterInterfaceTempFileHandle() { |
| 485 | return setterInterfaceTempFileHandle; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Sets to setter method's temporary file handle. |
| 490 | * |
| 491 | * @param setterForInterface file handle for to setter method |
| 492 | */ |
| 493 | private void setSetterInterfaceTempFileHandle(File setterForInterface) { |
| 494 | setterInterfaceTempFileHandle = setterForInterface; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Returns setter method's impl's temporary file handle. |
| 499 | * |
| 500 | * @return temporary file handle |
| 501 | */ |
| 502 | public File getSetterImplTempFileHandle() { |
| 503 | return setterImplTempFileHandle; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Sets to setter method's impl's temporary file handle. |
| 508 | * |
| 509 | * @param setterImpl file handle for to setter method's implementation class |
| 510 | */ |
| 511 | private void setSetterImplTempFileHandle(File setterImpl) { |
| 512 | setterImplTempFileHandle = setterImpl; |
| 513 | } |
| 514 | |
| 515 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 516 | * Creates an instance of temporary java code fragment. |
| 517 | * |
| 518 | * @param javaFileInfo generated java file information |
| 519 | * @throws IOException when fails to create new file handle |
| 520 | */ |
| 521 | TempJavaFragmentFiles(JavaFileInfo javaFileInfo) |
| 522 | throws IOException { |
| 523 | setExtendsList(new ArrayList<>()); |
| 524 | setJavaImportData(new JavaImportData()); |
| 525 | setJavaFileInfo(javaFileInfo); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 526 | clearGeneratedTempFileMask(); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 527 | setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(), |
| 528 | getJavaFileInfo().getPackageFilePath())); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 529 | |
| 530 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 531 | * Initialize getter when generation file type matches to interface |
| 532 | * mask. |
| 533 | */ |
| 534 | if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) { |
| 535 | addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK); |
| 536 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 537 | |
| 538 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 539 | * Initialize getter and setter when generation file type matches to |
| 540 | * builder interface mask. |
| 541 | */ |
| 542 | if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) { |
| 543 | addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK); |
| 544 | addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK); |
| 545 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 546 | |
| 547 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 548 | * Initialize getterImpl, setterImpl and attributes when generation file |
| 549 | * type matches to builder class mask. |
| 550 | */ |
| 551 | if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) { |
| 552 | addGeneratedTempFile(ATTRIBUTES_MASK); |
| 553 | addGeneratedTempFile(GETTER_FOR_CLASS_MASK); |
| 554 | addGeneratedTempFile(SETTER_FOR_CLASS_MASK); |
| 555 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 556 | |
| 557 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 558 | * Initialize getterImpl, attributes, constructor, hash code, equals and |
| 559 | * to strings when generation file type matches to impl class mask. |
| 560 | */ |
| 561 | if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) { |
| 562 | addGeneratedTempFile(ATTRIBUTES_MASK); |
| 563 | addGeneratedTempFile(GETTER_FOR_CLASS_MASK); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 564 | addGeneratedTempFile(HASH_CODE_IMPL_MASK); |
| 565 | addGeneratedTempFile(EQUALS_IMPL_MASK); |
| 566 | addGeneratedTempFile(TO_STRING_IMPL_MASK); |
| 567 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 568 | |
| 569 | /* |
| 570 | * Initialize temp files to generate type class. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 571 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 572 | if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 573 | addGeneratedTempFile(ATTRIBUTES_MASK); |
| 574 | addGeneratedTempFile(GETTER_FOR_CLASS_MASK); |
| 575 | addGeneratedTempFile(HASH_CODE_IMPL_MASK); |
| 576 | addGeneratedTempFile(EQUALS_IMPL_MASK); |
| 577 | addGeneratedTempFile(TO_STRING_IMPL_MASK); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 578 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 579 | |
| 580 | /* |
| 581 | * Initialize getter and setter when generation file type matches to |
| 582 | * builder interface mask. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 583 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 584 | if ((getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) { |
| 585 | addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK); |
| 586 | addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 587 | addGeneratedTempFile(GETTER_FOR_CLASS_MASK); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 588 | addGeneratedTempFile(SETTER_FOR_CLASS_MASK); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 589 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 590 | |
| 591 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 592 | * Initialize enum when generation file type matches to enum class mask. |
| 593 | */ |
| 594 | if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) { |
| 595 | addGeneratedTempFile(ENUM_IMPL_MASK); |
| 596 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 597 | |
| 598 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 599 | * Set temporary file handles. |
| 600 | */ |
| 601 | if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) { |
| 602 | setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME)); |
| 603 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 604 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 605 | if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) { |
| 606 | setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME)); |
| 607 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 608 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 609 | if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) { |
| 610 | setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME)); |
| 611 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 612 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 613 | if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) { |
| 614 | setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME)); |
| 615 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 616 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 617 | if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) { |
| 618 | setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME)); |
| 619 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 620 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 621 | if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) { |
| 622 | setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME)); |
| 623 | } |
| 624 | if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) { |
| 625 | setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME)); |
| 626 | } |
| 627 | if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) { |
| 628 | setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME)); |
| 629 | } |
| 630 | if ((getGeneratedTempFiles() & ENUM_IMPL_MASK) != 0) { |
| 631 | setEnumClassTempFileHandle(getTemporaryFileHandle(ENUM_CLASS_TEMP_FILE_NAME)); |
| 632 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Returns java file handle for interface file. |
| 637 | * |
| 638 | * @return java file handle for interface file |
| 639 | */ |
| 640 | private File getInterfaceJavaFileHandle() { |
| 641 | return interfaceJavaFileHandle; |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Sets the java file handle for interface file. |
| 646 | * |
| 647 | * @param interfaceJavaFileHandle java file handle |
| 648 | */ |
| 649 | private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) { |
| 650 | this.interfaceJavaFileHandle = interfaceJavaFileHandle; |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Returns java file handle for builder interface file. |
| 655 | * |
| 656 | * @return java file handle for builder interface file |
| 657 | */ |
| 658 | private File getBuilderInterfaceJavaFileHandle() { |
| 659 | return builderInterfaceJavaFileHandle; |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * Sets the java file handle for builder interface file. |
| 664 | * |
| 665 | * @param builderInterfaceJavaFileHandle java file handle |
| 666 | */ |
| 667 | private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) { |
| 668 | this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle; |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Returns java file handle for builder class file. |
| 673 | * |
| 674 | * @return java file handle for builder class file |
| 675 | */ |
| 676 | private File getBuilderClassJavaFileHandle() { |
| 677 | return builderClassJavaFileHandle; |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Sets the java file handle for builder class file. |
| 682 | * |
| 683 | * @param builderClassJavaFileHandle java file handle |
| 684 | */ |
| 685 | private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) { |
| 686 | this.builderClassJavaFileHandle = builderClassJavaFileHandle; |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Returns java file handle for impl class file. |
| 691 | * |
| 692 | * @return java file handle for impl class file |
| 693 | */ |
| 694 | private File getImplClassJavaFileHandle() { |
| 695 | return implClassJavaFileHandle; |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Sets the java file handle for impl class file. |
| 700 | * |
| 701 | * @param implClassJavaFileHandle java file handle |
| 702 | */ |
| 703 | private void setImplClassJavaFileHandle(File implClassJavaFileHandle) { |
| 704 | this.implClassJavaFileHandle = implClassJavaFileHandle; |
| 705 | } |
| 706 | |
| 707 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 708 | * Returns attribute's temporary file handle. |
| 709 | * |
| 710 | * @return temporary file handle |
| 711 | */ |
| 712 | public File getAttributesTempFileHandle() { |
| 713 | return attributesTempFileHandle; |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * Sets attribute's temporary file handle. |
| 718 | * |
| 719 | * @param attributeForClass file handle for attribute |
| 720 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 721 | void setAttributesTempFileHandle(File attributeForClass) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 722 | attributesTempFileHandle = attributeForClass; |
| 723 | } |
| 724 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 725 | |
| 726 | /** |
| 727 | * Returns getter method's impl's temporary file handle. |
| 728 | * |
| 729 | * @return temporary file handle |
| 730 | */ |
| 731 | public File getGetterImplTempFileHandle() { |
| 732 | return getterImplTempFileHandle; |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * Sets to getter method's impl's temporary file handle. |
| 737 | * |
| 738 | * @param getterImpl file handle for to getter method's impl |
| 739 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 740 | void setGetterImplTempFileHandle(File getterImpl) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 741 | getterImplTempFileHandle = getterImpl; |
| 742 | } |
| 743 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 744 | |
| 745 | /** |
| 746 | * Returns hash code method's temporary file handle. |
| 747 | * |
| 748 | * @return temporary file handle |
| 749 | */ |
| 750 | public File getHashCodeImplTempFileHandle() { |
| 751 | return hashCodeImplTempFileHandle; |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * Sets hash code method's temporary file handle. |
| 756 | * |
| 757 | * @param hashCodeMethod file handle for hash code method |
| 758 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 759 | void setHashCodeImplTempFileHandle(File hashCodeMethod) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 760 | hashCodeImplTempFileHandle = hashCodeMethod; |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Returns equals mehtod's temporary file handle. |
| 765 | * |
| 766 | * @return temporary file handle |
| 767 | */ |
| 768 | public File getEqualsImplTempFileHandle() { |
| 769 | return equalsImplTempFileHandle; |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * Sets equals method's temporary file handle. |
| 774 | * |
| 775 | * @param equalsMethod file handle for to equals method |
| 776 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 777 | void setEqualsImplTempFileHandle(File equalsMethod) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 778 | equalsImplTempFileHandle = equalsMethod; |
| 779 | } |
| 780 | |
| 781 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 782 | * Returns to string method's temporary file handle. |
| 783 | * |
| 784 | * @return temporary file handle |
| 785 | */ |
| 786 | public File getToStringImplTempFileHandle() { |
| 787 | return toStringImplTempFileHandle; |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * Sets to string method's temporary file handle. |
| 792 | * |
| 793 | * @param toStringMethod file handle for to string method |
| 794 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 795 | void setToStringImplTempFileHandle(File toStringMethod) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 796 | toStringImplTempFileHandle = toStringMethod; |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Returns temporary file handle for enum class file. |
| 801 | * |
| 802 | * @return temporary file handle for enum class file |
| 803 | */ |
| 804 | public File getEnumClassTempFileHandle() { |
| 805 | return enumClassTempFileHandle; |
| 806 | } |
| 807 | |
| 808 | /** |
| 809 | * Sets temporary file handle for enum class file. |
| 810 | * |
| 811 | * @param enumClassTempFileHandle temporary file handle for enum class file |
| 812 | */ |
| 813 | private void setEnumClassTempFileHandle(File enumClassTempFileHandle) { |
| 814 | this.enumClassTempFileHandle = enumClassTempFileHandle; |
| 815 | } |
| 816 | |
| 817 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 818 | * Returns list of classes to be extended by generated files. |
| 819 | * |
| 820 | * @return list of classes to be extended by generated files |
| 821 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 822 | List<String> getExtendsList() { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 823 | return extendsList; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * Sets class to be extended by generated file. |
| 828 | * |
| 829 | * @param extendsList list of classes to be extended |
| 830 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 831 | void setExtendsList(List<String> extendsList) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 832 | this.extendsList = extendsList; |
| 833 | } |
| 834 | |
| 835 | /** |
| 836 | * Adds class to the extends list. |
| 837 | * |
| 838 | * @param extend class to be extended |
| 839 | */ |
| 840 | public void addToExtendsList(String extend) { |
| 841 | getExtendsList().add(extend); |
| 842 | } |
| 843 | |
| 844 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 845 | * Adds attribute for class. |
| 846 | * |
| 847 | * @param attr attribute info |
| 848 | * @throws IOException when fails to append to temporary file |
| 849 | */ |
| 850 | private void addAttribute(JavaAttributeInfo attr) |
| 851 | throws IOException { |
| 852 | appendToFile(getAttributesTempFileHandle(), parseAttribute(attr) + FOUR_SPACE_INDENTATION); |
| 853 | } |
| 854 | |
| 855 | /** |
| 856 | * Adds getter for interface. |
| 857 | * |
| 858 | * @param attr attribute info |
| 859 | * @throws IOException when fails to append to temporary file |
| 860 | */ |
| 861 | private void addGetterForInterface(JavaAttributeInfo attr) |
| 862 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 863 | appendToFile(getGetterInterfaceTempFileHandle(), getGetterString(attr, getGeneratedJavaFiles()) + NEW_LINE); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /** |
| 867 | * Adds setter for interface. |
| 868 | * |
| 869 | * @param attr attribute info |
| 870 | * @throws IOException when fails to append to temporary file |
| 871 | */ |
| 872 | private void addSetterForInterface(JavaAttributeInfo attr) |
| 873 | throws IOException { |
| 874 | appendToFile(getSetterInterfaceTempFileHandle(), |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 875 | getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles()) + NEW_LINE); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /** |
| 879 | * Adds setter's implementation for class. |
| 880 | * |
| 881 | * @param attr attribute info |
| 882 | * @throws IOException when fails to append to temporary file |
| 883 | */ |
| 884 | private void addSetterImpl(JavaAttributeInfo attr) |
| 885 | throws IOException { |
| 886 | appendToFile(getSetterImplTempFileHandle(), |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 887 | getOverRideString() + getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles()) + |
| 888 | NEW_LINE); |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * Adds getter method's impl for class. |
| 893 | * |
| 894 | * @param attr attribute info |
| 895 | * @throws IOException when fails to append to temporary file |
| 896 | */ |
| 897 | private void addGetterImpl(JavaAttributeInfo attr) |
| 898 | throws IOException { |
| 899 | if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0 |
| 900 | || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) { |
| 901 | appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr, |
| 902 | getGeneratedJavaFiles()) + NEW_LINE); |
| 903 | } else { |
| 904 | appendToFile(getGetterImplTempFileHandle(), getJavaDoc(GETTER_METHOD, attr.getAttributeName(), false) |
| 905 | + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE); |
| 906 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /** |
| 910 | * Adds build method for interface. |
| 911 | * |
| 912 | * @return build method for interface |
| 913 | * @throws IOException when fails to append to temporary file |
| 914 | */ |
| 915 | String addBuildMethodForInterface() |
| 916 | throws IOException { |
| 917 | return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName()); |
| 918 | } |
| 919 | |
| 920 | /** |
| 921 | * Adds build method's implementation for class. |
| 922 | * |
| 923 | * @return build method implementation for class |
| 924 | * @throws IOException when fails to append to temporary file |
| 925 | */ |
| 926 | String addBuildMethodImpl() |
| 927 | throws IOException { |
| 928 | return getBuildString(getGeneratedJavaClassName()) + NEW_LINE; |
| 929 | } |
| 930 | |
| 931 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 932 | * Adds default constructor for class. |
| 933 | * |
| 934 | * @param modifier modifier for constructor. |
| 935 | * @param toAppend string which need to be appended with the class name |
| 936 | * @return default constructor for class |
| 937 | * @throws IOException when fails to append to file |
| 938 | */ |
| 939 | String addDefaultConstructor(String modifier, String toAppend) |
| 940 | throws IOException { |
| 941 | return NEW_LINE + getDefaultConstructorString(getGeneratedJavaClassName() + toAppend, modifier); |
| 942 | } |
| 943 | |
| 944 | /** |
| 945 | * Adds default constructor for class. |
| 946 | * |
| 947 | * @return default constructor for class |
| 948 | * @throws IOException when fails to append to file |
| 949 | */ |
| 950 | public String addOfMethod() |
| 951 | throws IOException { |
| 952 | return getJavaDoc(OF_METHOD, getGeneratedJavaClassName(), false) |
| 953 | + getOfMethod(getGeneratedJavaClassName(), null); |
| 954 | } |
| 955 | |
| 956 | /** |
| 957 | * Adds hash code method for class. |
| 958 | * |
| 959 | * @param attr attribute info |
| 960 | * @throws IOException when fails to append to temporary file |
| 961 | */ |
| 962 | private void addHashCodeMethod(JavaAttributeInfo attr) |
| 963 | throws IOException { |
| 964 | appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE); |
| 965 | } |
| 966 | |
| 967 | /** |
| 968 | * Adds equals method for class. |
| 969 | * |
| 970 | * @param attr attribute info |
| 971 | * @throws IOException when fails to append to temporary file |
| 972 | */ |
| 973 | private void addEqualsMethod(JavaAttributeInfo attr) |
| 974 | throws IOException { |
| 975 | appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE); |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * Adds ToString method for class. |
| 980 | * |
| 981 | * @param attr attribute info |
| 982 | * @throws IOException when fails to append to temporary file |
| 983 | */ |
| 984 | private void addToStringMethod(JavaAttributeInfo attr) |
| 985 | throws IOException { |
| 986 | appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE); |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * Adds enum class attributes to temporary file. |
| 991 | * |
| 992 | * @param curEnumInfo current YANG enum |
| 993 | * @throws IOException when fails to do IO operations. |
| 994 | */ |
| 995 | private void addAttributesForEnumClass(JavaAttributeInfo curEnumInfo) |
| 996 | throws IOException { |
| 997 | appendToFile(getEnumClassTempFileHandle(), |
| 998 | generateEnumAttributeString(curEnumInfo.getAttributeName(), getEnumValue())); |
| 999 | } |
| 1000 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1001 | |
| 1002 | /** |
| 1003 | * Returns a temporary file handle for the specific file type. |
| 1004 | * |
| 1005 | * @param fileName file name |
| 1006 | * @return temporary file handle |
| 1007 | * @throws IOException when fails to create new file handle |
| 1008 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1009 | File getTemporaryFileHandle(String fileName) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1010 | throws IOException { |
| 1011 | String path = getTempDirPath(); |
| 1012 | File dir = new File(path); |
| 1013 | if (!dir.exists()) { |
| 1014 | dir.mkdirs(); |
| 1015 | } |
| 1016 | File file = new File(path + fileName + TEMP_FILE_EXTENSION); |
| 1017 | if (!file.exists()) { |
| 1018 | file.createNewFile(); |
| 1019 | } |
| 1020 | return file; |
| 1021 | } |
| 1022 | |
| 1023 | /** |
| 1024 | * Returns a temporary file handle for the specific file type. |
| 1025 | * |
| 1026 | * @param fileName file name |
| 1027 | * @return temporary file handle |
| 1028 | * @throws IOException when fails to create new file handle |
| 1029 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1030 | File getJavaFileHandle(String fileName) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1031 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1032 | // createPackage(getAbsoluteDirPath(), getJavaFileInfo().getJavaName()); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1033 | return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo()); |
| 1034 | } |
| 1035 | |
| 1036 | /** |
| 1037 | * Returns data from the temporary files. |
| 1038 | * |
| 1039 | * @param file temporary file handle |
| 1040 | * @return stored data from temporary files |
| 1041 | * @throws IOException when failed to get data from the given file |
| 1042 | */ |
| 1043 | public String getTemporaryDataFromFileHandle(File file) |
| 1044 | throws IOException { |
| 1045 | String path = getTempDirPath(); |
| 1046 | if (new File(path + file.getName()).exists()) { |
| 1047 | return readAppendFile(path + file.getName(), EMPTY_STRING); |
| 1048 | } else { |
| 1049 | throw new IOException("Unable to get data from the given " |
| 1050 | + file.getName() + " file for " + getGeneratedJavaClassName() + PERIOD); |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | /** |
| 1055 | * Returns temporary directory path. |
| 1056 | * |
| 1057 | * @return directory path |
| 1058 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1059 | String getTempDirPath() { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1060 | return getPackageDirPathFromJavaJPackage(getAbsoluteDirPath()) + SLASH + getGeneratedJavaClassName() |
| 1061 | + TEMP_FOLDER_NAME_SUFIX + SLASH; |
| 1062 | } |
| 1063 | |
| 1064 | /** |
| 1065 | * Parses attribute to get the attribute string. |
| 1066 | * |
| 1067 | * @param attr attribute info |
| 1068 | * @return attribute string |
| 1069 | */ |
| 1070 | private String parseAttribute(JavaAttributeInfo attr) { |
| 1071 | /* |
| 1072 | * TODO: check if this utility needs to be called or move to the caller |
| 1073 | */ |
| 1074 | String attributeName = getCamelCase(getSmallCase(attr.getAttributeName()), null); |
| 1075 | if (attr.isQualifiedName()) { |
| 1076 | return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), attr.getImportInfo().getClassInfo(), |
| 1077 | attributeName, attr.isListAttr()); |
| 1078 | } else { |
| 1079 | return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName, |
| 1080 | attr.isListAttr()); |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | /** |
| 1085 | * Appends content to temporary file. |
| 1086 | * |
| 1087 | * @param file temporary file |
| 1088 | * @param data data to be appended |
| 1089 | * @throws IOException when fails to append to file |
| 1090 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1091 | void appendToFile(File file, String data) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1092 | throws IOException { |
| 1093 | try { |
| 1094 | insertDataIntoJavaFile(file, data); |
| 1095 | } catch (IOException ex) { |
| 1096 | throw new IOException("failed to write in temp file."); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | /** |
| 1101 | * Adds current node info as and attribute to the parent generated file. |
| 1102 | * |
| 1103 | * @param curNode current node which needs to be added as an attribute in |
| 1104 | * the parent generated code |
| 1105 | * @param isList is list construct |
| 1106 | * @throws IOException IO operation exception |
| 1107 | */ |
| 1108 | public static void addCurNodeInfoInParentTempFile(YangNode curNode, |
| 1109 | boolean isList) |
| 1110 | throws IOException { |
| 1111 | YangNode parent = getParentNodeInGenCode(curNode); |
| 1112 | if (!(parent instanceof JavaCodeGenerator)) { |
| 1113 | throw new TranslatorException("missing parent node to contain current node info in generated file"); |
| 1114 | } |
| 1115 | JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInParent(curNode, |
| 1116 | parent, isList); |
| 1117 | if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) { |
| 1118 | throw new TranslatorException("missing parent temp file handle"); |
| 1119 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1120 | getNodesInterfaceFragmentFiles(parent) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1121 | .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo); |
| 1122 | } |
| 1123 | |
| 1124 | /** |
| 1125 | * Creates an attribute info object corresponding to a data model node and |
| 1126 | * return it. |
| 1127 | * |
| 1128 | * @param curNode current data model node for which the java code generation |
| 1129 | * is being handled |
| 1130 | * @param parentNode parent node in which the current node is an attribute |
| 1131 | * @param isListNode is the current added attribute needs to be a list |
| 1132 | * @return AttributeInfo attribute details required to add in temporary |
| 1133 | * files |
| 1134 | */ |
| 1135 | public static JavaAttributeInfo getCurNodeAsAttributeInParent( |
| 1136 | YangNode curNode, YangNode parentNode, boolean isListNode) { |
| 1137 | String curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName(); |
| 1138 | /* |
| 1139 | * Get the import info corresponding to the attribute for import in |
| 1140 | * generated java files or qualified access |
| 1141 | */ |
| 1142 | JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode, |
| 1143 | curNodeName); |
| 1144 | if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) { |
| 1145 | throw new TranslatorException("Parent node does not have file info"); |
| 1146 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1147 | TempJavaFragmentFiles tempJavaFragmentFiles = getNodesInterfaceFragmentFiles(parentNode); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1148 | JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData(); |
| 1149 | boolean isQualified = parentImportData.addImportInfo(qualifiedTypeInfo); |
| 1150 | return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode); |
| 1151 | } |
| 1152 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1153 | public static TempJavaFragmentFiles getNodesInterfaceFragmentFiles(YangNode node) { |
| 1154 | TempJavaFragmentFiles tempJavaFragmentFiles; |
| 1155 | if (node instanceof RpcNotificationContainer) { |
| 1156 | tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node) |
| 1157 | .getTempJavaCodeFragmentFiles() |
| 1158 | .getServiceTempFiles(); |
| 1159 | } else { |
| 1160 | tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node) |
| 1161 | .getTempJavaCodeFragmentFiles() |
| 1162 | .getBeanTempFiles(); |
| 1163 | } |
| 1164 | return tempJavaFragmentFiles; |
| 1165 | } |
| 1166 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1167 | /** |
| 1168 | * Adds parent's info to current node import list. |
| 1169 | * |
| 1170 | * @param curNode current node for which import list needs to be updated |
| 1171 | */ |
| 1172 | public void addParentInfoInCurNodeTempFile(YangNode curNode) { |
| 1173 | caseImportInfo = new JavaQualifiedTypeInfo(); |
| 1174 | YangNode parent = getParentNodeInGenCode(curNode); |
| 1175 | if (!(parent instanceof JavaCodeGenerator)) { |
| 1176 | throw new TranslatorException("missing parent node to contain current node info in generated file"); |
| 1177 | } |
| 1178 | if (!(curNode instanceof JavaFileInfoContainer)) { |
| 1179 | throw new TranslatorException("missing java file information to get the package details " |
| 1180 | + "of attribute corresponding to child node"); |
| 1181 | } |
| 1182 | caseImportInfo.setClassInfo(getCaptialCase(getCamelCase(parent.getName(), null))); |
| 1183 | caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage()); |
| 1184 | ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles() |
| 1185 | .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo); |
| 1186 | } |
| 1187 | |
| 1188 | /** |
| 1189 | * Adds leaf attributes in generated files. |
| 1190 | * |
| 1191 | * @param listOfLeaves list of YANG leaf |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1192 | * @param yangPluginConfig |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1193 | * @throws IOException IO operation fail |
| 1194 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1195 | private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves, |
| 1196 | YangPluginConfig yangPluginConfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1197 | throws IOException { |
| 1198 | if (listOfLeaves != null) { |
| 1199 | for (YangLeaf leaf : listOfLeaves) { |
| 1200 | if (!(leaf instanceof JavaLeafInfoContainer)) { |
| 1201 | throw new TranslatorException("Leaf does not have java information"); |
| 1202 | } |
| 1203 | JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf; |
| 1204 | javaLeaf.updateJavaQualifiedInfo(); |
| 1205 | JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData( |
| 1206 | javaLeaf.getJavaQualifiedInfo(), |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1207 | javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()), |
| 1208 | javaLeaf.getDataType(), |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1209 | getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()), |
| 1210 | false); |
| 1211 | addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo); |
| 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * Adds leaf list's attributes in generated files. |
| 1218 | * |
| 1219 | * @param listOfLeafList list of YANG leaves |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1220 | * @param yangPluginConfig |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1221 | * @throws IOException IO operation fail |
| 1222 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1223 | private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1224 | throws IOException { |
| 1225 | if (listOfLeafList != null) { |
| 1226 | for (YangLeafList leafList : listOfLeafList) { |
| 1227 | if (!(leafList instanceof JavaLeafInfoContainer)) { |
| 1228 | throw new TranslatorException("Leaf-list does not have java information"); |
| 1229 | } |
| 1230 | JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList; |
| 1231 | javaLeaf.updateJavaQualifiedInfo(); |
| 1232 | JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData( |
| 1233 | javaLeaf.getJavaQualifiedInfo(), |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1234 | javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()), |
| 1235 | javaLeaf.getDataType(), |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1236 | getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()), |
| 1237 | true); |
| 1238 | addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo); |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | /** |
| 1244 | * Adds all the leaves in the current data model node as part of the |
| 1245 | * generated temporary file. |
| 1246 | * |
| 1247 | * @param curNode java file info of the generated file |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1248 | * @param yangPluginConfig plugin config |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1249 | * @throws IOException IO operation fail |
| 1250 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1251 | public void addCurNodeLeavesInfoToTempFiles(YangNode curNode, |
| 1252 | YangPluginConfig yangPluginConfig) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1253 | throws IOException { |
| 1254 | if (!(curNode instanceof YangLeavesHolder)) { |
| 1255 | throw new TranslatorException("Data model node does not have any leaves"); |
| 1256 | } |
| 1257 | YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1258 | addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig); |
| 1259 | addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | /** |
| 1263 | * Adds enum attributes to temporary files. |
| 1264 | * |
| 1265 | * @param curNode current YANG node |
| 1266 | * @throws IOException when fails to do IO operations |
| 1267 | */ |
| 1268 | public void addEnumAttributeToTempFiles(YangNode curNode) |
| 1269 | throws IOException { |
| 1270 | if (curNode instanceof YangEnumeration) { |
| 1271 | Set<YangEnum> enumSet = ((YangEnumeration) curNode).getEnumSet(); |
| 1272 | /* |
| 1273 | * Get the import info corresponding to the attribute for import in |
| 1274 | * generated java files or qualified access |
| 1275 | */ |
| 1276 | JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode, |
| 1277 | getJavaFileInfo().getJavaName()); |
| 1278 | for (YangEnum curEnum : enumSet) { |
| 1279 | JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(qualifiedTypeInfo, |
| 1280 | curEnum.getNamedValue(), null, false, false); |
| 1281 | setEnumValue(curEnum.getValue()); |
| 1282 | addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo); |
| 1283 | } |
| 1284 | } else { |
| 1285 | throw new TranslatorException("current node should be of type enum."); |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1290 | * Adds the new attribute info to the target generated temporary files. |
| 1291 | * |
| 1292 | * @param newAttrInfo the attribute info that needs to be added to temporary |
| 1293 | * files |
| 1294 | * @throws IOException IO operation fail |
| 1295 | */ |
| 1296 | void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo) |
| 1297 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1298 | setAttributePresent(true); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1299 | if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) { |
| 1300 | addAttribute(newAttrInfo); |
| 1301 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1302 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1303 | if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) { |
| 1304 | addGetterForInterface(newAttrInfo); |
| 1305 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1306 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1307 | if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) { |
| 1308 | addSetterForInterface(newAttrInfo); |
| 1309 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1310 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1311 | if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) { |
| 1312 | addSetterImpl(newAttrInfo); |
| 1313 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1314 | |
| 1315 | if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) { |
| 1316 | addGetterImpl(newAttrInfo); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1317 | } |
| 1318 | if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) { |
| 1319 | addHashCodeMethod(newAttrInfo); |
| 1320 | } |
| 1321 | if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) { |
| 1322 | addEqualsMethod(newAttrInfo); |
| 1323 | } |
| 1324 | if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) { |
| 1325 | addToStringMethod(newAttrInfo); |
| 1326 | } |
| 1327 | if ((getGeneratedTempFiles() & ENUM_IMPL_MASK) != 0) { |
| 1328 | addAttributesForEnumClass(newAttrInfo); |
| 1329 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | /** |
| 1333 | * Returns java class name. |
| 1334 | * |
| 1335 | * @param suffix for the class name based on the file type |
| 1336 | * @return java class name |
| 1337 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1338 | String getJavaClassName(String suffix) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1339 | return getCaptialCase(getJavaFileInfo().getJavaName()) + suffix; |
| 1340 | } |
| 1341 | |
| 1342 | /** |
| 1343 | * Returns the directory path. |
| 1344 | * |
| 1345 | * @return directory path |
| 1346 | */ |
| 1347 | private String getDirPath() { |
| 1348 | return getJavaFileInfo().getPackageFilePath(); |
| 1349 | } |
| 1350 | |
| 1351 | /** |
| 1352 | * Constructs java code exit. |
| 1353 | * |
| 1354 | * @param fileType generated file type |
| 1355 | * @param curNode current YANG node |
| 1356 | * @throws IOException when fails to generate java files |
| 1357 | */ |
| 1358 | public void generateJavaFile(int fileType, YangNode curNode) |
| 1359 | throws IOException { |
| 1360 | List<String> imports = new ArrayList<>(); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1361 | if (isAttributePresent()) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1362 | imports = getJavaImportData().getImports(); |
| 1363 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1364 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1365 | * Prepares java file generator for extends list. |
| 1366 | */ |
| 1367 | prepareJavaFileGeneratorForExtendsList(getExtendsList()); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1368 | createPackage(curNode); |
| 1369 | |
| 1370 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1371 | * Generate java code. |
| 1372 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1373 | if ((fileType & INTERFACE_MASK) != 0 || (fileType & |
| 1374 | BUILDER_INTERFACE_MASK) != 0) { |
| 1375 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1376 | * Adds import for case. |
| 1377 | */ |
| 1378 | if (curNode instanceof YangCase) { |
| 1379 | List<String> importData = ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles() |
| 1380 | .getBeanTempFiles().getJavaImportData().getImports(); |
| 1381 | for (String importInfo : importData) { |
| 1382 | if (!imports.contains(importInfo)) { |
| 1383 | imports.add(importInfo); |
| 1384 | } |
| 1385 | } |
| 1386 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1387 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1388 | * Adds import for HasAugmentation class. |
| 1389 | */ |
| 1390 | if (isHasAugmentationExtended(getExtendsList())) { |
| 1391 | addHasAugmentationImport(curNode, imports, true); |
| 1392 | } |
| 1393 | if (isAugmentedInfoExtended(getExtendsList())) { |
| 1394 | addAugmentedInfoImport(curNode, imports, true); |
| 1395 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1396 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1397 | * Create interface file. |
| 1398 | */ |
| 1399 | setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX))); |
| 1400 | setInterfaceJavaFileHandle( |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1401 | generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent())); |
| 1402 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1403 | * Create builder interface file. |
| 1404 | */ |
| 1405 | if ((fileType & BUILDER_INTERFACE_MASK) != 0) { |
| 1406 | setBuilderInterfaceJavaFileHandle( |
| 1407 | getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX))); |
| 1408 | setBuilderInterfaceJavaFileHandle( |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1409 | generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode, |
| 1410 | isAttributePresent())); |
| 1411 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1412 | * Append builder interface file to interface file and close it. |
| 1413 | */ |
| 1414 | mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle()); |
| 1415 | } |
| 1416 | insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose()); |
| 1417 | if (isHasAugmentationExtended(getExtendsList())) { |
| 1418 | addHasAugmentationImport(curNode, imports, false); |
| 1419 | } |
| 1420 | if (isAugmentedInfoExtended(getExtendsList())) { |
| 1421 | addAugmentedInfoImport(curNode, imports, false); |
| 1422 | } |
| 1423 | if (curNode instanceof YangCase) { |
| 1424 | removeCaseImport(imports); |
| 1425 | } |
| 1426 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1427 | if (((fileType & GENERATE_SERVICE_AND_MANAGER) != 0) |
| 1428 | && ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & IMPL_CLASS_MASK) != 0)) { |
| 1429 | if (isAttributePresent()) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1430 | addImportsToStringAndHasCodeMethods(curNode, imports); |
| 1431 | } |
| 1432 | if (isHasAugmentationExtended(getExtendsList())) { |
| 1433 | addAugmentedInfoImport(curNode, imports, true); |
| 1434 | addArrayListImport(curNode, imports, true); |
| 1435 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1436 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1437 | * Create builder class file. |
| 1438 | */ |
| 1439 | setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX))); |
| 1440 | setBuilderClassJavaFileHandle( |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1441 | generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode, isAttributePresent())); |
| 1442 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1443 | * Create impl class file. |
| 1444 | */ |
| 1445 | if ((fileType & IMPL_CLASS_MASK) != 0) { |
| 1446 | setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX))); |
| 1447 | setImplClassJavaFileHandle( |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1448 | generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent())); |
| 1449 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1450 | * Append impl class to builder class and close it. |
| 1451 | */ |
| 1452 | mergeJavaFiles(getImplClassJavaFileHandle(), getBuilderClassJavaFileHandle()); |
| 1453 | } |
| 1454 | insertDataIntoJavaFile(getBuilderClassJavaFileHandle(), getJavaClassDefClose()); |
| 1455 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1456 | |
| 1457 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1458 | * Creates type enum class file. |
| 1459 | */ |
| 1460 | if ((fileType & GENERATE_ENUM_CLASS) != 0) { |
| 1461 | setEnumClassJavaFileHandle(getJavaFileHandle(getJavaClassName(ENUM_CLASS_FILE_NAME_SUFFIX))); |
| 1462 | setEnumClassJavaFileHandle(generateEnumClassFile(getEnumClassJavaFileHandle(), curNode)); |
| 1463 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1464 | |
| 1465 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1466 | * Close all the file handles. |
| 1467 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1468 | freeTemporaryResources(false); |
| 1469 | } |
| 1470 | |
| 1471 | /** |
| 1472 | * Adds imports for ToString and HashCodeMethod. |
| 1473 | * |
| 1474 | * @param curNode current YANG node |
| 1475 | * @param imports import list |
| 1476 | * @return import list |
| 1477 | */ |
| 1478 | public List<String> addImportsToStringAndHasCodeMethods(YangNode curNode, List<String> imports) { |
| 1479 | imports.add(getJavaImportData().getImportForHashAndEquals()); |
| 1480 | imports.add(getJavaImportData().getImportForToString()); |
| 1481 | return imports; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | /** |
| 1485 | * Removes case import info from import list. |
| 1486 | * |
| 1487 | * @param imports list of imports |
| 1488 | * @return import for class |
| 1489 | */ |
| 1490 | private List<String> removeCaseImport(List<String> imports) { |
| 1491 | if (imports != null && caseImportInfo != null) { |
| 1492 | String caseImport = IMPORT + caseImportInfo.getPkgInfo() + PERIOD + caseImportInfo.getClassInfo() + |
| 1493 | SEMI_COLAN + NEW_LINE; |
| 1494 | imports.remove(caseImport); |
| 1495 | } |
| 1496 | return imports; |
| 1497 | } |
| 1498 | |
| 1499 | /** |
| 1500 | * Removes all temporary file handles. |
| 1501 | * |
| 1502 | * @param isErrorOccurred when translator fails to generate java files we |
| 1503 | * need to close all open file handles include temporary files |
| 1504 | * and java files. |
| 1505 | * @throws IOException when failed to delete the temporary files |
| 1506 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1507 | public void freeTemporaryResources(boolean isErrorOccurred) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1508 | throws IOException { |
| 1509 | boolean isError = isErrorOccurred; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1510 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1511 | * Close all java file handles and when error occurs delete the files. |
| 1512 | */ |
| 1513 | if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) { |
| 1514 | closeFile(getInterfaceJavaFileHandle(), isError); |
| 1515 | } |
| 1516 | if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) { |
| 1517 | closeFile(getBuilderClassJavaFileHandle(), isError); |
| 1518 | } |
| 1519 | if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) { |
| 1520 | closeFile(getBuilderInterfaceJavaFileHandle(), true); |
| 1521 | } |
| 1522 | if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) { |
| 1523 | closeFile(getImplClassJavaFileHandle(), true); |
| 1524 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1525 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1526 | if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) { |
| 1527 | closeFile(getEnumClassJavaFileHandle(), isError); |
| 1528 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1529 | |
| 1530 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1531 | * Close all temporary file handles and delete the files. |
| 1532 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1533 | if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) { |
| 1534 | closeFile(getGetterImplTempFileHandle(), true); |
| 1535 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1536 | if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) { |
| 1537 | closeFile(getAttributesTempFileHandle(), true); |
| 1538 | } |
| 1539 | if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) { |
| 1540 | closeFile(getHashCodeImplTempFileHandle(), true); |
| 1541 | } |
| 1542 | if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) { |
| 1543 | closeFile(getToStringImplTempFileHandle(), true); |
| 1544 | } |
| 1545 | if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) { |
| 1546 | closeFile(getEqualsImplTempFileHandle(), true); |
| 1547 | } |
| 1548 | if ((getGeneratedTempFiles() & ENUM_IMPL_MASK) != 0) { |
| 1549 | closeFile(getEnumClassTempFileHandle(), true); |
| 1550 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | /** |
| 1554 | * Returns if the attribute needs to be accessed in a qualified manner or |
| 1555 | * not, if it needs to be imported, then the same needs to be done. |
| 1556 | * |
| 1557 | * @param importInfo import info for the current attribute being added |
| 1558 | * @return status of the qualified access to the attribute |
| 1559 | */ |
| 1560 | public boolean getIsQualifiedAccessOrAddToImportList( |
| 1561 | JavaQualifiedTypeInfo importInfo) { |
| 1562 | boolean isImportPkgEqualCurNodePkg; |
| 1563 | if (importInfo.getClassInfo().contentEquals( |
| 1564 | getGeneratedJavaClassName())) { |
| 1565 | /* |
| 1566 | * if the current class name is same as the attribute class name, |
| 1567 | * then the attribute must be accessed in a qualified manner. |
| 1568 | */ |
| 1569 | return true; |
| 1570 | } else if (importInfo.getPkgInfo() != null) { |
| 1571 | /* |
| 1572 | * If the attribute type is having the package info, it is contender |
| 1573 | * for import list and also need to check if it needs to be a |
| 1574 | * qualified access. |
| 1575 | */ |
| 1576 | isImportPkgEqualCurNodePkg = isImportPkgEqualCurNodePkg(importInfo); |
| 1577 | if (!isImportPkgEqualCurNodePkg) { |
| 1578 | /* |
| 1579 | * If the package of the attribute added is not same as the |
| 1580 | * current class package, then it must either be imported for |
| 1581 | * access or it must be a qualified access. |
| 1582 | */ |
| 1583 | boolean isImportAdded = getJavaImportData().addImportInfo(importInfo); |
| 1584 | if (!isImportAdded) { |
| 1585 | /* |
| 1586 | * If the attribute type info is not imported, then it must |
| 1587 | * be a qualified access. |
| 1588 | */ |
| 1589 | return true; |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | return false; |
| 1594 | } |
| 1595 | |
| 1596 | /** |
| 1597 | * Checks if the import info is same as the package of the current generated |
| 1598 | * java file. |
| 1599 | * |
| 1600 | * @param importInfo import info for an attribute |
| 1601 | * @return true if the import info is same as the current nodes package |
| 1602 | * false otherwise |
| 1603 | */ |
| 1604 | public boolean isImportPkgEqualCurNodePkg(JavaQualifiedTypeInfo importInfo) { |
| 1605 | return getJavaFileInfo().getPackage() |
| 1606 | .contentEquals(importInfo.getPkgInfo()); |
| 1607 | } |
| 1608 | } |