Skip to content

Commit a4f5384

Browse files
committed
fix(converter): support for 'foreign-object-prototype'
if options compatibility=graaljs and foreignobjectprototype=true the conversion on a java.util.List remove methods which signature clash with javascript Array issue #34
1 parent 213c383 commit a4f5384

File tree

12 files changed

+328
-160
lines changed

12 files changed

+328
-160
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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+
}

core/src/main/java/org/bsc/java2typescript/TSConverter.java

-92
This file was deleted.

core/src/main/java/org/bsc/java2typescript/TSConverterContext.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class TSConverterContext extends TSConverterStatic implements Cloneable {
1212
public final TSType type;
1313
public final java.util.Map<String, TSType> declaredTypeMap;
14-
public final TSConverter.Compatibility compatibility;
14+
public final Java2TSConverter.Options options;
1515
final StringBuilder sb = new StringBuilder();
1616

1717
/**
@@ -21,18 +21,26 @@ public class TSConverterContext extends TSConverterStatic implements Cloneable {
2121
*/
2222
public static TSConverterContext of(TSType tstype,
2323
java.util.Map<String, TSType> declaredTypeMap,
24-
TSConverter.Compatibility compatibility) {
25-
return new TSConverterContext(tstype, declaredTypeMap, compatibility);
24+
Java2TSConverter.Options options) {
25+
return new TSConverterContext(tstype, declaredTypeMap, options);
2626
}
2727

2828

29-
private TSConverterContext(TSType type, Map<String, TSType> declaredClassMap, TSConverter.Compatibility compatibility) {
29+
private TSConverterContext(TSType type, Map<String, TSType> declaredClassMap, Java2TSConverter.Options options) {
3030
Objects.requireNonNull(type, "type is null!");
3131
Objects.requireNonNull(declaredClassMap, "declaredClassMap is null!");
3232

3333
this.type = type;
3434
this.declaredTypeMap = declaredClassMap;
35-
this.compatibility = compatibility;
35+
this.options = options;
36+
}
37+
38+
/**
39+
*
40+
* @return
41+
*/
42+
public Java2TSConverter.Options getOptions() {
43+
return options;
3644
}
3745

3846
/**
@@ -253,7 +261,7 @@ public String getMethodDecl(final Method m, boolean optional) {
253261
*/
254262
public TSConverterContext clone() {
255263

256-
return new TSConverterContext(type, declaredTypeMap, compatibility);
264+
return new TSConverterContext(type, declaredTypeMap, options);
257265
}
258266

259267
/**

core/src/main/java/org/bsc/java2typescript/TSConverterStatic.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static String getClassParametersDecl( java.util.List<String> type_paramet
126126
}
127127

128128
private static StringBuilder loadResourceByName( String name, StringBuilder result ) throws IOException {
129-
try(final java.io.InputStream is = TSConverter.class.getClassLoader().getResourceAsStream(name) ) {
129+
try(final java.io.InputStream is = Java2TSConverter.class.getClassLoader().getResourceAsStream(name) ) {
130130
int c; while( (c = is.read()) != -1 ) result.append((char)c);
131131
}
132132

core/src/main/java/org/bsc/java2typescript/TSType.java

+21-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
* @author bsorrentino
1717
*
1818
*/
19-
@SuppressWarnings("serial")
2019
public class TSType extends HashMap<String, Object> {
2120

2221
private static final String ALIAS = "alias";
@@ -165,7 +164,6 @@ public final String getNamespace() {
165164

166165
/**
167166
*
168-
* @param type
169167
* @return
170168
*/
171169
public Set<Field> getFields() {
@@ -181,22 +179,37 @@ public Set<Field> getFields() {
181179

182180
/**
183181
*
184-
* @param type
182+
* @param m
185183
* @return
186184
*/
187-
public Set<Method> getMethods() {
188-
final Predicate<Method> include = m -> !m.isBridge() && !m.isSynthetic() && Modifier.isPublic(m.getModifiers())
185+
protected boolean testIncludeMethod( Method m ) {
186+
return !m.isBridge() && !m.isSynthetic() && Modifier.isPublic(m.getModifiers())
189187
&& Character.isJavaIdentifierStart(m.getName().charAt(0))
190188
&& m.getName().chars().skip(1).allMatch(Character::isJavaIdentifierPart);
189+
}
191190

192-
return Stream.concat(Stream.of(getValue().getMethods()), Stream.of(getValue().getDeclaredMethods()))
193-
.filter(include).collect(Collectors.toSet());
191+
/**
192+
*
193+
* @return
194+
*/
195+
public Stream<Method> getMethodsAsStream() {
194196

197+
return Stream.concat(
198+
Stream.of(getValue().getMethods()),
199+
Stream.of(getValue().getDeclaredMethods()))
200+
.filter(this::testIncludeMethod);
201+
}
202+
203+
/**
204+
*
205+
* @return
206+
*/
207+
public final Set<Method> getMethods() {
208+
return getMethodsAsStream().collect(Collectors.toSet());
195209
}
196210

197211
/**
198212
*
199-
* @param name
200213
* @return
201214
*/
202215
private Class<?> getMemberClassForName( String fqn ) throws ClassNotFoundException {

0 commit comments

Comments
 (0)