tags:

views:

59

answers:

4

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 there 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!

A: 

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);
  }
}
Gerd Klima
+1  A: 

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

Chris Gow
+4  A: 

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.

Carl Smotricz
"Write a class in the same package to sneak access to and expose the field you need." -- what are the disadvantages to that one (if any)?
hal10001
There might be a security manager and policy in place to stop you from doing so; but that's unlikely. The bigger disadvantage is that it's "dirty" insofar as you're mixing your code into someone else's.
Carl Smotricz
A: 

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.

teabot