Bug 382509 - Making AspectJ more dynamic with minor syntax tweak.
Summary: Making AspectJ more dynamic with minor syntax tweak.
Status: NEW
Alias: None
Product: AspectJ
Classification: Tools
Component: LTWeaving (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows Vista
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: aspectj inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-06-13 11:20 EDT by Matthew Ong CLA
Modified: 2012-06-13 11:28 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Matthew Ong CLA 2012-06-13 11:20:21 EDT
With reference to the discussion shown within this posting.

http://aspectj.2085585.n4.nabble.com/How-to-add-methods-to-java-lang-String-td4650376.html#a4650390

Case 1) introduced a new method into the calling class without any issue.
public final 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);
  }
} 

----------------------------------
Case 2) allows the overriding of the call weaving also without issue.

public aspect JavaLangSystemAspect {
 /**
     * Selects calls to System.currentTimeMillis() occurring within tested code.
     */
    pointcut currentTimeMillis() :            
             call(public long System.currentTimeMillis());

   
    long around() : currentTimeMillis(){
        return 12345;  // overriden method without any privilege 
    }
}

public class SystemUser
{

    public static void MillisUser(){
        // will show only 12345, which is more dangerous than adding new
        // methods into existing class.
        System.out.println("The currentTimeMilis:"+System.currentTimeMillis());
    }
   
    public static void main(String[] args)
    {
        MillisUser(); // will show only 12345
    }

} 
---------------------------------------
<B>However when this class is added causes error in the weaver.</B>
Because I tried to simulate what is being down in Case 1.

// 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(); // ### SUGGEST that weaving done like call in case 2.
         System.out.println(string);
 }
} 

-----------------------------
Can such introduction be done without weaving the source class but rather at the calling class instead, just like the Pointcut call. 

This might allow existing java..* or javax.* classes within rt.jar untouched.
Comment 1 Matthew Ong CLA 2012-06-13 11:28:06 EDT
Hi,

I notice this citation being used within one of the posting.

It is not easy to weave the classes in JDK and it may not be legal.
With a quick search I can only find
http://aspectj.2085585.n4.nabble.com/IDT-in-JDK-classes-How-to-enable-java-weaving-tc2086437.html#none.
Please check list archives and docs for details. 

It is rather invalid because using AspectJ I can do Case #2 rather easily.

If it is the weaving process that the issue, weaving the calling class may not be the issue outside of the java.* javax.* or rt.jar classes. Since they will not have any idea of the newly added method.

With this ability, and introduction to the JKD development community, I predict that it will change the way how new JDK may be enhanced and patched without or waiting of new releases of the next version JDK. Developer may already do a prototyping of how to extend new methods into older java.* or javax.* classes.

Do consider this carefully. Meta Programming is a hot idea now because of 
languages like JRuby or mirah which allow opening existing classes.