Search in sources :

Example 1 with TypeCheckError

use of org.apache.xalan.xsltc.compiler.util.TypeCheckError in project servicemix-bundles by apache.

the class FunctionCall method typeCheckConstructor.

public Type typeCheckConstructor(SymbolTable stable) throws TypeCheckError {
    final Vector constructors = findConstructors();
    if (constructors == null) {
        // Constructor not found in this class
        throw new TypeCheckError(ErrorMsg.CONSTRUCTOR_NOT_FOUND, _className);
    }
    final int nConstructors = constructors.size();
    final int nArgs = _arguments.size();
    final Vector argsType = typeCheckArgs(stable);
    // Try all constructors
    int bestConstrDistance = Integer.MAX_VALUE;
    // reset
    _type = null;
    for (int j, i = 0; i < nConstructors; i++) {
        // Check if all parameters to this constructor can be converted
        final Constructor constructor = (Constructor) constructors.elementAt(i);
        final Class[] paramTypes = constructor.getParameterTypes();
        Class extType = null;
        int currConstrDistance = 0;
        for (j = 0; j < nArgs; j++) {
            // Convert from internal (translet) type to external (Java) type
            extType = paramTypes[j];
            final Type intType = (Type) argsType.elementAt(j);
            Object match = _internal2Java.maps(intType, extType);
            if (match != null) {
                currConstrDistance += ((JavaType) match).distance;
            } else if (intType instanceof ObjectType) {
                ObjectType objectType = (ObjectType) intType;
                if (objectType.getJavaClass() == extType)
                    continue;
                else if (extType.isAssignableFrom(objectType.getJavaClass()))
                    currConstrDistance += 1;
                else {
                    currConstrDistance = Integer.MAX_VALUE;
                    break;
                }
            } else {
                // no mapping available
                currConstrDistance = Integer.MAX_VALUE;
                break;
            }
        }
        if (j == nArgs && currConstrDistance < bestConstrDistance) {
            _chosenConstructor = constructor;
            _isExtConstructor = true;
            bestConstrDistance = currConstrDistance;
            _type = (_clazz != null) ? Type.newObjectType(_clazz) : Type.newObjectType(_className);
        }
    }
    if (_type != null) {
        return _type;
    }
    throw new TypeCheckError(ErrorMsg.ARGUMENT_CONVERSION_ERR, getMethodSignature(argsType));
}
Also used : ObjectType(org.apache.xalan.xsltc.compiler.util.ObjectType) BooleanType(org.apache.xalan.xsltc.compiler.util.BooleanType) Type(org.apache.xalan.xsltc.compiler.util.Type) ObjectType(org.apache.xalan.xsltc.compiler.util.ObjectType) IntType(org.apache.xalan.xsltc.compiler.util.IntType) MethodType(org.apache.xalan.xsltc.compiler.util.MethodType) ReferenceType(org.apache.xalan.xsltc.compiler.util.ReferenceType) Constructor(java.lang.reflect.Constructor) Vector(java.util.Vector) TypeCheckError(org.apache.xalan.xsltc.compiler.util.TypeCheckError)

Example 2 with TypeCheckError

use of org.apache.xalan.xsltc.compiler.util.TypeCheckError in project servicemix-bundles by apache.

the class FunctionCall method typeCheckExternal.

/**
 * Type check a call to an external (Java) method.
 * The method must be static an public, and a legal type conversion
 * must exist for all its arguments and its return type.
 * Every method of name <code>_fname</code> is inspected
 * as a possible candidate.
 */
