Skip to content

Commit 48cadfd

Browse files
author
James Bognar
committed
Minor SonarQube fixes
1 parent af63064 commit 48cadfd

23 files changed

Lines changed: 119 additions & 60 deletions

juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanContext.java

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3842,7 +3842,8 @@ public BeanSession.Builder createSession() {
38423842
*/
38433843
@Override
38443844
@SuppressWarnings({
3845-
"java:S3776" // Cognitive complexity acceptable for comprehensive conversion dispatch
3845+
"java:S3776", // Cognitive complexity acceptable for comprehensive conversion dispatch
3846+
"java:S6541" // Brain Method: conversion dispatch inherently requires handling many type pairs in one place
38463847
})
38473848
public Conversion<?,?> findConversion(Class<?> inType, Class<?> outType) {
38483849
var toMeta = getClassMeta(outType);
@@ -4053,7 +4054,7 @@ public Conversion<?,?> findConversion(Class<?> inType, Class<?> outType) {
40534054
throw rex("Cannot convert string to {0}: {1}", outType.getName(), str);
40544055
var elemType = args.length > 0 ? args[0] : null;
40554056
var l2 = JsonList.ofJson(str).setBeanSession(bs);
4056-
var result = (Collection<Object>) newCollection(outType);
4057+
var result = newCollection(outType);
40574058
l2.forEach(x -> result.add(elemType != null && x != null ? converter.to(x, elemType) : x));
40584059
return result;
40594060
} catch (Exception e) {
@@ -4134,14 +4135,14 @@ public Conversion<?,?> findConversion(Class<?> inType, Class<?> outType) {
41344135
var temporal = (java.time.temporal.Temporal) in;
41354136
java.time.Instant instant;
41364137
try {
4137-
instant = java.time.Instant.from(temporal);
4138-
} catch (java.time.DateTimeException e) {
4139-
// LocalDateTime lacks offset info - interpret using session/system timezone
4140-
var tz = sessionTimeZone(session);
4141-
var zoneId = tz != null ? tz.toZoneId() : java.time.ZoneId.systemDefault();
4142-
instant = java.time.LocalDateTime.from(temporal).atZone(zoneId).toInstant();
4143-
}
4144-
return Iso8601Utils.fromEpochMillis(instant.toEpochMilli(), toMeta, sessionTimeZone(session));
4138+
instant = java.time.Instant.from(temporal);
4139+
} catch (@SuppressWarnings("unused") java.time.DateTimeException e) {
4140+
// LocalDateTime lacks offset info - interpret using session/system timezone
4141+
var tz = sessionTimeZone(session);
4142+
var zoneId = tz != null ? tz.toZoneId() : java.time.ZoneId.systemDefault();
4143+
instant = java.time.LocalDateTime.from(temporal).atZone(zoneId).toInstant();
4144+
}
4145+
return Iso8601Utils.fromEpochMillis(instant.toEpochMilli(), toMeta, sessionTimeZone(session));
41454146
};
41464147
}
41474148
if (toMeta.isDate() && outType == java.util.Date.class) {
@@ -4152,14 +4153,14 @@ public Conversion<?,?> findConversion(Class<?> inType, Class<?> outType) {
41524153
var temporal = (java.time.temporal.Temporal) in;
41534154
java.time.Instant instant;
41544155
try {
4155-
instant = java.time.Instant.from(temporal);
4156-
} catch (java.time.DateTimeException e) {
4157-
// LocalDateTime lacks offset info - interpret using session/system timezone
4158-
var tz = sessionTimeZone(session);
4159-
var zoneId = tz != null ? tz.toZoneId() : java.time.ZoneId.systemDefault();
4160-
instant = java.time.LocalDateTime.from(temporal).atZone(zoneId).toInstant();
4161-
}
4162-
return java.util.Date.from(instant);
4156+
instant = java.time.Instant.from(temporal);
4157+
} catch (@SuppressWarnings("unused") java.time.DateTimeException e) {
4158+
// LocalDateTime lacks offset info - interpret using session/system timezone
4159+
var tz = sessionTimeZone(session);
4160+
var zoneId = tz != null ? tz.toZoneId() : java.time.ZoneId.systemDefault();
4161+
instant = java.time.LocalDateTime.from(temporal).atZone(zoneId).toInstant();
4162+
}
4163+
return java.util.Date.from(instant);
41634164
};
41644165
}
41654166

