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