blob: 370e0f687df34ee1673e7c19f87f9831302d0b2f [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-03-23 15:30:27 +05303 *
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
17package org.onosproject.yangutils.translator.tojava;
18
19import java.util.Objects;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053020
Vinod Kumar S38046502016-03-23 15:30:27 +053021import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053022import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053023import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Vinod Kumar S38046502016-03-23 15:30:27 +053024import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053025
Vinod Kumar S38046502016-03-23 15:30:27 +053026import com.google.common.base.MoreObjects;
27
Gaurav Agrawal338735b2016-04-18 18:53:11 +053028import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportClass;
29import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportPackage;
30
Vinod Kumar S38046502016-03-23 15:30:27 +053031/**
Bharat saraswald9822e92016-04-05 15:13:44 +053032 * Represents the information about individual imports in the generated file.
Vinod Kumar S38046502016-03-23 15:30:27 +053033 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053034public class JavaQualifiedTypeInfo
35 implements Comparable<JavaQualifiedTypeInfo> {
Vinod Kumar S38046502016-03-23 15:30:27 +053036 /**
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 saraswald9822e92016-04-05 15:13:44 +053047 * Creates a java qualified type info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053048 */
49 public JavaQualifiedTypeInfo() {
50 }
51
52 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053053 * Returns the imported package info.
Vinod Kumar S38046502016-03-23 15:30:27 +053054 *
55 * @return the imported package info
56 */
57 public String getPkgInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053058 return pkgInfo;
59 }
60
61 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053062 * Sets the imported package info.
Vinod Kumar S38046502016-03-23 15:30:27 +053063 *
64 * @param pkgInfo the imported package info
65 */
66 public void setPkgInfo(String pkgInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053067 this.pkgInfo = pkgInfo;
68 }
69
70 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053071 * Returns the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053072 *
73 * @return the imported class/interface info
74 */
75 public String getClassInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053076 return classInfo;
77 }
78
79 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053080 * Sets the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053081 *
82 * @param classInfo the imported class/interface info
83 */
84 public void setClassInfo(String classInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053085 this.classInfo = classInfo;
86 }
87
88 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053089 * Updates the leaf's java information.
Vinod Kumar S38046502016-03-23 15:30:27 +053090 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053091 * @param leaf leaf whose jave information is being updated
Vinod Kumar S38046502016-03-23 15:30:27 +053092 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053093 public static void updateLeavesJavaQualifiedInfo(JavaLeafInfoContainer leaf) {
Vinod Kumar S38046502016-03-23 15:30:27 +053094
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053095 JavaQualifiedTypeInfo importInfo = leaf.getJavaQualifiedInfo();
Vinod Kumar S38046502016-03-23 15:30:27 +053096
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053097 if (leaf.getDataType() == null) {
98 throw new TranslatorException("missing data type of leaf " + leaf.getName());
Vinod Kumar S38046502016-03-23 15:30:27 +053099 }
100
101 /*
102 * Current leaves holder is adding a leaf info as a attribute to the
103 * current class.
104 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530105 String className = AttributesJavaDataType.getJavaImportClass(leaf.getDataType(), leaf.isLeafList());
Vinod Kumar S38046502016-03-23 15:30:27 +0530106 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 Se4b9b0c2016-04-30 21:09:15 +0530112 String classPkg = AttributesJavaDataType.getJavaImportPackage(leaf.getDataType(),
113 leaf.isLeafList(), className);
Vinod Kumar S38046502016-03-23 15:30:27 +0530114 if (classPkg == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530115 throw new TranslatorException("import package cannot be null when the class is used");
Vinod Kumar S38046502016-03-23 15:30:27 +0530116 }
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 Se4b9b0c2016-04-30 21:09:15 +0530123 String dataTypeName = AttributesJavaDataType.getJavaDataType(leaf.getDataType());
Vinod Kumar S38046502016-03-23 15:30:27 +0530124 if (dataTypeName == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530125 throw new TranslatorException("not supported data type");
Vinod Kumar S38046502016-03-23 15:30:27 +0530126 }
127 importInfo.setClassInfo(dataTypeName);
128 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530129 }
130
131 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530132 * Returns the import info for an attribute, which needs to be used for code
Vinod Kumar S38046502016-03-23 15:30:27 +0530133 * generation for import or for qualified access.
134 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530135 * @param curNode current data model node for which the java file is being
136 * generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530137 * @param attributeName name of the attribute being added, it will used in
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530138 * import info for child class
Vinod Kumar S38046502016-03-23 15:30:27 +0530139 * @return return the import info for this attribute
140 */
141 public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfCurNode(YangNode curNode,
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530142 String attributeName) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530143
144 JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
145
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530146 if (!(curNode instanceof JavaFileInfoContainer)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530147 throw new TranslatorException("missing java file information to get the package details "
Vinod Kumar S38046502016-03-23 15:30:27 +0530148 + "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 Se4b9b0c2016-04-30 21:09:15 +0530156 importInfo.setPkgInfo((((JavaFileInfoContainer) curNode).getJavaFileInfo().getPackage() + "."
157 + ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName()).toLowerCase());
Vinod Kumar S38046502016-03-23 15:30:27 +0530158
159 return importInfo;
160 }
161
162 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +0530163 * Returns the java qualified type information for the wrapper classes.
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530164 *
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 S38046502016-03-23 15:30:27 +0530181
182 @Override
183 public int hashCode() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530184 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 saraswalcc1cdab2016-04-16 02:28:25 +0530202 * Checks if the import info matches.
Vinod Kumar S38046502016-03-23 15:30:27 +0530203 *
204 * @param importInfo matched import
205 * @return if equal or not
206 */
207 public boolean exactMatch(JavaQualifiedTypeInfo importInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530208 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 S38046502016-03-23 15:30:27 +0530215 return MoreObjects.toStringHelper(getClass())
216 .add("pkgInfo", pkgInfo)
217 .add("classInfo", classInfo).toString();
218 }
219
220 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530221 * Checks that there is no 2 objects with the same class name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530222 *
223 * @param other compared import info.
224 */
225 @Override
226 public int compareTo(JavaQualifiedTypeInfo other) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530227 return getClassInfo().compareTo(other.getClassInfo());
228 }
229
230}