diff --git a/transpiler/src/main/java/org/jsweet/transpiler/JSweetContext.java b/transpiler/src/main/java/org/jsweet/transpiler/JSweetContext.java index 06ce2831..01f18557 100644 --- a/transpiler/src/main/java/org/jsweet/transpiler/JSweetContext.java +++ b/transpiler/src/main/java/org/jsweet/transpiler/JSweetContext.java @@ -987,10 +987,14 @@ public class JSweetContext extends Context { * Gets the first value of the 'value' property for the given annotation * type if found on the given symbol. */ - public String getAnnotationValue(Symbol symbol, String annotationType, String defaultValue) { + public final String getAnnotationValue(Symbol symbol, String annotationType, String defaultValue) { return getAnnotationValue(symbol, annotationType, null, defaultValue); } + /** + * Gets the first value of the given property for the given annotation type + * if found on the given symbol. + */ public String getAnnotationValue(Symbol symbol, String annotationType, String propertyName, String defaultValue) { if (hasAnnotationFilters()) { String signature = symbol.toString(); diff --git a/transpiler/src/main/java/org/jsweet/transpiler/extensions/RemoveJavaDependenciesAdapter.java b/transpiler/src/main/java/org/jsweet/transpiler/extensions/RemoveJavaDependenciesAdapter.java index 87a3e066..e56395a1 100644 --- a/transpiler/src/main/java/org/jsweet/transpiler/extensions/RemoveJavaDependenciesAdapter.java +++ b/transpiler/src/main/java/org/jsweet/transpiler/extensions/RemoveJavaDependenciesAdapter.java @@ -95,14 +95,14 @@ public class RemoveJavaDependenciesAdapter extends Java extTypesMapping.put(Calendar.class.getName(), "Date"); extTypesMapping.put(GregorianCalendar.class.getName(), "Date"); extTypesMapping.put(TimeZone.class.getName(), "string"); - typesMapping.putAll(extTypesMapping); - complexTypesMapping - .add((typeTree, + addTypeMappings(extTypesMapping); + addTypeMapping( + (typeTree, name) -> name.startsWith("java.") && context.types.isSubtype(typeTree.type, context.symtab.throwableType) ? "Error" : null); - complexTypesMapping - .add((typeTree, + addTypeMapping( + (typeTree, name) -> typeTree instanceof JCTypeApply && WeakReference.class.getName() .equals(typeTree.type.tsym.getQualifiedName().toString()) ? ((JCTypeApply) typeTree).arguments.head : null); @@ -541,8 +541,8 @@ public class RemoveJavaDependenciesAdapter extends Java @Override protected boolean substituteFieldAccess(JCFieldAccess fieldAccess, TypeSymbol targetType, String accessedType) { - if (fieldAccess.sym.isStatic() && typesMapping.containsKey(accessedType) - && accessedType.startsWith("java.lang.") && !"class".equals(fieldAccess.name.toString())) { + if (fieldAccess.sym.isStatic() && isMappedType(accessedType) && accessedType.startsWith("java.lang.") + && !"class".equals(fieldAccess.name.toString())) { switch (accessedType) { case "java.lang.Float": diff --git a/transpiler/src/main/java/org/jsweet/transpiler/typescript/Java2TypeScriptAdapter.java b/transpiler/src/main/java/org/jsweet/transpiler/typescript/Java2TypeScriptAdapter.java index f31d5ffa..b1c5e0cb 100644 --- a/transpiler/src/main/java/org/jsweet/transpiler/typescript/Java2TypeScriptAdapter.java +++ b/transpiler/src/main/java/org/jsweet/transpiler/typescript/Java2TypeScriptAdapter.java @@ -125,8 +125,8 @@ public class Java2TypeScriptAdapter extends AbstractPri private final static String VAR_DECL_KEYWORD = Java2TypeScriptTranslator.VAR_DECL_KEYWORD; - protected Map typesMapping = new HashMap(); - protected List> complexTypesMapping = new ArrayList<>(); + private Map typesMapping = new HashMap(); + private List> complexTypesMapping = new ArrayList<>(); protected Map langTypesMapping = new HashMap(); protected Set langTypesSimpleNames = new HashSet(); protected Set baseThrowables = new HashSet(); @@ -230,6 +230,51 @@ public class Java2TypeScriptAdapter extends AbstractPri } + /** + * Adds a type mapping so that this adapter substitutes the source type with + * the target type during the transpilation process. + * + * @param sourceTypeName + * the fully qualified name of the type to be substituted + * @param targetTypeName + * the fully Qualified name of the type the source type is mapped + * to + */ + public void addTypeMapping(String sourceTypeName, String targetTypeName) { + typesMapping.put(sourceTypeName, targetTypeName); + } + + /** + * Adds a set of name-based type mappings. This method is equivalent to + * calling {@link #addTypeMapping(String, String)} for each entry of the + * given map. + */ + public void addTypeMappings(Map nameMappings) { + typesMapping.putAll(nameMappings); + } + + /** + * Returns true if the given type name is mapped through the + * {@link #addTypeMapping(String, String)} or + * {@link #addTypeMapping(String, String)} function. + */ + public boolean isMappedType(String sourceTypeName) { + return typesMapping.containsKey(sourceTypeName); + } + + /** + * Adds a type mapping so that this adapter substitutes the source type tree + * with a target type during the transpilation process. + * + * @param mappingFunction + * a function that takes the type tree, the type name, and + * returns a substitution (either under the form of a string, or + * of a string, or of another type tree). + */ + public void addTypeMapping(BiFunction mappingFunction) { + complexTypesMapping.add(mappingFunction); + } + @Override public String needsImport(JCImport importDecl, String qualifiedName) { if (isJSweetPath(qualifiedName) || typesMapping.containsKey(qualifiedName) diff --git a/transpiler/src/main/java/org/jsweet/transpiler/util/AbstractPrinterAdapter.java b/transpiler/src/main/java/org/jsweet/transpiler/util/AbstractPrinterAdapter.java index fff42da9..9d76635f 100644 --- a/transpiler/src/main/java/org/jsweet/transpiler/util/AbstractPrinterAdapter.java +++ b/transpiler/src/main/java/org/jsweet/transpiler/util/AbstractPrinterAdapter.java @@ -440,7 +440,7 @@ public abstract class AbstractPrinterAdapter { * @return the adapted comment (null will remove the JavaDoc comment) */ public String adaptDocComment(JCTree element, String commentText) { - return commentText; + return parentAdapter == null ? commentText : parentAdapter.adaptDocComment(element, commentText); } /**