Skip to content

Commit

Permalink
Merge branch 'feature/issue#7' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed May 18, 2018
2 parents 703d27c + 0e14caa commit 022c8c9
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 20 deletions.
8 changes: 7 additions & 1 deletion core/src/main/java/org/bsc/java2typescript/TSType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 33 additions & 18 deletions core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class TypescriptConverter {
/**
*
*/
private static BiFunction<Class<?>,Type, Boolean> typeParameterMatch = (declaringClass, type) ->
private static BiPredicate<Class<?>,Type> typeParameterMatch = (declaringClass, type) ->
( type instanceof TypeVariable ) ?
Arrays.stream(declaringClass.getTypeParameters())
.map( (tp) -> tp.getName())
Expand Down Expand Up @@ -192,7 +192,8 @@ static String getTypeName( TSType type, TSType declaringType, boolean packageRes
* @param onTypeMismatch
* @return
*/
public static <M extends Member> String convertJavaToTS( Type type,
public static <M extends Member> String convertJavaToTS(
Type type,
M declaringMember,
TSType declaringType,
java.util.Map<String, TSType> declaredTypeMap,
Expand All @@ -208,9 +209,10 @@ public static <M extends Member> String convertJavaToTS( Type type,
*
*/
final Predicate<TypeVariable<?>> 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 ) {
Expand Down Expand Up @@ -243,11 +245,10 @@ public static <M extends Member> String convertJavaToTS( Type type,
result = result.replace( tstype.getTypeName(), tstype.getSimpleTypeName());
}



final Type[] typeArgs = pType.getActualTypeArguments();

for( Type t : typeArgs ) {

if( t instanceof ParameterizedType ) {

final String typeName = convertJavaToTS( t,
Expand All @@ -265,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() ) {
Expand Down Expand Up @@ -298,7 +298,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];
Expand All @@ -312,14 +312,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 {
Expand Down Expand Up @@ -356,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;
}
Expand Down Expand Up @@ -611,10 +625,11 @@ private <E extends Executable> 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,
Expand Down
3 changes: 3 additions & 0 deletions core/src/test/java/org/bsc/java2typescript/ProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -343,4 +345,5 @@ public void testSample1_GenericArrayType() throws Exception {

}


}
2 changes: 2 additions & 0 deletions core/src/test/java/org/bsc/java2typescript/Sample1.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ public interface Sample1<E> {
<T> T concatMap(Function<? super E, ? extends Sample2<? extends T>> mapper);

<T> java.util.List<T[]> genericArrayType( E[] c );


}
128 changes: 128 additions & 0 deletions core/src/test/java/org/bsc/java2typescript/TestIssue7.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
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.Assert;
import org.junit.Test;

public class TestIssue7 extends AbstractConverterTest {

interface TestBean<K,V> {

void method1( Map.Entry<? extends K,V> reducer);
<E extends K> void method2( java.util.List<Map.Entry<E,V>> reducer);

Map.Entry<K,V> method3();

Map.Entry<K,V> reduceEntries(long parallelismThreshold, BiFunction<Map.Entry<K,V>,Map.Entry<K,V>,? extends Map.Entry<K,V>> reducer);


}

public static class TestBean1<K,V> {

public TestBean1() {

}
}

@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<K, V> ):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("<E>( reducer:java.util.List<java.util.Map$Entry<E, V>> ):void"));
}


}

@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<K, V>"));
}


}

@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("<K,V>( ):TestIssue7$TestBean1<K, V>"));
}


}
@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<K, V>, java.util.Map$Entry<K, V>, java.util.Map$Entry<K, V>> ):java.util.Map$Entry<K, V>"));
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 022c8c9

Please sign in to comment.