ccebinger / SWPSoSe14

2 stars 1 forks source link

add Fields to Lambda anonymous class #255

Closed Zelldon closed 10 years ago

Zelldon commented 10 years ago

For each variable which is used in the closure method add a field in the class


public class Main {

  public static void main(String[] args) {
    final String str = "Hello";   
    final Integer i = 5;
    Lambda la = new Lambda() {
      public void closure() {
        System.out.println(str);
        System.out.println(i);
      }
    };
    System.out.println(str);
    System.out.println(i);
    la.closure(); 
  }
}

Main disas:

$ javap -c -v Main.class 
Classfile /home/zelldon/Documents/Master/master_fub/CB-Project/SWPSoSe14/projekt-compiler/io/javaEx/Main.class
  Last modified Jul 10, 2014; size 665 bytes
  MD5 checksum 64e9784952e46e5bfd62415e4871ab72
  Compiled from "Main.java"
public class Main
  SourceFile: "Main.java"
  InnerClasses:
       static #3; //class Main$1
  minor version: 0
  major version: 51
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #11.#22        //  java/lang/Object."":()V
   #2 = Methodref          #23.#24        //  java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
   #3 = Class              #25            //  Main$1
   #4 = Methodref          #3.#26         //  Main$1."":(Ljava/lang/Integer;)V
   #5 = Fieldref           #27.#28        //  java/lang/System.out:Ljava/io/PrintStream;
   #6 = String             #29            //  Hello
   #7 = Methodref          #30.#31        //  java/io/PrintStream.println:(Ljava/lang/String;)V
   #8 = Methodref          #30.#32        //  java/io/PrintStream.println:(Ljava/lang/Object;)V
   #9 = InterfaceMethodref #33.#34        //  Lambda.closure:()V
  #10 = Class              #35            //  Main
  #11 = Class              #36            //  java/lang/Object
  #12 = Utf8               
  #13 = Utf8               InnerClasses
  #14 = Utf8               
  #15 = Utf8               ()V
  #16 = Utf8               Code
  #17 = Utf8               LineNumberTable
  #18 = Utf8               main
  #19 = Utf8               ([Ljava/lang/String;)V
  #20 = Utf8               SourceFile
  #21 = Utf8               Main.java
  #22 = NameAndType        #14:#15        //  "":()V
  #23 = Class              #37            //  java/lang/Integer
  #24 = NameAndType        #38:#39        //  valueOf:(I)Ljava/lang/Integer;
  #25 = Utf8               Main$1
  #26 = NameAndType        #14:#40        //  "":(Ljava/lang/Integer;)V
  #27 = Class              #41            //  java/lang/System
  #28 = NameAndType        #42:#43        //  out:Ljava/io/PrintStream;
  #29 = Utf8               Hello
  #30 = Class              #44            //  java/io/PrintStream
  #31 = NameAndType        #45:#46        //  println:(Ljava/lang/String;)V
  #32 = NameAndType        #45:#47        //  println:(Ljava/lang/Object;)V
  #33 = Class              #48            //  Lambda
  #34 = NameAndType        #49:#15        //  closure:()V
  #35 = Utf8               Main
  #36 = Utf8               java/lang/Object
  #37 = Utf8               java/lang/Integer
  #38 = Utf8               valueOf
  #39 = Utf8               (I)Ljava/lang/Integer;
  #40 = Utf8               (Ljava/lang/Integer;)V
  #41 = Utf8               java/lang/System
  #42 = Utf8               out
  #43 = Utf8               Ljava/io/PrintStream;
  #44 = Utf8               java/io/PrintStream
  #45 = Utf8               println
  #46 = Utf8               (Ljava/lang/String;)V
  #47 = Utf8               (Ljava/lang/Object;)V
  #48 = Utf8               Lambda
  #49 = Utf8               closure
{
  public Main();
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0       
         1: invokespecial #1                  // Method java/lang/Object."":()V
         4: return        
      LineNumberTable:
        line 3: 0

  public static void main(java.lang.String[]);
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=3, locals=4, args_size=1
         0: iconst_5      
         1: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
         4: astore_2      
         5: new           #3                  // class Main$1
         8: dup           
         9: aload_2       
        10: invokespecial #4                  // Method Main$1."":(Ljava/lang/Integer;)V
        13: astore_3      
        14: getstatic     #5                  // Field java/lang/System.out:Ljava/io/PrintStream;
        17: ldc           #6                  // String Hello
        19: invokevirtual #7                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        22: getstatic     #5                  // Field java/lang/System.out:Ljava/io/PrintStream;
        25: aload_2       
        26: invokevirtual #8                  // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
        29: aload_3       
        30: invokeinterface #9,  1            // InterfaceMethod Lambda.closure:()V
        35: return        
      LineNumberTable:
        line 7: 0
        line 8: 5
        line 14: 14
        line 15: 22
        line 16: 29
        line 17: 35
}

