JVM-编译期处理(语法糖)

编译期处理(语法糖)

所谓的 语法糖 ,其实就是指 java 编译器把 *.java 源码编译为 *.class 字节码的过程中,自动生成 和转换的一些代码,主要是为了减轻程序员的负担,算是 java 编译器给我们的一个额外福利(给糖吃嘛)

注意,以下代码的分析,借助了 javap 工具,idea 的反编译功能,idea 插件 jclasslib 等工具。另外, 编译器转换的结果直接就是 class 字节码,只是为了便于阅读,给出了 几乎等价 的 java 源码方式,并不是编译器还会转换出中间的 java 源码,切记

1 默认构造器

public class Candy1 { }

编译成class后的代码:

public class Candy1 { 
	// 这个无参构造是编译器帮助我们加上的 
		public Candy1() { 
				super(); // 即调用父类 Object 的无参构造方法,即调用 java/lang/Object." <init>":()V 
		} 
}

2 自动拆装箱

这个特性是 JDK 5 开始加入的, 代码片段1 :

public class Candy2 { 
	public static void main(String[] args) { 
		Integer x = 1; 
		int y = x; 
	} 
}

这段代码在 JDK 5 之前是无法编译通过的,必须改写为 代码片段 2 :

public class Candy2 { 
	public static void main(String[] args) { 
		Integer x = Integer.valueOf(1); 
		int y = x.intValue(); 
	} 
}
显然之前版本的代码太麻烦了,需要在基本类型和包装类型之间来回转换(尤其是集合类中操作的都是 包装类型),因此这些转换的事情在 JDK 5 以后都由编译器在编译阶段完成。即 `代码片段1` 都会在编 译阶段被转换为 `代码片段2`  

3 泛型集合取值

泛型也是在 JDK 5 开始加入的特性,但 java 在编译泛型代码后会执行 泛型擦除 的动作,即泛型信息 在编译为字节码之后就丢失了,实际的类型都当做了 Object 类型来处理:

public class Candy3 {

	public static void main(String[] args) { 
  	List<Integer> list = new ArrayList<>(); 
 	  list.add(10); // 实际调用的是 List.add(Object e) 
 	  Integer x = list.get(0); // 实际调用的是 Object obj = List.get(int index); 
  }
}

所以在取值时,编译器真正生成的字节码中,还要额外做一个类型转换的操作:

// 需要将 Object 转为 Integer 
Integer x = (Integer)list.get(0);

如果前面的

x 变量类型修改为 int 基本类型那么最终生成的字节码是:

// 需要将 Object 转为 Integer, 并执行拆箱操作 
int x = ((Integer)list.get(0)).intValue();

还好这些麻烦事都不用自己做。

擦除的是字节码上的泛型信息,可以看到 LocalVariableTypeTable 仍然保留了方法参数泛型的信息

 public cn.itcast.jvm.t3.candy.Candy3();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 11: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcn/itcast/jvm/t3/candy/Candy3;
