diff --git a/.github/workflows/maven-publish.yml b/.github/workflows/maven-publish.yml new file mode 100644 index 0000000..f0c5c28 --- /dev/null +++ b/.github/workflows/maven-publish.yml @@ -0,0 +1,26 @@ +# This workflow will build a package using Maven and then publish it to GitHub packages when a release is created +# For more information see: https://github.com/actions/setup-java#apache-maven-with-a-settings-path + +name: Maven Package + +on: + push: + branches: + - develop + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 1.8 + uses: actions/setup-java@v1 + with: + java-version: 1.8 + + - name: Build with Maven + env: + SONATYPE_PASSWORD: ${{ secrets.SonatypePassword }} + run: mvn -B deploy --file pom.xml -s settings-template.xml diff --git a/.gitignore b/.gitignore index 59655a1..afcc2ac 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ bin/ .project .vscode/ .factorypath +.idea/ +*.iml diff --git a/README.md b/README.md index 5fd9305..7389a87 100644 --- a/README.md +++ b/README.md @@ -183,3 +183,4 @@ The easier way to start your **Typescript on JVM** project is using the provided >-DarchetypeArtifactId=java2ts-processor-archetype \ >-DarchetypeVersion=1.1.0 >``` + diff --git a/archetype/pom.xml b/archetype/pom.xml index 20ecd0e..e5b1d4b 100644 --- a/archetype/pom.xml +++ b/archetype/pom.xml @@ -5,10 +5,10 @@ org.bsc.processor java2ts-processor-parent - 1.2.0 + 1.3.0-SNAPSHOT java2ts-processor-archetype - java2ts-processor::archetype - ${project.version} + java2ts-processor::archetype maven-archetype diff --git a/core/pom.xml b/core/pom.xml index abd0f6d..53538a9 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -1,20 +1,25 @@ - - 4.0.0 - - org.bsc.processor - java2ts-processor-parent - 1.2.0 - - java2ts-processor-core - java2ts-processor::core - ${project.version} - - - + + 4.0.0 + + org.bsc.processor + java2ts-processor-parent + 1.3.0-SNAPSHOT + + java2ts-processor-core + java2ts-processor::core + + + + + + + junit junit test - - + + \ No newline at end of file diff --git a/core/src/main/java/org/bsc/java2typescript/TSNamespace.java b/core/src/main/java/org/bsc/java2typescript/TSNamespace.java new file mode 100644 index 0000000..d937220 --- /dev/null +++ b/core/src/main/java/org/bsc/java2typescript/TSNamespace.java @@ -0,0 +1,28 @@ +package org.bsc.java2typescript; + +import java.util.Collections; +import java.util.Set; + +public class TSNamespace { + + private final String name; + + private final Set types; + + private TSNamespace(String name, Set types) { + this.name = name; + this.types = Collections.unmodifiableSet(types); + } + + public String getName() { + return name; + } + + public Set getTypes() { + return types; + } + + public static TSNamespace of( String name, Set types ) { + return new TSNamespace( name, types ); + } +} diff --git a/core/src/main/java/org/bsc/java2typescript/TSType.java b/core/src/main/java/org/bsc/java2typescript/TSType.java index 9873f59..b32ca89 100644 --- a/core/src/main/java/org/bsc/java2typescript/TSType.java +++ b/core/src/main/java/org/bsc/java2typescript/TSType.java @@ -29,7 +29,15 @@ protected TSType() { super(3); } - public static TSType from(Class cl) { + + public static TSType of() { + return new TSType() { + { + put(VALUE, Void.class); + } + }; + } + public static TSType of(Class cl) { return new TSType() { { put(VALUE, cl); diff --git a/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java b/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java index 0822ef8..0adf330 100644 --- a/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java +++ b/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java @@ -51,10 +51,11 @@ public TypescriptConverter(Compatibility compatibility) { public final boolean isRhino() { return compatibility == Compatibility.RHINO; } - + /** - * - * @param declaredClass + * + * @param type + * @param declaredTypeMap * @return */ public String processStatic(TSType type, java.util.Map declaredTypeMap) { @@ -263,7 +264,7 @@ Context getClassDecl() { sb.append("class "); - final TSType superclass = TSType.from(type.getValue().getSuperclass()); + final TSType superclass = TSType.of(type.getValue().getSuperclass()); if (superclass != null) { inherited.append(" extends ").append(getTypeName(superclass, type, true)); @@ -274,7 +275,7 @@ Context getClassDecl() { if (interfaces.length > 0) { - final String ifc = Arrays.stream(interfaces).map(c -> TSType.from(c)) + final String ifc = Arrays.stream(interfaces).map(c -> TSType.of(c)) .map(t -> getTypeName(t, type, true)).collect(Collectors.joining(", ")); inherited.append((type.getValue().isInterface()) ? " extends " : " implements ").append(ifc); @@ -326,9 +327,8 @@ Context processEnumDecl() { /** * - * @param sb - * @param type - * @param declaredTypeMap + * @param level + * @return */ Context processMemberClasses(int level) { @@ -344,7 +344,7 @@ Context processMemberClasses(int level) { Stream.of(memberClasses).peek(c -> debug("nested class name[%s]", c.getName())) // .filter(distinctByKey( c -> c.getSimpleName() )) - .filter(distinctByKey(c -> c.getName())).map(cl -> TSType.from(cl)) + .filter(distinctByKey(c -> c.getName())).map(cl -> TSType.of(cl)) .peek(t -> debug("nested type name[%s]", t.getTypeName())) .map(t -> processClass(level + 1, t, declaredTypeMap)) .forEach(decl -> sb.append(decl)); @@ -401,7 +401,8 @@ public Context contextOf(TSType tstype, java.util.Map declaredTy /** * - * @param bi + * @param level + * @param tstype * @param declaredTypeMap * @return */ @@ -417,17 +418,16 @@ public String processClass(int level, TSType tstype, java.util.Map genAbstractMethod = - m -> isRhino() ? - getMethodDecl(ctx, m, false /* non optional */) : - getMethodParametersAndReturnDecl(ctx, m, false); - + methods.stream() .filter(m -> Modifier.isAbstract(m.getModifiers())) .findFirst() .ifPresent( m -> ctx.append('\t') - .append( genAbstractMethod.apply(m) ) + .append(getMethodParametersAndReturnDecl(ctx, m, false)) + // Rhino compatibility ??? + //.append("\n\t") + //.append(getMethodDecl(ctx, m, false /* non optional */)) .append(ENDL)); methods.stream() diff --git a/core/src/main/java/org/bsc/java2typescript/TypescriptConverterStatic.java b/core/src/main/java/org/bsc/java2typescript/TypescriptConverterStatic.java index 09212df..1f671f1 100644 --- a/core/src/main/java/org/bsc/java2typescript/TypescriptConverterStatic.java +++ b/core/src/main/java/org/bsc/java2typescript/TypescriptConverterStatic.java @@ -39,12 +39,12 @@ public abstract class TypescriptConverterStatic { static final String ENDL = ";\n"; public static final List PREDEFINED_TYPES = Arrays.asList( - TSType.from(Class.class), - TSType.from(Serializable.class), - TSType.from(Closeable.class), - TSType.from(AutoCloseable.class), - TSType.from(Cloneable.class), - TSType.from(RandomAccess.class) + TSType.of(Class.class), + TSType.of(Serializable.class), + TSType.of(Closeable.class), + TSType.of(AutoCloseable.class), + TSType.of(Cloneable.class), + TSType.of(RandomAccess.class) ); @@ -58,20 +58,18 @@ public abstract class TypescriptConverterStatic { * */ static BiPredicate,Type> typeParameterMatch = (declaringClass, type) -> - ( type instanceof TypeVariable ) ? - Arrays.stream(declaringClass.getTypeParameters()) - .map( (tp) -> tp.getName()) - .anyMatch( name -> name.equals(((TypeVariable)type).getName())) : - false + type instanceof TypeVariable && Arrays.stream(declaringClass.getTypeParameters()) + .map(tp -> tp.getName()) + .anyMatch(name -> name.equals(((TypeVariable) type).getName())) ; static void log( String fmt, Object ...args ) { - if( Boolean.getBoolean("debug") ) System.out.println( format( fmt, args)); + if( Boolean.getBoolean("debug") ) System.out.printf( fmt, args); } static void debug( String fmt, Object ...args ) { System.out.print( "DEBUG: "); - System.out.println( format( fmt, args)); + System.out.printf( fmt, args ); } /** * @@ -265,18 +263,19 @@ static String convertJavaToTS( Class type, return format("any /*%s*/",type.getName()); } - - /** - * - * @param type - * @param declaringMember - * @param declaredTypeMap - * @param packageResolution - * @param typeMatch - * @param onTypeMismatch - * @return - */ - public static String convertJavaToTS( + + /** + * + * @param type + * @param declaringMember + * @param declaringType + * @param declaredTypeMap + * @param packageResolution + * @param onTypeMismatch + * @param + * @return + */ + public static String convertJavaToTS( Type type, M declaringMember, TSType declaringType, @@ -290,6 +289,7 @@ public static String convertJavaToTS( Objects.requireNonNull(declaredTypeMap, "declaredTypeMap argument is null!"); log( "PROCESSING MEMEBER: [%s]", declaringMember.getName()); + /** * */ diff --git a/core/src/main/resources/headerD-rhino.ts b/core/src/main/resources/headerD-rhino.ts new file mode 100644 index 0000000..2f3421f --- /dev/null +++ b/core/src/main/resources/headerD-rhino.ts @@ -0,0 +1,53 @@ +/* + * Project: java2typescript - https://github.com/bsorrentino/java2typescript + * + * Author: bsorrentino + * + * TYPESCRIPT DEFINITIONS + * + */ + +type int = number; +type long = number; +type float = number; +type double = number; +type byte = number; +type char = string; + +type chararray = [byte]; +type bytearray = [char]; + +declare namespace java.lang { + + interface Class {} + interface AutoCloseable {} + interface Cloneable {} + + type Object = any; +} + +declare namespace java.util { + + interface RandomAccess {} +} + +declare namespace java.io { + + interface Closeable {} + interface Serializable {} +} + +// +// Rhino +// + +declare const Packages:any; + +declare function print( ...args: any[] ):void + +declare function load( module:string ):void + +// +// Generated declarations +// + diff --git a/core/src/main/resources/headerD.ts b/core/src/main/resources/headerD.ts index 9de2c13..ff195f8 100644 --- a/core/src/main/resources/headerD.ts +++ b/core/src/main/resources/headerD.ts @@ -38,13 +38,7 @@ declare namespace java.io { } // -// Rhino -// - -declare const Packages:any; - -// -// Nashorn +// Nashorn compatibility // declare function print( ...args: any[] ):void @@ -58,3 +52,8 @@ declare namespace Java { export function from( list:java.util.List ):Array ; } + +// +// Generated declarations +// + diff --git a/core/src/test/java/org/bsc/java2typescript/AbstractConverterTest.java b/core/src/test/java/org/bsc/java2typescript/AbstractConverterTest.java index a4e99e5..09c2d3c 100644 --- a/core/src/test/java/org/bsc/java2typescript/AbstractConverterTest.java +++ b/core/src/test/java/org/bsc/java2typescript/AbstractConverterTest.java @@ -20,7 +20,7 @@ public abstract class AbstractConverterTest { protected java.util.Map declaredClassMap( Class ... classes) { return Stream.of( classes ) - .collect( Collectors.toMap( c -> c.getName(), c -> TSType.from(c) )) + .collect( Collectors.toMap( c -> c.getName(), c -> TSType.of(c) )) ; } protected java.util.Map declaredTypeMap( TSType ... types) { @@ -43,7 +43,7 @@ protected String getReturnType( java.util.Map declaredClassMap, { final Type rType = m.getGenericReturnType(); final String result = TypescriptConverter.convertJavaToTS(rType, m, - TSType.from(type), + TSType.of(type), declaredClassMap, true, Optional.empty()); diff --git a/core/src/test/java/org/bsc/java2typescript/ConverterTest.java b/core/src/test/java/org/bsc/java2typescript/ConverterTest.java index c87f703..7d455a2 100644 --- a/core/src/test/java/org/bsc/java2typescript/ConverterTest.java +++ b/core/src/test/java/org/bsc/java2typescript/ConverterTest.java @@ -50,8 +50,8 @@ public void testMethod1() throws Exception { final Method m = type.getMethod("method1", java.util.Map.Entry.class); final String result = converter.getMethodParametersAndReturnDecl( m, - TSType.from(type), - declaredTypeMap( TSType.from(Map.Entry.class), TSType.from(java.util.List.class)), + TSType.of(type), + declaredTypeMap( TSType.of(Map.Entry.class), TSType.of(java.util.List.class)), true ) ; Assert.assertThat( result, IsNull.notNullValue()); @@ -69,8 +69,8 @@ public void testMethod2() throws Exception { final Method m = type.getMethod("method2", Function.class); final String result = converter.getMethodParametersAndReturnDecl( m, - TSType.from(type), - declaredTypeMap( TSType.from(Function.class), TSType.from(java.util.List.class)), + TSType.of(type), + declaredTypeMap( TSType.of(Function.class), TSType.of(java.util.List.class)), true) ; Assert.assertThat( result, IsNull.notNullValue()); @@ -83,17 +83,17 @@ public void testMethod2() throws Exception { public void functionalInterfaceTest() { - Assert.assertThat(TSType.from(java.lang.Runnable.class).isFunctional() , equalTo(true)); + Assert.assertThat(TSType.of(java.lang.Runnable.class).isFunctional() , equalTo(true)); { - TSType t = TSType.from(Consumer.class); + TSType t = TSType.of(Consumer.class); Assert.assertThat(t.isFunctional() , equalTo(true)); t.setFunctional(false); Assert.assertThat(t.isFunctional() , equalTo(true)); } - Assert.assertThat(TSType.from(Action.class).isFunctional() , equalTo(true)); + Assert.assertThat(TSType.of(Action.class).isFunctional() , equalTo(true)); { - TSType t = TSType.from(Action2.class); + TSType t = TSType.of(Action2.class); Assert.assertThat(t.isFunctional() , equalTo(false)); t.setFunctional(true); Assert.assertThat(t.isFunctional() , equalTo(false)); diff --git a/core/src/test/java/org/bsc/java2typescript/MemeberClassTest.java b/core/src/test/java/org/bsc/java2typescript/MemeberClassTest.java index 09582d4..be7c91d 100644 --- a/core/src/test/java/org/bsc/java2typescript/MemeberClassTest.java +++ b/core/src/test/java/org/bsc/java2typescript/MemeberClassTest.java @@ -22,7 +22,7 @@ public class MemeberClassTest extends AbstractConverterTest { @Test public void testMemberClassInfos() { - final TSType type = TSType.from( java.util.Map.Entry.class ); + final TSType type = TSType.of( java.util.Map.Entry.class ); Assert.assertThat(type.supportNamespace() , equalTo(true)); Assert.assertThat(type.getNamespace() , equalTo("java.util")); @@ -35,7 +35,7 @@ public void testMemberClassInfos() { public void testMemberClass() throws Exception { TypescriptConverter.Context ctx = - converter.contextOf(TSType.from( java.util.Map.Entry.class ), declaredTypeMap(), Compatibility.NASHORN); + converter.contextOf(TSType.of( java.util.Map.Entry.class ), declaredTypeMap(), Compatibility.NASHORN); Assert.assertThat(ctx, IsNull.notNullValue()); @@ -55,8 +55,8 @@ public void testMemberClassUsage() throws Exception { final Type rType = m.getGenericReturnType(); final String result = TypescriptConverter.convertJavaToTS(rType, m, - TSType.from(type), - declaredTypeMap( TSType.from(java.util.Set.class), TSType.from(java.util.Map.Entry.class)), + TSType.of(type), + declaredTypeMap( TSType.of(java.util.Set.class), TSType.of(java.util.Map.Entry.class)), true, Optional.empty()); diff --git a/core/src/test/java/org/bsc/java2typescript/ProcessorTest.java b/core/src/test/java/org/bsc/java2typescript/ProcessorTest.java index c656a6c..5000f8e 100644 --- a/core/src/test/java/org/bsc/java2typescript/ProcessorTest.java +++ b/core/src/test/java/org/bsc/java2typescript/ProcessorTest.java @@ -27,12 +27,12 @@ public class ProcessorTest extends AbstractConverterTest { @Test public void testWildcardType() throws Exception { - final TSType type = TSType.from(Sample1.class); + final TSType type = TSType.of(Sample1.class); { final Method m = type.getValue().getMethod("merge", Sample2.class ); final String result = converter.getMethodParametersAndReturnDecl( m, type, - declaredTypeMap( TSType.from(String.class), TSType.from(Sample2.class)), + declaredTypeMap( TSType.of(String.class), TSType.of(Sample2.class)), true) ; Assert.assertThat( result, IsNull.notNullValue()); @@ -42,7 +42,7 @@ public void testWildcardType() throws Exception { final Method m = type.getValue().getMethod("merge", BiConsumer.class ); final String result = converter.getMethodParametersAndReturnDecl( m, type, - declaredTypeMap( TSType.from(String.class), TSType.from(Sample2.class), TSType.from(BiConsumer.class).setExport(false).setAlias("BiConsumer")), + declaredTypeMap( TSType.of(String.class), TSType.of(Sample2.class), TSType.of(BiConsumer.class).setExport(false).setAlias("BiConsumer")), true) ; Assert.assertThat( result, IsNull.notNullValue()); @@ -52,7 +52,7 @@ public void testWildcardType() throws Exception { final Method m = type.getValue().getMethod("concatMap", Function.class ); final String result = converter.getMethodParametersAndReturnDecl( m, type, - declaredTypeMap( TSType.from(String.class), TSType.from(Sample2.class), TSType.from(Function.class).setExport(false).setAlias("Func")), + declaredTypeMap( TSType.of(String.class), TSType.of(Sample2.class), TSType.of(Function.class).setExport(false).setAlias("Func")), true) ; Assert.assertThat( result, IsNull.notNullValue()); @@ -64,13 +64,13 @@ public void testWildcardType() throws Exception { @Test public void testFunctionalInterface() throws Exception { - final TSType type = TSType.from(Sample1.class); + final TSType type = TSType.of(Sample1.class); { final Method m = type.getValue().getMethod("transform", java.util.function.Function.class ); final Type pType = m.getGenericParameterTypes()[0]; final String result = TypescriptConverter.convertJavaToTS(pType, m, type, - declaredTypeMap( TSType.from(String.class), TSType.from(java.util.function.Function.class).setAlias("Func").setExport(false)), + declaredTypeMap( TSType.of(String.class), TSType.of(java.util.function.Function.class).setAlias("Func").setExport(false)), true, Optional.empty()); Assert.assertThat( result, IsNull.notNullValue()); @@ -82,7 +82,7 @@ public void testFunctionalInterface() throws Exception { final Method m = type.getValue().getMethod("transform", java.util.function.Function.class ); final String result = converter.getMethodParametersAndReturnDecl( m, type, - declaredTypeMap( TSType.from(String.class), TSType.from(java.util.function.Function.class).setAlias("Func").setExport(false)), + declaredTypeMap( TSType.of(String.class), TSType.of(java.util.function.Function.class).setAlias("Func").setExport(false)), true) ; Assert.assertThat( result, IsNull.notNullValue()); @@ -92,7 +92,7 @@ public void testFunctionalInterface() throws Exception { final Method m = type.getValue().getMethod("creator", java.util.concurrent.Callable.class ); final String result = converter.getMethodParametersAndReturnDecl( m, type, - declaredTypeMap( TSType.from(String.class), TSType.from(java.util.concurrent.Callable.class).setExport(false).setAlias("Supplier")), + declaredTypeMap( TSType.of(String.class), TSType.of(java.util.concurrent.Callable.class).setExport(false).setAlias("Supplier")), true) ; Assert.assertThat( result, IsNull.notNullValue()); @@ -104,7 +104,7 @@ public void testFunctionalInterface() throws Exception { @Test public void testClassDecl() throws Exception { - TypescriptConverter.Context ctx = converter.contextOf(TSType.from(ArrayList.class), Collections.emptyMap(), Compatibility.NASHORN); + TypescriptConverter.Context ctx = converter.contextOf(TSType.of(ArrayList.class), Collections.emptyMap(), Compatibility.NASHORN); { final String result = ctx.getClassDecl().toString(); @@ -123,8 +123,8 @@ public void testAlias() throws Exception { final Method m = type.getMethod("getAttributeList"); final Type rType = m.getGenericReturnType(); final String result = TypescriptConverter.convertJavaToTS(rType, m, - TSType.from(type), - declaredTypeMap( TSType.from(String.class), TSType.from(java.util.List.class).setExport(false).setAlias("List") ), + TSType.of(type), + declaredTypeMap( TSType.of(String.class), TSType.of(java.util.List.class).setExport(false).setAlias("List") ), true, Optional.empty()); Assert.assertThat( result, IsNull.notNullValue()); @@ -134,8 +134,8 @@ public void testAlias() throws Exception { final Method m = type.getMethod("getAttributeList", java.util.List.class); final String result = converter.getMethodParametersAndReturnDecl( m, - TSType.from(type), - declaredTypeMap( TSType.from(String.class), TSType.from(java.util.List.class).setExport(false).setAlias("List") ), + TSType.of(type), + declaredTypeMap( TSType.of(String.class), TSType.of(java.util.List.class).setExport(false).setAlias("List") ), true) ; Assert.assertThat( result, IsNull.notNullValue()); @@ -159,7 +159,7 @@ public void testSample1() throws Exception { final Method m = type.getMethod("method4"); final Type rType = m.getGenericReturnType(); final String result = TypescriptConverter.convertJavaToTS(rType, m, - TSType.from(type), + TSType.of(type), Collections.emptyMap(), true, Optional.empty()); @@ -170,7 +170,7 @@ public void testSample1() throws Exception { final Method m = type.getMethod("method5"); final Type rType = m.getGenericReturnType(); final String result = TypescriptConverter.convertJavaToTS(rType, m, - TSType.from(type), + TSType.of(type), Collections.emptyMap(), true, Optional.empty()); @@ -181,7 +181,7 @@ public void testSample1() throws Exception { final Method m = type.getMethod("method1_1"); final Type rType = m.getGenericReturnType(); final String result = TypescriptConverter.convertJavaToTS(rType, m, - TSType.from(type), + TSType.of(type), declaredClassMap(Sample2.class), true, Optional.empty()); @@ -192,7 +192,7 @@ public void testSample1() throws Exception { final Method m = type.getMethod("method1_2"); final Type rType = m.getGenericReturnType(); final String result = TypescriptConverter.convertJavaToTS(rType, m, - TSType.from(type), + TSType.of(type), declaredClassMap(Sample2.class,java.lang.Comparable.class), true, Optional.empty()); @@ -204,8 +204,8 @@ public void testSample1() throws Exception { final Method m = type.getMethod("method1_3"); final Type rType = m.getGenericReturnType(); final String result = TypescriptConverter.convertJavaToTS(rType, m, - TSType.from(type), - declaredTypeMap( TSType.from(java.util.function.BiPredicate.class).setExport(false).setAlias("BiPredicate") ), + TSType.of(type), + declaredTypeMap( TSType.of(java.util.function.BiPredicate.class).setExport(false).setAlias("BiPredicate") ), true, Optional.of(addTypeVar)); Assert.assertThat( result, IsNull.notNullValue()); @@ -218,7 +218,7 @@ public void testSample1() throws Exception { final Method m = type.getMethod("method1_3"); final Type rType = m.getGenericReturnType(); final String result = TypescriptConverter.convertJavaToTS(rType, m, - TSType.from(type), + TSType.of(type), Collections.emptyMap(), true, Optional.empty()); @@ -229,7 +229,7 @@ public void testSample1() throws Exception { final Method m = type.getMethod("method1"); final Type rType = m.getGenericReturnType(); final String result = TypescriptConverter.convertJavaToTS(rType, m, - TSType.from(type), + TSType.of(type), declaredClassMap(java.util.Map.class), true, Optional.empty()); @@ -241,7 +241,7 @@ public void testSample1() throws Exception { final Method m = type.getMethod("method2", Sample2.class); final Type rType = m.getGenericReturnType(); final String result = TypescriptConverter.convertJavaToTS(rType, m, - TSType.from(type), + TSType.of(type), Collections.emptyMap(), true, Optional.empty()); @@ -250,7 +250,7 @@ public void testSample1() throws Exception { final Type pType = m.getParameters()[0].getParameterizedType(); final String rresult = TypescriptConverter.convertJavaToTS(pType, m, - TSType.from(type), + TSType.of(type), declaredClassMap(Sample2.class), true, Optional.empty()); @@ -262,7 +262,7 @@ public void testSample1() throws Exception { final Method m = type.getMethod("method2_1", Sample2.class); final Type rType = m.getGenericReturnType(); final String result = TypescriptConverter.convertJavaToTS(rType, m, - TSType.from(type), + TSType.of(type), declaredClassMap(Sample2.class, CharSequence.class), true, Optional.empty()); @@ -271,7 +271,7 @@ public void testSample1() throws Exception { final Type pType = m.getParameters()[0].getParameterizedType(); final String rresult = TypescriptConverter.convertJavaToTS(pType, m, - TSType.from(type), + TSType.of(type), declaredClassMap(Sample2.class), true, Optional.empty()); @@ -291,8 +291,8 @@ public void testSample1() throws Exception { final Type pType = m.getParameters()[0].getParameterizedType(); final String rresult = TypescriptConverter.convertJavaToTS(pType, m, - TSType.from(type), - declaredTypeMap( TSType.from(Sample2.class), TSType.from(Consumer.class).setExport(false).setAlias("Consumer")), + TSType.of(type), + declaredTypeMap( TSType.of(Sample2.class), TSType.of(Consumer.class).setExport(false).setAlias("Consumer")), true, Optional.empty()); Assert.assertThat( rresult, IsNull.notNullValue()); @@ -313,7 +313,7 @@ public void testSample1() throws Exception { final String[] arr = {}; final Method m = type.getMethod("method6", arr.getClass()); - final String result = converter.getMethodParametersAndReturnDecl( m, TSType.from(type), Collections.emptyMap(), true) ; + final String result = converter.getMethodParametersAndReturnDecl( m, TSType.of(type), Collections.emptyMap(), true) ; Assert.assertThat( result, IsNull.notNullValue()); Assert.assertThat( result, IsEqual.equalTo("( ...args:string[] ):void")); @@ -339,8 +339,8 @@ public void testSample1_GenericArrayType() throws Exception { final Method m = type.getMethod("genericArrayType", arr.getClass()); final String result = converter.getMethodParametersAndReturnDecl( m, - TSType.from(type), - declaredTypeMap( TSType.from(java.util.List.class)), + TSType.of(type), + declaredTypeMap( TSType.of(java.util.List.class)), true) ; Assert.assertThat( result, IsNull.notNullValue()); diff --git a/core/src/test/java/org/bsc/java2typescript/TestIssue7.java b/core/src/test/java/org/bsc/java2typescript/TestIssue7.java index f02b647..f289ef0 100644 --- a/core/src/test/java/org/bsc/java2typescript/TestIssue7.java +++ b/core/src/test/java/org/bsc/java2typescript/TestIssue7.java @@ -44,8 +44,8 @@ public void testMethod1() throws Exception { final Method m = type.getMethod("method1", Map.Entry.class); final String result = converter.getMethodParametersAndReturnDecl( m, - TSType.from(type), - declaredTypeMap( TSType.from(BiFunction.class), TSType.from(Map.Entry.class)), + TSType.of(type), + declaredTypeMap( TSType.of(BiFunction.class), TSType.of(Map.Entry.class)), true) ; Assert.assertThat( result, IsNull.notNullValue()); @@ -62,8 +62,8 @@ public void testMethod2() throws Exception { final Method m = type.getMethod("method2", java.util.List.class); final String result = converter.getMethodParametersAndReturnDecl( m, - TSType.from(type), - declaredTypeMap( TSType.from(java.util.List.class), TSType.from(Map.Entry.class)), + TSType.of(type), + declaredTypeMap( TSType.of(java.util.List.class), TSType.of(Map.Entry.class)), true) ; Assert.assertThat( result, IsNull.notNullValue()); @@ -82,8 +82,8 @@ public void testMethod3() throws Exception { final Method m = type.getMethod("method3"); final String result = converter.getMethodParametersAndReturnDecl( m, - TSType.from(type), - declaredTypeMap( TSType.from(Map.Entry.class)), + TSType.of(type), + declaredTypeMap( TSType.of(Map.Entry.class)), true) ; Assert.assertThat( result, IsNull.notNullValue()); @@ -102,8 +102,8 @@ public void testConstructor() throws Exception { final Constructor m = type.getConstructor(); final String result = converter.getMethodParametersAndReturnDecl( m, - TSType.from(type), - declaredTypeMap( TSType.from(Map.Entry.class), TSType.from(type) ), + TSType.of(type), + declaredTypeMap( TSType.of(Map.Entry.class), TSType.of(type) ), true) ; Assert.assertThat( result, IsNull.notNullValue()); @@ -120,8 +120,8 @@ public void testReduceEntries() throws Exception { final Method m = type.getMethod("reduceEntries", Long.TYPE, BiFunction.class); final String result = converter.getMethodParametersAndReturnDecl( m, - TSType.from(type), - declaredTypeMap( TSType.from(BiFunction.class), TSType.from(Map.Entry.class)), + TSType.of(type), + declaredTypeMap( TSType.of(BiFunction.class), TSType.of(Map.Entry.class)), true) ; Assert.assertThat( result, IsNull.notNullValue()); diff --git a/pom.xml b/pom.xml index 185bf61..c30ff3b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,30 +1,36 @@ - + 4.0.0 org.bsc.processor java2ts-processor-parent - 1.2.0 + 1.3.0-SNAPSHOT pom - java2ts-processor::parent - ${project.version} + java2ts-processor::parent - Java Processor to generate Typescript Definition from Java classes - This is to help developing on JVM javascript engine (ie Nashorn/Rhino) using Typescript + Java Processor to generate Typescript Definition from Java classes - This is to help developing on JVM + javascript engine (ie Nashorn/Rhino) using Typescript https://github.com/bsorrentino/java2typescript + central ${release.repo.id} ${release.repo.url} + snapshot ${snapshot.repo.id} ${snapshot.repo.url} - false + + scm:git:https://github.com/bsorrentino/java2typescript.git @@ -34,12 +40,12 @@ 2017 - - - The MIT License - https://opensource.org/licenses/MIT - - + + + The MIT License + https://opensource.org/licenses/MIT + + @@ -56,18 +62,12 @@ 1.8 1.8 UTF-8 - - - sonatype-repo - https://oss.sonatype.org/content/repositories/snapshots - sonatype-repo - https://oss.sonatype.org/service/local/staging/deploy/maven2 - processor archetype samples + samples.rhino core @@ -87,92 +87,92 @@ compiler 0.9.4 - - org.glassfish - javax.json - 1.1.2 - - org.bsc - jvm-npm-core - 1.1.0 + org.glassfish + javax.json + 1.1.2 + + + org.bsc + jvm-npm-core + 1.1.0 - junit - junit - 4.12 - test + junit + junit + 4.13.1 + test - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.22.1 - - - - org.apache.maven.plugins - maven-archetype-plugin - 3.0.1 - - - - org.apache.maven.plugins - maven-resources-plugin - 3.0.2 - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.8.0 - - true - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - -Xdoclint:none - ${project.build.directory}/apidocs - - - - - org.apache.maven.plugins - maven-deploy-plugin - 2.8.2 - - - - org.apache.maven.plugins - maven-source-plugin - 3.0.1 - - - - org.bsc.maven - maven-processor-plugin - 3.3.3 - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + + + org.apache.maven.plugins + maven-archetype-plugin + 3.0.1 + + + + org.apache.maven.plugins + maven-resources-plugin + 3.0.2 + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + true + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.9.1 + + -Xdoclint:none + ${project.build.directory}/apidocs + + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.8.2 + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.1 + + + + org.bsc.maven + maven-processor-plugin + 3.3.3 + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + + @@ -221,10 +221,10 @@ - - --pinentry-mode - loopback - + + --pinentry-mode + loopback + @@ -235,7 +235,7 @@ sonatype-server https://oss.sonatype.org/ - + diff --git a/processor/pom.xml b/processor/pom.xml index 0a0ad87..b73f6e1 100644 --- a/processor/pom.xml +++ b/processor/pom.xml @@ -2,12 +2,12 @@ 4.0.0 java2ts-processor - java2ts-processor::processor - ${project.version} + java2ts-processor::processor org.bsc.processor java2ts-processor-parent - 1.2.0 + 1.3.0-SNAPSHOT diff --git a/processor/src/main/java/org/bsc/processor/AbstractProcessorEx.java b/processor/src/main/java/org/bsc/processor/AbstractProcessorEx.java index 1572204..783808e 100644 --- a/processor/src/main/java/org/bsc/processor/AbstractProcessorEx.java +++ b/processor/src/main/java/org/bsc/processor/AbstractProcessorEx.java @@ -69,14 +69,26 @@ public final java.util.Map getOptionMap() { return processingEnv.getOptions(); } + + /** + * + * @return + */ + public java.util.List elementFromAnnotations( ) { + + return annotations.stream() + .flatMap((e) -> roundEnv.getElementsAnnotatedWith(e).stream()) + .collect(Collectors.toList()); + } + /** * * @return */ - public java.util.List elementFromAnnotations( Optional> filter) { + public java.util.List elementFromAnnotationsWithFilter( Predicate filter ) { return annotations.stream() - .filter( filter.orElse((e) -> true) ) + .filter( filter ) .flatMap((e) -> roundEnv.getElementsAnnotatedWith(e).stream() ) .collect( Collectors.toList()); } @@ -129,12 +141,12 @@ protected FileObject getSourceOutputFile(Path subfolder, @Override public final boolean process(Set annotations, RoundEnvironment roundEnv) { - info("PROCESSOR START"); - - if (roundEnv.processingOver()) { + if (roundEnv.processingOver() || annotations.isEmpty() ) { return false; } + info("PROCESSOR [%s] START", getClass().getName()); + final Context processinContext = new Context(annotations, roundEnv); try { diff --git a/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java b/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java index 981bf8b..fd6ad0b 100644 --- a/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java +++ b/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java @@ -1,16 +1,9 @@ package org.bsc.processor; -import static org.bsc.java2typescript.TypescriptConverter.PREDEFINED_TYPES; - -import java.io.IOException; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; -import java.util.Set; -import java.util.function.Consumer; -import java.util.stream.Collectors; +import org.bsc.java2typescript.TSNamespace; +import org.bsc.java2typescript.TSType; +import org.bsc.java2typescript.TypescriptConverter; +import org.bsc.processor.annotation.Java2TS; import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedOptions; @@ -19,11 +12,19 @@ import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.ElementKind; -import javax.lang.model.element.ExecutableElement; import javax.tools.FileObject; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.stream.Collectors; -import org.bsc.java2typescript.TSType; -import org.bsc.java2typescript.TypescriptConverter;; +import static java.util.Optional.*; +import static org.bsc.java2typescript.TypescriptConverter.PREDEFINED_TYPES; + +; /** * @@ -38,32 +39,32 @@ public class TypescriptProcessor extends AbstractProcessorEx { final static String ENDL = ";\n"; static final List REQUIRED_TYPES = Arrays.asList( - TSType.from(java.lang.String.class).setExport(true), - TSType.from(java.lang.Iterable.class).setExport(true).setFunctional(true), - TSType.from(java.util.Iterator.class), - TSType.from(java.util.Collection.class), - TSType.from(java.util.List.class), - TSType.from(java.util.Set.class), - TSType.from(java.util.Map.class), - TSType.from(java.util.Optional.class).setExport(true), - TSType.from(java.util.stream.Stream.class).setExport(true), + TSType.of(java.lang.String.class).setExport(true), + TSType.of(java.lang.Iterable.class).setExport(true).setFunctional(true), + TSType.of(java.util.Iterator.class), + TSType.of(java.util.Collection.class), + TSType.of(java.util.List.class), + TSType.of(java.util.Set.class), + TSType.of(java.util.Map.class), + TSType.of(java.util.Optional.class).setExport(true), + TSType.of(java.util.stream.Stream.class).setExport(true), // Utility class(s) - TSType.from(java.util.stream.Collectors.class).setExport(true), - TSType.from(java.util.Collections.class).setExport(true), + TSType.of(java.util.stream.Collectors.class).setExport(true), + TSType.of(java.util.Collections.class).setExport(true), // Native functional interface(s) - TSType.from(java.util.function.Function.class).setAlias("Func"), - TSType.from(java.util.function.BiFunction.class).setAlias("BiFunction"), - TSType.from(java.util.function.Consumer.class).setAlias( "Consumer"), - TSType.from(java.util.function.BiConsumer.class).setAlias("BiConsumer"), - TSType.from(java.util.function.UnaryOperator.class).setAlias("UnaryOperator"), - TSType.from(java.util.function.BinaryOperator.class).setAlias("BinaryOperator"), - TSType.from(java.util.function.Supplier.class).setAlias("Supplier"), - TSType.from(java.util.function.Predicate.class).setAlias("Predicate"), - TSType.from(java.util.function.BiPredicate.class).setAlias("BiPredicate"), - TSType.from(java.lang.Runnable.class), - TSType.from(java.lang.Comparable.class) + TSType.of(java.util.function.Function.class).setAlias("Func"), + TSType.of(java.util.function.BiFunction.class).setAlias("BiFunction"), + TSType.of(java.util.function.Consumer.class).setAlias( "Consumer"), + TSType.of(java.util.function.BiConsumer.class).setAlias("BiConsumer"), + TSType.of(java.util.function.UnaryOperator.class).setAlias("UnaryOperator"), + TSType.of(java.util.function.BinaryOperator.class).setAlias("BinaryOperator"), + TSType.of(java.util.function.Supplier.class).setAlias("Supplier"), + TSType.of(java.util.function.Predicate.class).setAlias("Predicate"), + TSType.of(java.util.function.BiPredicate.class).setAlias("BiPredicate"), + TSType.of(java.lang.Runnable.class), + TSType.of(java.lang.Comparable.class) // Declare Functional Interface(s) ); @@ -97,14 +98,23 @@ private java.io.Writer openFile( Path file, String header ) throws IOException { @Override public boolean process( Context processingContext ) throws Exception { - final String targetDefinitionFile = processingContext.getOptionMap().getOrDefault("ts.outfile", "out"); + final String targetDefinitionFile = processingContext.getOptionMap().getOrDefault("ts.outfile", "out"); //final String compatibility = processingContext.getOptionMap().getOrDefault("compatibility", "nashorn"); - final String definitionsFile = targetDefinitionFile.concat(".d.ts"); + final String definitionsFile= targetDefinitionFile.concat(".d.ts"); final String typesFile = targetDefinitionFile.concat("-types.ts"); - - try( - final java.io.Writer wD = openFile( Paths.get(definitionsFile), "headerD.ts" ); + + final String compatibilityOption = + processingContext.getOptionMap() + .getOrDefault("compatibility", "NASHORN") + .toUpperCase(); + info("COMPATIBILITY WITH [%s]", compatibilityOption ); + + final TypescriptConverter converter = + new TypescriptConverter( TypescriptConverter.Compatibility.valueOf(compatibilityOption)); + + try( + final java.io.Writer wD = openFile( Paths.get(definitionsFile), converter.isRhino() ? "headerD-rhino.ts" : "headerD.ts" ); final java.io.Writer wT = openFile( Paths.get(typesFile), "headerT.ts" ); ) { @@ -123,79 +133,80 @@ public boolean process( Context processingContext ) throws Exception { } }; - final Set types = enumerateDeclaredPackageAndClass( processingContext ); - - types.addAll(REQUIRED_TYPES); - - types.addAll(PREDEFINED_TYPES); - - final java.util.Map declaredTypes = - types.stream() - .collect( Collectors.toMap( tt -> tt.getValue().getName() , tt -> tt )); - - final String compatibilityOption = - processingContext.getOptionMap() - .getOrDefault("compatibility", "NASHORN") - .toUpperCase(); - info("COMPATIBILITY WITH [%s]", compatibilityOption ); - - final TypescriptConverter converter = - new TypescriptConverter( TypescriptConverter.Compatibility.valueOf(compatibilityOption)); - + final List namespaces = enumerateDeclaredPackageAndClass( processingContext ); + + final Set types = new HashSet<>(PREDEFINED_TYPES); + types.addAll(REQUIRED_TYPES); + + namespaces.forEach( ns -> types.addAll( ns.getTypes() ) ); + + final java.util.Map declaredTypes = + types.stream() + .collect( Collectors.toMap( tt -> tt.getValue().getName() , tt -> tt )); + types.stream() - .filter( tt -> !PREDEFINED_TYPES.contains(tt) ) - .map( tt -> converter.processClass( 0, tt, declaredTypes)) - .forEach( wD_append ); + .filter( tt -> !PREDEFINED_TYPES.contains(tt) ) + .map( tt -> converter.processClass( 0, tt, declaredTypes)) + .forEach( wD_append ); + + wT_append.accept( String.format( "/// \n\n", definitionsFile ) ); - wT.append( "/// " ).append( "\n\n"); types.stream() - .filter( t -> t.isExport() ) - .map( t -> converter.processStatic( t, declaredTypes)) - .forEach( wT_append ); + .filter( t -> t.isExport() ) + .map( t -> converter.processStatic( t, declaredTypes)) + .forEach( wT_append ); } // end try-with-resources return true; } - /** - * - * @param entry - * @return - */ - @SuppressWarnings("unchecked") - private List getAnnotationValueValue( - java.util.Map.Entry entry ) - { + private boolean isJava2TS( AnnotationMirror am ) { + + info( "'%s'='%s'", am.getAnnotationType().toString(), Java2TS.class.getName()); + return am.getAnnotationType().toString().equals( Java2TS.class.getName() ); + + } + + private TSNamespace toNamespace( AnnotationMirror am ) { + + final Function> mapTypes = ( value ) -> + ((List)value.getValue()) + .stream() + .map( av -> av.getValue() ) + .filter( v -> v instanceof AnnotationMirror) + .map( v -> toMapObject(( AnnotationMirror)v, TSType::of ) ) + .collect( Collectors.toSet() ); + + + final String name = ofNullable( am.getElementValues().get( "name" ) ) + .map( v -> String.valueOf(v.getValue()) ) + .orElse(""); + + final Set types = ofNullable(am.getElementValues().get( "declare" )) + .map( mapTypes ) + .orElse( Collections.emptySet() ); + + return TSNamespace.of( name, types ); + + } - final AnnotationValue av = entry.getValue(); - return (List)av.getValue(); - } - /** * * @param processingContext * @return */ - private java.util.Set enumerateDeclaredPackageAndClass( final Context processingContext ) { + private List enumerateDeclaredPackageAndClass( final Context processingContext ) { return - processingContext.elementFromAnnotations( Optional.empty() ).stream() - .peek( e -> info( "Anotation [%s]", e.getKind().name()) ) - .filter( e -> ElementKind.PACKAGE==e.getKind() || ElementKind.CLASS==e.getKind() ) - .flatMap( e -> e.getAnnotationMirrors().stream() ) - .peek( m -> info( "Mirror [%s]", m.toString() )) - .flatMap( am -> am.getElementValues() - .entrySet() - .stream() - .filter( entry -> "declare".equals(String.valueOf(entry.getKey().getSimpleName())) )) - .flatMap( entry -> this.getAnnotationValueValue(entry).stream() ) - .map( av -> av.getValue() ) - .filter( v -> v instanceof AnnotationMirror).map( v -> ((AnnotationMirror)v) ) - .map( am -> toMapObject(am, () -> TSType.from( Void.class) ) ) - .collect( Collectors.toSet() ) - ; + processingContext.elementFromAnnotations().stream() + .peek( e -> info( "Annotation [%s]", e.getKind().name()) ) + .filter( e -> ElementKind.PACKAGE==e.getKind() || ElementKind.CLASS==e.getKind() ) + .flatMap( e -> e.getAnnotationMirrors().stream().filter( this::isJava2TS ) ) + .map( this::toNamespace ) + .collect( Collectors.toList() ) + ; } } diff --git a/processor/src/main/java/org/bsc/processor/annotation/Java2TS.java b/processor/src/main/java/org/bsc/processor/annotation/Java2TS.java index db1c62d..7547dd1 100644 --- a/processor/src/main/java/org/bsc/processor/annotation/Java2TS.java +++ b/processor/src/main/java/org/bsc/processor/annotation/Java2TS.java @@ -13,6 +13,6 @@ @Retention(RetentionPolicy.SOURCE) @Target( {ElementType.TYPE, ElementType.PACKAGE} ) public @interface Java2TS { - + String name() default ""; Type[] declare() default {}; } \ No newline at end of file diff --git a/samples.rhino/pom.xml b/samples.rhino/pom.xml index a461268..0557e02 100644 --- a/samples.rhino/pom.xml +++ b/samples.rhino/pom.xml @@ -1,14 +1,14 @@ 4.0.0 - sample - java2ts-processor::sample - ${project.version} - jar + rhino-sample + java2ts-processor::rhino::sample + rhino sample org.bsc.processor java2ts-processor-parent - 1.2.0-SNAPSHOT + 1.3.0-SNAPSHOT @@ -76,7 +76,7 @@ org.mozilla rhino - 1.7.11 + 1.7.12 diff --git a/samples/pom.xml b/samples/pom.xml index 980ad81..a4413cf 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -5,7 +5,7 @@ org.bsc.processor java2ts-processor-parent - 1.2.0 + 1.3.0-SNAPSHOT graaljs-sample @@ -14,7 +14,7 @@ 1.0.0-rc16 - 19.0.0 + 19.3.1 ${project.build.directory}/compiler app.js diff --git a/settings-template.xml b/settings-template.xml new file mode 100644 index 0000000..17de78e --- /dev/null +++ b/settings-template.xml @@ -0,0 +1,45 @@ + + + + + + + sonatype-server + bsorrentino + ${env.SONATYPE_PASSWORD} + + + + + + + + sonatype + + true + + + + sonatype-server + https://oss.sonatype.org/service/local/staging/deploy/maven2 + sonatype-server + https://oss.sonatype.org/content/repositories/snapshots + http://oss.sonatype.org/content/groups/public + + + + + + + + + +