Convert org.pkl.core POJOs to record classes (#808)

This commit is contained in:
Kushal Pisavadia
2024-11-13 23:54:41 +00:00
committed by GitHub
parent 406fa4cf40
commit 9a27616956
7 changed files with 89 additions and 337 deletions

View File

@@ -98,21 +98,21 @@ public abstract class Member implements Serializable {
return simpleName; return simpleName;
} }
public static class SourceLocation implements Serializable { public record SourceLocation(int startLine, int endLine) implements Serializable {
@Serial private static final long serialVersionUID = 0L; @Serial private static final long serialVersionUID = 0L;
private final int startLine; /**
private final int endLine; * @deprecated As of 0.28.0, replaced by {@link #startLine()}.
*/
public SourceLocation(int startLine, int endLine) { @Deprecated(forRemoval = true)
this.startLine = startLine;
this.endLine = endLine;
}
public int getStartLine() { public int getStartLine() {
return startLine; return startLine;
} }
/**
* @deprecated As of 0.28.0, replaced by {@link #endLine()}.
*/
@Deprecated(forRemoval = true)
public int getEndLine() { public int getEndLine() {
return endLine; return endLine;
} }

View File

@@ -41,20 +41,12 @@ public final class PklInfo {
return packageIndex; return packageIndex;
} }
/** A Pkl package index. */ /**
public static final class PackageIndex { * A Pkl package index.
private final String homepage; *
* @param homepage The homepage of this package index.
/** Constructs a {@link PackageIndex}. */ */
public PackageIndex(String homepage) { public record PackageIndex(String homepage) {
this.homepage = homepage;
}
/** The homepage of this package index. */
public String homepage() {
return homepage;
}
/** Returns the homepage of the given package. */ /** Returns the homepage of the given package. */
public String getPackagePage(String packageName, String packageVersion) { public String getPackagePage(String packageName, String packageVersion) {
return homepage + packageName + "/" + packageVersion + "/"; return homepage + packageName + "/" + packageVersion + "/";

View File

@@ -17,14 +17,18 @@ package org.pkl.core;
import com.oracle.truffle.api.Truffle; import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.TruffleOptions; import com.oracle.truffle.api.TruffleOptions;
import java.util.Objects;
import org.graalvm.home.Version; import org.graalvm.home.Version;
/** /**
* Information about the Pkl release that the current program runs on. This class is the Java * Information about the Pkl release that the current program runs on. This class is the Java
* equivalent of standard library module {@code pkl.platform}. * equivalent of standard library module {@code pkl.platform}.
*/ */
public final class Platform { public record Platform(
Language language,
Runtime runtime,
VirtualMachine virtualMachine,
OperatingSystem operatingSystem,
Processor processor) {
private static final Platform CURRENT; private static final Platform CURRENT;
static { static {
@@ -49,225 +53,46 @@ public final class Platform {
new Processor(architecture)); new Processor(architecture));
} }
private final Language language;
private final Runtime runtime;
private final VirtualMachine virtualMachine;
private final OperatingSystem operatingSystem;
private final Processor processor;
/** Constructs a platform. */
public Platform(
Language language,
Runtime runtime,
VirtualMachine virtualMachine,
OperatingSystem operatingSystem,
Processor processor) {
this.language = language;
this.runtime = runtime;
this.virtualMachine = virtualMachine;
this.operatingSystem = operatingSystem;
this.processor = processor;
}
/** The Pkl release that the current program runs on. */ /** The Pkl release that the current program runs on. */
public static Platform current() { public static Platform current() {
return CURRENT; return CURRENT;
} }
/** The language implementation of this platform. */ /**
public Language language() { * The language implementation of a platform.
return language; *
} * @param version the version of this language implementation
*/
public record Language(String version) {}
/** The language runtime of this platform. */ /**
public Runtime runtime() { * The language runtime of a platform.
return runtime; *
} * @param name the name of this language runtime.
* @param version the version of this language runtime.
*/
public record Runtime(String name, String version) {}
/** The virtual machine of this platform. */ /**
public VirtualMachine virtualMachine() { * The virtual machine of a platform.
return virtualMachine; *
} * @param name the name of this virtual machine.
* @param version the version of this virtual machine.
*/
public record VirtualMachine(String name, String version) {}
/** The operating system of this platform. */ /**
public OperatingSystem operatingSystem() { * The operating system of a platform.
return operatingSystem; *
} * @param name the name of this operating system.
* @param version the version of this operating system.
*/
public record OperatingSystem(String name, String version) {}
/** The processor of this platform. */ /**
public Processor processor() { * The processor of a platform.
return processor; *
} * @param architecture the instruction set architecture of this processor.
*/
@Override public record Processor(String architecture) {}
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Platform other)) return false;
return language.equals(other.language)
&& runtime.equals(other.runtime)
&& virtualMachine.equals(other.virtualMachine)
&& operatingSystem.equals(other.operatingSystem)
&& processor.equals(other.processor);
}
@Override
public int hashCode() {
return Objects.hash(language, runtime, virtualMachine, operatingSystem, processor);
}
/** The language implementation of a platform. */
public static final class Language {
private final String version;
/** Constructs a {@link Language}. */
public Language(String version) {
this.version = version;
}
/** The version of this language implementation. */
public String version() {
return version;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Language other)) return false;
return version.equals(other.version);
}
@Override
public int hashCode() {
return version.hashCode();
}
}
/** The language runtime of a platform. */
public static final class Runtime {
private final String name;
private final String version;
/** Constructs a {@link Runtime}. */
public Runtime(String name, String version) {
this.name = name;
this.version = version;
}
/** The name of this language runtime. */
public String name() {
return name;
}
/** The version of this language runtime. */
public String version() {
return version;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Runtime other)) return false;
return name.equals(other.name) && version.equals(other.version);
}
@Override
public int hashCode() {
return Objects.hash(name, version);
}
}
/** The virtual machine of a platform. */
public static final class VirtualMachine {
private final String name;
private final String version;
/** Constructs a {@link VirtualMachine}. */
public VirtualMachine(String name, String version) {
this.name = name;
this.version = version;
}
/** The name of this virtual machine. */
public String name() {
return name;
}
/** The version of this virtual machine. */
public String version() {
return version;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof VirtualMachine other)) return false;
return name.equals(other.name) && version.equals(other.version);
}
@Override
public int hashCode() {
return Objects.hash(name, version);
}
}
/** The operating system of a platform. */
public static final class OperatingSystem {
private final String name;
private final String version;
/** Constructs an {@link OperatingSystem}. */
public OperatingSystem(String name, String version) {
this.name = name;
this.version = version;
}
/** The name of this operating system. */
public String name() {
return name;
}
/** The version of this operating system. */
public String version() {
return version;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof OperatingSystem other)) return false;
return name.equals(other.name) && version.equals(other.version);
}
@Override
public int hashCode() {
return Objects.hash(name, version);
}
}
/** The processor of a platform. */
public static final class Processor {
private final String architecture;
/** Constructs a {@link Processor}. */
public Processor(String architecture) {
this.architecture = architecture;
}
/** The instruction set architecture of this processor. */
public String architecture() {
return architecture;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Processor other)) return false;
return architecture.equals(other.architecture);
}
@Override
public int hashCode() {
return architecture.hashCode();
}
}
} }

