From 010d3a24efd101fae9d5ba4ee65cf03ef158a356 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Fri, 18 May 2018 16:45:39 +0200 Subject: [PATCH 1/2] fix #7 --- .../java2typescript/TypescriptConverter.java | 24 +++--- .../bsc/java2typescript/ProcessorTest.java | 3 + .../java/org/bsc/java2typescript/Sample1.java | 2 + .../org/bsc/java2typescript/TestIssue7.java | 77 +++++++++++++++++++ .../org/bsc/java2ts/jdk8/package-info.java | 2 +- 5 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 core/src/test/java/org/bsc/java2typescript/TestIssue7.java diff --git a/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java b/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java index 044effe..1defb1b 100644 --- a/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java +++ b/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java @@ -248,6 +248,7 @@ public static String convertJavaToTS( Type type, final Type[] typeArgs = pType.getActualTypeArguments(); for( Type t : typeArgs ) { + if( t instanceof ParameterizedType ) { final String typeName = convertJavaToTS( t, @@ -298,7 +299,7 @@ else if( t instanceof WildcardType ) { final Type[] lb = wt.getLowerBounds(); final Type[] ub = wt.getUpperBounds(); - log( "Wildcard Type : %s lb:%d up:%d", type.getTypeName(), lb.length, ub.length ); + log( "Wildcard Type : %s lb:%d up:%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,14 +313,19 @@ else if( t instanceof WildcardType ) { result = result.replace( wt.getTypeName(), s); - // CHECK FOR NESTED WILDCARDTYPE - if( tt instanceof ParameterizedType && - Stream.of(((ParameterizedType)tt).getActualTypeArguments()) - .anyMatch( arg -> (arg instanceof WildcardType) )) - { - final Class clazz = (Class) (((ParameterizedType)tt).getRawType()); - final String typeName = wt.getTypeName().replace( clazz.getName(), clazz.getSimpleName()); - result = result.replace( typeName, s); + if( tt instanceof ParameterizedType ) { + // FIX ISSUE #7 + result = result.replace( "? extends ", "" ); + + // CHECK FOR NESTED WILDCARDTYPE + if( Stream.of(((ParameterizedType)tt).getActualTypeArguments()) + .anyMatch( arg -> (arg instanceof WildcardType) )) + { + final Class clazz = (Class) (((ParameterizedType)tt).getRawType()); + + final String typeName = wt.getTypeName().replace( clazz.getName(), clazz.getSimpleName()); + result = result.replace( typeName, s); + } } } else { diff --git a/core/src/test/java/org/bsc/java2typescript/ProcessorTest.java b/core/src/test/java/org/bsc/java2typescript/ProcessorTest.java index 14226a7..224070f 100644 --- a/core/src/test/java/org/bsc/java2typescript/ProcessorTest.java +++ b/core/src/test/java/org/bsc/java2typescript/ProcessorTest.java @@ -5,8 +5,10 @@ import java.lang.reflect.TypeVariable; import java.util.ArrayList; import java.util.Collections; +import java.util.Map; import java.util.Optional; import java.util.function.BiConsumer; +import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Stream; @@ -343,4 +345,5 @@ public void testSample1_GenericArrayType() throws Exception { } + } diff --git a/core/src/test/java/org/bsc/java2typescript/Sample1.java b/core/src/test/java/org/bsc/java2typescript/Sample1.java index bcf467f..bf8efbe 100644 --- a/core/src/test/java/org/bsc/java2typescript/Sample1.java +++ b/core/src/test/java/org/bsc/java2typescript/Sample1.java @@ -46,4 +46,6 @@ public interface Sample1 { T concatMap(Function> mapper); java.util.List genericArrayType( E[] c ); + + } diff --git a/core/src/test/java/org/bsc/java2typescript/TestIssue7.java b/core/src/test/java/org/bsc/java2typescript/TestIssue7.java new file mode 100644 index 0000000..83fa006 --- /dev/null +++ b/core/src/test/java/org/bsc/java2typescript/TestIssue7.java @@ -0,0 +1,77 @@ +package org.bsc.java2typescript; + +import java.lang.reflect.Method; +import java.util.Map; +import java.util.function.BiFunction; + +import org.hamcrest.core.IsEqual; +import org.hamcrest.core.IsNull; +import org.junit.Test; +import org.junit.Assert; + +public class TestIssue7 extends AbstractConverterTest { + + interface TestBean { + + void method1( Map.Entry reducer); + void method2( java.util.List> reducer); + + Map.Entry reduceEntries(long parallelismThreshold, BiFunction,Map.Entry,? extends Map.Entry> reducer); + + } + + @Test + public void testMethod1() throws Exception { + final Class type = TestBean.class; + + { + 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)), + true) ; + + Assert.assertThat( result, IsNull.notNullValue()); + Assert.assertThat( result, IsEqual.equalTo("( reducer:java.util.Map$Entry ):void")); + } + + } + @Test + public void testMethod2() throws Exception { + final Class type = TestBean.class; + + + { + 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)), + true) ; + + Assert.assertThat( result, IsNull.notNullValue()); + Assert.assertThat( result, IsEqual.equalTo("( reducer:java.util.List> ):void")); + } + + + } + @Test + public void testReduceEntries() throws Exception { + final Class type = TestBean.class; + + { + 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)), + true) ; + + Assert.assertThat( result, IsNull.notNullValue()); + Assert.assertThat( result, IsEqual.equalTo("( parallelismThreshold:long, reducer:java.util.function.BiFunction, java.util.Map$Entry, java.util.Map$Entry> ):java.util.Map$Entry")); + } + + + } +} \ No newline at end of file diff --git a/sample/src/main/java/org/bsc/java2ts/jdk8/package-info.java b/sample/src/main/java/org/bsc/java2ts/jdk8/package-info.java index cec4094..dcb8d10 100644 --- a/sample/src/main/java/org/bsc/java2ts/jdk8/package-info.java +++ b/sample/src/main/java/org/bsc/java2ts/jdk8/package-info.java @@ -19,7 +19,7 @@ @Type(java.net.URL.class), @Type(value=java.util.concurrent.Callable.class, alias="Callable"), - //@Type(value=java.util.concurrent.ConcurrentHashMap.class, export=true), + @Type(value=java.util.concurrent.ConcurrentHashMap.class, export=true), @Type(value=java.lang.management.MemoryType.class, export=true), // Member Classes From 0e14caa2b6b912cad5837b937e82244ab532a424 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Fri, 18 May 2018 18:46:02 +0200 Subject: [PATCH 2/2] fix #7 - resolved problem on new without annotation generation --- .../java/org/bsc/java2typescript/TSType.java | 8 ++- .../java2typescript/TypescriptConverter.java | 27 ++++++--- .../org/bsc/java2typescript/TestIssue7.java | 59 +++++++++++++++++-- 3 files changed, 80 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/org/bsc/java2typescript/TSType.java b/core/src/main/java/org/bsc/java2typescript/TSType.java index 381c692..231deea 100644 --- a/core/src/main/java/org/bsc/java2typescript/TSType.java +++ b/core/src/main/java/org/bsc/java2typescript/TSType.java @@ -211,7 +211,13 @@ private Class getClassFrom(Object dt) { @Override public boolean equals(Object o) { - return getValue().equals(((TSType) o).getValue()); + if( o instanceof Class ) { + return getValue().equals(o); + } + if( o instanceof TSType ) { + return getValue().equals(((TSType) o).getValue()); + } + return false; } @Override diff --git a/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java b/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java index 1defb1b..bdedc87 100644 --- a/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java +++ b/core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java @@ -55,7 +55,7 @@ public class TypescriptConverter { /** * */ - private static BiFunction,Type, Boolean> typeParameterMatch = (declaringClass, type) -> + private static BiPredicate,Type> typeParameterMatch = (declaringClass, type) -> ( type instanceof TypeVariable ) ? Arrays.stream(declaringClass.getTypeParameters()) .map( (tp) -> tp.getName()) @@ -192,7 +192,8 @@ static String getTypeName( TSType type, TSType declaringType, boolean packageRes * @param onTypeMismatch * @return */ - public static String convertJavaToTS( Type type, + public static String convertJavaToTS( + Type type, M declaringMember, TSType declaringType, java.util.Map declaredTypeMap, @@ -208,9 +209,10 @@ public static String convertJavaToTS( Type type, * */ final Predicate> typeMismatch = ( tv ) -> { - if( isStatic(declaringMember) ) return true; + if( isStatic(declaringMember) ) return true; if( declaringMember instanceof Constructor ) return true; - return !typeParameterMatch.apply(declaringType.getValue(), tv ); + if( declaringType.equals(type) ) return true; + return !typeParameterMatch.test(declaringType.getValue(), tv ); }; if( type instanceof ParameterizedType ) { @@ -243,8 +245,6 @@ public static String convertJavaToTS( Type type, result = result.replace( tstype.getTypeName(), tstype.getSimpleTypeName()); } - - final Type[] typeArgs = pType.getActualTypeArguments(); for( Type t : typeArgs ) { @@ -266,7 +266,6 @@ else if( t instanceof TypeVariable ) { log( "type variable: %s", t ); final TypeVariable tv = (TypeVariable)t; - if( typeMismatch.test(tv)) { if( onTypeMismatch.isPresent() ) { @@ -362,6 +361,15 @@ else if( type instanceof TypeVariable ) { else if( type instanceof Class ) { log( "class: %s", type.getTypeName() ); + // FIX ISSUE ON NEW + onTypeMismatch.ifPresent( tm -> { + Stream.of(((Class)type).getTypeParameters()) + .filter( tv -> typeMismatch.test(tv) ) + .forEach( tv -> tm.accept(tv)) + ; + + }); + final String name = convertJavaToTS( (Class)type, declaringType, declaredTypeMap, packageResolution); return name; } @@ -617,10 +625,11 @@ private String getMethodParametersAndReturnDecl( Context .collect(Collectors.joining(", ")) ; - final Type returnType = ( m instanceof Method ) ? + final Type returnType = ( m instanceof Method ) ? ((Method)m).getGenericReturnType() : ctx.type.getValue(); - + + final String tsReturnType = convertJavaToTS( returnType, m, diff --git a/core/src/test/java/org/bsc/java2typescript/TestIssue7.java b/core/src/test/java/org/bsc/java2typescript/TestIssue7.java index 83fa006..922c01f 100644 --- a/core/src/test/java/org/bsc/java2typescript/TestIssue7.java +++ b/core/src/test/java/org/bsc/java2typescript/TestIssue7.java @@ -1,13 +1,14 @@ package org.bsc.java2typescript; +import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.Map; import java.util.function.BiFunction; import org.hamcrest.core.IsEqual; import org.hamcrest.core.IsNull; -import org.junit.Test; import org.junit.Assert; +import org.junit.Test; public class TestIssue7 extends AbstractConverterTest { @@ -15,10 +16,20 @@ interface TestBean { void method1( Map.Entry reducer); void method2( java.util.List> reducer); - + + Map.Entry method3(); + Map.Entry reduceEntries(long parallelismThreshold, BiFunction,Map.Entry,? extends Map.Entry> reducer); - - } + + + } + + public static class TestBean1 { + + public TestBean1() { + + } + } @Test public void testMethod1() throws Exception { @@ -55,6 +66,46 @@ public void testMethod2() throws Exception { } + } + + @Test + public void testMethod3() throws Exception { + final Class type = TestBean.class; + + + { + final Method m = type.getMethod("method3"); + final String result = + converter.getMethodParametersAndReturnDecl( m, + TSType.from(type), + declaredTypeMap( TSType.from(Map.Entry.class)), + true) ; + + Assert.assertThat( result, IsNull.notNullValue()); + Assert.assertThat( result, IsEqual.equalTo("( ):java.util.Map$Entry")); + } + + + } + + @Test + public void testConstructor() throws Exception { + final Class type = TestBean1.class; + + + { + final Constructor m = type.getConstructor(); + final String result = + converter.getMethodParametersAndReturnDecl( m, + TSType.from(type), + declaredTypeMap( TSType.from(Map.Entry.class), TSType.from(type) ), + true) ; + + Assert.assertThat( result, IsNull.notNullValue()); + Assert.assertThat( result, IsEqual.equalTo("( ):TestIssue7$TestBean1")); + } + + } @Test public void testReduceEntries() throws Exception {