public Type typeCheckExternal(SymbolTable stable) throws TypeCheckError {
    int nArgs = _arguments.size();
    final String name = _fname.getLocalPart();
    // check if function is a contructor 'new'
    if (_fname.getLocalPart().equals("new")) {
        return typeCheckConstructor(stable);
    } else // check if we are calling an instance method
    {
        boolean hasThisArgument = false;
        if (nArgs == 0)
            _isStatic = true;
        if (!_isStatic) {
            if (_namespace_format == NAMESPACE_FORMAT_JAVA || _namespace_format == NAMESPACE_FORMAT_PACKAGE)
                hasThisArgument = true;
            Expression firstArg = (Expression) _arguments.elementAt(0);
            Type firstArgType = (Type) firstArg.typeCheck(stable);
            if (_namespace_format == NAMESPACE_FORMAT_CLASS && firstArgType instanceof ObjectType && _clazz != null && _clazz.isAssignableFrom(((ObjectType) firstArgType).getJavaClass()))
                hasThisArgument = true;
            if (hasThisArgument) {
                _thisArgument = (Expression) _arguments.elementAt(0);
                _arguments.remove(0);
                nArgs--;
                if (firstArgType instanceof ObjectType) {
                    _className = ((ObjectType) firstArgType).getJavaClassName();
                } else
                    throw new TypeCheckError(ErrorMsg.NO_JAVA_FUNCT_THIS_REF, name);
            }
        } else if (_className.length() == 0) {
            /*
		 * Warn user if external function could not be resolved.
		 * Warning will _NOT_ be issued is the call is properly
		 * wrapped in an <xsl:if> or <xsl:when> element. For details
		 * see If.parserContents() and When.parserContents()
		 */
            final Parser parser = getParser();
            if (parser != null) {
                reportWarning(this, parser, ErrorMsg.FUNCTION_RESOLVE_ERR, _fname.toString());
            }
            unresolvedExternal = true;
            // use "Int" as "unknown"
            return _type = Type.Int;
        }
    }
    final Vector methods = findMethods();
    if (methods == null) {
        // Method not found in this class
        throw new TypeCheckError(ErrorMsg.METHOD_NOT_FOUND_ERR, _className + "." + name);
    }
    Class extType = null;
    final int nMethods = methods.size();
    final Vector argsType = typeCheckArgs(stable);
    // Try all methods to identify the best fit
    int bestMethodDistance = Integer.MAX_VALUE;
    // reset internal type
    _type = null;
    for (int j, i = 0; i < nMethods; i++) {
        // Check if all paramteters to this method can be converted
        final Method method = (Method) methods.elementAt(i);
        final Class[] paramTypes = method.getParameterTypes();
        int currMethodDistance = 0;
        for (j = 0; j < nArgs; j++) {
            // Convert from internal (translet) type to external (Java) type
            extType = paramTypes[j];
            final Type intType = (Type) argsType.elementAt(j);
            Object match = _internal2Java.maps(intType, extType);
            if (match != null) {
                currMethodDistance += ((JavaType) match).distance;
            } else {
                // the moment. The real type checking is performed at runtime.
                if (intType instanceof ReferenceType) {
                    currMethodDistance += 1;
                } else if (intType instanceof ObjectType) {
                    ObjectType object = (ObjectType) intType;
                    if (extType.getName().equals(object.getJavaClassName()))
                        currMethodDistance += 0;
                    else if (extType.isAssignableFrom(object.getJavaClass()))
                        currMethodDistance += 1;
                    else {
                        currMethodDistance = Integer.MAX_VALUE;
                        break;
                    }
                } else {
                    currMethodDistance = Integer.MAX_VALUE;
                    break;
                }
            }
        }
        if (j == nArgs) {
            // Check if the return type can be converted
            extType = method.getReturnType();
            _type = (Type) _java2Internal.get(extType);
            if (_type == null) {
                _type = Type.newObjectType(extType);
            }
            // Use this method if all parameters & return type match
            if (_type != null && currMethodDistance < bestMethodDistance) {
                _chosenMethod = method;
                bestMethodDistance = currMethodDistance;
            }
        }
    }
    // have a this argument.
    if (_chosenMethod != null && _thisArgument == null && !Modifier.isStatic(_chosenMethod.getModifiers())) {
        throw new TypeCheckError(ErrorMsg.NO_JAVA_FUNCT_THIS_REF, getMethodSignature(argsType));
    }
    if (_type != null) {
        if (_type == Type.NodeSet) {
            getXSLTC().setMultiDocument(true);
        }
        return _type;
    }
    throw new TypeCheckError(ErrorMsg.ARGUMENT_CONVERSION_ERR, getMethodSignature(argsType));
}
Also used : Method(java.lang.reflect.Method) ReferenceType(org.apache.xalan.xsltc.compiler.util.ReferenceType) ObjectType(org.apache.xalan.xsltc.compiler.util.ObjectType) BooleanType(org.apache.xalan.xsltc.compiler.util.BooleanType) Type(org.apache.xalan.xsltc.compiler.util.Type) ObjectType(org.apache.xalan.xsltc.compiler.util.ObjectType) IntType(org.apache.xalan.xsltc.compiler.util.IntType) MethodType(org.apache.xalan.xsltc.compiler.util.MethodType) ReferenceType(org.apache.xalan.xsltc.compiler.util.ReferenceType) Vector(java.util.Vector) TypeCheckError(org.apache.xalan.xsltc.compiler.util.TypeCheckError)

Example 3 with TypeCheckError

use of org.apache.xalan.xsltc.compiler.util.TypeCheckError in project servicemix-bundles by apache.

the class FunctionCall method typeCheckStandard.

/**
 * Type check a call to a standard function. Insert CastExprs when needed.
 * If as a result of the insertion of a CastExpr a type check error is
 * thrown, then catch it and re-throw it with a new "this".
 */
