This commit is contained in:
Louis Grignon 2017-02-10 18:15:46 +01:00
commit 3b106e86e2
4 changed files with 60 additions and 11 deletions

View File

@ -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();

View File

@ -95,14 +95,14 @@ public class RemoveJavaDependenciesAdapter<C extends JSweetContext> 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<C extends JSweetContext> 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":

View File

@ -125,8 +125,8 @@ public class Java2TypeScriptAdapter<C extends JSweetContext> extends AbstractPri
private final static String VAR_DECL_KEYWORD = Java2TypeScriptTranslator.VAR_DECL_KEYWORD;
protected Map<String, String> typesMapping = new HashMap<String, String>();
protected List<BiFunction<JCTree, String, Object>> complexTypesMapping = new ArrayList<>();
private Map<String, String> typesMapping = new HashMap<String, String>();
private List<BiFunction<JCTree, String, Object>> complexTypesMapping = new ArrayList<>();
protected Map<String, String> langTypesMapping = new HashMap<String, String>();
protected Set<String> langTypesSimpleNames = new HashSet<String>();
protected Set<String> baseThrowables = new HashSet<String>();
@ -230,6 +230,51 @@ public class Java2TypeScriptAdapter<C extends JSweetContext> 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<String, String> 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<JCTree, String, Object> mappingFunction) {
complexTypesMapping.add(mappingFunction);
}
@Override
public String needsImport(JCImport importDecl, String qualifiedName) {
if (isJSweetPath(qualifiedName) || typesMapping.containsKey(qualifiedName)

View File

@ -440,7 +440,7 @@ public abstract class AbstractPrinterAdapter<C extends JSweetContext> {
* @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);
}
/**