Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » compiler creates generated fields/methods when class switches on enum
compiler creates generated fields/methods when class switches on enum [message #530147] Wed, 28 April 2010 12:33 Go to next message
Eclipse UserFriend
I am seeing behavior similar to this thread, http://forums.sun.com/thread.jspa?threadID=5417665

I have a simple class that switches on an enum (included at the bottom of this message).
If I compile within eclipse, and then look at the declared methods/fields using this class file, I see the generated SWITCH_TABLE methods.
This only occurs if i comple within eclipse. If I use javac, i do not see these methods/fields in the class.

Is there a way to disable?

here is an example


If I print out the declared methods against the eclipse generatd class I see
$SWITCH_TABLE$Simple$SortOrder
convertSortOrder

And the field is
field $SWITCH_TABLE$Simple$SortOrder

If I print this information out against a class built outside of eclipse with javac, I only see the declared method, convertSortOrder.

I am printing out the declared methods/fields as follows:
Method[] methods = Simple.class.getDeclaredMethods();
for (int i=0; i < methods.length; i++)
{
System.out.println(methods[i].getName());
}
Field[] fields = Simple.class.getDeclaredFields(); for (int i=0; i < fields.length; i++) {
System.out.println("field " +fields[i].getName()); }

This is the Simple class that switches on the enum:

import java.io.Serializable;

public class Simple implements Serializable {

public static enum SortOrder
{
START_DATE, END_DATE, PROCESS_TITLE
};

public String convertSortOrder(SortOrder sort)
{
String retStr = null;
switch (sort)
{
case PROCESS_TITLE:
retStr = "process";
break;
}
return retStr;
}
}
Re: compiler creates generated fields/methods when class switches on enum [message #530203 is a reply to message #530147] Wed, 28 April 2010 15:35 Go to previous messageGo to next message
Eclipse UserFriend
There is nothing wrong with this.
javac is using an anonymous class to achieve more or less the same result.
When compiling with javac (at least 1.6), you get three .class files.

With Eclipse you get only two .class files but you get some extra synthetic methods and fields.
If you filter methods and fields using isSynthetic(), you will get the same answer as javac.

HTH,

Olivier
Re: compiler creates generated fields/methods when class switches on enum [message #530205 is a reply to message #530203] Wed, 28 April 2010 15:41 Go to previous messageGo to next message
Eclipse UserFriend
Thanks for the info. Yes its usually not a problem. In this case i'm trying to generate a valid serialVersionUID. The generated value is different if I use the eclipse created class file vs a jar or class file built outside of eclipse.
So was hoping for a way to turn if off from within eclipse.

Thanks
Re: compiler creates generated fields/methods when class switches on enum [message #530529 is a reply to message #530205] Thu, 29 April 2010 19:52 Go to previous messageGo to next message
Eclipse UserFriend
You can use the generated serial version UID quickfix in Eclipse to generate a serial version UID field.

Then the same .class file can be used with javac or not since the serial version UID is defined and won't be computed at runtime.

HTH,

Olivier
Re: compiler creates generated fields/methods when class switches on enum [message #530664 is a reply to message #530529] Fri, 30 April 2010 09:22 Go to previous messageGo to next message
Eclipse UserFriend
The problem. These classes existed in previous release without the serialVersionUID declared...so of course one was generated by the serialization runtime.

I need to be backwards compatible so i have to be sure the generated serialVersionUID matches that generated by the runtime.

There is the problem. The one generated by eclipse or generated using the eclipse generated class takes into account these extra methods and fields. So they are different.

I just have to generate without using the eclipse generated classes.
Re: compiler creates generated fields/methods when class switches on enum [message #530737 is a reply to message #530664] Fri, 30 April 2010 12:11 Go to previous message
Eclipse UserFriend
Il 30/04/2010 15.22, Stephanie.Smith@sas.com ha scritto:
> I just have to generate without using the eclipse generated classes.

You have to generate the UID for the old classes using the serialver
command line utility (you find it in the JDK), than add the
serialVersionUID in your classes to be the same as that reported by
serialver.

Mauro.
Previous Topic:How to find the type parameters for a given superclass in the hierarchy
Next Topic:Eclipse ant build reports "cannot find symbol [javac] symbol : XXXX"
Goto Forum:
  


Current Time: Sat Mar 22 12:38:50 EDT 2025

Powered by FUDForum. Page generated in 0.04297 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top