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