Skip to content

Commit

Permalink
GH-184 Simplify Visibility and fix issues with public declarations in…
Browse files Browse the repository at this point in the history
…side package private classes & release 1.14.6
  • Loading branch information
Rollczi authored Jan 12, 2025
1 parent da614dc commit 65e1339
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 54 deletions.
4 changes: 2 additions & 2 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ repositories {
dependencies {
// Default
implementation 'net.dzikoysk:cdn:1.14.5'
implementation 'net.dzikoysk:cdn:1.14.6'
// Kotlin wrapper
implementation 'net.dzikoysk:cdn-kt:1.14.5'
implementation 'net.dzikoysk:cdn-kt:1.14.6'
}
```

Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
`maven-publish`
signing
jacoco
kotlin("jvm") version "1.8.10"
kotlin("jvm") version "2.1.0"
id("io.github.gradle-nexus.publish-plugin") version "2.0.0"
}

Expand All @@ -15,7 +15,7 @@ allprojects {
apply(plugin = "maven-publish")

group = "net.dzikoysk"
version = "1.14.5"
version = "1.14.6"

repositories {
maven("https://maven.reposilite.com/maven-central")
Expand Down
6 changes: 4 additions & 2 deletions cdn-kt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ dependencies {
implementation(kotlin("reflect"))
}

kotlin {
jvmToolchain(8)
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = "1.8"
languageVersion = "1.7"
freeCompilerArgs = listOf(
"-Xjvm-default=all", // For generating default methods in interfaces
// "-Xcontext-receivers"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlin.reflect.full.functions
import kotlin.reflect.full.memberProperties
import kotlin.reflect.jvm.javaField

class KotlinMemberResolver(private val visibilityToMatch: Visibility = Visibility.PRIVATE) : MemberResolver {
class KotlinMemberResolver(private val visibility: Visibility = Visibility.PRIVATE) : MemberResolver {

override fun fromField(type: Class<*>, field: Field): AnnotatedMember =
FieldMember(field, this)
Expand Down Expand Up @@ -40,8 +40,8 @@ class KotlinMemberResolver(private val visibilityToMatch: Visibility = Visibilit
.sortedBy { (_, index) -> index }
.map { (property) -> KPropertyMember(property, this) }

override fun getVisibilityToMatch(): Visibility {
return this.visibilityToMatch
override fun getVisibility(): Visibility {
return this.visibility
}

/**
Expand Down
4 changes: 4 additions & 0 deletions cdn-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ dependencies {
testImplementation("org.yaml:snakeyaml:1.33")
testImplementation("com.google.code.gson:gson:2.10.1")
testImplementation("com.fasterxml.jackson.core:jackson-databind:2.14.0")
}

kotlin {
jvmToolchain(8)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package net.dzikoysk.cdn.feature;

import net.dzikoysk.cdn.Cdn;
import net.dzikoysk.cdn.CdnException;
import net.dzikoysk.cdn.CdnFactory;
import net.dzikoysk.cdn.entity.Exclude;
import net.dzikoysk.cdn.reflect.Visibility;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import panda.std.Result;
import static panda.std.ResultAssertions.assertOk;

class ExcludeJavaTest {

static class ConfigurationWithMultiDescription {

@Exclude
private final static String STATIC_VALUE = "static";

public String value = "test";

}

@Test
@DisplayName("should render value with package-private visibility and exclude static field")
void test() {
Cdn standardJava = CdnFactory.createStandard()
.getSettings()
.withMemberResolver(Visibility.PACKAGE_PRIVATE)
.build();
Result<String, CdnException> renderResult = standardJava.render(new ConfigurationWithMultiDescription());
String render = ResultAssertions.assertOk(renderResult);

Assertions.assertEquals(
"value: test",
render
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public boolean isAnnotationPresent(Class<? extends Annotation> annotation) {
return annotation.isAnnotationPresent(annotation);
}

@Override
public List<AnnotatedMember> getAnnotatedMembers() {
List<AnnotatedMember> members = new ArrayList<>();
members.addAll(resolver.getFields(type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

public class DefaultMemberResolver implements MemberResolver {

private final Visibility visibilityToMatch;
private final Visibility visibility;

public DefaultMemberResolver(Visibility visibilityToMatch) {
this.visibilityToMatch = visibilityToMatch;
public DefaultMemberResolver(Visibility visibility) {
this.visibility = visibility;
}

@Override
Expand Down Expand Up @@ -66,8 +66,8 @@ public List<AnnotatedMember> getProperties(@NotNull Class<?> type) {
}

@Override
public Visibility getVisibilityToMatch() {
return this.visibilityToMatch;
public Visibility getVisibility() {
return this.visibility;
}

}
22 changes: 6 additions & 16 deletions cdn/src/main/java/net/dzikoysk/cdn/reflect/FieldMember.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import panda.std.Blank;
import panda.std.Option;
import panda.std.Result;
import panda.std.function.ThrowingFunction;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedType;
Expand All @@ -35,38 +34,29 @@ public class FieldMember implements AnnotatedMember {

private final Field field;
private final MemberResolver resolver;
private final Visibility visibility;

public FieldMember(Field field, MemberResolver resolver) {
this.field = field;
this.resolver = resolver;
this.visibility = Visibility.forMember(field);
this.field.setAccessible(true);
}

@Override
public boolean isIgnored() {
return CdnUtils.isIgnored(field, this.resolver.getVisibilityToMatch());
return CdnUtils.isIgnored(field, this.resolver.getVisibility());
}

@Override
public Result<Blank, ReflectiveOperationException> setValue(@NotNull Object instance, @NotNull Object value) {
return Result.attempt(ReflectiveOperationException.class, () -> this.onField(accessibleField -> {
accessibleField.set(instance, value);
return Result.attempt(ReflectiveOperationException.class, () -> {
field.set(instance, value);
return BLANK;
}));
});
}

@Override
public Result<Option<Object>, ReflectiveOperationException> getValue(@NotNull Object instance) {
return Result.attempt(ReflectiveOperationException.class, () -> Option.of(this.onField(accessibleField -> accessibleField.get(instance))));
}

private <R> R onField(ThrowingFunction<Field, R, ReflectiveOperationException> action) throws ReflectiveOperationException {
if (!visibility.isAccessible()) {
field.setAccessible(true);
}

return action.apply(field);
return Result.attempt(ReflectiveOperationException.class, () -> Option.of(field.get(instance)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public interface MemberResolver {

List<AnnotatedMember> getProperties(@NotNull Class<?> type);

Visibility getVisibilityToMatch();
Visibility getVisibility();

}
20 changes: 5 additions & 15 deletions cdn/src/main/java/net/dzikoysk/cdn/reflect/Visibility.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,19 @@
import java.util.function.Predicate;

public enum Visibility {
PUBLIC(true, Modifier::isPublic),
PACKAGE_PRIVATE (false, mod -> !Modifier.isPublic(mod) && !Modifier.isProtected(mod) && !Modifier.isPrivate(mod), PUBLIC),
PROTECTED(false, Modifier::isProtected, PACKAGE_PRIVATE, PUBLIC),
PRIVATE(false, Modifier::isPrivate, PROTECTED, PACKAGE_PRIVATE, PUBLIC);
PUBLIC(Modifier::isPublic),
PACKAGE_PRIVATE(mod -> !Modifier.isPublic(mod) && !Modifier.isProtected(mod) && !Modifier.isPrivate(mod), PUBLIC),
PROTECTED(Modifier::isProtected, PACKAGE_PRIVATE, PUBLIC),
PRIVATE(Modifier::isPrivate, PROTECTED, PACKAGE_PRIVATE, PUBLIC);

private final boolean accessible;
private final Visibility[] included;
private final Predicate<Integer> predicate;

private Visibility(boolean accessible, Predicate<Integer> predicate, Visibility... included) {
this.accessible = accessible;
Visibility(Predicate<Integer> predicate, Visibility... included) {
this.included = included;
this.predicate = predicate;
}

public boolean isAccessible() {
return accessible;
}

public boolean isVisible(int modifiers) {
if (predicate.test(modifiers)) {
return true;
Expand All @@ -46,10 +40,6 @@ public boolean isVisible(Class<?> type) {
return this.isVisible(type.getModifiers());
}

public boolean isNotVisible(int modifiers) {
return !this.isVisible(modifiers);
}

public static Visibility forMember(Member member) {
for (Visibility visibility : values()) {
if (visibility.isVisible(member)) {
Expand Down
10 changes: 3 additions & 7 deletions cdn/src/main/java/net/dzikoysk/cdn/serdes/CdnDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,10 @@ private <TYPE> Result<TYPE, ReflectiveOperationException> createInstance(Class<T

private <TYPE> Result<TYPE, ReflectiveOperationException> createInstance(Class<TYPE> type, Class<?>[] argsTypes, Object[] args) {
return Result.attempt(ReflectiveOperationException.class, () -> {
if (!settings.getAnnotationResolver().getVisibilityToMatch().isAccessible()) {
Constructor<TYPE> constructor = type.getDeclaredConstructor(argsTypes);
Constructor<TYPE> constructor = type.getDeclaredConstructor(argsTypes);

constructor.setAccessible(true);
return constructor.newInstance(args);
}

return type.getConstructor(argsTypes).newInstance(args);
constructor.setAccessible(true);
return constructor.newInstance(args);
});
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit 65e1339

Please sign in to comment.