From 8bb9b892432d558ab888093401bb7b6ca8df975e Mon Sep 17 00:00:00 2001 From: Sebastian Schneider Date: Wed, 22 Jul 2020 12:28:35 +0200 Subject: [PATCH] Added support for typscript string enums. --- .../src/main/java/jsweet/lang/StringEnum.java | 41 +++++++++++++++++++ .../main/java/org/jsweet/JSweetConfig.java | 14 ++++--- .../transpiler/Java2TypeScriptTranslator.java | 38 +++++++++++------ 3 files changed, 76 insertions(+), 17 deletions(-) create mode 100644 core-lib/es6/src/main/java/jsweet/lang/StringEnum.java diff --git a/core-lib/es6/src/main/java/jsweet/lang/StringEnum.java b/core-lib/es6/src/main/java/jsweet/lang/StringEnum.java new file mode 100644 index 00000000..e8106022 --- /dev/null +++ b/core-lib/es6/src/main/java/jsweet/lang/StringEnum.java @@ -0,0 +1,41 @@ +/* + * JSweet - http://www.jsweet.org + * Copyright (C) 2015 CINCHEO SAS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package jsweet.lang; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation is used to annotate enums to generate a TS string enum. + * + * @author Sebastian Schneider + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE }) +@Documented +public @interface StringEnum { + + /** + * Defines the actual value of the string type, to be used in case the + * string is not a valid Java identifier. + */ + String value() default ""; + +} diff --git a/transpiler/src/main/java/org/jsweet/JSweetConfig.java b/transpiler/src/main/java/org/jsweet/JSweetConfig.java index 3d584b53..9bd31f92 100644 --- a/transpiler/src/main/java/org/jsweet/JSweetConfig.java +++ b/transpiler/src/main/java/org/jsweet/JSweetConfig.java @@ -1,12 +1,12 @@ -/* +/* * JSweet transpiler - http://www.jsweet.org * Copyright (C) 2015 CINCHEO SAS - * + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -32,7 +32,7 @@ import org.apache.log4j.Logger; /** * This class contains static constants and utilities. - * + * * @author Renaud Pawlak */ @SuppressWarnings("serial") @@ -73,7 +73,7 @@ public abstract class JSweetConfig { /** * Initialize the classpath to include tools.jar. - * + * * @param jdkHome * the jdkHome option value (if not set or not found, fall back * to the JAVA_HOME environment variable) @@ -295,6 +295,10 @@ public abstract class JSweetConfig { * Fully-qualified name for the JSweet @StringType annotation (see JSweet core API). */ public static final String ANNOTATION_STRING_TYPE = JSweetConfig.LANG_PACKAGE + ".StringType"; + /** + * Fully-qualified name for the JSweet @StringEnum annotation (see JSweet core API). + */ + public static final String ANNOTATION_STRING_ENUM = JSweetConfig.LANG_PACKAGE + ".StringEnum"; /** * Fully-qualified name for the JSweet @Root annotation (see JSweet core API). */ diff --git a/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java b/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java index b880fb6d..8bc03bca 100644 --- a/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java +++ b/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java @@ -1,12 +1,12 @@ -/* +/* * JSweet transpiler - http://www.jsweet.org * Copyright (C) 2015 CINCHEO SAS - * + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -20,6 +20,7 @@ package org.jsweet.transpiler; import static org.apache.commons.lang3.StringUtils.isBlank; import static org.jsweet.JSweetConfig.ANNOTATION_ERASED; +import static org.jsweet.JSweetConfig.ANNOTATION_STRING_ENUM; import static org.jsweet.JSweetConfig.UTIL_CLASSNAME; import static org.jsweet.JSweetConfig.ANNOTATION_FUNCTIONAL_INTERFACE; import static org.jsweet.JSweetConfig.ANNOTATION_OBJECT_TYPE; @@ -162,7 +163,7 @@ import com.sun.tools.javac.util.Name; /** * This is a TypeScript printer for translating the Java AST to a TypeScript * program. - * + * * @author Renaud Pawlak */ public class Java2TypeScriptTranslator extends AbstractTreePrinter { @@ -248,7 +249,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { /** * A state flag indicating the comparison mode to be used by this printer for * printing comparison operators. - * + * * @author Renaud Pawlak */ public static enum ComparisonMode { @@ -273,7 +274,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { /** * Selects a comparison mode for subsequently printed comparison operators. - * + * * @see #exitComparisonMode() */ public void enterComparisonMode(ComparisonMode comparisonMode) { @@ -282,7 +283,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { /** * Exits a comparison mode and go back to the previous one. - * + * * @see #enterComparisonMode(ComparisonMode) */ public void exitComparisonMode() { @@ -314,6 +315,8 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { private boolean enumWrapperClassScope = false; + private boolean enumString = false; + private boolean removedSuperclass = false; private boolean declareClassScope; @@ -497,7 +500,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { /** * Enters a new class scope. - * + * * @see #exitScope() */ public void enterScope() { @@ -506,7 +509,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { /** * Exits a class scope. - * + * * @see #enterScope() */ public void exitScope() { @@ -515,7 +518,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { /** * Creates a new TypeScript translator. - * + * * @param adapter an object that can tune various aspects of the * TypeScript code generation * @param logHandler the handler for logging and error reporting @@ -1501,6 +1504,11 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { } else { print("enum "); getScope().enumScope = true; + if (context.hasAnnotationType(classdecl.sym, ANNOTATION_STRING_ENUM)) { + getScope().enumString = true; + getScope().enumWrapperClassScope = false; + getScope().isComplexEnum = false; + } } } else { if (getScope().declareClassScope && !(getIndent() != 0 && isDefinitionScope)) { @@ -1774,6 +1782,12 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { } int pos = getCurrentPosition(); print(def); + + if (getScope().enumScope && JCTree.Tag.VARDEF == def.getTag() && getScope().enumString) { + print(" = \""); + print(def); + print("\""); + } if (getCurrentPosition() == pos) { if (!getScope().enumScope) { removeLastIndent(); @@ -2695,7 +2709,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { /** * Print async keyword for given method if relevant. Prints nothing if method * shouldn't be async - * + * */ protected void printAsyncKeyword(JCMethodDecl methodDecl) { if (getScope().declareClassScope || getScope().interfaceScope) { @@ -5812,7 +5826,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { /** * Prints either a string, or the tree if the the string is null. - * + * * @param exprStr a string to be printed as is if not null * @param expr a tree to be printed if exprStr is null */