use of org.eclipse.winery.model.tosca.HasInheritance in project winery by eclipse.
the class IGenericRepository method getDefinitionsChildIdOfParent.
/**
* Determines the id of the parent in the inheritance hierarchy (if exists).
*
* @return the id of the parent class
*/
default Optional<DefinitionsChildId> getDefinitionsChildIdOfParent(HasInheritanceId id) {
final HasInheritance element = (HasInheritance) this.getDefinitions(id).getElement();
final HasType derivedFrom = element.getDerivedFrom();
QName derivedFromType = null;
if (derivedFrom != null) {
derivedFromType = derivedFrom.getTypeAsQName();
}
if (derivedFromType == null) {
return Optional.empty();
} else {
// Instantiate an id with the same class as the current id
DefinitionsChildId parentId;
Constructor<? extends DefinitionsChildId> constructor;
try {
constructor = id.getClass().getConstructor(QName.class);
} catch (NoSuchMethodException | SecurityException e1) {
throw new IllegalStateException("Could get constructor to instantiate parent id", e1);
}
try {
parentId = constructor.newInstance(derivedFromType);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new IllegalStateException("Could not instantiate id for parent", e);
}
return Optional.of(parentId);
}
}
Aggregations