use of org.apache.derby.iapi.services.compiler.MethodBuilder in project derby by apache.
the class MethodCallNode method generateVarargs.
/**
* <p>
* Generate the trailing routine arguments into a varargs array and
* push that array onto the stack.
* </p>
*/
private void generateVarargs(ExpressionClassBuilder acb, MethodBuilder mb) throws StandardException {
// the vararg is the last declared arg of the Java method. it is always
// an array type. right now we only support vararg static methods.
// if we have to support vararg constructors in the future, then this code
// will need adjustment.
int firstVarargIdx = getFirstVarargIdx();
String arrayType = methodParameterTypes[firstVarargIdx];
String cellType = stripOneArrayLevel(arrayType);
String varargType = cellType;
// must strip another array level off of out and in/out parameters
if (routineInfo != null) {
if (routineInfo.getParameterModes()[firstVarargIdx] != (ParameterMetaData.parameterModeIn)) {
varargType = stripOneArrayLevel(varargType);
}
}
int varargCount = methodParms.length - firstVarargIdx;
if (varargCount < 0) {
varargCount = 0;
}
// allocate an array to hold the varargs
LocalField arrayField = acb.newFieldDeclaration(Modifier.PRIVATE, arrayType);
MethodBuilder cb = acb.getConstructor();
cb.pushNewArray(cellType, varargCount);
cb.setField(arrayField);
// now put the arguments into the array
for (int i = 0; i < varargCount; i++) {
// push the array onto the stack
mb.getField(arrayField);
// evaluate the parameter and push it onto the stack
generateAndCastOneParameter(acb, mb, i + firstVarargIdx, cellType);
// move the parameter into the array, pop the stack
mb.setArrayElement(i);
}
// push the array onto the stack. it is the last parameter to the varargs routine.
mb.getField(arrayField);
}
use of org.apache.derby.iapi.services.compiler.MethodBuilder in project derby by apache.
the class ParameterNode method generateParameterValueSet.
// //////////////////////////////////////////////////////////////////
//
// STATIC ROUTINES
//
// //////////////////////////////////////////////////////////////////
/**
* Generate the code to create the ParameterValueSet, if necessary,
* when constructing the activation. Also generate the code to call
* a method that will throw an exception if we try to execute without
* all the parameters being set.
*
* This generated code goes into the Activation's constructor early on.
*
* @param acb The ExpressionClassBuilder for the class we're building
* @param numberOfParameters number of parameters for this statement
* @param parameterList The parameter list for the statement.
*
* @exception StandardException on error
*/
static void generateParameterValueSet(ExpressionClassBuilder acb, int numberOfParameters, List<ParameterNode> parameterList) throws StandardException {
if (numberOfParameters > 0) {
MethodBuilder constructor = acb.getConstructor();
/*
** Check the first parameter to see if it is a return
** parameter.
*/
boolean hasReturnParam = (parameterList.get(0)).isReturnOutputParam();
/*
** Generate the following:
**
** pvs =
** getLanguageConnectionContext()
** .getLanguageFactory()
** .getParameterValueSet(numberOfParameters);
**
** pvs is a ParameterValueSet that lives in the superclass of
** the activation being generated.
*/
// for the put field down below
constructor.pushThis();
/* Generate the call to getContext */
// ?X constructor.pushThis();
// ?Xconstructor.callMethod(VMOpcode.INVOKEINTERFACE, ClassName.Activation, "getLanguageConnectionContext",
// ?X ClassName.LanguageConnectionContext, 0);
/*
** Call getLanguageFactory()
*/
// ?Xconstructor.callMethod(VMOpcode.INVOKEINTERFACE, (String) null, "getLanguageFactory",
// ?X ClassName.LanguageFactory, 0);
/*
** Call getParameterValueSet(<number of parameters>, <hasReturnParam>)
*/
// first arg
constructor.push(numberOfParameters);
// second arg
constructor.push(hasReturnParam);
constructor.callMethod(VMOpcode.INVOKEVIRTUAL, ClassName.BaseActivation, "setParameterValueSet", "void", 2);
// ?Xconstructor.callMethod(VMOpcode.INVOKEINTERFACE, (String) null, "getParameterValueSet",
// ?X ClassName.ParameterValueSet, 2);
/* Assign the return from getParameterValueSet() to the field */
// ?Xconstructor.putField(ClassName.BaseActivation, "pvs", ClassName.ParameterValueSet);
// ?Xconstructor.endStatement();
/*
** Add a call to the execute() method to check
** for missing parameters
*/
MethodBuilder executeMethod = acb.getExecuteMethod();
executeMethod.pushThis();
executeMethod.callMethod(VMOpcode.INVOKEVIRTUAL, ClassName.BaseActivation, "throwIfMissingParms", "void", 0);
}
}
use of org.apache.derby.iapi.services.compiler.MethodBuilder in project derby by apache.
the class MatchingClauseNode method generate.
/**
* <p>
* Generate a method to invoke the INSERT/UPDATE/DELETE action. This method
* will be called at runtime by MatchingClauseConstantAction.executeConstantAction().
* </p>
*/
void generate(ActivationClassBuilder acb, ResultColumnList selectList, ResultSetNode generatedScan, HalfOuterJoinNode hojn, int clauseNumber) throws StandardException {
_clauseNumber = clauseNumber;
adjustMatchingRefinement(selectList, generatedScan);
generateInsertUpdateRow(acb, selectList, generatedScan, hojn);
_actionMethodName = "mergeActionMethod_" + _clauseNumber;
MethodBuilder mb = acb.getClassBuilder().newMethodBuilder(Modifier.PUBLIC, ClassName.ResultSet, _actionMethodName);
mb.addThrownException(ClassName.StandardException);
remapConstraints();
// now generate the action into this method
_dml.generate(acb, mb);
mb.methodReturn();
mb.complete();
}
use of org.apache.derby.iapi.services.compiler.MethodBuilder in project derby by apache.
the class ResultColumnList method generateCore.
/**
* Generate the code to place the columns' values into
* a row variable named "r". This wrapper is here
* rather than in ResultColumn, because that class does
* not know about the position of the columns in the list.
*
* This is the method that does the work.
*/
void generateCore(ExpressionClassBuilder acb, MethodBuilder mb, boolean genNulls) throws StandardException {
// generate the function and initializer:
// private ExecRow fieldX;
// In the constructor:
// fieldX = getExecutionFactory().getValueRow(# cols);
// private ExecRow exprN()
// {
// fieldX.setColumn(1, col(1).generateColumn(ps)));
// ... and so on for each column ...
// return fieldX;
// }
// static Method exprN = method pointer to exprN;
// this sets up the method and the static field.
MethodBuilder userExprFun = acb.newUserExprFun();
generateEvaluatedRow(acb, userExprFun, genNulls, false);
// what we return is the access of the field, i.e. the pointer to the method.
acb.pushMethodReference(mb, userExprFun);
}
use of org.apache.derby.iapi.services.compiler.MethodBuilder in project derby by apache.
the class FromVTI method generateConstructor.
private void generateConstructor(ActivationClassBuilder acb, MethodBuilder mb, boolean reuseablePs) throws StandardException {
String vtiType = version2 ? "java.sql.PreparedStatement" : "java.sql.ResultSet";
// this sets up the method and the static field.
// generates:
// java.sql.ResultSet userExprFun { }
MethodBuilder userExprFun = acb.newGeneratedFun(vtiType, Modifier.PUBLIC);
userExprFun.addThrownException("java.lang.Exception");
// If it's a re-useable PreparedStatement then hold onto it.
LocalField psHolder = reuseablePs ? acb.newFieldDeclaration(Modifier.PRIVATE, "java.sql.PreparedStatement") : null;
if (reuseablePs) {
userExprFun.getField(psHolder);
userExprFun.conditionalIfNull();
}
methodCall.generateExpression(acb, userExprFun);
userExprFun.upCast(vtiType);
if (reuseablePs) {
userExprFun.putField(psHolder);
userExprFun.startElseCode();
userExprFun.getField(psHolder);
userExprFun.completeConditional();
}
userExprFun.methodReturn();
// methodCall knows it is returning its value;
/* generates:
* return <newInvocation.generate(acb)>;
*/
// we are done modifying userExprFun, complete it.
userExprFun.complete();
// constructor is used in the final result set as an access of the new static
// field holding a reference to this new method.
// generates:
// ActivationClass.userExprFun
// which is the static field that "points" to the userExprFun
// that evaluates the where clause.
acb.pushMethodReference(mb, userExprFun);
// the activation is closed.
if (reuseablePs) {
MethodBuilder closeActivationMethod = acb.getCloseActivationMethod();
closeActivationMethod.getField(psHolder);
closeActivationMethod.conditionalIfNull();
// do nothing
// work around for no support for real if statements
closeActivationMethod.push(0);
closeActivationMethod.startElseCode();
closeActivationMethod.getField(psHolder);
closeActivationMethod.callMethod(VMOpcode.INVOKEINTERFACE, "java.sql.Statement", "close", "void", 0);
closeActivationMethod.push(0);
closeActivationMethod.completeConditional();
closeActivationMethod.endStatement();
}
}
Aggregations