Monday 22 January 2018

If java is passed-by-value why my object changes after executing the method?

itemprop="text">

I was debugging my method and it
somehow updates my element variable. Even if I don't work with
that variable from within the
method.




CODE:



private
static List createFormFieldsMapping(ArrayList
CDOfields,
List fieldMappings, Element element) {

System.out.println(" - Creating field mappings for "+element.name);
for
(Mapping fieldMapping : fieldMappings){
if
(fieldMapping.targetEntityFieldId!=null){
String formField =
getContactFieldNameById(fieldMapping.targetEntityFieldId);
formField =
formField.trim();

formField = formField.replaceAll("-",
"");
formField = formField.replaceAll("_", "");
formField =
formField.replaceAll(" ", "");
formField =
formField.toLowerCase();
Boolean matchFound = false;
for (String
cdoField : CDOfields){
String[] cdoFieldSplit =
cdoField.split(";",-1);
String cdoFieldModified
=cdoFieldSplit[1].trim();
cdoFieldModified = cdoFieldModified.replaceAll("-",
"");
cdoFieldModified = cdoFieldModified.replaceAll("_",
"");

cdoFieldModified = cdoFieldModified.replaceAll(" ",
"");
cdoFieldModified = cdoFieldModified.toLowerCase();
if
(cdoFieldModified.equals(formField)){
fieldMapping.targetEntityFieldId =
cdoFieldSplit[0];
matchFound = true;
break;

}
if (!matchFound){
// WRITE NOT MATCHED FORM FIELD TO A
FILE
}

}
}

}


element.processingSteps.targetEntityFieldId
is being changed



This is the way I call the
method:



List
fieldMapping = new
ArrayList();

Iterator i =
element.processingSteps.iterator();

while (i.hasNext())
{
ProcessingStep step = i.next();
if
(step.type.equals("FormStepCreateUpdateContactFromFormField")){
fieldMapping
= step.mappings;
step.execute = "never";
//i.remove();

}
}

// Update contact field IDs with CDO field
IDs
fieldMapping = createFormFieldsMapping(CDOfields, fieldMapping,
element);


Everything I
wanted was to kind of copy field mapping, process it by that method and then return back
and add it to list of
fieldMappings.



The
thing is that step.mappings is part of
element, but that step.mappings is
being put to an ArrayList
fieldMapping. By that I would assume that element should never
be edited by anything.


itemprop="text">
class="normal">Answer



I believe
this is because changing fieldMapping in this code also changes element because both are
"references" to the same
object:




fieldMapping.targetEntityFieldId
= cdoFieldSplit[0];


To
verify this is the case, add a conditional debug break (or an if-statement and a
print/log) that checks the equality of the two object
instances.



This is happening because you are
setting fieldMapping equal to the object pointed to by your
element.processingSteps.iterator() in following code.



Iterator i
= element.processingSteps.iterator();

while (i.hasNext())
{

ProcessingStep step = i.next();
if
(step.type.equals("FormStepCreateUpdateContactFromFormField")){
fieldMapping
= step.mappings;
step.execute = "never";
//i.remove();

}
}
// Update contact field IDs with CDO field IDs

fieldMapping = createFormFieldsMapping(CDOfields, fieldMapping,
element);



If
you do not want this behavior, then you need to "deep copy" the step.mappings object
when initializing fieldMapping.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...