Skip to content

Commit

Permalink
Merge branch 'release/1.0-beta1'
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Apr 16, 2018
2 parents 9b677a2 + 36fb837 commit 84ca359
Show file tree
Hide file tree
Showing 15 changed files with 318 additions and 294 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ It is not a transpiler from Java to Javascript like [datathings/java2typescript
## Similar projects

* [typescript-generator](https://github.com/vojtechhabarta/typescript-generator)
> typescript-generator is a tool for generating TypeScript definition files (.d.ts) from Java JSON classes. If you have REST service written in Java using object to JSON mapping you can use typescript-generator to generate TypeScript interfaces from Java classes.
* [1c](https://1c.wizawu.com/#/whatis)
> 1c is aimed to compile TypeScript to runnable code on both JVM and web browser. You can implement the full stack with TypeScript while using libraries from Maven and NPM together.
## Related Project

* [jvm-npm](https://github.com/bsorrentino/jvm-npm)
> NPM compliant CommonJS module loader for the JVM
## Description

Expand Down Expand Up @@ -123,5 +127,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=0.2.0
>-DarchetypeVersion=1.0-beta1
>```
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>0.2.0</version>
<version>1.0-beta1</version>
</parent>
<artifactId>java2ts-processor-archetype</artifactId>
<name>java2ts-processor::archetype - ${project.version}</name>
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>0.2.0</version>
<version>1.0-beta1</version>
</parent>
<artifactId>java2ts-processor-core</artifactId>
<name>java2ts-processor::core - ${project.version}</name>
Expand Down
324 changes: 164 additions & 160 deletions core/src/main/java/org/bsc/java2typescript/TSType.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,183 +8,187 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;


import static java.lang.String.format;
/**
*
* @author softphone
*
*/
@SuppressWarnings("serial")
public class TSType extends HashMap<String,Object> {

protected TSType() {
super(3);
}

public static TSType from( Class<?> cl, boolean export ) {
return new TSType() {{
put( "value", cl);
put( "export", export);
}};
}

public static TSType from( Class<?> cl, String alias, boolean export ) {
return new TSType() {{
put( "value", cl);
put( "export", export);
put( "alias", alias);
}};
}

/**
* Assume that class is managed as Functional Interface
*
* @param cl
* @param alias
* @return
*/
public static TSType functional( Class<?> cl, String alias ) {
return new TSType() {{
put( "value", cl);
put( "alias", alias);
put("functional", true);
}};
}

public static TSType from( Class<?> cl ) {
return new TSType() {{ put( "value", cl); }};
}

/**
*
* @return
*/
public Class<?> getValue() {
return getClassFrom(super.get("value"));
}

/**
*
* @return
*/
public boolean isExport() {
return (boolean) super.getOrDefault("export", false);
}

/**
*
* @return
*/
public TSType setExport( boolean export ) {
super.put("export", export);
return this;
}

/**
*
* @return
*/
public boolean isFunctionalInterface() {
return ((boolean) super.getOrDefault("functional", false)) ||
(getValue().isInterface() && getValue().isAnnotationPresent(FunctionalInterface.class));
}

/**
*
* @return
*/
public boolean hasAlias() {
final String alias = (String) super.get("alias");
return alias!=null && !alias.isEmpty();
}

/**
*
* @return
*/
public String getAlias() {
return (String) super.get("alias");
}

/**
*
* @return
*/
public final String getTypeName() {
return (hasAlias()) ? getAlias() : getValue().getName();
}

/**
*
* @return
*/
public final String getSimpleTypeName() {
return (hasAlias()) ? getAlias() : getValue().getSimpleName();
}
public class TSType extends HashMap<String, Object> {

private static final String ALIAS = "alias";
private static final String VALUE = "value";
private static final String EXPORT = "export";
private static final String NAMESPACE = "namespace";

protected TSType() {
super(3);
}

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

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

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

/**
*
* @param type
* @return
*/
public Set<Field> getFields() {

final Predicate<Field> std = f ->
!f.isSynthetic() &&
Modifier.isPublic( f.getModifiers() ) &&
Character.isJavaIdentifierStart(f.getName().charAt(0)) &&
f.getName().chars().skip(1).allMatch(Character::isJavaIdentifierPart);

return Stream.concat( Stream.of(getValue().getFields()), Stream.of(getValue().getDeclaredFields()) )
.filter(std)
.collect( Collectors.toSet( ) );

}

*
* @return
*/
public Class<?> getValue() {
return getClassFrom(super.get(VALUE));
}

/**
*
* @param type
* @return
*/
public Set<Method> getMethods() {
final Predicate<Method> include = m ->
!m.isBridge() &&
!m.isSynthetic() &&
Modifier.isPublic( m.getModifiers() ) &&
Character.isJavaIdentifierStart(m.getName().charAt(0)) &&
m.getName().chars().skip(1).allMatch(Character::isJavaIdentifierPart);

return Stream.concat( Stream.of(getValue().getMethods()), Stream.of(getValue().getDeclaredMethods()) )
.filter(include)
.collect( Collectors.toSet( ) );

}


*
* @return
*/
public boolean isExport() {
return (boolean) super.getOrDefault(EXPORT, false);
}

/**
*
* @return
*/
public TSType setExport(boolean exports) {
super.put(EXPORT, exports);
return this;
}

/**
*
* @return
*/
public boolean hasAlias() {
final String alias = (String) super.get(ALIAS);
return alias != null && !alias.isEmpty();
}

/**
*
* @return
*/
public String getAlias() {
return (String) super.get(ALIAS);
}

/**
*
* @return
*/
public final String getTypeName() {
return (hasAlias()) ? getAlias() : format( "%s.%s", getNamespace(), getValue().getSimpleName());
}

/**
*
* @return
*/
public final String getSimpleTypeName() {
return (hasAlias()) ? getAlias() : getValue().getSimpleName();
}

/**
*
* @return
*/
public final boolean supportNamespace() {
return !getValue().isMemberClass() && !hasAlias();
}

/**
*
* @return
*/
public final String getNamespace() {
return (String) super.getOrDefault(NAMESPACE, getValue().getPackage().getName());
}

/**
*
* @return
*/
public boolean isFunctionalInterface() {
return TypescriptConverter.isFunctionalInterface( getValue() );
}
/**
*
* @param type
* @return
*/
public Set<Field> getFields() {

final Predicate<Field> std = f -> !f.isSynthetic() && Modifier.isPublic(f.getModifiers())
&& Character.isJavaIdentifierStart(f.getName().charAt(0))
&& f.getName().chars().skip(1).allMatch(Character::isJavaIdentifierPart);

return Stream.concat(Stream.of(getValue().getFields()), Stream.of(getValue().getDeclaredFields())).filter(std)
.collect(Collectors.toSet());

}

/**
*
* @param type
* @return
*/
public Set<Method> getMethods() {
final Predicate<Method> include = m -> !m.isBridge() && !m.isSynthetic() && Modifier.isPublic(m.getModifiers())
&& Character.isJavaIdentifierStart(m.getName().charAt(0))
&& m.getName().chars().skip(1).allMatch(Character::isJavaIdentifierPart);

return Stream.concat(Stream.of(getValue().getMethods()), Stream.of(getValue().getDeclaredMethods()))
.filter(include).collect(Collectors.toSet());

}

/**
*
* @param dt
* @return
*/
private Class<?> getClassFrom( Object dt ) {
if( dt instanceof Class ) return (Class<?>)dt;

try {
private Class<?> getClassFrom(Object dt) {
if (dt instanceof Class)
return (Class<?>) dt;

try {
return Class.forName(dt.toString());
} catch (ClassNotFoundException e1) {
throw new RuntimeException(String.format("class not found [%s]",dt), e1);
throw new RuntimeException(String.format("class not found [%s]", dt), e1);
}
}
@Override
public boolean equals(Object o) {
return getValue().equals(((TSType)o).getValue());
}

@Override
public int hashCode() {
return getValue().hashCode();
}

@Override
public boolean equals(Object o) {
return getValue().equals(((TSType) o).getValue());
}

@Override
public int hashCode() {
return getValue().hashCode();
}

}
Loading

0 comments on commit 84ca359

Please sign in to comment.