tags:

views:

355

answers:

11

I am working on a project in which, to resolve a versioning issue, I am creating many classes which will never be tested and must not ever be used - they just need to be there to keep the compiler happy. I'd like to make sure they are never used. This morning I had the idea of throwing an exception in a static initialization block:

public class Dummy {

    static {
        throw new IllegalStateException("Do not use this class!");
    }
}

But the compiler doesn't like it. Can you think of any way to do this?

EDIT: to be clear (my fault I wasn't clear before), these won't just be empty classes. There will be all sorts of code in here, and by "keep the compiler happy" I did indeed mean that elsewhere I will be instantiating them, calling methods etc etc. I want this code elsewhere to compile but fail at runtime. I have accepted Jon's answer but will also be using @Deprecated and documenting extensively as appropriate.

+10  A: 

@Deprecated

dfa
+6  A: 

Give it a private default constructor, and make the class final.

jjnguy
That won't stop the class being loaded/initialized - but it may very well break the compilation that the OP needs to use.
Jon Skeet
+7  A: 

Just add a dummy condition:

public class Dummy {    
    static {
        if (true) {
            throw new IllegalStateException("Do not use this class!");
        }
    }
}

I'm not really sure I like this, but it may do what you want it to. Are you sure there's no alternatively which would let you get away without having a completely useless class like this?

Jon Skeet
Beat me to it, damn your eyes! :)
skaffman
Wouldn't it be better to make the class unusable at compile time instead of runtime?
jjnguy
Why the if statement?
Thorbjørn Ravn Andersen
Because without it the compiler barfs. The dummy if tricks it into accepting the statement.
skaffman
@jjnguy: I've been assuming that the OP basically knows what he wants to do. The fact that he's trying "to keep the compiler happy" suggests that it has to be usable at compile time but not execution time. We don't know the details, admittedly - and as I suggested at the bottom, it's worth trying to avoid this if possible - but I was trying to answer the question as posed.
Jon Skeet
Oh Jon, how wise you are. Some day I will learn some mind reading, then I will be able to perfectly answer questions before they are asked. Muahaha!!
jjnguy
I'd be a little reluctant to stop the class from loading - I'd rather stop the class from being constructed with something like this - unless you're trying to stop some other static bits in this class from executing.Problem is classpath scanners for example load classes arbitrarily, they don't construct them and sometime in the future this might occur? You only actually "use" the class if you construct it so rather put the blocker on the constructor rather than on class load.
Michael Wiles
A: 

I don't think you can do that, though you can put a @deprecated tag before the class declaration. This way the compiler will give you a warning if you try to use it, but it will still compile.

Torandi
+2  A: 

You may get around the compiler with something like:

public class Dummy {

    static {
        if (true) 
            throw new IllegalStateException("Do not use this class!");
    }
}

But that would be cheating ;-)

Avi
+2  A: 

If they are never referenced, make the classes default scope, so no other types outside of the package can see them. If they can't see them they can't reference them (without using reflection that is).

class Dummy {
    //no-op
}
Rich Seller
A: 

Use AssertionError instead of IllegalStateException

KitsuneYMG
+2  A: 

Assuming you don't want IllegalStateExceptions potentially being thrown in your production code, use assertions and make sure they are enabled on your QA/Test environment. The code is slightly nicer too:

public class Dummy {
    static {
        //This class should never be initialised!
        assert false : "This class should never be initialised";
    }

    ...
}
Alex Spurling
A: 

You could use your own ClassLoader and check every class against a black list of classes.

Andre Pareis
A: 

What do you mean by "use"? If you need to have them in there, it sounds like the compiler needs to be able to create the classes. However, if you don't want any instances of the class to be created, then create an Assertion or throw an exception in the actual constructor.

Larry Watanabe
+1  A: 

Why don't you just not put that class into the jar that gets deployed. That way it'll be there at compile time but if someone tried to access it at runtime you'll get a ClassNotFoundException.

Michael Wiles