Skip to content

Commit

Permalink
#6 add namespace class management
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Oct 11, 2020
1 parent 61342a4 commit bd291e3
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 147 deletions.
28 changes: 28 additions & 0 deletions core/src/main/java/org/bsc/java2typescript/TSNamespace.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.bsc.java2typescript;

import java.util.Collections;
import java.util.Set;

public class TSNamespace {

private final String name;

private final Set<TSType> types;

private TSNamespace(String name, Set<TSType> types) {
this.name = name;
this.types = Collections.unmodifiableSet(types);
}

public String getName() {
return name;
}

public Set<TSType> getTypes() {
return types;
}

public static TSNamespace of( String name, Set<TSType> types ) {
return new TSNamespace( name, types );
}
}
10 changes: 9 additions & 1 deletion core/src/main/java/org/bsc/java2typescript/TSType.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ protected TSType() {
super(3);
}

public static TSType from(Class<?> cl) {

public static TSType of() {
return new TSType() {
{
put(VALUE, Void.class);
}
};
}
public static TSType of(Class<?> cl) {
return new TSType() {
{
put(VALUE, cl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ Context getClassDecl() {

sb.append("class ");

final TSType superclass = TSType.from(type.getValue().getSuperclass());
final TSType superclass = TSType.of(type.getValue().getSuperclass());

if (superclass != null) {
inherited.append(" extends ").append(getTypeName(superclass, type, true));
Expand All @@ -275,7 +275,7 @@ Context getClassDecl() {

if (interfaces.length > 0) {

final String ifc = Arrays.stream(interfaces).map(c -> TSType.from(c))
final String ifc = Arrays.stream(interfaces).map(c -> TSType.of(c))
.map(t -> getTypeName(t, type, true)).collect(Collectors.joining(", "));
inherited.append((type.getValue().isInterface()) ? " extends " : " implements ").append(ifc);

Expand Down Expand Up @@ -344,7 +344,7 @@ Context processMemberClasses(int level) {

Stream.of(memberClasses).peek(c -> debug("nested class name[%s]", c.getName()))
// .filter(distinctByKey( c -> c.getSimpleName() ))
.filter(distinctByKey(c -> c.getName())).map(cl -> TSType.from(cl))
.filter(distinctByKey(c -> c.getName())).map(cl -> TSType.of(cl))
.peek(t -> debug("nested type name[%s]", t.getTypeName()))
.map(t -> processClass(level + 1, t, declaredTypeMap))
.forEach(decl -> sb.append(decl));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public abstract class TypescriptConverterStatic {
static final String ENDL = ";\n";

public static final List<TSType> PREDEFINED_TYPES = Arrays.asList(
TSType.from(Class.class),
TSType.from(Serializable.class),
TSType.from(Closeable.class),
TSType.from(AutoCloseable.class),
TSType.from(Cloneable.class),
TSType.from(RandomAccess.class)
TSType.of(Class.class),
TSType.of(Serializable.class),
TSType.of(Closeable.class),
TSType.of(AutoCloseable.class),
TSType.of(Cloneable.class),
TSType.of(RandomAccess.class)
);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class AbstractConverterTest {

protected java.util.Map<String,TSType> declaredClassMap( Class<?> ... classes) {
return Stream.of( classes )
.collect( Collectors.toMap( c -> c.getName(), c -> TSType.from(c) ))
.collect( Collectors.toMap( c -> c.getName(), c -> TSType.of(c) ))
;
}
protected java.util.Map<String,TSType> declaredTypeMap( TSType ... types) {
Expand All @@ -43,7 +43,7 @@ protected String getReturnType( java.util.Map<String, TSType> declaredClassMap,
{
final Type rType = m.getGenericReturnType();
final String result = TypescriptConverter.convertJavaToTS(rType, m,
TSType.from(type),
TSType.of(type),
declaredClassMap,
true,
Optional.empty());
Expand Down
16 changes: 8 additions & 8 deletions core/src/test/java/org/bsc/java2typescript/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public void testMethod1() throws Exception {
final Method m = type.getMethod("method1", java.util.Map.Entry.class);
final String result =
converter.getMethodParametersAndReturnDecl( m,
TSType.from(type),
declaredTypeMap( TSType.from(Map.Entry.class), TSType.from(java.util.List.class)),
TSType.of(type),
declaredTypeMap( TSType.of(Map.Entry.class), TSType.of(java.util.List.class)),
true ) ;

Assert.assertThat( result, IsNull.notNullValue());
Expand All @@ -69,8 +69,8 @@ public void testMethod2() throws Exception {
final Method m = type.getMethod("method2", Function.class);
final String result =
converter.getMethodParametersAndReturnDecl( m,
TSType.from(type),
declaredTypeMap( TSType.from(Function.class), TSType.from(java.util.List.class)),
TSType.of(type),
declaredTypeMap( TSType.of(Function.class), TSType.of(java.util.List.class)),
true) ;

Assert.assertThat( result, IsNull.notNullValue());
Expand All @@ -83,17 +83,17 @@ public void testMethod2() throws Exception {
public void functionalInterfaceTest() {


Assert.assertThat(TSType.from(java.lang.Runnable.class).isFunctional() , equalTo(true));
Assert.assertThat(TSType.of(java.lang.Runnable.class).isFunctional() , equalTo(true));
{
TSType t = TSType.from(Consumer.class);
TSType t = TSType.of(Consumer.class);
Assert.assertThat(t.isFunctional() , equalTo(true));
t.setFunctional(false);
Assert.assertThat(t.isFunctional() , equalTo(true));

}
Assert.assertThat(TSType.from(Action.class).isFunctional() , equalTo(true));
Assert.assertThat(TSType.of(Action.class).isFunctional() , equalTo(true));
{
TSType t = TSType.from(Action2.class);
TSType t = TSType.of(Action2.class);
Assert.assertThat(t.isFunctional() , equalTo(false));
t.setFunctional(true);
Assert.assertThat(t.isFunctional() , equalTo(false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MemeberClassTest extends AbstractConverterTest {
@Test
public void testMemberClassInfos() {

final TSType type = TSType.from( java.util.Map.Entry.class );
final TSType type = TSType.of( java.util.Map.Entry.class );

Assert.assertThat(type.supportNamespace() , equalTo(true));
Assert.assertThat(type.getNamespace() , equalTo("java.util"));
Expand All @@ -35,7 +35,7 @@ public void testMemberClassInfos() {
public void testMemberClass() throws Exception {

TypescriptConverter.Context ctx =
converter.contextOf(TSType.from( java.util.Map.Entry.class ), declaredTypeMap(), Compatibility.NASHORN);
converter.contextOf(TSType.of( java.util.Map.Entry.class ), declaredTypeMap(), Compatibility.NASHORN);

Assert.assertThat(ctx, IsNull.notNullValue());

Expand All @@ -55,8 +55,8 @@ public void testMemberClassUsage() throws Exception {
final Type rType = m.getGenericReturnType();

final String result = TypescriptConverter.convertJavaToTS(rType, m,
TSType.from(type),
declaredTypeMap( TSType.from(java.util.Set.class), TSType.from(java.util.Map.Entry.class)),
TSType.of(type),
declaredTypeMap( TSType.of(java.util.Set.class), TSType.of(java.util.Map.Entry.class)),
true,
Optional.empty());

Expand Down
Loading

0 comments on commit bd291e3

Please sign in to comment.