Java Forum / Tools / June 2004
Eclipse: reordering .java source file
Grisha Golberg - 02 Jun 2004 02:51 GMT Hi all,
I was wondering if there's Eclipse (I am using 3.0M8) functionality to not just display fields and methods in a particular order in the explorer, but to actually reorder them in the source file in the given order, by a mouse-click (or two).
It really is much easier to write things in any order (like, in a middle of writing something you decide you really need to declare X a constant, so you do it just above/below the method in question, because you don't want to scroll and search and come back), and then reorganize the source file. Any way at all?
Robert Klemme - 02 Jun 2004 08:44 GMT > Hi all, > [quoted text clipped - 8 lines] > want to scroll and search and come back), and then reorganize the source > file. Any way at all? Can't say for 3.x, but 2.x does not have this feature. There is a free refactoring and reformatting tool, that is capable of that:
http://jrefactory.sourceforge.net/
Regards
robert
Marco Hunsicker - 02 Jun 2004 08:49 GMT > I was wondering if there's Eclipse (I am using 3.0M8) functionality > to not just display fields and methods in a particular order in the > explorer, but to actually reorder them in the source file in the given > order, by a mouse-click (or two). > > [...] You could use a decent source code formatter. You can find a few links here: http://www.triemax.com/products/jalopy/links.html#related-projects
The Open Source Jalopy and JRefactory are two free formatters that support the reordering of methods. Jalopy provides an Eclipse Plugin but JRefactory (or any other formatting solution) can be integrated as a command-line tool as well. The commercial Jalopy works with Eclipse 3.0 out-of-the-box. Otherwise you need to tweak the plugin.xml. Hope that helps.
Best regards,
Marco Hunsicker
Roedy Green - 02 Jun 2004 19:51 GMT >> I was wondering if there's Eclipse (I am using 3.0M8) functionality >> to not just display fields and methods in a particular order in the >> explorer, but to actually reorder them in the source file in the given >> order, by a mouse-click (or two). Be careful with this. I remember being baffled why BigDate stopped working when VAJ reordered its methods and variables. The order of initialiation is often crucial.
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Grisha Golberg - 03 Jun 2004 21:09 GMT > >> I was wondering if there's Eclipse (I am using 3.0M8) functionality > >> to not just display fields and methods in a particular order in the [quoted text clipped - 4 lines] > working when VAJ reordered its methods and variables. The order of > initialiation is often crucial. Care to give an example that cannot be detected by rudimentary dependency analysis?
Roedy Green - 03 Jun 2004 21:27 GMT >Care to give an example that cannot be detected by rudimentary >dependency analysis? But Java does not do even rudimentary dependency analysis.
If you write code like this:
static int a = b + 4;
static int b = 3;
a will come out 4 not 7!!
Thus the order of you variables matters.
You can't just willy nilly sorting them in interesting orders and expecting the code to still work.
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Michael Amling - 04 Jun 2004 02:30 GMT >>Care to give an example that cannot be detected by rudimentary >>dependency analysis? [quoted text clipped - 8 lines] > > a will come out 4 not 7!! Did you get 4 by compiling that code and running it? jikes and javac consider using b before it's declared is an error. Maybe you're thinking of static int a=foo()+4; static int b=3; public static int foo() { return b; } in which a does get the value 4.
> Thus the order of you variables matters. > > You can't just willy nilly sorting them in interesting orders and > expecting the code to still work. For variables you do have to worry about order, but only for initialization. Method declarations can be in any order, AFAIK.
--Mike Amling
Roedy Green - 04 Jun 2004 02:38 GMT > Did you get 4 by compiling that code and running it? jikes and javac >consider using b before it's declared is an error. Maybe you're thinking of [quoted text clipped - 3 lines] > return b; >} Sorry I can't tell you precisely. You can see the code for BigDate at http://mindprod.com/products.html#BIGDATE.
If you change the order of the declarations, it stops working. When I did it, some time ago, the compiler gave no warning.
When you have statics, all variables ARE initialised, just not necessarily with the ultimate value. So I don't see how Java even could legitimately complain. Detection of uninitialised variables would apply only for locals.
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Michael Amling - 04 Jun 2004 15:20 GMT >> Did you get 4 by compiling that code and running it? jikes and javac >>consider using b before it's declared is an error. Maybe you're thinking of [quoted text clipped - 9 lines] > If you change the order of the declarations, it stops working. When I > did it, some time ago, the compiler gave no warning. I looked at BigDate46.java. Here's the first place I found, and there may be others, where lexical order would affect initialization results.
static { ... int last = daysInMonth( mmi, false ); ... }
daysInMonth() returns a value from usual_DaysPerMonthTable[], which, depending on lexical order, may or may not have been initialized by the the the above static initializer runs. Had the code from daysInMonth() been copied inline into the static initializer, then the compiler would have caught out-of-order initialization.
> When you have statics, all variables ARE initialised, just not > necessarily with the ultimate value. So I don't see how Java even > could legitimately complain. See "The Java Programming Language" for the conditions when javac, going back at least to 1.0.3, would consider using a variable before its declaration to be an error.
8.3.2.3 Restrictions on the use of Fields during Initialization
The declaration of a member needs to appear before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:
1. The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C. 2. The usage is not on the left hand side of an assignment. 3. C is the innermost class or interface enclosing the usage.
A compile-time error occurs if any of the three requirements above are not met. <<
From 8.3.2.1:
One subtlety here is that, at run time, static variables that are final and that are initialized with compile-time constant values are initialized first. This also applies to such fields in interfaces (§9.3.1). These variables are "constants" that will never be observed to have their default initial values (§4.5.5), even by devious programs. << A program that calls daysInMonth() from a static initializer that lexically precedes the declaration of usual_DaysPerMonthTable[] would count as a "devious program".
> Detection of uninitialised variables > would apply only for locals. It's not really uninitialized variables that cause the compiler to complain about static int a=b+4, b=3. The static variables in question do get their default initial values (binary zeros or null); it's just that their "initializer" (the expression to the right of the = sign in their declaration, or the value assigned by a static{} initializer) hasn't run yet. Local variables don't have default initial values, so the compiler (and the verifier, since the .class may not have been produced by a conforming compiler) has to complain where it would allow a static (or instance) variable to be used before its =3 is executed.
--Mike Amling
Roedy Green - 04 Jun 2004 02:43 GMT > Did you get 4 by compiling that code and running it? jikes and javac >consider using b before it's declared is an error. Maybe you're thinking of >static int a=foo()+4; public class Init { static int a = b + 1; static int b = 4;
/** * test harness * * @param args not used */ public static void main ( String[] args ) { System.out.println( a );
} }
This gives an error message, illegal forward reference. I'd be interested to see what happens if you compile it with an old JavaC, or VAJ.
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Sumit - 04 Jun 2004 21:54 GMT What kind of tweak is required in jalopy plugin.xml for Eclipse 3.0M9 or better? I tried changing the imports section. Previously it looked like:
<requires> <import plugin="org.eclipse.core.resources" /> <import plugin="org.eclipse.core.runtime" /> <import plugin="org.eclipse.jdt.core" /> <import plugin="org.eclipse.swt" /> <import plugin="org.eclipse.ui" /> <import plugin="org.apache.xerces" /> </requires
I changed it to:
<requires> <import plugin="org.eclipse.core.resources" /> <import plugin="org.eclipse.ui" /> <import plugin="org.eclipse.jdt.core" /> <import plugin="org.eclipse.jdt.ui" /> <import plugin="org.eclipse.text" /> <import plugin="org.eclipse.swt" /> <import plugin="org.eclipse.core.boot" /> <import plugin="org.eclipse.core.runtime" /> </requires>
(NOTE: In Eclipse 3.0M9 or better there is no xerces plugin.)
Even after this change Jalopy (version 0.6.2) doesn't work in Eclipse 3.0RC1.
Can you pl. help?
-sumit
> > I was wondering if there's Eclipse (I am using 3.0M8) functionality > > to not just display fields and methods in a particular order in the [quoted text clipped - 15 lines] > > Marco Hunsicker Marco Hunsicker - 05 Jun 2004 09:13 GMT > What kind of tweak is required in jalopy plugin.xml for Eclipse 3.0M9 > or better? [quoted text clipped - 15 lines] > > (NOTE: In Eclipse 3.0M9 or better there is no xerces plugin.) It seems they assume a J2SE 1.4 target VM.
I don't know about the Open Source one, but the commercial successor uses the following code:
<requires> <import plugin="org.eclipse.core.resources" /> <import plugin="org.eclipse.core.runtime" /> <import plugin="org.eclipse.jdt.core" /> <import plugin="org.eclipse.jdt.ui" /> <import plugin="org.eclipse.jface.text" /> <import plugin="org.eclipse.swt" /> <import plugin="org.eclipse.text" /> <import plugin="org.eclipse.ui" /> </requires>
Best regards,
Marco Hunsicker
Mohun Biswas - 02 Jun 2004 20:13 GMT > Hi all, > [quoted text clipped - 8 lines] > want to scroll and search and come back), and then reorganize the source > file. Any way at all? Not sure if this is what you mean, but IIRC in the Outline view you can just click on the representation of a method or member and drag it up or down.
 Signature Thanks, M.Biswas
