Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Net-0 committed Oct 19, 2024
1 parent d9b74ea commit 558e07c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 85 deletions.
33 changes: 0 additions & 33 deletions src/main/java/bsh/Reflect.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
* <pre>
* 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[][][][][][][]"
* </pre>
*/
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;
}
}
35 changes: 15 additions & 20 deletions src/main/java/bsh/security/SecurityError.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
}

Expand All @@ -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));

Check warning on line 80 in src/main/java/bsh/security/SecurityError.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/bsh/security/SecurityError.java#L80

Added line #L80 was not covered by tests
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);
}

Expand Down
32 changes: 0 additions & 32 deletions src/test/java/bsh/ReflectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import static bsh.TestUtil.eval;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down Expand Up @@ -720,35 +719,4 @@ public void findMostSpecificSignature() {
// assertEquals("most specific char[] class", 1, value);
}

/** Test {@link Reflect#prettyName(Class)} with primitive Class<?>, e.g.: <b>byte</b>, <b>int</b>, <b>char</b>, 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.: <b>java.lang.Object[]</b> */
@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.: <b>java.lang.Object[][][][][]</b> */
@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));
}
}

0 comments on commit 558e07c

Please sign in to comment.