From e69e29eb38aff0b69b2a7ac0b3fd865acb197ea0 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Mon, 21 May 2018 21:53:50 +0200 Subject: [PATCH 1/2] add functional management --- .../java/org/bsc/java2typescript/TSType.java | 41 +++++++++++++++---- .../org/bsc/processor/annotation/Type.java | 5 ++- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/org/bsc/java2typescript/TSType.java b/core/src/main/java/org/bsc/java2typescript/TSType.java index 231deea..47dfcfb 100644 --- a/core/src/main/java/org/bsc/java2typescript/TSType.java +++ b/core/src/main/java/org/bsc/java2typescript/TSType.java @@ -21,6 +21,7 @@ public class TSType extends HashMap { private static final String VALUE = "value"; private static final String EXPORT = "export"; private static final String NAMESPACE = "namespace"; + private static final String FUNCTIONAL = "functional"; protected TSType() { super(3); @@ -53,6 +54,7 @@ public static TSType from(Class cl) { }; } + /** * * @return @@ -73,8 +75,8 @@ public boolean isExport() { * * @return */ - public TSType setExport(boolean exports) { - super.put(EXPORT, exports); + public TSType setExport(boolean value) { + super.put(EXPORT, value); return this; } @@ -95,6 +97,34 @@ public String getAlias() { return (String) super.get(ALIAS); } + /** + * + * @return + */ + public boolean isFunctionalInterface() { + + + if( !getValue().isInterface()) return false; + if( getValue().isAnnotationPresent(FunctionalInterface.class)) return true; + + //return Arrays.stream(c.getDeclaredMethods()) + // .filter( m -> Modifier.isAbstract(m.getModifiers()) ) + // .count() == 1; + + return (Boolean)super.getOrDefault( FUNCTIONAL, false); + } + + /** + * + */ + public TSType setFunctionalInterface( boolean value ) { + + super.put(FUNCTIONAL, value); + return this; + + } + + private String getMemberSimpleTypeName() { return format( "%s$%s", getValue().getDeclaringClass().getSimpleName(), getValue().getSimpleName()); @@ -134,13 +164,6 @@ public final String getNamespace() { return (String) super.getOrDefault(NAMESPACE, getValue().getPackage().getName()); } - /** - * - * @return - */ - public boolean isFunctionalInterface() { - return TypescriptConverter.isFunctionalInterface( getValue() ); - } /** * * @param type diff --git a/processor/src/main/java/org/bsc/processor/annotation/Type.java b/processor/src/main/java/org/bsc/processor/annotation/Type.java index 7cc357c..1e80756 100644 --- a/processor/src/main/java/org/bsc/processor/annotation/Type.java +++ b/processor/src/main/java/org/bsc/processor/annotation/Type.java @@ -9,6 +9,7 @@ @Target( {ElementType.ANNOTATION_TYPE} ) public @interface Type { Class value(); - boolean export() default false ; - String alias() default ""; + boolean export() default false ; + String alias() default ""; + boolean functional() default false; } From e451c033628ab0312c0987797ac19b608d271ce2 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Mon, 21 May 2018 21:56:39 +0200 Subject: [PATCH 2/2] improve logging + fix wildcardtype issue + new functional model --- .../java2typescript/TypescriptConverter.java | 54 ++++++-------- .../bsc/java2typescript/ConverterTest.java | 72 +++++++++++++++++-- 2 files changed, 89 insertions(+), 37 deletions(-) diff --git a/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java b/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java index 1fbfc9a..5828658 100644 --- a/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java +++ b/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java @@ -72,18 +72,6 @@ protected final static void debug( String fmt, Object ...args ) { System.out.println( format( fmt, args)); } - - /** - * - * @param type - * @return - */ - public static final String processFunctionalInterface( TSType type ) { - Objects.requireNonNull(type, "argument 'type' is not defined!"); - - return null; - } - /** * * @param p @@ -123,17 +111,6 @@ static boolean isFactoryMethod( Method m ) { return (isStatic(m) && m.getReturnType().equals(m.getDeclaringClass())); } - - /** - * - * @return - */ - public static boolean isFunctionalInterface( final Class c ) { - if( !c.isInterface()) return false; - if( c.isAnnotationPresent(FunctionalInterface.class)) return true; - - return Arrays.stream(c.getDeclaredMethods()).filter( m -> Modifier.isAbstract(m.getModifiers()) ).count() == 1; - } /** * @@ -205,6 +182,7 @@ public static String convertJavaToTS( Objects.requireNonNull(declaringType, "declaringType argument is null!"); Objects.requireNonNull(declaredTypeMap, "declaredTypeMap argument is null!"); + log( "PROCESSING MEMEBER: [%s]", declaringMember.getName()); /** * */ @@ -247,6 +225,7 @@ public static String convertJavaToTS( final Type[] typeArgs = pType.getActualTypeArguments(); for( Type t : typeArgs ) { + log( "TypeArgs [%s]", t.getTypeName()); if( t instanceof ParameterizedType ) { @@ -256,8 +235,8 @@ public static String convertJavaToTS( declaredTypeMap, packageResolution, onTypeMismatch); - log( "Parameterized Type %s - %s", t.getTypeName(), typeName ); result = result.replace( t.getTypeName(), typeName); + log( "Parameterized Type\n\t%s\n\t%s\n\t%s", t.getTypeName(), typeName, result ); } else if( t instanceof TypeVariable ) { @@ -281,7 +260,6 @@ else if( t instanceof TypeVariable ) { continue; } else if( t instanceof Class ) { - log( "class: %s", t.getTypeName() ); final String name = convertJavaToTS( (Class)t, declaringType, declaredTypeMap, packageResolution, onTypeMismatch); @@ -290,14 +268,16 @@ else if( t instanceof Class ) { .replace(t.getTypeName(), name) .replace( "/*@*/", commented ) ; - } + log( "Class Type\n\t%s\n\t%s", t.getTypeName(), result ); + } else if( t instanceof WildcardType ) { + final WildcardType wt = (WildcardType) t; final Type[] lb = wt.getLowerBounds(); final Type[] ub = wt.getUpperBounds(); - - log( "Wildcard Type : %s lb:%d up:%d", wt.getTypeName(), lb.length, ub.length ); + + log( "Wildcard Type: \n\t%s\n\tlb:%d\n\tup:%d", wt.getTypeName(), lb.length, ub.length ); if( lb.length <= 1 && ub.length==1) { final Type tt = (lb.length==1) ? lb[0] : ub[0]; @@ -312,6 +292,7 @@ else if( t instanceof WildcardType ) { result = result.replace( wt.getTypeName(), s); if( tt instanceof ParameterizedType ) { + // FIX ISSUE #7 result = result.replace( "? extends ", "" ); @@ -321,13 +302,20 @@ else if( t instanceof WildcardType ) { { final Class clazz = (Class) (((ParameterizedType)tt).getRawType()); - final String typeName = wt.getTypeName().replace( clazz.getName(), clazz.getSimpleName()); + final String typeName = wt.getTypeName() + //.replace( clazz.getName(), clazz.getSimpleName()) + .replace( "? extends ", "" ) + ; result = result.replace( typeName, s); + } + } + log( "Wildcard Type(1):\n\t%s\n\t%s\n\t%s", t.getTypeName(), s, result ); } else { result = result.replace(wt.getTypeName(), format( "any/*%s*/", wt)); + log( "Wildcard Type(2)\n\t%s\n\t%s", t.getTypeName(), result ); } } else if( t instanceof GenericArrayType ) { @@ -358,8 +346,10 @@ else if( type instanceof TypeVariable ) { return type.getTypeName(); } else if( type instanceof Class ) { - log( "class: %s", type.getTypeName() ); - return convertJavaToTS( (Class)type, declaringType, declaredTypeMap, packageResolution, onTypeMismatch); + final String result = convertJavaToTS( (Class)type, declaringType, declaredTypeMap, packageResolution, onTypeMismatch); + log( "class:\n\t%s\n\t%s", type.getTypeName(), result ); + return result; + } else if( type instanceof WildcardType ) { throw new IllegalArgumentException( "type 'WildcardType' is a not supported yet!"); @@ -516,7 +506,7 @@ public String processStatic( TSType type, java.util.Map declared //Append class property ctx.append("\treadonly class:any;\n"); - if( isFunctionalInterface( type.getValue() ) ) { + if( type.isFunctionalInterface() ) { final java.util.Set TypeVarSet = new java.util.HashSet<>(5); final String tstype = convertJavaToTS( type.getValue(), diff --git a/core/src/test/java/org/bsc/java2typescript/ConverterTest.java b/core/src/test/java/org/bsc/java2typescript/ConverterTest.java index cc07d67..9576538 100644 --- a/core/src/test/java/org/bsc/java2typescript/ConverterTest.java +++ b/core/src/test/java/org/bsc/java2typescript/ConverterTest.java @@ -4,13 +4,19 @@ import static org.hamcrest.core.IsEqual.equalTo; +import java.lang.reflect.Method; +import java.util.Map; import java.util.function.Consumer; +import java.util.function.Function; +import org.hamcrest.core.IsEqual; +import org.hamcrest.core.IsNull; import org.junit.Assert; import org.junit.Test; public class ConverterTest extends AbstractConverterTest { + @FunctionalInterface interface Action { default void m1() {}; @@ -23,15 +29,71 @@ interface Action2 { void apply(); } + + interface TestBean { + + void method1( Map.Entry> p1 ); + void method2( Function> p1 ); + } + + @Test + public void testMethod1() throws Exception { + final Class type = TestBean.class; + + + { + 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)), + true) ; + + Assert.assertThat( result, IsNull.notNullValue()); + Assert.assertThat( result, IsEqual.equalTo("( p1:java.util.Map$Entry> ):void")); + } + + + } + @Test + public void testMethod2() throws Exception { + final Class type = TestBean.class; + + + { + 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)), + true) ; + + Assert.assertThat( result, IsNull.notNullValue()); + Assert.assertThat( result, IsEqual.equalTo("( p1:java.util.function.Function> ):void")); + } + + } + @Test public void functionalInterfaceTest() { - Assert.assertThat(TypescriptConverter.isFunctionalInterface(java.lang.Runnable.class) , equalTo(true)); - Assert.assertThat(TypescriptConverter.isFunctionalInterface(Consumer.class) , equalTo(true)); - Assert.assertThat(TypescriptConverter.isFunctionalInterface(Action.class) , equalTo(true)); - Assert.assertThat(TypescriptConverter.isFunctionalInterface(Action.class), equalTo(true)); - Assert.assertThat(TypescriptConverter.isFunctionalInterface(Action2.class) , equalTo(false)); + Assert.assertThat(TSType.from(java.lang.Runnable.class).isFunctionalInterface() , equalTo(true)); + { + TSType t = TSType.from(Consumer.class); + Assert.assertThat(t.isFunctionalInterface() , equalTo(true)); + t.setFunctionalInterface(false); + Assert.assertThat(t.isFunctionalInterface() , equalTo(true)); + + } + Assert.assertThat(TSType.from(Action.class).isFunctionalInterface() , equalTo(true)); + { + TSType t = TSType.from(Action2.class); + Assert.assertThat(t.isFunctionalInterface() , equalTo(false)); + t.setFunctionalInterface(true); + Assert.assertThat(t.isFunctionalInterface() , equalTo(true)); + } + } }