Skip to content
Snippets Groups Projects
Commit ef3ad32e authored by Simon Barner's avatar Simon Barner
Browse files

Use try-with-resources to prevent potential resource leaks

* Detected by enabling all resource-related Java compiler warnings
 * Java -> Compiler -> Errors/Warnings
 * Filtering for "resource" should show 3 warnings
 * Set to "Warning"

Issue-Ref: 3539
Issue-Url: https://af3-developer.fortiss.org/issues/3539



Signed-off-by: default avatarSimon Barner <barner@fortiss.org>
parent c7cca376
No related branches found
No related tags found
1 merge request!273539
CommandStackService.java 957bda69b5feb91f002aed4d25ed334e92801e7e GREEN
ConnectionCompositorService.java d69a60cd7a3d06e91d24fd32b9c00125ea71e0dd GREEN
ConstraintCheckerService.java 459b5eb717598e7e8bb71a0c87e57ea85cb00e4b GREEN
ConstraintService.java d869107b1cef404bc5e27edd7d30fc2ea86a0427 GREEN
ConstraintService.java 139187909523300c80a22be920329f1c9d0fb654 YELLOW
DummyTopLevelElement.java 42ae59a6a89a57035644d04f9f0f2d674797642a GREEN
ElementCompositorService.java 98c5d27e09881e60aa4f87c1ac0c7787cdec9f7c GREEN
LibraryPrototypeProvider.java b77eddbdca78f561ffb1233e98817be361c690ae GREEN
......
......@@ -202,11 +202,10 @@ public final class ConstraintService implements IIntrospectiveKernelService, ICo
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos;
final EObject hashRelevant = getChecksumRelevantEObject(constrained, ci);
try {
try(ObjectOutputStream oos = new ObjectOutputStream(baos)) {
final ResourceSet rset = new ResourceSetImpl();
oos = new ObjectOutputStream(baos);
Resource resource = rset.createResource(URI.createURI("temp"));
resource.getContents().add(hashRelevant);
Map<String, String> options = new HashMap<>();
......@@ -217,7 +216,6 @@ public final class ConstraintService implements IIntrospectiveKernelService, ICo
options.put(OPTION_PROCESS_DANGLING_HREF, OPTION_PROCESS_DANGLING_HREF_DISCARD);
resource.save(oos, options);
oos.close();
MessageDigest m = MessageDigest.getInstance("SHA1");
m.update(baos.toByteArray());
BigInteger res = new BigInteger(1, m.digest());
......@@ -227,11 +225,11 @@ public final class ConstraintService implements IIntrospectiveKernelService, ICo
String nameCstrd = constrained instanceof INamedElement
? ((INamedElement)constrained).getName() : constrained.toString();
String fileName = filePrefix + "_" + nameCstr + "_" + nameCstrd + ".xml";
FileOutputStream file = new FileOutputStream(fileName);
ObjectOutputStream oos2 = new ObjectOutputStream(file);
resource.save(oos2, options);
oos2.writeUTF("\nCHECKSUM: " + res.toString());
oos2.close();
try(FileOutputStream file = new FileOutputStream(fileName);
ObjectOutputStream oos2 = new ObjectOutputStream(file)) {
resource.save(oos2, options);
oos2.writeUTF("\nCHECKSUM: " + res.toString());
}
}
resource.getContents().remove(hashRelevant);
resource.delete(options);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment