|
| 1 | +package org.bsc.java2typescript; |
| 2 | + |
| 3 | +import org.bsc.java2typescript.transformer.TSJavaClass2DeclarationTransformer; |
| 4 | +import org.bsc.java2typescript.transformer.TSJavaClass2StaticDefinitionTransformer; |
| 5 | + |
| 6 | +import java.lang.reflect.Executable; |
| 7 | +import java.util.Optional; |
| 8 | + |
| 9 | +import static java.lang.String.format; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author bsorrentino |
| 13 | + */ |
| 14 | +public class Java2TSConverter extends TSConverterStatic { |
| 15 | + public static class Builder { |
| 16 | + |
| 17 | + private Compatibility compatibility = Compatibility.NASHORN; |
| 18 | + private boolean foreignObjectPrototype = false; |
| 19 | + |
| 20 | + private Builder() {} |
| 21 | + |
| 22 | + public Builder compatibility(Compatibility compatibility) { |
| 23 | + this.compatibility = compatibility; |
| 24 | + return this; |
| 25 | + } |
| 26 | + |
| 27 | + public Builder compatibility(String compatibility) { |
| 28 | + |
| 29 | + this.compatibility |
| 30 | + = Optional.ofNullable(compatibility) |
| 31 | + .map( v -> { |
| 32 | + try { |
| 33 | + return Java2TSConverter.Compatibility.valueOf(v.toUpperCase()); |
| 34 | + } catch( Exception e ) { |
| 35 | + return Compatibility.NASHORN; |
| 36 | + } |
| 37 | + }) |
| 38 | + .orElse( Compatibility.NASHORN ); |
| 39 | + return this; |
| 40 | + } |
| 41 | + |
| 42 | + public Builder foreignObjectPrototype(boolean foreignObjectPrototype) { |
| 43 | + this.foreignObjectPrototype = foreignObjectPrototype; |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + public Builder foreignObjectPrototype(String foreignObjectPrototype) { |
| 48 | + this.foreignObjectPrototype = |
| 49 | + Optional.ofNullable(foreignObjectPrototype) |
| 50 | + .map( v -> { |
| 51 | + try { |
| 52 | + return Boolean.valueOf(v); |
| 53 | + } catch( Exception e ) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + }) |
| 57 | + .orElse( false ); |
| 58 | + |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + public Java2TSConverter build() { |
| 63 | + return new Java2TSConverter( |
| 64 | + new Options( compatibility, foreignObjectPrototype) ); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * |
| 70 | + * @return |
| 71 | + */ |
| 72 | + public static Builder builder() { |
| 73 | + return new Builder(); |
| 74 | + } |
| 75 | + |
| 76 | + public enum Compatibility { |
| 77 | + NASHORN, RHINO, GRAALJS; |
| 78 | + |
| 79 | + public String javaType(String fqn) { |
| 80 | + switch (this.ordinal()) { |
| 81 | + case 1: |
| 82 | + return format("Packages.%s", fqn); |
| 83 | + default: |
| 84 | + return format("Java.type(\"%s\")", fqn); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static class Options { |
| 90 | + public final Compatibility compatibility; |
| 91 | + public final boolean foreignObjectPrototype; |
| 92 | + |
| 93 | + private Options(Compatibility compatibility, boolean foreignObjectPrototype) { |
| 94 | + this.compatibility = compatibility; |
| 95 | + this.foreignObjectPrototype = foreignObjectPrototype; |
| 96 | + } |
| 97 | + |
| 98 | + public static Options of(Compatibility compatibility, boolean foreignObjectPrototype) { |
| 99 | + return new Options(compatibility, foreignObjectPrototype); |
| 100 | + } |
| 101 | + |
| 102 | + public static Options of(Compatibility compatibility) { |
| 103 | + return new Options(compatibility, false); |
| 104 | + } |
| 105 | + |
| 106 | + public static Options ofDefault() { |
| 107 | + return new Options(Compatibility.NASHORN, false); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + final Options options; |
| 112 | + |
| 113 | + final TSJavaClass2DeclarationTransformer javaClass2DeclarationTransformer; |
| 114 | + final TSJavaClass2StaticDefinitionTransformer JavaClass2StaticDefinitionTransformer; |
| 115 | + |
| 116 | + private Java2TSConverter(Options options) { |
| 117 | + super(); |
| 118 | + this.options = options; |
| 119 | + |
| 120 | + javaClass2DeclarationTransformer = new TSJavaClass2DeclarationTransformer(); |
| 121 | + JavaClass2StaticDefinitionTransformer = new TSJavaClass2StaticDefinitionTransformer(); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @return |
| 126 | + */ |
| 127 | + public final boolean isRhino() { |
| 128 | + return options.compatibility == Compatibility.RHINO; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @param tstype |
| 133 | + * @param declaredTypeMap |
| 134 | + * @return |
| 135 | + */ |
| 136 | + public String javaClass2StaticDefinitionTransformer(TSType tstype, java.util.Map<String, TSType> declaredTypeMap) { |
| 137 | + |
| 138 | + final TSConverterContext ctx = TSConverterContext.of(tstype, declaredTypeMap, options); |
| 139 | + |
| 140 | + return JavaClass2StaticDefinitionTransformer |
| 141 | + .apply(ctx) |
| 142 | + .toString(); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param m |
| 147 | + * @param type |
| 148 | + * @param declaredTypeMap |
| 149 | + * @param packageResolution |
| 150 | + * @return |
| 151 | + */ |
| 152 | + public <E extends Executable> String getMethodParametersAndReturnDecl(E m, TSType type, |
| 153 | + java.util.Map<String, TSType> declaredTypeMap, boolean packageResolution) { |
| 154 | + |
| 155 | + return TSConverterContext.of(type, declaredTypeMap, options) |
| 156 | + .getMethodParametersAndReturnDecl(m, packageResolution); |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + /** |
| 161 | + * @param level |
| 162 | + * @param tstype |
| 163 | + * @param declaredTypeMap |
| 164 | + * @return |
| 165 | + */ |
| 166 | + public String javaClass2DeclarationTransformer(int level, TSType tstype, java.util.Map<String, TSType> declaredTypeMap) { |
| 167 | + |
| 168 | + final TSConverterContext ctx = TSConverterContext.of(tstype, declaredTypeMap, options); |
| 169 | + |
| 170 | + return javaClass2DeclarationTransformer |
| 171 | + .apply(ctx) |
| 172 | + .toString(); |
| 173 | + |
| 174 | + |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments