-
Notifications
You must be signed in to change notification settings - Fork 13
MCR-3258 Enable PMD rule ExceptionAsFlowControl #2480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2024.06.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,7 +130,7 @@ protected MCRStoredNode buildChildNode(Path fo) { | |
* Repairs additional metadata of this directory and all its children | ||
*/ | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe don't use a stream, this actually looks simpler:
|
||
void repairMetadata() throws IOException { | ||
writeData(e -> { | ||
e.setName("dir"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,7 @@ private void readAdditionalData() throws IOException { | |
} | ||
} | ||
|
||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this code. It looks way to complicated. @yagee-de can you fix this? |
||
protected void saveAdditionalData() throws IOException { | ||
Path target = path.resolve(DATA_FILE); | ||
try { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,6 +124,7 @@ public static List<String> writeMD5SumsToDirectory(String directory) throws NotD | |
.collect(Collectors.toList()); | ||
} | ||
|
||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use iterator again:
|
||
@MCRCommand(syntax = "generate md5sum file {0} for ifs2 file store {1}", | ||
help = "writes md5sum file {0} for every file in MCRFileStore with ID {1}") | ||
public static void writeMD5SumFile(String targetFile, String ifsStoreId) throws IOException { | ||
|
@@ -167,6 +168,7 @@ public static void writeMD5SumFile(String targetFile, String ifsStoreId) throws | |
} | ||
} | ||
|
||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getAllFiles() doesnt even throw an IOException :)
|
||
private static Stream<FileInfo> getAllFiles(Path nodePath, Element node) | ||
throws IOException { | ||
Function<Element, String> getName = e -> e.getAttributeValue("name"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to move the annotation to the method instead of the whole class. We have 3 problems here:
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,7 @@ public static boolean isDerivateSupported(String derivateID) { | |
* @return if content type is in property <code>MCR.Module-iview2.SupportedContentTypes</code> | ||
* @see MCRContentTypes#probeContentType(Path) | ||
*/ | ||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would extract it into two methods:
|
||
public static boolean isFileSupported(Path file) throws IOException { | ||
try { | ||
return Optional.ofNullable(file) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ public MCRTilingAction(MCRTileJob image) { | |
* Also this updates tileJob properties of {@link MCRTileJob} in the database. | ||
*/ | ||
@Override | ||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To me it looks like the inner try catch is unecessary because it's just there to add an extra log message. I would remove the inner try catch. And also update the log message in the "real" catch message: |
||
public void run() { | ||
tileJob.setStart(new Date()); | ||
MCRImage image; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -496,6 +496,7 @@ public Collection<String> getObjectBaseIds() { | |
} | ||
|
||
@Override | ||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here the API is a bit "stupid" cause there is no "real" IOException. OCFL throws a NotFoundException which is already a runtime exception. To fix it I would suggest:
|
||
public List<MCRObjectIDDate> retrieveObjectDates(List<String> ids) throws IOException { | ||
try { | ||
return ids.stream() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,7 @@ public String getPublicationStatus(@PathParam("objectID") String objectID) | |
@GET | ||
@Path("publish/{objectID}") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I would catch the WebApplicationException and rethrow it. This is not an internal server error in my opinion.
|
||
public String publish(@PathParam("objectID") String objectID) { | ||
MCRObjectID oid = checkID(objectID); | ||
MCRORCIDUser user = MCRORCIDSession.getCurrentUser(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,7 @@ public MCRORCIDWorkService(String orcid, MCRORCIDCredential credential) { | |
* @param object the MCRObject | ||
* @throws MCRORCIDException if cannot create work, transformation fails or cannot update flags | ||
*/ | ||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here would rethrow and maybe update the exception a bit:
} catch (MCRORCIDWorkAlreadyExistsException workAlreadyExistsException) {
|
||
public void createWork(MCRObject object) { | ||
if (!MCRORCIDUtils.checkPublishState(object)) { | ||
throw new MCRORCIDException("Object has wrong state"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,7 @@ private static String convertFormatToPath(Action action, String format) { | |
} | ||
} | ||
|
||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just throw this
instead of the InterruptedException |
||
private static byte[] callPandoc(String[] args, byte[] input) { | ||
class ThreadWrapper implements Runnable { | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,9 @@ | |
import org.mycore.pi.exceptions.MCRPersistentIdentifierException; | ||
|
||
public class MCRPIXPathMetadataService extends MCRPIMetadataService<MCRPersistentIdentifier> { | ||
|
||
@Override | ||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here the "check" is done way to late. It can be done at the start of the method. Also the method should throw an MCRPersistentIdentifierException.
|
||
public void insertIdentifier(MCRPersistentIdentifier identifier, MCRBase obj, String additional) { | ||
String xpath = getProperties().get("Xpath"); | ||
Document xml = obj.createXML(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -480,6 +480,7 @@ private boolean isFile() { | |
&& (headers.containsKey(HttpHeaders.CONTENT_LENGTH) || headers.containsKey("Transfer-Encoding")); | ||
} | ||
|
||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what todo here, this method does to much. @yagee-de can you look at this? |
||
private Response serveDirectory(MCRPath mcrPath, MCRFileAttributes dirAttrs) { | ||
Directory dir = new Directory(mcrPath, dirAttrs); | ||
try (DirectoryStream ds = Files.newDirectoryStream(mcrPath)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -488,6 +488,7 @@ public Response getThumbnail(@PathParam(PARAM_MCRID) String id, @PathParam("ext" | |
return getThumbnail(id, defaultSize, ext); | ||
} | ||
|
||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, method is just too big and does to much. Can you take a look @yagee-de? |
||
private Response getThumbnail(String id, int size, String ext) { | ||
List<MCRPath> mainDocs = MCRMetadataManager.getDerivateIds(MCRObjectID.getInstance(id), 1, TimeUnit.MINUTES) | ||
.stream() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -270,6 +270,7 @@ private void handleCanonicalizeRequest(int compilationId, CanonicalizeRequest ca | |
connection.sendMessage(compilationId, ProtocolUtil.inboundMessage(canonicalizeResponse.build())); | ||
} | ||
|
||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would refactor to this:
|
||
private void handleFunctionCallRequest(int compilationId, FunctionCallRequest functionCallRequest) | ||
throws IOException { | ||
FunctionCallResponse.Builder response = FunctionCallResponse.newBuilder() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,6 +108,7 @@ public DepositReceipt getMetadata(String collectionString, MCRObject object, Opt | |
return MCRSword.getCollection(collectionString).getMetadataProvider().provideMetadata(object); | ||
} | ||
|
||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. streams abused...
|
||
public void deleteObject(MCRObject object) throws SwordServerException { | ||
try { | ||
object | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -325,6 +325,7 @@ public Boolean call() throws Exception { | |
* | ||
* @return true if command processed successfully | ||
*/ | ||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would change to:
|
||
private boolean processCommand(String command) { | ||
if (command.trim().startsWith("#")) { | ||
// ignore comment | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,6 +80,7 @@ public static MCRFileUploadBucket getBucket(String bucketID) { | |
return BUCKET_MAP.get(bucketID); | ||
} | ||
|
||
@SuppressWarnings("PMD.ExceptionAsFlowControl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. simplified:
|
||
public static synchronized MCRFileUploadBucket createBucket(String bucketID, | ||
Map<String, List<String>> parameters, | ||
MCRUploadHandler uploadHandler) throws MCRUploadServerException { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can try:
Simplify findInConfigLibDir.
Change method signature to:
Add functional interface: