I want to check (actually prohibit) the call of the method of the super class from the instance of its subclass.
Example (here the Generics wouldn't be needed but I'm using it because I want to make this example close to my real situation):
abstract class GenericFoo<T> { }
class IntegerFoo extends GenericFoo<Integer> { }
class StringFoo extends GenericFoo<String> { }
abstract class GenericFooPrinter<T> {
void print(GenericFoo<?> genericFoo) {
System.out.println("GenericFooPrinter...");
System.out.println(genericFoo);
}
}
public class StringFooPrinter extends GenericFooPrinter<String> {
void print(StringFoo stringFoo) {
System.out.println("StringFooPrinter...");
super.print(stringFoo);
}
}
then
public class FooApplication {
public static void main(String[] args) {
var stringFooPrinter = new StringFooPrinter();
var integerFoo = new IntegerFoo();
var stringFoo = new StringFoo();
// the types unmatch (`String` of the instance vs `Integer` of the argument),
// so this calls `GenericFooPrinter.print`, which I want IntelliJ to check by custom-inspection, etc.
stringFooPrinter.print(integerFoo);
// this calls `StringFooPrinter.print`, which is okay
// (though it actually calls `GenericFooPrinter.print` via `super` inside it).
stringFooPrinter.print(stringFoo);
}
}
Now, how should I make IntelliJ IDEA to inspect when stringFooPrinter
is calling the method of its superclass, i.e. GenericFooPrinter.print
? Custom Inspection might be the way to go but any method that works is fine, but I don't want to change the method name to something else from .print
.
(Real Situation - probably need not to understand: I'm trying to override jooq's .get
and .set
method in a generated subclass from its ancestor class AbstractRecord
, and match the type of the argument TableField<XXXRecord>
to the type parameter XXXRecord
of its class, and inspect if the types unmatch and thus it calls the .get
or .set
of AbstractRecord
against the wrong field. Chainging the method name would work but I want to avoid it. I know .getId()
, etc. exists but I want to customize in this way.)
abstract
, so that the compiler can enforce that implementing classes overwrite it. Making a method available in a base class but not allowing people to use it seems weird. Why does it even exist in that case? I don't understand the last paragraph about jooq, but it sounds a bit like you're working against that framework. – Roberts.get
is very permissive and actually accepts the wrong fields, too. It is usually called internally from a subclass with a proper field, but it is public and also allowed for the user to call by yourself (with wrong fields, too). For example,articleRecord.get(TITLE)
, which is actually callingAbstractRecord.get(TITLE)
, is correctly okay but unfortunatellyarticleRecord.get(DATE_OF_BIRTH)
is also "okey" as per argument type but causes runtime error. – Waltman