From b487704b8824bb35ea64505bf1dc4b1c7d2ee37a Mon Sep 17 00:00:00 2001 From: Mike Lischke Date: Sat, 9 Mar 2024 11:46:22 +0100 Subject: [PATCH] Allow unsetting the java lib configuration Setting `javaLib` to an empty string prevents including any library that provides Java APIs, and also prevents adding `extends JavaObject` to classes that have no base class. Useful if you plan to completely rewrite the code without any Java reminiscence. Signed-off-by: Mike Lischke --- src/conversion/FileProcessor.ts | 17 +++++++++++++---- src/conversion/JavaToTypeScript.ts | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/conversion/FileProcessor.ts b/src/conversion/FileProcessor.ts index 20aa5e1..d762aa0 100644 --- a/src/conversion/FileProcessor.ts +++ b/src/conversion/FileProcessor.ts @@ -383,7 +383,7 @@ export class FileProcessor { header.append("\n"); let aliases = ""; - if (this.configuration.options?.useUnqualifiedTypes) { + if (this.configuration.options?.useUnqualifiedTypes && this.configuration.javaLib !== "") { // Create const reassignments and type aliases depending on the type of the imported symbol. this.packageImports.forEach((symbol, key) => { // Cannot create a type alias for an enum type (it would be infinite recursion). @@ -722,7 +722,7 @@ export class FileProcessor { if (context.EXTENDS()) { this.getContent(localBuilder, context.EXTENDS()); this.processTypeType(localBuilder, context.typeType()); - } else { + } else if (this.configuration.javaLib !== "") { // Add the default extends clause. localBuilder.append(" extends JavaObject"); this.registerJavaImport("JavaObject"); @@ -1355,6 +1355,11 @@ export class FileProcessor { } } + if (!hasSuperCall && this.configuration.javaLib === "") { + // Don't add a super call if no base class is specified and no java lib is used. + hasSuperCall = true; + } + result.containsThisCall = hasThisCall; // See if we should automatically add a call to super. Only if none exists yet and this class extends another. @@ -2739,8 +2744,12 @@ export class FileProcessor { builder.append("class extends "); } else { if (info.symbol instanceof InterfaceSymbol) { - builder.append("class extends JavaObject implements "); - this.registerJavaImport("JavaObject"); + if (this.configuration.javaLib === "") { + builder.append("class implements "); + } else { + builder.append("class extends JavaObject implements "); + this.registerJavaImport("JavaObject"); + } } else { builder.append("class extends "); } diff --git a/src/conversion/JavaToTypeScript.ts b/src/conversion/JavaToTypeScript.ts index c3ef8ce..07b09a9 100644 --- a/src/conversion/JavaToTypeScript.ts +++ b/src/conversion/JavaToTypeScript.ts @@ -201,7 +201,7 @@ export interface IConverterConfiguration { export class JavaToTypescriptConverter { public constructor(private configuration: IConverterConfiguration) { let javaLib; - if (!configuration.javaLib) { + if (configuration.javaLib === undefined) { javaLib = "jree"; // Use the jree node module as default. } else if (configuration.javaLib.indexOf("/") < 0) { javaLib = configuration.javaLib; // Assume this is another node module.