10

I understand that Page.MyFancyPage will return a PageReference to the page located at /apex/MyFancyPage. However I cannot find any documentation that describes what System.Page actually is.

How can a class (Page) have dynamic properties like MyFancyPage in APEX? Or is Page dynamically compiled each time a new Visualforce page is created and accessing the page name returns a PageReference for that page?

Does anyone know where I can learn about this class?

1
  • This is a really interesting question. I always just knew it worked. I tried looking for documentation over the last few minutes on it and I can't find anything. Should be interesting to hear if someone can provide some insight. Commented Jan 22, 2015 at 16:19

2 Answers 2

9

System.Page is a pseudo-class, that is, a class-like object that is missing some properties of a true class. Other such classes include Trigger, Database, Schema, Type, and Object. In each case, they do not have a Type reference (X.class), cannot be constructed directly or indirectly by Type.forName, and have some documented and undocumented properties.

For example, Object has an equals(Object) method and a hashCode() method, but Object isn't documented directly except in a generic manner in other places. All of these classes aren't "real" classes, but have class-like properties, including properties, methods, and interfaces.

These classes cannot be extended, constructed, or queried, and only behave correctly when used as described in the documentation (but you can sometimes do things with them that are not documented, at your own risk).

You won't find any direct documentation, because the System class isn't fully documented beyond what is actually fully implemented and vetted. You can see what Salesforce has chosen to expose via documentation, but you can't simply figure out all the properties it actually has-- it is a pseudo-class.

Also, notably, that java.lang.reflect isn't implemented in Apex Code, which was specifically excluded to allow tighter compilation and, arguably, to prevent developers from playing with features they shouldn't be.

4

What follows is an educated guess - but hopefully with good reasoning to suggest it is correct.

Page is not a class. Page is in fact a sort of map of page names to pages for your org, i.e. it is a variable on the System class and not a subclass.

How do I come to this assertion - firstly, if Page was a class, then we should be able to retrieve some information about it by running: System.debug(Page.class); However this returns the error "Page class does not exist". So we can pretty much rule that out.

Next notice that in Visualforce we can use the Global variable $Page to access a page, such as $Page.MyFancyPage, which provides us a link to the corresponding page. This is effectively identical syntax with just the $ on the front to provide access from the page.

So it would seem that "Page" is a variable on the System class which holds a map of the VF page names and the VF Page References to allow the system to retrieve them dynamically (and also ensure that they are not deleted when referenced).

There is another place I can see clearly this setup occurs - custom labels. System.Label.labelName in apex to retrieve the label or $Label.labelName in Visualforce to retrieve it.

As I say - only an educated guess, but I think it holds water.

2
  • The real mystery for me is how this object is able to dynamically resolve (or not resolve) arbitrary properties like MyFancyPage. Commented Jan 22, 2015 at 16:55
  • @SwisherSweet Everything is an Object, like in Java, except that Object isn't documented. I've had limited success playing around with Object, but note that any direct attempt to play with Object can cause your code to behave... oddly.
    – sfdcfox
    Commented Jan 22, 2015 at 16:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .