Does anyone know of a way to use the Tooling API to determine which Apex class the current class extends from? I have looked at the SymbolTable but cannot see any indication that this information is revealed.
Thanks for the help.
Cheers!
Does anyone know of a way to use the Tooling API to determine which Apex class the current class extends from? I have looked at the SymbolTable but cannot see any indication that this information is revealed.
Thanks for the help.
Cheers!
The answer is that the Tooling API does not support this information ...at least not in v29.0
Here are some pure-Apex solutions for determining whether a given Apex Class extends another given Apex Class.
(Option 1) Query the ApexClass object for its Body field, and parse out the body as a string to find the class that it extends
// Query for the class definition, retrieving the class Body
ApexClass cls = [select Id,Name,Body from ApexClass where Name = 'MyApexClass' limit 1];
String body = String.valueOf(cls.Body);
String classDef = body.substring(0,body.indexOf('{'));
String classExtended;
if (classDef.contains('extends')){
Integer extendsInitial = classDef.indexOf(' extends ');
Integer extendsStarts = extendsInitial+9;
Integer extendsEnds = classDef.indexOf(' ',extendsStarts);
classExtended = body.substring(extendsStarts,extendsEnds);
System.debug('Class ' + cls.Name + ' extends: ' + classExtended);
} else {
System.debug('Class ' + cls.Name + ' does not extend another class');
}
You can then compare classExtended
to whatever class you are wondering whether the class extends.
(Scenario 2) Use System.Type and try/catch to determine whether a given type is an instance of another type
String classToCheck = 'MyClassThatExtendsAnother';
String classExtended = 'MyBaseClass';
System.Type t = System.Type.forName(classToCheck);
MyBaseClass c;
try {
c = (MyBaseClass) JSON.deserialize('{}',t);
} catch (Exception ex){}
if (c == null){
System.debug('Class ' + classToCheck + ' does not extend ' + classExtended);
} else {
System.debug('Class ' + classToCheck + ' DOES extend ' + classExtended);
}