jjujuma
jjujuma

Reputation: 22785

Enforce presence of no-argument constructor at compile time (Java)

I've got a class somewhat like this:

public class Test {

    private final List<ISomeType> things = new LinkedList<ISomeType>();

    public <T extends ISomeType> Test(Class<T> clazz, int order) {
        for (int i = 0; i < order; i++) {
            try {
                this.things.add(clazz.newInstance());
            } catch (Exception e) { 
                // stackoverflowers use your imagination
            }
        }
    }
}

Where I expect and hope the Class clazz has an accessible no-argument constructor. Is there any way I can enforce presence of it at compile time?

Upvotes: 2

Views: 1362

Answers (6)

Eric L
Eric L

Reputation: 21

The link below shows how to solve a similar situation (checking whether the subclasses of a particular class all have no-argument constructor) using Java 6 and the Annotation Processing Tool:

Annotation Processing Tool

Maybe you can adapt their solution to solve your problem.

Upvotes: 1

Daniel Fanjul
Daniel Fanjul

Reputation: 3501

What about this?

interface Provider<T> {
   T get();
}

public class Test {

    private final List<ISomeType> things = new LinkedList<ISomeType>();

    public <T extends ISomeType> Test(Provider<T> provider, int order) {
        for (int i = 0; i < order; i++) {
            try {
                this.things.add(provider.get());
            } catch (Exception e) { 
                // stackoverflowers use your imagination
            }
        }
    }
}

Upvotes: 2

Daniel H.
Daniel H.

Reputation: 1842

There are some tools for code style validation that can be extended to check for this type of requirement. For eclipse (and others), PMD may help you. By looking at the tutorial I think you should be able to write an specific rule to check for constructors without any parameters.

Upvotes: 2

Eddie
Eddie

Reputation: 54421

Your best bet is to create a unit test that checks this for each class you care about, and then run the unit tests at build time. Alternately, you can create a test class -- not distributed with your code -- that does nothing but exercise the no-arg constructors of the classes you care about. Not a great option.

Upvotes: 1

John Meagher
John Meagher

Reputation: 24718

There is no way to enforce constructor requirements at compile time. At runtime you can check class.getConstructors() and ensure there is one that has no args (or just catch the exception like you are in the sample code).

Usually the no-arg constructor requirement is just listed in the Javadoc of the base class or interface.

Upvotes: 4

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

Reflection is about doing things at runtime instead of compile time. Don't use reflection if you want your code to work.

Upvotes: -2

Related Questions