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

>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.