Skip to content

Commit e5ea4eb

Browse files
committed
Fix reliability issues reported by Sonar
1 parent b30f281 commit e5ea4eb

20 files changed

Lines changed: 59 additions & 44 deletions

File tree

src/main/java/groovy/concurrent/AwaitableAdapterRegistry.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ public <T> Awaitable<T> toAwaitable(Object source) {
232232
cf.complete(future.get());
233233
} catch (ExecutionException e) {
234234
cf.completeExceptionally(e.getCause());
235+
} catch (InterruptedException e) {
236+
Thread.currentThread().interrupt();
237+
cf.completeExceptionally(e);
235238
} catch (Throwable e) {
236239
cf.completeExceptionally(e);
237240
}

src/main/java/groovy/io/LineColumnReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ public int read() throws IOException {
111111
newLineWasRead = true;
112112
if (c == '\r') {
113113
mark(1);
114-
c = (char)super.read();
114+
int nextChar = super.read();
115115
// check if we have \r\n like on Windows
116116
// if it's not \r\n we reset, otherwise, the \n is just consumed
117-
if (c != '\n') {
117+
if (nextChar != '\n') {
118118
reset();
119119
}
120120
}

src/main/java/groovy/lang/NumberRange.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ void calcSize(Comparable from, Comparable to, Number stepSize) {
544544

545545
private boolean isIntegral(Number stepSize) {
546546
BigDecimal tempStepSize = NumberMath.toBigDecimal(stepSize);
547-
return tempStepSize.equals(new BigDecimal(tempStepSize.toBigInteger()));
547+
return tempStepSize.compareTo(new BigDecimal(tempStepSize.toBigInteger())) == 0;
548548
}
549549

550550
/**

src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5484,7 +5484,7 @@ public List<DeclarationExpression> getDeclarationExpressions() {
54845484
// GROOVY-10355: a cast whose operand is the binary-only keyword identifier "in" or "as"
54855485
private static final String CAST_OF_BINARY_KEYWORD = "_CAST_OF_BINARY_KEYWORD";
54865486

5487-
private static final Pattern BARE_NAME_PATTERN = Pattern.compile("[A-Za-z_$][A-Za-z0-9_$]*(\\.[A-Za-z_$][A-Za-z0-9_$]*)*");
5487+
private static final Pattern BARE_NAME_PATTERN = Pattern.compile("[A-Za-z_$][A-Za-z0-9_$]*(?:\\.[A-Za-z_$][A-Za-z0-9_$]*)*");
54885488

54895489
private static final String CLASS_NAME = "_CLASS_NAME";
54905490
private static final String INSIDE_PARENTHESES_LEVEL = "_INSIDE_PARENTHESES_LEVEL";

src/main/java/org/codehaus/groovy/ast/FieldNode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ public boolean equals(Object obj) {
252252
return super.equals(obj);
253253
}
254254

255+
@Override
256+
public int hashCode() {
257+
return super.hashCode();
258+
}
259+
255260
/**
256261
* Returns the original declared type of this field before any transformations.
257262
* Useful for preserving type information through compilation phases that may modify types.

src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ private static long hash(String str) {
15571557
}
15581558
final byte[] hashBytes = md.digest(str.getBytes(StandardCharsets.UTF_8));
15591559
long hash = 0;
1560-
for (int i = Math.min(hashBytes.length, 7); i >= 0; i--) {
1560+
for (int i = Math.min(hashBytes.length - 1, 7); i >= 0; i--) {
15611561
hash = (hash << 8) | (hashBytes[i] & 0xFF);
15621562
}
15631563
return hash;

src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesMethodReferenceExpressionWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ private MethodNode chooseMethodRefMethod(final List<MethodNode> methods, final E
561561
score += 9;
562562
}
563563
return score;
564-
}).thenComparing(StaticTypesMethodReferenceExpressionWriter::isExtensionMethod)).get();
564+
}).thenComparing(StaticTypesMethodReferenceExpressionWriter::isExtensionMethod)).orElse(null);
565565
}
566566

567567
private List<MethodNode> findVisibleMethods(final String name, final ClassNode type) {

src/main/java/org/codehaus/groovy/runtime/MetaClassHelper.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,12 +1024,12 @@ public static void unwrap(Object[] arguments) {
10241024
*/
10251025
public static Object normalizeBoxedReturn(final Object value, final Class<?> returnType) {
10261026
if (value == null || !returnType.isPrimitive()) return value; // null includes void
1027-
if (returnType == int.class) return Integer.valueOf((Integer) value);
1028-
if (returnType == boolean.class) return Boolean.valueOf((Boolean) value);
1029-
if (returnType == long.class) return Long.valueOf((Long) value);
1030-
if (returnType == char.class) return Character.valueOf((Character) value);
1031-
if (returnType == byte.class) return Byte.valueOf((Byte) value);
1032-
if (returnType == short.class) return Short.valueOf((Short) value);
1027+
if (returnType == int.class) return Integer.valueOf(((Integer) value).intValue());
1028+
if (returnType == boolean.class) return Boolean.valueOf(((Boolean) value).booleanValue());
1029+
if (returnType == long.class) return Long.valueOf(((Long) value).longValue());
1030+
if (returnType == char.class) return Character.valueOf(((Character) value).charValue());
1031+
if (returnType == byte.class) return Byte.valueOf(((Byte) value).byteValue());
1032+
if (returnType == short.class) return Short.valueOf(((Short) value).shortValue());
10331033
return value; // float/double: valueOf does not cache on any path
10341034
}
10351035

src/test/groovy/org/codehaus/groovy/runtime/memoize/CacheCleanupTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class CacheCleanupTest {
5757
cache.put('key2', new SoftReference(ANCHOR))
5858
assert cache.@map.size() == 2
5959
for (i in (3..1000)) {
60-
cache.put("key${i}", new SoftReference(null)) //Simulating evicted objects
60+
cache.put('key' + i, new SoftReference(null)) //Simulating evicted objects
6161
cache.get('key1') //touch the non-null cache entries to keep them hot to prevent a potential LRU algorithm from evicting them
6262
cache.get('key2')
6363
}

src/test/groovy/org/codehaus/groovy/util/ManagedConcurrentValueMapTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class ManagedConcurrentValueMapTest {
2828
@Test
2929
void testEntriesRemoveSelfFromMapWhenFinalized() {
3030
for (int i = 0; i < 5; i++) {
31-
map.put("Key${i}", new Object())
31+
map.put('Key' + i, new Object())
3232
}
3333

3434
assert map.@internalMap.size() == 5

0 commit comments

Comments
 (0)