|
4 | 4 | import java.util.ArrayList; |
5 | 5 | import java.util.Collection; |
6 | 6 | import java.util.Iterator; |
7 | | -import java.util.List; |
8 | 7 |
|
9 | 8 | import org.json.JSONObject; |
10 | 9 | import org.sagebionetworks.schema.ObjectSchema; |
@@ -156,20 +155,25 @@ private static <T extends JSONEntity> T createEntityFromAdapter(Class<? extends |
156 | 155 | // Now create a new instance of the class |
157 | 156 | try { |
158 | 157 | T newInstance = null; |
159 | | - if(clazz.isInterface()){ |
| 158 | + if (clazz.isInterface()) { |
160 | 159 | String concreteType = extractConcreteType(adapter, clazz); |
| 160 | + Class<T> newInstanceClass; |
161 | 161 | // Use the concrete type to instantiate the object. |
162 | 162 | try { |
163 | | - newInstance = (T) Class.forName(concreteType).newInstance(); |
| 163 | + newInstanceClass = (Class<T>) Class.forName(concreteType); |
164 | 164 | } catch (ClassNotFoundException e) { |
165 | | - throw new IllegalArgumentException(String.format("Unknown %s : '%s'",ObjectSchema.CONCRETE_TYPE, concreteType), e); |
| 165 | + throw new IllegalArgumentException(String.format("Unknown %s : '%s'", ObjectSchema.CONCRETE_TYPE, concreteType), e); |
166 | 166 | } |
167 | | - }else{ |
| 167 | + if (!clazz.isAssignableFrom(newInstanceClass)) { |
| 168 | + throw new IllegalArgumentException(String.format("Unexpected concreteType: \"%s\" is not of type \"%s\"", concreteType, clazz.getName())); |
| 169 | + } |
| 170 | + newInstance = newInstanceClass.newInstance(); |
| 171 | + } else { |
168 | 172 | newInstance = clazz.newInstance(); |
169 | 173 | } |
170 | 174 | newInstance.initializeFromJSONObject(adapter); |
171 | 175 | return newInstance; |
172 | | - } catch (IllegalArgumentException e) { |
| 176 | + } catch (IllegalArgumentException e) { |
173 | 177 | throw e; |
174 | 178 | } catch (Exception e) { |
175 | 179 | throw new JSONObjectAdapterException(e); |
|
0 commit comments