Skip to content

Commit

Permalink
fix: Move OperandUtil#isWrapper and #isUnwrapper to Classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Mar 15, 2023
1 parent de4ebca commit adcf70b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 59 deletions.
3 changes: 2 additions & 1 deletion src/main/java/reincarnation/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import reincarnation.structure.Structure;
import reincarnation.structure.Try;
import reincarnation.structure.While;
import reincarnation.util.Classes;

/**
* @version 2018/11/05 15:07:53
Expand Down Expand Up @@ -1322,7 +1323,7 @@ private Node createConnectorNode(Node previous, Node next) {
* @return
*/
private Signal<OperandMethodCall> throughUnwrapper(OperandMethodCall call) {
if (OperandUtil.isUnwrapper(call.method)) {
if (Classes.isUnwrapper(call.method)) {
return call.owner.children(OperandMethodCall.class);
} else {
return I.signal(call);
Expand Down
57 changes: 1 addition & 56 deletions src/main/java/reincarnation/OperandUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -24,7 +23,7 @@
import kiss.I;
import reincarnation.operator.AccessMode;

public class OperandUtil {
class OperandUtil {

/**
* Load {@link Class} by internal name.
Expand Down Expand Up @@ -298,58 +297,4 @@ static OperandMethodCall convertMethod(Method method, Object... parameters) {
return new OperandMethodCall(AccessMode.THIS, method.getDeclaringClass(), method.getName(), method
.getParameterTypes(), convert(method.getDeclaringClass()), I.list(convert(parameters)));
}

/**
* Check whether the specified method is unwrapper for primitive or not.
*
* @param method
* @return
*/
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
|| (type == Double.class && name.equals("doubleValue")) // for double
|| (type == Boolean.class && name.equals("booleanValue")) // for boolean
|| (type == Byte.class && name.equals("byteValue")) // for byte
|| (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
}
}
3 changes: 1 addition & 2 deletions src/main/java/reincarnation/coder/java/JavaCoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import kiss.Ⅱ;
import kiss.Ⅲ;
import reincarnation.Operand;
import reincarnation.OperandUtil;
import reincarnation.Reincarnation;
import reincarnation.coder.Code;
import reincarnation.coder.Coder;
Expand Down Expand Up @@ -719,7 +718,7 @@ public void writeMethodCall(Method method, Code context, List<Code> params, Acce
if (mode == AccessMode.SUPER) {
write("super.", method.getName(), buildParameter(method, params));
} else {
if (OperandUtil.isWrapper(method)) {
if (Classes.isWrapper(method)) {
write(params.get(0));
} else {
if (Classes.isStatic(method) && current.is(method.getDeclaringClass())) {
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/reincarnation/util/Classes.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
package reincarnation.util;

import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

import kiss.I;
import kiss.model.Model;

public class Classes {
Expand Down Expand Up @@ -144,4 +147,58 @@ private static void inner(Class clazz, Set<Class> set) {
inner(declared, set);
}
}

/**
* Check whether the specified method is unwrapper for primitive or not.
*
* @param method
* @return
*/
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
|| (type == Double.class && name.equals("doubleValue")) // for double
|| (type == Boolean.class && name.equals("booleanValue")) // for boolean
|| (type == Byte.class && name.equals("byteValue")) // for byte
|| (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
}
}

0 comments on commit adcf70b

Please sign in to comment.