Skip to content

Commit

Permalink
Merge branch 'release/1.0-beta2'
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed May 9, 2018
2 parents 84ca359 + b6f3065 commit 379a0ea
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 22 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Java Processor to **generate [Typescript](https://www.typescriptlang.org/) Defi

## What is it for ?

This is to help developing on **JVM javascript engine** (ie [Nashorn](http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html)/[Rhino](https://github.com/mozilla/rhino)) using [Typescript](https://www.typescriptlang.org/)
This is to help developing on **JVM javascript engine** (ie [Nashorn](http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html)) using [Typescript](https://www.typescriptlang.org/)

The main goal is having the definitions available in the modern IDE like [Visual Studio Code](https://code.visualstudio.com/) and [Atom](https://atom.io/) and then use the **intellisense** feature available for java classes within typescript

Expand All @@ -27,6 +27,13 @@ It is not a transpiler from Java to Javascript like [datathings/java2typescript
* [jvm-npm](https://github.com/bsorrentino/jvm-npm)
> NPM compliant CommonJS module loader for the JVM
## DEMO

To give an idea about **how to work** there is a demo available online [here](https://java2ts-demo.herokuapp.com/).

> The backend of such demo has been developed using typescript on jvm provided by this project itself ([dogfooding](https://en.wikipedia.org/wiki/Eating_your_own_dog_food)).
> [java2typescript-demo project](https://github.com/bsorrentino/java2typescript-demo)
## Description

Basic idea is to develop a Project by mixing Java & Javascript code or even all in Javascript relying on the [Nashorn Javascript engine](http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html) embedded in JDK from Java8. This powerful engine enable Javascript language in JVM and allows to access to every java class either from JDK and external JAR(s) in a pretty straightforward way.
Expand Down Expand Up @@ -127,5 +134,5 @@ The easier way to start your **Typescript on JVM** project is using the provided
>mvn archetype:generate \
>-DarchetypeGroupId=org.bsc.processor \
>-DarchetypeArtifactId=java2ts-processor-archetype \
>-DarchetypeVersion=1.0-beta1
>-DarchetypeVersion=1.0-beta2
>```
2 changes: 1 addition & 1 deletion archetype/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.bsc.processor</groupId>
<artifactId>java2ts-processor-parent</artifactId>
<version>1.0-beta1</version>
<version>1.0-beta2</version>
</parent>
<artifactId>java2ts-processor-archetype</artifactId>
<name>java2ts-processor::archetype - ${project.version}</name>
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.bsc.processor</groupId>
<artifactId>java2ts-processor-parent</artifactId>
<version>1.0-beta1</version>
<version>1.0-beta2</version>
</parent>
<artifactId>java2ts-processor-core</artifactId>
<name>java2ts-processor::core - ${project.version}</name>
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/org/bsc/java2typescript/TSType.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ protected TSType() {
super(3);
}

public static TSType from(Class<?> cl, boolean export) {
public static TSType from(Class<?> cl, boolean exports) {
return new TSType() {
{
put(VALUE, cl);
put(EXPORT, export);
put(EXPORT, exports);
}
};
}

public static TSType from(Class<?> cl, String alias, boolean export) {
public static TSType from(Class<?> cl, String alias, boolean exports) {
return new TSType() {
{
put(VALUE, cl);
put(EXPORT, export);
put(EXPORT, exports);
put(ALIAS, alias);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.lang.String.format;

import java.io.Closeable;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
Expand Down Expand Up @@ -71,6 +72,7 @@ private final static void debug( String fmt, Object ...args ) {
System.out.println( format( fmt, (Object[])args));
}


/**
*
* @param type
Expand Down Expand Up @@ -412,6 +414,43 @@ private static String convertJavaToTS( Class<?> type,
return format("any /*%s*/",type.getName());

}

private static StringBuilder loadResourceByName( String name, StringBuilder result ) throws IOException {
try(final java.io.InputStream is = TypescriptConverter.class.getClassLoader().getResourceAsStream(name) ) {
int c; while( (c = is.read()) != -1 ) result.append((char)c);
}

return result;

}

/**
*
* @param sb
* @return
* @throws IOException
*/
public static StringBuilder loadDefaultDefinition( Optional<StringBuilder> sb ) throws IOException {

Objects.requireNonNull(sb, "sb is null!");

return loadResourceByName( "headerT.ts", sb.orElseGet( () -> new StringBuilder() ));

}

/**
*
* @param sb
* @return
* @throws IOException
*/
public static StringBuilder loadDefaultDeclarations( Optional<StringBuilder> sb ) throws IOException {

Objects.requireNonNull(sb, "sb is null!");

return loadResourceByName( "headerD.ts", sb.orElseGet( () -> new StringBuilder() ));

}

/**
*
Expand Down Expand Up @@ -680,6 +719,8 @@ Context getClassDecl()

if( type.getValue().isEnum() ) sb.append( "/* enum */" );

if( type.hasAlias()) sb.append("declare ");

sb.append( "class ");

final TSType superclass = TSType.from(type.getValue().getSuperclass());
Expand Down Expand Up @@ -887,7 +928,7 @@ public String processClass( int level, TSType tstype, java.util.Map<String, TSTy
.append('\n');

// NESTED CLASSES
if( level == 0 ) ctx.processNestedClasses( level );
//if( level == 0 ) ctx.processNestedClasses( level );

if( tstype.supportNamespace() )
ctx.append("\n} // end namespace ")
Expand Down
12 changes: 8 additions & 4 deletions core/src/main/resources/headerD.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//
// JAVA TYPESCRIPT DEDINITION
//

/*
* Project: java2typescript - https://github.com/bsorrentino/java2typescript
*
* Author: bsorrentino
*
* TYPESCRIPT DEFINITIONS
*
*/

type int = number;
type long = number;
Expand Down
13 changes: 9 additions & 4 deletions core/src/main/resources/headerT.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//
// EXPORT DECLARATIONS
//
//
/*
* Project: java2typescript - https://github.com/bsorrentino/java2typescript
*
* Author: bsorrentino
*
* TYPESCRIPT EXPORTED DECLARATIONS
*
*/


2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.bsc.processor</groupId>
<artifactId>java2ts-processor-parent</artifactId>
<version>1.0-beta1</version>
<version>1.0-beta2</version>
<packaging>pom</packaging>

<name>java2ts-processor::parent - ${project.version}</name>
Expand Down
2 changes: 1 addition & 1 deletion processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.bsc.processor</groupId>
<artifactId>java2ts-processor-parent</artifactId>
<version>1.0-beta1</version>
<version>1.0-beta2</version>
</parent>

<build>
Expand Down
2 changes: 1 addition & 1 deletion sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.bsc.processor</groupId>
<artifactId>java2ts-processor-parent</artifactId>
<version>1.0-beta1</version>
<version>1.0.0-SNAPSHOT</version>
</parent>

<build>
Expand Down
4 changes: 2 additions & 2 deletions sample/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./", /* Base directory to resolve non-absolute module names. */
"paths": { "ts/*":["target/ts/*"]}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
"baseUrl": "./", /* Base directory to resolve non-absolute module names. */
"paths": { "ts/*":["target/ts/*"]}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
Expand Down

0 comments on commit 379a0ea

Please sign in to comment.