7

In a Java source, I don't want use some package. For instance, I don't want any reference to swing, or io, or other.

Is there a system to check that, at compile time, or at test time?

For instance, with supposed annotation

@NoPackage("javax.swing")
class Foo
{
    private JFrame fram; // NOT OK.
}

Why I need this? Because I have an application with swing, and I want refactor it with a part which uses swing, and other which doesn't, because I want to port it to other ui stuff (web, pda, etc).

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Istao
  • 7,425
  • 6
  • 32
  • 39
  • Why would you need to do this? The compiler will tell you which packages are not present by giving you errors when you try to use methods/Objects/etc. from that package. – fireshadow52 Apr 11 '12 at 14:12
  • 1
    @fireshadow52 The OP isn't trying to use a non-existent package. He just wants to forbid the use of some existing ones. – adarshr Apr 11 '12 at 14:14
  • @adarshr Okay thanks. Sorry I misunderstood. +1 to the question now. – fireshadow52 Apr 11 '12 at 14:16
  • You can scan the source for the package names, or scan the .class files. Adarshr's scheme would probably work too. – Hot Licks Apr 11 '12 at 14:25
  • Hi, I edit my question to answer fireshadow52 question. – Istao Apr 11 '12 at 14:30

6 Answers6

3

I am not aware of such a tool, but it is not that complex to create one. References to all used classes are coded in classfile. Using some classfile library, classfiles can be inspected, and all used classes (or packages) listed. If annotations like @NoPaquage inserted, then the tool could just check if the class really dosent use that packages.

Community
  • 1
  • 1
Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
  • Thnaks.I already use spring, wich use AspectJ. Do you know if there is something to do that in these tools ? – Istao Apr 11 '12 at 14:41
2

I can think of one rather hacky way to do this. However, you need to have a list of such forbidden classes, not just packages. Say, you want to forbid the usage of javax.swing.JFrame.

Just create the below fake class.

package javax.swing;

class JFrame {
}

Notice that the class isn't public, so any attempt to import it leads to the compilation error "The type javax.swing.JFrame is not visible."

You could create a JAR file out of all such forbidden classes and make sure they get loaded last by the classloader. This ensures that a compiler error definitely occurs. You can simply choose to include/exlude this JAR file whenever you want to run this test - a trivial task if using ant.

adarshr
  • 61,315
  • 23
  • 138
  • 167
0

I think that this would be very difficult, since there is also the possibility of a class using reflection to get at what it wants.

If I was attempting to do this, I would write my own custom classloader, but I'm not sure if the classloader interface known which class is actually trying to load. I think you could use a custom classloader if you want to forbid all the code from using a certain package, but if you want it more class specific, you may need something manual like grep-ing the code?

John Farrelly
  • 7,289
  • 9
  • 42
  • 52
0

Something like this:

$ find srcTree -name \*.java | xargs grep javax\\.swing
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

You can't have the JVM filter the code you are writing. However, you could use generics to restrict the classes that are allowed in all public APIs. You can also restrict inheritance like this:

class Parent {
    public Parent() throws Exception {
        if (this instanceof Child)
            throw new Exception("It's a child");
    }
}

public class Child extends Parent {
    public Child() throws Exception {
        super();
    }
}
Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
0

You can choose the packaqe you want to use in the import area.

In Eclipse you can choose the package your class come from.

You have to delete the import line for choose the class in another package.

cl-r
  • 1,264
  • 1
  • 12
  • 26