001 /*
002 * Copyright 2010-2015 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin.descriptors;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
021
022 public abstract class Visibility {
023 private final boolean isPublicAPI;
024 private final String name;
025
026 protected Visibility(@NotNull String name, boolean isPublicAPI) {
027 this.isPublicAPI = isPublicAPI;
028 this.name = name;
029 }
030
031 public boolean isPublicAPI() {
032 return isPublicAPI;
033 }
034
035 /**
036 * True, if it makes sense to check this visibility in imports and not import inaccessible declarations with such visibility.
037 * Hint: return true, if this visibility can be checked on file's level.
038 * Examples:
039 * it returns false for PROTECTED because protected members of classes can be imported to be used in subclasses of their containers,
040 * so when we are looking at the import, we don't know whether it is legal somewhere in this file or not.
041 * it returns true for INTERNAL, because an internal declaration is either visible everywhere in a file, or invisible everywhere in the same file.
042 * it returns true for PRIVATE, because there's no point in importing privates: they are inaccessible unless their short name is
043 * already available without an import
044 */
045 public abstract boolean mustCheckInImports();
046
047 /**
048 * @return null if the answer is unknown
049 */
050 protected Integer compareTo(@NotNull Visibility visibility) {
051 return Visibilities.compareLocal(this, visibility);
052 }
053
054 @Override
055 public String toString() {
056 return name;
057 }
058
059 @NotNull
060 public Visibility normalize() {
061 return this;
062 }
063
064 protected abstract boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from);
065 }