Skip to content
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

Java17 syntax #9327

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.persistence.FlushModeType;

Expand Down Expand Up @@ -283,8 +282,8 @@ <T> DataResult<T> execute(Map<String, ?> parameters, List<?> inClause,

private Integer internalExecuteUpdateNoSubClause(Map<String, ?> parameters, Mode mode) {
Object resultObj = executeChecking(sqlStatement, qMap, parameters, mode, null);
if (resultObj instanceof Integer) {
return (Integer) resultObj;
if (resultObj instanceof Integer integer) {
return integer;
}
return 0;
}
Expand Down Expand Up @@ -316,8 +315,8 @@ private int internalExecuteUpdate(Map<String, ?> parameters, List<?> inClause, M
Object resultObj = executeChecking(finalQuery, qMap, parameters, mode, null);
subStart += subLength;

if (resultObj instanceof Integer) {
returnInt = returnInt + (Integer) resultObj;
if (resultObj instanceof Integer integer) {
returnInt = returnInt + integer;
}
}
return returnInt;
Expand Down Expand Up @@ -359,7 +358,7 @@ private <T> DataResult<T> internalExecute(Map<String, ?> parameters, List<?> inC
.map(finalQuery -> executeChecking(finalQuery, qMap, parameters, mode, null))
.filter(res -> drClazz.isAssignableFrom(res.getClass()))
.map(res -> drClazz.cast(res))
.collect(Collectors.toList());
.toList();
DataResult<T> result = null;
if (!results.isEmpty()) {
result = results.get(0);
Expand All @@ -384,8 +383,8 @@ private String commaSeparatedList(List<?> list) {
else {
firstValue = false;
}
if (value instanceof String) {
sb.append("'").append((String) value).append("'");
if (value instanceof String str) {
sb.append("'").append(str).append("'");
}
else {
sb.append(value);
Expand Down Expand Up @@ -560,8 +559,8 @@ private PreparedStatement prepareStatement(Connection connection, String sql, Mo
PreparedStatement ps = connection.prepareStatement(sql);

// allow limiting the results for better performance.
if (mode != null && mode instanceof SelectMode) {
ps.setMaxRows(((SelectMode) mode).getMaxRows());
if (mode instanceof SelectMode smode) {
ps.setMaxRows(smode.getMaxRows());
}
return ps;
}
Expand All @@ -579,8 +578,8 @@ private Map<String, Object> processOutputParams(CallableStatement cs,
// to do otherwise just doesn't make a lot of sense.
Integer pos = positions.next();
Object o = cs.getObject(pos);
if (o instanceof BigDecimal) {
o = ((BigDecimal) o).longValue();
if (o instanceof BigDecimal bdec) {
o = bdec.longValue();
}
result.put(param, o);
}
Expand All @@ -607,8 +606,8 @@ Map<String, Object> executeCallable(Map<String, Object> inParams,
"HibernateException executing CachedStatement", he);
}
catch (RuntimeException e) {
if (e.getCause() instanceof SQLException) {
throw SqlExceptionTranslator.sqlException((SQLException) e.getCause());
if (e.getCause() instanceof SQLException ex) {
throw SqlExceptionTranslator.sqlException(ex);
}
throw e;
}
Expand Down Expand Up @@ -773,8 +772,7 @@ private void addToObject(List<String> columns, ResultSet rs, Object obj,
throws SQLException {

List<String> columnSkip;
if (elaborator && obj instanceof RowCallback) {
RowCallback cb = (RowCallback) obj;
if (elaborator && obj instanceof RowCallback cb) {
cb.callback(rs);
columnSkip = cb.getCallBackColumns();
}
Expand Down Expand Up @@ -917,8 +915,8 @@ private Map<Object, Integer> generatePointers(List<Object> dr, String key) {
while (i.hasNext()) {
Object row = i.next();

if (row instanceof Row) {
pointers.put(((Row) row).get(key), pos);
if (row instanceof Row r) {
pointers.put(r.get(key), pos);
}
else {
Object keyData = MethodUtil.callMethod(row,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
* A Finder implementation that uses multiple finders to search
Expand All @@ -35,6 +34,6 @@ class CompositeFinder implements Finder {
public List<String> findExcluding(String[] excluding, String endStr) {
return this.finderList.stream()
.flatMap(finder -> finder.findExcluding(excluding, endStr).stream())
.collect(Collectors.toList());
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import javax.persistence.FlushModeType;
Expand Down Expand Up @@ -150,8 +149,7 @@ private <T> void bindParameters(Query<T> query, Map<String, Object> parameters)
}

for (Map.Entry<String, Object> entry: parameters.entrySet()) {
if (entry.getValue() instanceof Collection) {
Collection<?> c = (Collection<?>) entry.getValue();
if (entry.getValue() instanceof Collection c) {
if (c.size() > 1000) {
LOG.error("Query executed with Collection larger than 1000");
}
Expand Down Expand Up @@ -551,12 +549,12 @@ public static String getByteArrayContents(byte[] barr) {
public static String getBlobContents(Object blob) {
// Returned by Hibernate, and also returned by mode queries
// from an Oracle database
if (blob instanceof byte[]) {
return getByteArrayContents((byte[]) blob);
if (blob instanceof byte[] byt) {
return getByteArrayContents(byt);
}
// Returned only by mode queries from a Postgres database
if (blob instanceof Blob) {
return getByteArrayContents(blobToByteArray((Blob) blob));
if (blob instanceof Blob blb) {
return getByteArrayContents(blobToByteArray(blb));
}
return null;
}
Expand Down Expand Up @@ -814,7 +812,7 @@ protected static <E, T, R> T splitAndExecuteQuery(List<E> list, String parameter

List<List<E>> batches = IntStream.iterate(0, i -> i < size, i -> i + LIST_BATCH_MAX_SIZE)
.mapToObj(i -> list.subList(i, Math.min(i + LIST_BATCH_MAX_SIZE, size)))
.collect(Collectors.toList());
.toList();
return batches.stream()
.map(b -> {
query.setParameterList(parameterName, b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import javax.persistence.FlushModeType;
Expand Down Expand Up @@ -540,12 +539,12 @@ public String getByteArrayContents(byte[] barr) {
public String getBlobContents(Object blob) {
// Returned by Hibernate, and also returned by mode queries
// from an Oracle database
if (blob instanceof byte[]) {
return getByteArrayContents((byte[]) blob);
if (blob instanceof byte[] byt) {
return getByteArrayContents(byt);
}
// Returned only by mode queries from a Postgres database
if (blob instanceof Blob) {
return getByteArrayContents(blobToByteArray((Blob) blob));
if (blob instanceof Blob blb) {
return getByteArrayContents(blobToByteArray(blb));
}
return null;
}
Expand Down Expand Up @@ -803,7 +802,7 @@ private <E, T, R> T splitAndExecuteQuery(List<E> list, String parameterName,

List<List<E>> batches = IntStream.iterate(0, i -> i < size, i -> i + LIST_BATCH_MAX_SIZE)
.mapToObj(i -> list.subList(i, Math.min(i + LIST_BATCH_MAX_SIZE, size)))
.collect(Collectors.toList());
.toList();
return batches.stream()
.map(b -> {
query.setParameterList(parameterName, b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public void run() {
for (MessageAction action : actionHandlers) {
LOG.debug("run() - got action: {}", action.getClass().getName());
try {
if (msg instanceof EventDatabaseMessage) {
EventDatabaseMessage evtdb = (EventDatabaseMessage) msg;
if (msg instanceof EventDatabaseMessage evtdb) {
LOG.debug("Got a EventDatabaseMessage");
while (evtdb.getTransaction().isActive()) {
if (LOG.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,18 @@ public Long getAsLong(Object o) {
Long ret = null;

if (o != null) {
if (o instanceof String[]) {
String[] s = (String[])o;
if (o instanceof String[] s) {
if (s.length > 0 && !StringUtils.isEmpty(s[0])) {
ret = Long.valueOf(s[0]);
}
}
else if (o instanceof String) {
String s = (String)o;
else if (o instanceof String s) {
if (!StringUtils.isEmpty(s)) {
ret = Long.valueOf(s);
}
}
else if (o instanceof Long) {
ret = (Long) o;
else if (o instanceof Long l) {
ret = l;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public class ChannelAclHandler extends BaseHandler {
protected Channel getChannel(User usr, Map<String, Object> ctx) {
Object cidObj = ctx.get(CID);
String cidStr = null;
if (cidObj instanceof String) {
cidStr = (String)cidObj;
if (cidObj instanceof String str) {
cidStr = str;
}
else if (cidObj instanceof String[]) {
cidStr = ((String[])cidObj)[0];
else if (cidObj instanceof String[] sarr) {
cidStr = sarr[0];
}

Long cid = null;
Expand Down
4 changes: 2 additions & 2 deletions java/code/src/com/redhat/rhn/common/util/CSVWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ private void writeList(List<?> values) throws IOException {
while (itr.hasNext()) {
Object value = itr.next();
// If it's a List of Strings
if (value instanceof String) {
write((String) value);
if (value instanceof String str) {
write(str);
if (itr.hasNext()) {
writeSeparator();
}
Expand Down
11 changes: 5 additions & 6 deletions java/code/src/com/redhat/rhn/common/util/DatePicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,14 @@ public void readMap(Map<String, ?> map) {
if (value == null) {
fieldValue = null;
}
else if (value instanceof Integer) {
fieldValue = (Integer) value;
else if (value instanceof Integer integer) {
fieldValue = integer;
}
else if (value instanceof String) {
fieldValue = Integer.parseInt((String) value);
else if (value instanceof String str) {
fieldValue = Integer.parseInt(str);
}
//this is necessary for reading request parameters.
else if (value instanceof String[]) {
String[] s = (String[]) value;
else if (value instanceof String[] s) {
if (StringUtils.isEmpty(s[0])) {
fieldValue = null;
}
Expand Down
5 changes: 2 additions & 3 deletions java/code/src/com/redhat/rhn/common/util/ServletUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public static String pathWithParams(String base, Map<String, Object> params) {
if (me.getValue() == null) {
values = List.of();
}
else if (me.getValue() instanceof Object[]) {
Object[] paramValues = (Object[]) me.getValue();
else if (me.getValue() instanceof Object[] paramValues) {
values = Arrays.stream(paramValues)
.map(o -> StringUtil.urlEncode(String.valueOf(o)))
.collect(Collectors.toList());
Expand All @@ -102,7 +101,7 @@ else if (me.getValue() instanceof List) {

List<String> allValues = values.stream()
.map(v -> StringUtil.urlEncode(me.getKey()) + "=" + v)
.collect(Collectors.toList());
.toList();

if (!allValues.isEmpty()) {
if (firstPass) {
Expand Down
13 changes: 6 additions & 7 deletions java/code/src/com/redhat/rhn/common/util/XmlToPlainText.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,19 @@ public String convert(String snippet) {
}

private void toPlainText(Object current) {
if (current instanceof Text) {
process((Text)current);
if (current instanceof Text text) {
process(text);
}
else if (current instanceof Element) {
Element elem = (Element)current;
else if (current instanceof Element elem) {
if ("a".equalsIgnoreCase(elem.getName())) {
href = elem.getAttributeValue("href").trim();
}
for (Object o : elem.getContent()) {
toPlainText(o);
}
}
else if (current instanceof Document) {
for (Object o : ((Document)current).getContent()) {
else if (current instanceof Document doc) {
for (Object o : doc.getContent()) {
toPlainText(o);
}
}
Expand All @@ -82,7 +81,7 @@ else if (current instanceof Document) {
private void process(Text current) {
String text = current.getTextTrim();
if (!StringUtils.isBlank(text)) {
if (plainText.length() > 0 && !IGNORABLES.contains(text)) {
if (!plainText.isEmpty() && !IGNORABLES.contains(text)) {
plainText.append(" ");
}
plainText.append(text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.redhat.rhn.common.util.manifestfactory.ManifestFactoryLookupException;
import com.redhat.rhn.testing.RhnBaseTestCase;
Expand Down Expand Up @@ -48,7 +48,7 @@ public void testFactory() {

List l = (List)PrimitiveFactory.getObject("list-object");
assertEquals(l.size(), 12);
assertTrue(l.get(0) instanceof java.lang.String);
assertInstanceOf(String.class, l.get(0));
}

@Test
Expand Down
Loading
Loading