开发者

Getting package-level access to a field from outside the package?

开发者 https://www.devze.com 2022-12-11 19:25 出处:网络
I\'m using an API that has an abstract class that I wish to extend (in my concrete class) in order to take advantage of some methods within the parent. Unfortunately, the programmer of that API class

I'm using an API that has an abstract class that I wish to extend (in my concrete class) in order to take advantage of some methods within the parent. Unfortunately, the programmer of that API class decided with one field in particular to give it package-level access with no public setter. The field value is instead set within a method. Is t开发者_开发知识库here a common "bridge pattern" so that I can get/set that field appropriately? This is not only important for my functionality, but for testing as well (since I will need to mock out the value that is set in that field in my test). Thanks!


Some possibilities:

  • Obtain and modify the source code for "the API".

  • Write a class in the same package to sneak access to and expose the field you need.

  • Write a wrapper around the API's class providing access to that field with any changed semantics you like.

  • Use reflection to break the access protection on the API's class.


You are going to have to use reflection in order to set the field. In order to set the field, you'd call setAccessible on it. I haven't tested this but something like the following should work:

ClassWithProtectedField o = new ClassWithProtectedField();
Class c = o.getClass();
java.lang.reflect.Field protectedField = c.getField("protectedField");
protectedField.setAccessible(true);
protectedField.set(o, someValue);

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Field.html


If you are able, you could create a subclass of the API class within the original package and expose the field publicly. If you cannot do this then you could potentially use reflection to modify the field's value, assuming that your SecurityManager is configured to allow it - but this feels like a bit of a code smell.


If you have control over the SecurityManager or there is non in place, you can use Reflection to make the field accessible and then change its value.

Example code:

import java.lang.reflect.Field;

public class ModifyProtectedFieldSample {

    public static void setProtectedField(final Object targetClass,
                                     final String fieldName,
                                     final Object value) {
    try {
        final Field field = targetClass.getClass().getDeclaredField(fieldName);
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }

        field.set(targetClass, value);
    } catch (NoSuchFieldException e) {
        System.out.println("NoSuchFieldException " + e);
    } catch (SecurityException e) {
        System.out.println("SecurityException"+ e);
    } catch (IllegalAccessException e) {
        System.out.println("IllegalAccessException"+ e);
    }
  }

  public static void main(final String[] args) {
    setProtectedField(String.class, "someFieldName", Boolean.FALSE);
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消