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