2

So, for example, StringBuilder inherits from the abstract class AbstractStringBuilder. As I understand it, StringBuilder has no fields itself (except for serialVersionUID). Rather, its state is represented by the fields in AbstractStringBuilder and manipulated by calling super in the implementations of the methods it overrides.

Is there a way via reflection to get the private char array named value declared in AbstractStringBuilder that is associated with a particular instance of StringBuilder? This is the closest I got.

import java.lang.reflect.Field;
import java.util.Arrays;

public class Test
{
   public static void main(String[ ] args) throws Exception
   {
      StringBuilder foo = new StringBuilder("xyzzy");
      Field bar = foo.getClass( ).getSuperclass( ).getDeclaredField("value");
      bar.setAccessible(true);
      char[ ] baz = (char[ ])bar.get(new StringBuilder( ));
   }
}

That gets me an array of sixteen null characters. Note that I'm looking for solutions involving reflection, since I need a general technique that isn't limited to StringBuilder. Any ideas?

5
  • Why do you think that the Object class has a field called value? Commented Dec 5, 2010 at 22:41
  • @Roflcoptr I don't think that. AbstractStringBuilder has a field called value.
    – gdejohn
    Commented Dec 5, 2010 at 22:43
  • Hmm If i look in the API, StringBuilder inherits directly from object Commented Dec 5, 2010 at 22:45
  • @Roflcoptr Yeah, not sure why the documentation's off, but you can can test this for yourself by calling getSuperclass on StringBuilder.class. It actually inherits from AbstractStringBuilder.
    – gdejohn
    Commented Dec 5, 2010 at 22:46
  • 1
    AbstractStringBuilder is a package-private/default-access class. It's not part of the API, other than that it is mentioned in the serialisation docs. Commented Dec 5, 2010 at 23:44

2 Answers 2

7
char[ ] baz = (char[ ])bar.get(new StringBuilder( ));

Your problem is that you're inspecting a new StringBuilder... so of course it's empty (and 16 chars is the default size). You need to pass in foo

3
  • Well, I'll be damned. I'm not sure why it didn't occur to me to try that. Looks like it works. Thank you.
    – gdejohn
    Commented Dec 5, 2010 at 23:33
  • Can you explain why this works? How can you get the value of a private field in some class from an instance of a subclass of that class, which doesn't inherit it?
    – gdejohn
    Commented Dec 8, 2010 at 12:52
  • 1
    The private field is still part of the child object, it's just not accessible from within the child by the standard means. As an interesting aside, a private field is actually private to the class not the instance - I'd be keen to know the why behind that one.
    – CurtainDog
    Commented Dec 8, 2010 at 23:21
0

It might be worth looking in the Apache Commons BeanUtils library. Here is the link to their API Javadocs. The library contains lots of high-level methods that make Reflection easier to use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.