Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] How to add methods to java.lang.String

There are certain limitations and restrictions when modifying the
system classes.  Here is what works:

Your aspect:
public privileged aspect StringUtils {
  public String String.ltrim() {
        return "test ltrim";
  }
}

Apply that to the JVM classes:
ajc -inpath rt.jar StringUtils.java -outjar newrt.jar

(rt.jar being the windows jar containing String)

Your main class:
public class Main {
  public static void main(String []argv) {
    String string = "ABCDEFG";
    string = string.ltrim();
    System.out.println(string);
  }
}

Compiling it:
javac -Xbootclasspath/p:newrt.jar Main.java

(need the newrt ahead of the regular rt, hence the -X)

Running it:
java -Xbootclasspath/p:newrt.jar Main
test ltrim

Basically load time weaving isn't going to work for you as these
classes get loaded early on and by a loader that doesn't get a weaver
attached to it - so we go with compile time weaving.  Because we are
compile time weaving we have to ensure our woven classes are used
instead of those the JVM would ordinarily use (-Xbootclasspath/p where
p is for prefix).

I can't comment on  the legality of it, but I perhaps wouldn't advise
shipping a product that included a modified set of classes like this.
But it can be done.

cheers,
Andy


On 7 June 2012 10:17, MattOng <ongbp@xxxxxxxxx> wrote:
>
> From what I can see in sample code:
>
> public class Point{}
>
> public aspect Point
> {
>    // a new method is introduced into the Point Class.
>    public String Point.toLongString(){
>       return "### FROM ASPECT ###";
>    }
> }
>
> public class Main{
>  public static void main(String[] args){
>      Point p1 = new Point();
>
>      // This line below works without any error.
>      System.out.println("p1.toLongString =" +p1.toLongString());
>      System.out.println("p1 =" + p1);
>  }
> }
>
> ---------------------------------------------------------------
> However, when I tried this, it does not work
>
> // Extends the String class and provide other useful
> public privileged aspect StringUtils {
>
>        public String java.lang.String.ltrim(){
>                return "TEST LTRIM";
>        }
>
>        public String java.lang.String.rtrim(){
>                return "TEST RTRIM";
>        }
>
> }
>
> public class Main {
>        public static void main(String[] args) {
>                String string="ABCDEFG";
>                //  Complians with the error shown below
>                string=string.ltrim();
>                System.out.println(string);
>        }
> }
>
> ---------------------------------------------------------
> Exception in thread "main" java.lang.NoSuchMethodError:
> java.lang.String.ltrim()Ljava/lang/String;
>        at
> stringext.StringUtils.ajc$interMethodDispatch1$stringext_StringUtils$java_lang_String$ltrim(StringUtils.aj)
>        at stringext.Main.main(Main.java:11)
> ----------------------------------------------------------
>
> Please help to show how this can be done properly. Many thanks in advance.
>
>
> --
> View this message in context: http://aspectj.2085585.n4.nabble.com/How-to-add-methods-to-java-lang-String-tp4650376.html
> Sent from the AspectJ - users mailing list archive at Nabble.com.
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top