Before getting to the main subject of this chapter, we will first implement some constraint checks that are complementary to type checking. Correct access to fields and methods will be checked in the next chapter, where we implement a custom scoping mechanism.
The SmallJava parser accepts input with cyclic class hierarchies, for instance:
class A extends C {} class C extends B {} class B extends A {}
This cannot be checked in the parser; we must write a validator @Check
method to mark such situations as errors (this is similar to the validator for the Entities DSL in Chapter 4, Validation). As you will see later in this chapter, we will often need to traverse the inheritance hierarchy of a class to perform additional checks. Also in this case, it is our job to avoid an infinite loop in case of cycles. We write a utility method in SmallJavaModelUtil
that collects all the classes in the hierarchy of a given SJClass
, to avoid entering...