Skip to content

Commit 02ce317

Browse files
authored
MCR-3248 PMD rule UnnecessaryCast (#2409)
1 parent cbf8887 commit 02ce317

File tree

8 files changed

+41
-19
lines changed

8 files changed

+41
-19
lines changed

mycore-base/src/main/java/org/mycore/common/MCRCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public boolean isEmpty() {
249249
* @return the fill rate of this cache as double value
250250
*/
251251
public double getFillRate() {
252-
return capacity == 0 ? 1.0 : (double) getCurrentSize() / (double) capacity;
252+
return capacity == 0 ? 1.0 : (double) getCurrentSize() / capacity;
253253
}
254254

255255
/**

mycore-base/src/main/java/org/mycore/frontend/fileupload/MCRUploadHandlerIFS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public synchronized long receiveFile(String path, InputStream in, long length, S
242242
}
243243
});
244244
this.filesUploaded++;
245-
int progress = (int) (((float) this.filesUploaded / (float) getNumFiles()) * 100f);
245+
int progress = (int) (((float) this.filesUploaded / getNumFiles()) * 100f);
246246
this.setProgress(progress);
247247
}
248248
}

mycore-base/src/main/java/org/mycore/frontend/jersey/resources/MCRJerseyExceptionMapper.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,40 @@ public Response toResponse(Exception exc) {
108108

109109
private Response getResponse(Exception exc) {
110110
if (exc instanceof WebApplicationException wae) {
111-
Response response = wae.getResponse();
112-
if (response.hasEntity()) {
113-
return response;
114-
}
115-
return Response.fromResponse(response)
116-
.entity(exc.getMessage())
111+
return getResponse(wae);
112+
}
113+
114+
String message = exc.getMessage();
115+
boolean hasMessage = message != null;
116+
boolean isFormRequest = isFormRequest();
117+
118+
Object entity = isFormRequest && hasMessage ? message : new MCRExceptionContainer(exc);
119+
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(entity).build();
120+
}
121+
122+
private static Response getResponse(WebApplicationException wae) {
123+
Response response = wae.getResponse();
124+
if (!response.hasEntity()) {
125+
response = Response
126+
.fromResponse(response)
127+
.entity(wae.getMessage())
117128
.type(MediaType.TEXT_PLAIN_TYPE)
118129
.build();
119130
}
120-
Object entity = Optional.ofNullable(request.getContentType())
131+
return response;
132+
}
133+
134+
private boolean isFormRequest() {
135+
return Optional
136+
.ofNullable(request.getContentType())
121137
.map(MediaType::valueOf)
122-
.filter(t -> t.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE) || t
123-
.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE))
124-
.map(t -> (Object) exc.getMessage()) //do not container form requests responses
125-
.orElseGet(() -> new MCRExceptionContainer(exc));
126-
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(entity).build();
138+
.stream()
139+
.anyMatch(MCRJerseyExceptionMapper::isFormRequestMediaType);
140+
}
141+
142+
private static boolean isFormRequestMediaType(MediaType mediaType) {
143+
return mediaType.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
144+
|| mediaType.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE);
127145
}
128146

129147
@XmlRootElement(name = "error")

mycore-oai/src/main/java/org/mycore/oai/MCROAISetManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ private MCRSet createSet(String setId, Element setElement) {
234234
.stream() //all setDescription
235235
.flatMap(e -> e.getChildren().stream().limit(1)) //first childElement of setDescription
236236
.peek(Element::detach)
237-
.map(d -> (Description) new Description() {
237+
.map(d -> new Description() {
238238
@Override
239239
public Element toXML() {
240240
return d;

mycore-orcid2/src/main/java/org/mycore/orcid2/v3/work/MCRORCIDWorkSummaryUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private static boolean checkPutCodeExistsInSummaries(List<WorkSummary> works, lo
121121

122122
private static long[] getPutCodesNotCreatedByThisAppFromSummaries(Stream<WorkSummary> works) {
123123
return works.filter(w -> !MCRORCIDUtils.isCreatedByThisApplication(w.retrieveSourcePath()))
124-
.map(WorkSummary::getPutCode).mapToLong(l -> (long) l).toArray();
124+
.map(WorkSummary::getPutCode).mapToLong(Long::longValue).toArray();
125125
}
126126

127127
private static long getPutCodeCreatedByThisAppFromSummaries(Stream<WorkSummary> works) {

mycore-solr/src/main/java/org/mycore/solr/index/statistic/MCRSolrIndexStatistic.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public synchronized double reset() {
7373
if (docs == 0) {
7474
return time == 0 ? 0 : Double.MAX_VALUE;
7575
}
76-
return ((double) time / (double) docs);
76+
return (double) time / docs;
7777
}
7878
}
7979
}

mycore-sword/src/main/java/org/mycore/sword/MCRSwordUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ public static String buildCollectionPaginationLinkHref(String collection, Intege
556556
*/
557557
public static void addPaginationLinks(IRI collectionIRI, String collection, Feed feed,
558558
MCRSwordCollectionProvider collectionProvider) throws SwordServerException {
559-
final int lastPage = (int) Math.ceil((double) collectionProvider.getIDSupplier().getCount()
559+
final int lastPage = (int) Math.ceil(collectionProvider.getIDSupplier().getCount()
560560
/ (double) MCRSwordConstants.MAX_ENTRYS_PER_PAGE);
561561
int currentPage = ParseLinkUtil.CollectionIRI.getPaginationFromCollectionIRI(collectionIRI);
562562

ruleset.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,12 @@
108108
<rule ref="category/java/codestyle.xml/PackageCase"/>
109109
<rule ref="category/java/codestyle.xml/UnnecessaryAnnotationValueElement"/>
110110
<rule ref="category/java/codestyle.xml/UnnecessaryBoxing"/>
111-
<!-- TODO: Create PR to fix this rule -->
112111
<!--
112+
Rule remains inactive due to two known bugs in PMD:
113+
- https://github.com/pmd/pmd/issues/5523
114+
- https://github.com/pmd/pmd/issues/5440
115+
Both bugs should be resolved in PMD 7.11.
116+
TODO: Activate rule, once we use PMD 7.11 or later.
113117
<rule ref="category/java/codestyle.xml/UnnecessaryCast"/>
114118
-->
115119
<rule ref="category/java/codestyle.xml/UnnecessaryConstructor"/>

0 commit comments

Comments
 (0)