npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

java-to-javascript

v0.1.15

Published

Convert Java Classes to ES6 Classes

Downloads

59

Readme

Java to JavaScript

Convert Java Classes to ES6 Classes.

Translates: classes, methods, variables, statics, and more!

(Originally created for converting Processing to p5.js)

🔗 Live Editor

Example

Input Java:

class MyClass {
  int x = 42;
  static String y = "Life";
  
  MyClass(String secret) {
    final String result = MyClass.y + secret + x;
    if (result != null) {
      purpose();
    }
  }
}

Output JavaScript:

class MyClass {
  constructor(secret) {
    this.x = 42;

    const result = MyClass.y + secret + this.x;
    if (result !== null) {
      purpose();
    }
  }
}
MyClass.y = 'Life';

More Examples...

Install

NPM

$ npm install java-to-javascript

Script tag

<script src="https://unpkg.com/java-to-javascript@latest/build/java-to-javascript.min.js"></script>

Note: You can replace latest with the library's version you want to use, e.g. v0.1.10. The library will be exposed on the global window scope as javaToJavascript.

Module API (For Node and the browser)

javaToJavascript(javaString, options?, progress?)

Returns: string - - Converted JavaScript

// Node module:
var javaToJavascript = require('java-to-javascript');
// Browser script:
var javaToJavascript = window.javaToJavascript;

var jsString = javaToJavascript( /* params */ );

| Param | Type | Description | | --- | --- | --- | | javaString | string | Java file contents | | [options] | object | | | [options.globalVars] | object | Object keys are added to the globalScope object. If the value is a string, the variable is renamed to that string | | [options.globalScope] | string | If specified, variables in globalVars are appended to globalScope object | | [options.p5] | boolean | Sets globalScope to 'p5', adds p5 variable mappings to globalVars, and allows for global methods and variables | | [options.ugly] | boolean | Don't beautify JavaScript code | | [progress] | function | Callback on progress of conversion. Args are progress value (0.0 to 1.0), and a message string |

Command Line API

Usage: java-to-javascript [options] <input_file>

Options:

  -V, --version         output the version number
  -o, --output <file>   Specifies the output filename. (Default is the input filename with a .js extension)
  -s, --scope <scope>   If specified, variables in `globals` are appended to `scope` object
  -g, --globals <file>  JSON or JavaScript file containing a global variable mapping. See README
  --p5                  Sets `scope` to "p5", adds p5 variable mappings to `globals`, and allows for global methods and variables
  --ugly                Don't beautify JavaScript code
  -h, --help            output usage information

How It Works

  • Parse Java code to create an AST (abstract syntax tree)
  • Replace global variables that the user specifies
  • Generate JavaScript code from the AST
  • Beautify JavaScript

Polyfills

I've included some Java Class (partial) polyfills in polyfills.js that help in the conversion of Java to JS.

Included Polyfills: List (alias ArrayList), Map (alias HashMap)

BUGS!

  • Local variables or method parameters with the same name as a variable in their class will incorrectly be assigned to the this object.

    Example

    Input Java:

    class Thing {
      int x, y;
      myMethod(int x) {
        int y = 20;
        this.x = x + y;
      }
    }

    Output JavaScript:

    class Thing {
      myMethod(x) {
        let y = 20;
        this.x = this.x + this.y;
      }
    }
  • Unnecessary or deeply nested parentheses can cause very long or infinite process hanging.

    Example

    /* I don't know the exact cause, but here's what I have found: */
    ((testFunc())); // Normal
    (((testFunc()))); // Long parse time
    ((((testFunc())))); // Infinitely hangs
  • I convert chars to numbers e.g. 'W' -> 87, which can cause discrepences in your code

Unsupported

  • Nested classes aka anonymous classes
  • Synchronized methods and statements
  • Interfaces and abstract classes
  • Enums
  • Packages and imports

DISCLAIMER

This is a work in progress! Not all Java features are supported, and some are too difficult to translate to JS, so make sure to doublecheck the resulting code (Also, it assumes you are passing in valid Java).

Please report bugs to Github Issues!

CREDITS

  • Java lexical parser: Github user mazko (https://github.com/mazko/jsjavaparser/blob/master/src/Java.1.7.pegjs)
  • Lexical Parser -> JS: peg.js