Main anonymous class $1

$ javap -c -v Main\$1.class 
Classfile /home/zelldon/Documents/Master/master_fub/CB-Project/SWPSoSe14/projekt-compiler/io/javaEx/Main$1.class
  Last modified Jul 10, 2014; size 636 bytes
  MD5 checksum 4f9abd6dd0c4f53fee418f252ab55149
  Compiled from "Main.java"
final class Main$1 implements Lambda
  SourceFile: "Main.java"
  EnclosingMethod: #21.#22                // Main.main
  InnerClasses:
       static #7; //class Main$1
  minor version: 0
  major version: 51
  flags: ACC_FINAL, ACC_SUPER

Constant pool:
   #1 = Fieldref           #7.#23         //  Main$1.val$i:Ljava/lang/Integer;
   #2 = Methodref          #8.#24         //  java/lang/Object."":()V
   #3 = Fieldref           #25.#26        //  java/lang/System.out:Ljava/io/PrintStream;
   #4 = String             #27            //  Hello
   #5 = Methodref          #28.#29        //  java/io/PrintStream.println:(Ljava/lang/String;)V
   #6 = Methodref          #28.#30        //  java/io/PrintStream.println:(Ljava/lang/Object;)V
   #7 = Class              #31            //  Main$1
   #8 = Class              #34            //  java/lang/Object
   #9 = Class              #35            //  Lambda
  #10 = Utf8               val$i
  #11 = Utf8               Ljava/lang/Integer;
  #12 = Utf8               
  #13 = Utf8               (Ljava/lang/Integer;)V
  #14 = Utf8               Code
  #15 = Utf8               LineNumberTable
  #16 = Utf8               closure
  #17 = Utf8               ()V
  #18 = Utf8               SourceFile
  #19 = Utf8               Main.java
  #20 = Utf8               EnclosingMethod
  #21 = Class              #36            //  Main
  #22 = NameAndType        #37:#38        //  main:([Ljava/lang/String;)V
  #23 = NameAndType        #10:#11        //  val$i:Ljava/lang/Integer;
  #24 = NameAndType        #12:#17        //  "":()V
  #25 = Class              #39            //  java/lang/System
  #26 = NameAndType        #40:#41        //  out:Ljava/io/PrintStream;
  #27 = Utf8               Hello
  #28 = Class              #42            //  java/io/PrintStream
  #29 = NameAndType        #43:#44        //  println:(Ljava/lang/String;)V
  #30 = NameAndType        #43:#45        //  println:(Ljava/lang/Object;)V
  #31 = Utf8               Main$1
  #32 = Utf8               
  #33 = Utf8               InnerClasses
  #34 = Utf8               java/lang/Object
  #35 = Utf8               Lambda
  #36 = Utf8               Main
  #37 = Utf8               main
  #38 = Utf8               ([Ljava/lang/String;)V
  #39 = Utf8               java/lang/System
  #40 = Utf8               out
  #41 = Utf8               Ljava/io/PrintStream;
  #42 = Utf8               java/io/PrintStream
  #43 = Utf8               println
  #44 = Utf8               (Ljava/lang/String;)V
  #45 = Utf8               (Ljava/lang/Object;)V
{
  final java.lang.Integer val$i;
    flags: ACC_FINAL, ACC_SYNTHETIC

  Main$1(java.lang.Integer);
    flags: 
    Code:
      stack=2, locals=2, args_size=2
         0: aload_0       
         1: aload_1       
         2: putfield      #1                  // Field val$i:Ljava/lang/Integer;
         5: aload_0       
         6: invokespecial #2                  // Method java/lang/Object."":()V
         9: return        
      LineNumberTable:
        line 8: 0

  public void closure();
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #4                  // String Hello
         5: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
        11: aload_0       
        12: getfield      #1                  // Field val$i:Ljava/lang/Integer;
        15: invokevirtual #6                  // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
        18: return        
      LineNumberTable:
        line 10: 0
        line 11: 8
        line 12: 18
}
Zelldon commented 10 years ago

219

Zelldon commented 10 years ago

TODO

Zelldon commented 10 years ago

done checked for lambda_call_in_other_function and lambda_func

Problem with nested_lambda and lambda_directions2