use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.
the class ConditionalNode method bindExpression.
/**
* Bind this expression. This means binding the sub-expressions,
* as well as figuring out what the return type is for this expression.
*
* @param fromList The FROM list for the query this
* expression is in, for binding columns.
* @param subqueryList The subquery list being built as we find SubqueryNodes
* @param aggregates The aggregate list being built as we find AggregateNodes
*
* @return The new top of the expression tree.
*
* @exception StandardException Thrown on error
*/
@Override
ValueNode bindExpression(FromList fromList, SubqueryList subqueryList, List<AggregateNode> aggregates) throws StandardException {
CompilerContext cc = getCompilerContext();
int previousReliability = orReliability(CompilerContext.CONDITIONAL_RESTRICTION);
ValueNodeList caseOperandParameters = bindCaseOperand(cc, fromList, subqueryList, aggregates);
testConditions.bindExpression(fromList, subqueryList, aggregates);
// parameter), find out which type best describes it.
if (caseOperandParameters != null) {
// when testConditions was bound.
for (ValueNode vn : caseOperandParameters) {
// Check that this parameter is comparable to all the other
// parameters in the list. This indirectly checks whether
// all when operands have compatible types.
caseOperandParameters.comparable(vn);
// Replace the dummy parameter node with the actual case
// operand.
testConditions.accept(new ReplaceNodeVisitor(vn, caseOperand));
}
// Finally, after we have determined that all the when operands
// are compatible, and we have reinserted the case operand into
// the tree, set the type of the case operand to the dominant
// type of all the when operands.
caseOperand.setType(caseOperandParameters.getDominantTypeServices());
}
thenElseList.bindExpression(fromList, subqueryList, aggregates);
// Find the type of the first typed value in thenElseList and cast
// all untyped NULL values to that type. We don't need to find the
// dominant type here, since a top-level cast to that type will be
// added later, if necessary.
DataTypeDescriptor nullType = thenElseList.getTypeServices();
if (nullType == null) {
// an error.
throw StandardException.newException(SQLState.LANG_ALL_RESULT_EXPRESSIONS_UNTYPED);
} else {
recastNullNodes(nullType, fromList, subqueryList, aggregates);
}
// Set the result type of this conditional to be the dominant type
// of the result expressions.
setType(thenElseList.getDominantTypeServices());
/* testCondition must be a boolean expression.
* If it is a ? parameter on the left, then set type to boolean,
* otherwise verify that the result type is boolean.
*/
testConditions.setParameterDescriptor(new DataTypeDescriptor(TypeId.BOOLEAN_ID, true));
for (ValueNode testCondition : testConditions) {
if (!testCondition.getTypeServices().getTypeId().equals(TypeId.BOOLEAN_ID)) {
throw StandardException.newException(SQLState.LANG_CONDITIONAL_NON_BOOLEAN);
}
}
// Set the type of the parameters.
thenElseList.setParameterDescriptor(getTypeServices());
/* The then and else expressions must be type compatible */
ClassInspector cu = getClassFactory().getClassInspector();
/*
** If it is comparable, then we are ok. Note that we
** could in fact allow any expressions that are convertible()
** since we are going to generate a cast node, but that might
** be confusing to users...
*/
for (ValueNode expr : thenElseList) {
DataTypeDescriptor dtd = expr.getTypeServices();
String javaTypeName = dtd.getTypeId().getCorrespondingJavaTypeName();
String resultJavaTypeName = getTypeId().getCorrespondingJavaTypeName();
if (!dtd.comparable(getTypeServices(), false, getClassFactory()) && !cu.assignableTo(javaTypeName, resultJavaTypeName) && !cu.assignableTo(resultJavaTypeName, javaTypeName)) {
throw StandardException.newException(SQLState.LANG_NOT_TYPE_COMPATIBLE, dtd.getTypeId().getSQLTypeName(), getTypeId().getSQLTypeName());
}
}
// The result is nullable if and only if at least one of the result
// expressions is nullable (DERBY-6567).
setNullability(thenElseList.isNullable());
/*
** Generate a CastNode if necessary and
** stick it over the original expression
*/
TypeId condTypeId = getTypeId();
for (int i = 0; i < thenElseList.size(); i++) {
ValueNode expr = thenElseList.elementAt(i);
if (expr.getTypeId().typePrecedence() != condTypeId.typePrecedence()) {
// Cast to dominant type.
ValueNode cast = new CastNode(expr, getTypeServices(), getContextManager());
cast = cast.bindExpression(fromList, subqueryList, aggregates);
thenElseList.setElementAt(cast, i);
}
}
cc.setReliability(previousReliability);
return this;
}
use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.
the class LuceneSupport method getIndexDescriptorNoPrivs.
/**
* Invoke a static method (possibly supplied by the user) to instantiate an index descriptor.
* The method has no arguments.
*/
static LuceneIndexDescriptor getIndexDescriptorNoPrivs(String indexDescriptorMaker) throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException {
int lastDotIdx = indexDescriptorMaker.lastIndexOf(".");
String className = indexDescriptorMaker.substring(0, lastDotIdx);
ClassInspector ci = getClassFactory().getClassInspector();
Class<? extends Object> klass = ci.getClass(className);
String methodName = indexDescriptorMaker.substring(lastDotIdx + 1, indexDescriptorMaker.length());
Method method = klass.getDeclaredMethod(methodName);
return (LuceneIndexDescriptor) method.invoke(null);
}
use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.
the class QueryTreeNode method verifyClassExist.
/**
* Verify that a java class exists, is accessible (public)
* and not a class representing a primitive type.
* @param javaClassName The name of the java class to resolve.
*
* @exception StandardException Thrown on error
*/
void verifyClassExist(String javaClassName) throws StandardException {
ClassInspector classInspector = getClassFactory().getClassInspector();
Throwable reason = null;
boolean foundMatch = false;
try {
foundMatch = classInspector.accessible(javaClassName);
} catch (ClassNotFoundException cnfe) {
reason = cnfe;
}
if (!foundMatch)
throw StandardException.newException(SQLState.LANG_TYPE_DOESNT_EXIST2, reason, javaClassName);
if (ClassInspector.primitiveType(javaClassName))
throw StandardException.newException(SQLState.LANG_TYPE_DOESNT_EXIST3, javaClassName);
}
use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.
the class StaticClassFieldReferenceNode method bindExpression.
/**
* Bind this expression. This means binding the sub-expressions,
* as well as figuring out what the return type is for this expression.
*
* @param fromList The FROM list for the query this
* expression is in, for binding columns.
* @param subqueryList The subquery list being built as we find SubqueryNodes
* @param aggregates The aggregate list being built as we find AggregateNodes
*
* @return Nothing
*
* @exception StandardException Thrown on error
*/
JavaValueNode bindExpression(FromList fromList, SubqueryList subqueryList, List<AggregateNode> aggregates) throws StandardException {
ClassInspector classInspector = getClassFactory().getClassInspector();
if (((getCompilerContext().getReliability() & CompilerContext.INTERNAL_SQL_ILLEGAL) != 0) || !javaClassName.startsWith("java.sql.")) {
throw StandardException.newException(SQLState.LANG_SYNTAX_ERROR, javaClassName + "::" + fieldName);
}
verifyClassExist(javaClassName);
/*
** Find the field that is public.
*/
field = classInspector.findPublicField(javaClassName, fieldName, true);
/* Get the field type */
setJavaTypeName(classInspector.getType(field));
return this;
}
use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.
the class ColumnDefinitionNode method checkUserType.
/**
* Check the validity of a user type. Checks whether this column
* definition describes a user type that either doesn't exist or is
* inaccessible, or that doesn't implement Serializable.
*
* @exception StandardException Thrown on error
*/
void checkUserType(TableDescriptor td) throws StandardException {
String columnTypeName;
// omitted. we can't check generation clauses until later on
if (hasGenerationClause() && (getType() == null)) {
return;
}
/* Built-in types need no checking */
if (!getType().getTypeId().userType())
return;
// bind the UDT if necessary
setType(bindUserType(getType()));
ClassInspector classInspector = getClassFactory().getClassInspector();
columnTypeName = getType().getTypeId().getCorrespondingJavaTypeName();
/* User type - We first check for the columnTypeName as a java class.
* If that fails, then we treat it as a class alias.
*/
boolean foundMatch = false;
Throwable reason = null;
try {
foundMatch = classInspector.accessible(columnTypeName);
} catch (ClassNotFoundException cnfe) {
reason = cnfe;
}
if (!foundMatch) {
throw StandardException.newException(SQLState.LANG_TYPE_DOESNT_EXIST, reason, columnTypeName, name);
}
if (!classInspector.assignableTo(columnTypeName, "java.io.Serializable") && // Before Java2, SQLData is not defined, assignableTo call returns false
!classInspector.assignableTo(columnTypeName, "java.sql.SQLData")) {
getCompilerContext().addWarning(StandardException.newWarning(SQLState.LANG_TYPE_NOT_SERIALIZABLE, columnTypeName, name));
}
}
Aggregations