Roman Soroka created THRIFT-4513:
------------------------------------

             Summary: thrift generated java code is not stable for constants
                 Key: THRIFT-4513
                 URL: https://issues.apache.org/jira/browse/THRIFT-4513
             Project: Thrift
          Issue Type: Improvement
          Components: Java - Compiler
            Reporter: Roman Soroka


In certain cases, output generated with java compiler is unstable, this 
prevents correct incremental generation, artifact caching, etc. 

This is especially crucible if the goal is reproducible builds: same results 
for the same source inputs. 

Here's example: 

Let's say we have thrift definition:

const map<string, Enum1Enum2> CONSTANT_MAP =
{
 'key1' : \{"enum1" : Enum1.ENUM1_VALUE1, "enum2" : Enum2.ENUM2_VALUE},
 'key2' : \{"enum1" : Enum1.ENUM1_VALUE2, "enum2" : Enum2.ENUM2_VALUE},
 'key3' : \{"enum1" : Enum1.ENUM1_VALUE3, "enum2" : Enum2.ENUM2_VALUE},
....
}

Compiler generates code like:

public static final java.util.Map<java.lang.String,Enum1Enum2> CONSTANT_MAP = 
new java.util.HashMap<java.lang.String,Enum1Enum2>();
 static {
 Enum1Enum2 tmp199 = new Enum1Enum2();
 tmp199.setEnum1(com.java.package.Enum1.ENUM1_VALUE2);
 tmp199.setEnum2(com.java.package.Enum2.ENUM2_VALUE);

CONSTANT_MAP.put("key2", tmp199);
 Enum1Enum2 tmp200 = new Enum1Enum2();
 tmp200.setEnum1(com.java.package.Enum1.ENUM1_VALUE1);
 tmp200.setEnum2(com.java.package.Enum2.ENUM2_VALUE);

CONSTANT_MAP.put("key1", tmp200);
 Enum1Enum2 tmp201 = new Enum1Enum2();
 tmp201.setEnum1(com.java.package.Enum1.ENUM1_VALUE3);
 tmp201.setEnum2(com.java.package.Enum2.ENUM2_VALUE);

CONSTANT_MAP.put("key3", tmp201);

Possible reorderings:

1. Values are added in different order from run to run.

2. Set methods in value construction may be called in different order.

Note: this results in equivalent java maps, so there is no runtime issues. 

Proposed remedy: 
When building construction of Constants from JSON, java compiler should use 
by-key ordering.

 



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to