private static void generateGetEstimatedSize(ClassDefinition definition, List<FieldDefinition> stateFields) { MethodDefinition method = definition.declareMethod(a(PUBLIC), "getEstimatedSize", type(long.class)); Variable estimatedSize = method.getScope().declareVariable(long.class, "estimatedSize"); method.getBody().append(estimatedSize.set(constantLong(0L))); for (FieldDefinition stateField : stateFields) { method.getBody() .append(estimatedSize.set( BytecodeExpressions.add( estimatedSize, method.getThis().getField(stateField).invoke("getEstimatedSize", long.class)))); } method.getBody().append(estimatedSize.ret()); }
private static CompiledLambda defineLambdaMethod( RowExpressionCompiler innerExpressionCompiler, ClassDefinition classDefinition, String methodName, List<Parameter> inputParameters, LambdaDefinitionExpression lambda) { checkCondition(inputParameters.size() <= 254, NOT_SUPPORTED, "Too many arguments for lambda expression"); Class<?> returnType = Primitives.wrap(lambda.getBody().getType().getJavaType()); MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), methodName, type(returnType), inputParameters); Scope scope = method.getScope(); Variable wasNull = scope.declareVariable(boolean.class, "wasNull"); BytecodeNode compiledBody = innerExpressionCompiler.compile(lambda.getBody(), scope, Optional.empty()); method.getBody() .putVariable(wasNull, false) .append(compiledBody) .append(boxPrimitiveIfNecessary(scope, returnType)) .ret(returnType); Handle lambdaAsmHandle = new Handle( Opcodes.H_INVOKEVIRTUAL, method.getThis().getType().getClassName(), method.getName(), method.getMethodDescriptor(), false); return new CompiledLambda( lambdaAsmHandle, method.getReturnType(), method.getParameterTypes()); }
private static void generateGetSizeInBytesMethod(ClassDefinition classDefinition, FieldDefinition sizeField) { MethodDefinition getSizeInBytesMethod = classDefinition.declareMethod(a(PUBLIC), "getSizeInBytes", type(long.class)); Variable thisVariable = getSizeInBytesMethod.getThis(); getSizeInBytesMethod.getBody() .append(thisVariable.getField(sizeField)) .retLong(); }
private static void generateHashRowMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> joinChannelTypes) { Parameter position = arg("position", int.class); Parameter page = arg("blocks", Page.class); MethodDefinition hashRowMethod = classDefinition.declareMethod(a(PUBLIC), "hashRow", type(long.class), position, page); Variable resultVariable = hashRowMethod.getScope().declareVariable(long.class, "result"); hashRowMethod.getBody().push(0L).putVariable(resultVariable); for (int index = 0; index < joinChannelTypes.size(); index++) { BytecodeExpression type = constantType(callSiteBinder, joinChannelTypes.get(index)); BytecodeExpression block = page.invoke("getBlock", Block.class, constantInt(index)); hashRowMethod .getBody() .getVariable(resultVariable) .push(31L) .append(OpCode.LMUL) .append(typeHashCode(type, block, position)) .append(OpCode.LADD) .putVariable(resultVariable); } hashRowMethod .getBody() .getVariable(resultVariable) .retLong(); }
private static void generatePrepareFinal(ClassDefinition definition) { MethodDefinition method = definition.declareMethod( a(PUBLIC), "prepareFinal", type(void.class)); method.getBody().ret(); }
/** * Defines the generics bridge method with untyped args to the type specific read method. */ private void defineReadBridgeMethod() { Parameter protocol = arg("protocol", TProtocolReader.class); MethodDefinition method = new MethodDefinition( classDefinition, a(PUBLIC, BRIDGE, SYNTHETIC), "read", type(Object.class), protocol) .addException(Exception.class); method.getBody().append(method.getThis() .invoke("read", structType, ImmutableList.of(protocol)) .ret()); classDefinition.addMethod(method); }
/** * Defines the read method for a struct. */ private void defineReadStructMethod() { Parameter protocol = arg("protocol", TProtocolReader.class); MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "read", structType, protocol) .addException(Exception.class); // ProtocolReader reader = new ProtocolReader(protocol); Variable reader = method.getScope().declareVariable(ProtocolReader.class, "reader"); method.getBody().append(reader.set(newInstance(ProtocolReader.class, protocol))); // read all of the data in to local variables Map<Short, Variable> structData = readFieldValues(method, reader); // build the struct Variable result = buildStruct(method, structData); // return the instance method.getBody().append(result.ret()); }
private static InstructionNode invoke(OpCode invocationType, MethodDefinition method) { return new InvokeInstruction(invocationType, method.getDeclaringClass().getType(), method.getName(), method.getReturnType(), method.getParameterTypes()); }
public final BytecodeExpression invoke(MethodDefinition method, Iterable<? extends BytecodeExpression> parameters) { List<BytecodeExpression> params = ImmutableList.copyOf(parameters); checkArgument(method.getParameters().size() == params.size(), "Expected %s params found %s", method.getParameters().size(), params.size()); return invoke(method.getName(), method.getReturnType(), method.getParameterTypes(), parameters); }
public MethodDefinition declareMethod( EnumSet<Access> access, String name, ParameterizedType returnType, Iterable<Parameter> parameters) { MethodDefinition methodDefinition = new MethodDefinition(this, access, name, returnType, parameters); for (MethodDefinition method : methods) { if (name.equals(method.getName()) && method.getParameterTypes().equals(methodDefinition.getParameterTypes())) { throw new IllegalArgumentException("Method with same name and signature already exists: " + name); } } methods.add(methodDefinition); return methodDefinition; }
getMethodDescriptor(), genericMethodSignature(returnType, parameterTypes), exceptions);
public ClassDefinition( EnumSet<Access> access, ParameterizedType type, ParameterizedType superClass, ParameterizedType... interfaces) { requireNonNull(access, "access is null"); requireNonNull(type, "type is null"); requireNonNull(superClass, "superClass is null"); requireNonNull(interfaces, "interfaces is null"); this.access = access; this.type = type; this.superClass = superClass; this.interfaces.addAll(ImmutableList.copyOf(interfaces)); classInitializer = new MethodDefinition(this, a(STATIC), "<clinit>", ParameterizedType.type(void.class), ImmutableList.of()); }
private static void generateConstructor( ClassDefinition classDefinition, CachedInstanceBinder cachedInstanceBinder, Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap, Consumer<MethodDefinition> additionalStatements) { MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC)); BytecodeBlock body = constructorDefinition.getBody(); Variable thisVariable = constructorDefinition.getThis(); body.comment("super();") .append(thisVariable) .invokeConstructor(Object.class); additionalStatements.accept(constructorDefinition); cachedInstanceBinder.generateInitializations(thisVariable, body); body.ret(); }
private void generateProjectMethod( ClassDefinition classDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap, String methodName, RowExpression projection) { Parameter session = arg("session", ConnectorSession.class); Parameter cursor = arg("cursor", RecordCursor.class); Parameter output = arg("output", BlockBuilder.class); MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), methodName, type(void.class), session, cursor, output); method.comment("Projection: %s", projection.toString()); Scope scope = method.getScope(); Variable wasNullVariable = scope.declareVariable(type(boolean.class), "wasNull"); RowExpressionCompiler compiler = new RowExpressionCompiler( callSiteBinder, cachedInstanceBinder, fieldReferenceCompiler(cursor), metadata.getFunctionRegistry(), compiledLambdaMap); method.getBody() .comment("boolean wasNull = false;") .putVariable(wasNullVariable, false) .comment("evaluate projection: " + projection.toString()) .append(compiler.compile(projection, scope, Optional.of(output))) .ret(); }
private static MethodDefinition generateGetIntermediateType(ClassDefinition definition, CallSiteBinder callSiteBinder, List<Type> type) { MethodDefinition methodDefinition = definition.declareMethod(a(PUBLIC), "getIntermediateType", type(Type.class)); if (type.size() == 1) { methodDefinition.getBody() .append(constantType(callSiteBinder, getOnlyElement(type))) .retObject(); } else { methodDefinition.getBody() .append(constantType(callSiteBinder, RowType.anonymous(type))) .retObject(); } return methodDefinition; }
/** * Defines the generics bridge method with untyped args to the type specific write method. */ private void defineWriteBridgeMethod() { Parameter struct = arg("struct", Object.class); Parameter protocol = arg("protocol", TProtocolWriter.class); MethodDefinition method = new MethodDefinition( classDefinition, a(PUBLIC, BRIDGE, SYNTHETIC), "write", null, struct, protocol) .addException(Exception.class); method.getBody().append(method.getThis() .invoke("write", type(void.class), ImmutableList.of(structType, protocol.getType()), ImmutableList.of(struct.cast(structType), protocol)) .ret()); classDefinition.addMethod(method); }