Skip to content

Commit

Permalink
fix #7 - resolved problem on new without annotation generation
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed May 18, 2018
1 parent 010d3a2 commit 0e14caa
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 14 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
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,8 +245,6 @@ 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 ) {
Expand All @@ -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() ) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -617,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
59 changes: 55 additions & 4 deletions core/src/test/java/org/bsc/java2typescript/TestIssue7.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
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 {

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 {
Expand Down Expand Up @@ -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<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 {
Expand Down

0 comments on commit 0e14caa

Please sign in to comment.