Skip to content

Commit

Permalink
chore(sonar): Fix instanceof check and cast with pattern matching in …
Browse files Browse the repository at this point in the history
…juel. (operaton#85)
  • Loading branch information
Dirk Olmes committed Nov 8, 2024
1 parent 6b26fee commit 82f1cbd
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ protected Method getMethod(Bindings bindings, ELContext context, Class<?> return
if (value == null) {
throw new MethodNotFoundException(LocalMessages.get("error.identifier.method.notfound", name));
}
if (value instanceof Method) {
Method method = (Method)value;
if (value instanceof Method method) {
if (returnType != null && !returnType.isAssignableFrom(method.getReturnType())) {
throw new MethodNotFoundException(LocalMessages.get("error.identifier.method.notfound", name));
}
Expand Down
3 changes: 1 addition & 2 deletions juel/src/main/java/org/operaton/bpm/impl/juel/Bindings.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ public <T> T convert(Object value, Class<T> type) {

@Override
public boolean equals(Object obj) {
if (obj instanceof Bindings) {
Bindings other = (Bindings)obj;
if (obj instanceof Bindings other) {
return Arrays.equals(functions, other.functions)
&& Arrays.equals(variables, other.variables)
&& converter.equals(other.converter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ private static final boolean lt0(TypeConverter converter, Object o1, Object o2)
if (t1 == String.class || t2 == String.class) {
return converter.convert(o1, String.class).compareTo(converter.convert(o2, String.class)) < 0;
}
if (o1 instanceof Comparable) {
return ((Comparable)o1).compareTo(o2) < 0;
if (o1 instanceof Comparable comparable) {
return comparable.compareTo(o2) < 0;
}
if (o2 instanceof Comparable) {
return ((Comparable)o2).compareTo(o1) > 0;
if (o2 instanceof Comparable comparable) {
return comparable.compareTo(o1) > 0;
}
throw new ELException(LocalMessages.get("error.compare.types", o1.getClass(), o2.getClass()));
}
Expand All @@ -84,11 +84,11 @@ private static final boolean gt0(TypeConverter converter, Object o1, Object o2)
if (t1 == String.class || t2 == String.class) {
return converter.convert(o1, String.class).compareTo(converter.convert(o2, String.class)) > 0;
}
if (o1 instanceof Comparable) {
return ((Comparable)o1).compareTo(o2) > 0;
if (o1 instanceof Comparable comparable) {
return comparable.compareTo(o2) > 0;
}
if (o2 instanceof Comparable) {
return ((Comparable)o2).compareTo(o1) < 0;
if (o2 instanceof Comparable comparable) {
return comparable.compareTo(o1) < 0;
}
throw new ELException(LocalMessages.get("error.compare.types", o1.getClass(), o2.getClass()));
}
Expand Down Expand Up @@ -177,14 +177,14 @@ public static final boolean empty(TypeConverter converter, Object o) {
if (o == null || "".equals(o)) {
return true;
}
if (o instanceof Object[]) {
return ((Object[])o).length == 0;
if (o instanceof Object[] array) {
return array.length == 0;
}
if (o instanceof Map<?,?>) {
return ((Map<?,?>)o).isEmpty();
if (o instanceof Map<?,?> map) {
return map.isEmpty();
}
if (o instanceof Collection<?>) {
return ((Collection<?>)o).isEmpty();
if (o instanceof Collection<?> collection) {
return collection.isEmpty();
}
return false;
}
Expand Down
38 changes: 19 additions & 19 deletions juel/src/main/java/org/operaton/bpm/impl/juel/NumberOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private final static boolean isDotEe(String value) {
}

private final static boolean isDotEe(Object value) {
return value instanceof String && isDotEe((String)value);
return value instanceof String stringValue && isDotEe(stringValue);
}

private static final boolean isFloatOrDouble(Object value) {
Expand Down Expand Up @@ -144,35 +144,35 @@ public static final Number neg(TypeConverter converter, Object value) {
if (value == null) {
return LONG_ZERO;
}
if (value instanceof BigDecimal) {
return ((BigDecimal)value).negate();
if (value instanceof BigDecimal bigDecimal) {
return bigDecimal.negate();
}
if (value instanceof BigInteger) {
return ((BigInteger)value).negate();
if (value instanceof BigInteger bigInteger) {
return bigInteger.negate();
}
if (value instanceof Double) {
return -((Double) value).doubleValue();
if (value instanceof Double doubleValue) {
return -doubleValue.doubleValue();
}
if (value instanceof Float) {
return -((Float) value).floatValue();
if (value instanceof Float floatValue) {
return -floatValue.floatValue();
}
if (value instanceof String) {
if (isDotEe((String)value)) {
if (value instanceof String stringValue) {
if (isDotEe(stringValue)) {
return -converter.convert(value, Double.class).doubleValue();
}
return -converter.convert(value, Long.class).longValue();
}
if (value instanceof Long) {
return -((Long) value).longValue();
if (value instanceof Long longValue) {
return -longValue.longValue();
}
if (value instanceof Integer) {
return -((Integer) value).intValue();
if (value instanceof Integer integerValue) {
return -integerValue.intValue();
}
if (value instanceof Short) {
return (short) -((Short) value).shortValue();
if (value instanceof Short shortValue) {
return (short) -shortValue.shortValue();
}
if (value instanceof Byte) {
return (byte) -((Byte) value).byteValue();
if (value instanceof Byte byteValue) {
return (byte) -byteValue.byteValue();
}
throw new ELException(LocalMessages.get("error.negate", value.getClass()));
}
Expand Down
Loading

0 comments on commit 82f1cbd

Please sign in to comment.