aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/syntax/compiler/NewObject.java
blob: 4b5881b91a8a35a61ea0b091683f079c26359230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.syntax.compiler;

import net.bytebuddy.description.method.MethodDescription.ForLoadedConstructor;
import net.bytebuddy.implementation.Implementation.Context;
import net.bytebuddy.implementation.bytecode.Duplication;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.implementation.bytecode.member.MethodInvocation;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.util.List;

/**
 * Builds byte code for new object creation from constructor calls.
 *
 * <p>Java byte code for new object creation looks like the following pseudo code:
 * <pre>
 * NEW
 * DUP
 * ...load constructor parameters...
 * INVOKESPECIAL constructor
 * </pre>
 * This is because a constructor is actually called on the reference to the object itself, which
 * is left on the stack by NEW.
 *
 * This class helps with wrapping the parameter loading with this structure.
 */
public class NewObject implements StackManipulation {

  private final ForLoadedConstructor constructor;
  private final StackManipulation arguments;

  /**
   * Intermediate state builder for new object construction with missing constructor argument
   * loading.
   */
  public static final class NewObjectBuilder {
    private final ForLoadedConstructor constructor;

    private NewObjectBuilder(ForLoadedConstructor constructor) {
      this.constructor = constructor;
    }

    /**
     * Adds the argument loading in the correct for new object construction.
     */
    public NewObject arguments(StackManipulation... arguments) {
      return new NewObject(constructor, new StackManipulation.Compound(arguments));
    }

    /**
     * Adds the argument loading in the correct for new object construction.
     */
    public NewObject arguments(List<StackManipulation> arguments) {
      return new NewObject(constructor, new StackManipulation.Compound(arguments));
    }
  }

  private NewObject(ForLoadedConstructor constructor, StackManipulation arguments) {
    this.constructor = constructor;
    this.arguments = arguments;
  }

  /**
   * Looks for a constructor in the class with the given parameter types and returns an
   * intermediate builder.
   */
  public static NewObjectBuilder fromConstructor(Class<?> clazz, Class<?>... parameterTypes) {
    return new NewObjectBuilder(ReflectionUtils.getConstructor(clazz, parameterTypes));
  }

  @Override
  public boolean isValid() {
    return true;
  }

  @Override
  public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
    methodVisitor.visitTypeInsn(Opcodes.NEW, constructor.getDeclaringType().getInternalName());
    return new StackManipulation.Compound(
            Duplication.SINGLE, arguments, MethodInvocation.invoke(constructor))
        .apply(methodVisitor, implementationContext);
  }

  @Override
  public String toString() {
    return "NewObject(" + constructor + ", " + arguments + ")";
  }
}