View File

@@ -164,25 +164,15 @@ public final class Release {
} }
/** The source code of a Pkl release. */ /** The source code of a Pkl release. */
public static final class SourceCode { public record SourceCode(String homepage, String version) {
private final String homepage; /**
private final String version; * @deprecated As of 0.28.0, replaced by {@link #version()}.
*/
/** Constructs a {@link SourceCode}. */ @Deprecated(forRemoval = true)
public SourceCode(String homepage, String version) {
this.homepage = homepage;
this.version = version;
}
public String getVersion() { public String getVersion() {
return version; return version;
} }
/** The homepage of this source code. */
public String homepage() {
return homepage;
}
/** /**
* Returns the source code page of the file with the given path. <b>Note:</b> Files may be moved * Returns the source code page of the file with the given path. <b>Note:</b> Files may be moved
* or deleted anytime. * or deleted anytime.
@@ -195,75 +185,20 @@ public final class Release {
public String getSourceCodeUrlScheme() { public String getSourceCodeUrlScheme() {
return homepage + "blob/" + version + "/stdlib%{path}#L%{line}-L%{endLine}"; return homepage + "blob/" + version + "/stdlib%{path}#L%{line}-L%{endLine}";
} }
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof SourceCode other)) return false;
return homepage.equals(other.homepage) && version.equals(other.version);
}
@Override
public int hashCode() {
return Objects.hash(homepage, version);
}
} }
/** The documentation of a Pkl release. */ /**
public static final class Documentation { * The documentation of a Pkl release.
private final String homepage; *
* @param homepage the homepage of this documentation.
/** Constructs a {@link Documentation}. */ */
public Documentation(String homepage) { public record Documentation(String homepage) {}
this.homepage = homepage;
}
/** The homepage of this documentation. */
public String homepage() {
return homepage;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Documentation other)) return false;
return homepage.equals(other.homepage);
}
@Override
public int hashCode() {
return homepage.hashCode();
}
}
/** /**
* The standard library of a Pkl release. * The standard library of a Pkl release.
* *
* @since 0.21.0 * @since 0.21.0
* @param modules the modules of this standard library.
*/ */
public static final class StandardLibrary { public record StandardLibrary(Set<String> modules) {}
private final Set<String> modules;
/** Constructs a {@link StandardLibrary}. */
public StandardLibrary(Set<String> modules) {
this.modules = modules;
}
/** The modules of this standard library. */
public Set<String> modules() {
return modules;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof StandardLibrary other)) return false;
return modules.equals(other.modules);
}
@Override
public int hashCode() {
return modules.hashCode();
}
}
} }

