Runtime Extensibility Flashcards
Runtime Extensibility
Extensibility is a key feature of Java
⚫ Inheritance, method overriding and interfaces provide
mechanisms to achieve this… at compile time.
Sometimes we need runtime extensibility
⚫ Need to make your program more ‘powerful’
⚫ Without recompiling your code.
⚫ Why? What examples are there?
Common Design Pattern 1
Programmer A writes library code.
At a later date, Programmer B writes some application code and integrates codebases at compiler time.
Common Design Pattern 2
Programmer B writes application
At a later date, Programmer C writes some support code that needs to be integrated into Programmer B’s application…
Analysis 1
Case 1 is trivial… Case 2 less so!
▪ Inheritance allows creation of new class (Borg)
▪ Polymorphism allows instances of the Borg class to be still treated as Person instances within Programmer B’s application…
▪ But how does an instance of Borg get created?
▪ Programmer B can’t do it, as the Borg class hasn’t yet been written, and can never predict a Borg invasion.
▪ Programmer C can’t directly do it either, as it is Programmer B’s application that will likely decide when it needs to instantiate a
class!
▪ Remember Programmers do not always release source code for their programs, and then there’s the question of ownership…
Analysis 2
The issue is that programmer B does not know about the exact type of Programmer C’s class…
▪ …so can’t call new().
▪ Programmer B could defer this to Programmer C however!
▪ If Programmer C writes a method for creating Borgs that meets a well defined interface defined by Programmer B…
▪ Programmer B can call it even though it hasn’t been written yet!
▪ Programmer B just needs to define a minimum set of functionality required create an instance of a future class!
Class Factories 1
Solution is something known as a Class Factory
To create a Class Factory, define:
▪ A base type that you expect future classes to be polymorphically compatible with – either a base class or an interface. In our example, this is the Person class…
▪ An abstract class or interface defining a method that creates instances and returns object references of your base type. This is
known as a Class Factory.
▪ Programmers wishing to extended the base type to provide new functionality also extend the class factory and override that
abstract method…
Programmers can then extend your base class, and create their own implementation of a class factory.
Later programmers then:
▪ Create new types that extends/implements the base type
▪ Implement a Class Factory meeting the given interface
▪ Register the class factory with the library code (application specific).
Dynamic Class Loading…
Class factories are an elegant way to help make code extensible
But process still occurs at compile time. i.e. Something still needs to create an instance of BorgFactory!
Not ideal… there are many applications that need to be extended in the field – e.g. browser plugins etc.
Java runs inside a virtual machine
⚫ Separated from the operating system and hardware
⚫ Classes are actually loaded ON DEMAND by your program…
It is possible to control this process
⚫ Define the classes you want at RUNTIME, not compile time
⚫ Type checking is also done at runtime…
Reflection
Ability of a program to introspect, and reason about its own structure…
The Java VM holds a model of all the classes in a program
Programmatically access the description of a class
Programmatically invoke methods (including constructors) on a class.
The class Class implements this functionality.
No constructor, but can obtain a Class instance by:
⚫ Invoking getClass on any instance.
⚫ Using the Class.forName(String s) method.
The Class class
Represents a single class.
Many methods can be used to find out about the class
⚫ getMethods()
⚫ getFields()
⚫ getInterfaces()
⚫ getPackage()
⚫ getSuperclass()
newInstance() – creates an instance of that class!
Check out the Java API documentation for more details:
http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html
newInstance()
Watch out for exceptions.
⚫ if the class cannot be found, this is now a runtime error…
⚫ ClassNotFoundException, InstatiationException, anything from constructor
What type is the returned object?
⚫ The class of the one created (obviously)
But how do we reference it?
⚫ Creating it is OK, but we know we often need to have references to object for later use…
⚫ Don’t KNOW the name of the class at compile time…
instanceof
We can also test the class of an instance at runtime using instanceof
<object> instanceof <class>
Returns a boolean value. True if and only if:
⚫ object is an instance of class or
⚫ object is a subclass of class or
⚫ object implements the interface class.
E.g.
Lecturer l;
if (l instanceof(Person))
{
…
}
</class></object>
Case Study: Class Loading