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