Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 332207 - TypeHelperImpl.resolveEnumConstant() needs to use Enum.name() to resolve the constant
Summary: TypeHelperImpl.resolveEnumConstant() needs to use Enum.name() to resolve the ...
Status: CLOSED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Tom Ware CLA
QA Contact:
URL:
Whiteboard: submitted_patch
Keywords:
: 332094 (view as bug list)
Depends on:
Blocks:
 
Reported: 2010-12-09 09:17 EST by Pascal Filion CLA
Modified: 2022-06-09 10:10 EDT (History)
3 users (show)

See Also:
tom.ware: iplog+


Attachments
Patch with test (4.18 KB, application/octet-stream)
2010-12-21 14:49 EST, Tom Ware CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Pascal Filion CLA 2010-12-09 09:17:34 EST
Build Identifier: 2.2.0.v20101201-r8590

Enum.toString() should not be used in TypeHelperImpl.resolveEnumConstant() because it can result in not finding the constant. The objects need to be type casted to Enum<?> and then name() should be used. The code would become:

for (int i = 0; i < constants.length; i++) {
   Enum<?> enumConstant = (Enum<?) enumConstant;
   if (enumConstant.name().equals(constants[i])) {
      return constants[i];
   }
}


A user could have an enum like this:

public enum Color {
   RED("1"),
   BLUE("2");
   String color;
   Color(String color) {
      this.color = color;
   }
   public String toString() {
      return color;
   }
}

Doing constant.toString() on the constants will return "1" and "2" and Color.RED would not be resolved.

Reproducible: Didn't try

Steps to Reproduce:
1. Create an enum that overrides toString() and return anything that is not the name of the method (constant name).
2. Try to resolve the enum constant.
3. No enum constant will be found even though there is a match.
Comment 1 Pascal Filion CLA 2010-12-09 09:19:55 EST
Correction, the updated for loop is:

for (int i = 0; i < constants.length; i++) {
   Enum<?> enumConstant = (Enum<?>) constants[i];
   if (enumConstant.name().equals(constant)) {
      return enumConstant;
   }
}
Comment 2 Tom Ware CLA 2010-12-17 09:18:49 EST
*** Bug 332094 has been marked as a duplicate of this bug. ***
Comment 3 Tom Ware CLA 2010-12-21 14:49:33 EST
Created attachment 185667 [details]
Patch with test
Comment 4 Tom Ware CLA 2010-12-21 15:32:11 EST
Checked-in community submitted fix and added test case

Reviewed by: Tom Ware - reviewed community-submitted fix

Added test to JUnitJPQLSimpleTestSuite

Tested with JPA and Core LRG
Comment 5 Eclipse Webmaster CLA 2022-06-09 10:10:15 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink