diff --git a/src/main/java/bsh/Reflect.java b/src/main/java/bsh/Reflect.java index d20dcc4a..c1cd5a07 100644 --- a/src/main/java/bsh/Reflect.java +++ b/src/main/java/bsh/Reflect.java @@ -1303,37 +1303,4 @@ public static Class getType(Object arg) { return Types.getType(arg); } - /** Find the types of some objects. */ - public static Class[] getTypes(Object ...args) { - return Types.getTypes(args); - } - - /** - * Just a method to return the pretty name of any Class - * - *
-     * prettyName(String.class)
-     *  returns "java.lang.String"
-     * prettyName(byte.class)
-     *  returns "byte"
-     * prettyName((new Object[3]).getClass())
-     *  returns "java.lang.Object[];"
-     * prettyName((new int[3][4][5][6][7][8][9]).getClass())
-     *  returns "int[][][][][][][]"
-     * 
- */ - public static String prettyName(Class clas) { - return Types.prettyName(clas); - } - - /** - * Just a method to return the pretty names of some Classes - * @see Reflect#prettyName(Class) - */ - public static String[] prettyNames(Class[] classes) { - String[] prettyNames = new String[classes.length]; - for (int i = 0; i < classes.length; i++) - prettyNames[i] = prettyName(classes[i]); - return prettyNames; - } } diff --git a/src/main/java/bsh/security/SecurityError.java b/src/main/java/bsh/security/SecurityError.java index b7b42f48..eefcd217 100644 --- a/src/main/java/bsh/security/SecurityError.java +++ b/src/main/java/bsh/security/SecurityError.java @@ -25,8 +25,12 @@ public EvalError toEvalError(Node node, CallStack callstack) { /** This method basically return the types of args at a concatened String by ", " */ private static String argsTypesString(Object[] args) { - Class[] argTypes = Reflect.getTypes(args); - return String.join(", ", Reflect.prettyNames(argTypes)); + String[] argTypeNames = new String[args.length]; + for (int i = 0; i < args.length; i++) { + final Class _class = Reflect.getType(args[i]); + argTypeNames[i] = _class != null ? _class.getTypeName() : "null"; + } + return String.join(", ", argTypeNames); } /** Create a error for when can't construct a instance */ @@ -43,29 +47,25 @@ static SecurityError reflectCantConstruct(Class _class, Object[] args) { /** Create a error for when can't invoke a static method */ static SecurityError cantInvokeStaticMethod(Class _class, String methodName, Object[] args) { - String className = Reflect.prettyName(_class); - String msg = String.format("Can't invoke this static method: %s.%s(%s)", className, methodName, argsTypesString(args)); + String msg = String.format("Can't invoke this static method: %s.%s(%s)", _class.getTypeName(), methodName, argsTypesString(args)); return new SecurityError(msg); } /** Create a error for when can't invoke a static method using reflection */ static SecurityError reflectCantInvokeStaticMethod(Class _class, String methodName, Object[] args) { - String className = Reflect.prettyName(_class); - String msg = String.format("Can't invoke this static method using reflection: %s.%s(%s)", className, methodName, argsTypesString(args)); + String msg = String.format("Can't invoke this static method using reflection: %s.%s(%s)", _class.getTypeName(), methodName, argsTypesString(args)); return new SecurityError(msg); } /** Create a error for when can't invoke a method */ static SecurityError cantInvokeMethod(Object thisArg, String methodName, Object[] args) { - String className = Reflect.prettyName(thisArg.getClass()); - String msg = String.format("Can't invoke this method: %s.%s(%s)", className, methodName, argsTypesString(args)); + String msg = String.format("Can't invoke this method: %s.%s(%s)", thisArg.getClass().getTypeName(), methodName, argsTypesString(args)); return new SecurityError(msg); } /** Create a error for when can't invoke a method using reflection */ static SecurityError reflectCantInvokeMethod(Object thisArg, String methodName, Object[] args) { - String className = Reflect.prettyName(thisArg.getClass()); - String msg = String.format("Can't invoke this method using reflection: %s.%s(%s)", className, methodName, argsTypesString(args)); + String msg = String.format("Can't invoke this method using reflection: %s.%s(%s)", thisArg.getClass().getTypeName(), methodName, argsTypesString(args)); return new SecurityError(msg); } @@ -77,36 +77,31 @@ static SecurityError cantInvokeLocalMethod(String methodName, Object[] args) { /** Create a error for when can't invoke a super method */ static SecurityError cantInvokeSuperMethod(Class superClass, String methodName, Object[] args) { - String superClassName = Reflect.prettyName(superClass); - String msg = String.format("Can't invoke this super method: %s.%s(%s)", superClassName, methodName, argsTypesString(args)); + String msg = String.format("Can't invoke this super method: %s.%s(%s)", superClass.getTypeName(), methodName, argsTypesString(args)); return new SecurityError(msg); } /** Create a error for when can't get a field */ static SecurityError cantGetField(Object thisArg, String fieldName) { - String className = Reflect.prettyName(thisArg.getClass()); - String msg = String.format("Can't get this field: %s.%s", className, fieldName); + String msg = String.format("Can't get this field: %s.%s", thisArg.getClass().getTypeName(), fieldName); return new SecurityError(msg); } /** Create a error for when can't get a field */ static SecurityError reflectCantGetField(Object thisArg, String fieldName) { - String className = Reflect.prettyName(thisArg.getClass()); - String msg = String.format("Can't get this field using reflection: %s.%s", className, fieldName); + String msg = String.format("Can't get this field using reflection: %s.%s", thisArg.getClass().getTypeName(), fieldName); return new SecurityError(msg); } /** Create a error for when can't get a field */ static SecurityError cantGetStaticField(Class _class, String fieldName) { - String className = Reflect.prettyName(_class); - String msg = String.format("Can't get this static field: %s.%s", className, fieldName); + String msg = String.format("Can't get this static field: %s.%s", _class.getTypeName(), fieldName); return new SecurityError(msg); } /** Create a error for when can't get a field */ static SecurityError reflectCantGetStaticField(Class _class, String fieldName) { - String className = Reflect.prettyName(_class); - String msg = String.format("Can't get this static field using reflection: %s.%s", className, fieldName); + String msg = String.format("Can't get this static field using reflection: %s.%s", _class.getTypeName(), fieldName); return new SecurityError(msg); } diff --git a/src/test/java/bsh/ReflectTest.java b/src/test/java/bsh/ReflectTest.java index ac3cb4de..43551e0f 100644 --- a/src/test/java/bsh/ReflectTest.java +++ b/src/test/java/bsh/ReflectTest.java @@ -3,7 +3,6 @@ import static bsh.TestUtil.eval; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -720,35 +719,4 @@ public void findMostSpecificSignature() { // assertEquals("most specific char[] class", 1, value); } - /** Test {@link Reflect#prettyName(Class)} with primitive Class, e.g.: byte, int, char, etc... */ - @Test - public void pretty_name_of_primitive() { - Assert.assertEquals("byte", Reflect.prettyName(byte.class)); - } - - /** Test {@link Reflect#prettyName(Class)} with an array Class, e.g.: java.lang.Object[] */ - @Test - public void pretty_name_of_array() { - Assert.assertEquals("java.lang.Object[]", Reflect.prettyName(new Object[3].getClass())); - } - - /** Test {@link Reflect#prettyName(Class)} with an matrix Class, e.g.: java.lang.Object[][][][][] */ - @Test - public void pretty_name_of_matrix() { - Assert.assertEquals("int[][][][][][][]", Reflect.prettyName(new int[3][4][5][6][7][8][9].getClass())); - } - - /** Test {@link Reflect#prettyName(Class)} with an null value */ - @Test - public void pretty_name_of_null() { - Assert.assertEquals("null", Reflect.prettyName(null)); - } - - /** Test {@link Reflect#prettyNames(Class[])} */ - @Test - public void pretty_names() { - Class[] classes = { byte.class, Object[].class, int[][][][][][][].class, null }; - String[] expectedNames = { "byte", "java.lang.Object[]" , "int[][][][][][][]", "null" }; - Assert.assertArrayEquals(expectedNames, Reflect.prettyNames(classes)); - } }