Skip to content

Commit

Permalink
Merge branch 'feature/functionalinterface' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Mar 1, 2018
2 parents ae79433 + 7945b91 commit c730223
Show file tree
Hide file tree
Showing 15 changed files with 226 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
@Java2TS(declare = {
@Type(java.lang.Iterable.class),
@Type(java.lang.Comparable.class),
@Type(java.lang.Runnable.class),
@Type(java.lang.System.class),


@Type(java.util.stream.BaseStream.class),
@Type(value=java.util.stream.Stream.class,export=true),
@Type(value=java.util.stream.Collectors.class,export=true),

@Type(java.util.Iterator.class),
@Type(java.util.Comparator.class),
@Type(java.util.Collection.class),
@Type(java.util.Map.class),
@Type(java.util.List.class ),
@Type(java.util.Set.class),
@Type(value=java.util.Arrays.class, export=true),
@Type(java.util.HashMap.class),
@Type(java.util.HashSet.class),
@Type(java.util.ArrayList.class),
@Type(java.util.Optional.class),

@Type(value=java.nio.file.Files.class, export=true),
@Type(java.nio.file.Path.class),
@Type(value=java.nio.file.Paths.class, export=true),
@Type(java.nio.file.AccessMode.class),

@Type(java.util.function.UnaryOperator.class),
@Type(java.util.function.Consumer.class),
@Type(java.util.function.Predicate.class),

@Type(value=java.net.URI.class, export=true),
@Type(java.net.URL.class)
Expand All @@ -37,6 +12,6 @@

package ${package};


import org.bsc.processor.annotation.Java2TS;
import org.bsc.processor.annotation.Type;

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


import {Stream,Arrays,URI,Collectors} from "ts/${typescript-filename}-types";
import {Stream,Arrays,URI,Collectors} from "${typescript-filename}-types";



Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./", /* Base directory to resolve non-absolute module names. */
"paths": { "ts/*":["target/ts/*"]}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
"baseUrl": "target/ts", /* Base directory to resolve non-absolute module names. */
// "paths": { "ts/*":["target/ts/*"]}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
Expand Down
1 change: 1 addition & 0 deletions archetype/tofilter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
</arguments>
<!-- remove comment if want skip tsc error
<successCodes>
<code>0</code>
<code>2</code>
</successCodes>
-->
Expand Down
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>

</plugins>
Expand Down
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
95 changes: 59 additions & 36 deletions processor/src/main/java/org/bsc/processor/TypescriptHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
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;
import java.util.stream.Stream;

public class TypescriptHelper {

Expand All @@ -28,12 +28,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 All @@ -46,7 +40,13 @@ public class TypescriptHelper {
;

private final static void log( String fmt, Object ...args ) {
//System.out.println( format( fmt, (Object[])args));
System.out.println( format( fmt, (Object[])args));
}

public static final String processFunctionalInterface( TSType type ) {
Objects.requireNonNull(type, "argument 'type' is not defined!");

return null;
}

/**
Expand Down Expand Up @@ -88,16 +88,17 @@ public static final String getParameterName( Parameter p ) {
}

/**
*
* @param m
* @return
*/
static boolean isStaticMethod( Method m ) {
final int modifier = m.getModifiers();

return (Modifier.isStatic( modifier) &&
Modifier.isPublic( modifier )) ;
}
*
* @param m
* @return
*/
static boolean isStaticMethod( Method m ) {

final int modifier = m.getModifiers();

return (Modifier.isStatic( modifier) &&
Modifier.isPublic( modifier )) ;
}

/**
*
Expand Down Expand Up @@ -221,7 +222,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 @@ -266,8 +267,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 All @@ -291,7 +292,7 @@ else if( t instanceof TypeVariable ) {

final TypeVariable<?> tv = (TypeVariable<?>)t;

if( isStaticMethod(declaringMethod) || !typeParameterMatch.apply(declaringType.getValue(), tv )) {
if( isStaticMethod( declaringMethod ) || !typeParameterMatch.apply(declaringType.getValue(), tv )) {

if( onTypeMismatch.isPresent() ) {
onTypeMismatch.get().accept(tv);
Expand Down Expand Up @@ -322,18 +323,29 @@ else if( t instanceof WildcardType ) {

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

log( "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,
declaringMethod,
declaringType,
declaredTypeMap,
packageResolution,
onTypeMismatch));
final String s = convertJavaToTS( tt,
declaringMethod,
declaringType,
declaredTypeMap,
packageResolution,
onTypeMismatch);

if( tt instanceof ParameterizedType &&
Stream.of((Type[])((ParameterizedType)tt).getActualTypeArguments())
.anyMatch( arg -> (arg instanceof WildcardType) ))
{
// not supportded nested WildcardType
result = format( "%s/*%s*/", s, wt.getTypeName() ) ;
}
else {
result = result.replace( wt.getTypeName(), s);
}
}
else {
result = result.replace(wt.getTypeName(), format( "any/*%s*/", wt));
Expand All @@ -352,7 +364,7 @@ else if( type instanceof TypeVariable ) {

final TypeVariable<?> tv = (TypeVariable<?>)type;

if( isStaticMethod(declaringMethod) || !typeParameterMatch.apply(declaringType.getValue(), tv )) {
if( isStaticMethod( declaringMethod ) || !typeParameterMatch.apply(declaringType.getValue(), tv )) {

final String name = tv.getName();

Expand All @@ -378,12 +390,23 @@ 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));

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

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

final String tt = convertJavaToTS( componentType,
declaringMethod,
declaringType,
declaredTypeMap,
packageResolution,
onTypeMismatch);
return format("[%s]", tt);


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

}

throw new IllegalArgumentException( "type is a not recognised type!");
Expand Down
Loading

0 comments on commit c730223

Please sign in to comment.