Search in sources :

Example 1 with ClassInspector

use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.

the class MethodCallNode method generateAndCastOneParameter.

/**
 * <p>
 * Generate and cast one parameter, pushing the result onto the stack.
 * </p>
 */
private void generateAndCastOneParameter(ExpressionClassBuilder acb, MethodBuilder mb, int param, String parameterType) throws StandardException {
    ClassInspector classInspector = getClassFactory().getClassInspector();
    generateOneParameter(acb, mb, param);
    // type from the SQL-J expression
    String argumentType = getParameterTypeName(methodParms[param]);
    if (!parameterType.equals(argumentType)) {
        // 
        if (ClassInspector.primitiveType(argumentType) && parameterType.equals(JSQLType.getWrapperClassName(JSQLType.getPrimitiveID(argumentType)))) {
            // short must be converted to int
            if ("short".equals(argumentType)) {
                mb.cast("int");
            }
            mb.callMethod(VMOpcode.INVOKESTATIC, parameterType, "valueOf", parameterType, 1);
        } else // to the parameter type.
        if (ClassInspector.primitiveType(parameterType)) {
            mb.cast(parameterType);
        } else {
            // for a procedure
            if (routineInfo != null) {
                // probably should be only for INOUT/OUT parameters.
                return;
            }
            if (SanityManager.DEBUG) {
                SanityManager.ASSERT(classInspector.assignableTo(argumentType, parameterType), "Argument type " + argumentType + " is not assignable to parameter " + parameterType);
            }
            /*
                ** Set the parameter type in case the argument type is narrower
                ** than the parameter type.
                */
            mb.upCast(parameterType);
        }
    }
}
Also used : ClassInspector(org.apache.derby.iapi.services.loader.ClassInspector)

Example 2 with ClassInspector

use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.

the class MethodCallNode method getMethodParameterClasses.

/**
 *	Get the resolved Classes of our parameters
 *
 *	@return	the Classes of our parameters
 */
Class<?>[] getMethodParameterClasses() {
    ClassInspector ci = getClassFactory().getClassInspector();
    Class<?>[] parmTypeClasses = new Class<?>[methodParms.length];
    for (int i = 0; i < methodParms.length; i++) {
        String className = methodParameterTypes[i];
        try {
            parmTypeClasses[i] = ci.getClass(className);
        } catch (ClassNotFoundException cnfe) {
            /* We should never get this exception since we verified 
				 * that the classes existed at bind time.  Just return null.
				 */
            if (SanityManager.DEBUG) {
                SanityManager.THROWASSERT("Unexpected exception", cnfe);
            }
            return null;
        }
    }
    return parmTypeClasses;
}
Also used : ClassInspector(org.apache.derby.iapi.services.loader.ClassInspector)

Example 3 with ClassInspector

use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.

the class NewInvocationNode 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 {
    bindParameters(fromList, subqueryList, aggregates);
    verifyClassExist(javaClassName);
    /*
		** Get the parameter type names out of the parameters and put them
		** in an array.
		*/
    String[] parmTypeNames = getObjectSignature();
    boolean[] isParam = getIsParam();
    ClassInspector classInspector = getClassFactory().getClassInspector();
    /*
		** Find the matching constructor.
		*/
    try {
        /* First try with built-in types and mappings */
        method = classInspector.findPublicConstructor(javaClassName, parmTypeNames, null, isParam);
        /* If no match, then retry to match any possible combinations of
			 * object and primitive types.
			 */
        if (method == null) {
            String[] primParmTypeNames = getPrimitiveSignature(false);
            method = classInspector.findPublicConstructor(javaClassName, parmTypeNames, primParmTypeNames, isParam);
        }
    } catch (ClassNotFoundException e) {
        /*
			** If one of the classes couldn't be found, just act like the
			** method couldn't be found.  The error lists all the class names,
			** which should give the user enough info to diagnose the problem.
			*/
        method = null;
    }
    if (method == null) {
        /* Put the parameter type names into a single string */
        String parmTypes = "";
        for (int i = 0; i < parmTypeNames.length; i++) {
            if (i != 0)
                parmTypes += ", ";
            parmTypes += (parmTypeNames[i].length() != 0 ? parmTypeNames[i] : MessageService.getTextMessage(SQLState.LANG_UNTYPED));
        }
        throw StandardException.newException(SQLState.LANG_NO_CONSTRUCTOR_FOUND, javaClassName, parmTypes);
    }
    methodParameterTypes = classInspector.getParameterTypes(method);
    for (int i = 0; i < methodParameterTypes.length; i++) {
        if (ClassInspector.primitiveType(methodParameterTypes[i]))
            methodParms[i].castToPrimitive(true);
    }
    /* Set type info for any null parameters */
    if (someParametersAreNull()) {
        setNullParameterInfo(methodParameterTypes);
    }
    /* Constructor always returns an object of type javaClassName */
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(javaClassName.equals(classInspector.getType(method)), "Constructor is wrong type, expected " + javaClassName + " actual is " + classInspector.getType(method));
    }
    setJavaTypeName(javaClassName);
    if (routineInfo != null) {
        TypeDescriptor returnType = routineInfo.getReturnType();
        if (returnType != null) {
            setCollationType(returnType.getCollationType());
        }
    }
    return this;
}
Also used : TypeDescriptor(org.apache.derby.catalog.TypeDescriptor) ClassInspector(org.apache.derby.iapi.services.loader.ClassInspector)

Example 4 with ClassInspector

use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.

the class NewInvocationNode method findPublicMethod.

