tags:

views:

158

answers:

3

I'm new to Java, but I understand the basics. I know that interfaces are abstract classes and they're used for emulating multiple inheritance (kind of - I know that in Java this is not allowed). I have this code; would you explain it to me?

Here is part of a method that iterates over a list of classes:

Constructor[] c = aClass.getConstructors();
for (Constructor constructor : c) {
  if (constructor.getParameterTypes().length == 0) {
    AInterface action = (AInterface) constructor.newInstance();
    try {
      action.run(request, response);
    }
  }
}

Here is the interface definition used by the above code:

public interface AInterface 
{
  void run(HttpServletRequest request, HttpServletResponse response);
}
+2  A: 

It's looking for a 0-argument constructor for the class AClass. There will not be more than one. :)

It then creates an instance of that class using that constructor.

It then calls the "run" method of that class with two objects.

Hope this helps.

lavinio
+2  A: 

It uses reflection to create an instance of the class whose java.lang.Class object is stored in the aClass variable, using it's zero-argument constructor (if it has one), and then calls one of its methods (it is assumed to implement AInterface).

That code looks like it's from some web framework. Why are you looking at it? Reflection is one of the more advanced things about Java, not something that beginners usually need to deal with.

For what is calling the method run, if the method run hasn't a body? For what is good?

The class which is created (aClass) is a concrete class, not an interface, and it will implement the interface and contain the body of the run method. You need to learn more about interfaces (Google is your friend).

Esko Luontola
+2  A: 
Constructor[] c = aClass.getConstructors();  // Retrieve all the constructors 
                                             // of the class 'aClass' 
for (Constructor constructor : c) { // Iterate all constructors
    // Until the only default constructor is found.
    if (constructor.getParameterTypes().length == 0) { 
    // Create a new instance of 'aClass' using the default constructor
    // Hope that 'aClass' implements 'AInterface' otherwise ClassCastException ...
    AInterface action = (AInterface) constructor.newInstance();
    try {
      // action has been cast to AInterface - so it can be used to call the run method
      // defined on the AInterface interface. 
      action.run(request, response);
    } // Oops - missing catch and/or finally. That won't compile
  }
}
// Missing a bit of error handling here for the countless reflection exception
// Unless the method is declared as 'throws Exception' or similar

Example of usage:

You have a class somewhere called 'TheClass'

public class TheClass implements AInterface {
    public void run(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("Hello from TheClass.run(...)");
        // Implement the method ...
    }
}

Somewhere in the application, either using Reflection or reading a Configuration file. The application finds out that it should execute 'TheClass'. You do not have any indication about 'TheClass' other that it should implement AInterface and it has an default constructor.

You can create the Class object using

Class aClass = Class.forName("TheClass");

and use 'aClass' in you previous code snippet (after adding the code for errorhandling). You should see in the console

Hello from TheClass.run(...)
vdr