Skip to content

Generated property getter methods don't handle the case when the property doesn't apply #2593

@AaronGreenhouse

Description

@AaronGreenhouse

Summary

When updating the FlowLatencyAnalysisSwitch to use the new generated property getter methods, I discovered that the generated property set code doesn't nicely handle the case where the property doesn't to apply to an AADL element. The underlying property lookup code throws a PropertyDoesNotApplyToHolderException. This is allowed to propagate to the caller. This would be fine if there were an easy way to test if the property applies, but there isn't one. You really just have to try it and see what happens. This exception should not be propagated, but caught internally to take advantage of the Optional class.

Expected behavior

I expected the generated property getter method to return an empty Optional instance in this case, same as when the property value is undefined.

Actual behavior

The underlying PropertyDoesNotApplyToHolderException propagates to the caller of the method.

** Possible fix **

I fixed this by updating CodeGenUtil.lookupProperty() on my local branch to be

	public static PropertyExpression lookupProperty(Property property, NamedElement lookupContext,
			Optional<Mode> mode) {
		try {
			Optional<PropertyExpression> modalValue = mode.map(m -> {
				PropertyAssociation association = lookupContext.getPropertyValue(property).first();
				if (association == null) {
					PropertyExpression defaultValue = property.getDefaultValue();
					if (defaultValue == null) {
						throw new PropertyNotPresentException(lookupContext, property, "No property value");
					} else {
						return defaultValue;
					}
				} else {
					return association.valueInMode(m);
				}
			});
			return modalValue.orElseGet(() -> lookupContext.getNonModalPropertyValue(property));
		} catch (final PropertyDoesNotApplyToHolderException e) {
			throw new PropertyNotPresentException(lookupContext, property, "Property does not apply");
		}
	}

This seems to do what I want, but I'm not sure if there are other places I need to catch the exception in the code used by the generated method.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions