Skip to content

Commit

Permalink
fix enum generation
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Mar 3, 2018
1 parent c730223 commit 58fc447
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 60 deletions.
9 changes: 9 additions & 0 deletions processor/src/main/java/org/bsc/processor/TSType.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public Class<?> getValue() {
public boolean isExport() {
return (boolean) super.getOrDefault("export", false);
}

/**
*
* @return
*/
public TSType setExport( boolean export ) {
super.put("export", export);
return this;
}

/**
*
Expand Down
44 changes: 18 additions & 26 deletions processor/src/main/java/org/bsc/processor/TypescriptHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import static java.lang.String.format;

import java.beans.PropertyDescriptor;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
Expand Down Expand Up @@ -40,7 +40,7 @@ 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 ) {
Expand Down Expand Up @@ -92,7 +92,7 @@ public static final String getParameterName( Parameter p ) {
* @param m
* @return
*/
static boolean isStaticMethod( Method m ) {
static <M extends Member> boolean isStatic( M m ) {

final int modifier = m.getModifiers();

Expand All @@ -107,31 +107,23 @@ static boolean isStaticMethod( Method m ) {
*/
static boolean isFactoryMethod( Method m ) {

return (isStaticMethod(m) &&
return (isStatic(m) &&
m.getReturnType().equals(m.getDeclaringClass()));
}

/**
*
* @param pd
* @return
*/
static boolean isPropertyValid( PropertyDescriptor pd ) {
return !( "class".equalsIgnoreCase(pd.getName()) );
}

/**
*
* @param type
* @param declaredClassMap
* @param isSuperclassValid
* @return
*/
static String getClassDecl( TSType tstype,
static StringBuilder getClassDecl(
StringBuilder statement,
TSType tstype,
java.util.Map<String, TSType> declaredClassMap )
{

final StringBuilder statement = new StringBuilder();
final StringBuilder inherited = new StringBuilder();

if( tstype.getValue().isInterface() ) {
Expand Down Expand Up @@ -178,8 +170,8 @@ static String getClassDecl( TSType tstype,
.append("*/");
}

return statement.append( " {")
.toString();
return statement.append( " {");


}

Expand Down Expand Up @@ -233,22 +225,22 @@ static String getTypeName( TSType type, TSType declaringType, boolean packageRes
/**
*
* @param type
* @param declaringMethod
* @param declaringMember
* @param declaredTypeMap
* @param packageResolution
* @param typeMatch
* @param onTypeMismatch
* @return
*/
public static String convertJavaToTS( Type type,
Method declaringMethod,
public static <M extends Member> String convertJavaToTS( Type type,
M declaringMember,
TSType declaringType,
java.util.Map<String, TSType> declaredTypeMap,
boolean packageResolution,
Optional<Consumer<TypeVariable<?>>> onTypeMismatch)
{
Objects.requireNonNull(type, "Type argument is null!");
Objects.requireNonNull(declaringMethod, "declaringMethod argument is null!");
Objects.requireNonNull(declaringMember, "declaringMethod argument is null!");
Objects.requireNonNull(declaringType, "declaringType argument is null!");
Objects.requireNonNull(declaredTypeMap, "declaredTypeMap argument is null!");

Expand Down Expand Up @@ -277,7 +269,7 @@ public static String convertJavaToTS( Type type,
if( t instanceof ParameterizedType ) {

final String typeName = convertJavaToTS( t,
declaringMethod,
declaringMember,
declaringType,
declaredTypeMap,
packageResolution,
Expand All @@ -292,7 +284,7 @@ else if( t instanceof TypeVariable ) {

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

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

if( onTypeMismatch.isPresent() ) {
onTypeMismatch.get().accept(tv);
Expand Down Expand Up @@ -330,7 +322,7 @@ else if( t instanceof WildcardType ) {
final Type tt = (lb.length==1) ? lb[0] : ub[0];

final String s = convertJavaToTS( tt,
declaringMethod,
declaringMember,
declaringType,
declaredTypeMap,
packageResolution,
Expand Down Expand Up @@ -364,7 +356,7 @@ else if( type instanceof TypeVariable ) {

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

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

final String name = tv.getName();

Expand Down Expand Up @@ -395,7 +387,7 @@ else if( type instanceof GenericArrayType ) {
log( "generic array type: %s", componentType.getTypeName() );

final String tt = convertJavaToTS( componentType,
declaringMethod,
declaringMember,
declaringType,
declaredTypeMap,
packageResolution,
Expand Down
101 changes: 68 additions & 33 deletions processor/src/main/java/org/bsc/processor/TypescriptProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import static org.bsc.processor.TypescriptHelper.convertJavaToTS;
import static org.bsc.processor.TypescriptHelper.getClassDecl;
import static org.bsc.processor.TypescriptHelper.getParameterName;
import static org.bsc.processor.TypescriptHelper.isStaticMethod;

import java.io.Closeable;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -317,6 +317,25 @@ private String getMethodDecl( final Method m, TSType declaringClass, java.util.M

}

/**
*
* @param type
* @return
*/
private Set<Field> getFields( final TSType type ) {

final Predicate<Field> std = f ->
!f.isSynthetic() &&
Modifier.isPublic( f.getModifiers() ) &&
Character.isJavaIdentifierStart(f.getName().charAt(0)) &&
f.getName().chars().skip(1).allMatch(Character::isJavaIdentifierPart);

return Stream.concat( Stream.of(type.getValue().getFields()), Stream.of(type.getValue().getDeclaredFields()) )
.filter(std)
.collect( Collectors.toSet( ) );

}

/**
*
* @param type
Expand Down Expand Up @@ -364,31 +383,44 @@ private void processNestedClasses( StringBuilder sb, TSType tstype, java.util.Ma
;
}

/**
*
* @param sb
* @param type
* @param declaredClassMap
*/
private void processEnum( StringBuilder sb, TSType type, java.util.Map<String, TSType> declaredClassMap ) {
if( !type.getValue().isEnum() ) return ;

Arrays.stream( type.getValue().getEnumConstants() )
.forEach( (c) -> {
sb.append( '\t' )
.append( "static ")
.append( c.toString() )
.append( ':')
.append( type.getSimpleTypeName() )
.append( ';' )
.append( '\n' )
;
});

sb.append( '\n' );


private StringBuilder processEnumDecl( StringBuilder sb, TSType type ) {
if( type.getValue().isEnum() ) {
type.setExport(true); // force export
Arrays.stream( type.getValue().getEnumConstants() )
.forEach( (c) ->
sb.append( '\t' )
.append( "// ")
.append( c.toString() )
.append( ':')
.append( type.getSimpleTypeName() )
.append( ';' )
.append( '\n' )
);
sb.append('\n');
}

return sb;

}

private StringBuilder processEnumType( StringBuilder sb, TSType type ) {
if( type.getValue().isEnum() ) {
Arrays.stream( type.getValue().getEnumConstants() )
.forEach( (c) ->
sb.append( '\t' )
.append( c.toString() )
.append( ':')
.append( type.getTypeName() )
.append( ';' )
.append( '\n' )
);
sb.append('\n');
}

return sb;

}

/**
*
Expand All @@ -399,18 +431,21 @@ private String processStatic( TSType type, java.util.Map<String, TSType> declare

final StringBuilder sb = new StringBuilder();

final java.util.Set<Method> methodSet =
getMethods( type )
.stream()
.filter( TypescriptHelper::isStaticMethod )
.collect( Collectors.toCollection(() -> new java.util.LinkedHashSet<Method>() ));

sb.append("interface ")
.append( type.getSimpleTypeName() )
.append("Static {\n\n")
;

processEnumType(sb, type)
//Append class property
.append("\treadonly class:any;\n");

final java.util.Set<Method> methodSet =
getMethods( type )
.stream()
.filter( TypescriptHelper::isStatic )
.collect( Collectors.toCollection(() -> new java.util.LinkedHashSet<>() ));

if( !methodSet.isEmpty() ) {

methodSet.stream()
Expand Down Expand Up @@ -456,14 +491,14 @@ private String processClass( TSType tstype, java.util.Map<String, TSType> declar
.append(" {\n\n")
;

sb.append( getClassDecl(tstype, declaredClassMap) ).append("\n\n");
getClassDecl( sb, tstype, declaredClassMap).append("\n\n");

processEnum(sb, tstype, declaredClassMap);
processEnumDecl(sb, tstype);

final java.util.Set<Method> methodSet =
getMethods( tstype )
.stream()
.filter( md -> (tstype.isExport() && isStaticMethod(md))==false )
.filter( md -> (tstype.isExport() && TypescriptHelper.isStatic(md))==false )
.filter( (md) -> {
final String name = md.getName();
return !( name.contains("$") || // remove unnamed
Expand Down
3 changes: 2 additions & 1 deletion processor/src/test/java/org/bsc/processor/ProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public void testFunctionalInterface() throws Exception {
public void testClassDecl() throws Exception {

{
final String result = TypescriptHelper.getClassDecl( TSType.from(ArrayList.class), Collections.emptyMap());
final String result =
TypescriptHelper.getClassDecl( new StringBuilder(), TSType.from(ArrayList.class), Collections.emptyMap()).toString();

Assert.assertThat( result, IsNull.notNullValue());
Assert.assertThat( result, IsEqual.equalTo("class ArrayList<E>/* extends AbstractList<E> implements List<E>, RandomAccess, java.lang.Cloneable, java.io.Serializable*/ {"));
Expand Down

0 comments on commit 58fc447

Please sign in to comment.