From bd291e321920cfd768d0ad6b55967cba9f2f8898 Mon Sep 17 00:00:00 2001 From: bartolomeo sorrentino Date: Sun, 11 Oct 2020 23:46:10 +0200 Subject: [PATCH] #6 add namespace class management --- .../org/bsc/java2typescript/TSNamespace.java | 28 +++ .../java/org/bsc/java2typescript/TSType.java | 10 +- .../java2typescript/TypescriptConverter.java | 6 +- .../TypescriptConverterStatic.java | 12 +- .../AbstractConverterTest.java | 4 +- .../bsc/java2typescript/ConverterTest.java | 16 +- .../bsc/java2typescript/MemeberClassTest.java | 8 +- .../bsc/java2typescript/ProcessorTest.java | 60 +++--- .../org/bsc/java2typescript/TestIssue7.java | 20 +- .../bsc/processor/AbstractProcessorEx.java | 16 +- .../bsc/processor/TypescriptProcessor.java | 176 ++++++++++-------- .../org/bsc/processor/annotation/Java2TS.java | 2 +- 12 files changed, 211 insertions(+), 147 deletions(-) create mode 100644 core/src/main/java/org/bsc/java2typescript/TSNamespace.java 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 7e42c26..0adf330 100644 --- a/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java +++ b/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java @@ -264,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)); @@ -275,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); @@ -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)); diff --git a/core/src/main/java/org/bsc/java2typescript/TypescriptConverterStatic.java b/core/src/main/java/org/bsc/java2typescript/TypescriptConverterStatic.java index 9dcabbb..5fa5eb7 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) ); 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/processor/src/main/java/org/bsc/processor/AbstractProcessorEx.java b/processor/src/main/java/org/bsc/processor/AbstractProcessorEx.java index 1572204..dc85254 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()); } diff --git a/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java b/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java index a5bcbbe..d087ea2 100644 --- a/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java +++ b/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java @@ -1,29 +1,31 @@ package org.bsc.processor; -import static org.bsc.java2typescript.TypescriptConverter.PREDEFINED_TYPES; +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; +import javax.annotation.processing.SupportedSourceVersion; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.AnnotationValue; +import javax.lang.model.element.ElementKind; +import javax.tools.FileObject; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; +import java.util.HashSet; import java.util.List; -import java.util.Optional; import java.util.Set; import java.util.function.Consumer; import java.util.stream.Collectors; -import javax.annotation.processing.SupportedAnnotationTypes; -import javax.annotation.processing.SupportedOptions; -import javax.annotation.processing.SupportedSourceVersion; -import javax.lang.model.SourceVersion; -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 static org.bsc.java2typescript.TypescriptConverter.PREDEFINED_TYPES; -import org.bsc.java2typescript.TSType; -import org.bsc.java2typescript.TypescriptConverter;; +; /** * @@ -38,32 +40,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,7 +99,7 @@ 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"); @@ -132,70 +134,84 @@ public boolean process( Context processingContext ) throws Exception { } }; - final Set types = enumerateDeclaredPackageAndClass( processingContext ); + final List namespaces = 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 Set types = new HashSet<>(); + types.addAll(REQUIRED_TYPES); + types.addAll(PREDEFINED_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 AnnotationValue nameValue = am.getElementValues().get( "name" ); + + final AnnotationValue typesValue = am.getElementValues().get( "declare" ); + + final String name = (nameValue!=null) ? String.valueOf(nameValue.getValue()) : ""; + + final Object typesValueValues = (typesValue!=null ) ? typesValue.getValue() : null; + + if( typesValueValues instanceof List ) { + final Set types = ((List)typesValueValues) + .stream() + .map( av -> av.getValue() ) + .filter( v -> v instanceof AnnotationMirror) + .map( v -> toMapObject(( AnnotationMirror)v, TSType::of ) ) + .collect( Collectors.toSet() ); + + return TSNamespace.of( name, types); + + } + return TSNamespace.of( name, new HashSet() ); + + } - 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