Skip to content
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public class AvroEntryBuilder extends SchemaImpl.EntryImpl.BuilderImpl {

@Override
public Schema.Entry.Builder withElementSchema(final Schema schema) {
if (schema instanceof AvroSchema) {
final AvroSchema innerSchema = (AvroSchema) schema;
if (schema instanceof AvroSchema innerSchema) {
AvroSchema avroSchema = this.authorizeNull(innerSchema);
return super.withElementSchema(avroSchema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@
}

public AvroRecord(final Record record) {
if (record instanceof AvroRecord) {
final AvroRecord avr = (AvroRecord) record;
if (record instanceof AvroRecord avr) {
this.delegate = avr.delegate;
this.schema = avr.schema;
return;
Expand Down Expand Up @@ -105,41 +104,41 @@
// RecordImpl store BigDecimal directly, no any convert as not necessary, so here need to convert to string for
// beam's AvroCoder which cloud platform use
// also here for any Collection<BigDecimal> as Array type
if (value instanceof BigDecimal) {
return BigDecimal.class.cast(value).toString();
if (value instanceof BigDecimal bigDecimal) {
return bigDecimal.toString();
}

if (value instanceof Collection) {
return Collection.class.cast(value).stream().map(v -> this.directMapping(v, entry)).collect(toList());
if (value instanceof Collection collection) {

Check warning on line 111 in component-runtime-beam/src/main/java/org/talend/sdk/component/runtime/beam/spi/record/AvroRecord.java

View check run for this annotation

sonar-rnd / SonarQube Code Analysis

component-runtime-beam/src/main/java/org/talend/sdk/component/runtime/beam/spi/record/AvroRecord.java#L111

Provide the parametrized type for this generic.
return collection.stream().map(v -> this.directMapping(v, entry)).collect(toList());

Check warning on line 112 in component-runtime-beam/src/main/java/org/talend/sdk/component/runtime/beam/spi/record/AvroRecord.java

View check run for this annotation

sonar-rnd / SonarQube Code Analysis

component-runtime-beam/src/main/java/org/talend/sdk/component/runtime/beam/spi/record/AvroRecord.java#L112

Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()' and ensure that the list is unmodified.
}
if (value instanceof RecordImpl) {
return new AvroRecord((Record) value).delegate;
}
if (value instanceof Record) {
return Unwrappable.class.cast(value).unwrap(IndexedRecord.class);
}
if (value instanceof ZonedDateTime) {
return ZonedDateTime.class.cast(value).toInstant().toEpochMilli();
if (value instanceof ZonedDateTime zonedDateTime) {
return zonedDateTime.toInstant().toEpochMilli();
}
if (value instanceof Date) {
return Date.class.cast(value).getTime();
if (value instanceof Date date) {
return date.getTime();
}
if (value instanceof byte[]) {
return ByteBuffer.wrap(byte[].class.cast(value));
if (value instanceof byte[] bytes) {
return ByteBuffer.wrap(bytes);
}

if (value instanceof Long) {
if (value instanceof Long longValue) {
String logicalType = entry.getLogicalType();
if (logicalType != null) {
if (SchemaProperty.LogicalType.DATE.key().equals(logicalType)) {
return Math.toIntExact(
Instant.ofEpochMilli((Long) value)
Instant.ofEpochMilli(longValue)
.atZone(UTC)
.toLocalDate()
.toEpochDay()); // Avro stores dates as int
} else if (LogicalType.TIME.key().equals(logicalType)) {
// QTDI-1252: Avro time-millis logical type stores int milliseconds from 0:00:00 not from Unix Epoch
final Instant instant = Instant.ofEpochMilli((Long) value);
final Instant instant = Instant.ofEpochMilli(longValue);
final ZonedDateTime zonedDateTime = instant.atZone(UTC);
return Math.toIntExact(zonedDateTime.toLocalTime().toNanoOfDay() / 1_000_000);
}
Expand Down Expand Up @@ -239,18 +238,19 @@
return doMap(expectedType, unwrapUnion(fieldSchema), value);
}

private <T> T doMap(final Class<T> expectedType, final org.apache.avro.Schema fieldSchemaRaw, final Object value) {

Check failure on line 241 in component-runtime-beam/src/main/java/org/talend/sdk/component/runtime/beam/spi/record/AvroRecord.java

View check run for this annotation

sonar-rnd / SonarQube Code Analysis

component-runtime-beam/src/main/java/org/talend/sdk/component/runtime/beam/spi/record/AvroRecord.java#L241

Refactor this method to reduce its Cognitive Complexity from 59 to the 15 allowed.

if (value != null && expectedType == value.getClass() && !(value instanceof Collection)) {
return expectedType.cast(value);
}

if (value instanceof IndexedRecord && (Record.class == expectedType || Object.class == expectedType)) {
return expectedType.cast(new AvroRecord(IndexedRecord.class.cast(value)));
if (value instanceof IndexedRecord indexedRecord
&& (Record.class == expectedType || Object.class == expectedType)) {
return expectedType.cast(new AvroRecord(indexedRecord));
}

if (value instanceof ByteBuffer && byte[].class == expectedType) {
return expectedType.cast(ByteBuffer.class.cast(value).array());
if (value instanceof ByteBuffer byteBuffer && byte[].class == expectedType) {
return expectedType.cast(byteBuffer.array());
}

final org.apache.avro.Schema fieldSchema = unwrapUnion(fieldSchemaRaw);
Expand Down Expand Up @@ -311,8 +311,8 @@
.cast(doMapCollection(itemType, Collection.class.cast(value), fieldSchema.getElementType()));
}

if (value instanceof org.joda.time.DateTime && ZonedDateTime.class == expectedType) {
final long epochMilli = org.joda.time.DateTime.class.cast(value).getMillis();
if (value instanceof org.joda.time.DateTime dateTime && ZonedDateTime.class == expectedType) {
final long epochMilli = dateTime.getMillis();
return expectedType.cast(ZonedDateTime.ofInstant(java.time.Instant.ofEpochMilli(epochMilli), UTC));
}

Expand All @@ -338,7 +338,7 @@
if (value instanceof Utf8 && Object.class == expectedType) {
return expectedType.cast(value.toString());
}
if (Collection.class.isAssignableFrom(expectedType) && value instanceof Collection) {
if (Collection.class.isAssignableFrom(expectedType) && value instanceof Collection collection) {

Check warning on line 341 in component-runtime-beam/src/main/java/org/talend/sdk/component/runtime/beam/spi/record/AvroRecord.java

View check run for this annotation

sonar-rnd / SonarQube Code Analysis

component-runtime-beam/src/main/java/org/talend/sdk/component/runtime/beam/spi/record/AvroRecord.java#L341

Provide the parametrized type for this generic.
final org.apache.avro.Schema elementType = fieldSchema.getElementType();
final org.apache.avro.Schema elementSchema = unwrapUnion(elementType);
Class<?> toType = Object.class;
Expand All @@ -347,7 +347,7 @@
} else if (elementSchema.getType() == org.apache.avro.Schema.Type.ARRAY) {
toType = Collection.class;
}
final Collection<?> objects = this.doMapCollection(toType, Collection.class.cast(value), elementSchema);
final Collection<?> objects = this.doMapCollection(toType, collection, elementSchema);
return expectedType.cast(objects);
}
return expectedType.cast(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public AvroSchema find(final Schema schema) {
if (schema == null || schema instanceof AvroSchema) {
return (AvroSchema) schema;
}
if (schema instanceof SchemaImpl) {
final SchemaImpl realSchema = (SchemaImpl) schema;
if (schema instanceof SchemaImpl realSchema) {
if ((!this.cache.containsKey(realSchema))
&& this.cache.size() >= AvroSchemaCache.MAX_SIZE) {
this.removeOldest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,16 @@ public Builder with(final Entry entry, final Object value) {
if (entry.getType() == Schema.Type.DATETIME) {
if (value == null) {
withDateTime(entry, (ZonedDateTime) value);
} else if (value instanceof Long) {
withTimestamp(entry, (Long) value);
} else if (value instanceof Date) {
withDateTime(entry, (Date) value);
} else if (value instanceof ZonedDateTime) {
withDateTime(entry, (ZonedDateTime) value);
} else if (value instanceof Instant) {
withInstant(entry, (Instant) value);
} else if (value instanceof Temporal) {
withTimestamp(entry, ((Temporal) value).get(ChronoField.INSTANT_SECONDS) * 1000L);
} else if (value instanceof Long longValue) {
withTimestamp(entry, longValue);
} else if (value instanceof Date date) {
withDateTime(entry, date);
} else if (value instanceof ZonedDateTime zonedDateTime) {
withDateTime(entry, zonedDateTime);
} else if (value instanceof Instant instant) {
withInstant(entry, instant);
} else if (value instanceof Temporal temporal) {
withTimestamp(entry, temporal.get(ChronoField.INSTANT_SECONDS) * 1000L);
}
return this;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,14 +781,12 @@ public ParameterMeta findDatastoreParameterMeta(final String plugin, final Strin
*/
public static Map<String, String> jsonToMap(final JsonValue jsonValue, final String path) {
final Map<String, String> result = new HashMap<>();
if (jsonValue instanceof JsonObject) {
JsonObject jsonObj = (JsonObject) jsonValue;
if (jsonValue instanceof JsonObject jsonObj) {
for (String key : jsonObj.keySet()) {
String newPath = path.isEmpty() ? key : path + "." + key;
result.putAll(jsonToMap(jsonObj.get(key), newPath));
}
} else if (jsonValue instanceof JsonArray) {
JsonArray jsonArray = (JsonArray) jsonValue;
} else if (jsonValue instanceof JsonArray jsonArray) {
for (int i = 0; i < jsonArray.size(); i++) {
String newPath = path + "[" + i + "]";
result.putAll(jsonToMap(jsonArray.get(i), newPath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public Object createServiceInstance(final ClassLoader loader, final String conta
.initialize(instance,
new InterceptorHandlerFacade(serviceClass.getConstructor().newInstance(), allServices));
}
if (instance instanceof BaseService) {
this.updateService((BaseService) instance, containerId, serviceClass.getName());
if (instance instanceof BaseService baseService) {
this.updateService(baseService, containerId, serviceClass.getName());
}
return instance;
} catch (final InstantiationException | IllegalAccessException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ private Schema toSchema(final JsonObject json) {
this.addProps(builder::withProp, json);

final JsonValue orderValue = json.get("order");
if (orderValue instanceof JsonString) {
final Schema.EntriesOrder order = Schema.EntriesOrder.of(((JsonString) orderValue).getString());
if (orderValue instanceof JsonString jsonString) {
final Schema.EntriesOrder order = Schema.EntriesOrder.of(jsonString.getString());
return builder.build(order);
} else {
return builder.build();
Expand All @@ -109,11 +109,11 @@ private Schema toSchema(final JsonObject json) {
private void treatElementSchema(final JsonObject json,
final Consumer<Schema> setter) {
final JsonValue elementSchema = json.get(ELEMENT_SCHEMA);
if (elementSchema instanceof JsonObject) {
final Schema schema = this.toSchema((JsonObject) elementSchema);
if (elementSchema instanceof JsonObject jsonObject) {
final Schema schema = this.toSchema(jsonObject);
setter.accept(schema);
} else if (elementSchema instanceof JsonString) {
final Schema.Type innerType = Schema.Type.valueOf(((JsonString) elementSchema).getString());
} else if (elementSchema instanceof JsonString jsonString) {
final Schema.Type innerType = Schema.Type.valueOf(jsonString.getString());
setter.accept(this.factory.newSchemaBuilder(innerType).build());
}
}
Expand Down Expand Up @@ -276,29 +276,29 @@ private JsonValue toValue(final Object object) {
if (object == null) {
return JsonValue.NULL;
}
if (object instanceof Integer) {
return Json.createValue((Integer) object);
if (object instanceof Integer i) {
return Json.createValue(i);
}
if (object instanceof Long) {
return Json.createValue((Long) object);
if (object instanceof Long l) {
return Json.createValue(l);
}
if (object instanceof Double || object instanceof Float) {
return Json.createValue((Double) object);
Comment thread
undx marked this conversation as resolved.
}
if (object instanceof BigInteger) {
return Json.createValue((BigInteger) object);
if (object instanceof BigInteger bi) {
return Json.createValue(bi);
}
if (object instanceof Boolean) {
if (object == Boolean.TRUE) {
return JsonValue.TRUE;
}
return JsonValue.FALSE;
}
if (object instanceof BigDecimal) {
return Json.createValue((BigDecimal) object);
if (object instanceof BigDecimal bd) {
return Json.createValue(bd);
}
if (object instanceof String) {
return Json.createValue((String) object);
if (object instanceof String s) {
return Json.createValue(s);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ void findException() {
}

private Record toRecord(final Object object) {
if (object instanceof Record) {
return (Record) object;
if (object instanceof Record rcd) {
return rcd;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ public int hashCode() {
public boolean equals(final Object obj) {
if (this == obj) {
return true;
} else if (obj instanceof ParameterizedType) {
final ParameterizedType that = (ParameterizedType) obj;
} else if (obj instanceof ParameterizedType that) {
final Type thatRawType = that.getRawType();
return that.getOwnerType() == null
&& (rawType == null ? thatRawType == null : rawType.equals(thatRawType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public OutputFactory asOutputFactory() {
if (ref != null && value != null) {
if (value instanceof javax.json.JsonValue) {
ref.add(jsonb.fromJson(value.toString(), ref.getType()));
} else if (value instanceof Record) {
ref.add(registry.find(ref.getType()).newInstance(Record.class.cast(value)));
} else if (value instanceof Record rcd) {
ref.add(registry.find(ref.getType()).newInstance(rcd));
} else {
ref.add(jsonb.fromJson(jsonb.toJson(value), ref.getType()));
}
Expand All @@ -67,8 +67,8 @@ public OutputFactory asOutputFactoryForGuessSchema() {
if (ref != null && value != null) {
if (value instanceof javax.json.JsonValue) {
ref.add(jsonb.fromJson(value.toString(), ref.getType()));
} else if (value instanceof Record) {
ref.add(((Record) value).getSchema());
} else if (value instanceof Record rcd) {
ref.add(rcd.getSchema());
} else if (value instanceof Schema) {
ref.add(value);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@

private DiscoverSchemaException transformException(final Exception e) {
DiscoverSchemaException discoverSchemaException;
if (e instanceof DiscoverSchemaException) {
discoverSchemaException = DiscoverSchemaException.class.cast(e);
} else if (e instanceof ComponentException) {
discoverSchemaException = new DiscoverSchemaException((ComponentException) e);
if (e instanceof DiscoverSchemaException dse) {
discoverSchemaException = dse;
} else if (e instanceof ComponentException ce) {
discoverSchemaException = new DiscoverSchemaException(ce);
} else {
discoverSchemaException = new DiscoverSchemaException(e.getMessage(), e.getStackTrace(), EXCEPTION);
}
Expand Down Expand Up @@ -253,12 +253,11 @@
}
final Object schemaResult =
actionRef.getInvoker().apply(buildActionConfig(actionRef, configuration, schema, branch));
if (schemaResult instanceof Schema) {
final Schema result = (Schema) schemaResult;
if (schemaResult instanceof Schema result) {
if (result.getEntries().isEmpty()) {
throw new DiscoverSchemaException(ERROR_NO_AVAILABLE_SCHEMA_FOUND, EXCEPTION);
} else {
fromSchema(Schema.class.cast(schemaResult));
fromSchema(result);
}
}
}
Expand Down Expand Up @@ -468,8 +467,8 @@
: buildActionConfig(actionRef, configuration, schema, "INPUT");
final Object schemaResult = actionRef.getInvoker().apply(actionConfiguration);

if (schemaResult instanceof Schema) {
return fromSchema(Schema.class.cast(schemaResult));
if (schemaResult instanceof Schema resultSchema) {
return fromSchema(resultSchema);

} else {
log.error(ERROR_INSTANCE_SCHEMA);
Expand Down Expand Up @@ -501,8 +500,7 @@
public Collection<Column> getFixedSchema(final String execute) {
SchemaConverter sc = new SchemaConverter();
Object o = sc.toObjectImpl(execute);
if (o instanceof Schema) {
final Schema schema = Schema.class.cast(o);
if (o instanceof Schema schema) {
final Collection<Schema.Entry> entries = schema.getEntries();
if (entries == null || entries.isEmpty()) {
log.info(NO_COLUMN_FOUND_BY_GUESS_SCHEMA);
Expand Down Expand Up @@ -675,10 +673,10 @@
if (rowObject == null) {
return false;
}
if (rowObject instanceof Record) {
return fromSchema(Record.class.cast(rowObject).getSchema());
} else if (rowObject instanceof java.util.Map) {
return guessInputSchemaThroughResults(input, (java.util.Map) rowObject);
if (rowObject instanceof Record rcd) {
return fromSchema(rcd.getSchema());
} else if (rowObject instanceof java.util.Map map) {

Check warning on line 678 in component-studio/component-runtime-di/src/main/java/org/talend/sdk/component/runtime/di/schema/TaCoKitGuessSchema.java

View check run for this annotation

sonar-rnd / SonarQube Code Analysis

component-studio/component-runtime-di/src/main/java/org/talend/sdk/component/runtime/di/schema/TaCoKitGuessSchema.java#L678

Provide the parametrized type for this generic.
return guessInputSchemaThroughResults(input, map);
} else if (rowObject instanceof java.util.Collection) {
throw new Exception("Can't guess schema from a Collection");
} else {
Expand Down Expand Up @@ -707,12 +705,12 @@
* @return true if completed; false if one more result row is needed.
*/
public boolean guessSchemaThroughResult(final Object rowObject) throws Exception {
if (rowObject instanceof java.util.Map) {
return guessSchemaThroughResult((java.util.Map) rowObject);
} else if (rowObject instanceof Schema) {
return fromSchema(Schema.class.cast(rowObject));
} else if (rowObject instanceof Record) {
return fromSchema(Record.class.cast(rowObject).getSchema());
if (rowObject instanceof java.util.Map map) {

Check warning on line 708 in component-studio/component-runtime-di/src/main/java/org/talend/sdk/component/runtime/di/schema/TaCoKitGuessSchema.java

View check run for this annotation

sonar-rnd / SonarQube Code Analysis

component-studio/component-runtime-di/src/main/java/org/talend/sdk/component/runtime/di/schema/TaCoKitGuessSchema.java#L708

Provide the parametrized type for this generic.
return guessSchemaThroughResult(map);
} else if (rowObject instanceof Schema schema) {
return fromSchema(schema);
} else if (rowObject instanceof Record rcd) {
return fromSchema(rcd.getSchema());
} else if (rowObject instanceof java.util.Collection) {
throw new Exception("Can't guess schema from a Collection");
} else {
Expand Down
Loading
Loading