Logo Search packages:      
Sourcecode: janino version File versions  Download package

void org::codehaus::janino::UnitCompiler::checkAccessible ( IClass  iClassDeclaringMember,
Access  memberAccess,
Java.BlockStatement  contextBlockStatement 
) throws CompileException [inline, private]

Verify that a member (class, interface, field or method) declared in a given class is accessible from a given block statement context, according to JLS2 6.6.1.4.

Definition at line 4243 of file UnitCompiler.java.

References org::codehaus::janino::Access::DEFAULT, org::codehaus::janino::IClass::getDeclaringIClass(), org::codehaus::janino::IClass::getDescriptor(), org::codehaus::janino::IClass::isAssignableFrom(), org::codehaus::janino::Access::PRIVATE, and org::codehaus::janino::Access::PUBLIC.

                              {

        // At this point, memberAccess is PUBLIC, DEFAULT, PROECTEDED or PRIVATE.

        // PUBLIC members are always accessible.
        if (memberAccess == Access.PUBLIC) return;

        // At this point, the member is DEFAULT, PROECTEDED or PRIVATE accessible.

        // Determine the class declaring the context block statement.
        IClass iClassDeclaringContextBlockStatement;
        for (Java.Scope s = contextBlockStatement.getEnclosingScope();; s = s.getEnclosingScope()) {
            if (s instanceof Java.TypeDeclaration) {
                iClassDeclaringContextBlockStatement = this.resolve((Java.TypeDeclaration) s);
                break;
            }
        }

        // Access is always allowed for block statements declared in the same class as the member.
        if (iClassDeclaringContextBlockStatement == iClassDeclaringMember) return;

        // Check whether the member and the context block statement are enclosed by the same
        // top-level type.
        {
            IClass topLevelIClassEnclosingMember = iClassDeclaringMember;
                for (IClass c = iClassDeclaringMember.getDeclaringIClass(); c != null; c = c.getDeclaringIClass()) {
                topLevelIClassEnclosingMember = c;
            }
            IClass topLevelIClassEnclosingContextBlockStatement = iClassDeclaringContextBlockStatement;
            for (IClass c = iClassDeclaringContextBlockStatement.getDeclaringIClass(); c != null; c = c.getDeclaringIClass()) {
                topLevelIClassEnclosingContextBlockStatement = c;
            }

            if (topLevelIClassEnclosingMember == topLevelIClassEnclosingContextBlockStatement) return;
        }

        if (memberAccess == Access.PRIVATE) {
            this.compileError("Private member cannot be accessed from type \"" + iClassDeclaringContextBlockStatement + "\".", contextBlockStatement.getLocation());
            return;
        }

        // At this point, the member is DEFAULT or PROTECTED accessible.

        // Check whether the member and the context block statement are declared in the same
        // package.
        if (Descriptor.areInSamePackage(
            iClassDeclaringMember.getDescriptor(),
            iClassDeclaringContextBlockStatement.getDescriptor()
        )) return;

        if (memberAccess == Access.DEFAULT) {
            this.compileError("Member with \"" + memberAccess + "\" access cannot be accessed from type \"" + iClassDeclaringContextBlockStatement + "\".", contextBlockStatement.getLocation());
            return;
        }

        // At this point, the member is PROTECTED accessible.

        // Check whether the class declaring the context block statement is a subclass of the
        // class declaring the member.
        if (!iClassDeclaringMember.isAssignableFrom(iClassDeclaringContextBlockStatement)) {
            this.compileError("Protected member cannot be accessed from type \"" + iClassDeclaringContextBlockStatement + "\", which is neither declared in the same package as or is a subclass of \"" + iClassDeclaringMember + "\".", contextBlockStatement.getLocation());
            return;
        }
        return;
    }


Generated by  Doxygen 1.6.0   Back to index