Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Jun 5, 2018
2 parents d0f11e4 + 3ac2763 commit d18a321
Show file tree
Hide file tree
Showing 17 changed files with 236 additions and 114 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ Name | Mandatory | Type | Description
--- | --- | --- | ---
**value** | Yes | Class | Full Qualified Name of Java class
**alias** | No | String | Assign a new name to exported class in typescript
**export** | No | boolean | if **true**, other than typescript declaration adds definition in file *`-types.ts`. **It is need for instantiable classes, in order to use `new` operator or factory method(s)**
**export** | No | boolean | If **true**, other than typescript declaration adds definition in file *`-types.ts`. **It is need for instantiable classes, in order to use `new` operator or factory method(s)**
**functional** | No | boolean | If **true** other than typescript declaration adds a **particular** definition. Valid only for interfaces that have one abstract method and haven't `@FunctionalInterface` annotation

**Example**
```Java
Expand All @@ -96,6 +97,8 @@ Name | Mandatory | Type | Description
@Type(value=java.net.URI.class, export=true),
@Type(java.net.URL.class),

@Type(java.lang.Runnable.class, functional=true)

})
package org.mypackage;
```
Expand Down Expand Up @@ -146,5 +149,5 @@ The easier way to start your **Typescript on JVM** project is using the provided
>mvn archetype:generate \
>-DarchetypeGroupId=org.bsc.processor \
>-DarchetypeArtifactId=java2ts-processor-archetype \
>-DarchetypeVersion=1.0-rc1
>-DarchetypeVersion=1.0.0
>```
2 changes: 1 addition & 1 deletion archetype/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.bsc.processor</groupId>
<artifactId>java2ts-processor-parent</artifactId>
<version>1.0-rc1</version>
<version>1.0.0</version>
</parent>
<artifactId>java2ts-processor-archetype</artifactId>
<name>java2ts-processor::archetype - ${project.version}</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
@Java2TS(declare = {
@Type(value=java.util.Arrays.class, export=true),

@Java2TS(declare = {
// Declare class(s)
@Type(java.nio.file.Path.class),
@Type(value=java.net.URI.class, export=true),
@Type(java.net.URL.class),

// Utility Class(s)
@Type(value=java.nio.file.Paths.class, export=true),
@Type(value=java.util.Arrays.class, export=true),
@Type(value=java.nio.file.Files.class, export=true),
@Type(java.nio.file.Path.class),
@Type(value=java.nio.file.Paths.class, export=true),

@Type(value=java.net.URI.class, export=true),
@Type(java.net.URL.class)
// Member Class(s)
@Type(value=java.util.Map.Entry.class),

// Native functional Interface(s)
@Type(value=java.lang.Callable.class),

// Declare as Functional Interface


})

Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.bsc.processor</groupId>
<artifactId>java2ts-processor-parent</artifactId>
<version>1.0-rc1</version>
<version>1.0.0</version>
</parent>
<artifactId>java2ts-processor-core</artifactId>
<name>java2ts-processor::core - ${project.version}</name>
Expand Down
70 changes: 42 additions & 28 deletions core/src/main/java/org/bsc/java2typescript/TSType.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.lang.String.format;
import java.util.Arrays;

/**
*
* @author softphone
Expand All @@ -21,30 +23,12 @@ public class TSType extends HashMap<String, Object> {
private static final String VALUE = "value";
private static final String EXPORT = "export";
private static final String NAMESPACE = "namespace";
private static final String FUNCTIONAL = "functional";

protected TSType() {
super(3);
}

public static TSType from(Class<?> cl, boolean exports) {
return new TSType() {
{
put(VALUE, cl);
put(EXPORT, exports);
}
};
}

public static TSType from(Class<?> cl, String alias, boolean exports) {
return new TSType() {
{
put(VALUE, cl);
put(EXPORT, exports);
put(ALIAS, alias);
}
};
}

public static TSType from(Class<?> cl) {
return new TSType() {
{
Expand Down Expand Up @@ -73,8 +57,8 @@ public boolean isExport() {
*
* @return
*/
public TSType setExport(boolean exports) {
super.put(EXPORT, exports);
public TSType setExport(boolean value) {
super.put(EXPORT, value);
return this;
}

Expand All @@ -95,6 +79,43 @@ public String getAlias() {
return (String) super.get(ALIAS);
}

/**
*
* @return
*/
public TSType setAlias( String value ) {
super.put(ALIAS,value);
return this;
}

/**
* Test is functional interface
*
* @return
*/
public boolean isFunctional() {


if( !getValue().isInterface()) return false;
if( getValue().isAnnotationPresent(FunctionalInterface.class)) return true;

return (Boolean)super.getOrDefault( FUNCTIONAL, false) &&
Arrays.stream(getValue().getDeclaredMethods())
.filter( m -> Modifier.isAbstract(m.getModifiers()) )
.count() == 1;
}

/**
*
*/
public TSType setFunctional( boolean value ) {

super.put(FUNCTIONAL, value);
return this;

}


private String getMemberSimpleTypeName() {

return format( "%s$%s", getValue().getDeclaringClass().getSimpleName(), getValue().getSimpleName());
Expand Down Expand Up @@ -134,13 +155,6 @@ public final String getNamespace() {
return (String) super.getOrDefault(NAMESPACE, getValue().getPackage().getName());
}

/**
*
* @return
*/
public boolean isFunctionalInterface() {
return TypescriptConverter.isFunctionalInterface( getValue() );
}
/**
*
* @param type
Expand Down
56 changes: 23 additions & 33 deletions core/src/main/java/org/bsc/java2typescript/TypescriptConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,6 @@ protected final static void debug( String fmt, Object ...args ) {
System.out.println( format( fmt, args));
}


/**
*
* @param type
* @return
*/
public static final String processFunctionalInterface( TSType type ) {
Objects.requireNonNull(type, "argument 'type' is not defined!");

return null;
}

/**
*
* @param p
Expand Down Expand Up @@ -123,17 +111,6 @@ static boolean isFactoryMethod( Method m ) {
return (isStatic(m) &&
m.getReturnType().equals(m.getDeclaringClass()));
}

/**
*
* @return
*/
public static boolean isFunctionalInterface( final Class<?> c ) {
if( !c.isInterface()) return false;
if( c.isAnnotationPresent(FunctionalInterface.class)) return true;

return Arrays.stream(c.getDeclaredMethods()).filter( m -> Modifier.isAbstract(m.getModifiers()) ).count() == 1;
}

/**
*
Expand Down Expand Up @@ -205,6 +182,7 @@ public static <M extends Member> String convertJavaToTS(
Objects.requireNonNull(declaringType, "declaringType argument is null!");
Objects.requireNonNull(declaredTypeMap, "declaredTypeMap argument is null!");

log( "PROCESSING MEMEBER: [%s]", declaringMember.getName());
/**
*
*/
Expand Down Expand Up @@ -247,6 +225,7 @@ public static <M extends Member> String convertJavaToTS(
final Type[] typeArgs = pType.getActualTypeArguments();

for( Type t : typeArgs ) {
log( "TypeArgs [%s]", t.getTypeName());

if( t instanceof ParameterizedType ) {

Expand All @@ -256,8 +235,8 @@ public static <M extends Member> String convertJavaToTS(
declaredTypeMap,
packageResolution,
onTypeMismatch);
log( "Parameterized Type %s - %s", t.getTypeName(), typeName );
result = result.replace( t.getTypeName(), typeName);
log( "Parameterized Type\n\t%s\n\t%s\n\t%s", t.getTypeName(), typeName, result );

}
else if( t instanceof TypeVariable ) {
Expand All @@ -281,7 +260,6 @@ else if( t instanceof TypeVariable ) {
continue;
}
else if( t instanceof Class ) {
log( "class: %s", t.getTypeName() );

final String name = convertJavaToTS( (Class<?>)t, declaringType, declaredTypeMap, packageResolution, onTypeMismatch);

Expand All @@ -290,14 +268,16 @@ else if( t instanceof Class ) {
.replace(t.getTypeName(), name)
.replace( "/*@*/", commented )
;
}
log( "Class Type\n\t%s\n\t%s", t.getTypeName(), result );
}
else if( t instanceof WildcardType ) {

final WildcardType wt = (WildcardType) t;

final Type[] lb = wt.getLowerBounds();
final Type[] ub = wt.getUpperBounds();
log( "Wildcard Type : %s lb:%d up:%d", wt.getTypeName(), lb.length, ub.length );

log( "Wildcard Type: \n\t%s\n\tlb:%d\n\tup:%d", wt.getTypeName(), lb.length, ub.length );

if( lb.length <= 1 && ub.length==1) {
final Type tt = (lb.length==1) ? lb[0] : ub[0];
Expand All @@ -312,6 +292,7 @@ else if( t instanceof WildcardType ) {
result = result.replace( wt.getTypeName(), s);

if( tt instanceof ParameterizedType ) {

// FIX ISSUE #7
result = result.replace( "? extends ", "" );

Expand All @@ -321,13 +302,20 @@ else if( t instanceof WildcardType ) {
{
final Class<?> clazz = (Class<?>) (((ParameterizedType)tt).getRawType());

final String typeName = wt.getTypeName().replace( clazz.getName(), clazz.getSimpleName());
final String typeName = wt.getTypeName()
//.replace( clazz.getName(), clazz.getSimpleName())
.replace( "? extends ", "" )
;
result = result.replace( typeName, s);

}

}
log( "Wildcard Type(1):\n\t%s\n\t%s\n\t%s", t.getTypeName(), s, result );
}
else {
result = result.replace(wt.getTypeName(), format( "any/*%s*/", wt));
log( "Wildcard Type(2)\n\t%s\n\t%s", t.getTypeName(), result );
}
}
else if( t instanceof GenericArrayType ) {
Expand Down Expand Up @@ -358,8 +346,10 @@ else if( type instanceof TypeVariable ) {
return type.getTypeName();
}
else if( type instanceof Class ) {
log( "class: %s", type.getTypeName() );
return convertJavaToTS( (Class<?>)type, declaringType, declaredTypeMap, packageResolution, onTypeMismatch);
final String result = convertJavaToTS( (Class<?>)type, declaringType, declaredTypeMap, packageResolution, onTypeMismatch);
log( "class:\n\t%s\n\t%s", type.getTypeName(), result );
return result;

}
else if( type instanceof WildcardType ) {
throw new IllegalArgumentException( "type 'WildcardType' is a not supported yet!");
Expand Down Expand Up @@ -516,7 +506,7 @@ public String processStatic( TSType type, java.util.Map<String, TSType> declared
//Append class property
ctx.append("\treadonly class:any;\n");

if( isFunctionalInterface( type.getValue() ) ) {
if( type.isFunctional() ) {

final java.util.Set<String> TypeVarSet = new java.util.HashSet<>(5);
final String tstype = convertJavaToTS( type.getValue(),
Expand Down Expand Up @@ -928,7 +918,7 @@ public String processClass( int level, TSType tstype, java.util.Map<String, TSTy

ctx.getClassDecl().append("\n\n");

if( tstype.isFunctionalInterface() ) {
if( tstype.isFunctional() ) {

tstype.getMethods().stream()
.filter( m -> Modifier.isAbstract(m.getModifiers()) )
Expand Down
Loading

0 comments on commit d18a321

Please sign in to comment.