View Javadoc
1   package ca.uhn.fhir.context;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  import static org.apache.commons.lang3.StringUtils.isBlank;
23  
24  import java.lang.reflect.ParameterizedType;
25  import java.lang.reflect.Type;
26  import java.util.Map;
27  
28  import org.hl7.fhir.instance.model.api.IBase;
29  import org.hl7.fhir.instance.model.api.IBaseDatatype;
30  import org.hl7.fhir.instance.model.api.IPrimitiveType;
31  
32  import ca.uhn.fhir.model.api.annotation.DatatypeDef;
33  import ca.uhn.fhir.model.api.annotation.ResourceDef;
34  
35  public class RuntimePrimitiveDatatypeDefinition extends BaseRuntimeElementDefinition<IPrimitiveType<?>> implements IRuntimeDatatypeDefinition {
36  
37  	private Class<?> myNativeType;
38  	private BaseRuntimeElementDefinition<?> myProfileOf;
39  	private Class<? extends IBaseDatatype> myProfileOfType;
40  	private boolean mySpecialization;
41  
42  	public RuntimePrimitiveDatatypeDefinition(DatatypeDef theDef, Class<? extends IPrimitiveType<?>> theImplementingClass, boolean theStandardType) {
43  		super(theDef.name(), theImplementingClass, theStandardType);
44  
45  		String resourceName = theDef.name();
46  		if (isBlank(resourceName)) {
47  			throw new ConfigurationException("Resource type @" + ResourceDef.class.getSimpleName() + " annotation contains no resource name: " + theImplementingClass.getCanonicalName());
48  		}
49  
50  		mySpecialization = theDef.isSpecialization();
51  		myProfileOfType = theDef.profileOf();
52  		if (myProfileOfType.equals(IBaseDatatype.class)) {
53  			myProfileOfType = null;
54  		}
55  
56  		determineNativeType(theImplementingClass);
57  	}
58  
59  	private void determineNativeType(Class<? extends IPrimitiveType<?>> theImplementingClass) {
60  		Class<?> clazz = theImplementingClass;
61  		while (clazz.equals(Object.class) == false) {
62  			Type type = clazz.getGenericSuperclass();
63  			if (type instanceof ParameterizedType) {
64  				ParameterizedType superPt = (ParameterizedType) type;
65  				Type rawType = superPt.getRawType();
66  				if (rawType instanceof Class) {
67  					Class<?> rawClass = (Class<?>) rawType;
68  					if (rawClass.getName().endsWith(".BasePrimitive") || rawClass.getName().endsWith(".PrimitiveType")) {
69  						Type typeVariable = superPt.getActualTypeArguments()[0];
70  						if (typeVariable instanceof Class) {
71  							myNativeType = (Class<?>) typeVariable;
72  							break;
73  						}
74  					}
75  				}
76  			}
77  			clazz = clazz.getSuperclass();
78  		}
79  	}
80  
81  	@Override
82  	public ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum getChildType() {
83  		return ChildTypeEnum.PRIMITIVE_DATATYPE;
84  	}
85  
86  	public Class<?> getNativeType() {
87  		return myNativeType;
88  	}
89  
90  	@Override
91  	public Class<? extends IBaseDatatype> getProfileOf() {
92  		return myProfileOfType;
93  	}
94  
95  	@Override
96  	public boolean isProfileOf(Class<? extends IBaseDatatype> theType) {
97  		if (myProfileOfType != null) {
98  			if (myProfileOfType.equals(theType)) {
99  				return true;
100 			} else if (myProfileOf instanceof IRuntimeDatatypeDefinition) {
101 				return ((IRuntimeDatatypeDefinition) myProfileOf).isProfileOf(theType);
102 			}
103 		}
104 		return false;
105 	}
106 
107 	@Override
108 	public boolean isSpecialization() {
109 		return mySpecialization;
110 	}
111 
112 	@Override
113 	void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
114 		super.sealAndInitialize(theContext, theClassToElementDefinitions);
115 
116 		if (myProfileOfType != null) {
117 			myProfileOf = theClassToElementDefinitions.get(myProfileOfType);
118 			if (myProfileOf == null) {
119 				StringBuilder b = new StringBuilder();
120 				b.append("Unknown profileOf value: ");
121 				b.append(myProfileOfType);
122 				b.append(" in type ");
123 				b.append(getImplementingClass().getName());
124 				b.append(" - Valid types: ");
125 				b.append(theClassToElementDefinitions.keySet());
126 				throw new ConfigurationException(b.toString());
127 			}
128 		}
129 	}
130 
131 }