Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameters missing when the java code is compiled

Tags:

java

I have a code like

private List<xyzClass> fieldName;

public List<xyzClass> getFieldName(){
    return fieldName;
}

public void setFieldName(List<xyzClass> abc){
    this.fieldName= abc;
}

but when compiled to java byte code, the parameteres are missing as below:

private List fieldName;

public List getFieldName(){
    return fieldName;
}

public void setFieldName(List abc){
    this.fieldName= abc;
}

This is creating issue with SOAP as not able to typecast to xyzClass Exception.

From Eclipse I see the class file created is good but when I tried command line javac or maven the class file is missing these parameters.

like image 243
Pankaj Kumar Avatar asked Dec 28 '25 04:12

Pankaj Kumar


1 Answers

This is as a result of type erasure. Basically java uses the generics to confirm that you are not violating the type constraints during compile time. All the generics are then removed and replaced with the highest class in the inheritance hierarchy. This will default to Object but if you say <T extends ParentClass> (for example) the T generics will be replaced with ParentClass.

like image 55
Nicholas Robinson Avatar answered Dec 30 '25 17:12

Nicholas Robinson