tags:

views:

126

answers:

4

In Eclipse 3.5, say I have a package structure like this:

 tom.package1
 tom.package1.packageA
 tom.package1.packageB

if I right click on an the tom.package1 package and go to Refactor->Rename, an option "Rename subpackages" appears as a checkbox. If I select it, and then rename tom.package1 to tom.red my package structure ends up like this:

tom.red
tom.red.packageA
tom.red.packageB

Yet I hear that Java's packages are not hierarchical. The Java Tutorials back that up (see the section on Apparent Hierarchies of Packages). It certainly seems like Eclipse is treating packages as hierarchical in this case.

I was curious why access specifiers couldn't allow/restrict access to "sub-packages" in a previous question because I KNEW I had seen "sub-packages" referenced somewhere before.

So are Eclipse's refactoring tools intentionally misleading impressionable young minds by furthering the "sub-package" myth? Or am I misinterpreting something here?

+7  A: 

Eclipse can't possibly violate the JLS in this case, because it has nothing to do with compiling or running Java source or bytecode.

The refactoring tools behave as they do because that behaviour is useful to developers. The behaviour is useful to developers because, for many intents and purposes, we do treat packages as hierarchal (a.b.c has some kind of relationship with a.b, even if that relationship is not consistent from project to project). That doesn't mean Java treats them as hierarchal intrinsically.

One example where people treat packages as very hierarchal is in configuring a logging framework such as log4j. Again, it's not intrinsic to log4j, but that's how people use it in practice.

Mark Peters
That's funny, I had thought one of the core parts of Eclipses JDT was an incremental compiler. How did they pull that off without having any contact with the language spec, I wonder? I get what you're saying about the refactoring tools being for convenience only (and I do find them to be), but Eclipse DOES seem to ALWAYS want to treat a.b.c as related to a.b, when the language spec explicitly says it might not be. My question might have been phrased a little sensationally. Maybe it should been "Are the refactoring tools pretending relationships in the language which aren't really there?
Tom Tresansky
@Tom: First off, I said Eclipse can't possibly violate the JLS **in this case**. Refactoring tools have nothing to do with the compiling nor the runtime behaviour of your project. It would be like creating a bash script to do the same and saying that violates the JLS. Second I don't see how you can say Eclipse always seems to treat them as related **when the default package rename does not rename subpackages**. Is this entire question because they use the term "subpackage"?
Mark Peters
+1  A: 

Java's packages are not hierarchical, but Eclipse stores packages on your system's file structure.

tom.package1.packageA is represented on a Windows file system as tom/package1/packageA.

When you ask Eclipse to refactor a package name, you're asking Eclipse to change the name of the file system directory structure.

You can have packages in Eclipse like:

tom.package1.packageA
tom.package2.packageB
tom.package3.packageC

You'll just have different 2nd level file system directories.

Gilbert Le Blanc
`javac` *also* expects your source files to have such a file hierarchy.
Mark Peters
And more importantly so.
danben
+5  A: 

Java packages are not hierarchical in the sense that importing everything from package A does not import everything from package A.B.

However, Java packages do correspond directly to the directory structure on the file system, and directories are hierarchical. So Eclipse is doing the correct thing - it is renaming the directory, which automatically changes the name of the parent directory of the renamed directory's children (to state the very obvious).

danben
Note that it doesn't actually do that by default. Renaming the directory is what happens when you deviate from the default and check that option to rename subpackages as well. But it's right either way.
Mark Peters
@Mark Peters: What does it do if you don't take that option - make a new directory and move all of the files over but also leave the old one to contain the subdirectories?
danben
@danben: Yep, that's exactly what it does. Which is what you would expect if using the "Flat" Package Presentation in the Package Explorer. It might take you by surprise if using the "Hierarchal" presentation.
Mark Peters
It is doing the right or expected thing on the file system, I get that. But does no one else other than me find it referring to "sub-packages" confusing. There is no such beast as a Java sub-package, right?
Tom Tresansky
@Tom Tresansky: Sure there is. From the JLS (http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html): "The naming structure for packages is hierarchical (§7.1). The members of a package are class and interface types (§7.6), which are declared in compilation units of the package, and **subpackages**, which may contain compilation units and subpackages of their own." It just refers to a package within another package.
danben
+2  A: 

even java itself has the concept of subpackage:

http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html

java -ea[:<package name>"..." | :<class name> ]

Enable assertions. Assertions are disabled by default.

With no arguments, enableassertions or -ea enables assertions. With one argument ending in "...", the switch enables assertions in the specified package and any subpackages. If the argument is simply "...", the switch enables assertions in the unnamed package in the current working directory. With one argument not ending in "...", the switch enables assertions in the specified class.

If a single command line contains multiple instances of these switches, they are processed in order before loading any classes. So, for example, to run a program with assertions enabled only in package com.wombat.fruitbat (and any subpackages), the following command could be used:

java -ea:com.wombat.fruitbat... <Main Class>
irreputable
So packages are non-hierarchical, but they are officially referred to by adding a prefix that clearly implies hierarchy. Awesome!
Tom Tresansky
@Tom Tresansky: you are still missing the point. The hierarchy is in the *directory structure*, which java packages correspond to directly. When we say Java packages are non-hierarchical, we mean a very specific thing: that importing everything in a package does not import anything from its subpackages. If you prefer to disambiguate the two by not using the term "non-hierarchical" then that would be fine.
danben
@Tom: You seem to think that the tutorials are infallible (they're not; in fact they are frequently wrong) and also that the same terminology used in different context refers to the same thing. The second paragraph of the JLS ch. 7 dealing with Packages says `The naming structure for packages is hierarchical`. They are hierarchal in *that context*. In *the context of importing*, they are not hierarchal *in that `import java.awt.*` does not import `java.awt.image.*`. Context, context, context!
Mark Peters