Skip to content

Commit

Permalink
generate functional interface with default methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Apr 16, 2018
1 parent e46dfee commit 3974de1
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,6 @@ public static boolean isFunctionalInterface( final Class<?> c ) {
return Arrays.stream(c.getDeclaredMethods()).filter( m -> Modifier.isAbstract(m.getModifiers()) ).count() == 1;
}

/**
*
* @return
*/
public static boolean isFunctionalInterface( final Class<?> c, Consumer<Method> consumer ) {

if( !isFunctionalInterface(c) ) return false;

Arrays.stream(c.getDeclaredMethods())
.filter( m -> Modifier.isAbstract(m.getModifiers()) )
.findFirst()
.ifPresent(consumer);

return true;
}

/**
*
* @param type_parameters_list
Expand Down Expand Up @@ -610,7 +594,7 @@ private <E extends Executable> String getMethodParametersAndReturnDecl( Context
* @param m
* @return
*/
private String getMethodDecl( Context ctx, final Method m ) {
private String getMethodDecl( Context ctx, final Method m, boolean optional ) {

final StringBuilder sb = new StringBuilder();

Expand All @@ -624,6 +608,7 @@ private String getMethodDecl( Context ctx, final Method m ) {
}
else {
sb.append(m.getName());
if( optional ) sb.append('?');
}

sb.append( getMethodParametersAndReturnDecl(ctx, m, true) );
Expand Down Expand Up @@ -723,14 +708,17 @@ Context getClassDecl()


}

sb.append( getTypeName(type, type, true) );

if( inherited.length()>0 ) {
if( inherited.length()>0 || type.hasAlias()) {

sb.append("/*")
.append( inherited )
.append("*/");
sb.append("/*");

if( type.hasAlias() ) sb.append( type.getValue().getName() );
if( inherited.length()>0 ) sb.append( inherited );

sb.append("*/");
}

sb.append( " {");
Expand Down Expand Up @@ -846,7 +834,27 @@ public String processClass( int level, TSType tstype, java.util.Map<String, TSTy

ctx.getClassDecl().append("\n\n");

if( !TypescriptConverter.isFunctionalInterface(tstype.getValue(), m -> ctx.append( getMethodParametersAndReturnDecl( ctx, m, false) ))) {
if( tstype.isFunctionalInterface() ) {

tstype.getMethods().stream()
.filter( m -> Modifier.isAbstract(m.getModifiers()) )
.findFirst()
.ifPresent( m -> ctx.append( '\t' )
.append( getMethodParametersAndReturnDecl( ctx, m, false) )
.append( ENDL )) ;

tstype.getMethods().stream()
.filter( m -> !Modifier.isAbstract(m.getModifiers()) )
.map( m -> getMethodDecl(ctx, m, true /*optional*/) )
.sorted()
.forEach( decl ->
ctx.append( '\t' )
.append(decl)
.append( ENDL ))
;


} else {

ctx.processEnumDecl();

Expand All @@ -866,7 +874,7 @@ public String processClass( int level, TSType tstype, java.util.Map<String, TSTy
.collect( Collectors.toCollection(() -> new java.util.LinkedHashSet<Method>() ));

methodSet.stream()
.map( md -> getMethodDecl(ctx, md) )
.map( md -> getMethodDecl(ctx, md, false /*optional*/) )
.sorted().forEach( (decl) ->
ctx.append( '\t' )
.append(decl)
Expand Down

0 comments on commit 3974de1

Please sign in to comment.