Skip to content

Commit

Permalink
added support for @PackagePrivate
Browse files Browse the repository at this point in the history
added one more test
  • Loading branch information
Michail Plushnikov committed Apr 10, 2016
1 parent 9b8adad commit c65ef86
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 20 deletions.
Expand Up @@ -36,32 +36,35 @@ public boolean isSupported(@NotNull PsiModifierList modifierList, @NotNull Strin
} }


@Override @Override
@SuppressWarnings("unchecked")
public Boolean hasModifierProperty(@NotNull PsiModifierList modifierList, @NotNull String name) { public Boolean hasModifierProperty(@NotNull PsiModifierList modifierList, @NotNull String name) {


/* FINAL */ final PsiModifierListOwner parentElement = PsiTreeUtil.getParentOfType(modifierList, PsiModifierListOwner.class, false);
if (PsiModifier.FINAL.equals(name)) { if (null != parentElement) {
PsiModifierListOwner searchableElement = PsiTreeUtil.getParentOfType(modifierList, PsiModifierListOwner.class, false);
if (null != searchableElement && !PsiAnnotationSearchUtil.isAnnotatedWith(searchableElement, lombok.experimental.NonFinal.class)) {
return Boolean.TRUE;
}


return null; // FINAL
} if (PsiModifier.FINAL.equals(name)) {
if (!PsiAnnotationSearchUtil.isAnnotatedWith(parentElement, lombok.experimental.NonFinal.class)) {
return Boolean.TRUE;
}
}


if (modifierList.getParent() instanceof PsiField && // PRIVATE
// Visibility is only changed for not package private fields
// TODO add support for @PackagePrivate
!(modifierList.hasExplicitModifier(PsiModifier.PUBLIC) ||
modifierList.hasExplicitModifier(PsiModifier.PRIVATE) ||
modifierList.hasExplicitModifier(PsiModifier.PRIVATE))) {
/* PRIVATE */
if (PsiModifier.PRIVATE.equals(name)) { if (PsiModifier.PRIVATE.equals(name)) {
return Boolean.TRUE; if (modifierList.getParent() instanceof PsiField &&
// Visibility is only changed for package private fields
hasPackagePrivateModifier(modifierList) &&
// except they are annotated with @PackagePrivate
!PsiAnnotationSearchUtil.isAnnotatedWith(parentElement, lombok.experimental.PackagePrivate.class)) {
return Boolean.TRUE;
}
} }
} }

// _default_
/* _default_ */
return null; return null;
} }

private boolean hasPackagePrivateModifier(@NotNull PsiModifierList modifierList) {
return !(modifierList.hasExplicitModifier(PsiModifier.PUBLIC) || modifierList.hasExplicitModifier(PsiModifier.PRIVATE) ||
modifierList.hasExplicitModifier(PsiModifier.PROTECTED));
}
} }
Expand Up @@ -54,4 +54,8 @@ protected boolean shouldCompareCodeBlocks() {
public void testValue$ValueWithGeneric176() throws IOException { public void testValue$ValueWithGeneric176() throws IOException {
doTest(true); doTest(true);
} }

public void testValue$ValueWithPackagePrivate() throws IOException {
doTest(true);
}
} }
Expand Up @@ -12,6 +12,6 @@ public class ValueExample {
double score; double score;


public void la() { public void la() {
ValueExample.builder().age(1).build(); // ValueExample.builder().age(1).build();
} }
} }
@@ -0,0 +1,27 @@
package de.plushnikov.value;

import lombok.Value;
import lombok.experimental.NonFinal;
import lombok.experimental.PackagePrivate;

