I have a very creative solution to allow my unsigned code all access through my signed library. Although bad practice, I fear I have no other solution (except rewriting a lot of code, that then needs to be rewrited back).
I have the following class:
public abstract class full {
public static <T,S,P> S access(final T object, final String function, final P parameter) {
AccessController.doPrivileged(new PrivilegedAction<S>() {
@Override
public S run() {
try {
if (parameter == null) {
Class[] argTypes = { };
Object passedArgv[] = { };
return (S)object.getClass().getMethod(function, argTypes).invoke(object, passedArgv);
} else {
Class[] argTypes = { parameter.getClass() };
Object passedArgv[] = { parameter };
return (S)object.getClass().getMethod(function, argTypes).invoke(object, passedArgv);
}
} catch (NoSuchMethodException ex) {
Logger.getLogger(full.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(full.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(full.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(full.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
});
return null;
}
}
It works great for functions that return Objects, but not for primitives.
Boolean bool = full.access(..);
will result in a nullpointer, because generic methods can’t handle primitives.
Any ideas about how to take the right approach?
To test:
public static Boolean test() {
return true;
}
public Main() {
System.out.println(((Boolean)redirect.full.access(this, "test", null)));
}
The reason you are getting a
NullPointerExceptionis because you are always returningnull. You calldoPrivileged(...), yetnullis still returned. Try returning the output ofdoPrivileged(...). By the looks of things, Auto(un)boxing is working here on both the inputs and the returned outputs.This worked for me (I passed in a primitive int, and expected a primitive boolean):
On another note, you could make this more robust. When I passed in an
Integerinstead of anObject, the equals method was not found, even though it would have been a valid method call. If you do not find the method with the givenClass, you should check again with the classes’ parent classes.