Code Monkey home page Code Monkey logo

java2typescript's Introduction

java2typescript

Join the chat at https://gitter.im/bsorrentino/java2typescript

Java Processor to generate Typescript Definition file (.d.ts) from whatever Java classes.

Diagram of solution

java2ts diagram

What is it for ?

This is to help developing on JVM javascript engine, either GraalJs or Rhino using Typescript

Nashorn has been DEPRECATED (refer to JEP335 )

The main goal is having the definitions available in the modern IDE like Visual Studio Code and Atom and then use the intellisense feature available for java classes within typescript

What is it not for ?

It is not a transpiler from Java to Javascript like datathings/java2typescript

Similar projects

typescript-generator is a tool for generating TypeScript definition files (.d.ts) from Java JSON classes. If you have REST service written in Java using object to JSON mapping you can use typescript-generator to generate TypeScript interfaces from Java classes.

1c is aimed to compile TypeScript to runnable code on both JVM and web browser. You can implement the full stack with TypeScript while using libraries from Maven and NPM together.

Related Project

NPM compliant CommonJS module loader for the JVM

DEMO

To give an idea about how to work there is a demo available online here.

The backend of such demo has been developed using typescript on jvm provided by this project itself (dogfooding). java2typescript-demo project

Description

Basic idea is to develop a Project by mixing Java & Javascript code (or completely in Javascript) relying on the Nashorn Javascript engine or Rhino javascript engine. This powerful engines enable Javascript language in JVM and they allow to access to every java class present in classpath in a pretty straightforward way. That's cool, the Javascript is very easy to learn, but the question are :

  1. Is it possible develop a complete and well structured Javascript application ?
  2. Developing in Javascript on JVM is as productive as programming in Java ?

Just to answer the questions above that this project has been developed.

  1. Is it possible develop a complete and well structured Javascript application ?

Yes it is, but with help of a powerful Typescript that fills language's gaps and produce Javascript as output

  1. Developing in Javascript on JVM is as productive as programming in Java ?

Yes it could be, but we need :

  • An IDE with intellisense capability that is able to inspect java/javascript classes during development.

This is the main goal of project, that is to generate Typescript Declarations enabling every Typescript-Aware IDE (eg. Atom, VSCode) to give you the intellisense feature make more comfortable use access to Java features/classes from Javascript

  • A module module system that allow to arrange application in separate well-defined modules

This is achieved by project jvm-npm that enable use of commonJS enabling Typescript Modules.

Getting Started

Let assume that you have successfully setup a java project

Declares the classes you want to generate the Typescript declaration for

Create a file package-info.java and annotate the given package with @Java2TS that will contain all the @Type of which you want the Typescript declaration.

** Type Attributes **

Name Mandatory Type Description
value Yes Class Full Qualified Name of Java class
alias No String Assign a new name to exported class in typescript
export No boolean If true, other than typescript declaration adds definition in file *-types.ts. It is need for instantiable classes, in order to use new operator or factory method(s)
functional No boolean If true other than typescript declaration adds a particular definition. Valid only for interfaces that have one abstract method and haven't @FunctionalInterface annotation

Example

@Java2TS(declare = {

  @Type(java.nio.file.Files.class, export="true"),
  @Type(java.nio.file.Path.class),
  @Type(java.nio.file.Paths.class, export="true"),

  @Type(value=java.util.stream.Stream.class,export=true),

  @Type(java.util.Collection.class),
  @Type(java.util.Map.class),
  @Type(value=java.util.List.class, alias="List" ),
  @Type(java.util.Set.class),
  @Type(value=java.util.Arrays.class, export=true),

  @Type(java.util.Optional.class),

  @Type(value=java.net.URI.class, export=true),
  @Type(java.net.URL.class),

  @Type(java.lang.Runnable.class, functional=true)

})
package org.mypackage;

Add the dependency containing the Java2TS Processor

<dependency>
  <groupId>org.bsc.processor</groupId>
  <artifactId>java2ts-processor</artifactId>
  <version>version</version>
</dependency>

Add the Annotation Processor Plugin

Graajs compatibility see samples

<plugin>
  <groupId>org.bsc.maven</groupId>
  <artifactId>maven-processor-plugin</artifactId>
  <version>3.3.3</version>
  <executions>
    <execution>
      <id>process</id>
      <goals>
        <goal>process</goal>
      </goals>
      <phase>generate-sources</phase>
      <configuration>
          <outputDirectory>${project.build.directory}</outputDirectory>
       <options>
            <ts.outfile>name</ts.outfile><!-- name of generated file -->
       </options>
      </configuration>
    </execution>
  </executions>
