use strict equality for core types (more generic + utility functions)

This commit is contained in:
Renaud Pawlak 2020-07-02 09:50:23 +02:00
parent 0fb29d765a
commit f91c0cf37a
4 changed files with 46 additions and 4 deletions

View File

@ -72,6 +72,7 @@ import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.PrimitiveType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
@ -1285,9 +1286,9 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
} else {
switch (targetMethodName) {
case "equals":
if (types().isSameType(invocationElement.getTargetExpression().getType(),
util().getType(String.class))
|| util().isNumber(invocationElement.getTargetExpression().getType())) {
TypeMirror t1 = util().toPrimitiveTypeOrType(invocationElement.getTargetExpression().getType());
TypeMirror t2 = util().toPrimitiveTypeOrType(invocationElement.getArgument(0).getType());
if (types().isSameType(t1, t2) && util().isCoreType(t1)) {
if(isInlinedExpression(invocationElement)) {
print("(");
}

View File

@ -56,6 +56,11 @@ public interface Util {
*/
boolean isNumber(TypeMirror type);
/**
* Tells if the given type is a boolean.
*/
boolean isBoolean(TypeMirror type);
/**
* Tells if the given element is deprecated.
*/
@ -126,4 +131,12 @@ public interface Util {
*/
List<Element> getAllMembers(TypeElement typeElement);
/**
* Gets the type as a primitive type (by unboxing it) when possible.
*
* @param type the origin type
* @return the origin type or the corresponding primitive type if possible
*/
TypeMirror toPrimitiveTypeOrType(TypeMirror type);
}

View File

@ -157,6 +157,11 @@ public class UtilSupport implements Util {
return org.jsweet.transpiler.util.Util.isNumber(type);
}
@Override
public boolean isBoolean(TypeMirror type) {
return org.jsweet.transpiler.util.Util.isBoolean(type);
}
@Override
public boolean isDeprecated(Element element) {
return org.jsweet.transpiler.util.Util.isDeprecated(element);
@ -231,4 +236,12 @@ public class UtilSupport implements Util {
return getAllMembers((TypeElement)context.modelTypes.asElement(type));
}
@Override
public TypeMirror toPrimitiveTypeOrType(TypeMirror type) {
try {
return context.types.unboxedTypeOrType((Type)type);
} catch (Exception e) {
return type;
}
}
}

View File

@ -1059,6 +1059,21 @@ public class Util {
}
}
/**
* Returns true is the type is a boolean.
*/
public static boolean isBoolean(TypeMirror type) {
if (type == null) {
return false;
}
switch (type.getKind()) {
case BOOLEAN:
return true;
default:
return false;
}
}
/**
* Returns true is the type is a core.
*/