diff --git a/jdk8/src/main/java/org/bsc/java2ts/jdk8/package-info.java b/jdk8/src/main/java/org/bsc/java2ts/jdk8/package-info.java index 246960f..50fd0b3 100644 --- a/jdk8/src/main/java/org/bsc/java2ts/jdk8/package-info.java +++ b/jdk8/src/main/java/org/bsc/java2ts/jdk8/package-info.java @@ -14,7 +14,7 @@ @Type(java.util.stream.BaseStream.class), - @Type(java.util.stream.Stream.class), + @Type(value=java.util.stream.Stream.class,export=true), @Type(java.util.Iterator.class), @Type(java.util.Comparator.class), diff --git a/processor/src/main/java/org/bsc/processor/TypescriptHelper.java b/processor/src/main/java/org/bsc/processor/TypescriptHelper.java index 1b4a743..b8dcb7c 100644 --- a/processor/src/main/java/org/bsc/processor/TypescriptHelper.java +++ b/processor/src/main/java/org/bsc/processor/TypescriptHelper.java @@ -34,8 +34,7 @@ static String getClassDecl( Class type, final StringBuilder inherited = new StringBuilder(); if( type.isInterface() ) { - statement.append( "interface "); - + statement.append( "interface "); } else { @@ -47,7 +46,7 @@ static String getClassDecl( Class type, if( superclass!=null ) { inherited .append( " extends ") - .append( getName(superclass, type) ) + .append( getName(superclass, type, true) ) ; } } @@ -57,7 +56,7 @@ static String getClassDecl( Class type, if(interfaces.length > 0 ) { final String ifc = Arrays.stream(interfaces) - .map( (c) -> getName(c,type) ) + .map( (c) -> getName(c,type, true) ) .collect( Collectors.joining(", ")) ; inherited @@ -150,25 +149,26 @@ static String getName( Type type, Class declaringClass ) throws ClassNotFound * @param declaringClass * @return */ - static String getName( Class type, Class declaringClass ) { - final Package currentNS = declaringClass.getPackage(); - - final java.util.List dc_parameters_list = - Arrays.stream(declaringClass.getTypeParameters()) - .map( (tp) -> tp.getName()) - .collect(Collectors.toList()); + static String getName( Class type, Class declaringClass, boolean packageResolution ) { - final java.util.List type_parameters_list = - Arrays.stream(type.getTypeParameters()) - .map( (tp) -> (dc_parameters_list.contains(tp.getName()) ) ? tp.getName() : "any" ) - .collect(Collectors.toList()); - - final java.util.List parameters = - dc_parameters_list.size() == type_parameters_list.size() ? dc_parameters_list : type_parameters_list ; - - boolean isFunctionaInterface = ( type.isInterface() && type.isAnnotationPresent(FunctionalInterface.class)); - - return new StringBuilder() + final java.util.List dc_parameters_list = + Arrays.stream(declaringClass.getTypeParameters()) + .map( (tp) -> tp.getName()) + .collect(Collectors.toList()); + + final java.util.List type_parameters_list = + Arrays.stream(type.getTypeParameters()) + .map( (tp) -> (dc_parameters_list.contains(tp.getName()) ) ? tp.getName() : "any" ) + .collect(Collectors.toList()); + + final java.util.List parameters = + dc_parameters_list.size() == type_parameters_list.size() ? dc_parameters_list : type_parameters_list ; + + boolean isFunctionaInterface = ( type.isInterface() && type.isAnnotationPresent(FunctionalInterface.class)); + + final Package currentNS = (packageResolution) ? declaringClass.getPackage() : null; + + return new StringBuilder() .append( type.getPackage().equals(currentNS) || isFunctionaInterface ? type.getSimpleName() : @@ -178,15 +178,19 @@ static String getName( Class type, Class declaringClass ) { .toString(); } - /** * * @param type * @param declaringClass * @param declaredClassMap + * @param packageResolution * @return */ - static String convertJavaToTS( Class type, Class declaringClass, java.util.Map> declaredClassMap ) { + static String convertJavaToTS( Class type, + Class declaringClass, + java.util.Map> declaredClassMap, + boolean packageResolution ) + { if( type == null ) return "any"; @@ -205,7 +209,7 @@ static String convertJavaToTS( Class type, Class declaringClass, java.util if( type.isArray()) return format("[any] /* %s */",type.getName()); if( declaredClassMap.containsKey(type.getName()) ) { - return getName(type,declaringClass); + return getName(type, declaringClass, packageResolution); } return format("any /* %s */",type.getName()); diff --git a/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java b/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java index 55e42a2..a106194 100644 --- a/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java +++ b/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java @@ -79,31 +79,6 @@ public class TypescriptProcessor extends AbstractProcessorEx { java.util.stream.Stream.class, java.util.Optional.class ); - - /** - * - * @param w - * @param t - */ - private void addDeclaration( java.io.Writer w, TSType t, boolean isRhinoCompatible ) { - if( !t.isExport() ) return; - - Class type = t.getValue(); - - info( "export type [%s]", type.getName() ); - - try { - if( isRhinoCompatible ) { - w.append( String.format( "export const %s:%s\t\t=\t%s;\n", type.getSimpleName(), type.getName(), type.getName())); - } - else { - w.append( String.format( "export const %s:%s\t\t=\tJava.type( \"%s\" );\n", type.getSimpleName(), type.getName(), type.getName())); - } - } catch (IOException e) { - error( "error adding [%s]", t.getValue()); - } - - } /** * @@ -120,7 +95,7 @@ private java.io.Writer openFile( Path file, String header ) throws IOException { final java.io.Writer w = out.openWriter(); - try(final java.io.InputStream is = getClass().getClassLoader().getResourceAsStream("headerD.ts") ) { + try(final java.io.InputStream is = getClass().getClassLoader().getResourceAsStream(header) ) { int c; while( (c = is.read()) != -1 ) w.write(c); } @@ -134,23 +109,20 @@ private java.io.Writer openFile( Path file, String header ) throws IOException { @Override public boolean process( Context processingContext ) throws Exception { - final String targetDefinitionFile = processingContext.getOptionMap().getOrDefault("ts.outfile", "out"); + final String targetDefinitionFile = processingContext.getOptionMap().getOrDefault("ts.outfile", "out"); //final String compatibility = processingContext.getOptionMap().getOrDefault("compatibility", "nashorn"); try( final java.io.Writer wD = openFile( Paths.get(targetDefinitionFile.concat(".d.ts")), "headerD.ts" ); - /* final java.io.Writer wT = openFile( Paths.get(targetDefinitionFile.concat(".ts")), "headerT.ts" ); */ + final java.io.Writer wT = openFile( Paths.get(targetDefinitionFile.concat("-types.ts")), "headerT.ts" ); ) { - - + final List types = enumerateDeclaredPackageAndClass( processingContext ); final List> classes = types.stream() - //.peek( t -> addDeclaration(wT, t, compatibility.equalsIgnoreCase("rhino")) ) .map( t -> t.getValue() ) .collect( Collectors.toList()); - - + // // Check for Required classes // @@ -159,7 +131,8 @@ public boolean process( Context processingContext ) throws Exception { .forEach( c -> classes.add(c) ); - final java.util.Map> declaredClasses = classes.stream().collect( Collectors.toMap( clazz -> clazz.getName() , clazz -> clazz )); + final java.util.Map> declaredClasses = + classes.stream().collect( Collectors.toMap( clazz -> clazz.getName() , clazz -> clazz )); PREDEFINED_CLASSES.forEach( clazz -> declaredClasses.put( clazz.getName(), clazz) ); @@ -174,6 +147,18 @@ public boolean process( Context processingContext ) throws Exception { } } ); + types.stream() + .filter( t -> t.isExport() ) + .map( t -> t.getValue() ) + .map( clazz -> processStatic( clazz, declaredClasses)) + .forEach( s -> { + try { + wT.append( s ); + } catch (IOException e) { + error( "error adding [%s]", s); + } + } ); + } // end try-with-resources return true; @@ -186,7 +171,9 @@ public boolean process( Context processingContext ) throws Exception { * @param declaredClassMap * @return */ - private String getPropertyDecl( Class declaringClass, PropertyDescriptor pd, java.util.Map> declaredClassMap ) { + private String getPropertyDecl( Class declaringClass, + PropertyDescriptor pd, + java.util.Map> declaredClassMap ) { final StringBuilder sb = new StringBuilder(); @@ -230,12 +217,94 @@ private String getPropertyDecl( Class declaringClass, PropertyDescriptor pd, final String tsType = convertJavaToTS( pd.getPropertyType(), declaringClass, - declaredClassMap); + declaredClassMap, + true); return sb.append(tsType) .toString(); } + /** + * + * @param m + * @param declaringClass + * @param declaredClassMap + * @return + */ + private String getMethodParametersDecl( Method m, + Class declaringClass, + java.util.Map> declaredClassMap, + boolean packageResolution ) + { + final Class returnType = m.getReturnType(); + + final Parameter[] params = m.getParameters(); + + final String params_string = + Arrays.stream(params) + .map( (tp) -> + String.format( "%s:%s", + tp.getName(), + convertJavaToTS(tp.getType(),declaringClass,declaredClassMap, packageResolution) ) ) + .collect(Collectors.joining(", ")) + ; + + final String tsType = + convertJavaToTS( returnType, + declaringClass, + declaredClassMap, + packageResolution); + + return new StringBuilder() + .append("( ") + .append(params_string) + .append(" ):") + .append(tsType) + .toString(); + } + + /** + * + * @param sb + * @param m + */ + private void appendStaticMethodTypeParameters( final StringBuilder sb, final Method m ) { + final TypeVariable[] return_type_parameters = m.getReturnType().getTypeParameters(); + + if( return_type_parameters.length > 0 ) { + + final String pp = + Arrays.asList( return_type_parameters ) + .stream() + .map( t -> t.getName() ) + .collect(Collectors.joining(",", "<", ">")) ; + + sb.append( pp ); + + } + } + + /** + * + * @param m + * @param declaringClass + * @param declaredClassMap + * @return + */ + private String getFactoryMethodDecl( final Method m, Class declaringClass, java.util.Map> declaredClassMap ) { + + final StringBuilder sb = new StringBuilder(); + + sb.append(m.getName()); + + appendStaticMethodTypeParameters(sb, m); + + sb.append( getMethodParametersDecl(m, declaringClass, declaredClassMap, false) ); + + return sb.toString(); + + } + /** * * @param m @@ -244,7 +313,6 @@ private String getPropertyDecl( Class declaringClass, PropertyDescriptor pd, * @return */ private String getMethodDecl( final Method m, Class declaringClass, java.util.Map> declaredClassMap ) { - final Class returnType = m.getReturnType(); final StringBuilder sb = new StringBuilder(); @@ -255,19 +323,8 @@ private String getMethodDecl( final Method m, Class declaringClass, java.util } sb.append("static ").append(m.getName()); - - - final TypeVariable[] return_type_parameters = m.getReturnType().getTypeParameters(); - - if( return_type_parameters.length > 0 ) { - - sb.append( - Arrays.asList( return_type_parameters ) - .stream() - .map( t -> t.getName() ) - .collect(Collectors.joining(",", "<", ">")) ); - - } + + appendStaticMethodTypeParameters(sb, m); } else { @@ -277,27 +334,9 @@ private String getMethodDecl( final Method m, Class declaringClass, java.util } //if( m.getDeclaringClass().isInterface()) sb.append('?'); - sb.append("( "); - - final Parameter[] params = m.getParameters(); - - final String params_string = - Arrays.stream(params) - .map( (tp) -> - String.format( "%s:%s", - tp.getName(), - convertJavaToTS(tp.getType(),declaringClass,declaredClassMap) ) ) - .collect(Collectors.joining(", ")) - ; - - final String tsType = convertJavaToTS( returnType, - declaringClass, - declaredClassMap); + sb.append( getMethodParametersDecl(m, declaringClass, declaredClassMap, true) ); - return sb.append(params_string) - .append(" ):") - .append(tsType) - .toString(); + return sb.toString(); } @@ -393,6 +432,66 @@ private void processEnum( StringBuilder sb, Class type, java.util.Map type, java.util.Map> declaredClassMap ) { + + final StringBuilder sb = new StringBuilder(); + + final java.util.Set methodSet = + getMethods( type ) + .stream() + .filter( this::isFactoryMethod ) + .collect( Collectors.toCollection(() -> new java.util.LinkedHashSet() )); + + if( !methodSet.isEmpty() ) { + + sb.append("interface ") + .append( type.getSimpleName() ) + .append("Static {\n\n") + ; + + methodSet.stream() + .map( md -> getFactoryMethodDecl(md, type, declaredClassMap) ) + .sorted().forEach( (decl) -> + sb.append( '\t' ) + .append(decl) + .append( ENDL )) + ; + } + + sb.append( "}\n\n" ) + .append("export const ") + .append(type.getSimpleName()) + .append(": ") + .append(type.getSimpleName()) + .append("Static = Java.type(\"") + .append( type.getName() ) + .append("\")") + .append( ENDL ) + .append("\n\n") + ; + + return sb.toString(); + } + + /** * * @param bi @@ -410,21 +509,19 @@ private String processClass( BeanInfo bi, java.util.Map> decla final java.util.Set methodSet = getMethods( type ) .stream() - .filter( (md) -> { // Remove setter and getter - return !Arrays.asList(pds).stream().anyMatch( (pd) -> { - final Method rm = pd.getReadMethod(); - final Method wm = pd.getWriteMethod(); - return (md.equals(rm) || md.equals(wm)); - }); - }) + .filter( md -> !isFactoryMethod(md) ) .filter( (md) -> { - final String name = md.getName(); - - return !( name.contains("$") || // remove unnamed - name.equals("wait") || - name.equals("notify") || - name.equals("notifyAll") ); - }) + final String name = md.getName(); + return !( name.contains("$") || // remove unnamed + name.equals("wait") || + name.equals("notify") || + name.equals("notifyAll") ); + }) + .filter( md -> // Remove setter and getter + !Arrays.asList(pds) + .stream() + .anyMatch( pd -> (md.equals(pd.getReadMethod()) || md.equals(pd.getWriteMethod())) ) + ) //.peek( (md ) -> System.out.printf( "==> CLASS [%s] - METHOD\t[%s]\n",type.getSimpleName(),md.getName())) .collect( Collectors.toCollection(() -> new java.util.LinkedHashSet() )); diff --git a/processor/src/main/resources/headerD.ts b/processor/src/main/resources/headerD.ts index c9a95c0..90bf7a3 100644 --- a/processor/src/main/resources/headerD.ts +++ b/processor/src/main/resources/headerD.ts @@ -14,58 +14,13 @@ type bytearray = [char]; type Runnable = () => void; -interface Supplier { - ():T; //get():T -} - -interface Consumer /*extends Function*/ { - andThen?( arg0:Consumer ):Consumer; - ( v:T ):void; //accept(t:T):void -} +type Supplier = () => T; -interface ConsumerConstructor { - new( args: T): Consumer; - ( args: T): Consumer; - readonly prototype: Function; -} +type Consumer = ( v:T) => void; -declare const Consumer : ConsumerConstructor; +type UnaryOperator = ( v:T ) => T ; -interface UnaryOperator/* extends Function*/ { - - andThen?( arg0:any /* java.util.function.Function */ ):any /* java.util.function.Function */; - compose?( arg0:any /* java.util.function.Function */ ):any /* java.util.function.Function */; - ( v:T ):T //apply(t:T):T - -} - -interface UnaryOperatorConstructor { - new( args: T): UnaryOperator; - ( args: T): UnaryOperator; - /*static */identity( ):UnaryOperator; - readonly prototype: Function; -} - -declare const UnaryOperator : UnaryOperatorConstructor; - -interface Predicate { - - and?( arg0:Predicate ):Predicate; - negate?():Predicate; - or?( arg0:Predicate ):Predicate; - ( v:T ):boolean //test( arg0:T /* java.lang.Object */ ):boolean; - -} - -interface PredicateConstructor { - new( args: T): UnaryOperator; - ( args: T): UnaryOperator; - - /*static*/ isEqual( arg0:any /* java.lang.Object */ ):Predicate; - -} - -declare const Predicate : PredicateConstructor; +type Predicate = ( v:T ) => boolean; declare namespace java.lang { @@ -92,19 +47,3 @@ declare namespace java.io { interface Closeable {} interface Serializable {} } - -declare namespace java.util.stream { - - interface StreamConstructor{ - builder( ):Stream.Builder; - concat( arg0:Stream,arg1:Stream ):Stream; - empty( ):Stream; - generate( arg0:Supplier ):Stream; - iterate( seed:T,arg1:UnaryOperator ):Stream; - of( arg0:Array ):Stream; - of( arg0:T ):Stream; - - } - - export const Stream : java.util.stream.StreamConstructor; -} diff --git a/processor/src/main/resources/headerT.ts b/processor/src/main/resources/headerT.ts index 8b8094d..82e96ad 100644 --- a/processor/src/main/resources/headerT.ts +++ b/processor/src/main/resources/headerT.ts @@ -1,3 +1 @@ /// - -