1

I wanted to create my own StringBuffer which has 2-3 more methods added to the java.lang.StringBuffer. Since the original class is final I cannot extend that class.

Now, If I copy paste the class into my own class, then it says 'AbstractStringBuilder is not visible' error.

How to extend this class in my own namespace (say com.util).

6
  • 6
    did you notice that StringBuffer has a Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.? Commented Dec 11, 2012 at 11:29
  • what methods you want to add? which class you are copy pasting? Commented Dec 11, 2012 at 11:30
  • 1
    @Denis -is that current ? OpenJDK StringBuilder appears to be GPLed (IANAL). grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/… Commented Dec 11, 2012 at 11:33
  • @BrianAgnew: I'm still on jdk6, it has a proprietary license. Commented Dec 11, 2012 at 11:35
  • 1
    IMHO Making StringBuffer synchronized was not a good design choice and using StringBuilder is better, not just for performance but making it clear that it does not attempt to be thread safe. Commented Dec 11, 2012 at 11:41

4 Answers 4

6

I would personally use the decorator pattern for this - a class that wraps the original StringBuilder and then provides extra methods to use as appropriate.

If you only want to add a couple more methods, you may also want to look at just using equivalent static methods that do the job.

(As others have mentioned, StringBuilder is the better, more efficient choice over StringBuffer, unless you really need the thread safety the latter provides.)

Simply copying and pasting the code isn't a great idea, you're likely breaking licensing laws and duplicating functionality (even if that functionality is in the original API.)

Sign up to request clarification or add additional context in comments.

Comments

6

I would work with StringBuilder rather than StringBuffer.

It's unfortunate that you can't implement an interface representing StringBuilder (unless Appendable is suitable?). Have you tried creating an EnhancedStringBuilder class that contains your StringBuilder as a member. You can then delegate most(all?) calls to this, and provide your own.

An alternative is to simply take the OpenJDK source code for StringBuilder and repackage and enhance it. I think the licensing will permit this, but I'm not familiar with your circumstances and you should check this.

1 Comment

the licensing would require making whole application GPLv2 licensed.
5

Potential licensing issues aside, I would not recommend copying and pasting the source code just to add a couple of methods. Instead, I'd suggest making those methods static and placing them into a helper class (call it StringBuilderUtils or something similar).

The methods would take an instance of StringBuilder or StringBuffer (or perhaps even Appendable if you can implement your functionality in terms of Appendable).

1 Comment

This does hinge on the required functionality being implementable via the public API. Worth noting
0

You could copy the AbstractStringBuilder also. But I don't know the dependencies of that one. My recommendation is to create a Wrapper class to StringBuilder, and encapsulate it.

Don't worry about the performance, delegate calls can really easy be inlined into classes using your StringBuilderWrapper.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.