1

I made a new class and named it SelectOption.cls so i could use it as wrapper class, and after a while i noticed that other Apex controller was using System SelectOption class, and that visualforce functionality stopped working. My guess is that i somehow overrode it using my custom class. So i deleted it, but i got new error.

java.lang.AssertionError: apex.bytecodeinterpreter.InterpreterRuntimeException: Unable to load class: com/salesforce/api/fast/List$$lSelectOption$$r

Can someone explain principle of overriding, and if this has to do something with it?

7
  • can't you use another class name instead SelectOption ? – Ratan Paul Apr 8 '17 at 16:49
  • 2
    You could replace the system SelectOption usage with System.SelectOption instead of just SelectOption. – Santanu Halder Apr 8 '17 at 16:55
  • Yeah, i changed the class name but i still get the same error. Santanu, that would be my last option because i don't want to break someone else's code. – Nikola Naumovski Apr 8 '17 at 17:23
  • If you update the existing code to use System.SelectOption then it is not breaking the existing code but it is correcting the existing code. – javanoob Apr 8 '17 at 20:44
  • I just want to undo the changes i made, if it is possible. – Nikola Naumovski Apr 8 '17 at 20:52
4

"Overriding" is the process of replacing one method with another in child classes. What you've actually done is something called "shadowing," which is where you've hidden a system class with a custom class. The order of preferences is kind of explained in Type Resolution and System Namespace for Types:

For a type reference TypeN, the parser first looks up that type as a scalar type.

If TypeN is not found, the parser looks up locally defined types.

If TypeN still is not found, the parser looks up a class of that name.

If TypeN still is not found, the parser looks up system types such as sObjects.

In other words, the preferred order of operations is a variable name, then inner classes, then top level classes, then system classes.

Custom classes are always searched first, and can therefore shadow system classes. It is strongly recommended that you do not name variables or classes the same as system classes, because that effectively removes the ability to use that class.

There are ways to work around this shadowing in most cases, but it requires that you're willing to update every single class affected by it. It's simply a lot easier to not break things to begin with. Naming a class Test, Database, SelectOption, PageReference, Schema, Math, and so on (pretty much anything that's in the documentation that's not a reserved keyword) will break other code. Just don't do it.

To fix this, you're going to need to clear out the content of your pages (change them to just <apex:page />, delete the offending class(es), then restore your code. The first step breaks the metadata association, while the last step obviously forces compilation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.