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.load.java;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.kotlin.descriptors.*;
022 import org.jetbrains.kotlin.resolve.DescriptorUtils;
023 import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
024
025 public class JavaVisibilities {
026 private JavaVisibilities() {
027 }
028
029 public static final Visibility PACKAGE_VISIBILITY = new Visibility("package", false) {
030 @Override
031 public boolean isVisible(@Nullable ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) {
032 return areInSamePackage(what, from);
033 }
034
035 @Override
036 public boolean mustCheckInImports() {
037 return true;
038 }
039
040 @Override
041 protected Integer compareTo(@NotNull Visibility visibility) {
042 if (this == visibility) return 0;
043 if (Visibilities.isPrivate(visibility)) return 1;
044 return -1;
045 }
046
047 @NotNull
048 @Override
049 public String getDisplayName() {
050 return "public/*package*/";
051 }
052
053 @NotNull
054 @Override
055 public Visibility normalize() {
056 return Visibilities.PROTECTED;
057 }
058
059 @NotNull
060 @Override
061 public EffectiveVisibility effectiveVisibility(@Nullable ClassDescriptor classDescriptor) {
062 return EffectiveVisibility.PackagePrivate.INSTANCE;
063 }
064 };
065
066 public static final Visibility PROTECTED_STATIC_VISIBILITY = new Visibility("protected_static", true) {
067 @Override
068 public boolean isVisible(@Nullable ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) {
069 return isVisibleForProtectedAndPackage(receiver, what, from);
070 }
071
072 @Override
073 public boolean mustCheckInImports() {
074 return false;
075 }
076
077 @NotNull
078 @Override
079 public String getDisplayName() {
080 return "protected/*protected static*/";
081 }
082
083 @NotNull
084 @Override
085 public Visibility normalize() {
086 return Visibilities.PROTECTED;
087 }
088 };
089
090 public static final Visibility PROTECTED_AND_PACKAGE = new Visibility("protected_and_package", true) {
091 @Override
092 public boolean isVisible(@Nullable ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) {
093 return isVisibleForProtectedAndPackage(receiver, what, from);
094 }
095
096 @Override
097 public boolean mustCheckInImports() {
098 return false;
099 }
100
101 @Override
102 protected Integer compareTo(@NotNull Visibility visibility) {
103 if (this == visibility) return 0;
104 if (visibility == Visibilities.INTERNAL) return null;
105 if (Visibilities.isPrivate(visibility)) return 1;
106 return -1;
107 }
108
109 @NotNull
110 @Override
111 public String getDisplayName() {
112 return "protected/*protected and package*/";
113 }
114
115 @NotNull
116 @Override
117 public Visibility normalize() {
118 return Visibilities.PROTECTED;
119 }
120 };
121
122 private static boolean isVisibleForProtectedAndPackage(
123 @Nullable ReceiverValue receiver,
124 @NotNull DeclarationDescriptorWithVisibility what,
125 @NotNull DeclarationDescriptor from
126 ) {
127 if (areInSamePackage(DescriptorUtils.unwrapFakeOverrideToAnyDeclaration(what), from)) {
128 return true;
129 }
130
131 return Visibilities.PROTECTED.isVisible(receiver, what, from);
132 }
133
134 private static boolean areInSamePackage(@NotNull DeclarationDescriptor first, @NotNull DeclarationDescriptor second) {
135 PackageFragmentDescriptor whatPackage = DescriptorUtils.getParentOfType(first, PackageFragmentDescriptor.class, false);
136 PackageFragmentDescriptor fromPackage = DescriptorUtils.getParentOfType(second, PackageFragmentDescriptor.class, false);
137 return fromPackage != null && whatPackage != null && whatPackage.getFqName().equals(fromPackage.getFqName());
138 }
139 }