Skip to content

Commit

Permalink
add support for no-annotated functional interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Mar 1, 2018
1 parent 491a8bd commit 365cda8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
24 changes: 24 additions & 0 deletions processor/src/main/java/org/bsc/processor/TSType.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ public static TSType from( Class<?> cl, String alias, boolean export ) {
}};
}

/**
* Assume that class is managed as Functional Interface
*
* @param cl
* @param alias
* @return
*/
public static TSType functional( Class<?> cl, String alias ) {
return new TSType() {{
put( "value", cl);
put( "alias", alias);
put("functional", true);
}};
}

protected static TSType from( Class<?> cl ) {
return new TSType() {{ put( "value", cl); }};
}
Expand All @@ -49,6 +64,15 @@ public boolean isExport() {
return (boolean) super.getOrDefault("export", false);
}

/**
*
* @return
*/
public boolean isFunctionalInterface() {
return ((boolean) super.getOrDefault("functional", false)) ||
(getValue().isInterface() && getValue().isAnnotationPresent(FunctionalInterface.class));
}

/**
*
* @return
Expand Down
24 changes: 9 additions & 15 deletions processor/src/main/java/org/bsc/processor/TypescriptHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class TypescriptHelper {
Expand All @@ -28,12 +27,6 @@ public class TypescriptHelper {
public static BiPredicate<Class<?>, Class<?>> isPackageMatch = (a, b) ->
a.getPackage().equals(b.getPackage()) ;

/**
*
*/
public static Predicate<Class<?>> isFunctionalInterface = type ->
type.isInterface() && type.isAnnotationPresent(FunctionalInterface.class);

/**
*
*/
Expand Down Expand Up @@ -228,7 +221,7 @@ static String getTypeName( TSType type, TSType declaringType, boolean packageRes

return new StringBuilder()
.append(
type.getValue().getPackage().equals(currentNS) || isFunctionalInterface.test(type.getValue()) ?
type.getValue().getPackage().equals(currentNS) || type.isFunctionalInterface() ?
type.getSimpleTypeName() :
type.getTypeName()
)
Expand Down Expand Up @@ -273,8 +266,8 @@ public static String convertJavaToTS( Type type,
.replace( rawType.getName(), tstype.getTypeName()) // use Alias
;

if( isFunctionalInterface.test(rawType) || (packageResolution && isPackageMatch.test(rawType, declaringType.getValue())) ) {
result = result.replace( rawType.getName(), rawType.getSimpleName());
if( tstype.isFunctionalInterface() || (packageResolution && isPackageMatch.test(tstype.getValue(), declaringType.getValue())) ) {
result = result.replace( tstype.getTypeName(), tstype.getSimpleTypeName());
}

final Type[] typeArgs = pType.getActualTypeArguments();
Expand Down Expand Up @@ -385,12 +378,13 @@ else if( type instanceof WildcardType ) {
else if( type instanceof GenericArrayType ) {
final GenericArrayType t = (GenericArrayType)type;

log( "generic array type: %s", t.getGenericComponentType().getTypeName() );
//throw new IllegalArgumentException( format("type <%s> 'GenericArrayType' is a not supported yet!", type));
final Type componentType = t.getGenericComponentType();

log( "generic array type: %s", componentType.getTypeName() );

return ( typeParameterMatch.apply(declaringType.getValue(), t.getGenericComponentType() )) ?
format("[%s]", t.getGenericComponentType() ) :
format("[any/*%s*/]", t.getGenericComponentType() );
return ( typeParameterMatch.apply(declaringType.getValue(), componentType )) ?
format("[%s]", componentType ) :
format("[any/*%s*/]", componentType );
}

throw new IllegalArgumentException( "type is a not recognised type!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public boolean process( Context processingContext ) throws Exception {
.append("// TYPE ALIASES\n")
.append("//\n\n");
types.stream()
.filter( t -> !t.isFunctionalInterface() )
.filter( t -> t.hasAlias() )
.map( t -> TypescriptHelper.getAliasDeclaration(t.getValue(), t.getAlias()) )
.forEach( wD_append );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
Class<?> value();
boolean export() default false ;
String alias() default "";
boolean functional() default false ;
}

0 comments on commit 365cda8

Please sign in to comment.