Package org.jboss.byteman.rule.type

Examples of org.jboss.byteman.rule.type.TypeGroup


     *
     */
    public Type typeCheck(Type expected) throws TypeException {
        // check the exception type is defined and then look for a relevant constructor

        TypeGroup typeGroup = getTypeGroup();

        type = Type.dereference(typeGroup.create(typeName));

        if (type == null || type.isUndefined()) {
            throw new TypeException("ThrowExpression.typeCheck : unknown exception type " + typeName + getPos());
        }

        if (!Throwable.class.isAssignableFrom(type.getTargetClass())) {
            throw new TypeException("ThrowExpression.typeCheck : not an exception type " + typeName  + getPos());
        }

        Class clazz = type.getTargetClass();
        // if we can find a unique method then we can use it to type the parameters
        // otherwise we do it the hard way
        int arity = arguments.size();
        Constructor[] constructors = clazz.getConstructors();
        List<Constructor> candidates = new ArrayList<Constructor>();
        boolean duplicates = false;

        for (Constructor constructor : constructors) {
            if (constructor.getParameterTypes().length == arity) {
                candidates.add(constructor);
            }
        }

        argumentTypes = new ArrayList<Type>();

        // check each argument in turn -- if all candidates have the same argument type then
        // use that as the type to check against
        for (int i = 0; i < arguments.size() ; i++) {
            if (candidates.isEmpty()) {
                throw new TypeException("ThrowExpression.typeCheck : invalid constructor for target class " + typeName + getPos());
            }

            // TODO get and prune operations do not allow for coercion but type check does!
            // e.g. the parameter type may be int and the arg type float
            // or the parameter type may be String and the arg type class Foo
            // reimplement this using type inter-assignability to do the pruning

            Class candidateClass = getCandidateArgClass(candidates, i);
            Type candidateType;
            if (candidateClass != null) {
                candidateType = typeGroup.ensureType(candidateClass);
            } else {
                candidateType = Type.UNDEFINED;
            }
            Type argType = arguments.get(i).typeCheck(candidateType);
            argumentTypes.add(argType);
            if (candidateType == Type.UNDEFINED) {
                // we had several constructors to choose from
                candidates = pruneCandidates(candidates, i, argType.getTargetClass());
            }
        }

        if (candidates.isEmpty()) {
            throw new TypeException("ThrowExpression.typeCheck : invalid constructor for target class " + typeName + getPos());
        }

        if (candidates.size() > 1) {
            throw new TypeException("ThrowExpression.typeCheck : ambiguous constructor signature for target class " + typeName + getPos());
        }

        constructor = candidates.get(0);

        // make sure we know the formal parameter types and have included them in the typegroup

        paramTypes = new ArrayList<Type>();
        Class<?>[] paramClasses = constructor.getParameterTypes();

        for (int i = 0; i < arguments.size() ; i++) {
            paramTypes.add(typeGroup.ensureType(paramClasses[i]));
        }

        checkThrownTypeIsValid();

        return type;
View Full Code Here


     * @throws TypeException if it is otherwise not legitimate to throw a value of the computed type from the trigger
     * method
     */
    private void checkThrownTypeIsValid() throws TypeWarningException, TypeException
    {
        TypeGroup typeGroup = getTypeGroup();

        // if the thrown type can be assigned to RuntimeException then we are out of here

        if (RuntimeException.class.isAssignableFrom(type.getTargetClass())) {
            return;
        }

        // see if the trigering method declares this exception type as a thrown exception

        Iterator<Type> iterator = typeGroup.getExceptionTypes().iterator();
        while (iterator.hasNext()) {
            Type exceptionType = iterator.next();
            if (Type.dereference(exceptionType).isAssignableFrom(type)) {
                // ok we found a suitable declaration for the exception
                return;
View Full Code Here

    {
        List<String> paramTypes = Type.parseMethodDescriptor(candidateDesc, false);
        int paramCount = paramTypes.size();
        int errorCount = 0;

        TypeGroup typegroup = rule.getTypeGroup();

        Bindings bindings = rule.getBindings();
        Iterator<Binding> iterator = bindings.iterator();

        while (iterator.hasNext()) {
View Full Code Here

    public Type typeCheck(Type expected) throws TypeException {
        // if we have no recipient then we use the rule's helper as a target via a binding
        // to $-1. this means  we can type check the call against methods of class Helper
        // without having to do any special case processing.

        TypeGroup typeGroup =  getTypeGroup();

        if (recipient == null && pathList != null) {
            // treat the pathlist as a typename or a static field dereference possibly combined with
            // further field dereferences

            // factor off a typename from the path
            Type rootType = typeGroup.match(pathList);
            if (rootType == null) {
                throw new TypeException("FieldExpression.typeCheck : invalid path " + getPath(pathList.length) + " to static method " + name + getPos());
            }

            // find out how many of the path elements are included in the type name

            String rootTypeName = rootType.getName();

            int idx = getPathCount(rootTypeName);

            if (idx < pathList.length) {
                // create a static field reference using the type name and the first field name and wrap it with
                // enough field references to use up all the path

                String fieldName = pathList[idx++];
                Expression recipient = new StaticExpression(rule, Type.UNDEFINED, token, fieldName, rootTypeName);
                while (idx < pathList.length) {
                    recipient = new FieldExpression(rule, Type.UNDEFINED, token, pathList[idx++], recipient, null);
                }
                this.recipient = recipient;
            } else {
                // ok, this method reference is actually a static method call -- record the root type for later
                this.recipient = null;
                this.rootType = rootType;
            }
            // get rid of the path list now
            this.pathList = null;
            // not strictly necessary?
            if (this.recipient != null) {
                this.recipient.bind();
            }
        }

        // if we don't have a recipient and we didn't find a static class for the method then this is
        // a builtin

        boolean isBuiltIn = false;

        if (recipient == null) {
            if (rootType == null) {
                isBuiltIn = true;
                Type ruleType = typeGroup.create(rule.getHelperClass().getCanonicalName());
                recipient = new DollarExpression(rule, ruleType, token, DollarExpression.HELPER_IDX);
                recipient.bind();

                rootType = recipient.typeCheck(Type.UNDEFINED);
            }
        } else {
            rootType = recipient.typeCheck(Type.UNDEFINED);
        }

        // see if we can find a method for this call
       
        findMethod(isBuiltIn);

        // now go back and identify the parameter types

        this. paramTypes = new ArrayList<Type>();
        Class<?>[] paramClasses = method.getParameterTypes();

        for (int i = 0; i < arguments.size(); i++) {
            Class<?> paramClass = paramClasses[i];
            paramTypes.add(typeGroup.ensureType(paramClass));
        }

        type = typeGroup.ensureType(method.getReturnType());

        if (Type.dereference(expected).isDefined() && !expected.isAssignableFrom(type)) {
            throw new TypeException("MethodExpression.typeCheck : invalid expected type " + expected.getName() + getPos());
        }
View Full Code Here

    private void findMethod(boolean publicOnly) throws TypeException
    {
        // check all declared methods of each class in the class hierarchy using the one with
        // the most specific recipient type if we can find it

        TypeGroup typeGroup =  getTypeGroup();
        Class<?> clazz = rootType.getTargetClass();
        boolean isStatic = (recipient == null);

        int arity = arguments.size();
        while (clazz != null) {
            List<Method> candidates = new ArrayList<Method>();
            Class<?> superClazz = clazz.getSuperclass();
            try {
                Method[] methods;
                if (publicOnly) {
                    methods = clazz.getMethods();
                } else {
                    methods = clazz.getDeclaredMethods();
                }
                List<Type> argumentTypes = new ArrayList<Type>();

                for (Method method : methods) {
                    int modifiers = method.getModifiers();
                    // ensure we only look at static or non static methods as appropriate
                    if (Modifier.isStatic(modifiers) == isStatic) {
                        if (method.getName().equals(name) &&
                                method.getParameterTypes().length == arity) {
                            candidates.add(method);
                        }
                    }
                }
                // check each argument in turn -- if all candidates have the same argument type then
                // use that as the type to check against
                for (int i = 0; i < arguments.size() ; i++) {
                    if (candidates.isEmpty()) {
                        // no more possible matches
                        break;
                    }
                    Class candidateClass = getCandidateArgClass(candidates, i);
                    Type candidateType;
                    if (candidateClass != null) {
                        candidateType = typeGroup.ensureType(candidateClass);
                    } else {
                        candidateType = Type.UNDEFINED;
                    }
                    Type argType = arguments.get(i).typeCheck(candidateType);
                    argumentTypes.add(argType);
View Full Code Here

    }

    public Type typeCheck(Type expected) throws TypeException {
        if (owner == null && pathList != null) {
            // factor off a typename from the path
            TypeGroup typeGroup = getTypeGroup();
            Type rootType = typeGroup.match(pathList);
            if (rootType == null) {
                throw new TypeException("FieldExpression.typeCheck : invalid path " + getPath(pathList.length) + " to static field " + fieldName + getPos());
            }

            // find out how many of the path elements are included in the type name
View Full Code Here

     *
     */
    public Type typeCheck(Type expected) throws TypeException {
        // check the exception type is defined and then look for a relevant constructor

        TypeGroup typeGroup = getTypeGroup();

        type = Type.dereference(typeGroup.create(typeName));

        if (type == null || type.isUndefined()) {
            throw new TypeException("ThrowExpression.typeCheck : unknown exception type " + typeName + getPos());
        }

        if (!Throwable.class.isAssignableFrom(type.getTargetClass())) {
            throw new TypeException("ThrowExpression.typeCheck : not an exception type " + typeName  + getPos());
        }

        Class clazz = type.getTargetClass();
        // if we can find a unique method then we can use it to type the parameters
        // otherwise we do it the hard way
        int arity = arguments.size();
        Constructor[] constructors = clazz.getConstructors();
        List<Constructor> candidates = new ArrayList<Constructor>();
        boolean duplicates = false;

        for (Constructor constructor : constructors) {
            if (constructor.getParameterTypes().length == arity) {
                candidates.add(constructor);
            }
        }

        argumentTypes = new ArrayList<Type>();

        // check each argument in turn -- if all candidates have the same argument type then
        // use that as the type to check against
        for (int i = 0; i < arguments.size() ; i++) {
            if (candidates.isEmpty()) {
                throw new TypeException("ThrowExpression.typeCheck : invalid constructor for target class " + typeName + getPos());
            }

            // TODO get and prune operations do not allow for coercion but type check does!
            // e.g. the parameter type may be int and the arg type float
            // or the parameter type may be String and the arg type class Foo
            // reimplement this using type inter-assignability to do the pruning

            Class candidateClass = getCandidateArgClass(candidates, i);
            Type candidateType;
            if (candidateClass != null) {
                candidateType = typeGroup.ensureType(candidateClass);
            } else {
                candidateType = Type.UNDEFINED;
            }
            Type argType = arguments.get(i).typeCheck(candidateType);
            argumentTypes.add(argType);
            if (candidateType == Type.UNDEFINED) {
                // we had several constructors to choose from
                candidates = pruneCandidates(candidates, i, argType.getTargetClass());
            }
        }

        if (candidates.isEmpty()) {
            throw new TypeException("ThrowExpression.typeCheck : invalid constructor for target class " + typeName + getPos());
        }

        if (candidates.size() > 1) {
            throw new TypeException("ThrowExpression.typeCheck : ambiguous constructor signature for target class " + typeName + getPos());
        }

        constructor = candidates.get(0);

        // make sure we know the formal parameter types and have included them in the typegroup

        paramTypes = new ArrayList<Type>();
        Class<?>[] paramClasses = constructor.getParameterTypes();

        for (int i = 0; i < arguments.size() ; i++) {
            paramTypes.add(typeGroup.ensureType(paramClasses[i]));
        }

        // expected type should always be void since throw can only occur as a top level action
        // however, we need to be sure that the trigering method throws this exception type or
        // else that it is a subtype of runtime exception

        if (RuntimeException.class.isAssignableFrom(type.getTargetClass())) {
            return type;
        } else {
            Iterator<Type> iterator = typeGroup.getExceptionTypes().iterator();
            while (iterator.hasNext()) {
                Type exceptionType = iterator.next();
                if (Type.dereference(exceptionType).isAssignableFrom(type)) {
                    // ok we foudn a suitable declaration for the exception
                    return type;
View Full Code Here

        bind();
    }

    public Type typeCheck(Type expected) throws TypeException {
        // look for a class whose name matches some initial segment of pathList
        TypeGroup typeGroup = getTypeGroup();
        ownerType = Type.dereference(typeGroup.create(ownerTypeName));
        if (ownerType.isUndefined()) {
            throw new TypeException("StaticExpression.typeCheck : invalid path " + ownerTypeName + " to static field " + fieldName + getPos());
        }

        Class clazz = ownerType.getTargetClass();
        try {
            field  = lookupField(clazz);
        } catch (NoSuchFieldException e) {
                // oops
            throw new TypeException("StaticExpression.typeCheck : invalid field name " + fieldName + getPos());
        }

        if ((field.getModifiers() & Modifier.STATIC)== 0) {
            // oops
            throw new TypeException("StaticExpression.typeCheck : field is not static " + fieldName + getPos());
        }

        clazz = field.getType();
        type = typeGroup.ensureType(clazz);

        if (Type.dereference(expected).isDefined() && !expected.isAssignableFrom(type)) {
            throw new TypeException("StaticExpression.typeCheck : invalid expected return type " + expected.getName() + getPos());
        }
        return type;
View Full Code Here

     *
     */
    public Type typeCheck(Type expected) throws TypeException {
        // check the new instance type is defined and then look for a relevant constructor

        TypeGroup typeGroup = getTypeGroup();

        type = Type.dereference(typeGroup.create(typeName));

        if (type == null || type.isUndefined()) {
            throw new TypeException("NewExpression.typeCheck : unknown type " + typeName + getPos());
        }

        if (type.isObject() && arrayDimCount == 0) {
            // we need to look for a suitable constructor
            Class clazz = type.getTargetClass();
            // if we can find a unique method then we can use it to type the parameters
            // otherwise we do it the hard way
            int arity = arguments.size();
            Constructor[] constructors = clazz.getConstructors();
            List<Constructor> candidates = new ArrayList<Constructor>();
            boolean duplicates = false;

            for (Constructor constructor : constructors) {
                if (constructor.getParameterTypes().length == arity) {
                    candidates.add(constructor);
                }
            }

            argumentTypes = new ArrayList<Type>();

            // check each argument in turn -- if all candidates have the same argument type then
            // use that as the type to check against
            for (int i = 0; i < arguments.size() ; i++) {
                if (candidates.isEmpty()) {
                    throw new TypeException("NewExpression.typeCheck : invalid constructor for target class " + typeName + getPos());
                }

                // TODO get and prune operations do not allow for coercion but type check does!
                // e.g. the parameter type may be int and the arg type float
                // or the parameter type may be String and the arg type class Foo
                // reimplement this using type inter-assignability to do the pruning

                Class candidateClass = getCandidateArgClass(candidates, i);
                Type candidateType;
                if (candidateClass != null) {
                    candidateType = typeGroup.ensureType(candidateClass);
                } else {
                    candidateType = Type.UNDEFINED;
                }
                Type argType = arguments.get(i).typeCheck(candidateType);
                argumentTypes.add(argType);
                if (candidateType == Type.UNDEFINED) {
                    // we had several constructors to choose from
                    candidates = pruneCandidates(candidates, i, argType.getTargetClass());
                }
            }

            if (candidates.isEmpty()) {
                throw new TypeException("NewExpression.typeCheck : invalid constructor for target class " + typeName + getPos());
            }

            if (candidates.size() > 1) {
                throw new TypeException("NewExpression.typeCheck : ambiguous constructor signature for target class " + typeName + getPos());
            }

            constructor = candidates.get(0);

            // make sure we know the formal parameter types and have included them in the typegroup

            paramTypes = new ArrayList<Type>();
            Class<?>[] paramClasses = constructor.getParameterTypes();

            for (int i = 0; i < arguments.size() ; i++) {
                paramTypes.add(typeGroup.ensureType(paramClasses[i]));
            }
        } else if (arrayDimCount == 0) {
            // if we have a primitive type then have to have some array dimensions
            throw new TypeException("NewExpression.typeCheck : invalid type for new operation " + getPos());
        }
        // if this is a new array operation we must have at least one defined dimension and we cannot have
        // more dimensions than we can fit into a byte

        if (arrayDimCount > 0 && arrayDimDefinedCount == 0) {
            throw new TypeException("NewExpression.typeCheck : array dimension missing " + getPos());
        }

        if (arrayDimCount > Byte.MAX_VALUE) {
            throw new TypeException("NewExpression.typeCheck : too many array dimensions " + getPos());
        }
        // if we have any array dimension sizings then ensure they all type check as integer expressions

        for (int i = 0; i < arrayDimCount ; i++) {
            if (i < arrayDimDefinedCount) {
                Expression expr = arrayDims.get(i);
                expr.typeCheck(Type.I);
            }
            // replace the current type with the corresponding array type
            type = typeGroup.createArray(type);
        }

        // if the expected type is defined then ensure we can assign this type to it

        if (Type.dereference(expected).isDefined() && !expected.isAssignableFrom(type)) {
View Full Code Here

        this.ruleScript = ruleScript;
        this.helperClass = null;
        this.loader = loader;

        typeGroup = new TypeGroup(loader);
        bindings = new Bindings();
        checked = false;
        triggerClass = null;
        triggerMethod = null;
        triggerDescriptor = null;
View Full Code Here

TOP

Related Classes of org.jboss.byteman.rule.type.TypeGroup

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.