public static void main(java.lang.String[]); 
	descriptor: ([Ljava/lang/String;)V 
  flags: ACC_PUBLIC, ACC_STATIC 
  Code:
		stack=2, locals=3, args_size=1 
       -------new ArrayList对象----------         
       0: new #2   // class java/util/ArrayList 
       3: dup 
       4: invokespecial #3 // Method java/util/ArrayList."<init>":()V 
       7: astore_1 
                
       8: aload_1 
       9: bipush 10 
       11: invokestatic #4 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 
       //add方法的参数类型实际是Object类型
       14: invokeinterface #5, 2 // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z 
       19: pop 
       20: aload_1 
       21: iconst_0 
       //get方法的参数类型实际是Object类型
       22: invokeinterface #6, 2 // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object; 
       //	强制类型转换         
       27: checkcast #7 // class java/lang/Integer 
       30: astore_2 
       31: return 
       LineNumberTable:
					line 8: 0 
          line 9: 8 
          line 10: 20 
          line 11: 31 
       LocalVariableTable:
			  	Start Length Slot Name    Signature
              0   32    0   args      [Ljava/lang/String; 
              8   24    1   list      Ljava/util/List;
       LocalVariableTypeTable:
					Start Length  Slot   Signature
					    8   24     1      Ljava/util/List<Ljava/lang/Integer;>;
  • list.add 和 get 方法在bycecode层面上的参数类型都是Object类型,最后会执行checkcast指令进行类型转换为真实类型

  • 方法体内部的泛型在虚拟机执行时会进行泛型擦除

  • 使用反射,能够获得方法参数和返回值中的泛型信息:

public Set<Integer> test(List<String> list, Map<Integer, Object> map) { }

Method test = Candy3.class.getMethod("test", List.class, Map.class);
        Type[] types = test.getGenericParameterTypes();
        for (Type type : types) {
            if (type instanceof ParameterizedType) {
                ParameterizedType parameterizedType = (ParameterizedType) type;
                System.out.println("原始类型 - " + parameterizedType.getRawType());
                Type[] arguments = parameterizedType.getActualTypeArguments();
                for (int i = 0; i < arguments.length; i++) {
                    System.out.printf("泛型参数[%d] - %s\n", i, arguments[i]);
                }
            }
				}

输出

原始类型 - interface java.util.List 
泛型参数[0] - class java.lang.String 
原始类型 - interface java.util.Map 
泛型参数[0] - class java.lang.Integer 
泛型参数[1] - class java.lang.Object

4 可变参数

可变参数也是 JDK 5 开始加入的新特性: 例如:

public class Candy4 {

    public static void foo(String... args) {
        String[] array = args;
        // 直接赋值 
        System.out.println(array);
    }
    public static void main(String[] args) {
        foo("hello", "world");
    }
}

可变参数 String... args 其实是一个 String[] args ,从代码中的赋值语句中就可以看出来。 同 样 java 编译器会在编译期间将上述代码变换为:

public class Candy4 {
    public static void foo(String[] args) {
        String[] array = args;
        // 直接赋值 
        System.out.println(array);
    }
    public static void main(String[] args) {
        foo(new String[]{"hello", "world"});
    }
}

注意 如果调用了 foo() 则等价代码为 foo(new String[]{}) ,创建了一个空的数组,而不会 传递 null 进去

5 foreach 循环

仍是 JDK 5 开始引入的语法糖,数组的循环:

public class Candy5_1 {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        // 数组赋初值的简化写法也是语法糖哦 
        for (int e : array) {
            System.out.println(e);
        }
    }
} 

会被编译器转换为:

public class Candy5_1 {
    public Candy5_1() {}
    public static void main(String[] args) {
        int[] array = new int[]{1, 2, 3, 4, 5};
        for (int i = 0; i < array.length; ++i) {
            int e = array[i];
            System.out.println(e);
        }
    }
}

而集合的循环:

public class Candy5_2 {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        for (Integer i : list) {
            System.out.println(i);
        }
    }
}

实际被编译器转换为对迭代器的调用:

public class Candy5_2 {
    public Candy5_2() {}
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        Iterator iter = list.iterator();
        while (iter.hasNext()) {
            Integer e = (Integer) iter.next();
            System.out.println(e);
        }
    }
}