Wald - 03 Jun 2004 09:00 GMT >> I was wondering if there's Eclipse (I am using 3.0M8) >> functionality to not just display fields and methods in a [quoted text clipped - 5 lines] > you can just click on the representation of a method or member > and drag it up or down. Maybe I'm misunderstanding, but I just tried this in a recent 3.0 stream integration build. It doesn't seem to allow dragging in the Outline view.
Regards, Wald
Mohun Biswas - 03 Jun 2004 13:38 GMT >>Not sure if this is what you mean, but IIRC in the Outline view >>you can just click on the representation of a method or member [quoted text clipped - 3 lines] > stream integration build. It doesn't seem to allow dragging in the > Outline view. I just checked and found the same thing. I also have a 3.0 build. I really could swear I remember doing this in 2.1 - is it possible it's a withdrawn feature?
For me, a right click in the Outline view does show cut/paste capability.
 Signature Thanks, M.Biswas
Grisha Golberg - 03 Jun 2004 21:10 GMT By the way, thanks all. I liked Jalopy, but -- I just downloaded 3.0RC1, and this feature is already included! Who knew?
(Now to figure out why some other things stopped working, like VSS integration. O the pain! :)
Pedro - 03 Jun 2004 21:31 GMT > Hi all, > [quoted text clipped - 8 lines] > want to scroll and search and come back), and then reorganize the source > file. Any way at all? Sure, there are 2 ways I use regularly.
1: In package explorer, select source file, right click and select Source/Sort Members 2: In outline view, select class (not a method or field), right click and select Source/Sort Members
The order of the members can be defined somewhere in the preferences.
Regards, Pedro
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|