001    /*
002     * Copyright (C) 2014 XStream Committers.
003     * All rights reserved.
004     *
005     * Created on 09. January 2014 by Joerg Schaible
006     */
007    package com.thoughtworks.xstream.security;
008    
009    /**
010     * Wrapper to negate another type permission.
011     * <p>
012     * If the wrapped {@link TypePermission} allows the type, this instance will throw a {@link ForbiddenClassException}
013     * instead. An instance of this permission cannot be used to allow a type.
014     * </p>
015     * 
016     * @author J&ouml;rg Schaible
017     * @since 1.4.7
018     */
019    public class NoPermission implements TypePermission {
020    
021        private final TypePermission permission;
022    
023        /**
024         * Construct a NoPermission.
025         * 
026         * @param permission the permission to negate or <code>null</code> to forbid any type
027         * @since 1.4.7
028         */
029        public NoPermission(final TypePermission permission) {
030            this.permission = permission;
031        }
032    
033        public boolean allows(final Class type) {
034            if (permission == null || permission.allows(type)) {
035                throw new ForbiddenClassException(type);
036            }
037            return false;
038        }
039    }