/*
 * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package impl.java.dyn.util;

import impl.java.dyn.MemberName;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

/**
 * This class centralizes information about the JVM's linkage access control.
 * @author jrose
 */
public class VerifyAccess {

    private VerifyAccess() { }  // cannot instantiate

    /**
     * Evaluate the JVM linkage rules for access to the given method on behalf of caller.
     * Return non-null if and only if the given accessing class has at least partial
     * privileges to invoke the given method.  The return value {@code Object.class}
     * denotes unlimited privileges.
     * <p>
     * Some circumstances require an additional check on the
     * leading parameter (the receiver) of the method, if it is non-static.
     * In the case of {@code invokespecial} ({@code doDispatch} is false),
     * the leading parameter must be the accessing class or a subclass.
     * In the case of a call to a {@code protected} method outside the same
     * package, the same constraint applies.
     * @param m the proposed callee
     * @param doDispatch if false, a non-static m will be invoked as if by {@code invokespecial}
     * @param accessingClass the class for which the access check is being made
     * @return null if the method is not accessible, else a receiver type constraint, else {@code Object.class}
     */
    public static Class<?> isAccessible(Class<?> defc, int mods,
            boolean doDispatch, Class<?> accessingClass) {
        if (!isAccessible(defc, accessingClass))
            return null;
        Class<?> constraint = Object.class;
        if (!doDispatch && !Modifier.isStatic(mods)) {
            constraint = accessingClass;
        }
        if (Modifier.isPublic(mods))
            return constraint;
        if (Modifier.isPrivate(mods))
            return (defc == accessingClass) ? constraint : null;
        if (isSamePackage(defc, accessingClass))
            return constraint;
        if (Modifier.isProtected(mods) && defc.isAssignableFrom(accessingClass))
            return constraint;
        // else it is private or package scoped, and not close enough
        return null;
    }

    /**
     * Evaluate the JVM linkage rules for access to the given class on behalf of caller.
     */
    public static boolean isAccessible(Class<?> refc, Class<?> accessingClass) {
        int mods = refc.getModifiers();
        if (Modifier.isPublic(mods))
            return true;
        if (isSamePackage(accessingClass, refc))
            return true;
        return false;
    }

    /**
     * Test if two classes have the same class loader and package qualifier.
     * @param class1
     * @param class2
     * @return whether they are in the same package
     */
    public static boolean isSamePackage(Class<?> class1, Class<?> class2) {
        if (class1 == class2)
            return true;
        if (class1.getClassLoader() != class2.getClassLoader())
            return false;
        String name1 = class1.getName(), name2 = class2.getName();
        int dot = name1.lastIndexOf('.');
        if (dot != name2.lastIndexOf('.'))
            return false;
        for (int i = 0; i < dot; i++) {
            if (name1.charAt(i) != name2.charAt(i))
                return false;
        }
        return true;
    }

    /**
     * Test if two classes are defined as part of the same package member (top-level class).
     * If this is true, they can share private access with each other.
     * @param class1
     * @param class2
     * @return whether they are identical or nested together
     */
    public static boolean isSamePackageMember(Class<?> class1, Class<?> class2) {
        if (class1 == class2)
            return true;
        if (!isSamePackage(class1, class2))
            return false;
        if (getOutermostEnclosingClass(class1) != getOutermostEnclosingClass(class2))
            return false;
        return true;
    }

    private static Class<?> getOutermostEnclosingClass(Class<?> c) {
        Class<?> pkgmem = c;
        for (Class<?> enc = c; (enc = enc.getEnclosingClass()) != null; )
            pkgmem = enc;
        return pkgmem;
    }
}