@Value
public class ValueWithPackagePrivate {
private int privateInt;
protected int protectedInt;
public int publicInt;
int anInt;

@PackagePrivate
int annotatedInt;

@NonFinal
int nonFinalInt;

@PackagePrivate
public int shouldBePublicInt;

public static void main(String[] args) {
final ValueWithPackagePrivate test = new ValueWithPackagePrivate(1, 2, 3, 4, 5, 6, 7);
System.out.println(test);
}
}
107 changes: 107 additions & 0 deletions testData/after/value/ValueWithPackagePrivate.java
@@ -0,0 +1,107 @@
public final class ValueWithPackagePrivate {
private final int privateInt;
protected final int protectedInt;
public final int publicInt;
private final int anInt;

final int annotatedInt;

private int nonFinalInt;

public final int shouldBePublicInt;

@java.beans.ConstructorProperties({"privateInt", "protectedInt", "publicInt", "anInt", "annotatedInt", "nonFinalInt", "shouldBePublicInt"})
public ValueWithPackagePrivate(int privateInt, int protectedInt, int publicInt, int anInt, int annotatedInt, int nonFinalInt, int shouldBePublicInt) {
this.privateInt = privateInt;
this.protectedInt = protectedInt;
this.publicInt = publicInt;
this.anInt = anInt;
this.annotatedInt = annotatedInt;
this.nonFinalInt = nonFinalInt;
this.shouldBePublicInt = shouldBePublicInt;
}

public static void main(String[] args) {
final ValueWithPackagePrivate test = new ValueWithPackagePrivate(1, 2, 3, 4, 5, 6, 7);
System.out.println(test);
}

public int getPrivateInt() {
return this.privateInt;
}

public int getProtectedInt() {
return this.protectedInt;
}

public int getPublicInt() {
return this.publicInt;
}

public int getAnInt() {
return this.anInt;
}

public int getAnnotatedInt() {
return this.annotatedInt;
}

public int getNonFinalInt() {
return this.nonFinalInt;
}

public int getShouldBePublicInt() {
return this.shouldBePublicInt;
}

public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof ValueWithPackagePrivate)) {
return false;
}
final ValueWithPackagePrivate other = (ValueWithPackagePrivate) o;
if (this.privateInt != other.privateInt) {
return false;
}
if (this.protectedInt != other.protectedInt) {
return false;
}
if (this.publicInt != other.publicInt) {
return false;
}
if (this.anInt != other.anInt) {
return false;
}
if (this.annotatedInt != other.annotatedInt) {
return false;
}
if (this.nonFinalInt != other.nonFinalInt) {
return false;
}
if (this.shouldBePublicInt != other.shouldBePublicInt) {
return false;
}

return true;
}

public int hashCode() {
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.privateInt;
result = result * PRIME + this.protectedInt;
result = result * PRIME + this.publicInt;
result = result * PRIME + this.anInt;
result = result * PRIME + this.annotatedInt;
result = result * PRIME + this.nonFinalInt;
result = result * PRIME + this.shouldBePublicInt;
return result;
}

public String toString() {
return "ValueWithPackagePrivate(privateInt=" + this.privateInt + ", protectedInt=" + this.protectedInt + ", publicInt=" + this.publicInt
+ ", anInt=" + this.anInt + ", annotatedInt=" + this.annotatedInt + ", nonFinalInt=" + this.nonFinalInt + ", shouldBePublicInt=" + this.shouldBePublicInt +")";
}
}
25 changes: 25 additions & 0 deletions testData/before/value/ValueWithPackagePrivate.java
@@ -0,0 +1,25 @@
import lombok.Value;
import lombok.experimental.NonFinal;
import lombok.experimental.PackagePrivate;

@Value
public class ValueWithPackagePrivate {
private int privateInt;
protected int protectedInt;
public int publicInt;
int anInt;

@PackagePrivate
int annotatedInt;

@NonFinal
int nonFinalInt;

@PackagePrivate
public int shouldBePublicInt;

public static void main(String[] args) {
final ValueWithPackagePrivate test = new ValueWithPackagePrivate(1, 2, 3, 4, 5, 6, 7);
System.out.println(test);
}
}

0 comments on commit c65ef86

Please sign in to comment.