1 /*
2  * Copyright 2014 - 2020 Rafael Winterhalter
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.bytebuddy.implementation.bytecode.constant;
17
18 import net.bytebuddy.implementation.Implementation;
19 import net.bytebuddy.implementation.bytecode.StackManipulation;
20 import net.bytebuddy.implementation.bytecode.StackSize;
21 import net.bytebuddy.jar.asm.MethodVisitor;
22 import net.bytebuddy.jar.asm.Opcodes;
23
24 /**
25  * Represents a stack manipulation to load a {@code null} pointer onto the operand stack.
26  */

27 public enum NullConstant implements StackManipulation {
28
29     /**
30      * The singleton instance.
31      */

32     INSTANCE(StackSize.SINGLE);
33
34     /**
35      * The size impact of loading the {@code null} reference onto the operand stack.
36      */

37     private final Size size;
38
39     /**
40      * Creates a null constant.
41      *
42      * @param size The size of the constant on the operand stack.
43      */

44     NullConstant(StackSize size) {
45         this.size = size.toIncreasingSize();
46     }
47
48     /**
49      * {@inheritDoc}
50      */

51     public boolean isValid() {
52         return true;
53     }
54
55     /**
56      * {@inheritDoc}
57      */

58     public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
59         methodVisitor.visitInsn(Opcodes.ACONST_NULL);
60         return size;
61     }
62 }
63