Skip to content

Commit

Permalink
feat: (Java) Hide wrapper method call.
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Mar 2, 2023
1 parent 7defc41 commit 0ece9ec
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
41 changes: 39 additions & 2 deletions src/main/java/reincarnation/OperandUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
7 changes: 6 additions & 1 deletion src/main/java/reincarnation/coder/java/JavaCoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -587,7 +588,11 @@ public void writeMethodCall(Method method, Code context, List<? extends Code> 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));
}
}
}
}
Expand Down

0 comments on commit 0ece9ec

Please sign in to comment.