Skip to content

Commit

Permalink
refinement support for WildcardType
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Feb 8, 2018
1 parent 750f2bf commit 782570d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
27 changes: 21 additions & 6 deletions processor/src/main/java/org/bsc/processor/TypescriptHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ public class TypescriptHelper {
/**
*
*/
public static BiFunction<Class<?>,TypeVariable<?>, Boolean> typeParameterMatch = (declaringClass, typeParameter) ->
Arrays.stream(declaringClass.getTypeParameters())
public static BiFunction<Class<?>,Type, Boolean> typeParameterMatch = (declaringClass, type) ->
( type instanceof TypeVariable ) ?
Arrays.stream(declaringClass.getTypeParameters())
.map( (tp) -> tp.getName())
.anyMatch( name -> name.equals(typeParameter.getName()))
.anyMatch( name -> name.equals(((TypeVariable<?>)type).getName())) :
false
;

private final static Consumer<String> log = msg -> System.out.println(msg);
Expand Down Expand Up @@ -335,8 +337,21 @@ else if( t instanceof Class ) {
result = result.replace(t.getTypeName(), name);
}
else if( t instanceof WildcardType ) {
//throw new IllegalArgumentException( format("type argument <%s> 'WildcardType' is a not supported yet!", t));
result = result.replace(t.getTypeName(), format( "any/*%s*/", t));
final WildcardType wt = (WildcardType) t;

final Type[] lb = wt.getLowerBounds();
final Type[] ub = wt.getUpperBounds();

log.accept(format( "Wildcard Type : %s lb:%d up:%d", type.getTypeName(), lb.length, ub.length ));

if( lb.length <= 1 && ub.length==1) {
final Type tt = (lb.length==1) ? lb[0] : ub[0];

result = result.replace( wt.getTypeName(), convertJavaToTS( tt, declaringClass, declaredClassMap, packageResolution));
}
else {
result = result.replace(wt.getTypeName(), format( "any/*%s*/", wt));
}
}
else if( t instanceof GenericArrayType ) {
throw new IllegalArgumentException( format("type argument <%s> 'GenericArrayType' is a not supported yet!", t));
Expand Down Expand Up @@ -372,7 +387,7 @@ else if( type instanceof GenericArrayType ) {
log.accept(format( "generic array type: %s", t.getGenericComponentType().getTypeName() ));
//throw new IllegalArgumentException( format("type <%s> 'GenericArrayType' is a not supported yet!", type));

return ( typeParameterMatch.apply(declaringClass, (TypeVariable<?>) t.getGenericComponentType() )) ?
return ( typeParameterMatch.apply(declaringClass, t.getGenericComponentType() )) ?
format("[%s]", t.getGenericComponentType() ) :
format("[any/*%s*/]", t.getGenericComponentType() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ protected String getMethodParametersDecl( Method m,
.map( (tp) ->
String.format( "%s:%s",
getParameterName(tp),
convertJavaToTS(tp.getType(),declaringClass,declaredClassMap, packageResolution) ) )
convertJavaToTS(tp.getParameterizedType(),declaringClass,declaredClassMap, packageResolution) ) )
.collect(Collectors.joining(", "))
;

Expand Down
35 changes: 35 additions & 0 deletions processor/src/test/java/org/bsc/processor/RefrectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -105,6 +106,40 @@ public void testSample1() throws Exception {
Assert.assertThat( rresult, IsNull.notNullValue());
Assert.assertThat( rresult, IsEqual.equalTo("Sample2<string>"));

}
{
final Method m = type.getMethod("method2_1", Sample2.class);
final Type rType = m.getGenericReturnType();
final String result = TypescriptHelper.convertJavaToTS(rType, type,
declaredClassMap(Sample2.class, CharSequence.class),
true);
Assert.assertThat( result, IsNull.notNullValue());
Assert.assertThat( result, IsEqual.equalTo("string"));

final Type pType = m.getParameters()[0].getParameterizedType();
final String rresult = TypescriptHelper.convertJavaToTS(pType, type,
declaredClassMap(Sample2.class),
true);
Assert.assertThat( rresult, IsNull.notNullValue());
Assert.assertThat( rresult, IsEqual.equalTo("Sample2<any /*java.lang.CharSequence*/>"));

}
{
final Method m = type.getMethod("method2_2", Consumer.class);
final Type rType = m.getGenericReturnType();
final String result = TypescriptHelper.convertJavaToTS(rType, type,
declaredClassMap(Sample2.class, CharSequence.class),
true);
Assert.assertThat( result, IsNull.notNullValue());
Assert.assertThat( result, IsEqual.equalTo("string"));

final Type pType = m.getParameters()[0].getParameterizedType();
final String rresult = TypescriptHelper.convertJavaToTS(pType, type,
declaredClassMap(Sample2.class, Consumer.class),
true);
Assert.assertThat( rresult, IsNull.notNullValue());
Assert.assertThat( rresult, IsEqual.equalTo("Consumer<E>"));

}

{
Expand Down
5 changes: 5 additions & 0 deletions processor/src/test/java/org/bsc/processor/Sample1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.concurrent.Future;
import java.util.function.BiPredicate;
import java.util.function.Consumer;

public interface Sample1<E> {

Expand All @@ -14,6 +15,10 @@ public interface Sample1<E> {
<E2> BiPredicate<E2, E2> method1_3();

String method2( Sample2<String> s2 );

String method2_1( Sample2<? extends CharSequence> s2 );

String method2_2( Consumer<? extends E> s2 );

E method3();

Expand Down

0 comments on commit 782570d

Please sign in to comment.