A utility class for managing Java imports during code generation.

This class simplifies the handling of Java imports by allowing you to register full qualified class names and ensuring that simple class names do not collide.

const importManager = new JavaImportManager();

const code = `
${importManager.useClass('java.util.List');} list1;
${importManager.useClass('java.awt.List');} list2;
`
console.log(code);
// Output:
// List list1;
// java.awt.List list2;

console.log(importManager.generateImports());
// Output:
// import java.awt.List;

Note, that the import manager will only generate imports for classes that are actually used. Conflicting class names will be resolved by using the full qualified name.

Constructors

Properties

classes: Map<string, string> = ...
currentPackage: string

Methods

  • Generates a list of Java import statements based on the registered classes.

    This method iterates over the registered classes and produces the corresponding import statements in alphabetical order. The result can be directly included at the beginning of a Java file.

    Method useClass shouldn't be called after this method.

    Returns string

    A formatted string containing all the import statements, separated by line breaks.

  • Registers a class for use in the generated Java code and returns a suitable name.

    If the simple name of the provided full qualified name is not already registered, this method registers it and returns the simple name. If a collision occurs (i.e., two different full qualified names share the same simple name), the method returns the full qualified name instead.

    This method should be called with the same full qualified name every time it is used in the generated code.

    Parameters

    • fullQualifiedName: string

      The fully qualified name of the class (e.g., java.util.List).

    Returns string

    The name to use in the generated code, either the simple class name or the full qualified name.