@@ -4227,9 +4228,9 @@ public Conversion<?,?> findConversion(Class<?> inType, Class<?> outType) {
42274228
return (in, memberOf, session, args) -> converter.to(in.toString(), memberOf, session, Boolean.class);
42284229
}
42294230

4230-
// --- Object → Bean via toString() + BeanMap.load() (fallback for bean-compatible types) ---
4231-
// Matches old convertToMemberType fallback: if (to.isBean()) return newBeanMap(to.inner()).load(value.toString()).getBean()
4232-
// Excludes cases where input is already assignable to output (handled by BeanSession isInstance shortcut).
4231+
// --- Object → Bean via toString() + BeanMap.load() (fallback for bean-compatible types) ---
4232+
// Replicates the old convertToMemberType fallback for beans (newBeanMap + load + getBean).
4233+
// Excludes cases where input is already assignable to output (handled by BeanSession isInstance shortcut).
42334234
if (toMeta.isBean() && !Map.class.isAssignableFrom(inType) && !CharSequence.class.isAssignableFrom(inType)
42344235
&& !outType.isAssignableFrom(inType)) {
42354236
return (in, memberOf, session, args) -> {
@@ -4249,7 +4250,7 @@ private static TimeZone sessionTimeZone(ConverterSession session) {
42494250
return session == null ? null : session.get(TimeZone.class).orElse(null);
42504251
}
42514252

4252-
private BeanSession beanSession(ConverterSession session) {
4253+
private static BeanSession beanSession(ConverterSession session) {
42534254
return session instanceof BeanSession bs ? bs : null;
42544255
}
42554256

@@ -4266,7 +4267,7 @@ private static Collection<Object> newCollection(Class<?> outType) {
42664267
return new TreeSet<>();
42674268
try {
42684269
return (Collection<Object>) outType.getDeclaredConstructor().newInstance();
4269-
} catch (Exception e) {
4270+
} catch (@SuppressWarnings("unused") Exception e) {
42704271
return new ArrayList<>();
42714272
}
42724273
}
@@ -4913,17 +4914,22 @@ final ClassMeta[] findParameters(Type o, Class c) {
49134914

49144915
// Loop until we find a ParameterizedType
49154916
if (! (o instanceof ParameterizedType)) {
4916-
loop: do {
4917+
while (nn(c)) {
49174918
o = c.getGenericSuperclass();
49184919
if (o instanceof ParameterizedType)
4919-
break loop;
4920+
break;
4921+
boolean found = false;
49204922
for (var t : c.getGenericInterfaces()) {
49214923
o = t;
4922-
if (o instanceof ParameterizedType)
4923-
break loop;
4924+
if (o instanceof ParameterizedType) {
4925+
found = true;
4926+
break;
4927+
}
49244928
}
4929+
if (found)
4930+
break;
49254931
c = c.getSuperclass();
4926-
} while (nn(c));
4932+
}
49274933
}
49284934

49294935
if (o instanceof ParameterizedType o2 && ! o2.getRawType().equals(Enum.class)) {

juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@
6868
@Bean(properties = "innerClass,elementType,keyType,valueType,notABeanReason,initException,beanMeta")
6969
@SuppressWarnings({
7070
"java:S1200", // Class has 23 dependencies, acceptable for this core reflection metadata class
71-
"java:S1452" // Wildcard required - ClassMeta<?>, ObjectSwap<T,?>, etc. for element/component types
71+
"java:S1452", // Wildcard required - ClassMeta<?>, ObjectSwap<T,?>, etc. for element/component types
72+
"java:S6539" // Monster Class: ClassMeta is a focused reflection-metadata cache; splitting would increase coupling
7273
})
7374
public class ClassMeta<T> extends ClassInfoTyped<T> {
7475

@@ -868,8 +869,8 @@ public boolean hasMutaterFrom(Class<?> c) {
868869
// array conversions from element types, which can produce false positives here.
869870
if (inner().isArray() && c.isArray())
870871
return false;
871-
// Exclude Collections and Maps: BeanSession handles those via convertToCollectionType/convertToMapType;
872-
// BasicConverter's generic collection/map conversions produce false positives (Mutaters never did these).
872+
// Exclude Collections and Maps: BeanSession handles collection/map conversion directly;
873+
// BasicConverter's generic conversions produce false positives that Mutaters never did.
873874
if (Collection.class.isAssignableFrom(inner()) || Map.class.isAssignableFrom(inner()))
874875
return false;
875876
return BasicConverter.INSTANCE.canConvert(c, inner());
@@ -906,8 +907,8 @@ public boolean hasMutaterTo(Class<?> c) {
906907
// array conversions from element types, which can produce false positives here.
907908
if (inner().isArray() && c.isArray())
908909
return false;
909-
// Exclude Collection/Map targets: BeanSession handles those via convertToCollectionType/convertToMapType;
910-
// BasicConverter's generic collection/map conversions produce false positives (Mutaters never did these).
910+
// Exclude Collection/Map targets: BeanSession handles collection/map conversion directly;
911+
// BasicConverter's generic conversions produce false positives that Mutaters never did.
911912
if (Collection.class.isAssignableFrom(c) || Map.class.isAssignableFrom(c))
912913
return false;
913914
return BasicConverter.INSTANCE.canConvert(inner(), c);
@@ -1452,9 +1453,9 @@ private BeanMeta.BeanMetaValue<T> findBeanMeta() {
14521453
private KeyValueTypes findKeyValueTypes() {
14531454
if (cat.is(MAP) && ! cat.is(BEANMAP)) {
14541455
// If this is a MAP, see if it's parameterized (e.g. AddressBook extends HashMap<String,Person>)
1455-
var parameters = beanContext.findParameters(inner(), inner());
1456-
if (nn(parameters) && parameters.length == 2) {
1457-
return new KeyValueTypes(parameters[0], parameters[1]);
1456+
var typeParams = beanContext.findParameters(inner(), inner());
1457+
if (nn(typeParams) && typeParams.length == 2) {
1458+
return new KeyValueTypes(typeParams[0], typeParams[1]);
14581459
}
14591460
return new KeyValueTypes(beanContext.getClassMeta(Object.class), beanContext.getClassMeta(Object.class));
14601461
}
@@ -1467,9 +1468,9 @@ private ClassMeta<?> findElementType() {
14671468
if (cat.is(ARRAY)) {
14681469
return beanContext.getClassMeta(inner().getComponentType());
14691470
} else if (cat.is(COLLECTION) || cat.is(ITERABLE) || cat.is(ITERATOR) || cat.is(STREAM) || is(Optional.class)) {
1470-
var parameters = beanContext.findParameters(inner(), inner());
1471-
if (nn(parameters) && parameters.length == 1) {
1472-
return parameters[0];
1471+
var typeParams = beanContext.findParameters(inner(), inner());
1472+
if (nn(typeParams) && typeParams.length == 1) {
1473+
return typeParams[0];
14731474
}
14741475
return beanContext.getClassMeta(Object.class);
14751476
}

juneau-core/juneau-marshall/src/main/java/org/apache/juneau/InvalidDataConversionException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private static String name(Object o) {
4646
}
4747

4848
private static String value(Object o) {
49-
if (o instanceof Class o2)
49+
if (o instanceof Class<?> o2)
5050
return "'" + name(o2) + "'";
5151
return Json5Serializer.DEFAULT == null ? "'" + o.toString() + "'" : Json5Serializer.DEFAULT.toString(o);
5252
}

juneau-core/juneau-marshall/src/main/java/org/apache/juneau/annotation/SwapAnnotation.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ public Builder on(MethodInfo...value) {
221221

222222
}
223223

224+
@SuppressWarnings({
225+
"java:S2160" // equals not needed; annotation object identity is sufficient for usage in Sets/Maps
226+
})
224227
private static class Object extends AppliedOnClassAnnotationObject implements Swap {
225228

226229
private final String[] description;

juneau-core/juneau-marshall/src/main/java/org/apache/juneau/bson/BsonParserSession.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ private Object readTypedValue(BsonInputStream is, int elementType, ClassMeta<?>
118118
return o;
119119
}
120120

121+
@SuppressWarnings({
122+
"resource" // is is caller-owned; this method does not close it
123+
})
121124
private <T> T parseDocument(BsonInputStream is, ClassMeta<?> eType, Object outer, BeanPropertyMeta pMeta) throws IOException, ParseException, ExecutableException {
122125
is.readDocumentSize();
123126
if (eType == null)
@@ -233,6 +236,10 @@ private BeanMap<?> applyTypeProperty(BeanMap<?> beanMap, String typeName, ClassM
233236
return beanMap;
234237
}
235238

239+
@SuppressWarnings({
240+
"unused", // pMeta kept for API consistency with other parseXxx methods
241+
"java:S1172" // Same as above
242+
})
236243
private Object parseArray(BsonInputStream is, ClassMeta<?> eType, Object outer, BeanPropertyMeta pMeta) throws IOException, ParseException, ExecutableException {
237244
is.readDocumentSize();
238245
if (eType == null)

juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cbor/CborParserSession.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ protected CborParserSession(Builder builder) {
183183
* Workhorse method.
184184
*/
185185
@SuppressWarnings({
186+
"resource", // is is caller-owned; this method does not close it
186187
"java:S3776", // Cognitive complexity acceptable for this specific logic
187-
"java:S6541", // Single-threaded session contexts do not require synchronization
188+
"java:S6541" // Single-threaded session contexts do not require synchronization
188189
})
189190
private <T> T parseAnything(ClassMeta<?> eType, CborInputStream is, Object outer, BeanPropertyMeta pMeta) throws IOException, ParseException, ExecutableException {
190191

juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cbor/annotation/CborAnnotation.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ public Builder on(MethodInfo...value) {
175175
}
176176
}
177177

178+
@SuppressWarnings({
179+
"java:S2160" // equals not needed; annotation object identity is sufficient for usage in Sets/Maps
180+
})
178181
private static class Object extends AppliedOnClassAnnotationObject implements Cbor {
179182

180183
private final String[] description;

juneau-core/juneau-marshall/src/main/java/org/apache/juneau/collections/JsonMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,8 @@ public JsonMap filtered() {
751751
|| (x instanceof Boolean x2 && x2.equals(false))
752752
|| (x instanceof Number x3 && x3.intValue() == -1)
753753
|| (isArray(x) && Array.getLength(x) == 0)
754-
|| (x instanceof Map x2 && x2.isEmpty())
755-
|| (x instanceof Collection x3 && x3.isEmpty())
754+
|| (x instanceof Map<?,?> x2 && x2.isEmpty())
755+
|| (x instanceof Collection<?> x3 && x3.isEmpty())
756756
));
757757
// @formatter:on
758758
}

juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cp/BasicFileFinder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,14 @@ protected Optional<InputStream> find(String name, Locale locale) throws IOExcept
178178

179179
if (lf == null) {
180180
List<String> candidateFileNames = getCandidateFileNames(name, locale);
181-
paths: for (LocalDir root : roots) {
181+
for (LocalDir root : roots) {
182182
for (var cfn : candidateFileNames) {
183183
lf = root.resolve(cfn);
184184
if (nn(lf))
185-
break paths;
185+
break;
186186
}
187+
if (nn(lf))
188+
break;
187189
}
188190

189191
if (nn(lf) && isIgnoredFile(lf.getName()))

juneau-core/juneau-marshall/src/main/java/org/apache/juneau/hjson/HjsonTokenizer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ public static Token number(Number value) {
137137
*
138138
* @param reader The reader to tokenize.
139139
*/
140+
@SuppressWarnings({
141+
"resource" // BufferedReader is owned by PushbackReader; PushbackReader constructor never throws
142+
})
140143
public HjsonTokenizer(Reader reader) {
141144
var in = reader instanceof BufferedReader br ? br : new BufferedReader(reader);
142145
this.reader = new PushbackReader(in, 4);

0 commit comments

Comments
 (0)