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.javamodel; |
| 17 | |
| 18 | import org.onosproject.yangutils.datamodel.YangType; |
| 19 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
| 20 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo; |
| 21 | import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 22 | import org.onosproject.yangutils.translator.tojava.utils.YangToJavaNamingConflictUtil; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Represents java information corresponding to the YANG type. |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 26 | * |
| 27 | * @param <T> generic parameter for YANG java type |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 28 | */ |
| 29 | public class YangJavaType<T> |
| 30 | extends YangType<T> |
| 31 | implements JavaQualifiedTypeResolver { |
| 32 | |
| 33 | private JavaQualifiedTypeInfo javaQualifiedAccess; |
| 34 | |
| 35 | /** |
| 36 | * Create a YANG leaf object with java qualified access details. |
| 37 | */ |
| 38 | public YangJavaType() { |
| 39 | super(); |
| 40 | setJavaQualifiedInfo(new JavaQualifiedTypeInfo()); |
| 41 | } |
| 42 | |
| 43 | @Override |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 44 | public void updateJavaQualifiedInfo(YangToJavaNamingConflictUtil confilictResolver) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 45 | JavaQualifiedTypeInfo importInfo = getJavaQualifiedInfo(); |
| 46 | |
| 47 | /* |
| 48 | * Type is added as an attribute in the class. |
| 49 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 50 | String className = AttributesJavaDataType.getJavaImportClass(this, false, confilictResolver); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 51 | if (className != null) { |
| 52 | /* |
| 53 | * Corresponding to the attribute type a class needs to be imported, |
| 54 | * since it can be a derived type or a usage of wrapper classes. |
| 55 | */ |
| 56 | importInfo.setClassInfo(className); |
| 57 | String classPkg = AttributesJavaDataType.getJavaImportPackage(this, |
| 58 | false, className); |
| 59 | if (classPkg == null) { |
| 60 | throw new TranslatorException("import package cannot be null when the class is used"); |
| 61 | } |
| 62 | importInfo.setPkgInfo(classPkg); |
| 63 | } else { |
| 64 | /* |
| 65 | * The attribute does not need a class to be imported, for example |
| 66 | * built in java types. |
| 67 | */ |
| 68 | String dataTypeName = AttributesJavaDataType.getJavaDataType(this); |
| 69 | if (dataTypeName == null) { |
| 70 | throw new TranslatorException("not supported data type"); |
| 71 | } |
| 72 | importInfo.setClassInfo(dataTypeName); |
| 73 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 74 | setJavaQualifiedInfo(importInfo); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public JavaQualifiedTypeInfo getJavaQualifiedInfo() { |
| 79 | return javaQualifiedAccess; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void setJavaQualifiedInfo(JavaQualifiedTypeInfo typeInfo) { |
| 84 | javaQualifiedAccess = typeInfo; |
| 85 | } |
| 86 | } |