diff options
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeUtils.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeUtils.java | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeUtils.java b/src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeUtils.java index 4eb5433529..832d4e3e51 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeUtils.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeUtils.java @@ -13,8 +13,12 @@ // limitations under the License. package com.google.devtools.build.lib.syntax.compiler; +import net.bytebuddy.description.method.MethodDescription.ForLoadedMethod; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.description.type.generic.GenericTypeDescription; import net.bytebuddy.implementation.bytecode.ByteCodeAppender; import net.bytebuddy.implementation.bytecode.ByteCodeAppender.Compound; +import net.bytebuddy.implementation.bytecode.Removal; import net.bytebuddy.implementation.bytecode.StackManipulation; import net.bytebuddy.implementation.bytecode.member.FieldAccess; import net.bytebuddy.implementation.bytecode.member.MethodInvocation; @@ -27,6 +31,29 @@ import java.util.List; public class ByteCodeUtils { /** + * Helper method to wrap {@link StackManipulation}s into a {@link ByteCodeAppender} and add it + * to a list of appenders. + */ + public static void append(List<ByteCodeAppender> code, StackManipulation... manipulations) { + code.add(new ByteCodeAppender.Simple(manipulations)); + } + + /** + * As {@link #invoke(Class, String, Class...)} and additionally clears the returned value from + * the stack, if any. + */ + public static StackManipulation cleanInvoke( + Class<?> clazz, String methodName, Class<?>... parameterTypes) { + ForLoadedMethod method = ReflectionUtils.getMethod(clazz, methodName, parameterTypes); + GenericTypeDescription returnType = method.getReturnType(); + if (returnType.equals(TypeDescription.VOID)) { + return MethodInvocation.invoke(method); + } + return new StackManipulation.Compound( + MethodInvocation.invoke(method), Removal.pop(returnType.asErasure())); + } + + /** * Create a {@link ByteCodeAppender} applying a list of them. * * <p>Exists just because {@link Compound} does not have a constructor taking a list. |