Skip to content

Commit

Permalink
Merge branch 'feature/functional' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed May 21, 2018
2 parents 170e864 + e451c03 commit 4ceabf8
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 48 deletions.
41 changes: 32 additions & 9 deletions core/src/main/java/org/bsc/java2typescript/TSType.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class TSType extends HashMap<String, Object> {
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);
Expand Down Expand Up @@ -53,6 +54,7 @@ public static TSType from(Class<?> cl) {
};
}


/**
*
* @return
Expand All @@ -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;
}

Expand All @@ -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());
Expand Down Expand Up @@ -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
Expand Down
54 changes: 22 additions & 32 deletions core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
*
Expand Down Expand Up @@ -205,6 +182,7 @@ public static <M extends Member> String convertJavaToTS(
Objects.requireNonNull(declaringType, "declaringType argument is null!");
Objects.requireNonNull(declaredTypeMap, "declaredTypeMap argument is null!");

log( "PROCESSING MEMEBER: [%s]", declaringMember.getName());
/**
*
*/
Expand Down Expand Up @@ -247,6 +225,7 @@ public static <M extends Member> String convertJavaToTS(
final Type[] typeArgs = pType.getActualTypeArguments();

for( Type t : typeArgs ) {
log( "TypeArgs [%s]", t.getTypeName());

if( t instanceof ParameterizedType ) {

Expand All @@ -256,8 +235,8 @@ public static <M extends Member> 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 ) {
Expand All @@ -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);

Expand All @@ -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];
Expand All @@ -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 ", "" );

Expand All @@ -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 ) {
Expand Down Expand Up @@ -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!");
Expand Down Expand Up @@ -516,7 +506,7 @@ public String processStatic( TSType type, java.util.Map<String, TSType> declared
//Append class property
ctx.append("\treadonly class:any;\n");

if( isFunctionalInterface( type.getValue() ) ) {
if( type.isFunctionalInterface() ) {

final java.util.Set<String> TypeVarSet = new java.util.HashSet<>(5);
final String tstype = convertJavaToTS( type.getValue(),
Expand Down
72 changes: 67 additions & 5 deletions core/src/test/java/org/bsc/java2typescript/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {};
Expand All @@ -23,15 +29,71 @@ interface Action2 {

void apply();
}

interface TestBean {

void method1( Map.Entry<Object, java.util.List<?>> p1 );
void method2( Function<String, ? extends java.util.List<?>> 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<any /*java.lang.Object*/, java.util.List<any /*java.lang.Object*/>> ):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<string, java.util.List<any /*java.lang.Object*/>> ):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));
}

}

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

0 comments on commit 4ceabf8

Please sign in to comment.