public class Request
{
private HashMap<String, String> fMetaData = new HashMap<String, String>();
private final String fSomething;
/**
* Constructor.
*/
public Request(final String something)
{
assert something != null : "something may not be null";
fSomething = something;
}
/**
* Constructor.
*/
public Request(final String something, final Map<String, String> metaData)
{...}
private HashMap<String, String> fMetaData = new HashMap<String, String>();
|
|
1. removed call to java/util/HashMap::<init> → SURVIVED 2. Removed assignment to member variable fMetaData → SURVIVED 3. removed call to java/util/HashMap::<init> → KILLED 4. Removed assignment to member variable fMetaData → KILLED |
public class Request
{
private HashMap<String, String> fMetaData;
private final String fSomething;
/**
* Constructor.
*/
public Request(final String something)
{
assert something != null : "something may not be null";
fMetaData = new HashMap<String, String>();
fSomething = something;
}
So we do the assignment in the constructor. Now we get our perfect score:
1. removed call to java/util/HashMap::<init> → KILLED
2. Removed assignment to member variable fMetaData → KILLED
So we have 2 questions.
1. Why is there a difference between the two approaches, to us they do exactly the same?
2. Why there are seemingly the same mutation for one line that produce different outcomes?
We would really like to understand this more.
Bavo Vander Henst
Product Developer
The first version of your class provides two constructors. The one that takes the map overwrites the value assigned to fMetaData, so the earlier inline call to new HashMap is redundant.
The call is required if the class in constructed using the single argument version.
I'm therefore guessing that all the tests you have that require fMetaData to be non null construct the class using the 2 parameter constructor.public class Request {
private final Map<String, String> fMetaData;
private final String fSomething;
public Request(final String something) {
this(something, new HashMap<String,String>());
}
public Request(final String something, final Map<String, String> metaData) {
assert something != null : "something may not be null";
this.fSomething = something;
this.fMetaData = metaData;
}
}