Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava; |
| 18 | |
| 19 | import java.util.Objects; |
| 20 | |
| 21 | import org.onosproject.yangutils.datamodel.YangNode; |
| 22 | import org.onosproject.yangutils.datamodel.YangType; |
| 23 | import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType; |
| 24 | |
| 25 | import com.google.common.base.MoreObjects; |
| 26 | |
| 27 | /** |
| 28 | * Maintains the information about individual imports in the generated file. |
| 29 | */ |
| 30 | public class JavaQualifiedTypeInfo implements Comparable<JavaQualifiedTypeInfo> { |
| 31 | |
| 32 | /** |
| 33 | * Package location where the imported class/interface is defined. |
| 34 | */ |
| 35 | private String pkgInfo; |
| 36 | |
| 37 | /** |
| 38 | * Class/interface being referenced. |
| 39 | */ |
| 40 | private String classInfo; |
| 41 | |
| 42 | /** |
| 43 | * Default constructor. |
| 44 | */ |
| 45 | public JavaQualifiedTypeInfo() { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get the imported package info. |
| 50 | * |
| 51 | * @return the imported package info |
| 52 | */ |
| 53 | public String getPkgInfo() { |
| 54 | |
| 55 | return pkgInfo; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set the imported package info. |
| 60 | * |
| 61 | * @param pkgInfo the imported package info |
| 62 | */ |
| 63 | public void setPkgInfo(String pkgInfo) { |
| 64 | |
| 65 | this.pkgInfo = pkgInfo; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the imported class/interface info. |
| 70 | * |
| 71 | * @return the imported class/interface info |
| 72 | */ |
| 73 | public String getClassInfo() { |
| 74 | |
| 75 | return classInfo; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Set the imported class/interface info. |
| 80 | * |
| 81 | * @param classInfo the imported class/interface info |
| 82 | */ |
| 83 | public void setClassInfo(String classInfo) { |
| 84 | |
| 85 | this.classInfo = classInfo; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get the import info for an attribute, which needs to be used for code |
| 90 | * generation for import or for qualified access. |
| 91 | * |
| 92 | * @param curNode current data model node for which the java file is being |
| 93 | * generated. |
| 94 | * @param attrType type of attribute being added, it will be null, when the |
| 95 | * child class is added as an attribute |
| 96 | * @param attributeName name of the attribute being added, it will used in |
| 97 | * import info for child class. |
| 98 | * @param isListAttr is the added attribute going to be used as a list |
| 99 | * @return return the import info for this attribute |
| 100 | */ |
| 101 | public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfLeafAttribute(YangNode curNode, |
| 102 | YangType<?> attrType, String attributeName, |
| 103 | boolean isListAttr) { |
| 104 | |
| 105 | JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo(); |
| 106 | |
| 107 | if (attrType == null) { |
| 108 | throw new RuntimeException("missing data type of leaf " + attributeName); |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Current leaves holder is adding a leaf info as a attribute to the |
| 113 | * current class. |
| 114 | */ |
| 115 | String className = AttributesJavaDataType.getJavaImportClass(attrType, isListAttr); |
| 116 | if (className != null) { |
| 117 | /* |
| 118 | * Corresponding to the attribute type a class needs to be imported, |
| 119 | * since it can be a derived type or a usage of wrapper classes. |
| 120 | */ |
| 121 | importInfo.setClassInfo(className); |
| 122 | String classPkg = AttributesJavaDataType.getJavaImportPackage(attrType, isListAttr, className); |
| 123 | if (classPkg == null) { |
| 124 | throw new RuntimeException("import package cannot be null when the class is used"); |
| 125 | } |
| 126 | importInfo.setPkgInfo(classPkg); |
| 127 | } else { |
| 128 | /* |
| 129 | * The attribute does not need a class to be imported, for example |
| 130 | * built in java types. |
| 131 | */ |
| 132 | String dataTypeName = AttributesJavaDataType.getJavaDataType(attrType); |
| 133 | if (dataTypeName == null) { |
| 134 | throw new RuntimeException("not supported data type"); |
| 135 | } |
| 136 | importInfo.setClassInfo(dataTypeName); |
| 137 | } |
| 138 | return importInfo; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get the import info for an attribute, which needs to be used for code |
| 143 | * generation for import or for qualified access. |
| 144 | * |
| 145 | * @param curNode current data model node for which the java file is being |
| 146 | * generated. |
| 147 | * @param attributeName name of the attribute being added, it will used in |
| 148 | * import info for child class. |
| 149 | * @param isListAttr is the added attribute going to be used as a list |
| 150 | * @return return the import info for this attribute |
| 151 | */ |
| 152 | public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfCurNode(YangNode curNode, |
| 153 | String attributeName, boolean isListAttr) { |
| 154 | |
| 155 | JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo(); |
| 156 | |
| 157 | if (!(curNode instanceof HasJavaFileInfo)) { |
| 158 | throw new RuntimeException("missing java file information to get the package details " |
| 159 | + "of attribute corresponding to child node"); |
| 160 | } |
| 161 | /* |
| 162 | * The scenario when we need to add the child class as an attribute in |
| 163 | * the current class. The child class is in the package of the current |
| 164 | * classes package with current classes name. |
| 165 | */ |
| 166 | importInfo.setClassInfo(attributeName); |
| 167 | importInfo.setPkgInfo((((HasJavaFileInfo) curNode).getJavaFileInfo().getPackage() + "." |
| 168 | + ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName()).toLowerCase()); |
| 169 | |
| 170 | return importInfo; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get if the attribute needs to be accessed in a qualified manner or not, |
| 175 | * if it needs to be imported, then the same needs to be done. |
| 176 | * |
| 177 | * @param curNode current cache of the data model node for which java file |
| 178 | * is bing generated |
| 179 | * @param importInfo import info for the current attribute being added |
| 180 | * @return status of the qualified access to the attribute |
| 181 | */ |
| 182 | public static boolean getIsQualifiedAccessOrAddToImportList(YangNode curNode, |
| 183 | JavaQualifiedTypeInfo importInfo) { |
| 184 | |
| 185 | boolean isImportPkgEqualCurNodePkg; |
| 186 | if (!(curNode instanceof HasJavaFileInfo)) { |
| 187 | throw new RuntimeException("missing java file info for getting the qualified access"); |
| 188 | } |
| 189 | if (importInfo.getClassInfo().contentEquals( |
| 190 | ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName())) { |
| 191 | /* |
| 192 | * if the current class name is same as the attribute class name, |
| 193 | * then the attribute must be accessed in a qualified manner. |
| 194 | */ |
| 195 | return true; |
| 196 | } else if (importInfo.getPkgInfo() != null) { |
| 197 | /* |
| 198 | * If the attribute type is having the package info, it is contender |
| 199 | * for import list and also need to check if it needs to be a |
| 200 | * qualified access. |
| 201 | */ |
| 202 | isImportPkgEqualCurNodePkg = isImportPkgEqualCurNodePkg(curNode, importInfo); |
| 203 | if (!isImportPkgEqualCurNodePkg) { |
| 204 | /* |
| 205 | * If the package of the attribute added is not same as the |
| 206 | * current class package, then it must either be imported for |
| 207 | * access or it must be a qualified access. |
| 208 | */ |
| 209 | if (!(curNode instanceof HasJavaImportData)) { |
| 210 | /* |
| 211 | * If the current data model node is not supposed to import |
| 212 | * data, then this is a usage issue and needs to be fixed. |
| 213 | */ |
| 214 | throw new RuntimeException("Current node needs to support Imports"); |
| 215 | } |
| 216 | |
| 217 | boolean isImportAdded = ((HasJavaImportData) curNode).getJavaImportData() |
| 218 | .addImportInfo(curNode, importInfo); |
| 219 | if (!isImportAdded) { |
| 220 | /* |
| 221 | * If the attribute type info is not imported, then it must |
| 222 | * be a qualified access. |
| 223 | */ |
| 224 | return true; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Check if the import info is same as the package of the current generated |
| 233 | * java file. |
| 234 | * |
| 235 | * @param curNode Java identifier of the current data model node |
| 236 | * @param importInfo import info for an attribute |
| 237 | * @return true if the import info is same as the current nodes package |
| 238 | * false otherwise |
| 239 | */ |
| 240 | public static boolean isImportPkgEqualCurNodePkg( |
| 241 | YangNode curNode, JavaQualifiedTypeInfo importInfo) { |
| 242 | |
| 243 | if (!(curNode instanceof HasJavaFileInfo)) { |
| 244 | throw new RuntimeException("missing java file info for the data model node"); |
| 245 | } |
| 246 | return ((HasJavaFileInfo) curNode).getJavaFileInfo().getPackage() |
| 247 | .contentEquals(importInfo.getPkgInfo() |
| 248 | + "." + importInfo.getClassInfo()); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public int hashCode() { |
| 253 | |
| 254 | return Objects.hash(pkgInfo, classInfo); |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public boolean equals(Object obj) { |
| 259 | |
| 260 | if (this == obj) { |
| 261 | return true; |
| 262 | } |
| 263 | if (obj instanceof JavaQualifiedTypeInfo) { |
| 264 | JavaQualifiedTypeInfo other = (JavaQualifiedTypeInfo) obj; |
| 265 | return Objects.equals(pkgInfo, other.pkgInfo) && |
| 266 | Objects.equals(classInfo, other.classInfo); |
| 267 | } |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * check if the import info matches. |
| 273 | * |
| 274 | * @param importInfo matched import |
| 275 | * @return if equal or not |
| 276 | */ |
| 277 | public boolean exactMatch(JavaQualifiedTypeInfo importInfo) { |
| 278 | |
| 279 | return equals(importInfo) |
| 280 | && Objects.equals(pkgInfo, importInfo.getPkgInfo()) |
| 281 | && Objects.equals(classInfo, importInfo.getClassInfo()); |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public String toString() { |
| 286 | |
| 287 | return MoreObjects.toStringHelper(getClass()) |
| 288 | .add("pkgInfo", pkgInfo) |
| 289 | .add("classInfo", classInfo).toString(); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Check that there is no 2 objects with the same class name. |
| 294 | * |
| 295 | * @param other compared import info. |
| 296 | */ |
| 297 | @Override |
| 298 | public int compareTo(JavaQualifiedTypeInfo other) { |
| 299 | |
| 300 | return getClassInfo().compareTo(other.getClassInfo()); |
| 301 | } |
| 302 | |
| 303 | } |