View File

@@ -72,8 +72,8 @@ class EvaluateSchemaTest {
assertThat(module.moduleName).isEqualTo("test") assertThat(module.moduleName).isEqualTo("test")
assertThat(module.moduleClass.sourceLocation.startLine).isEqualTo(2) assertThat(module.moduleClass.sourceLocation.startLine()).isEqualTo(2)
assertThat(module.moduleClass.sourceLocation.endLine).isEqualTo(26) assertThat(module.moduleClass.sourceLocation.endLine()).isEqualTo(26)
} }
private fun checkModuleProperties(module: ModuleSchema) { private fun checkModuleProperties(module: ModuleSchema) {
@@ -81,21 +81,21 @@ class EvaluateSchemaTest {
assertThat(properties).hasSize(3) assertThat(properties).hasSize(3)
val propertyb1 = properties.getValue("propertyb1") val propertyb1 = properties.getValue("propertyb1")
assertThat(propertyb1.sourceLocation.startLine).isEqualTo(5) assertThat(propertyb1.sourceLocation.startLine()).isEqualTo(5)
assertThat(propertyb1.sourceLocation.endLine).isEqualTo(5) assertThat(propertyb1.sourceLocation.endLine()).isEqualTo(5)
assertThat(propertyb1.type).isEqualTo(PType.UNKNOWN) assertThat(propertyb1.type).isEqualTo(PType.UNKNOWN)
val propertyb2 = properties.getValue("propertyb2") val propertyb2 = properties.getValue("propertyb2")
assertThat(propertyb2.sourceLocation.startLine).isEqualTo(8) assertThat(propertyb2.sourceLocation.startLine()).isEqualTo(8)
assertThat(propertyb2.sourceLocation.endLine).isEqualTo(9) assertThat(propertyb2.sourceLocation.endLine()).isEqualTo(9)
val paramType = propertyb2.type val paramType = propertyb2.type
assertThat(paramType).isInstanceOf(PType.Class::class.java) assertThat(paramType).isInstanceOf(PType.Class::class.java)
paramType as PType.Class paramType as PType.Class
assertThat(paramType.pClass).isEqualTo(BaseModule.getIntClass().export()) assertThat(paramType.pClass).isEqualTo(BaseModule.getIntClass().export())
val propertyb3 = properties.getValue("propertyb3") val propertyb3 = properties.getValue("propertyb3")
assertThat(propertyb3.sourceLocation.startLine).isEqualTo(24) assertThat(propertyb3.sourceLocation.startLine()).isEqualTo(24)
assertThat(propertyb3.sourceLocation.endLine).isEqualTo(24) assertThat(propertyb3.sourceLocation.endLine()).isEqualTo(24)
} }
private fun checkModuleMethods(module: ModuleSchema) { private fun checkModuleMethods(module: ModuleSchema) {
@@ -103,14 +103,14 @@ class EvaluateSchemaTest {
assertThat(methods).hasSize(3) assertThat(methods).hasSize(3)
val methodb1 = methods.getValue("methodb1") val methodb1 = methods.getValue("methodb1")
assertThat(methodb1.sourceLocation.startLine).isEqualTo(12) assertThat(methodb1.sourceLocation.startLine()).isEqualTo(12)
assertThat(methodb1.sourceLocation.endLine).isEqualTo(12) assertThat(methodb1.sourceLocation.endLine()).isEqualTo(12)
assertThat(methodb1.parameters).isEmpty() assertThat(methodb1.parameters).isEmpty()
assertThat(methodb1.returnType).isEqualTo(PType.UNKNOWN) assertThat(methodb1.returnType).isEqualTo(PType.UNKNOWN)
val methodb2 = methods.getValue("methodb2") val methodb2 = methods.getValue("methodb2")
assertThat(methodb2.sourceLocation.startLine).isEqualTo(15) assertThat(methodb2.sourceLocation.startLine()).isEqualTo(15)
assertThat(methodb2.sourceLocation.endLine).isEqualTo(16) assertThat(methodb2.sourceLocation.endLine()).isEqualTo(16)
val paramType = methodb2.parameters.getValue("str") val paramType = methodb2.parameters.getValue("str")
assertThat(paramType).isInstanceOf(PType.Constrained::class.java) assertThat(paramType).isInstanceOf(PType.Constrained::class.java)
paramType as PType.Constrained paramType as PType.Constrained
@@ -130,8 +130,8 @@ class EvaluateSchemaTest {
assertThat(returnType.constraints).isEqualTo(listOf("isPositive")) assertThat(returnType.constraints).isEqualTo(listOf("isPositive"))
val methodb3 = methods.getValue("methodb3") val methodb3 = methods.getValue("methodb3")
assertThat(methodb3.sourceLocation.startLine).isEqualTo(26) assertThat(methodb3.sourceLocation.startLine()).isEqualTo(26)
assertThat(methodb3.sourceLocation.endLine).isEqualTo(26) assertThat(methodb3.sourceLocation.endLine()).isEqualTo(26)
assertThat(methodb3.parameters.keys).containsExactly("x", "_#1", "i", "_#3") assertThat(methodb3.parameters.keys).containsExactly("x", "_#1", "i", "_#3")
} }
@@ -139,8 +139,8 @@ class EvaluateSchemaTest {
val classes = module.classes val classes = module.classes
assertThat(classes).hasSize(1) assertThat(classes).hasSize(1)
val classb1 = classes.getValue("Classb1") val classb1 = classes.getValue("Classb1")
assertThat(classb1.sourceLocation.startLine).isEqualTo(19) assertThat(classb1.sourceLocation.startLine()).isEqualTo(19)
assertThat(classb1.sourceLocation.endLine).isEqualTo(22) assertThat(classb1.sourceLocation.endLine()).isEqualTo(22)
assertThat(classb1.properties).hasSize(2) assertThat(classb1.properties).hasSize(2)
} }
@@ -157,8 +157,8 @@ class EvaluateSchemaTest {
.isEqualTo(URI("modulepath:/org/pkl/core/EvaluateSchemaTestBaseModule.pkl")) .isEqualTo(URI("modulepath:/org/pkl/core/EvaluateSchemaTestBaseModule.pkl"))
assertThat(supermodule.moduleName).isEqualTo("test.base") assertThat(supermodule.moduleName).isEqualTo("test.base")
assertThat(supermodule.moduleClass.sourceLocation.startLine).isEqualTo(1) assertThat(supermodule.moduleClass.sourceLocation.startLine()).isEqualTo(1)
assertThat(supermodule.moduleClass.sourceLocation.endLine).isEqualTo(10) assertThat(supermodule.moduleClass.sourceLocation.endLine()).isEqualTo(10)
val properties = supermodule.moduleClass.properties val properties = supermodule.moduleClass.properties
assertThat(properties).hasSize(1) assertThat(properties).hasSize(1)

View File

@@ -274,8 +274,8 @@ internal abstract class ModuleOrClassPageGenerator<S>(
protected fun HtmlBlockTag.renderMemberSourceLink(member: Member) { protected fun HtmlBlockTag.renderMemberSourceLink(member: Member) {
// Prevent churn by setting static line numbers. // Prevent churn by setting static line numbers.
// This is so our doc generator tests don't break if, say, we change sources in the stdlib. // This is so our doc generator tests don't break if, say, we change sources in the stdlib.
val startLine = if (isTestMode) 123 else member.sourceLocation.startLine val startLine = if (isTestMode) 123 else member.sourceLocation.startLine()
val endLine = if (isTestMode) 456 else member.sourceLocation.endLine val endLine = if (isTestMode) 456 else member.sourceLocation.endLine()
val moduleSourceUrl = val moduleSourceUrl =
pageScope.resolveModuleNameToSourceUrl( pageScope.resolveModuleNameToSourceUrl(
member.moduleName, member.moduleName,

View File

@@ -114,8 +114,8 @@ internal fun String.replaceSourceCodePlaceholders(
sourceLocation: Member.SourceLocation sourceLocation: Member.SourceLocation
): String { ): String {
return replace("%{path}", path) return replace("%{path}", path)
.replace("%{line}", sourceLocation.startLine.toString()) .replace("%{line}", sourceLocation.startLine().toString())
.replace("%{endLine}", sourceLocation.endLine.toString()) .replace("%{endLine}", sourceLocation.endLine().toString())
} }
/** Encodes a URI string, encoding characters that are part of URI syntax. */ /** Encodes a URI string, encoding characters that are part of URI syntax. */