Skip to content

Commit

Permalink
Merge pull request #321 from SpineEventEngine/allow-field-instantiation
Browse files Browse the repository at this point in the history
Allow instantiation of `FieldDeclaration`
  • Loading branch information
yevhenii-nadtochii authored Feb 17, 2025
2 parents 24310af + 1981ba9 commit 8d38c9b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 176 deletions.
44 changes: 22 additions & 22 deletions dependencies.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


# Dependencies of `io.spine.protodata:protodata-api:0.92.0`
# Dependencies of `io.spine.protodata:protodata-api:0.92.1`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -1041,12 +1041,12 @@

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Sun Feb 16 18:33:54 WET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Mon Feb 17 12:57:57 CET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.protodata:protodata-api-tests:0.92.0`
# Dependencies of `io.spine.protodata:protodata-api-tests:0.92.1`

## Runtime
1. **Group** : org.jetbrains. **Name** : annotations. **Version** : 13.0.
Expand Down Expand Up @@ -1894,12 +1894,12 @@ This report was generated on **Sun Feb 16 18:33:54 WET 2025** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Sun Feb 16 18:33:54 WET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Mon Feb 17 12:57:57 CET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.protodata:protodata-backend:0.92.0`
# Dependencies of `io.spine.protodata:protodata-backend:0.92.1`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -2928,12 +2928,12 @@ This report was generated on **Sun Feb 16 18:33:54 WET 2025** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Sun Feb 16 18:33:55 WET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Mon Feb 17 12:57:58 CET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.protodata:protodata-cli:0.92.0`
# Dependencies of `io.spine.protodata:protodata-cli:0.92.1`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -4045,12 +4045,12 @@ This report was generated on **Sun Feb 16 18:33:55 WET 2025** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Sun Feb 16 18:33:55 WET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Mon Feb 17 12:57:58 CET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.protodata:protodata-gradle-api:0.92.0`
# Dependencies of `io.spine.protodata:protodata-gradle-api:0.92.1`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -5061,12 +5061,12 @@ This report was generated on **Sun Feb 16 18:33:55 WET 2025** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Sun Feb 16 18:33:56 WET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Mon Feb 17 12:57:58 CET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.protodata:protodata-gradle-plugin:0.92.0`
# Dependencies of `io.spine.protodata:protodata-gradle-plugin:0.92.1`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -6249,12 +6249,12 @@ This report was generated on **Sun Feb 16 18:33:56 WET 2025** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Sun Feb 16 18:33:56 WET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Mon Feb 17 12:57:58 CET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.protodata:protodata-java:0.92.0`
# Dependencies of `io.spine.protodata:protodata-java:0.92.1`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -7283,12 +7283,12 @@ This report was generated on **Sun Feb 16 18:33:56 WET 2025** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Sun Feb 16 18:33:56 WET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Mon Feb 17 12:57:59 CET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.protodata:protodata-params:0.92.0`
# Dependencies of `io.spine.protodata:protodata-params:0.92.1`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -8324,12 +8324,12 @@ This report was generated on **Sun Feb 16 18:33:56 WET 2025** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Sun Feb 16 18:33:56 WET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Mon Feb 17 12:57:59 CET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.protodata:protodata-protoc:0.92.0`
# Dependencies of `io.spine.protodata:protodata-protoc:0.92.1`

