Move java visibilities to JavaVisibilities class
Move related utilities to where they're used
This commit is contained in:
committed by
Pavel V. Talanov
parent
e1a10eb419
commit
85e7b87ccd
+3
-11
@@ -94,11 +94,11 @@ public final class DescriptorResolverUtils {
|
||||
}
|
||||
if (modifierListOwner.hasModifierProperty(PsiModifier.PROTECTED)) {
|
||||
if (modifierListOwner.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
return JavaDescriptorResolver.PROTECTED_STATIC_VISIBILITY;
|
||||
return JavaVisibilities.PROTECTED_STATIC_VISIBILITY;
|
||||
}
|
||||
return JavaDescriptorResolver.PROTECTED_AND_PACKAGE;
|
||||
return JavaVisibilities.PROTECTED_AND_PACKAGE;
|
||||
}
|
||||
return JavaDescriptorResolver.PACKAGE_VISIBILITY;
|
||||
return JavaVisibilities.PACKAGE_VISIBILITY;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -119,14 +119,6 @@ public final class DescriptorResolverUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Visibility getConstructorVisibility(ClassDescriptor classDescriptor) {
|
||||
Visibility containingClassVisibility = classDescriptor.getVisibility();
|
||||
if (containingClassVisibility == JavaDescriptorResolver.PROTECTED_STATIC_VISIBILITY) {
|
||||
return JavaDescriptorResolver.PROTECTED_AND_PACKAGE;
|
||||
}
|
||||
return containingClassVisibility;
|
||||
}
|
||||
|
||||
public static void checkPsiClassIsNotJet(@Nullable PsiClass psiClass) {
|
||||
if (psiClass instanceof JetJavaMirrorMarker) {
|
||||
throw new IllegalStateException("trying to resolve fake jet PsiClass as regular PsiClass: " + psiClass.getQualifiedName());
|
||||
|
||||
-105
@@ -20,7 +20,6 @@ import com.intellij.psi.PsiClass;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||
import org.jetbrains.jet.lang.resolve.java.provider.NamedMembers;
|
||||
import org.jetbrains.jet.lang.resolve.java.resolver.*;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
@@ -38,110 +37,6 @@ public class JavaDescriptorResolver implements DependencyClassByQualifiedNameRes
|
||||
|
||||
public static final Name JAVA_ROOT = Name.special("<java_root>");
|
||||
|
||||
public static final Visibility PACKAGE_VISIBILITY = new Visibility("package", false) {
|
||||
@Override
|
||||
protected boolean isVisible(@NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) {
|
||||
return DescriptorUtils.isInSameNamespace(what, from);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Integer compareTo(@NotNull Visibility visibility) {
|
||||
if (this == visibility) return 0;
|
||||
if (visibility == Visibilities.PRIVATE) return 1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "public/*package*/";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Visibility normalize() {
|
||||
return Visibilities.INTERNAL;
|
||||
}
|
||||
};
|
||||
|
||||
public static final Visibility PROTECTED_STATIC_VISIBILITY = new Visibility("protected_static", false) {
|
||||
@Override
|
||||
protected boolean isVisible(@NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) {
|
||||
ClassDescriptor fromClass = DescriptorUtils.getParentOfType(from, ClassDescriptor.class, false);
|
||||
if (fromClass == null) return false;
|
||||
|
||||
ClassDescriptor whatClass;
|
||||
// protected static class
|
||||
if (what instanceof ClassDescriptor) {
|
||||
DeclarationDescriptor containingDeclaration = what.getContainingDeclaration();
|
||||
assert containingDeclaration instanceof ClassDescriptor : "Only static nested classes can have protected_static visibility";
|
||||
whatClass = (ClassDescriptor) containingDeclaration;
|
||||
}
|
||||
// protected static function or property
|
||||
else {
|
||||
DeclarationDescriptor whatDeclarationDescriptor = what.getContainingDeclaration();
|
||||
assert whatDeclarationDescriptor instanceof NamespaceDescriptor : "Only static declarations can have protected_static visibility";
|
||||
whatClass = DescriptorUtils.getClassForCorrespondingJavaNamespace((NamespaceDescriptor) whatDeclarationDescriptor);
|
||||
}
|
||||
|
||||
assert whatClass != null : "Couldn't find ClassDescriptor for protected static member " + what;
|
||||
|
||||
if (DescriptorUtils.isSubclass(fromClass, whatClass)) {
|
||||
return true;
|
||||
}
|
||||
return isVisible(what, fromClass.getContainingDeclaration());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "protected/*protected static*/";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Visibility normalize() {
|
||||
return Visibilities.PROTECTED;
|
||||
}
|
||||
};
|
||||
|
||||
public static final Visibility PROTECTED_AND_PACKAGE = new Visibility("protected_and_package", false) {
|
||||
@Override
|
||||
protected boolean isVisible(@NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) {
|
||||
if (DescriptorUtils.isInSameNamespace(what, from)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
ClassDescriptor whatClass = DescriptorUtils.getParentOfType(what, ClassDescriptor.class, false);
|
||||
if (whatClass == null) return false;
|
||||
|
||||
ClassDescriptor fromClass = DescriptorUtils.getParentOfType(from, ClassDescriptor.class, false);
|
||||
if (fromClass == null) return false;
|
||||
|
||||
if (DescriptorUtils.isSubclass(fromClass, whatClass)) {
|
||||
return true;
|
||||
}
|
||||
return isVisible(what, fromClass.getContainingDeclaration());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Integer compareTo(@NotNull Visibility visibility) {
|
||||
if (this == visibility) return 0;
|
||||
if (visibility == Visibilities.INTERNAL) return null;
|
||||
if (visibility == Visibilities.PRIVATE) return 1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "protected/*protected and package*/";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Visibility normalize() {
|
||||
return Visibilities.PROTECTED;
|
||||
}
|
||||
};
|
||||
|
||||
private JavaPropertyResolver propertiesResolver;
|
||||
private JavaClassResolver classResolver;
|
||||
private JavaConstructorResolver constructorResolver;
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.lang.resolve.java;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.descriptors.impl.NamespaceDescriptorParent;
|
||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||
|
||||
public class JavaVisibilities {
|
||||
private JavaVisibilities() {
|
||||
}
|
||||
|
||||
public static final Visibility PACKAGE_VISIBILITY = new Visibility("package", false) {
|
||||
@Override
|
||||
protected boolean isVisible(@NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) {
|
||||
return isInSameNamespace(what, from);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Integer compareTo(@NotNull Visibility visibility) {
|
||||
if (this == visibility) return 0;
|
||||
if (visibility == Visibilities.PRIVATE) return 1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "public/*package*/";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Visibility normalize() {
|
||||
return Visibilities.INTERNAL;
|
||||
}
|
||||
};
|
||||
|
||||
public static final Visibility PROTECTED_STATIC_VISIBILITY = new Visibility("protected_static", false) {
|
||||
@Override
|
||||
protected boolean isVisible(@NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) {
|
||||
ClassDescriptor fromClass = DescriptorUtils.getParentOfType(from, ClassDescriptor.class, false);
|
||||
if (fromClass == null) return false;
|
||||
|
||||
ClassDescriptor whatClass;
|
||||
// protected static class
|
||||
if (what instanceof ClassDescriptor) {
|
||||
DeclarationDescriptor containingDeclaration = what.getContainingDeclaration();
|
||||
assert containingDeclaration instanceof ClassDescriptor : "Only static nested classes can have protected_static visibility";
|
||||
whatClass = (ClassDescriptor) containingDeclaration;
|
||||
}
|
||||
// protected static function or property
|
||||
else {
|
||||
DeclarationDescriptor whatDeclarationDescriptor = what.getContainingDeclaration();
|
||||
assert whatDeclarationDescriptor instanceof NamespaceDescriptor : "Only static declarations can have protected_static visibility";
|
||||
whatClass = getClassForCorrespondingJavaNamespace((NamespaceDescriptor) whatDeclarationDescriptor);
|
||||
}
|
||||
|
||||
assert whatClass != null : "Couldn't find ClassDescriptor for protected static member " + what;
|
||||
|
||||
if (DescriptorUtils.isSubclass(fromClass, whatClass)) {
|
||||
return true;
|
||||
}
|
||||
return isVisible(what, fromClass.getContainingDeclaration());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "protected/*protected static*/";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Visibility normalize() {
|
||||
return Visibilities.PROTECTED;
|
||||
}
|
||||
};
|
||||
|
||||
public static final Visibility PROTECTED_AND_PACKAGE = new Visibility("protected_and_package", false) {
|
||||
@Override
|
||||
protected boolean isVisible(@NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) {
|
||||
if (isInSameNamespace(what, from)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
ClassDescriptor whatClass = DescriptorUtils.getParentOfType(what, ClassDescriptor.class, false);
|
||||
if (whatClass == null) return false;
|
||||
|
||||
ClassDescriptor fromClass = DescriptorUtils.getParentOfType(from, ClassDescriptor.class, false);
|
||||
if (fromClass == null) return false;
|
||||
|
||||
if (DescriptorUtils.isSubclass(fromClass, whatClass)) {
|
||||
return true;
|
||||
}
|
||||
return isVisible(what, fromClass.getContainingDeclaration());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Integer compareTo(@NotNull Visibility visibility) {
|
||||
if (this == visibility) return 0;
|
||||
if (visibility == Visibilities.INTERNAL) return null;
|
||||
if (visibility == Visibilities.PRIVATE) return 1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "protected/*protected and package*/";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Visibility normalize() {
|
||||
return Visibilities.PROTECTED;
|
||||
}
|
||||
};
|
||||
|
||||
private static boolean isInSameNamespace(@NotNull DeclarationDescriptor first, @NotNull DeclarationDescriptor second) {
|
||||
NamespaceDescriptor whatPackage = DescriptorUtils.getParentOfType(first, NamespaceDescriptor.class, false);
|
||||
NamespaceDescriptor fromPackage = DescriptorUtils.getParentOfType(second, NamespaceDescriptor.class, false);
|
||||
return fromPackage != null && whatPackage != null && whatPackage.equals(fromPackage);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static ClassDescriptor getClassForCorrespondingJavaNamespace(@NotNull NamespaceDescriptor correspondingNamespace) {
|
||||
NamespaceDescriptorParent containingDeclaration = correspondingNamespace.getContainingDeclaration();
|
||||
if (!(containingDeclaration instanceof NamespaceDescriptor)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
NamespaceDescriptor namespaceDescriptor = (NamespaceDescriptor) containingDeclaration;
|
||||
|
||||
ClassifierDescriptor classDescriptor = namespaceDescriptor.getMemberScope().getClassifier(correspondingNamespace.getName());
|
||||
if (classDescriptor != null && classDescriptor instanceof ClassDescriptor) {
|
||||
return (ClassDescriptor) classDescriptor;
|
||||
}
|
||||
|
||||
ClassDescriptor classDescriptorForOuterClass = getClassForCorrespondingJavaNamespace(namespaceDescriptor);
|
||||
if (classDescriptorForOuterClass == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ClassifierDescriptor innerClassDescriptor =
|
||||
classDescriptorForOuterClass.getUnsubstitutedInnerClassesScope().getClassifier(correspondingNamespace.getName());
|
||||
if (innerClassDescriptor instanceof ClassDescriptor) {
|
||||
return (ClassDescriptor) innerClassDescriptor;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+10
-1
@@ -86,7 +86,7 @@ public final class JavaConstructorResolver {
|
||||
constructors.add(trace.get(BindingContext.CONSTRUCTOR, psiClass));
|
||||
}
|
||||
else {
|
||||
Visibility constructorVisibility = DescriptorResolverUtils.getConstructorVisibility(containingClass);
|
||||
Visibility constructorVisibility = getConstructorVisibility(containingClass);
|
||||
// We need to create default constructors for classes and abstract classes.
|
||||
// Example:
|
||||
// class Kotlin() : Java() {}
|
||||
@@ -161,6 +161,15 @@ public final class JavaConstructorResolver {
|
||||
return constructors;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Visibility getConstructorVisibility(@NotNull ClassDescriptor classDescriptor) {
|
||||
Visibility visibility = classDescriptor.getVisibility();
|
||||
if (visibility == JavaVisibilities.PROTECTED_STATIC_VISIBILITY) {
|
||||
return JavaVisibilities.PROTECTED_AND_PACKAGE;
|
||||
}
|
||||
return visibility;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private ConstructorDescriptor resolveConstructor(
|
||||
PsiClass psiClass,
|
||||
|
||||
Reference in New Issue
Block a user