From 0ece9ec685b12c1d9027fd6c1b1b140b136e7f39 Mon Sep 17 00:00:00 2001 From: Teletha Date: Thu, 2 Mar 2023 11:41:46 +0900 Subject: [PATCH] feat: (Java) Hide wrapper method call. --- src/main/java/reincarnation/OperandUtil.java | 41 ++++++++++++++++++- .../reincarnation/coder/java/JavaCoder.java | 7 +++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/main/java/reincarnation/OperandUtil.java b/src/main/java/reincarnation/OperandUtil.java index 8069f54d..556b52b0 100644 --- a/src/main/java/reincarnation/OperandUtil.java +++ b/src/main/java/reincarnation/OperandUtil.java @@ -14,11 +14,14 @@ import java.lang.reflect.Array; import java.lang.reflect.Method; +import java.lang.reflect.Parameter; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; -class OperandUtil { +import kiss.I; + +public class OperandUtil { /** * Load {@link Class} by internal name. @@ -246,10 +249,17 @@ static Operand defaultValueFor(Class type) { * @param method * @return */ - static boolean isUnwrapper(Method method) { + public static boolean isUnwrapper(Method method) { Class type = method.getDeclaringClass(); String name = method.getName(); + // check parameter + Parameter[] params = method.getParameters(); + if (params.length != 0) { + return false; + } + + // check owner and method name return (type == Integer.class && name.equals("intValue")) // for int || (type == Long.class && name.equals("longValue")) // for long || (type == Float.class && name.equals("floatValue")) // for float @@ -259,4 +269,31 @@ static boolean isUnwrapper(Method method) { || (type == Short.class && name.equals("shortValue")) // for short || (type == Character.class && name.equals("intValue")); // for char } + + /** + * Check whether the specified method is wrapper for primitive or not. + * + * @param method + * @return + */ + public static boolean isWrapper(Method method) { + Class type = method.getDeclaringClass(); + String name = method.getName(); + + // check parameter + Parameter[] params = method.getParameters(); + if (params.length != 1 || I.wrap(params[0].getType()) != type) { + return false; + } + + // check owner and method name + return (type == Integer.class && name.equals("valueOf")) // for int + || (type == Long.class && name.equals("valueOf")) // for long + || (type == Float.class && name.equals("valueOf")) // for float + || (type == Double.class && name.equals("valueOf")) // for double + || (type == Boolean.class && name.equals("valueOf")) // for boolean + || (type == Byte.class && name.equals("valueOf")) // for byte + || (type == Short.class && name.equals("valueOf")) // for short + || (type == Character.class && name.equals("valueOf")); // for char + } } \ No newline at end of file diff --git a/src/main/java/reincarnation/coder/java/JavaCoder.java b/src/main/java/reincarnation/coder/java/JavaCoder.java index b87a65a1..de86e0ee 100644 --- a/src/main/java/reincarnation/coder/java/JavaCoder.java +++ b/src/main/java/reincarnation/coder/java/JavaCoder.java @@ -25,6 +25,7 @@ import kiss.Variable; import kiss.Ⅲ; import reincarnation.Operand; +import reincarnation.OperandUtil; import reincarnation.Reincarnation; import reincarnation.coder.Code; import reincarnation.coder.Coder; @@ -587,7 +588,11 @@ public void writeMethodCall(Method method, Code context, List pa if (mode == AccessMode.SUPER) { write("super.", method.getName(), buildParameter(method, params)); } else { - write(context, ".", method.getName(), buildParameter(method, params)); + if (OperandUtil.isWrapper(method)) { + write(params.get(0)); + } else { + write(context, ".", method.getName(), buildParameter(method, params)); + } } } }