Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isVisibleFrom and isVisibleFromSubclass to Visibility #227

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion common/src/main/java/com/google/auto/common/Visibility.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,43 @@ public static Visibility effectiveVisibilityOfElement(Element element) {
}
return effectiveVisibility;
}
}

/**
* Returns whether the given element is visible from another element in the given package.
*/
public static boolean isVisibleFrom(Element element, PackageElement from) {
switch (effectiveVisibilityOfElement(element)) {
case PUBLIC:
return true;
case PROTECTED:
case DEFAULT:
return MoreElements.getPackage(element).equals(from);
case PRIVATE:
return false;
default:
throw new AssertionError();
}
}

/**
* Returns whether the given element is visible from another element in the given package,
* assuming that other element is a subclass (or a member of a subclass) of the former (or
* its enclosing type).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being a subclass doesn't guarantee protected access. If the element being accessed is a constructor or an instance method, it is only accessible from within the same instance. I'm not sure how to capture that here. Maybe just give in and actually mention protected.

*
* <p>This is similar to {@link #isVisibleFrom(Element, PackageElement)} except for the
* treatment of {@code protected} elements.
*/
public static boolean isVisibleFromSubclass(Element element, PackageElement from) {
switch (effectiveVisibilityOfElement(element)) {
case PUBLIC:
case PROTECTED:
return true;
case DEFAULT:
return MoreElements.getPackage(element).equals(from);
case PRIVATE:
return false;
default:
throw new AssertionError();
}
}
}
123 changes: 123 additions & 0 deletions common/src/test/java/com/google/auto/common/VisibilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.auto.common;

import com.google.testing.compile.CompilationRule;
import javax.lang.model.element.PackageElement;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -26,6 +27,8 @@
import static com.google.auto.common.Visibility.PROTECTED;
import static com.google.auto.common.Visibility.PUBLIC;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

