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

Migrate to JDK 11 and Java 11 syntax [part 1] #698

Merged
merged 27 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f9553e6
Bump the version.
armiol Dec 8, 2021
140914e
Update to the latest `config`.
armiol Dec 8, 2021
384a1e1
Switch to Java 11 in the build script.
armiol Dec 8, 2021
f9dee8c
Update the report files.
armiol Dec 8, 2021
164174f
Update the code style.
armiol Dec 8, 2021
0c82d23
Run Java 11 on GitHub Actions.
armiol Dec 8, 2021
9704a74
Switch to Java 11 in IDEA.
armiol Dec 8, 2021
4ce66e6
Set `VariableTypeCanBeExplicit` to `WARNING`.
armiol Dec 8, 2021
29a119c
Set `RedundantExplicitVariableType` to `WARNING`. Turn off `VariableT…
armiol Dec 8, 2021
830e95c
Use `var`s in `io.spine.base` package.
armiol Dec 8, 2021
092d43d
Use `var`s in `io.spine.code` package and its subpackages.
armiol Dec 8, 2021
5e9ed3e
Use `var`s in `io.spine.environment` package.
armiol Dec 9, 2021
34c1da5
Use `var`s in `io.spine.io` package.
armiol Dec 9, 2021
bb07ef8
Use `var`s in `io.spine.json` package.
armiol Dec 9, 2021
588d972
Use `var`s in `io.spine.logging` package.
armiol Dec 9, 2021
a95f514
Use `var`s in `io.spine.protobuf` package.
armiol Dec 9, 2021
50cbe41
Use `var`s in `io.spine.query` package.
armiol Dec 9, 2021
b6407a8
Use `var`s in `io.spine.reflect` package.
armiol Dec 9, 2021
ad68e4b
Use `var`s in `io.spine.security` package.
armiol Dec 9, 2021
66fce09
Use `var`s in `io.spine.string` package.
armiol Dec 9, 2021
ca292f0
Use `var`s in `io.spine.type` package.
armiol Dec 9, 2021
a0f7862
Use `var`s in `io.spine.util` package.
armiol Dec 9, 2021
cc7d595
Use `var`s in `io.spine.validate` package and its subpackages.
armiol Dec 9, 2021
c966e3a
Use `var`s in `io.spine.value` package.
armiol Dec 9, 2021
e566365
Extract the "constant" part from the stream processing.
armiol Dec 10, 2021
57c384b
Reformat the ternary expression.
armiol Dec 10, 2021
b0b3737
Simplify the implementation.
armiol Dec 10, 2021
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
2 changes: 1 addition & 1 deletion .github/workflows/build-on-ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

- uses: actions/setup-java@v2
with:
java-version: 8
java-version: 11
distribution: zulu
cache: gradle

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-on-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

- uses: actions/setup-java@v2
with:
java-version: 8
java-version: 11
distribution: zulu
cache: gradle

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/increment-guard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

- uses: actions/setup-java@v2
with:
java-version: 8
java-version: 11
distribution: zulu
cache: gradle

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

- uses: actions/setup-java@v2
with:
java-version: 8
java-version: 11
distribution: zulu
cache: gradle

Expand Down
2 changes: 1 addition & 1 deletion .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 9 additions & 11 deletions base/src/main/java/io/spine/base/Errors.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private Errors() {
* Creates new instance of {@link Error} by the passed {@code Throwable}.
*/
public static Error fromThrowable(Throwable throwable) {
Error.Builder result = toErrorBuilder(throwable);
var result = toErrorBuilder(throwable);
return result.build();
}

Expand All @@ -58,7 +58,7 @@ public static Error fromThrowable(Throwable throwable) {
* @return new instance of {@link Error}
*/
public static Error causeOf(Throwable throwable) {
Error.Builder error = toBuilderCauseOf(throwable);
var error = toBuilderCauseOf(throwable);
return error.build();
}

Expand All @@ -76,7 +76,7 @@ public static Error causeOf(Throwable throwable) {
* @see #causeOf(Throwable) as the recommended overload
*/
public static Error causeOf(Throwable throwable, int errorCode) {
Error.Builder error = toBuilderCauseOf(throwable).setCode(errorCode);
var error = toBuilderCauseOf(throwable).setCode(errorCode);
return error.build();
}

Expand All @@ -101,20 +101,18 @@ private static Error.Builder toBuilderCauseOf(Throwable throwable) {
* the {@code Throwable} to convert
* @return new builder of {@link Error}
*/
@SuppressWarnings("CheckReturnValue") // calling builder
@SuppressWarnings({"CheckReturnValue", "ResultOfMethodCallIgnored"}) // Calling builder.
private static Error.Builder toErrorBuilder(Throwable throwable) {
checkNotNull(throwable);
String type = throwable.getClass()
.getCanonicalName();
String message = nullToEmpty(throwable.getMessage());
String stacktrace = getStackTraceAsString(throwable);
Error.Builder result = Error
.newBuilder()
var type = throwable.getClass().getCanonicalName();
var message = nullToEmpty(throwable.getMessage());
var stacktrace = getStackTraceAsString(throwable);
var result = Error.newBuilder()
.setType(type)
.setMessage(message)
.setStacktrace(stacktrace);
if (throwable instanceof ValidationException) {
ValidationException validationException = (ValidationException) throwable;
var validationException = (ValidationException) throwable;
result.setValidationError(validationException.asValidationError());
}
return result;
Expand Down
81 changes: 38 additions & 43 deletions base/src/main/java/io/spine/base/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.Immutable;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Message;
Expand All @@ -42,7 +41,6 @@
import io.spine.value.ValueHolder;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -100,7 +98,7 @@ public static Field withPath(FieldPath path) {
*/
public static Field parse(String path) {
checkNotNull(path);
FieldPath fp = doParse(path);
var fp = doParse(path);
return create(fp);
}

Expand All @@ -112,8 +110,8 @@ public static Field parse(String path) {
*/
public static Field named(String fieldName) {
checkName(fieldName);
FieldPath path = create(ImmutableList.of(fieldName));
Field result = create(path);
var path = create(ImmutableList.of(fieldName));
var result = create(path);
return result;
}

Expand All @@ -129,8 +127,8 @@ public static Field named(String fieldName) {
* if there is no field with the passed number in this message type
*/
public static Field withNumberIn(int number, Descriptor message) {
String name = nameOf(number, message);
Field result = named(name);
var name = nameOf(number, message);
var result = named(name);
return result;
}

Expand All @@ -149,9 +147,7 @@ public static Field withNumberIn(int number, Descriptor message) {
public static String nameOf(int fieldNumber, Descriptor message) {
checkNotNull(message);
checkArgument(fieldNumber > 0);
String result = message
.getFields()
.stream()
var result = message.getFields().stream()
.filter(f -> f.getNumber() == fieldNumber)
.findFirst()
.map(FieldDescriptor::getName)
Expand All @@ -168,9 +164,9 @@ public static String nameOf(int fieldNumber, Descriptor message) {
*/
public Field nested(String fieldName) {
checkName(fieldName);
FieldPath newPath = path().toBuilder()
.addFieldName(fieldName)
.build();
var newPath = path().toBuilder()
.addFieldName(fieldName)
.build();
return create(newPath);
}

Expand All @@ -181,9 +177,9 @@ public Field nested(Field other) {
checkNotNull(other);
List<String> fieldNames = other.path()
.getFieldNameList();
FieldPath newPath = path().toBuilder()
.addAllFieldName(fieldNames)
.build();
var newPath = path().toBuilder()
.addAllFieldName(fieldNames)
.build();
return create(newPath);
}

Expand All @@ -201,7 +197,7 @@ public FieldPath path() {
* in this type of messages
*/
public Optional<Object> findValue(Message holder) {
Object value = doGetValue(path(), holder, false);
var value = doGetValue(path(), holder, false);
return Optional.ofNullable(value);
}

Expand All @@ -211,7 +207,7 @@ public Optional<Object> findValue(Message holder) {
* @throws IllegalStateException if the type of the passed message does not declare this field
*/
public Object valueIn(Message holder) {
Object result = findValue(holder).orElseThrow(
var result = findValue(holder).orElseThrow(
() -> newIllegalStateException("Unable to get the field `%s` from `%s`.",
this, shortDebugString(holder))
);
Expand All @@ -222,8 +218,8 @@ this, shortDebugString(holder))
* Checks if the field is present (as top-level or nested) in the given message type.
*/
public boolean presentIn(Descriptor message) {
Optional<FieldDescriptor> descriptor = findDescriptor(message);
boolean result = descriptor.isPresent();
var descriptor = findDescriptor(message);
var result = descriptor.isPresent();
return result;
}

Expand All @@ -241,21 +237,21 @@ public Optional<FieldDescriptor> findDescriptor(Descriptor message) {
* Obtains the type of the referenced field in the passed message class.
*/
public Optional<Class<?>> findType(Class<? extends Message> holderType) {
Descriptor message = TypeName.of(holderType).messageDescriptor();
var message = TypeName.of(holderType).messageDescriptor();
@Nullable FieldDescriptor field = fieldIn(path(), message);
if (field == null) {
return Optional.empty();
}
Class<?> result = classOf(field);
var result = classOf(field);
return Optional.of(result);
}

/**
* Checks if the field is a nested field.
*/
public boolean isNested() {
int pathComponents = path().getFieldNameCount();
boolean result = pathComponents > 1;
var pathComponents = path().getFieldNameCount();
var result = pathComponents > 1;
return result;
}

Expand All @@ -273,8 +269,7 @@ public String toString() {
/** Creates a new path containing the passed elements. */
private static FieldPath create(List<String> elements) {
elements.forEach(Field::checkName);
FieldPath result = FieldPath
.newBuilder()
var result = FieldPath.newBuilder()
.addAllFieldName(elements)
.build();
return result;
Expand All @@ -284,7 +279,7 @@ private static FieldPath create(List<String> elements) {
@VisibleForTesting
static FieldPath doParse(String fieldPath) {
checkArgument(!fieldPath.isEmpty(), "A field path must not be empty.");
List<String> pathElements = dotSplitter.splitToList(fieldPath);
var pathElements = dotSplitter.splitToList(fieldPath);
return create(pathElements);
}

Expand All @@ -306,11 +301,11 @@ static FieldPath doParse(String fieldPath) {
* if the call is {@code strict} and the value not found
*/
private static @Nullable Object doGetValue(FieldPath path, Message holder, boolean strict) {
Message message = holder;
var message = holder;
Object currentValue = message;
for (Iterator<String> iterator = path.getFieldNameList().iterator(); iterator.hasNext(); ) {
String fieldName = iterator.next();
Descriptor type = message.getDescriptorForType();
for (var iterator = path.getFieldNameList().iterator(); iterator.hasNext(); ) {
var fieldName = iterator.next();
var type = message.getDescriptorForType();
@Nullable FieldDescriptor field = type.findFieldByName(fieldName);
if (field == null) {
if (strict) {
Expand Down Expand Up @@ -360,11 +355,11 @@ private static Object convertIfEnum(Object currentValue) {
if (!(currentValue instanceof EnumValueDescriptor)) {
return currentValue;
}
EnumValueDescriptor value = (EnumValueDescriptor) currentValue;
EnumDescriptor enumType = value.getType();
TypeName typeName = TypeName.of(enumType.getFullName());
var value = (EnumValueDescriptor) currentValue;
var enumType = value.getType();
var typeName = TypeName.of(enumType.getFullName());
Class<? extends Enum> cls = typeName.toEnumClass();
Enum enumValue = Enum.valueOf(cls, value.getName());
var enumValue = Enum.valueOf(cls, value.getName());
return enumValue;
}

Expand All @@ -374,10 +369,10 @@ private static Object convertIfEnum(Object currentValue) {
* @return the descriptor or {@code null} if the message type does not declare this field
*/
private static @Nullable FieldDescriptor fieldIn(FieldPath path, Descriptor message) {
Descriptor current = message;
var current = message;
FieldDescriptor field = null;
for (Iterator<String> iterator = path.getFieldNameList().iterator(); iterator.hasNext(); ) {
String fieldName = iterator.next();
for (var iterator = path.getFieldNameList().iterator(); iterator.hasNext(); ) {
var fieldName = iterator.next();
field = current.findFieldByName(fieldName);
if (field == null) {
return null;
Expand All @@ -402,15 +397,15 @@ private static void checkName(String fieldName) {

/** Obtains the type of the values stored in the field. */
static Class<?> classOf(FieldDescriptor field) {
FieldDescriptor.Type type = field.getType();
var type = field.getType();
if (type == MESSAGE) {
Class<?> cls = TypeUrl.from(field.getMessageType()).toJavaClass();
var cls = TypeUrl.from(field.getMessageType()).toJavaClass();
return cls;
} else if (type == ENUM) {
Class<?> cls = TypeUrl.from(field.getEnumType()).toJavaClass();
var cls = TypeUrl.from(field.getEnumType()).toJavaClass();
return cls;
} else {
Class<?> result = ScalarType.javaType(field.toProto().getType());
var result = ScalarType.javaType(field.toProto().getType());
return result;
}
}
Expand All @@ -424,7 +419,7 @@ private static void checkNotEmpty(FieldPath path) throws IllegalArgumentExceptio
* Joins the passed path elements into the string representation of the path.
*/
private static String join(Iterable<String> elements) {
String result = joiner.join(elements);
var result = joiner.join(elements);
return result;
}
}
12 changes: 6 additions & 6 deletions base/src/main/java/io/spine/base/IdType.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ Message fromMessage(Message message) {

@Override
<I> I defaultValue(Class<I> idClass) {
Class<? extends Message> msgClass = (Class<? extends Message>) idClass;
Message result = defaultInstance(msgClass);
var msgClass = (Class<? extends Message>) idClass;
var result = defaultInstance(msgClass);
return (I) result;
}

Expand All @@ -205,7 +205,7 @@ boolean matchField(FieldDescriptor field) {
* if the passed value is not of supported type
*/
static <I> IdType of(I id) {
for (IdType type : values()) {
for (var type : values()) {
if (type.matchValue(id)) {
return type;
}
Expand Down Expand Up @@ -251,7 +251,7 @@ static <I> IdType of(I id) {
* Converts the passed ID object into the Protobuf implementation instance.
*/
<I> Message toMessage(I id) {
Message message = TypeConverter.toMessage(id);
var message = TypeConverter.toMessage(id);
return message;
}

Expand All @@ -260,8 +260,8 @@ <I> Message toMessage(I id) {
* then packs it into {@code Any}.
*/
<I> Any pack(I id) {
Message msg = toMessage(id);
Any result = AnyPacker.pack(msg);
var msg = toMessage(id);
var result = AnyPacker.pack(msg);
return result;
}
}
Loading