public Type typeCheckStandard(SymbolTable stable) throws TypeCheckError {
    // HACK!!!
    _fname.clearNamespace();
    final int n = _arguments.size();
    final Vector argsType = typeCheckArgs(stable);
    final MethodType args = new MethodType(Type.Void, argsType);
    final MethodType ptype = lookupPrimop(stable, _fname.getLocalPart(), args);
    if (ptype != null) {
        for (int i = 0; i < n; i++) {
            final Type argType = (Type) ptype.argsType().elementAt(i);
            final Expression exp = (Expression) _arguments.elementAt(i);
            if (!argType.identicalTo(exp.getType())) {
                try {
                    _arguments.setElementAt(new CastExpr(exp, argType), i);
                } catch (TypeCheckError e) {
                    // invalid conversion
                    throw new TypeCheckError(this);
                }
            }
        }
        _chosenMethodType = ptype;
        return _type = ptype.resultType();
    }
    throw new TypeCheckError(this);
}
Also used : MethodType(org.apache.xalan.xsltc.compiler.util.MethodType) BooleanType(org.apache.xalan.xsltc.compiler.util.BooleanType) Type(org.apache.xalan.xsltc.compiler.util.Type) ObjectType(org.apache.xalan.xsltc.compiler.util.ObjectType) IntType(org.apache.xalan.xsltc.compiler.util.IntType) MethodType(org.apache.xalan.xsltc.compiler.util.MethodType) ReferenceType(org.apache.xalan.xsltc.compiler.util.ReferenceType) Vector(java.util.Vector) TypeCheckError(org.apache.xalan.xsltc.compiler.util.TypeCheckError)

Example 4 with TypeCheckError

use of org.apache.xalan.xsltc.compiler.util.TypeCheckError in project servicemix-bundles by apache.

the class FunctionCall method typeCheck.

/**
 * Type check a function call. Since different type conversions apply,
 * type checking is different for standard and external (Java) functions.
 */
public Type typeCheck(SymbolTable stable) throws TypeCheckError {
    if (_type != null)
        return _type;
    final String namespace = _fname.getNamespace();
    String local = _fname.getLocalPart();
    if (isExtension()) {
        _fname = new QName(null, null, local);
        return typeCheckStandard(stable);
    } else if (isStandard()) {
        return typeCheckStandard(stable);
    } else // Handle extension functions (they all have a namespace)
    {
        try {
            _className = getClassNameFromUri(namespace);
            final int pos = local.lastIndexOf('.');
            if (pos > 0) {
                _isStatic = true;
                if (_className != null && _className.length() > 0) {
                    _namespace_format = NAMESPACE_FORMAT_PACKAGE;
                    _className = _className + "." + local.substring(0, pos);
                } else {
                    _namespace_format = NAMESPACE_FORMAT_JAVA;
                    _className = local.substring(0, pos);
                }
                _fname = new QName(namespace, null, local.substring(pos + 1));
            } else {
                if (_className != null && _className.length() > 0) {
                    try {
                        _clazz = ObjectFactory.findProviderClass(_className, ObjectFactory.findClassLoader(), true);
                        _namespace_format = NAMESPACE_FORMAT_CLASS;
                    } catch (ClassNotFoundException e) {
                        _namespace_format = NAMESPACE_FORMAT_PACKAGE;
                    }
                } else
                    _namespace_format = NAMESPACE_FORMAT_JAVA;
                if (local.indexOf('-') > 0) {
                    local = replaceDash(local);
                }
                String extFunction = (String) _extensionFunctionTable.get(namespace + ":" + local);
                if (extFunction != null) {
                    _fname = new QName(null, null, extFunction);
                    return typeCheckStandard(stable);
                } else
                    _fname = new QName(namespace, null, local);
            }
            return typeCheckExternal(stable);
        } catch (TypeCheckError e) {
            ErrorMsg errorMsg = e.getErrorMsg();
            if (errorMsg == null) {
                final String name = _fname.getLocalPart();
                errorMsg = new ErrorMsg(ErrorMsg.METHOD_NOT_FOUND_ERR, name);
            }
            getParser().reportError(ERROR, errorMsg);
            return _type = Type.Void;
        }
    }
}
Also used : ErrorMsg(org.apache.xalan.xsltc.compiler.util.ErrorMsg) TypeCheckError(org.apache.xalan.xsltc.compiler.util.TypeCheckError)

Aggregations

TypeCheckError (org.apache.xalan.xsltc.compiler.util.TypeCheckError)4 Vector (java.util.Vector)3 BooleanType (org.apache.xalan.xsltc.compiler.util.BooleanType)3 IntType (org.apache.xalan.xsltc.compiler.util.IntType)3 MethodType (org.apache.xalan.xsltc.compiler.util.MethodType)3 ObjectType (org.apache.xalan.xsltc.compiler.util.ObjectType)3 ReferenceType (org.apache.xalan.xsltc.compiler.util.ReferenceType)3 Type (org.apache.xalan.xsltc.compiler.util.Type)3 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 ErrorMsg (org.apache.xalan.xsltc.compiler.util.ErrorMsg)1