@RunWith(JUnit4.class)
public class VisibilityTest {
Expand Down Expand Up @@ -141,4 +144,124 @@ private Visibility effectiveVisiblityOfClass(Class<?> clazz) {
return Visibility.effectiveVisibilityOfElement(
compilation.getElements().getTypeElement(clazz.getCanonicalName()));
}

@Test
public void visibilityFrom() {
PackageElement samePackage = compilation.getElements().getPackageElement("com.google.auto.common");
assertTrue(isVisibleFromClass(PublicClass.class, samePackage));
assertTrue(isVisibleFromClass(ProtectedClass.class, samePackage));
assertTrue(isVisibleFromClass(DefaultClass.class, samePackage));
assertFalse(isVisibleFromClass(PrivateClass.class, samePackage));

assertTrue(isVisibleFromClass(PublicClass.NestedPublicClass.class, samePackage));
assertTrue(isVisibleFromClass(PublicClass.NestedProtectedClass.class, samePackage));
assertTrue(isVisibleFromClass(PublicClass.NestedDefaultClass.class, samePackage));
assertFalse(isVisibleFromClass(PublicClass.NestedPrivateClass.class, samePackage));

assertTrue(isVisibleFromClass(ProtectedClass.NestedPublicClass.class, samePackage));
assertTrue(isVisibleFromClass(ProtectedClass.NestedProtectedClass.class, samePackage));
assertTrue(isVisibleFromClass(ProtectedClass.NestedDefaultClass.class, samePackage));
assertFalse(isVisibleFromClass(ProtectedClass.NestedPrivateClass.class, samePackage));

assertTrue(isVisibleFromClass(DefaultClass.NestedPublicClass.class, samePackage));
assertTrue(isVisibleFromClass(DefaultClass.NestedProtectedClass.class, samePackage));
assertTrue(isVisibleFromClass(DefaultClass.NestedDefaultClass.class, samePackage));
assertFalse(isVisibleFromClass(DefaultClass.NestedPrivateClass.class, samePackage));

assertFalse(isVisibleFromClass(PrivateClass.NestedPublicClass.class, samePackage));
assertFalse(isVisibleFromClass(PrivateClass.NestedProtectedClass.class, samePackage));
assertFalse(isVisibleFromClass(PrivateClass.NestedDefaultClass.class, samePackage));
assertFalse(isVisibleFromClass(PrivateClass.NestedPrivateClass.class, samePackage));

PackageElement otherPackage = compilation.getElements().getPackageElement("com.example");
assertTrue(isVisibleFromClass(PublicClass.class, otherPackage));
assertFalse(isVisibleFromClass(ProtectedClass.class, otherPackage));
assertFalse(isVisibleFromClass(DefaultClass.class, otherPackage));
assertFalse(isVisibleFromClass(PrivateClass.class, otherPackage));

assertTrue(isVisibleFromClass(PublicClass.NestedPublicClass.class, otherPackage));
assertFalse(isVisibleFromClass(PublicClass.NestedProtectedClass.class, otherPackage));
assertFalse(isVisibleFromClass(PublicClass.NestedDefaultClass.class, otherPackage));
assertFalse(isVisibleFromClass(PublicClass.NestedPrivateClass.class, otherPackage));

assertFalse(isVisibleFromClass(ProtectedClass.NestedPublicClass.class, otherPackage));
assertFalse(isVisibleFromClass(ProtectedClass.NestedProtectedClass.class, otherPackage));
assertFalse(isVisibleFromClass(ProtectedClass.NestedDefaultClass.class, otherPackage));
assertFalse(isVisibleFromClass(ProtectedClass.NestedPrivateClass.class, otherPackage));

assertFalse(isVisibleFromClass(DefaultClass.NestedPublicClass.class, otherPackage));
assertFalse(isVisibleFromClass(DefaultClass.NestedProtectedClass.class, otherPackage));
assertFalse(isVisibleFromClass(DefaultClass.NestedDefaultClass.class, otherPackage));
assertFalse(isVisibleFromClass(DefaultClass.NestedPrivateClass.class, otherPackage));

assertFalse(isVisibleFromClass(PrivateClass.NestedPublicClass.class, otherPackage));
assertFalse(isVisibleFromClass(PrivateClass.NestedProtectedClass.class, otherPackage));
assertFalse(isVisibleFromClass(PrivateClass.NestedDefaultClass.class, otherPackage));
assertFalse(isVisibleFromClass(PrivateClass.NestedPrivateClass.class, otherPackage));
}

private boolean isVisibleFromClass(Class<?> clazz, PackageElement from) {
return Visibility.isVisibleFrom(
compilation.getElements().getTypeElement(clazz.getCanonicalName()), from);
}

@Test
public void visibilityFromSubclass() {
PackageElement samePackage = compilation.getElements().getPackageElement("com.google.auto.common");
assertTrue(isVisibleFromSubclass(PublicClass.class, samePackage));
assertTrue(isVisibleFromSubclass(ProtectedClass.class, samePackage));
assertTrue(isVisibleFromSubclass(DefaultClass.class, samePackage));
assertFalse(isVisibleFromSubclass(PrivateClass.class, samePackage));

assertTrue(isVisibleFromSubclass(PublicClass.NestedPublicClass.class, samePackage));
assertTrue(isVisibleFromSubclass(PublicClass.NestedProtectedClass.class, samePackage));
assertTrue(isVisibleFromSubclass(PublicClass.NestedDefaultClass.class, samePackage));
assertFalse(isVisibleFromSubclass(PublicClass.NestedPrivateClass.class, samePackage));

assertTrue(isVisibleFromSubclass(ProtectedClass.NestedPublicClass.class, samePackage));
assertTrue(isVisibleFromSubclass(ProtectedClass.NestedProtectedClass.class, samePackage));
assertTrue(isVisibleFromSubclass(ProtectedClass.NestedDefaultClass.class, samePackage));
assertFalse(isVisibleFromSubclass(ProtectedClass.NestedPrivateClass.class, samePackage));

assertTrue(isVisibleFromSubclass(DefaultClass.NestedPublicClass.class, samePackage));
assertTrue(isVisibleFromSubclass(DefaultClass.NestedProtectedClass.class, samePackage));
assertTrue(isVisibleFromSubclass(DefaultClass.NestedDefaultClass.class, samePackage));
assertFalse(isVisibleFromSubclass(DefaultClass.NestedPrivateClass.class, samePackage));

assertFalse(isVisibleFromSubclass(PrivateClass.NestedPublicClass.class, samePackage));
assertFalse(isVisibleFromSubclass(PrivateClass.NestedProtectedClass.class, samePackage));
assertFalse(isVisibleFromSubclass(PrivateClass.NestedDefaultClass.class, samePackage));
assertFalse(isVisibleFromSubclass(PrivateClass.NestedPrivateClass.class, samePackage));

PackageElement otherPackage = compilation.getElements().getPackageElement("com.example");
assertTrue(isVisibleFromSubclass(PublicClass.class, otherPackage));
assertTrue(isVisibleFromSubclass(ProtectedClass.class, otherPackage));
assertFalse(isVisibleFromSubclass(DefaultClass.class, otherPackage));
assertFalse(isVisibleFromSubclass(PrivateClass.class, otherPackage));

assertTrue(isVisibleFromSubclass(PublicClass.NestedPublicClass.class, otherPackage));
assertTrue(isVisibleFromSubclass(PublicClass.NestedProtectedClass.class, otherPackage));
assertFalse(isVisibleFromSubclass(PublicClass.NestedDefaultClass.class, otherPackage));
assertFalse(isVisibleFromSubclass(PublicClass.NestedPrivateClass.class, otherPackage));

assertTrue(isVisibleFromSubclass(ProtectedClass.NestedPublicClass.class, otherPackage));
assertTrue(isVisibleFromSubclass(ProtectedClass.NestedProtectedClass.class, otherPackage));
assertFalse(isVisibleFromSubclass(ProtectedClass.NestedDefaultClass.class, otherPackage));
assertFalse(isVisibleFromSubclass(ProtectedClass.NestedPrivateClass.class, otherPackage));

assertFalse(isVisibleFromSubclass(DefaultClass.NestedPublicClass.class, otherPackage));
assertFalse(isVisibleFromSubclass(DefaultClass.NestedProtectedClass.class, otherPackage));
assertFalse(isVisibleFromSubclass(DefaultClass.NestedDefaultClass.class, otherPackage));
assertFalse(isVisibleFromSubclass(DefaultClass.NestedPrivateClass.class, otherPackage));

assertFalse(isVisibleFromSubclass(PrivateClass.NestedPublicClass.class, otherPackage));
assertFalse(isVisibleFromSubclass(PrivateClass.NestedProtectedClass.class, otherPackage));
assertFalse(isVisibleFromSubclass(PrivateClass.NestedDefaultClass.class, otherPackage));
assertFalse(isVisibleFromSubclass(PrivateClass.NestedPrivateClass.class, otherPackage));
}

private boolean isVisibleFromSubclass(Class<?> clazz, PackageElement from) {
return Visibility.isVisibleFromSubclass(
compilation.getElements().getTypeElement(clazz.getCanonicalName()), from);
}
}