Reputation: 189
Earlier the code was like this -
try {
some other code
......
......
ByteArrayInputStream annoBais = new ByteArrayInputStream(annoBytes);
DataInputStream dis = new DataInputStream(annoBais);
InputStream annoStream = dis;
inputRecord.put("XMLStream", annoStream);
MappedRecord resultMappedRecord = (MappedRecord)interaction.execute(interactionSpec,inputRecord);
HashMap mappedAnnotIds = (HashMap)resultMappedRecord.get(("ResultHashMap").toString());
annoStream.close(); //closed here
annoBais.close(); // closed here
dis.close(); // closed here
......
......
some more code
}
I changed it to -
try {
some other code
......
......
@lombok.Cleanup ByteArrayInputStream annoBais = new ByteArrayInputStream(annoBytes);
@lombok.Cleanup DataInputStream dis = new DataInputStream(annoBais);
@lombok.Cleanup InputStream annoStream = dis;
inputRecord.put("XMLStream", annoStream);
MappedRecord resultMappedRecord (MappedRecord)interaction.execute(interactionSpec,inputRecord);
HashMap mappedAnnotIds = (HashMap)resultMappedRecord.get(("ResultHashMap").toString());
......
......
some more code
}
Is @lombok.Cleanup going to have the same scope ? Will it close at the same place where it was earlier being closed manually ? If not, how can I close it in a way that it still has the same scope ?
Upvotes: 1
Views: 4954
Reputation: 46422
With
@lombok.Cleanup ByteArrayInputStream annoBais = new ByteArrayInputStream(annoBytes);
@lombok.Cleanup DataInputStream dis = new DataInputStream(annoBais);
@lombok.Cleanup InputStream annoStream = dis;
all three resources get closed at the closing brace in reverse order of their declaration. AFAIK, that's exactly the same as with try-with-resources.
@Cleanup
works even when some of the close statement throw. That's the same, too.
It works even with Java 6, but you really shouldn't be using Java 6.
@Cleanup
has a superior syntax, but that's subjective. I'm stopping using it as try-with-resources is a build-in feature sure to be supported forever or alike.
Also using try with resources is causing me to handle few other exceptions.
Definitely not, this must be the same, too. Note that 99% of the time, you should add the exceptions to the throws clause or wrap and re-throw them.
Note that
@lombok.Cleanup InputStream annoStream = dis;
makes no sense as you're not acquiring a new resource. So
InputStream annoStream = dis;
would be better as there's nothing new to close. Even better would be to ditch annoStream
as it hardly ever makes sense to have two variables for one thing.
Luckily, calling close
multiple times is harmless.
Upvotes: 2
Reputation: 8695
The correct way to use @lombok.Cleanup here is by not using lombok; Java7 solves this with try with resources.
Eg, with your code:
some other code
......
......
try ( ByteArrayInputStream annoBais = new ByteArrayInputStream(annoBytes);
DataInputStream dis = new DataInputStream(annoBais) ) {
InputStream annoStream = dis;
inputRecord.put("XMLStream", annoStream);
MappedRecord resultMappedRecord = (MappedRecord)interaction.execute(interactionSpec,inputRecord);
HashMap mappedAnnotIds = (HashMap)resultMappedRecord.get(("ResultHashMap").toString());
}
// annoBais & dis get closed here.
// Note: annoStream is an alias of dis, not a separate resource.
......
......
some more code
Of course, this code needs to be surrounded by a try {} catch () {}
block, or your method must declare that it throws
the required exceptions.
Using "try with resources" does not cause you to have to handle additional exceptions. You always need to handle all checked exceptions, either by catching them or declaring your methods throws them.
Upvotes: 7