ronantakizawa/codereview-bench huggingface dataset:
Repository: apple/pkl
File: pkl-core/pkl-core.gradle.kts (Kotlin)

Code before:
  classpath = tasks.test.get().classpath

  useJUnitPlatform {
    includeEngines("LinuxAarch64LanguageSnippetTestsEngine")
  }
}

val testAlpineExecutableAmd64 by tasks.registering(Test::class) {
  enabled = buildInfo.os.isLinux && buildInfo.arch == "amd64" && buildInfo.hasMuslToolchain
  dependsOn(":pkl-cli:alpineExecutableAmd64")

  inputs.dir("src/test/files/LanguageSnippetTests/input")
  inputs.dir("src/test/files/LanguageSnippetTests/input-helper")
  inputs.dir("src/test/files/LanguageSnippetTests/output")

  testClassesDirs = files(tasks.test.get().testClassesDirs)
  classpath = tasks.test.get().classpath

  useJUnitPlatform {
    includeEngines("AlpineLanguageSnippetTestsEngine")
  }
}

tasks.testNative {
  dependsOn(testLinuxExecutableAmd64)
  dependsOn(testLinuxExecutableAarch64)
  dependsOn(testMacExecutableAmd64)
  dependsOn(testMacExecutableAarch64)
  dependsOn(testAlpineExecutableAmd64)
}

tasks.clean {
  delete("generated/")
  delete(layout.buildDirectory.dir("test-packages"))
}

spotless {
  antlr4 {
    licenseHeaderFile(rootProject.file("buildSrc/src/main/resources/license-header.star-block.txt"))
    target(files("src/main/antlr/PklParser.g4", "src/main/antlr/PklLexer.g4"))
  }
}


Review comment (suggestion):
```suggestion
    buildInfo.os.isMacOsX -> {
      dependsOn(testMacExecutableAmd64)
      if (buildInfo.arch == "aarch64") {    
        dependsOn(testMacExecutableAarch64)
      }
    }
```

Code after:
  }
}

val testAlpineExecutableAmd64 by tasks.registering(Test::class) {
  dependsOn(":pkl-cli:alpineExecutableAmd64")

  inputs.dir("src/test/files/LanguageSnippetTests/input")
  inputs.dir("src/test/files/LanguageSnippetTests/input-helper")
  inputs.dir("src/test/files/LanguageSnippetTests/output")

  testClassesDirs = files(tasks.test.get().testClassesDirs)
  classpath = tasks.test.get().classpath

  useJUnitPlatform {
    includeEngines("AlpineLanguageSnippetTestsEngine")
  }
}

tasks.testNative {
  when {
    buildInfo.os.isMacOsX -> {
      dependsOn(testMacExecutableAmd64)
      if (buildInfo.arch == "aarch64") {
        dependsOn(testMacExecutableAarch64)
      }
    }
    buildInfo.os.isLinux && buildInfo.arch == "aarch64" -> {
      dependsOn(testLinuxExecutableAarch64)
    }
    buildInfo.os.isLinux && buildInfo.arch == "amd64" -> {
      dependsOn(testLinuxExecutableAmd64)
      if (buildInfo.hasMuslToolchain) {
        dependsOn(testAlpineExecutableAmd64)
      }
    }
  }
}

tasks.clean {
  delete("generated/")
  delete(layout.buildDirectory.dir("test-packages"))
}

spotless {
  antlr4 {
    licenseHeaderFile(rootProject.file("buildSrc/src/main/resources/license-header.star-block.txt"))
    target(files("src/main/antlr/PklParser.g4", "src/main/antlr/PklLexer.g4"))
  }
}
Repository: apple/pkl
File: pkl-gradle/pkl-gradle.gradle.kts (Kotlin)

Code before:
      }
    }
  }
}

gradlePlugin {
  plugins {
    create("pkl") {
      id = "org.pkl-lang"
      implementationClass = "org.pkl.gradle.PklPlugin"
      displayName = "pkl-gradle"
      description = "Gradle plugin for interacting with Pkl"
    }
  }
}

gradlePluginTests {
  // keep in sync with `PklPlugin.MIN_GRADLE_VERSION`
  minGradleVersion = GradleVersion.version("8.1")
  maxGradleVersion = GradleVersion.version("8.99")
  skippedGradleVersions = listOf()
}

signing {
  publishing.publications.withType(MavenPublication::class.java).configureEach {
    if (name != "library") {
      sign(this)
    }
  }
}


