apple / pkl

A configuration as code language with rich validation and tooling.
https://pkl-lang.org
Apache License 2.0
10.15k stars 270 forks source link

Leverage basic Java 17 features #451

Closed translatenix closed 5 months ago

translatenix commented 5 months ago

Changes:

Result: Better code, fewer IntelliJ warnings

KushalP commented 5 months ago

Missing instanceof cases

Thank you for starting this. There are a few instanceof cases missing here. Provided below in the form of a git diff output on top of your changes:

diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/ModuleInfo.java b/pkl-core/src/main/java/org/pkl/core/runtime/ModuleInfo.java
index f452e03e5..fc9f6f941 100644
--- a/pkl-core/src/main/java/org/pkl/core/runtime/ModuleInfo.java
+++ b/pkl-core/src/main/java/org/pkl/core/runtime/ModuleInfo.java
@@ -134,8 +134,8 @@ public final class ModuleInfo {
             assert memberNode != null; // import is never a constant
             var importNode = memberNode.getBodyNode();
             var importUri =
-                importNode instanceof ImportNode
-                    ? ((ImportNode) importNode).getImportUri()
+                importNode instanceof ImportNode node
+                    ? node.getImportUri()
                     : ((ImportGlobNode) importNode).getImportUri();
             imports.put(propertyDef.getName().toString(), importUri);
             continue;
diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmDynamic.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmDynamic.java
index f4780bb61..d9d62ed23 100644
--- a/pkl-core/src/main/java/org/pkl/core/runtime/VmDynamic.java
+++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmDynamic.java
@@ -157,7 +157,6 @@ public final class VmDynamic extends VmObject {
   }

   private boolean isHiddenOrLocalProperty(Object key) {
-    return key instanceof Identifier
-        && (key == Identifier.DEFAULT || ((Identifier) key).isLocalProp());
+    return key instanceof Identifier i && (key == Identifier.DEFAULT || i.isLocalProp());
   }
 }
diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmTypeAlias.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmTypeAlias.java
index 57cd95341..ee9408fb6 100644
--- a/pkl-core/src/main/java/org/pkl/core/runtime/VmTypeAlias.java
+++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmTypeAlias.java
@@ -114,8 +114,8 @@ public final class VmTypeAlias extends VmValue {
    */
   @TruffleBoundary
   public SourceSection getConstraintSection() {
-    if (typeNode instanceof ConstrainedTypeNode) {
-      return ((ConstrainedTypeNode) typeNode).getFirstConstraintSection();
+    if (typeNode instanceof ConstrainedTypeNode node) {
+      return node.getFirstConstraintSection();
     }

     throw new VmExceptionBuilder()
diff --git a/pkl-core/src/main/java/org/pkl/core/stdlib/protobuf/RendererNodes.java b/pkl-core/src/main/java/org/pkl/core/stdlib/protobuf/RendererNodes.java
index d5c7439c1..0a0d39384 100644
--- a/pkl-core/src/main/java/org/pkl/core/stdlib/protobuf/RendererNodes.java
+++ b/pkl-core/src/main/java/org/pkl/core/stdlib/protobuf/RendererNodes.java
@@ -594,7 +594,7 @@ public final class RendererNodes {

         type = requiresWrapper() ? null : mappingType;
         return type;
-      } else if (type instanceof UnionTypeNode) {
+      } else if (type instanceof UnionTypeNode node) {
         // Some non-obvious normalization going on here:
         // - A union type resolves to a union type, unless all element types are string or string
         // literal types.
@@ -602,7 +602,7 @@ public final class RendererNodes {
         // - All string literal types are combined into a single String case.
         var hasString = false;
         var elements = new ArrayList<TypeNode>();
-        for (var t : ((UnionTypeNode) type).getElementTypeNodes()) {
+        for (var t : node.getElementTypeNodes()) {
           var resolved = resolveType(t);
           if (resolved instanceof StringLiteralTypeNode || resolved instanceof StringTypeNode) {
             if (!hasString) {
translatenix commented 5 months ago

@KushalP It would be easiest if you submitted a follow-up PR. There’s definitely more Java 17 work to be done.