</plugin>

Enforce Rhino compatibility see samples

<plugin>
  <groupId>org.bsc.maven</groupId>
  <artifactId>maven-processor-plugin</artifactId>
  <version>3.3.3</version>
  <executions>
    <execution>
      <id>process</id>
      <goals>
        <goal>process</goal>
      </goals>
      <phase>generate-sources</phase>
      <configuration>
          <outputDirectory>${project.build.directory}</outputDirectory>
       <options>
            <ts.outfile>name</ts.outfile><!-- name of generated file -->
            <compatibility>rhino</compatibility>
       </options>
      </configuration>
    </execution>
  </executions>
</plugin>

Use Maven Archetype

The easier way to start your Typescript on JVM project is using the provided maven archetype

Interactive Mode

mvn archetype:generate \
-DarchetypeGroupId=org.bsc.processor \
-DarchetypeArtifactId=java2ts-processor-archetype \
-DarchetypeVersion=1.1.0

java2typescript's People

Contributors

bsorrentino avatar dependabot[bot] avatar gitter-badger avatar pintomau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

java2typescript's Issues

add support for Graal.JS

Since the Nashorn seems going to be deprecated (refer to JEP335 ) decision that I really don't understand and seems to me more strategical than technical, we have to identify alternative solutions

One suitable solution is to move on Rhino (refer issue #8 )

Another one seems to use Graal.JS that should be available as independent module on top of JVM 9

Error on process project class using gradle

I'm trying to setup this lib on my project, but when i map a class from my project it gives me class not found exception.

i made this repo with a reproduction of the problem: https://github.com/eloyleonardo/test-d-ts.

If i remove the the line with my custom class all pass.

It looks like the processor doesn't have access to the project classes (classpath), but i can't find any tricks to make this works.

Is this expected ? Or is there any change on gradle to the processor find my class ?

Problem on ConcurrentHashMap declaration (BiFunction)

See BiFunction declaration below

class ConcurrentHashMap<K, V>/* extends java.util.AbstractMap<K, V> implements ConcurrentMap<K, V>, java.io.Serializable*/ {

reduceEntries( arg0:long, arg1:BiFunction<java.util.Map$Entry<K, V>, java.util.Map$Entry<K, V>, ? extends java.util.Map$Entry<K, V>> ):java.util.Map$Entry<K, V>;

} // end ConcurrentHashMap

[Feature Request] Gradle support

Would it be possible to add Gradle support? Or if it's a lot of work could you maybe give some pointers so i can take a look at porting?

Thanks!

verify generation of MemoryType definition

Processing enum java.lang.management.MemoryType produced a strange code:

interface MemoryTypeStatic {

Heap memory:java.lang.management.MemoryType;
Nonheap memory:java.lang.management.MemoryType;

readonly class:any;
valueOf( arg0:string ):java.lang.management.MemoryType;
valueOf<T>( arg0:java.lang.Class<T>, arg1:string ):T;
values(  ):[java.lang.management.MemoryType];
}

Target classes are loaded and initialized via ClassLoader

This is extremely problematic because classes (or their static dependencies) may have static initializers that outright fail in a compilation / annotation processor environment. This is a big motivator for the Java javax.lang.model packages.

Is it possible to generate the definitions for the JVM?

Would it be possible to generate the definitions from the whole JVM itself? Nashorn allows one to use all the Java classes inside your javascripts, so having the types for the whole JVM would be nice to help writing it in typescript.

generate definitions for Functional Interface

Idea is to generate a definition that will have one constructor that accept one parameter that is declaration of functional interface itself

Example

package-info.java

@Type(value=java.lang.Iterable.class)

<types>-types.ts

interface IterableStatic {
  new<T>( arg0:java.lang.Iterable<T> ):java.lang.Iterable<T>;
}

const Iterable:IterableStatic  = Java.type("java.lang.Iterable");

add support for multi package-type.java

Idea is to allow to increase project modularisation

this feature implies that for definitions we will have the possibility to define a custom namespace for each Java2TS annotation

Lack of namespacing in types file output can lead to naming collisions

It is ... unfortunate that we end up having 2 Xxx names for every given Java class for which we want to access contructors (or enum values). So I get that these don't really fall nicely into a single namespace.

Elevating all "exported" classes to the same global namespace does not seem like an appropriate solution, though.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.