Skip to content

Commit

Permalink
Add new LstType decorator
Browse files Browse the repository at this point in the history
Rather than explicitly adding a symbol to every class, this is now done indirectly by the `LstType` decorator. This has the advantage that the decorator can at the same time register the inverse mapping in a global map, which can then be used for the deserialization.
  • Loading branch information
knutwannheden committed Sep 19, 2024
1 parent 04a707b commit 5a43d9d
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 124 deletions.
7 changes: 4 additions & 3 deletions openrewrite/src/core/markers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {javaType, randomId, Tree, UUID} from "./tree";
import {randomId, UUID} from "./tree";
import {Parser} from "./parser";
import {LstType} from "./utils";

// This allows `isMarker()` to identify any `Marker` implementations
export const MarkerSymbol = Symbol('Marker');
Expand All @@ -19,8 +20,8 @@ export function isMarker(tree: any): tree is Marker {
return tree && tree[MarkerSymbol] === true;
}

@LstType("org.openrewrite.marker.Markers")
export class Markers {
static [javaType] = "org.openrewrite.marker.Markers";
public static readonly EMPTY: Markers = new Markers(randomId(), []);

private readonly _id: UUID;
Expand Down Expand Up @@ -52,8 +53,8 @@ export class Markers {
}
}

@LstType("org.openrewrite.ParseExceptionResult")
export class ParseExceptionResult implements Marker {
static [javaType] = "org.openrewrite.ParseExceptionResult";
[MarkerSymbol] = true;
private readonly _id: UUID;
private readonly _parserType: string;
Expand Down
16 changes: 6 additions & 10 deletions openrewrite/src/core/tree.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {v4 as uuidv4} from 'uuid';
import {Marker, Markers, ParseExceptionResult} from "./markers";
import {ListUtils} from "./utils";
import {ListUtils, LstType} from "./utils";
import {Parser, ParserInput} from "./parser";
import {ExecutionContext} from "./execution";
import path from "node:path";
Expand Down Expand Up @@ -33,8 +33,6 @@ export const randomId = (): UUID => {
return uuidv4() as UUID;
}

export const javaType = Symbol('javaType');

export interface Tree {
get id(): UUID;

Expand All @@ -50,8 +48,6 @@ export interface Tree {
}

export abstract class TreeVisitor<T extends Tree, P> {
static [javaType] = "org.openrewrite.TreeVisitor";

private _cursor: Cursor;

protected constructor() {
Expand Down Expand Up @@ -107,8 +103,8 @@ export abstract class TreeVisitor<T extends Tree, P> {

type Constructor<T = {}> = (abstract new(...args: any[]) => T) | ((obj: any) => obj is T) | symbol;

@LstType("org.openrewrite.Cursor")
export class Cursor {
static [javaType] = "org.openrewrite.Cursor";
static ROOT_VALUE: String = "root";

private readonly _parent: Cursor | null;
Expand Down Expand Up @@ -160,8 +156,8 @@ export class Cursor {
}
}

@LstType("org.openrewrite.Checksum")
export class Checksum {
static [javaType] = "org.openrewrite.Checksum";
private readonly _algorithm: string;
private readonly _value: ArrayBuffer;

Expand All @@ -179,8 +175,8 @@ export class Checksum {
}
}

@LstType("org.openrewrite.FileAttributes")
export class FileAttributes {
static [javaType] = "org.openrewrite.FileAttributes";
private readonly _creationTime: Date | undefined;
private readonly _lastModifiedTime: Date | undefined;
private readonly _lastAccessTime: Date | undefined;
Expand Down Expand Up @@ -386,8 +382,8 @@ class DefaultMarkerPrinter implements MarkerPrinter {
}
}

@LstType("org.openrewrite.PrintOutputCapture")
export class PrintOutputCapture<P> {
static [javaType] = "org.openrewrite.PrintOutputCapture";
private readonly _context: P;
private readonly _markerPrinter: MarkerPrinter;
private readonly _out: string[];
Expand Down Expand Up @@ -434,8 +430,8 @@ export abstract class PrinterFactory {
abstract createPrinter<P>(cursor: Cursor): TreeVisitor<Tree, PrintOutputCapture<P>>;
}

@LstType("org.openrewrite.tree.ParseError")
export class ParseError implements SourceFile {
static [javaType] = "org.openrewrite.tree.ParseError";
[SourceFileSymbol]: true = true;

private readonly _id: UUID;
Expand Down
20 changes: 19 additions & 1 deletion openrewrite/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,22 @@ export class ListUtils {

return newLs as T[];
}
}
}

const typeRegistry = new Map<string, new (...args: any[]) => any>();

const LST_TYPE_KEY = Symbol('lstType');

export function LstType(typeName: string) {
return function <T extends { new (...args: any[]): {} }>(constructor: T) {
// Add the static symbol property to the class constructor
Object.defineProperty(constructor, LST_TYPE_KEY, {
value: typeName,
writable: false,
enumerable: false,
});

// Register the class in the global type registry
typeRegistry.set(typeName, constructor);
};
}
14 changes: 6 additions & 8 deletions openrewrite/src/java/support_types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {javaType, Markers, SourceFile, Tree, UUID} from "../core";
import {LstType, Markers, SourceFile, Tree, UUID} from "../core";
import {J} from "./tree";
import {JavaType} from "./types";

Expand Down Expand Up @@ -34,21 +34,21 @@ export interface TypedTree extends Tree {
withType(type: JavaType | null): TypedTree;
}

@LstType("org.openrewrite.java.tree.Space")
export class Space {
static [javaType] = "org.openrewrite.java.tree.Space";
static readonly EMPTY: Space = new Space();
static readonly SINGLE_SPACE: Space = new Space();
}

export interface Comment {
}

@LstType("org.openrewrite.java.tree.TextComment")
export class TextComment implements Comment {
static [javaType] = "org.openrewrite.java.tree.TextComment";
}

@LstType("org.openrewrite.java.tree.JRightPadded")
export class JRightPadded<T> {
static [javaType] = "org.openrewrite.java.tree.JRightPadded";
constructor(element: T, after: Space, markers: Markers) {
this._element = element;
this._after = after;
Expand Down Expand Up @@ -149,9 +149,8 @@ export class JRightPadded<T> {
}
}

@LstType("org.openrewrite.java.tree.JLeftPadded")
export class JLeftPadded<T> {
static [javaType] = "org.openrewrite.java.tree.JLeftPadded";

constructor(before: Space, element: T, markers: Markers) {
this._before = before;
this._element = element;
Expand Down Expand Up @@ -195,9 +194,8 @@ export class JLeftPadded<T> {
}
}

@LstType("org.openrewrite.java.tree.JContainer")
export class JContainer<T> {
static [javaType] = "org.openrewrite.java.tree.JContainer";

constructor(before: Space, elements: JRightPadded<T>[], markers: Markers) {
this._before = before;
this._elements = elements;
Expand Down
Loading

0 comments on commit 5a43d9d

Please sign in to comment.