Xbase does not require an explicit return statement for methods body. To adhere to the Java semantics, in the Jbase validator, which all Jbase languages' validators automatically extend, we provide the method
protected void checkMissingReturn(XBlockExpression body)
This should be manually called from your validator passing an XBlockExpression which represents the body of an inferred JvmOperation. It will check that, if the corresponding Java method is not void, a return is specified in all possible paths.
For example, in our JbaseTestlanguage we do
class JbaseTestlanguageValidator extends AbstractJbaseTestlanguageValidator {
@Check
def void checkReturnInOperation(AbstractOperation op) {
checkMissingReturn(op.body as XBlockExpression)
}
because for each AbstractOperation we infer a Java method in the inferrer.
Xbase does not check whether a variable reference refers to a variable which is surely initialized, since in the generated Java code all Xbase variables will be initialized to default values unless a specific initialization expression is specified.
Thus, the following code will be correct
int i;
int j = i;
On the contrary, Java checks variable initialization. For example, the above Java snippet, when compiled with Java, will issue the error
The local variable i may not have been initialized
If you want to enforce the same semantic check in your Jbase language, you can use the following method in your inherited validator.
protected void checkVariableInitialization(XBlockExpression e)
You should call such method only on an XBlockExpression which is the block expression of an element representing a Java method (not on blocks contained in other blocks, otherwise the analysis will raise several false negatives).
For example, the JbaseTestlanguage does the following:
class JbaseTestlanguageValidator extends AbstractJbaseTestlanguageValidator {
@Check
def void checkVariableInitializationInMainBlock(JbaseTestLanguageModel m) {
checkVariableInitialization(m.block)
}
@Check
def void checkVariableInitializationInOperation(AbstractOperation op) {
checkVariableInitialization(op.body as XBlockExpression)
}
}