/**
 * Is this class have a public method with the specified signiture
 * This is useful for the VTI interface where we want to see
 * if the class has the option static method for returning the
 * ResultSetMetaData.
 *
 * @param methodName	The method name we are looking for
 * @param staticMethod	Whether or not the method we are looking for is static
 *
 * @return Member		The Member representing the method (or null
 *						if the method doesn't exist).
 *
 * @exception StandardException		Thrown on error
 */
protected Member findPublicMethod(String methodName, boolean staticMethod) throws StandardException {
    Member publicMethod;
    /*
		** Get the parameter type names out of the parameters and put them
		** in an array.
		*/
    String[] parmTypeNames = getObjectSignature();
    boolean[] isParam = getIsParam();
    ClassInspector classInspector = getClassFactory().getClassInspector();
    try {
        publicMethod = classInspector.findPublicMethod(javaClassName, methodName, parmTypeNames, null, isParam, staticMethod, false, hasVarargs());
        /* If no match, then retry to match any possible combinations of
			 * object and primitive types.
			 */
        if (publicMethod == null) {
            String[] primParmTypeNames = getPrimitiveSignature(false);
            publicMethod = classInspector.findPublicMethod(javaClassName, methodName, parmTypeNames, primParmTypeNames, isParam, staticMethod, false, hasVarargs());
        }
    } catch (ClassNotFoundException e) {
        /* We should always be able to find the class at this point
			 * since the protocol is to check to see if it exists
			 * before checking for a method off of it.  Anyway, just return
			 * null if the class doesn't exist, since the method doesn't
			 * exist in that case.
			 */
        if (SanityManager.DEBUG) {
            SanityManager.THROWASSERT("Unexpected exception", e);
        }
        return null;
    }
    return publicMethod;
}
Also used : ClassInspector(org.apache.derby.iapi.services.loader.ClassInspector) Member(java.lang.reflect.Member)

Example 5 with ClassInspector

use of org.apache.derby.iapi.services.loader.ClassInspector in project derby by apache.

the class FromVTI method getNewInstance.

private Object getNewInstance() throws StandardException {
    NewInvocationNode constructor = (NewInvocationNode) methodCall;
    Class<?>[] paramTypeClasses = constructor.getMethodParameterClasses();
    Object[] paramObjects;
    if (paramTypeClasses != null) {
        paramObjects = new Object[paramTypeClasses.length];
        for (int index = 0; index < paramTypeClasses.length; index++) {
            Class<?> paramClass = paramTypeClasses[index];
            paramObjects[index] = methodParms[index].getConstantValueAsObject();
            // Short or Byte object.
            if ((paramObjects[index] != null) && paramClass.isPrimitive()) {
                if (paramClass.equals(Short.TYPE)) {
                    paramObjects[index] = Short.valueOf(((Integer) paramObjects[index]).shortValue());
                } else if (paramClass.equals(Byte.TYPE)) {
                    paramObjects[index] = Byte.valueOf(((Integer) paramObjects[index]).byteValue());
                }
            }
            // Pass defaults for unknown primitive values
            if (paramObjects[index] == null && paramClass.isPrimitive()) {
                if (paramClass.equals(Integer.TYPE)) {
                    paramObjects[index] = Integer.valueOf(0);
                } else if (paramClass.equals(Short.TYPE)) {
                    paramObjects[index] = Short.valueOf((short) 0);
                } else if (paramClass.equals(Byte.TYPE)) {
                    paramObjects[index] = Byte.valueOf((byte) 0);
                } else if (paramClass.equals(Long.TYPE)) {
                    paramObjects[index] = Long.valueOf((long) 0);
                } else if (paramClass.equals(Float.TYPE)) {
                    paramObjects[index] = (float) 0;
                } else if (paramClass.equals(Double.TYPE)) {
                    paramObjects[index] = (double) 0;
                } else if (paramClass.equals(Boolean.TYPE)) {
                    paramObjects[index] = Boolean.FALSE;
                } else if (paramClass.equals(Character.TYPE)) {
                    paramObjects[index] = Character.valueOf(Character.MIN_VALUE);
                }
            }
        }
    } else {
        paramTypeClasses = new Class<?>[0];
        paramObjects = new Object[0];
    }
    try {
        ClassInspector classInspector = getClassFactory().getClassInspector();
        String javaClassName = methodCall.getJavaClassName();
        Constructor<?> constr = classInspector.getClass(javaClassName).getConstructor(paramTypeClasses);
        return constr.newInstance(paramObjects);
    } catch (Throwable t) {
        throw StandardException.unexpectedUserException(t);
    }
}
Also used : ClassInspector(org.apache.derby.iapi.services.loader.ClassInspector)

Aggregations

ClassInspector (org.apache.derby.iapi.services.loader.ClassInspector)12 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)2 TypeId (org.apache.derby.iapi.types.TypeId)2 Member (java.lang.reflect.Member)1 Method (java.lang.reflect.Method)1 ResultSet (java.sql.ResultSet)1 TypeDescriptor (org.apache.derby.catalog.TypeDescriptor)1 TypeDescriptorImpl (org.apache.derby.catalog.types.TypeDescriptorImpl)1 UserDefinedTypeIdImpl (org.apache.derby.catalog.types.UserDefinedTypeIdImpl)1 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)1 TypeCompiler (org.apache.derby.iapi.sql.compile.TypeCompiler)1 LuceneIndexDescriptor (org.apache.derby.optional.api.LuceneIndexDescriptor)1