注意 foreach 循环写法,能够配合数组,以及所有实现了 Iterable 接口的集合类一起使用,其 中 Iterable 用来获取集合的迭代器( Iterator

6 switch 字符串

从 JDK 7 开始,switch 可以作用于字符串和枚举类,这个功能其实也是语法糖,例如:

public class Candy6_1 {
    public static void choose(String str) {
        switch (str) {
            case "hello": {
                System.out.println("h");
                break;
            }
            case "world": {
                System.out.println("w");
                break;
            }
        }
    }
}

注意

switch 配合 String 和枚举使用时,变量不能为null,原因分析完语法糖转换后的代码应当自 然清楚

会被编译器转换为:

public class Candy6_1 {
    public Candy6_1() { }
    public static void choose(String str) {
        byte x = -1;
        switch (str.hashCode()) {
            case 99162322: // hello 的 hashCode 
                if (str.equals("hello")) {
                    x = 0;
                }
                break;
            case 113318802: // world 的 hashCode 
                if (str.equals("world")) {
                    x = 1;
                }
        }
        switch (x) {
            case 0:
                System.out.println("h");
                break;
            case 1:
							  System.out.println("w");
        }
    }
}

可以看到,执行了两遍 switch,第一遍是根据字符串的 hashCodeequals 将字符串的转换为相应 byte 类型,第二遍才是利用 byte 执行进行比较。

为什么第一遍时必须既比较 hashCode,又利用 equals 比较呢?hashCode 是为了提高效率,减少可 能的比较;而 equals 是为了防止 hashCode 冲突,例如 BMC. 这两个字符串的hashCode值都是 2123 ,如果有如下代码:

public class Candy6_2 {
    public static void choose(String str) {
        switch (str) {
            case "BM": {
                System.out.println("h");
                break;
            }
            case "C.": {
                System.out.println("w");
                break;
            }
        }
    }
}

会被编译器转换为:

public class Candy6_2 {
    public Candy6_2() { }
    public static void choose(String str) {
        byte x = -1;
        switch (str.hashCode()) {
            case 2123: // hashCode 值可能相同,需要进一步用 equals 比较 
                if (str.equals("C.")) {
                    x = 1;
                } else if (str.equals("BM")) {
                    x = 0;
                }
            default:
                switch (x) {
                    case 0:
                        System.out.println("h");
                        break;
                    case 1:
                        System.out.println("w");
                }
        }
    }
}

7 switch 枚举

switch 枚举的例子,原始代码:

enum Sex { 
  MALE, FEMALE 
}
public class Candy7 {
    public static void foo(Sex sex) {
        switch (sex) {
            case MALE:
                System.out.println("男"); break;
            case FEMALE:
                System.out.println("女"); break;
        }
    }
}

转换后代码:

public class Candy7 {
    /**
     * 定义一个合成类,$MAP这个名字自己取的,jvm中不是这样(仅 jvm 使用,对我们不可见) 
     * 用来映射枚举的 ordinal 与数组元素的关系 
     * 枚举的 ordinal 表示枚举对象的序号,从 0 开始 
     * 即 MALE 的 ordinal()=0,FEMALE 的 ordinal()=1
     */
    static class $MAP {

        // 数组大小即为枚举元素个数,里面存储case用来对比的数字
        static int[] map = new int[2];
        static {
            map[Sex.MALE.ordinal()] = 1;
            map[Sex.FEMALE.ordinal()] = 2;
        }
    }
    public static void foo(Sex sex) {
        int x = $MAP.map[sex.ordinal()];
        switch (x) {
            case 1:
                System.out.println("男");
                break;
            case 2:
                System.out.println("女");
                break;
        }
    }
}

8 枚举类

以前面的性别枚举为例:

enum Sex { 
  MALE, FEMALE 
}

转换后代码:

public final class Sex extends Enum<Sex> {
    public static final Sex MALE;
    public static final Sex FEMALE;
    private static final Sex[] $VALUES;

    static {
        MALE = new Sex("MALE", 0);
        FEMALE = new Sex("FEMALE", 1);
        $VALUES = new Sex[]{MALE, FEMALE};
    }

    /**
     * Sole constructor. Programmers cannot invoke this constructor.
     * It is for use by code emitted by the compiler in response to
     * enum type declarations.
     * 
     * @param name - The name of this enum constant, which is the identifier  used to declare it.
     *
     * @param ordinal - The ordinal of this enumeration constant (its position in the enum declaration, where the initial constant is assigned
     */
    private Sex(String name, int ordinal) {
        super(name, ordinal);
    }
    public static Sex[] values() {
        return $VALUES.clone();
    }
    public static Sex valueOf(String name) {
        return Enum.valueOf(Sex.class, name);
    }
}

9 匿名内部类

源代码:

public class Candy11 {

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("ok");
            }
        };
    }
}

转换后代码:

// 额外生成的类 
final class Candy11$1 implements Runnable {
    Candy11$1() { }
    public void run() {
        System.out.println("ok");
    }

}
public class Candy11 {
    public static void main(String[] args) {
        Runnable runnable = new Candy11$1();
    }
}

引用局部变量的匿名内部类,源代码:

public class Candy11 {

    public static void test(final int x) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("ok:" + x);
            }
        };
    }
}

转换后代码:

// 额外生成的类
final class Candy11$1 implements Runnable {
    int val$x;

    Candy11$1(int x) {
        this.val$x = x;
    }
    public void run() {
        System.out.println("ok:" + this.val$x);
    }

}
public class Candy11 {
    public static void test(final int x) {
        Runnable runnable = new Candy11$1(x);
    }
}

注意 这同时解释了为什么匿名内部类引用局部变量时,局部变量必须是 final 的:因为在创建 Candy11$1 对象时,将 x 的值赋值给了 Candy11$1 对象的 val$x 属 性 ,所以x 不应该再发生变化了,如果 变化 , 那 么 val$x 属性没有机会再跟着一起变化 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值