Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 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; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 20 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 25 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 26 | import com.google.common.base.MoreObjects; |
| 27 | |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportClass; |
| 29 | import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportPackage; |
| 30 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 31 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 32 | * Represents the information about individual imports in the generated file. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 33 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 34 | public class JavaQualifiedTypeInfo |
| 35 | implements Comparable<JavaQualifiedTypeInfo> { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 36 | /** |
| 37 | * Package location where the imported class/interface is defined. |
| 38 | */ |
| 39 | private String pkgInfo; |
| 40 | |
| 41 | /** |
| 42 | * Class/interface being referenced. |
| 43 | */ |
| 44 | private String classInfo; |
| 45 | |
| 46 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 47 | * Creates a java qualified type info object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 48 | */ |
| 49 | public JavaQualifiedTypeInfo() { |
| 50 | } |
| 51 | |
| 52 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 53 | * Returns the imported package info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 54 | * |
| 55 | * @return the imported package info |
| 56 | */ |
| 57 | public String getPkgInfo() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 58 | return pkgInfo; |
| 59 | } |
| 60 | |
| 61 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 62 | * Sets the imported package info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 63 | * |
| 64 | * @param pkgInfo the imported package info |
| 65 | */ |
| 66 | public void setPkgInfo(String pkgInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 67 | this.pkgInfo = pkgInfo; |
| 68 | } |
| 69 | |
| 70 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 71 | * Returns the imported class/interface info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 72 | * |
| 73 | * @return the imported class/interface info |
| 74 | */ |
| 75 | public String getClassInfo() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 76 | return classInfo; |
| 77 | } |
| 78 | |
| 79 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 80 | * Sets the imported class/interface info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 81 | * |
| 82 | * @param classInfo the imported class/interface info |
| 83 | */ |
| 84 | public void setClassInfo(String classInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 85 | this.classInfo = classInfo; |
| 86 | } |
| 87 | |
| 88 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 89 | * Updates the leaf's java information. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 90 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 91 | * @param leaf leaf whose jave information is being updated |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 92 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 93 | public static void updateLeavesJavaQualifiedInfo(JavaLeafInfoContainer leaf) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 94 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 95 | JavaQualifiedTypeInfo importInfo = leaf.getJavaQualifiedInfo(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 96 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 97 | if (leaf.getDataType() == null) { |
| 98 | throw new TranslatorException("missing data type of leaf " + leaf.getName()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Current leaves holder is adding a leaf info as a attribute to the |
| 103 | * current class. |
| 104 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 105 | String className = AttributesJavaDataType.getJavaImportClass(leaf.getDataType(), leaf.isLeafList()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 106 | if (className != null) { |
| 107 | /* |
| 108 | * Corresponding to the attribute type a class needs to be imported, |
| 109 | * since it can be a derived type or a usage of wrapper classes. |
| 110 | */ |
| 111 | importInfo.setClassInfo(className); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 112 | String classPkg = AttributesJavaDataType.getJavaImportPackage(leaf.getDataType(), |
| 113 | leaf.isLeafList(), className); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 114 | if (classPkg == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 115 | throw new TranslatorException("import package cannot be null when the class is used"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 116 | } |
| 117 | importInfo.setPkgInfo(classPkg); |
| 118 | } else { |
| 119 | /* |
| 120 | * The attribute does not need a class to be imported, for example |
| 121 | * built in java types. |
| 122 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 123 | String dataTypeName = AttributesJavaDataType.getJavaDataType(leaf.getDataType()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 124 | if (dataTypeName == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 125 | throw new TranslatorException("not supported data type"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 126 | } |
| 127 | importInfo.setClassInfo(dataTypeName); |
| 128 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 132 | * Returns the import info for an attribute, which needs to be used for code |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 133 | * generation for import or for qualified access. |
| 134 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 135 | * @param curNode current data model node for which the java file is being |
| 136 | * generated |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 137 | * @param attributeName name of the attribute being added, it will used in |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 138 | * import info for child class |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 139 | * @return return the import info for this attribute |
| 140 | */ |
| 141 | public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfCurNode(YangNode curNode, |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 142 | String attributeName) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 143 | |
| 144 | JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo(); |
| 145 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 146 | if (!(curNode instanceof JavaFileInfoContainer)) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 147 | throw new TranslatorException("missing java file information to get the package details " |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 148 | + "of attribute corresponding to child node"); |
| 149 | } |
| 150 | /* |
| 151 | * The scenario when we need to add the child class as an attribute in |
| 152 | * the current class. The child class is in the package of the current |
| 153 | * classes package with current classes name. |
| 154 | */ |
| 155 | importInfo.setClassInfo(attributeName); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 156 | importInfo.setPkgInfo((((JavaFileInfoContainer) curNode).getJavaFileInfo().getPackage() + "." |
| 157 | + ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName()).toLowerCase()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 158 | |
| 159 | return importInfo; |
| 160 | } |
| 161 | |
| 162 | /** |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 163 | * Returns the java qualified type information for the wrapper classes. |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 164 | * |
| 165 | * @param referredTypesAttrInfo attribute of referred type |
| 166 | * @return return the import info for this attribute |
| 167 | */ |
| 168 | public static JavaQualifiedTypeInfo getQualifiedInfoOfFromString(JavaAttributeInfo referredTypesAttrInfo) { |
| 169 | /* |
| 170 | * Get the java qualified type information for the wrapper classes and |
| 171 | * set it in new java attribute information. |
| 172 | */ |
| 173 | JavaQualifiedTypeInfo qualifiedInfoOfFromString = new JavaQualifiedTypeInfo(); |
| 174 | qualifiedInfoOfFromString.setClassInfo( |
| 175 | getJavaImportClass(referredTypesAttrInfo.getAttributeType(), true)); |
| 176 | qualifiedInfoOfFromString.setPkgInfo( |
| 177 | getJavaImportPackage(referredTypesAttrInfo.getAttributeType(), true, null)); |
| 178 | return qualifiedInfoOfFromString; |
| 179 | } |
| 180 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 181 | |
| 182 | @Override |
| 183 | public int hashCode() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 184 | return Objects.hash(pkgInfo, classInfo); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public boolean equals(Object obj) { |
| 189 | |
| 190 | if (this == obj) { |
| 191 | return true; |
| 192 | } |
| 193 | if (obj instanceof JavaQualifiedTypeInfo) { |
| 194 | JavaQualifiedTypeInfo other = (JavaQualifiedTypeInfo) obj; |
| 195 | return Objects.equals(pkgInfo, other.pkgInfo) && |
| 196 | Objects.equals(classInfo, other.classInfo); |
| 197 | } |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 202 | * Checks if the import info matches. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 203 | * |
| 204 | * @param importInfo matched import |
| 205 | * @return if equal or not |
| 206 | */ |
| 207 | public boolean exactMatch(JavaQualifiedTypeInfo importInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 208 | return equals(importInfo) |
| 209 | && Objects.equals(pkgInfo, importInfo.getPkgInfo()) |
| 210 | && Objects.equals(classInfo, importInfo.getClassInfo()); |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public String toString() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 215 | return MoreObjects.toStringHelper(getClass()) |
| 216 | .add("pkgInfo", pkgInfo) |
| 217 | .add("classInfo", classInfo).toString(); |
| 218 | } |
| 219 | |
| 220 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 221 | * Checks that there is no 2 objects with the same class name. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 222 | * |
| 223 | * @param other compared import info. |
| 224 | */ |
| 225 | @Override |
| 226 | public int compareTo(JavaQualifiedTypeInfo other) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 227 | return getClassInfo().compareTo(other.getClassInfo()); |
| 228 | } |
| 229 | |
| 230 | } |