## Runtime
1. **Group** : com.google.code.findbugs. **Name** : jsr305. **Version** : 3.0.2.
Expand Down Expand Up @@ -9166,12 +9166,12 @@ This report was generated on **Sun Feb 16 18:33:56 WET 2025** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Sun Feb 16 18:33:57 WET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Mon Feb 17 12:57:59 CET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.protodata:protodata-test-env:0.92.0`
# Dependencies of `io.spine.protodata:protodata-test-env:0.92.1`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -10215,12 +10215,12 @@ This report was generated on **Sun Feb 16 18:33:57 WET 2025** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Sun Feb 16 18:33:57 WET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Mon Feb 17 12:57:59 CET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.protodata:protodata-testlib:0.92.0`
# Dependencies of `io.spine.protodata:protodata-testlib:0.92.1`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -11351,4 +11351,4 @@ This report was generated on **Sun Feb 16 18:33:57 WET 2025** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Sun Feb 16 18:33:57 WET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Mon Feb 17 12:58:00 CET 2025** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
114 changes: 39 additions & 75 deletions java/src/main/kotlin/io/spine/protodata/java/Fields.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,93 +33,57 @@ import io.spine.tools.psi.java.Environment.elementFactory
/**
* A declaration of a Java field.
*
* The declared field may OR may not be initialized,
* depending on a specific implementation.
* The declared field may OR may not be initialized.
*/
public sealed class FieldDeclaration<T>(
public class FieldDeclaration<T>(
public val name: String,
code: String
) : MemberDeclaration(code) {

/**
* Returns an expression that reads the value of this field.
* Declares an initialized Java field.
*
* An example usage:
*
* ```
* val height = FieldDeclaration("public final", PrimitiveName.INT, "height", Expression<Int>("180"))
* println(height) // `public final int height = 180;`
* ```
*
* @param modifiers The field modifiers separated with a space.
* @param type The field type.
* @param name The field name.
* @param value The field value.
*/
public fun read(useThis: Boolean = false): ReadField<T> = ReadField(name, useThis)
}

/**
* Declares and initializes a Java field.
*
* An example usage:
*
* ```
* val height = InitField("public final", PrimitiveName.INT, "height", Expression<Int>("180"))
* println(height) // `public final int height = 180;`
* ```
*
* @param modifiers The field modifiers separated with a space.
* @param type The field type.
* @param name The field name.
* @param value The field value.
*/
public class InitField<T>(
public val modifiers: String,
public val type: JavaTypeName,
name: String,
public val value: Expression<T>
) : FieldDeclaration<T>(name, "$modifiers $type $name = $value;")

/**
* Declares a Java field with the given parameters.
*
* Please note, the declared field is NOT initialized.
*
* An example usage:
*
* ```
* val height = DeclField("public final", PrimitiveName.INT, "height")
* println(height) // `public final int height;`
* ```
*
* @param modifiers The field modifiers separated with a space.
* @param type The field type.
* @param name The field name.
*/
public class DeclField<T>(
public val modifiers: String,
public val type: JavaTypeName,
name: String
) : FieldDeclaration<T>(name, "$modifiers $type $name;") {
public constructor(
modifiers: String,
type: JavaTypeName,
name: String,
value: Expression<T>
) : this(name, "$modifiers $type $name = $value;")

/**
* Returns an expression that sets the value for this field.
* Declares a non-initialized Java field.
*
* An example usage:
*
* ```
* val height = FieldDeclaration("public final", PrimitiveName.INT, "height")
* println(height) // `public final int height;`
* ```
*
* @param modifiers The field modifiers separated with a space.
* @param type The field type.
* @param name The field name.
*/
public fun set(value: Expression<T>, useThis: Boolean = false): SetField<T> =
SetField(name, value, useThis)
}

/**
* Assigns a [value] to a field with the given [name].
*
* An example usage:
*
* ```
* val setHeight = SetField("height", Expression<Int>("180"), useThis = true)
* println(setHeight) // `this.height = 180;`
* ```
*
* @param name The field name.
* @param value The value to assign.
* @param explicitThis Tells whether to use the explicit `this` keyword.
*/
public class SetField<T>(
public val name: String,
public val value: Expression<T>,
public val explicitThis: Boolean = false
) : Statement("${field(name, explicitThis)} = $value;") {
public constructor(
modifiers: String,
type: JavaTypeName,
name: String
) : this(name, "$modifiers $type $name;")

/**
* Returns an expression that reads the field value.
* Returns an expression that reads the value of this field.
*/
public fun read(useThis: Boolean = false): ReadField<T> = ReadField(name, useThis)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,41 @@ import assertCode
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test

@DisplayName("`DeclField` should")
internal class DeclFieldSpec {
@DisplayName("`FieldDeclaration` should")
internal class FieldDeclarationSpec {

private val surname = DeclField<String>(
modifiers = "private final",
type = ClassName(String::class),
name = "surname"
)
@Test
fun `declare an arbitrary Java field`() {
val code = "private static String surname = \"\";"
val field = FieldDeclaration<String>("surname", code)
assertCode(field, code)
}

@Test
fun `declare a Java field`() {
assertCode(surname, "private final java.lang.String surname;")
fun `declare a non-initialized Java field`() {
val field = FieldDeclaration<String>(
modifiers = "private final",
type = ClassName(String::class),
name = "surname"
)
assertCode(field, "private final java.lang.String surname;")
}

@Test
fun `set a value to the declared field`() {
val anderson = "Anderson"
val expression = StringLiteral(anderson)
assertCode(surname.set(expression), "surname = \"$anderson\";")
assertCode(surname.set(expression, useThis = true), "this.surname = \"$anderson\";")
fun `declare an initialized Java field`() {
val field = FieldDeclaration(
modifiers = "private final",
type = ClassName(String::class),
name = "surname",
value = StringLiteral("Anderson")
)
assertCode(field, "private final java.lang.String surname = \"Anderson\";")
}

@Test
fun `provide a read access to the declared field`() {
assertCode(surname.read(), "surname")
assertCode(surname.read(useThis = true), "this.surname")
fun `provide a read access to the field`() {
val field = FieldDeclaration<String>("surname", "private static String surname = \"\";")
assertCode(field.read(), "surname")
assertCode(field.read(useThis = true), "this.surname")
}
}
Loading

0 comments on commit 8d38c9b

Please sign in to comment.