Review comment (suggestion):
```suggestion
  // Need additional disablement here because of how jansi is packaged in plugin
```

Code after:
      }
    }
  }
}

gradlePlugin {
  plugins {
    create("pkl") {
      id = "org.pkl-lang"
      implementationClass = "org.pkl.gradle.PklPlugin"
      displayName = "pkl-gradle"
      description = "Gradle plugin for interacting with Pkl"
    }
  }
}

gradlePluginTests {
  // keep in sync with `PklPlugin.MIN_GRADLE_VERSION`
  minGradleVersion = GradleVersion.version("8.1")
  maxGradleVersion = GradleVersion.version("8.99")
  skippedGradleVersions = listOf()
}

tasks.withType<Test>().configureEach {
  // Disable colour output in tests
  // Need additional disablement here because of how Jansi is packaged in the plugin
  systemProperty("org.pkl.thirdparty.jansiAnsi.disable", "true")
}

signing {
  publishing.publications.withType(MavenPublication::class.java).configureEach {
    if (name != "library") {
      sign(this)
    }
  }
}
Repository: apple/pkl
File: pkl-core/src/main/java/org/pkl/core/module/ModuleKeys.java (Java)

Code before:
      return FileResolver.listElements(baseUri);
    }

    @Override
    public ResolvedModuleKey resolve(SecurityManager securityManager)
        throws IOException, SecurityManagerException {
      securityManager.checkResolveModule(uri);
      // Disallow paths that contain `\\` characters if on Windows
      // (require `/` as the path separator on all OSes)
      var uriPath = uri.getPath();
      if (java.io.File.separatorChar == '\\' && uriPath != null && uriPath.contains("\\")) {
        throw new FileNotFoundException();
      }
      var realPath = Path.of(uri).toRealPath();
      var resolvedUri = realPath.toUri();
      securityManager.checkResolveModule(resolvedUri);
      return ResolvedModuleKeys.file(this, resolvedUri, realPath);
    }

    @Override
    public boolean isGlobbable() {
      return true;
    }

    @Override
    public boolean isLocal() {
      return true;
    }

    @Override
    public boolean hasHierarchicalUris() {
      return true;
    }
  }

  private static final class ModulePath implements ModuleKey {
    final URI uri;
    final ModulePathResolver resolver;

    ModulePath(URI uri, ModulePathResolver resolver) {
      if (uri.getPath() == null) {
        throw new IllegalArgumentException(
            ErrorMessages.create("invalidModuleUriMissingSlash", uri, "modulepath"));
      }

      this.uri = uri;
      this.resolver = resolver;
    }

    @Override
    public URI getUri() {

Review comment (bug):
```suggestion
        // impossible to get here; we started from a valid URI to begin with
        throw PklBugException.unreachableCode();
```

Code after:

    @Override
    public ResolvedModuleKey resolve(SecurityManager securityManager)
        throws IOException, SecurityManagerException {
      securityManager.checkResolveModule(uri);
      // Disallow paths that contain `\\` characters if on Windows
      // (require `/` as the path separator on all OSes)
      var uriPath = uri.getPath();
      if (java.io.File.separatorChar == '\\' && uriPath != null && uriPath.contains("\\")) {
        throw new FileNotFoundException();
      }
      var realPath = IoUtils.pathOf(uri).toRealPath();
      var resolvedUri = realPath.toUri();
      securityManager.checkResolveModule(resolvedUri);
      return ResolvedModuleKeys.file(this, resolvedUri, realPath);
    }

    @Override
    public boolean isGlobbable() {
      return true;
    }

    @Override
    public boolean isLocal() {
      return true;
    }

    @Override
    public boolean hasHierarchicalUris() {
      return true;
    }
  }

  private static final class ModulePath implements ModuleKey {
    final URI uri;
    final ModulePathResolver resolver;

    ModulePath(URI uri, ModulePathResolver resolver) {
      if (uri.getPath() == null) {
        throw new IllegalArgumentException(
            ErrorMessages.create("invalidModuleUriMissingSlash", uri, "modulepath"));
      }

      this.uri = uri;
      this.resolver = resolver;
    }

    @Override
    public URI getUri() {
      return uri;
    }
Repository: apple/pkl
File: pkl-codegen-kotlin/src/test/kotlin/org/pkl/codegen/kotlin/KotlinCodeGeneratorTest.kt (Kotlin)

Code before:
      """
      package my

      import kotlin.String

      data class Mod(
        val pigeon: Person,
        val parrot: Person
      ) {
        data class Person(
          val name: String
        )
      }
    """,
      kotlinCode
    )

    assertCompilesSuccessfully(kotlinCode)
  }

  @Test
  fun `simple module name`() {
    val kotlinCode =
      generateKotlinCode(
        """
      module mod

      pigeon: Person
      parrot: Person

      class Person {
        name: String
      }
    """
      )

    assertEqualTo(
      """
      import kotlin.String

      data class Mod(
        val pigeon: Person,
        val parrot: Person
      ) {
        data class Person(
          val name: String
        )
      }
    """,
      kotlinCode
    )

Review comment (refactor):
```suggestion
  fun `abstract class that extends open class`() {
```

Code after:
      |  class Baz(
      |    one: Long,
      |    two: String,
      |    val three: Duration
      |  ) : Bar(one, two) {
      |    fun copy(
      |      one: Long = this.one,
      |      two: String = this.two,
      |      three: Duration = this.three
      |    ): Baz = Baz(one, two, three)
    """
        .trimMargin(),
      kotlinCode
    )

    assertContains(
      """
      |  class Qux(
      |    one: Long,
      |    two: String
      |  ) : Bar(one, two) {
      |    fun copy(one: Long = this.one, two: String = this.two): Qux = Qux(one, two)
      """
        .trimMargin(),
      kotlinCode
    )

    assertCompilesSuccessfully(kotlinCode)
  }

  // https://github.com/apple/pkl/issues/569
  @Test
  fun `abstract class that extends open class`() {
    val kotlinCode =
      generateKotlinCode(
        """
      module my.mod

      open class Foo { one: Int }
      abstract class Bar extends Foo { two: String }
      class Baz extends Bar { three: Duration }
      class Qux extends Bar {}
      """
      )

    assertContains(
      """
      |  open class Foo(
      |    open val one: Long
      |  ) {
      |    open fun copy(one: Long = this.one): Foo = Foo(one)
Repository: apple/pkl
File: pkl-codegen-kotlin/src/test/kotlin/org/pkl/codegen/kotlin/KotlinCodeGeneratorTest.kt (Kotlin)

Code before:

      class Persons {
        hidden pigeon2: String
        parrot2: String
      }
      """
      )

    assertThat(kotlinCode)
      .doesNotContain("pigeon1: String")
      .contains("parrot1: String")
      .doesNotContain("pigeon2: String")
      .contains("parrot2: String")
  }

  @Test
  fun kdoc() {
    val kotlinCode =
      generateKotlinCode(
        """
      /// module comment.
      /// *emphasized* `code`.
      module my.mod

      /// module property comment.
      /// *emphasized* `code`.
      pigeon: Person

      /// class comment.
      /// *emphasized* `code`.
      open class Product {
        /// class property comment.
        /// *emphasized* `code`.
        price: String
      }

      /// class comment.
      /// *emphasized* `code`.
      class Person {
        /// class property comment.
        /// *emphasized* `code`.
        name: String
      }

      /// type alias comment.
      /// *emphasized* `code`.
      typealias Email = String(contains("@"))
      """,
        generateKdoc = true
      )


Review comment (refactor):
```suggestion
  fun `abstract class that extends open class without adding properties`() {
```

Code after:
      kotlinCode
    )

    assertContains(
      """
      |  class Baz(
      |    one: Long,
      |    two: String,
      |    val three: Duration
      |  ) : Bar(one, two) {
      |    fun copy(
      |      one: Long = this.one,
      |      two: String = this.two,
      |      three: Duration = this.three
      |    ): Baz = Baz(one, two, three)
      |
      |    override fun copy(one: Long): Baz = Baz(one, two, three)
      """
        .trimMargin(),
      kotlinCode
    )

    assertContains(
      """
      |  class Qux(
      |    one: Long,
      |    two: String
      |  ) : Bar(one, two) {
      |    fun copy(one: Long = this.one, two: String = this.two): Qux = Qux(one, two)
      |
      |    override fun copy(one: Long): Qux = Qux(one, two)
      """
        .trimMargin(),
      kotlinCode
    )

    assertCompilesSuccessfully(kotlinCode)
  }

  // https://github.com/apple/pkl/issues/569
  @Test
  fun `abstract class that extends open class without adding properties`() {
    val kotlinCode =
      generateKotlinCode(
        """
      module my.mod

      open class Foo { one: Int }
      abstract class Bar extends Foo {}
      class Baz extends Bar { two: Duration }
      class Qux extends Bar {}
Repository: apple/pkl
File: pkl-executor/src/main/java/org/pkl/executor/ExecutorException.java (Java)

Code before:
 * limitations under the License.
 */
package org.pkl.executor;

/**
 * Indicates an {@link Executor} error. {@link #getMessage()} returns a user-facing error message.
 */
public final class ExecutorException extends RuntimeException {
  String pklVersion = "unresolved";

  public ExecutorException(String message) {
    super(message);
  }

  public ExecutorException(String message, Throwable cause) {
    super(message, cause);
  }

  public ExecutorException(String message, String version, Throwable cause) {
    super(message, cause);
    pklVersion = version;
  }

  public String getPklVersion() {
    return pklVersion;
  }
}


Review comment (suggestion):
Some nits:

* We can probably just return `null` if there is no pkl version to relate.
* Add a doc comment to `getPklVersion()`
* Would be nice to add the Pkl version to `getMessage()`

```suggestion
  String pklVersion = null;

  public ExecutorException(String message) {
    super(message);
  }

  public ExecutorException(String message, Throwable cause) {
    super(message, cause);
  }

  public ExecutorException(String message, String version, Throwable cause) {
    super(message, cause);
    pklVersion = version;
  }

  /**
   * The selected Pkl version used to evaluate the module.
   *
   * Returns {@code null} if this exception does not originate from an underlying Pkl evaluator.
   */
  public String getPklVersion() {
    return pklVersion;
  }

  @Override
  public String getMessage() {
    var message = super.getMessage();
    if (pklVersion == null) {
      return message;
    }
    return message + "\nPkl version: " + pklVersion;
  }
```

Code after:
 * limitations under the License.
 */
package org.pkl.executor;

import java.util.Objects;

/**
 * Indicates an {@link Executor} error. {@link #getMessage()} returns a user-facing error message.
 */
public final class ExecutorException extends RuntimeException {
  private final String pklVersion;

  public ExecutorException(String message) {
    super(message);
    pklVersion = null;
  }

  public ExecutorException(String message, Throwable cause) {
    super(message, cause);
    pklVersion = null;
  }

  public ExecutorException(String message, Throwable cause, String version) {
    super(message, cause);
    pklVersion = Objects.requireNonNull(version);
  }

  /**
   * The selected Pkl version used to evaluate the module.
   *
   * <p>Returns {@code null} if this exception does not originate from an underlying Pkl evaluator.
   */
  public String getPklVersion() {
    return pklVersion;
  }

  @Override
  public String getMessage() {
    var message = super.getMessage();
    if (pklVersion == null) {
      return message;
    }
    return message + "\nPkl version: " + pklVersion;
  }
}
Repository: apple/pkl
File: pkl-codegen-java/src/test/kotlin/org/pkl/codegen/java/JavaCodeGeneratorTest.kt (Kotlin)

Code before:
      .contains(
        """
        |  public static class None extends Foo {
        |    public None(@Named("one") long one) {
        |      super(one);
        |    }
      """
          .trimMargin()
      )
      .contains(
        """
        |  public static class Bar extends None {
        |    protected final String two;
        |
        |    public Bar(@Named("one") long one, @Named("two") String two) {
        |      super(one);
        |      this.two = two;
        |    }
      """
          .trimMargin()
      )
      .isEqualToResourceFile("Inheritance.jva")
  }

  @Test
  fun `reserved words`() {
    val props = javaReservedWords.joinToString("\n") { "`$it`: Int" }

    val fooClass =
      generateJavaCode(
          """
      module my.mod

      class Foo {
        $props
      }
    """
            .trimIndent()
        )
        .compile()
        .getValue("my.Mod\$Foo")

    assertThat(fooClass.declaredFields).allSatisfy(Consumer { it.name.startsWith("_") })
  }

  @Test
  fun getters() {
    val javaCode =
      generateJavaCode(
        """
      module my.mod

Review comment (suggestion):
[nit] "empty" feels like a better word for this

```suggestion
  fun `empty classes`() {
```

Code after:
      .contains(
        """
        |  public static class None extends Foo {
        |    public None(@Named("one") long one) {
        |      super(one);
        |    }
      """
          .trimMargin()
      )
      .contains(
        """
        |  public static class Bar extends None {
        |    protected final String two;
        |
        |    public Bar(@Named("one") long one, @Named("two") String two) {
        |      super(one);
        |      this.two = two;
        |    }
      """
          .trimMargin()
      )
      .isEqualToResourceFile("Inheritance.jva")
  }

  @Test
  fun `stateless classes`() {
    val javaCode =
      generateJavaCode(
        """
      module my.mod

      class Foo
      abstract class Bar
      class Baz extends Bar
    """
          .trimIndent()
      )

    assertThat(javaCode)
      .contains(
        """
        |  public static final class Foo {
        |    public Foo() {
        |    }
      """
          .trimMargin()
      )
      .contains(
        """
        |  public abstract static class Bar {
        |    protected Bar() {
Repository: apple/pkl
File: pkl-core/src/main/java/org/pkl/core/resource/Resource.java (Java)

Code before:
/*
 * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.pkl.core.resource;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/** An external (file, HTTP, etc.) resource. */
public final class Resource {
  private final URI uri;

  private final byte[] bytes;

  /** Constructs a resource. */
  public Resource(URI uri, byte[] bytes) {
    this.uri = uri;
    this.bytes = bytes;
  }

  /** Returns the URI of this resource. */
  public URI getUri() {
    return uri;
  }

  public byte[] getBytes() {
    return bytes;
  }

  /** Returns the text content of this resource. */
  public String getText() {
    return new String(bytes, StandardCharsets.UTF_8);
  }

  /** Returns the content of this resource in Base64. */
  public String getBase64() {

Review comment (suggestion):
```suggestion
  /**
   * @deprecated As of 0.28.0, replaced by {@link #uri()}
   */
  @Deprecated(forRemoval = true)
```

Code after:
/*
 * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.pkl.core.resource;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/** An external (file, HTTP, etc.) resource. */
public record Resource(URI uri, byte[] bytes) {
  /**
   * @deprecated As of 0.28.0, replaced by {@link #uri()}
   */
  @Deprecated(forRemoval = true)
  public URI getUri() {
    return uri;
  }

  /**
   * @deprecated As of 0.28.0, replaced by {@link #bytes()}
   */
  @Deprecated(forRemoval = true)
  public byte[] getBytes() {
    return bytes;
  }

  /** Returns the text content of this resource. */
  public String getText() {
    return new String(bytes, StandardCharsets.UTF_8);
  }

  /** Returns the content of this resource in Base64. */
  public String getBase64() {
    return Base64.getEncoder().encodeToString(bytes);
  }
}
Repository: apple/pkl
File: pkl-core/src/main/java/org/pkl/core/resource/Resource.java (Java)

Code before:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.pkl.core.resource;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/** An external (file, HTTP, etc.) resource. */
public final class Resource {
  private final URI uri;

  private final byte[] bytes;

  /** Constructs a resource. */
  public Resource(URI uri, byte[] bytes) {
    this.uri = uri;
    this.bytes = bytes;
  }

  /** Returns the URI of this resource. */
  public URI getUri() {
    return uri;
  }

  public byte[] getBytes() {
    return bytes;
  }

  /** Returns the text content of this resource. */
  public String getText() {
    return new String(bytes, StandardCharsets.UTF_8);
  }

  /** Returns the content of this resource in Base64. */
  public String getBase64() {
    return Base64.getEncoder().encodeToString(bytes);
  }
}


Review comment (suggestion):
```suggestion
  /**
   * @deprecated As of 0.28.0, replaced by {@link #bytes()}
   */
  @Deprecated(forRemoval = true)
```

Code after:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.pkl.core.resource;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/** An external (file, HTTP, etc.) resource. */
public record Resource(URI uri, byte[] bytes) {
  /**
   * @deprecated As of 0.28.0, replaced by {@link #uri()}
   */
  @Deprecated(forRemoval = true)
  public URI getUri() {
    return uri;
  }

  /**
   * @deprecated As of 0.28.0, replaced by {@link #bytes()}
   */
  @Deprecated(forRemoval = true)
  public byte[] getBytes() {
    return bytes;
  }

  /** Returns the text content of this resource. */
  public String getText() {
    return new String(bytes, StandardCharsets.UTF_8);
  }

  /** Returns the content of this resource in Base64. */
  public String getBase64() {
    return Base64.getEncoder().encodeToString(bytes);
  }
}