Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/io/airlift/bytecode/BytecodeBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.google.common.collect.ImmutableList;
import io.airlift.bytecode.debug.LineNumberNode;
import io.airlift.bytecode.instruction.BootstrapMethod;
import io.airlift.bytecode.instruction.Constant;
import io.airlift.bytecode.instruction.InvokeInstruction;
import io.airlift.bytecode.instruction.JumpInstruction;
Expand Down Expand Up @@ -494,6 +495,16 @@ public BytecodeNode invokeDynamic(String name,
return this;
}

public BytecodeNode invokeDynamic(String name,
ParameterizedType returnType,
Iterable<ParameterizedType> parameterTypes,
BootstrapMethod bootstrapMethod,
List<Object> bootstrapArgs)
{
nodes.add(InvokeInstruction.invokeDynamic(name, returnType, parameterTypes, bootstrapMethod, bootstrapArgs));
return this;
}

public BytecodeBlock ret(Class<?> type)
{
if (type == long.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.airlift.bytecode.MethodDefinition;
import io.airlift.bytecode.OpCode;
import io.airlift.bytecode.ParameterizedType;
import io.airlift.bytecode.instruction.BootstrapMethod;

import java.lang.invoke.MethodType;
import java.lang.reflect.Constructor;
Expand All @@ -27,7 +28,6 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Iterables.toArray;
import static io.airlift.bytecode.ParameterizedType.type;
import static io.airlift.bytecode.expression.ArithmeticBytecodeExpression.createArithmeticBytecodeExpression;
import static io.airlift.bytecode.instruction.Constant.loadBoolean;
Expand Down Expand Up @@ -176,7 +176,26 @@ public static BytecodeExpression constantDynamic(
name,
type,
bootstrapMethod,
toArray(bootstrapArgs, Object.class)));
ImmutableList.copyOf(bootstrapArgs)));
}

public static BytecodeExpression constantDynamic(
String name,
ParameterizedType type,
BootstrapMethod bootstrapMethod,
Iterable<? extends Object> bootstrapArgs)
{
requireNonNull(name, "name is null");
requireNonNull(type, "type is null");
requireNonNull(bootstrapMethod, "bootstrapMethod is null");
requireNonNull(bootstrapArgs, "bootstrapArgs is null");
return new ConstantBytecodeExpression(
type,
loadDynamic(
name,
type,
bootstrapMethod,
ImmutableList.copyOf(bootstrapArgs)));
}

public static BytecodeExpression defaultValue(ParameterizedType type)
Expand Down Expand Up @@ -575,6 +594,23 @@ public static BytecodeExpression invokeDynamic(
ParameterizedType returnType,
Iterable<ParameterizedType> parameterTypes,
Iterable<? extends BytecodeExpression> parameters)
{
return new InvokeDynamicBytecodeExpression(
BootstrapMethod.from(bootstrapMethod),
bootstrapArgs,
methodName,
returnType,
parameters,
parameterTypes);
}

