I have 2 packages , A and B which contain the same list of classes. I am not able to use dozer or any mapping tool directly because the Enum values are uppercase in one package and camelcase in another package. I want to write a generic method which takes in the enum from package A and convert it into enum in package B using Reflection. Below is the specific code that I had written -
return a.CardType.valueOf(((b.CardType)source).getValue().toUpperCase());
I want to write generic code to convert assuming I have source class ( enum type) as object , source class name and destination class name
is that what you want
public class Mango {
static enum Upper {A,B,C}
static enum Lower {a,b,c}
static <SRC extends Enum<SRC>,DST extends Enum<DST>> DST convert(SRC a, Class<DST> classDst ){
return Enum.valueOf( classDst,a.name().toUpperCase());
}
public static void main(String[] args) {
System.out.print(convert(Lower.a,Upper.class));
}
}
EDIT:
So you want convert lowercase/uppercase enum to camelCase? Just modify convert function to :
static <SRC extends Enum<SRC>,DST extends Enum<DST>> DST convert(SRC a, Class<DST> classDst ){
for (DST dst : EnumSet.allOf(classDst)){
if (dst.name().equalsIgnoreCase(a.name())){
return dst;
}
}
throw new IllegalArgumentException("Value not found");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.