From 24c3247ff94ec4abe26343cb338c46385b7902f7 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Fri, 29 Dec 2017 23:24:37 +0100 Subject: [PATCH] disable export generation --- .../org/bsc/java2ts/jdk8/package-info.java | 18 +-- .../bsc/processor/TypescriptProcessor.java | 117 ++++++++++-------- .../main/resources/{header.ts => headerD.ts} | 3 - processor/src/main/resources/headerT.ts | 3 + 4 files changed, 77 insertions(+), 64 deletions(-) rename processor/src/main/resources/{header.ts => headerD.ts} (97%) create mode 100644 processor/src/main/resources/headerT.ts 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 397765a..126da64 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 @@ -7,12 +7,12 @@ @Type(java.lang.Class.class), @Type(java.lang.Iterable.class), @Type(java.lang.CharSequence.class), - @Type(value=java.lang.String.class, export=true), + @Type(java.lang.String.class), @Type(java.lang.Comparable.class), @Type(java.lang.Runnable.class), @Type(java.util.stream.BaseStream.class), - @Type(value=java.util.stream.Stream.class, export=true), + @Type(java.util.stream.Stream.class), @Type(java.util.Iterator.class), @Type(java.util.Comparator.class), @@ -20,14 +20,15 @@ @Type(java.util.Map.class), @Type(java.util.List.class ), @Type(java.util.Set.class), - @Type(value=java.util.HashMap.class, export=true), - @Type(value=java.util.HashSet.class, export=true), - @Type(value=java.util.ArrayList.class, export=true), - @Type(value=java.util.Optional.class, export=true), + @Type(java.util.Arrays.class), + @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.Files.class), @Type(java.nio.file.Path.class), - @Type(value=java.nio.file.Paths.class, export=true), + @Type(java.nio.file.Paths.class), @Type(java.nio.file.AccessMode.class), @Type(java.util.function.UnaryOperator.class), @@ -37,5 +38,6 @@ }) package org.bsc.java2ts.jdk8; + import org.bsc.processor.annotation.Java2TS; import org.bsc.processor.annotation.Type; diff --git a/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java b/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java index 2b6f087..372fedb 100644 --- a/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java +++ b/processor/src/main/java/org/bsc/processor/TypescriptProcessor.java @@ -17,6 +17,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.LinkedHashSet; @@ -97,10 +98,10 @@ private void addDeclaration( java.io.Writer w, TSType t, boolean isRhinoCompatib try { if( isRhinoCompatible ) { - w.append( String.format( "exports.%s:%s\t\t=\t%s;\n", type.getSimpleName(), type.getName(), type.getName())); + w.append( String.format( "export const %s:%s\t\t=\t%s;\n", type.getSimpleName(), type.getName(), type.getName())); } else { - w.append( String.format( "exports.%s:%s\t\t=\tJava.type( \"%s\" );\n", type.getSimpleName(), type.getName(), type.getName())); + 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()); @@ -108,6 +109,27 @@ private void addDeclaration( java.io.Writer w, TSType t, boolean isRhinoCompatib } + /** + * + * @param file + * @param header + * @return + * @throws IOException + */ + private java.io.Writer openFile( Path file, String header ) throws IOException { + + final FileObject out = super.getSourceOutputFile( Paths.get("ts"), file ); + + info( "output file [%s]", out.getName() ); + + final java.io.Writer w = out.openWriter(); + + try(final java.io.InputStream is = getClass().getClassLoader().getResourceAsStream("headerD.ts") ) { + int c; while( (c = is.read()) != -1 ) w.write(c); + } + + return w; + } /** * * @param processingContext @@ -116,58 +138,47 @@ private void addDeclaration( java.io.Writer w, TSType t, boolean isRhinoCompatib @Override public boolean process( Context processingContext ) throws Exception { - final String targetDefinitionFile = processingContext.getOptionMap().getOrDefault("ts.outfile", "out"); - final String compatibility = processingContext.getOptionMap().getOrDefault("compatibility", "nashorn"); + 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 FileObject outD = super.getSourceOutputFile( Paths.get("ts"), Paths.get(targetDefinitionFile.concat(".d.ts"))); - final FileObject outT = super.getSourceOutputFile( Paths.get("ts"), Paths.get(targetDefinitionFile.concat(".ts"))); - - info( "output definition file [%s]", outD.getName() ); - info( "output declaration file [%s]", outT.getName() ); - - final java.io.Writer wD = outD.openWriter(); - final java.io.Writer wT = outT.openWriter(); - - try(final java.io.InputStream is = getClass().getClassLoader().getResourceAsStream("header.ts") ) { - int c; - while( (c = is.read()) != -1 ) wD.write(c); - } - - final List types = enumerateDeclaredPackageAndClass( processingContext ); - - - final List> classes = types.stream() - .peek( t -> addDeclaration(wT, t, compatibility.equalsIgnoreCase("rhino")) ) - .map( t -> t.valueAsClass() ) - .collect( Collectors.toList()); - - - // - // Check for Required classes - // - REQUIRED_CLASSES.stream() - .filter( c -> !classes.contains(c)) - .forEach( c -> classes.add(c) ); - - - final java.util.Map> declaredClasses = classes.stream().collect( Collectors.toMap( clazz -> clazz.getName() , clazz -> clazz )); - - PREDEFINED_CLASSES.forEach( clazz -> declaredClasses.put( clazz.getName(), clazz) ); - - classes.stream() - .filter( clazz -> !PREDEFINED_CLASSES.contains(clazz) ) - .map( clazz -> processClass( getBeanInfo(clazz), declaredClasses)) - .forEach( s -> { - try { - wD.append( s ); - } catch (IOException e) { - error( "error adding [%s]", s); - } - } ); - - wD.close(); - wT.close(); + final List types = enumerateDeclaredPackageAndClass( processingContext ); + + final List> classes = types.stream() + //.peek( t -> addDeclaration(wT, t, compatibility.equalsIgnoreCase("rhino")) ) + .map( t -> t.valueAsClass() ) + .collect( Collectors.toList()); + + + // + // Check for Required classes + // + REQUIRED_CLASSES.stream() + .filter( c -> !classes.contains(c)) + .forEach( c -> classes.add(c) ); + + + final java.util.Map> declaredClasses = classes.stream().collect( Collectors.toMap( clazz -> clazz.getName() , clazz -> clazz )); + + PREDEFINED_CLASSES.forEach( clazz -> declaredClasses.put( clazz.getName(), clazz) ); + + classes.stream() + .filter( clazz -> !PREDEFINED_CLASSES.contains(clazz) ) + .map( clazz -> processClass( getBeanInfo(clazz), declaredClasses)) + .forEach( s -> { + try { + wD.append( s ); + } catch (IOException e) { + error( "error adding [%s]", s); + } + } ); + + } // end try-with-resources return true; } diff --git a/processor/src/main/resources/header.ts b/processor/src/main/resources/headerD.ts similarity index 97% rename from processor/src/main/resources/header.ts rename to processor/src/main/resources/headerD.ts index 64ace1d..c9a95c0 100644 --- a/processor/src/main/resources/header.ts +++ b/processor/src/main/resources/headerD.ts @@ -12,9 +12,6 @@ type char = string; type chararray = [byte]; type bytearray = [char]; -// Nashorn -declare function print( ...args: any[]):void - type Runnable = () => void; interface Supplier { diff --git a/processor/src/main/resources/headerT.ts b/processor/src/main/resources/headerT.ts new file mode 100644 index 0000000..8b8094d --- /dev/null +++ b/processor/src/main/resources/headerT.ts @@ -0,0 +1,3 @@ +/// + +