public static BytecodeExpression invokeDynamic(
BootstrapMethod bootstrapMethod,
Iterable<? extends Object> bootstrapArgs,
String methodName,
ParameterizedType returnType,
Iterable<ParameterizedType> parameterTypes,
Iterable<? extends BytecodeExpression> parameters)
{
return new InvokeDynamicBytecodeExpression(
bootstrapMethod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import io.airlift.bytecode.BytecodeNode;
import io.airlift.bytecode.MethodGenerationContext;
import io.airlift.bytecode.ParameterizedType;
import io.airlift.bytecode.instruction.BootstrapMethod;

import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -29,15 +29,15 @@
class InvokeDynamicBytecodeExpression
extends BytecodeExpression
{
private final Method bootstrapMethod;
private final BootstrapMethod bootstrapMethod;
private final List<Object> bootstrapArgs;
private final String methodName;
private final ParameterizedType returnType;
private final List<BytecodeExpression> parameters;
private final List<ParameterizedType> parameterTypes;

InvokeDynamicBytecodeExpression(
Method bootstrapMethod,
BootstrapMethod bootstrapMethod,
Iterable<?> bootstrapArgs,
String methodName,
ParameterizedType returnType,
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/io/airlift/bytecode/instruction/BootstrapMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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 io.airlift.bytecode.instruction;

import com.google.common.collect.ImmutableList;
import io.airlift.bytecode.ParameterizedType;

import java.lang.reflect.Method;
import java.util.List;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.airlift.bytecode.ParameterizedType.type;
import static java.util.Arrays.stream;
import static java.util.Objects.requireNonNull;

public class BootstrapMethod
{
private final ParameterizedType ownerClass;
private final String name;
private final ParameterizedType returnType;
private final List<ParameterizedType> parameterTypes;

public BootstrapMethod(ParameterizedType ownerClass, String name, ParameterizedType returnType, List<ParameterizedType> parameterTypes)
{
this.ownerClass = requireNonNull(ownerClass, "ownerClass is null");
this.name = requireNonNull(name, "name is null");
this.returnType = requireNonNull(returnType, "returnType is null");
this.parameterTypes = ImmutableList.copyOf(requireNonNull(parameterTypes, "parameterTypes is null"));
}

public ParameterizedType getOwnerClass()
{
return ownerClass;
}

public String getName()
{
return name;
}

public ParameterizedType getReturnType()
{
return returnType;
}

public List<ParameterizedType> getParameterTypes()
{
return parameterTypes;
}

public static BootstrapMethod from(Method method)
{
return new BootstrapMethod(
type(method.getDeclaringClass()),
method.getName(),
type(method.getReturnType()),
stream(method.getParameterTypes())
.map(ParameterizedType::type)
.collect(toImmutableList()));
}
}
23 changes: 18 additions & 5 deletions src/main/java/io/airlift/bytecode/instruction/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public static Constant loadDynamic(
return new DynamicConstant(
name,
type,
bootstrapMethod,
BootstrapMethod.from(bootstrapMethod),
ImmutableList.copyOf(bootstrapArguments));
}

Expand All @@ -171,6 +171,19 @@ public static Constant loadDynamic(
ParameterizedType type,
Method bootstrapMethod,
Object... bootstrapArguments)
{
return new DynamicConstant(
name,
type,
BootstrapMethod.from(bootstrapMethod),
ImmutableList.copyOf(bootstrapArguments));
}

public static Constant loadDynamic(
String name,
ParameterizedType type,
BootstrapMethod bootstrapMethod,
Iterable<Object> bootstrapArguments)
{
return new DynamicConstant(
name,
Expand Down Expand Up @@ -664,10 +677,10 @@ public static class DynamicConstant
{
private final String name;
private final ParameterizedType type;
private final Method bootstrapMethod;
private final BootstrapMethod bootstrapMethod;
private final List<Object> bootstrapArguments;

public DynamicConstant(String name, ParameterizedType type, Method bootstrapMethod, List<Object> bootstrapArguments)
public DynamicConstant(String name, ParameterizedType type, BootstrapMethod bootstrapMethod, List<Object> bootstrapArguments)
{
this.name = name;
this.type = type;
Expand All @@ -686,7 +699,7 @@ public String getName()
return name;
}

public Method getBootstrapMethod()
public BootstrapMethod getBootstrapMethod()
{
return bootstrapMethod;
}
Expand All @@ -701,7 +714,7 @@ public void accept(MethodVisitor visitor, MethodGenerationContext generationCont
{
Handle bootstrapMethodHandle = new Handle(
Opcodes.H_INVOKESTATIC,
type(bootstrapMethod.getDeclaringClass()).getClassName(),
bootstrapMethod.getOwnerClass().getClassName(),
bootstrapMethod.getName(),
methodDescription(
bootstrapMethod.getReturnType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private static InstructionNode invoke(OpCode invocationType, Class<?> target, St
public static InstructionNode invokeDynamic(String name,
ParameterizedType returnType,
Iterable<ParameterizedType> parameterTypes,
Method bootstrapMethod,
BootstrapMethod bootstrapMethod,
Iterable<Object> bootstrapArguments)
{
return new InvokeDynamicInstruction(name,
Expand All @@ -269,6 +269,19 @@ public static InstructionNode invokeDynamic(String name,
ImmutableList.copyOf(bootstrapArguments));
}

public static InstructionNode invokeDynamic(String name,
ParameterizedType returnType,
Iterable<ParameterizedType> parameterTypes,
Method bootstrapMethod,
Iterable<Object> bootstrapArguments)
{
return new InvokeDynamicInstruction(name,
returnType,
parameterTypes,
BootstrapMethod.from(bootstrapMethod),
ImmutableList.copyOf(bootstrapArguments));
}

public static InstructionNode invokeDynamic(String name,
ParameterizedType returnType,
Iterable<ParameterizedType> parameterTypes,
Expand All @@ -278,7 +291,7 @@ public static InstructionNode invokeDynamic(String name,
return new InvokeDynamicInstruction(name,
returnType,
parameterTypes,
bootstrapMethod,
BootstrapMethod.from(bootstrapMethod),
ImmutableList.copyOf(bootstrapArguments));
}

Expand All @@ -290,7 +303,7 @@ public static InstructionNode invokeDynamic(String name,
return new InvokeDynamicInstruction(name,
type(methodType.returnType()),
methodType.parameterList().stream().map(ParameterizedType::type).collect(toImmutableList()),
bootstrapMethod,
BootstrapMethod.from(bootstrapMethod),
ImmutableList.copyOf(bootstrapArguments));
}

Expand All @@ -302,7 +315,7 @@ public static InstructionNode invokeDynamic(String name,
return new InvokeDynamicInstruction(name,
type(methodType.returnType()),
methodType.parameterList().stream().map(ParameterizedType::type).collect(toImmutableList()),
bootstrapMethod,
BootstrapMethod.from(bootstrapMethod),
ImmutableList.copyOf(bootstrapArguments));
}

Expand Down Expand Up @@ -377,13 +390,13 @@ public <T> T accept(BytecodeNode parent, BytecodeVisitor<T> visitor)
public static class InvokeDynamicInstruction
extends InvokeInstruction
{
private final Method bootstrapMethod;
private final BootstrapMethod bootstrapMethod;
private final List<Object> bootstrapArguments;

public InvokeDynamicInstruction(String name,
ParameterizedType returnType,
Iterable<ParameterizedType> parameterTypes,
Method bootstrapMethod,
BootstrapMethod bootstrapMethod,
List<Object> bootstrapArguments)
{
super(INVOKEDYNAMIC, null, name, returnType, parameterTypes);
Expand All @@ -394,8 +407,9 @@ public InvokeDynamicInstruction(String name,
@Override
public void accept(MethodVisitor visitor, MethodGenerationContext generationContext)
{
Handle bootstrapMethodHandle = new Handle(Opcodes.H_INVOKESTATIC,
type(bootstrapMethod.getDeclaringClass()).getClassName(),
Handle bootstrapMethodHandle = new Handle(
Opcodes.H_INVOKESTATIC,
bootstrapMethod.getOwnerClass().getClassName(),
bootstrapMethod.getName(),
methodDescription(
bootstrapMethod.getReturnType(),
Expand All @@ -408,7 +422,7 @@ public void accept(MethodVisitor visitor, MethodGenerationContext generationCont
bootstrapArguments.toArray(new Object[0]));
}

public Method getBootstrapMethod()
public BootstrapMethod getBootstrapMethod()
{
return bootstrapMethod;
}
Expand Down