0

I am trying to execute below written code and the code should throw an exception but it isn't doint it

try {
    Field.class.getMethod("getInt", Object.class).setAccessible(false);
    StringSearch.class.getMethod("searchChars",cc.getClass(),pattern3.getClass()).setAccessible(false);
    ss4.getClass().getMethod("searchChars",cc.getClass(),pattern3.getClass()).setAccessible(false);
    ss4.searchChars(cc,pattern3);
    ss4.searchString(str,pattern);
}
catch(NoSuchMethodException ex){
    ex.printStackTrace();
}

it should actually throw IllegalAccessException.

ss4 is an object of class BNDMWildcardsCI (one of the algo for String search) cc, pattern3 are character arrays str, pattern are Strings

why it isn't throwing an exception, it is not throwing the NoSuchMethodFound exception means that it is able to find the method also i tried to print the isAccessible and it says false but when i run the tests it doesn't throw any exception

Dante May Code
  • 11,177
  • 9
  • 49
  • 81
Nirav
  • 49
  • 1
  • 14
  • I'm confused, you are asking why it is not throwing an IllegalAccessException? because you didnt catch that exception? – Justin McDonald Dec 10 '12 at 23:58
  • Actually I want the code to throw IllegalAccessException, and that is why I am setting setAccessible as false. I want to see that the algo I am testing BNDMWildcardsCI is able to handle the IllegalAccessException or not...there the IllegalAcessException is set or not – Nirav Dec 10 '12 at 23:59

2 Answers2

2

To the best of my knowledge, if a method is declared public (or otherwise accessible), setAccessible(false) can't make it private. It's only useful if you have a private method and you previously called setAccessible(true).

David Moles
  • 48,006
  • 27
  • 136
  • 235
  • so is there any other way I can do it?? – Nirav Dec 11 '12 at 00:17
  • I have tried to use SecurityManager like below SecurityManager SecMgr = new SecurityManager(); SecMgr.checkPackageAccess("java.lang.reflect.Field"); but dont know how to revoke permission to a method in the package or revoke access to package itself – Nirav Dec 11 '12 at 00:20
  • You can't directly control access to a method via a SecurityManager; you can use a policy file to create a special permission, and then *in that method* check whether that permission exists (see [this answer](http://stackoverflow.com/questions/5486797/how-to-prevent-public-methods-from-being-called-from-specific-classes/5490106#5490106)) but it's pretty ugly, and if you have to modify the public method you might as well just make it non-public. – David Moles Dec 11 '12 at 23:30
  • It's really not clear what you're trying to do here; I think you should post the actual security problem you're trying to solve as a separate question. – David Moles Dec 11 '12 at 23:31
0

The method setAccessible(boolean) work on object reflect not on normal object. In your code you set it on a method object not on the ss4 object.

To show my point:

Class<?> clazz = ss4.getClass();
Method searchCharsMethod = clazz.getMethod("searchChars",cc.getClass(),pattern3.getClass());
       searchCharsMethod.setAccessible(true);

You have set the accessible flag to false on object assigned to searchCharsMethod not ss4.

As bonus see what happen when you call

searchCharsMethod.invoke(ss4,cc,pattern3);

For more please read the documentation

  • it doesn't do any change.. but i have figured that it doesn't work is access specifier is public so I have now only one choice to restrict access to package java.lang.reflect and that restriction can make my program throw IllegalAccessException – Nirav Dec 11 '12 at 02:36