Bug 148899

Summary: [clean up] Refactorings to downgrade Java 5 source
Product: [Eclipse Project] JDT Reporter: Missing name Mising name <wuntoy>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: ASSIGNED --- QA Contact:
Severity: enhancement    
Priority: P4 CC: benno.baumgartner, daniel_megert, martinae, mn
Version: 3.2Keywords: helpwanted
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description Missing name Mising name CLA 2006-06-27 17:58:56 EDT
Linked to my comment in bug 107783 comment 7, produce refactorings to convert from Java 5 source code to Java 1.4-compliant syntax.  The clean up wizard could be used to apply these en masse (plus change project compliance level).  This would allow the development advantages of Java 5 and the ability to compile for a pre-1.5 target, required for some tools and runtime environments.

Refactorings may include:
 - Convert generics to erased types and casts
 - Remove annotations
 - Convert varargs to arrays
 - Make boxing/unboxing explicit
 - Inline static imports to fully qualified names
 - Convert enhanced for to standard for	
 - Convert enums to typesafe enumeration pattern
(see bug 107783 for a discussion of some of the issues)
 
Note that the enumeration refactoring would need to handle enumerated switch statements.  It may not be possible to convert these to if..else if.. statements if there are conditional breaks, so a naive (non-optimised) conversion of:

	switch(a)
	{
	case A.FIRST:
		System.out.println("1");
		if(!fallThrough)
		{
			break;
		}
	case A.SECOND:
		System.out.println("2");
		break;
	default
		System.out.println("Should not be reached");
	}


may be:

	do  // switch(a)
	{
		boolean hasRun = false;

		if(a == A.FIRST)
		{
			hasRun = true;
			System.out.println("1");
			if(!fallThrough)
			{
				break;
			}
		}
		if(a == A.SECOND || a == A.FIRST)
		{
			hasRun = true;
			System.out.println("2");
			break;
		}
		if(!hasRun || a == A.SECOND || a == A.FIRST)
		{
			System.out.println("Should not be reached");
		}
	} while (false);  // end switch(a)
	
(The do..while(false) contains the switch to ensure the break statements have their intended meaning).

From my understanding of the JDT, several of these refactorings should be relatively simple AST rewrites, though those dependent on the Java 5 API (e.g. the enhanced for loop) would be more involved.  (I wanted to contribute a patch rather than just suggest all the above, though I got a bit too lost in the refactorings source).
Comment 1 Martin Aeschlimann CLA 2006-06-28 04:34:57 EDT
There's always a lot of work involved with refactorings as there are always many corner cases and a refactoring promises to not break your code.
Especially the enum to constant transformation won't be trivial.

This is definitly an interesting request. We will see what we can do in this direction. We have to be very carful with approaching new features; therefor help is highly welcome. This would be also make a nice extra plugin, but don't underestimate the work.

I change the tag to [clean up] as I think this is where this would fit best.